Skip to main content

custom/
custom.rs

1use core::time::Duration;
2
3use universal_time::{
4    set_global_time_context, Instant, MonotonicClock, SystemTime, WallClock, UNIX_EPOCH,
5};
6
7struct BoardClock;
8
9impl WallClock for BoardClock {
10    fn system_time(&self) -> Option<SystemTime> {
11        // Replace with your RTC / SNTP source.
12        let unix_secs: u64 = rtc_unix_seconds();
13        Some(SystemTime::from_unix_duration(Duration::from_secs(
14            unix_secs,
15        )))
16    }
17}
18
19impl MonotonicClock for BoardClock {
20    fn instant(&self) -> Option<Instant> {
21        // Replace with your monotonic timer ticks.
22        let millis: u64 = board_millis_since_boot();
23        Some(Instant::from_ticks(Duration::from_millis(millis)))
24    }
25}
26
27fn rtc_unix_seconds() -> u64 {
28    // platform-specific implementation
29    0
30}
31
32fn board_millis_since_boot() -> u64 {
33    // platform-specific implementation
34    0
35}
36
37static CLOCK: BoardClock = BoardClock;
38
39fn main() {
40    set_global_time_context(&CLOCK).unwrap();
41
42    let start = Instant::now();
43    let now = SystemTime::now();
44    let elapsed = start.elapsed();
45    let since_epoch = now.duration_since(UNIX_EPOCH).unwrap_or_default();
46
47    println!("elapsed = {:?}", elapsed);
48    println!("since epoch = {:?}", since_epoch);
49}