Skip to content

Commit facc028

Browse files
authored
Merge pull request #1982 from rust-osdev/improvements4
uefi-raw: format negative time zones correctly
2 parents f8d6fca + 923389b commit facc028

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

uefi-raw/src/time.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,18 @@ impl Display for Time {
9393
if self.time_zone == Self::UNSPECIFIED_TIMEZONE {
9494
write!(f, " (local)")?;
9595
} else {
96-
let offset_in_hours = self.time_zone as f32 / 60.0;
96+
let sign = if self.time_zone < 0 { '-' } else { '+' };
97+
let offset_in_hours = self.time_zone.unsigned_abs() as f32 / 60.0;
9798
let integer_part = offset_in_hours as i16;
9899
// We can't use "offset_in_hours.fract()" because it is part of `std`.
99100
let fraction_part = offset_in_hours - (integer_part as f32);
100101
// most time zones
101102
if fraction_part == 0.0 {
102-
write!(f, "UTC+{offset_in_hours}")?;
103+
write!(f, "UTC{sign}{offset_in_hours}")?;
103104
}
104105
// time zones with 30min offset (and perhaps other special time zones)
105106
else {
106-
write!(f, "UTC+{offset_in_hours:.1}")?;
107+
write!(f, "UTC{sign}{offset_in_hours:.1}")?;
107108
}
108109
}
109110

@@ -168,5 +169,8 @@ mod tests {
168169

169170
time.time_zone = 150;
170171
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");
171175
}
172176
}

0 commit comments

Comments
 (0)