intel_crashlog/metadata/
time.rs1#[cfg(not(feature = "std"))]
5use alloc::fmt;
6#[cfg(feature = "std")]
7use std::fmt;
8
9#[derive(Clone)]
11pub enum TimeZone {
12 Utc,
13 Unspecified,
14}
15
16impl TimeZone {
17 fn suffix(&self) -> &'static str {
18 match self {
19 TimeZone::Utc => "Z",
20 TimeZone::Unspecified => "",
21 }
22 }
23}
24
25#[derive(Clone)]
27pub struct Time {
28 pub year: u16,
29 pub month: u8,
30 pub day: u8,
31 pub hour: u8,
32 pub minute: u8,
33 pub second: u8,
34 pub millisecond: u16,
35 pub time_zone: TimeZone,
36}
37
38impl fmt::Display for Time {
39 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40 write!(
41 f,
42 "{:04}{:02}{:02}T{:02}{:02}{:02}.{:03}{}",
43 self.year,
44 self.month,
45 self.day,
46 self.hour,
47 self.minute,
48 self.second,
49 self.millisecond,
50 self.time_zone.suffix(),
51 )
52 }
53}
54
55impl Time {
56 #[cfg(feature = "std")]
60 pub fn now() -> Option<Self> {
61 use jiff::{Timestamp, tz};
62
63 let now = Timestamp::try_from(std::time::SystemTime::now())
64 .inspect_err(|err| log::info!("Cannot get the current time: {err}"))
65 .ok()?;
66
67 let utc_time = tz::Offset::UTC.to_datetime(now);
68 Some(Time {
69 year: utc_time.year() as u16,
70 month: utc_time.month() as u8,
71 day: utc_time.day() as u8,
72 hour: utc_time.hour() as u8,
73 minute: utc_time.minute() as u8,
74 second: utc_time.second() as u8,
75 millisecond: utc_time.millisecond() as u16,
76 time_zone: TimeZone::Utc,
77 })
78 }
79
80 #[cfg(all(not(feature = "std"), target_os = "uefi"))]
85 pub fn now() -> Option<Self> {
86 let time = uefi::runtime::get_time()
87 .inspect_err(|err| log::info!("Cannot get RTC time: {err}"))
88 .ok()?;
89
90 Some(Time {
91 year: time.year(),
92 month: time.month(),
93 day: time.day(),
94 hour: time.hour(),
95 minute: time.minute(),
96 second: time.second(),
97 millisecond: (time.nanosecond() / 1_000_000) as u16,
98 time_zone: TimeZone::Unspecified,
99 })
100 }
101}