feat: new parse formats

Signed-off-by: Christina Sørensen <christina@cafkafk.com>
This commit is contained in:
Christina Sørensen 2024-05-10 13:27:43 +02:00
parent 0848872af8
commit dd62aaad99
Signed by: cafkafk
GPG key ID: 26C542FD97F965CE

View file

@ -13,12 +13,44 @@ fn main() -> io::Result<()> {
let buffer = &args[1]; 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<Local>;
if let Some(t) = buffer.parse::<DateTime<Local>>().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? // NOTE: is local appropriate here?
let timestamp = buffer.parse::<DateTime<Local>>().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 days = ((delta.num_seconds() / 60) / 60) / 24;
let hours = ((delta.num_seconds() / 60) / 60) % 24; let hours = ((delta.num_seconds() / 60) / 60) % 24;