From f9f3fa521254dfeafe24e301d1818eb92c3dba74 Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Mon, 26 Aug 2024 11:29:17 -0600 Subject: [PATCH] precision also affects displaying timestamp on log --- src/main.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5beda5d..54e2dd2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -90,7 +90,7 @@ fn log(f: &String, enabled: &bool, since: &Option, verbose: &bool, preci } let since = parse_time(since)?; if *verbose { - eprintln!("since = {} ({})", system_time_to_unix_seconds(&since), timestamp(&system_time_to_unix_seconds(&since))); + eprintln!("since = {} ({})", system_time_to_unix_seconds(&since), timestamp(&system_time_to_unix_seconds(&since), &2)); } let tsheet = load(&f)?; @@ -105,9 +105,9 @@ fn log(f: &String, enabled: &bool, since: &Option, verbose: &bool, preci for i in 0..tsheet.xs.len() { let x = &tsheet.xs[i]; if *verbose { - eprintln!("{} != {}?", &curr.t, x.timestamp()); + eprintln!("{} != {}?", &curr.t, x.timestamp(&precision)); } - if curr.t != x.timestamp() { + if curr.t != x.timestamp(&precision) { if curr.xs.len() > 0 { if *verbose { eprintln!("push {:?}", &curr.xs); @@ -115,7 +115,7 @@ fn log(f: &String, enabled: &bool, since: &Option, verbose: &bool, preci result.push(curr.clone()); } curr.xs.truncate(0); - curr.t = x.timestamp(); + curr.t = x.timestamp(&precision); curr.d = 0.0; } let mut d = 1.0; @@ -130,7 +130,7 @@ fn log(f: &String, enabled: &bool, since: &Option, verbose: &bool, preci match x.x.len() { 0 => {}, _ => { - curr.t = x.timestamp(); + curr.t = x.timestamp(&precision); curr.xs.push(LogX{d: d, x: x.x.clone()}); }, }; @@ -339,8 +339,8 @@ impl X { UNIX_EPOCH.add(Duration::from_secs(self.t.try_into().unwrap())) } - fn timestamp(&self) -> String { - timestamp(&self.t) + fn timestamp(&self, precision: &u32) -> String { + timestamp(&self.t, &precision) } } @@ -348,9 +348,13 @@ fn system_time_to_unix_seconds(st: &SystemTime) -> i64 { st.duration_since(UNIX_EPOCH).unwrap().as_secs() as i64 } -fn timestamp(t: &i64) -> String { +fn timestamp(t: &i64, precision: &u32) -> String { let dt = Local.timestamp_opt(*t, 0).unwrap(); - dt.format("%Y-%m-%d").to_string() + match precision { + 0 => dt.format("%Y-%m-%d").to_string(), + 1 => dt.format("%Y-%m-%dT%H:%M").to_string(), + _ => dt.format("%Y-%m-%dT%H:%M:%S").to_string(), + } } fn new_x(t: i64, x: String, tag: String) -> X {