Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/uefi-raw/src/time.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / uefi-raw / src / time.rs` 176 lines ยท 5008 bytes Raw Blame History 1 // SPDX-License-Identifier: MIT OR Apache-2.0 2 3 //! Date and time types. 4 5 use bitflags::bitflags; 6 use core::fmt::{self, Display, Formatter}; 7 8 /// Date and time representation. 9 #[derive(Debug, Default, Copy, Clone, Eq)] 10 #[repr(C)] 11 pub struct Time { 12 /// Year. Valid range: `1900..=9999`. 13 pub year: u16, 14 15 /// Month. Valid range: `1..=12`. 16 pub month: u8, 17 18 /// Day of the month. Valid range: `1..=31`. 19 pub day: u8, 20 21 /// Hour. Valid range: `0..=23`. 22 pub hour: u8, 23 24 /// Minute. Valid range: `0..=59`. 25 pub minute: u8, 26 27 /// Second. Valid range: `0..=59`. 28 pub second: u8, 29 30 /// Unused padding. 31 pub pad1: u8, 32 33 /// Nanosececond. Valid range: `0..=999_999_999`. 34 pub nanosecond: u32, 35 36 /// Offset in minutes from UTC. Valid range: `-1440..=1440`, or 37 /// [`Time::UNSPECIFIED_TIMEZONE`]. 38 pub time_zone: i16, 39 40 /// Daylight savings time information. 41 pub daylight: Daylight, 42 43 /// Unused padding. 44 pub pad2: u8, 45 } 46 47 impl Time { 48 /// Indicates the time should be interpreted as local time. 49 pub const UNSPECIFIED_TIMEZONE: i16 = 0x07ff; 50 51 /// Creates an invalid `Time` with all fields set to zero. 52 #[must_use] 53 pub const fn invalid() -> Self { 54 Self { 55 year: 0, 56 month: 0, 57 day: 0, 58 hour: 0, 59 minute: 0, 60 second: 0, 61 pad1: 0, 62 nanosecond: 0, 63 time_zone: 0, 64 daylight: Daylight::empty(), 65 pad2: 0, 66 } 67 } 68 69 /// True if all fields are within valid ranges, false otherwise. 70 #[must_use] 71 pub fn is_valid(&self) -> bool { 72 (1900..=9999).contains(&self.year) 73 && (1..=12).contains(&self.month) 74 && (1..=31).contains(&self.day) 75 && self.hour <= 23 76 && self.minute <= 59 77 && self.second <= 59 78 && self.nanosecond <= 999_999_999 79 && ((-1440..=1440).contains(&self.time_zone) 80 || self.time_zone == Self::UNSPECIFIED_TIMEZONE) 81 } 82 } 83 84 impl Display for Time { 85 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 86 write!(f, "{:04}-{:02}-{:02} ", self.year, self.month, self.day)?; 87 write!( 88 f, 89 "{:02}:{:02}:{:02}.{:09}", 90 self.hour, self.minute, self.second, self.nanosecond 91 )?; 92 93 if self.time_zone == Self::UNSPECIFIED_TIMEZONE { 94 write!(f, " (local)")?; 95 } else { 96 let sign = if self.time_zone < 0 { '-' } else { '+' }; 97 let offset_in_hours = self.time_zone.unsigned_abs() as f32 / 60.0; 98 let integer_part = offset_in_hours as i16; 99 // We can't use "offset_in_hours.fract()" because it is part of `std`. 100 let fraction_part = offset_in_hours - (integer_part as f32); 101 // most time zones 102 if fraction_part == 0.0 { 103 write!(f, "UTC{sign}{offset_in_hours}")?; 104 } 105 // time zones with 30min offset (and perhaps other special time zones) 106 else { 107 write!(f, "UTC{sign}{offset_in_hours:.1}")?; 108 } 109 } 110 111 Ok(()) 112 } 113 } 114 115 /// The padding fields of `Time` are ignored for comparison. 116 impl PartialEq for Time { 117 fn eq(&self, other: &Self) -> bool { 118 self.year == other.year 119 && self.month == other.month 120 && self.day == other.day 121 && self.hour == other.hour 122 && self.minute == other.minute 123 && self.second == other.second 124 && self.nanosecond == other.nanosecond 125 && self.time_zone == other.time_zone 126 && self.daylight == other.daylight 127 } 128 } 129 130 bitflags! { 131 /// A bitmask containing daylight savings time information. 132 #[repr(transparent)] 133 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)] 134 pub struct Daylight: u8 { 135 /// Time is affected by daylight savings time. 136 const ADJUST_DAYLIGHT = 0x01; 137 138 /// Time has been adjusted for daylight savings time. 139 const IN_DAYLIGHT = 0x02; 140 } 141 } 142 143 #[cfg(test)] 144 mod tests { 145 extern crate alloc; 146 147 use super::*; 148 use alloc::string::ToString; 149 150 #[test] 151 fn test_time_display() { 152 let mut time = Time { 153 year: 2023, 154 month: 5, 155 day: 18, 156 hour: 11, 157 minute: 29, 158 second: 57, 159 nanosecond: 123_456_789, 160 time_zone: Time::UNSPECIFIED_TIMEZONE, 161 daylight: Daylight::empty(), 162 pad1: 0, 163 pad2: 0, 164 }; 165 assert_eq!(time.to_string(), "2023-05-18 11:29:57.`123456789` (local)"); 166 167 time.time_zone = 120; 168 assert_eq!(time.to_string(), "2023-05-18 11:29:57.123456789UTC+2"); 169 170 time.time_zone = 150; 171 assert_eq!(time.to_string(), "2023-05-18 11:29:57.123456789UTC+2.5"); 172 173 time.time_zone = -330; 174 assert_eq!(time.to_string(), "2023-05-18 11:29:57.123456789UTC-5.5"); 175 } 176 }