Improve time formatting output to use local time and to hide nanoseconds

This commit is contained in:
Campbell Alden 2023-07-11 17:21:41 +09:00
parent 5a79895f92
commit 708d2bb049

View file

@ -1,6 +1,6 @@
use std::io::{stdin, BufRead}; use std::io::{stdin, BufRead};
use chrono::{NaiveTime, Duration, Utc}; use chrono::{NaiveTime, Duration, Local};
use clap::Parser; use clap::Parser;
use atty::Stream; use atty::Stream;
@ -51,17 +51,20 @@ fn live_spans() -> Vec<Duration> {
let mut durations = vec![]; let mut durations = vec![];
let mut seen: Option<NaiveTime> = None; let mut seen: Option<NaiveTime> = None;
for _line in stdin().lock().lines() { for _line in stdin().lock().lines() {
let mut now = Utc::now().naive_local().time(); let mut now = Local::now().naive_local().time();
if let Some(previous) = &seen { if let Some(previous) = &seen {
if now < *previous { if now < *previous {
now = now + Duration::hours(12); now = now + Duration::hours(12);
} }
println!("Closing span from {previous} - {now}"); let prev_time = previous.format("%H:%M");
let now_time = now.format("%H:%M");
println!("Closing span from {prev_time} - {now_time}");
println!("Status: Away"); println!("Status: Away");
durations.push(now - *previous); durations.push(now - *previous);
seen = None; seen = None;
} else { } else {
println!("Starting span at {now}"); let now_time = now.format("%H:%M");
println!("Starting span at {now_time}");
println!("Status: Working"); println!("Status: Working");
seen = Some(now); seen = Some(now);
} }