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/src/runtime/time/mod.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 / src / runtime / time / mod.rs` 348 lines ยท 9984 bytes Raw Blame History 1 // SPDX-License-Identifier: MIT OR Apache-2.0 2 3 //! Module for UEFI time-related types and definitions and convenience and 4 //! abstractions build around these. 5 6 #[cfg(feature = "time03")] 7 pub use integration_common::TimeConversionError; 8 9 use core::fmt::{Debug, Display, Formatter}; 10 use core::{error, fmt, result}; 11 use uefi_raw::time::{Daylight, Time as RawTime}; 12 13 #[cfg(any(feature = "jiff02", feature = "time03"))] 14 mod integration_common; 15 #[cfg(feature = "jiff02")] 16 mod integration_jiff_crate; 17 #[cfg(feature = "time03")] 18 mod integration_time_crate; 19 20 /// Date and time representation. 21 /// 22 /// # Integration with Time-related Crates of the Ecosystem 23 /// 24 /// Handling time is complicated. Therefore, we do not reinvent the wheel and 25 /// forward all complexity of time to well-known crates of the ecosystem. For 26 /// that, we provide integrations with various crates: 27 /// 28 /// ## Integration with [`time`][time crate] crate 29 /// 30 /// - [`TryFrom`]: `PrimitiveDateTime` <--> [`Time`] (without timezone) 31 /// - [`TryFrom`]: `OffsetDateTime` <--> [`Time`] (with timezone) 32 /// 33 /// ## Integration with [`jiff`][jiff crate] crate 34 /// 35 /// - [`TryFrom`]: `DateTime` <--> [`Time`] (without timezone) 36 /// - [`TryFrom`]: `Zoned` <--> [`Time`] (with timezone) 37 /// 38 /// [time crate]: https://crates.io/crates/time 39 /// [jiff crate]: https://crates.io/crates/jiff 40 #[derive(Copy, Clone, Eq, PartialEq)] 41 #[repr(transparent)] 42 pub struct Time(RawTime); 43 44 /// Input parameters for [`Time::new`]. 45 #[derive(Copy, Clone, Debug)] 46 pub struct TimeParams { 47 /// Year in the range `1900..=9999`. 48 pub year: u16, 49 50 /// Month in the range `1..=12`. 51 pub month: u8, 52 53 /// Day in the range `1..=31`. 54 pub day: u8, 55 56 /// Hour in the range `0..=23`. 57 pub hour: u8, 58 59 /// Minute in the range `0..=59`. 60 pub minute: u8, 61 62 /// Second in the range `0..=59`. 63 pub second: u8, 64 65 /// Fraction of a second represented as nanoseconds in the range 66 /// `0..=999_999_999`. 67 pub nanosecond: u32, 68 69 /// Offset in minutes from UTC in the range `-1440..=1440`, or 70 /// local time if `None`. 71 pub time_zone: Option, 72 73 /// Daylight savings time information. 74 pub daylight: Daylight, 75 } 76 77 /// Error returned by [`Time`] methods. A bool value of `true` means 78 /// the specified field is outside its valid range. 79 #[expect(missing_docs)] 80 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] 81 pub struct TimeError { 82 pub year: bool, 83 pub month: bool, 84 pub day: bool, 85 pub hour: bool, 86 pub minute: bool, 87 pub second: bool, 88 pub nanosecond: bool, 89 pub timezone: bool, 90 pub daylight: bool, 91 } 92 93 impl error::Error for TimeError {} 94 95 impl Display for TimeError { 96 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 97 if self.year { 98 writeln!(f, "year not within `1900..=9999`")?; 99 } 100 if self.month { 101 writeln!(f, "month not within `1..=12")?; 102 } 103 if self.day { 104 writeln!(f, "day not within `1..=31`")?; 105 } 106 if self.hour { 107 writeln!(f, "hour not within `0..=23`")?; 108 } 109 if self.minute { 110 writeln!(f, "minute not within `0..=59`")?; 111 } 112 if self.second { 113 writeln!(f, "second not within `0..=59`")?; 114 } 115 if self.nanosecond { 116 writeln!(f, "nanosecond not within `0..=999_999_999`")?; 117 } 118 if self.timezone { 119 writeln!( 120 f, 121 "time_zone not `Time::UNSPECIFIED_TIMEZONE` nor within `-1440..=1440`" 122 )?; 123 } 124 if self.daylight { 125 writeln!(f, "unknown bits set for daylight")?; 126 } 127 Ok(()) 128 } 129 } 130 131 impl Time { 132 /// Unspecified Timezone/local time. 133 const UNSPECIFIED_TIMEZONE: i16 = RawTime::UNSPECIFIED_TIMEZONE; 134 135 /// Create a `Time` value. If a field is not in the valid range, 136 /// [`TimeError`] is returned. 137 pub fn new(params: TimeParams) -> result::Result { 138 let time = Self(RawTime { 139 year: params.year, 140 month: params.month, 141 day: params.day, 142 hour: params.hour, 143 minute: params.minute, 144 second: params.second, 145 pad1: 0, 146 nanosecond: params.nanosecond, 147 time_zone: params.time_zone.unwrap_or(Self::UNSPECIFIED_TIMEZONE), 148 daylight: params.daylight, 149 pad2: 0, 150 }); 151 152 time.is_valid().map(|_| time) 153 } 154 155 /// Create an invalid `Time` with all fields set to zero. This can 156 /// be used with [`FileInfo`] to indicate a field should not be 157 /// updated when calling [`File::set_info`]. 158 /// 159 /// [`FileInfo`]: uefi::proto::media::file::FileInfo 160 /// [`File::set_info`]: uefi::proto::media::file::File::set_info 161 #[must_use] 162 pub const fn invalid() -> Self { 163 Self(RawTime::invalid()) 164 } 165 166 /// `Ok()` if all fields are within valid ranges, `Err(TimeError)` otherwise. 167 pub fn is_valid(&self) -> result::Result<(), TimeError> { 168 let mut err = TimeError::default(); 169 if !(1900..=9999).contains(&self.year()) { 170 err.year = true; 171 } 172 if !(1..=12).contains(&self.month()) { 173 err.month = true; 174 } 175 if !(1..=31).contains(&self.day()) { 176 err.day = true; 177 } 178 if self.hour() > 23 { 179 err.hour = true; 180 } 181 if self.minute() > 59 { 182 err.minute = true; 183 } 184 if self.second() > 59 { 185 err.second = true; 186 } 187 if self.nanosecond() > 999_999_999 { 188 err.nanosecond = true; 189 } 190 if self.time_zone().is_some() && !((-1440..=1440).contains(&self.time_zone().unwrap())) { 191 err.timezone = true; 192 } 193 // All fields are false, i.e., within their valid range. 194 if err == TimeError::default() { 195 Ok(()) 196 } else { 197 Err(err) 198 } 199 } 200 201 /// Query the year. 202 #[must_use] 203 pub const fn year(&self) -> u16 { 204 self.0.year 205 } 206 207 /// Query the month. 208 #[must_use] 209 pub const fn month(&self) -> u8 { 210 self.0.month 211 } 212 213 /// Query the day. 214 #[must_use] 215 pub const fn day(&self) -> u8 { 216 self.0.day 217 } 218 219 /// Query the hour. 220 #[must_use] 221 pub const fn hour(&self) -> u8 { 222 self.0.hour 223 } 224 225 /// Query the minute. 226 #[must_use] 227 pub const fn minute(&self) -> u8 { 228 self.0.minute 229 } 230 231 /// Query the second. 232 #[must_use] 233 pub const fn second(&self) -> u8 { 234 self.0.second 235 } 236 237 /// Query the nanosecond. 238 #[must_use] 239 pub const fn nanosecond(&self) -> u32 { 240 self.0.nanosecond 241 } 242 243 /// Query the time offset in minutes from UTC, or None if using local time. 244 #[must_use] 245 pub const fn time_zone(&self) -> Option { 246 if self.0.time_zone == Self::UNSPECIFIED_TIMEZONE { 247 None 248 } else { 249 Some(self.0.time_zone) 250 } 251 } 252 253 /// Query the daylight savings time information. 254 #[must_use] 255 pub const fn daylight(&self) -> Daylight { 256 self.0.daylight 257 } 258 } 259 260 impl Debug for Time { 261 fn fmt(&self, f: &mut Formatter) -> fmt::Result { 262 write!( 263 f, 264 "{:04}-{:02}-{:02} ", 265 self.0.year, self.0.month, self.0.day 266 )?; 267 write!( 268 f, 269 "{:02}:{:02}:{:02}.{:09}", 270 self.0.hour, self.0.minute, self.0.second, self.0.nanosecond 271 )?; 272 if self.0.time_zone == Self::UNSPECIFIED_TIMEZONE { 273 write!(f, ", Timezone=local")?; 274 } else { 275 write!(f, ", Timezone={}", self.0.time_zone)?; 276 } 277 write!(f, ", Daylight={:?}", self.0.daylight) 278 } 279 } 280 281 impl Display for Time { 282 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 283 write!(f, "{}", self.0) 284 } 285 } 286 287 /// Error returned from failing to convert a byte slice into a [`Time`]. 288 #[derive(Clone, Copy, Debug, PartialEq, Eq)] 289 pub enum TimeByteConversionError { 290 /// One or more fields of the converted [`Time`] is invalid. 291 InvalidFields(TimeError), 292 /// The byte slice is not large enough to hold a [`Time`]. 293 InvalidSize, 294 } 295 296 impl Display for TimeByteConversionError { 297 fn fmt(&self, f: &mut Formatter) -> fmt::Result { 298 match self { 299 Self::InvalidFields(error) => write!(f, "{error}"), 300 Self::InvalidSize => write!( 301 f, 302 "the byte slice is not large enough to hold a Time struct" 303 ), 304 } 305 } 306 } 307 308 impl TryFrom<&[u8]> for Time { 309 type Error = TimeByteConversionError; 310 311 fn try_from(bytes: &[u8]) -> result::Result { 312 if size_of::() <= bytes.len() { 313 let year = u16::from_le_bytes(bytes[0..2].try_into().unwrap()); 314 let month = bytes[2]; 315 let day = bytes[3]; 316 let hour = bytes[4]; 317 let minute = bytes[5]; 318 let second = bytes[6]; 319 let nanosecond = u32::from_le_bytes(bytes[8..12].try_into().unwrap()); 320 let time_zone = match i16::from_le_bytes(bytes[12..14].try_into().unwrap()) { 321 Self::UNSPECIFIED_TIMEZONE => None, 322 num => Some(num), 323 }; 324 let daylight = Daylight::from_bits(bytes[14]).ok_or_else(|| { 325 TimeByteConversionError::InvalidFields(TimeError { 326 daylight: true, 327 ..Default::default() 328 }) 329 })?; 330 331 let time_params = TimeParams { 332 year, 333 month, 334 day, 335 hour, 336 minute, 337 second, 338 nanosecond, 339 time_zone, 340 daylight, 341 }; 342 343 Self::new(time_params).map_err(TimeByteConversionError::InvalidFields) 344 } else { 345 Err(TimeByteConversionError::InvalidSize) 346 } 347 } 348 }