shared/
time.rs

1use chrono::Timelike;
2
3// TODO: use something better
4pub fn to_char(digit: u32) -> char {
5    match digit {
6        0 => '0',
7        1 => '1',
8        2 => '2',
9        3 => '3',
10        4 => '4',
11        5 => '5',
12        6 => '6',
13        7 => '7',
14        8 => '8',
15        9 => '9',
16        _ => '?',
17    }
18}
19
20pub fn write_time(rtc: &mut impl crate::Rtc, seconds: bool) -> heapless::String<8> {
21    let now = chrono::DateTime::<chrono::Utc>::from_timestamp(rtc.timestamp().unwrap(), 0).unwrap();
22    let mut text = heapless::String::new();
23
24    text.push(to_char(now.hour() / 10)).unwrap();
25    text.push(to_char(now.hour() % 10)).unwrap();
26    text.push(':').unwrap();
27    text.push(to_char(now.minute() / 10)).unwrap();
28    text.push(to_char(now.minute() % 10)).unwrap();
29
30    if seconds {
31        text.push(':').unwrap();
32        text.push(to_char(now.second() / 10)).unwrap();
33        text.push(to_char(now.second() % 10)).unwrap();
34    }
35    text
36}