From dd62aaad99553ac8f32070c49a08b6ce52b2b5e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christina=20S=C3=B8rensen?= Date: Fri, 10 May 2024 13:27:43 +0200 Subject: [PATCH] feat: new parse formats MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christina Sørensen --- src/main.rs | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3143b77..7ffee6d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,12 +13,44 @@ fn main() -> io::Result<()> { let buffer = &args[1]; + //println!("{buffer}"); + + let tz = *Local::now().offset(); + + // println!( + // "{}", + // NaiveDateTime::new( + // Local::now().date_naive(), + // NaiveTime::parse_from_str("12:00:00", "%H:%M:%S").unwrap() + // ) + // .and_local_timezone(tz) + // .single() + // .unwrap() + // ); + + let timestamp: DateTime; + if let Some(t) = buffer.parse::>().ok() { + timestamp = t; + } else if let Some(t) = NaiveDateTime::parse_from_str(buffer, "%Y-%m-%d %H:%M:%S").ok() { + timestamp = t.and_local_timezone(tz).single().unwrap().into(); + } else if let Some(t) = NaiveTime::parse_from_str(buffer, "%H:%M:%S").ok() { + timestamp = NaiveDateTime::new(Local::now().date_naive(), t) + .and_local_timezone(tz) + .single() + .unwrap() + .into(); + } else { + println!("Couldn't parse timestamp!"); + std::process::abort(); + } + // NOTE: is local appropriate here? - let timestamp = buffer.parse::>().unwrap(); + let now = Local::now(); - println!("{}", timestamp.to_rfc3339()); + // println!("{}", timestamp.to_rfc3339()); + // println!("{}", now.to_rfc3339()); - let delta = DateTime::signed_duration_since(Local::now(), timestamp); + let delta = DateTime::signed_duration_since(now, timestamp); let days = ((delta.num_seconds() / 60) / 60) / 24; let hours = ((delta.num_seconds() / 60) / 60) % 24;