Skip to main content

intel_crashlog/metadata/
time.rs

1// Copyright (C) 2026 Intel Corporation
2// SPDX-License-Identifier: MIT
3
4#[cfg(not(feature = "std"))]
5use alloc::fmt;
6#[cfg(feature = "std")]
7use std::fmt;
8
9/// Crash Log Extraction Time, expressed in the local time of the system
10#[derive(Clone)]
11pub struct Time {
12    pub year: u16,
13    pub month: u8,
14    pub day: u8,
15    pub hour: u8,
16    pub minute: u8,
17    pub second: u8,
18    pub millisecond: u16,
19}
20
21impl fmt::Display for Time {
22    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
23        write!(
24            f,
25            "{:04}{:02}{:02}T{:02}{:02}{:02}.{:03}",
26            self.year, self.month, self.day, self.hour, self.minute, self.second, self.millisecond,
27        )
28    }
29}
30
31impl Time {
32    /// Returns the current [Time], or [None] if it cannot be determined
33    ///
34    /// The time is read from the operating system
35    #[cfg(feature = "std")]
36    pub fn now() -> Option<Self> {
37        use jiff::{Timestamp, tz};
38
39        let now = Timestamp::try_from(std::time::SystemTime::now())
40            .inspect_err(|err| log::info!("Cannot get the current time: {err}"))
41            .ok()?;
42
43        let tz = tz::TimeZone::system();
44
45        if tz == tz::TimeZone::unknown() {
46            log::warn!("Cannot determine the system time zone");
47        }
48
49        let local = now.to_zoned(tz).datetime();
50        Some(Time {
51            year: local.year() as u16,
52            month: local.month() as u8,
53            day: local.day() as u8,
54            hour: local.hour() as u8,
55            minute: local.minute() as u8,
56            second: local.second() as u8,
57            millisecond: local.millisecond() as u16,
58        })
59    }
60
61    /// Returns the current [Time], or [None] if it cannot be determined
62    ///
63    /// The time is read from the UEFI runtime services
64    #[cfg(all(not(feature = "std"), target_os = "uefi"))]
65    pub fn now() -> Option<Self> {
66        let time = uefi::runtime::get_time()
67            .inspect_err(|err| log::info!("Cannot get RTC time: {err}"))
68            .ok()?;
69
70        Some(Time {
71            year: time.year(),
72            month: time.month(),
73            day: time.day(),
74            hour: time.hour(),
75            minute: time.minute(),
76            second: time.second(),
77            millisecond: (time.nanosecond() / 1_000_000) as u16,
78        })
79    }
80}