Skip to content

Commit 2d2e5a2

Browse files
authored
Better fmt::Debug for Time struct + fmt::Display (#256)
1 parent 9dc10a1 commit 2d2e5a2

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

src/table/runtime.rs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::table::boot::MemoryDescriptor;
66
use crate::{CStr16, Char16, Guid, Result, Status};
77
use bitflags::bitflags;
88
use core::fmt;
9+
use core::fmt::Formatter;
910
use core::mem::MaybeUninit;
1011
use core::ptr;
1112

@@ -214,6 +215,9 @@ bitflags! {
214215
}
215216

216217
impl Time {
218+
/// Unspecified Timezone/local time.
219+
const UNSPECIFIED_TIMEZONE: i16 = 0x07ff;
220+
217221
/// Build an UEFI time struct
218222
#[allow(clippy::too_many_arguments)]
219223
pub fn new(
@@ -302,13 +306,48 @@ impl Time {
302306

303307
impl fmt::Debug for Time {
304308
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
305-
write!(f, "{}-{}-{} ", self.year, self.month, self.day)?;
309+
write!(f, "{:04}-{:02}-{:02} ", self.year, self.month, self.day)?;
310+
write!(
311+
f,
312+
"{:02}:{:02}:{:02}.{:09}",
313+
self.hour, self.minute, self.second, self.nanosecond
314+
)?;
315+
if self.time_zone == Self::UNSPECIFIED_TIMEZONE {
316+
write!(f, ", Timezone=local")?;
317+
} else {
318+
write!(f, ", Timezone={}", self.time_zone)?;
319+
}
320+
write!(f, ", Daylight={:?}", self.daylight)
321+
}
322+
}
323+
324+
impl fmt::Display for Time {
325+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
326+
write!(f, "{:04}-{:02}-{:02} ", self.year, self.month, self.day)?;
306327
write!(
307328
f,
308-
"{}:{}:{}.{} ",
329+
"{:02}:{:02}:{:02}.{:09}",
309330
self.hour, self.minute, self.second, self.nanosecond
310331
)?;
311-
write!(f, "{} {:?}", self.time_zone, self.daylight)
332+
333+
if self.time_zone == Self::UNSPECIFIED_TIMEZONE {
334+
write!(f, " (local)")?;
335+
} else {
336+
let offset_in_hours = self.time_zone as f32 / 60.0;
337+
let integer_part = offset_in_hours as i16;
338+
// We can't use "offset_in_hours.fract()" because it is part of `std`.
339+
let fraction_part = offset_in_hours - (integer_part as f32);
340+
// most time zones
341+
if fraction_part == 0.0 {
342+
write!(f, "UTC+{}", offset_in_hours)?;
343+
}
344+
// time zones with 30min offset (and perhaps other special time zones)
345+
else {
346+
write!(f, "UTC+{:.1}", offset_in_hours)?;
347+
}
348+
}
349+
350+
Ok(())
312351
}
313352
}
314353

0 commit comments

Comments
 (0)