1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
use crate::error::Result;
use crate::facility::{ FacilityMap, FacilityType };
use crate::module::{ CreatableModule, Module };
use std::time::Duration;
use async_executor::StaticExecutor;
use time::OffsetDateTime;
use time::format_description::{ FormatDescriptionV3, parse_owned };
pub struct TimeModule {
time_format: FormatDescriptionV3<'static>,
most_recent_report: Option<OffsetDateTime>,
}
impl CreatableModule for TimeModule {
const REQUIRED_FACILITIES: FacilityType = FacilityType::empty();
async fn new(_facilities: &mut FacilityMap,
_executor: &'static StaticExecutor)
-> Result<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(TimeModule {
time_format,
most_recent_report: None,
})
}
}
impl Module for TimeModule {
fn format(&mut self, _facilities: &mut FacilityMap) -> Result<String> {
let time = OffsetDateTime::now_local().unwrap();
self.most_recent_report = Some(time);
Ok(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))
}
}
fn is_visible(&self) -> bool {
true
}
}
|