From 26b12d9c396d1b93ff547ea24fff08750e0762b5 Mon Sep 17 00:00:00 2001
From: vindarel <vindarel@mailz.org>
Date: Sun, 24 Mar 2019 17:23:24 +0100
Subject: [PATCH] [feat] add -h/--human parameter to dbex members

to print timestamps as a date.

this was actually coded by @librelois in a live session !
---
 lib/core/core/cli/dbex.rs                     |  3 +++
 lib/core/core/lib.rs                          |  5 ++++-
 lib/modules/blockchain/blockchain/Cargo.toml  |  1 +
 lib/modules/blockchain/blockchain/src/dbex.rs | 16 ++++++++++++----
 4 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/lib/core/core/cli/dbex.rs b/lib/core/core/cli/dbex.rs
index a7e91a60..12fb9196 100644
--- a/lib/core/core/cli/dbex.rs
+++ b/lib/core/core/cli/dbex.rs
@@ -76,6 +76,9 @@ pub struct MembersOpt {
     #[structopt(short = "e", long = "expire")]
     /// show members expire date
     pub expire: bool,
+    /// human readable output
+    #[structopt(short = "h", long = "human")]
+    pub human_readable: bool,
 }
 
 #[derive(StructOpt, Debug, Clone)]
diff --git a/lib/core/core/lib.rs b/lib/core/core/lib.rs
index f97e6a97..7e3ed04b 100644
--- a/lib/core/core/lib.rs
+++ b/lib/core/core/lib.rs
@@ -322,7 +322,10 @@ impl<'a, 'b: 'a> DuniterCore<'b, 'a, DuRsConf> {
                             profile.as_str(),
                             &conf,
                             opts.csv,
-                            &DBExQuery::WotQuery(DBExWotQuery::ExpireMembers(members_opts.reverse)),
+                            &DBExQuery::WotQuery(DBExWotQuery::ExpireMembers(
+                                members_opts.reverse,
+                                members_opts.human_readable,
+                            )),
                         );
                     } else {
                         dbex(
diff --git a/lib/modules/blockchain/blockchain/Cargo.toml b/lib/modules/blockchain/blockchain/Cargo.toml
index 5cfd27b8..1a727f45 100644
--- a/lib/modules/blockchain/blockchain/Cargo.toml
+++ b/lib/modules/blockchain/blockchain/Cargo.toml
@@ -10,6 +10,7 @@ edition = "2018"
 path = "src/lib.rs"
 
 [dependencies]
+chrono = "0.4.6"
 dirs = "1.0.2"
 duniter-conf = { path = "../../../core/conf" }
 dup-crypto = { path = "../../../tools/crypto" }
diff --git a/lib/modules/blockchain/blockchain/src/dbex.rs b/lib/modules/blockchain/blockchain/src/dbex.rs
index 6cfca059..9eb082e0 100644
--- a/lib/modules/blockchain/blockchain/src/dbex.rs
+++ b/lib/modules/blockchain/blockchain/src/dbex.rs
@@ -14,6 +14,8 @@
 // along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
 use crate::*;
+
+use chrono::prelude::*;
 use dubp_documents::documents::transaction::*;
 use duniter_module::DuniterConf;
 use dup_crypto::keys::*;
@@ -28,7 +30,7 @@ pub enum DBExWotQuery {
     /// Ask distance of all members
     AllDistances(bool),
     /// Show members expire date
-    ExpireMembers(bool),
+    ExpireMembers(bool, bool),
     /// Show members list
     ListMembers(bool),
     /// Ask member datas
@@ -228,7 +230,7 @@ pub fn dbex_wot<DC: DuniterConf>(profile: &str, conf: &DC, csv: bool, query: &DB
                 compute_distances_duration.subsec_millis()
             );
         }
-        DBExWotQuery::ExpireMembers(ref reverse) => {
+        DBExWotQuery::ExpireMembers(reverse, human_readable) => {
             // Open blockchain database
             let blockchain_db = open_file_db::<LocalBlockchainV10Datas>(&db_path, "blockchain.db")
                 .expect("Fail to open blockchain db");
@@ -263,13 +265,19 @@ pub fn dbex_wot<DC: DuniterConf>(profile: &str, conf: &DC, csv: bool, query: &DB
                     expire_dates
                 })
                 .expect("Fail to read ms db");
-            if *reverse {
+            if reverse {
                 expire_dates.sort_unstable_by(|(_, d1), (_, d2)| d1.cmp(&d2));
             } else {
                 expire_dates.sort_unstable_by(|(_, d1), (_, d2)| d2.cmp(&d1));
             }
             for (node_id, expire_date) in expire_dates {
-                println!("{}, {}", wot_uid_index[&node_id], expire_date);
+                if human_readable {
+                    let datetime = NaiveDateTime::from_timestamp(expire_date as i64, 0);
+                    let human_expire_date = datetime.format("%Y-%m-%d %H:%M:%S UTC");
+                    println!("{}, {}", wot_uid_index[&node_id], human_expire_date);
+                } else {
+                    println!("{}, {}", wot_uid_index[&node_id], expire_date);
+                }
             }
         }
         DBExWotQuery::MemberDatas(ref uid) => {
-- 
GitLab