diff options
| -rw-r--r-- | src/modules/time.rs | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/src/modules/time.rs b/src/modules/time.rs index f8ef95c..25c5864 100644 --- a/src/modules/time.rs +++ b/src/modules/time.rs @@ -7,6 +7,7 @@ use time::format_description::{ FormatDescriptionV3, parse_owned }; pub struct TimeModule { time_format: FormatDescriptionV3<'static>, + most_recent_report: Option<OffsetDateTime>, } @@ -18,17 +19,32 @@ impl TimeModule { let time_format = parse_owned::<3>(&format_string).unwrap(); - Arc::new(Mutex::new(TimeModule { time_format })) + Arc::new(Mutex::new(TimeModule { + time_format, + most_recent_report: None, + })) } pub fn format(&mut self) -> String { let time = OffsetDateTime::now_local().unwrap(); + self.most_recent_report = Some(time); + time.format(&self.time_format).unwrap() } pub fn sleep_duration(&self) -> Option<Duration> { - Some(Duration::from_millis(1000)) + if let Some(most_recent_report) = self.most_recent_report { + let (_, _, _, millis) = most_recent_report.to_hms_milli(); + + /* An easy way to check whether this behavior is correct is to open + * several copies of the dock atop each other, and check whether they're + * in sync. + */ + Some(Duration::from_millis(1001 - millis as u64)) + } else { + Some(Duration::from_millis(1000)) + } } } |