#[test] fn test_time_addassignment() { let hms = |h, m, s| NaiveTime::from_hms_opt(h, m, s).unwrap(); letmut time = hms(12, 12, 12);
time += TimeDelta::try_hours(10).unwrap();
assert_eq!(time, hms(22, 12, 12));
time += TimeDelta::try_hours(10).unwrap();
assert_eq!(time, hms(8, 12, 12));
}
#[test] fn test_time_subassignment() { let hms = |h, m, s| NaiveTime::from_hms_opt(h, m, s).unwrap(); letmut time = hms(12, 12, 12);
time -= TimeDelta::try_hours(10).unwrap();
assert_eq!(time, hms(2, 12, 12));
time -= TimeDelta::try_hours(10).unwrap();
assert_eq!(time, hms(16, 12, 12));
}
// the format specifier should have no effect on `NaiveTime`
assert_eq!(
format!("{:30}", NaiveTime::from_hms_milli_opt(3, 5, 7, 9).unwrap()), "03:05:07.009"
);
}
#[test] fn test_time_from_str() { // valid cases let valid = [ "0:0:0", "0:0:0.0000000", "0:0:0.0000003", " 4 : 3 : 2.1 ", " 09:08:07 ", " 09:08 ", " 9:8:07 ", "01:02:03", "4:3:2.1", "9:8:7", "09:8:7", "9:08:7", "9:8:07", "09:08:7", "09:8:07", "09:08:7", "9:08:07", "09:08:07", "9:8:07.123", "9:08:7.123", "09:8:7.123", "09:08:7.123", "9:08:07.123", "09:8:07.123", "09:08:07.123", "09:08:07.123", "09:08:07.1234", "09:08:07.12345", "09:08:07.123456", "09:08:07.1234567", "09:08:07.12345678", "09:08:07.123456789", "09:08:07.1234567891", "09:08:07.12345678912", "23:59:60.373929310237",
]; for &s in &valid {
eprintln!("test_time_parse_from_str valid {s:?}"); let d = match s.parse::<NaiveTime>() {
Ok(d) => d,
Err(e) => panic!("parsing `{s}` has failed: {e}"),
}; let s_ = format!("{d:?}"); // `s` and `s_` may differ, but `s.parse()` and `s_.parse()` must be same let d_ = match s_.parse::<NaiveTime>() {
Ok(d) => d,
Err(e) => {
panic!("`{s}` is parsed into `{d:?}`, but reparsing that has failed: {e}")
}
};
assert!(
d == d_, "`{s}` is parsed into `{d:?}`, but reparsed result \
`{d_:?}` does not match"
);
}
// some invalid cases // since `ParseErrorKind` is private, all we can do is to check if there was an error let invalid = [ "", // empty "x", // invalid "15", // missing data "15:8:", // trailing colon "15:8:x", // invalid data "15:8:9x", // invalid data "23:59:61", // invalid second (out of bounds) "23:54:35 GMT", // invalid (timezone non-sensical for NaiveTime) "23:54:35 +0000", // invalid (timezone non-sensical for NaiveTime) "1441497364.649", // valid datetime, not a NaiveTime "+1441497364.649", // valid datetime, not a NaiveTime "+1441497364", // valid datetime, not a NaiveTime "001:02:03", // invalid hour "01:002:03", // invalid minute "01:02:003", // invalid second "12:34:56.x", // invalid fraction "12:34:56. 0", // invalid fraction format "09:08:00000000007", // invalid second / invalid fraction format
]; for &s in &invalid {
eprintln!("test_time_parse_from_str invalid {s:?}");
assert!(s.parse::<NaiveTime>().is_err());
}
}
#[test] fn test_overflowing_offset() { let hmsm = |h, m, s, n| NaiveTime::from_hms_milli_opt(h, m, s, n).unwrap();
let positive_offset = FixedOffset::east_opt(4 * 60 * 60).unwrap(); // regular time let t = hmsm(5, 6, 7, 890);
assert_eq!(t.overflowing_add_offset(positive_offset), (hmsm(9, 6, 7, 890), 0));
assert_eq!(t.overflowing_sub_offset(positive_offset), (hmsm(1, 6, 7, 890), 0)); // leap second is preserved, and wrap to next day let t = hmsm(23, 59, 59, 1_000);
assert_eq!(t.overflowing_add_offset(positive_offset), (hmsm(3, 59, 59, 1_000), 1));
assert_eq!(t.overflowing_sub_offset(positive_offset), (hmsm(19, 59, 59, 1_000), 0)); // wrap to previous day let t = hmsm(1, 2, 3, 456);
assert_eq!(t.overflowing_sub_offset(positive_offset), (hmsm(21, 2, 3, 456), -1)); // an odd offset let negative_offset = FixedOffset::west_opt(((2 * 60) + 3) * 60 + 4).unwrap(); let t = hmsm(5, 6, 7, 890);
assert_eq!(t.overflowing_add_offset(negative_offset), (hmsm(3, 3, 3, 890), 0));
assert_eq!(t.overflowing_sub_offset(negative_offset), (hmsm(7, 9, 11, 890), 0));
assert_eq!(t.overflowing_add_offset(positive_offset).0, t + positive_offset);
assert_eq!(t.overflowing_sub_offset(positive_offset).0, t - positive_offset);
}
#[test] #[cfg(feature = "rkyv-validation")] fn test_rkyv_validation() { let t_min = NaiveTime::MIN; let bytes = rkyv::to_bytes::<_, 8>(&t_min).unwrap();
assert_eq!(rkyv::from_bytes::<NaiveTime>(&bytes).unwrap(), t_min);
let t_max = NaiveTime::MAX; let bytes = rkyv::to_bytes::<_, 8>(&t_max).unwrap();
assert_eq!(rkyv::from_bytes::<NaiveTime>(&bytes).unwrap(), t_max);
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet am 2026-08-25)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.