Skip to content

Commit cf7c825

Browse files
committed
uefi: time: time crate: add unit tests
1 parent 56578ff commit cf7c825

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

uefi/src/runtime/time_defs/integration_common.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ impl Error for ConversionErrorInner {
7676
}
7777

7878
#[cfg(test)]
79-
#[allow(unused)]
8079
pub(super) mod test_helpers {
8180
use super::*;
8281
use crate::runtime::TimeParams;

uefi/src/runtime/time_defs/integration_time_crate.rs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl TryFrom<Time> for OffsetDateTime {
5050
}
5151

5252
let h = (value.0.time_zone / 60) as i8;
53-
let m = (value.0.time_zone.abs() % 60) as i8;
53+
let m = (value.0.time_zone % 60) as i8;
5454

5555
let offset = UtcOffset::from_hms(h, m, 0)
5656
.map_err(time::Error::ComponentRange)
@@ -107,3 +107,61 @@ impl TryFrom<OffsetDateTime> for Time {
107107
Self::new(params).map_err(|e| ConversionError(ConversionErrorInner::InvalidUefiTime(e)))
108108
}
109109
}
110+
111+
#[cfg(test)]
112+
mod tests {
113+
use super::super::integration_common::test_helpers;
114+
use super::*;
115+
use time::{OffsetDateTime, PrimitiveDateTime};
116+
117+
#[test]
118+
fn primitive_roundtrip_basic() {
119+
test_helpers::primitive_roundtrip::<PrimitiveDateTime>();
120+
}
121+
122+
#[test]
123+
fn zoned_roundtrip_positive_offset() {
124+
test_helpers::zoned_roundtrip::<OffsetDateTime>();
125+
}
126+
127+
#[test]
128+
fn zoned_roundtrip_negative_offset() {
129+
test_helpers::negative_offset_roundtrip::<OffsetDateTime>();
130+
}
131+
132+
#[test]
133+
fn nanoseconds_preserved() {
134+
test_helpers::preserves_nanoseconds::<OffsetDateTime>();
135+
}
136+
137+
#[test]
138+
fn unspecified_timezone_is_rejected() {
139+
test_helpers::unspecified_timezone_fails::<OffsetDateTime>();
140+
}
141+
142+
#[test]
143+
fn invalid_date_is_rejected() {
144+
test_helpers::invalid_calendar_date_fails::<PrimitiveDateTime>();
145+
}
146+
147+
// important real-world offset edge case
148+
#[test]
149+
fn half_hour_timezone_roundtrip() {
150+
let t = test_helpers::sample_time(90); // +01:30
151+
152+
let dt: OffsetDateTime = t.try_into().unwrap();
153+
let back: Time = dt.try_into().unwrap();
154+
155+
assert_eq!(back.0.time_zone, 90);
156+
}
157+
158+
#[test]
159+
fn negative_half_hour_timezone_roundtrip() {
160+
let t = test_helpers::sample_time(-330); // -05:30
161+
162+
let dt: OffsetDateTime = t.try_into().unwrap();
163+
let back: Time = dt.try_into().unwrap();
164+
165+
assert_eq!(back.0.time_zone, -330);
166+
}
167+
}

0 commit comments

Comments
 (0)