fix(modeshow): display correct durations for hour,minute,day #19
cargo devel CI / cargo CI (push) Successful in 1m58s Details

This commit is contained in:
Christoph J. Scherr 2024-07-26 10:38:37 +02:00
parent 19c1939cfb
commit 4957c601ea
2 changed files with 22 additions and 6 deletions

View File

@ -1,7 +1,7 @@
#![warn(clippy::pedantic, clippy::style, clippy::nursery)] #![warn(clippy::pedantic, clippy::style, clippy::nursery)]
#![allow(clippy::question_mark_used)] #![allow(clippy::question_mark_used)]
use chrono::{DateTime, Local, SubsecRound, Timelike}; use chrono::{DateTime, Local, SubsecRound, TimeDelta, Timelike};
use clap::Parser; use clap::Parser;
use libpt::cli::args::HELP_TEMPLATE; use libpt::cli::args::HELP_TEMPLATE;
use libpt::cli::clap::ArgGroup; use libpt::cli::clap::ArgGroup;
@ -113,24 +113,24 @@ impl Clock {
if since_last_reset.num_seconds() >= 1 if since_last_reset.num_seconds() >= 1
&& since_last_reset.num_seconds() >= len.as_secs() && 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 => { TimeBarLength::Minute => {
if since_last_reset.num_seconds() >= 1 && Local::now().second() == 0 { 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)"); debug!("reset the time of the time bar (minute)");
} }
} }
TimeBarLength::Hour => { TimeBarLength::Hour => {
if since_last_reset.num_minutes() >= 1 && Local::now().minute() == 0 { 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)"); debug!("reset the time of the time bar (hour)");
} }
} }
TimeBarLength::Day => { TimeBarLength::Day => {
if since_last_reset.num_hours() >= 1 && Local::now().hour() == 0 { 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)"); debug!("reset the time of the time bar (day)");
} }
} }
@ -148,13 +148,17 @@ impl Clock {
TimeBarLength::Minute => { TimeBarLength::Minute => {
self.last_reset = Some( self.last_reset = Some(
Local::now() Local::now()
.with_second(0) .round_subsecs(0)
.with_second(1)
.expect("tried to use a time that does not exist"), .expect("tried to use a time that does not exist"),
); );
} }
TimeBarLength::Hour => { TimeBarLength::Hour => {
self.last_reset = Some( self.last_reset = Some(
Local::now() Local::now()
.round_subsecs(0)
.with_second(1)
.expect("tried to use a time that does not exist")
.with_minute(0) .with_minute(0)
.expect("tried to use a time that does not exist"), .expect("tried to use a time that does not exist"),
); );
@ -162,6 +166,11 @@ impl Clock {
TimeBarLength::Day => { TimeBarLength::Day => {
self.last_reset = Some( self.last_reset = Some(
Local::now() 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) .with_hour(0)
.expect("tried to use a time that does not exist"), .expect("tried to use a time that does not exist"),
); );

View File

@ -8,6 +8,7 @@ use crate::clock::timebar::TimeBarLength;
use super::Clock; use super::Clock;
// TODO: make this a ringbuffer with a custom struct inside?
#[derive(Debug, Clone, PartialEq, Default)] #[derive(Debug, Clone, PartialEq, Default)]
pub struct Data { pub struct Data {
now: [DateTime<Local>; 2], now: [DateTime<Local>; 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( _ => humantime::Duration::from(
data.now() data.now()
.round_subsecs(0) .round_subsecs(0)