// SPDX-License-Identifier: MIT OR Apache-2.0 // Copyright 2024 René Ladan use radio_datetime_utils::{RadioDateTimeUtils, LEAP_ANNOUNCED, LEAP_MISSING, LEAP_PROCESSED}; // quick and dirty parsing function fn parse_ls(ls: Option) -> String { if ls.is_none() { return String::from("*"); } let mut res = String::from(""); let s_ls = ls.unwrap(); if s_ls & LEAP_ANNOUNCED != 0 { res += "announced "; } if s_ls & LEAP_PROCESSED != 0 { res += "processed "; } if s_ls & LEAP_MISSING != 0 { res += "missing"; } res } fn main() { let mut rdt = RadioDateTimeUtils::new(7); // initial values rdt.set_year(Some(24), true, false); rdt.set_month(Some(3), true, false); rdt.set_weekday(Some(3), true, false); // Wednesday rdt.set_day(Some(13), true, false); rdt.set_hour(Some(21), true, false); rdt.set_minute(Some(5), true, false); // Do NOT forget to initialize the DST field, otherwise add_minute() will // refuse to increase the time which ultimately results in a panic in // a test in set_leap_second() : // thread 'main' panicked at src/lib.rs:406:16: // attempt to multiply with overflow rdt.set_dst(Some(false), Some(false), false); rdt.set_leap_second(Some(false), 60); println!( "Initial : date={:?}-{:?}-{:?} weekday={:?} time={:?}:{:?} LS={}", rdt.get_year(), rdt.get_month(), rdt.get_day(), rdt.get_weekday(), rdt.get_hour(), rdt.get_minute(), parse_ls(rdt.get_leap_second()) ); // insert a leap second, in the middle of a business week (nothing in this crate // dictates that leap seconds should happen at midnight, UTC? local?). Normally, // time station start notifying one or more hours in advance. set_leap_second() // currently only supports notifications one hour in advance. while rdt.get_minute().unwrap() < 59 { rdt.add_minute(); // Keep feeding the announcement, must be at least for 50% of the last hour or part thereof: rdt.set_leap_second(Some(true), 60); // Keep internal bookkeeping up-to-date: rdt.bump_minutes_running(); } println!( "Just before leap second: date={:?}-{:?}-{:?} weekday={:?} time={:?}:{:?} LS={}", rdt.get_year(), rdt.get_month(), rdt.get_day(), rdt.get_weekday(), rdt.get_hour(), rdt.get_minute(), parse_ls(rdt.get_leap_second()) ); rdt.add_minute(); rdt.set_leap_second(Some(true), 61); // insert the leap second rdt.bump_minutes_running(); // optional, but a good idea println!( "Just after leap second : date={:?}-{:?}-{:?} weekday={:?} time={:?}:{:?} LS={}", rdt.get_year(), rdt.get_month(), rdt.get_day(), rdt.get_weekday(), rdt.get_hour(), rdt.get_minute(), parse_ls(rdt.get_leap_second()) ); // in the new, now normal again, hour: rdt.add_minute(); rdt.set_leap_second(Some(false), 60); rdt.bump_minutes_running(); // optional println!( "Back to business : date={:?}-{:?}-{:?} weekday={:?} time={:?}:{:?} LS={}", rdt.get_year(), rdt.get_month(), rdt.get_day(), rdt.get_weekday(), rdt.get_hour(), rdt.get_minute(), parse_ls(rdt.get_leap_second()) ); }