diff options
| author | Irene Knapp <ireneista@irenes.space> | 2026-11-18 14:15:25 -0700 |
|---|---|---|
| committer | Irene Knapp <ireneista@irenes.space> | 2026-11-18 14:15:25 -0700 |
| commit | 0bcfe7b468a83436a077824ca60e09c5fdd3f803 (patch) | |
| tree | 317dd485d46324d2eb09a2764c917fece485e754 /src/module/time.rs | |
| parent | ddc986ea05c5c17758e1028ce0d9f20dc133ec9d (diff) | |
plurality
Force-Push: yes Change-Id: I6e5400ab4ce64a54fe84e5facb1da56fd26c4667
Diffstat (limited to 'src/module/time.rs')
| -rw-r--r-- | src/module/time.rs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/module/time.rs b/src/module/time.rs new file mode 100644 index 0000000..f334fd0 --- /dev/null +++ b/src/module/time.rs @@ -0,0 +1,56 @@ +use crate::error::Result; +use crate::facility::FacilityType; +use crate::module::Module; + +use std::sync::{ Arc, Mutex }; +use std::time::Duration; + +use time::OffsetDateTime; +use time::format_description::{ FormatDescriptionV3, parse_owned }; + + +pub struct TimeModule { + time_format: FormatDescriptionV3<'static>, + most_recent_report: Option<OffsetDateTime>, +} + + +impl Module for TimeModule { + const REQUIRED_FACILITIES: FacilityType = FacilityType::empty(); + + fn new() -> Result<Arc<Mutex<TimeModule>>> { + let format_string = + "[weekday repr:short] [year]-[month]-[day] ".to_owned() + + "[hour repr:24]:[minute]:[second]"; + + let time_format = parse_owned::<3>(&format_string).unwrap(); + + Ok(Arc::new(Mutex::new(TimeModule { + time_format, + most_recent_report: None, + }))) + } + + fn format(&mut self) -> String { + let time = OffsetDateTime::now_local().unwrap(); + + self.most_recent_report = Some(time); + + time.format(&self.time_format).unwrap() + } + + fn sleep_duration(&self) -> Option<Duration> { + 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)) + } + } +} + |