From 4957c601ea72187099e37b333c0929b7a7ecf3cb Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Fri, 26 Jul 2024 10:38:37 +0200 Subject: [PATCH] fix(modeshow): display correct durations for hour,minute,day #19 --- src/clock.rs | 21 +++++++++++++++------ src/clock/ui.rs | 7 +++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/clock.rs b/src/clock.rs index 99ad61b..e17b5ce 100644 --- a/src/clock.rs +++ b/src/clock.rs @@ -1,7 +1,7 @@ #![warn(clippy::pedantic, clippy::style, clippy::nursery)] #![allow(clippy::question_mark_used)] -use chrono::{DateTime, Local, SubsecRound, Timelike}; +use chrono::{DateTime, Local, SubsecRound, TimeDelta, Timelike}; use clap::Parser; use libpt::cli::args::HELP_TEMPLATE; use libpt::cli::clap::ArgGroup; @@ -113,24 +113,24 @@ impl Clock { if since_last_reset.num_seconds() >= 1 && since_last_reset.num_seconds() >= len.as_secs() { - self.last_reset = Some(Local::now()); + self.last_reset = Some(Local::now().round_subsecs(0)); } } TimeBarLength::Minute => { if since_last_reset.num_seconds() >= 1 && Local::now().second() == 0 { - self.last_reset = Some(Local::now()); + self.last_reset = Some(Local::now().round_subsecs(0)); debug!("reset the time of the time bar (minute)"); } } TimeBarLength::Hour => { if since_last_reset.num_minutes() >= 1 && Local::now().minute() == 0 { - self.last_reset = Some(Local::now()); + self.last_reset = Some(Local::now().round_subsecs(0)); debug!("reset the time of the time bar (hour)"); } } TimeBarLength::Day => { if since_last_reset.num_hours() >= 1 && Local::now().hour() == 0 { - self.last_reset = Some(Local::now()); + self.last_reset = Some(Local::now().round_subsecs(0)); debug!("reset the time of the time bar (day)"); } } @@ -148,13 +148,17 @@ impl Clock { TimeBarLength::Minute => { self.last_reset = Some( Local::now() - .with_second(0) + .round_subsecs(0) + .with_second(1) .expect("tried to use a time that does not exist"), ); } TimeBarLength::Hour => { self.last_reset = Some( Local::now() + .round_subsecs(0) + .with_second(1) + .expect("tried to use a time that does not exist") .with_minute(0) .expect("tried to use a time that does not exist"), ); @@ -162,6 +166,11 @@ impl Clock { TimeBarLength::Day => { self.last_reset = Some( Local::now() + .round_subsecs(0) + .with_second(1) + .expect("tried to use a time that does not exist") + .with_minute(0) + .expect("tried to use a time that does not exist") .with_hour(0) .expect("tried to use a time that does not exist"), ); diff --git a/src/clock/ui.rs b/src/clock/ui.rs index 5d2d1db..f13b98d 100644 --- a/src/clock/ui.rs +++ b/src/clock/ui.rs @@ -8,6 +8,7 @@ use crate::clock::timebar::TimeBarLength; use super::Clock; +// TODO: make this a ringbuffer with a custom struct inside? #[derive(Debug, Clone, PartialEq, Default)] pub struct Data { now: [DateTime; 2], @@ -142,6 +143,12 @@ pub fn timebarw_label<'a>( ) } } + TimeBarLength::Hour => humantime::Duration::from( + data.now() + .signed_duration_since(last_reset) + .to_std() + .unwrap(), + ), _ => humantime::Duration::from( data.now() .round_subsecs(0)