dst/
dst.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2// Copyright 2024 René Ladan <rene0+codeberg@freedom.nl>
3
4use radio_datetime_utils::{DST_ANNOUNCED, DST_PROCESSED, DST_SUMMER, RadioDateTimeUtils};
5
6// quick and dirty parsing function
7fn parse_dst(dst: Option<u8>) -> String {
8    if dst.is_none() {
9        return String::from("*");
10    }
11    let mut res = String::from("");
12    let s_dst = dst.unwrap();
13    if s_dst & DST_ANNOUNCED != 0 {
14        res += "announced ";
15    }
16    if s_dst & DST_PROCESSED != 0 {
17        res += "processed ";
18    }
19    if s_dst & DST_SUMMER != 0 {
20        res += "summer";
21    }
22    res
23}
24
25fn main() {
26    let mut rdt = RadioDateTimeUtils::new(7);
27
28    // initial values
29    rdt.set_year(Some(24), true, false);
30    rdt.set_month(Some(3), true, false);
31    rdt.set_weekday(Some(3), true, false); // Wednesday
32    rdt.set_day(Some(13), true, false);
33    rdt.set_hour(Some(21), true, false);
34    rdt.set_minute(Some(5), true, false);
35    rdt.set_dst(Some(false), Some(false), false);
36    println!(
37        "Initial           : date={:?}-{:?}-{:?} weekday={:?} time={:?}:{:?} DST={}",
38        rdt.get_year(),
39        rdt.get_month(),
40        rdt.get_day(),
41        rdt.get_weekday(),
42        rdt.get_hour(),
43        rdt.get_minute(),
44        parse_dst(rdt.get_dst())
45    );
46
47    // switch to DST, in the middle of a business week (nothing in this crate
48    // dictates that DST switches should happen on Sunday nights). Normally,
49    // time station start notifying one or more hours in advance. set_dst()
50    // currently only supports notifications one hour in advance.
51    while rdt.get_minute().unwrap() < 59 {
52        rdt.add_minute();
53        // Keep feeding the announcement, must be at least for 50% of the last hour or part thereof:
54        rdt.set_dst(Some(false), Some(true), false);
55        // Keep internal bookkeeping up-to-date:
56        rdt.bump_minutes_running();
57    }
58    println!(
59        "Just before switch: date={:?}-{:?}-{:?} weekday={:?} time={:?}:{:?} DST={}",
60        rdt.get_year(),
61        rdt.get_month(),
62        rdt.get_day(),
63        rdt.get_weekday(),
64        rdt.get_hour(),
65        rdt.get_minute(),
66        parse_dst(rdt.get_dst())
67    );
68    rdt.add_minute();
69    rdt.set_dst(Some(true), Some(true), false); // compulsory, sign off the new DST value
70    rdt.bump_minutes_running(); // optional, but a good idea
71    println!(
72        "Just after switch : date={:?}-{:?}-{:?} weekday={:?} time={:?}:{:?} DST={}",
73        rdt.get_year(),
74        rdt.get_month(),
75        rdt.get_day(),
76        rdt.get_weekday(),
77        rdt.get_hour(),
78        rdt.get_minute(),
79        parse_dst(rdt.get_dst())
80    );
81    // in the new, now normal again, hour:
82    rdt.add_minute();
83    rdt.set_dst(Some(true), Some(false), false);
84    rdt.bump_minutes_running(); // optional
85    println!(
86        "Back to business  : date={:?}-{:?}-{:?} weekday={:?} time={:?}:{:?} DST={}",
87        rdt.get_year(),
88        rdt.get_month(),
89        rdt.get_day(),
90        rdt.get_weekday(),
91        rdt.get_hour(),
92        rdt.get_minute(),
93        parse_dst(rdt.get_dst())
94    );
95}