From 120732780f4a3ca7e93ceadb4cbf3c3ef832098d Mon Sep 17 00:00:00 2001 From: Johannes Maibaum Date: Fri, 3 Jan 2020 09:53:20 +0100 Subject: [PATCH 1/2] Add bounds test for `DateTime::from_time()` Fails for 1980-01-01 00:00:00. --- src/types.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/types.rs b/src/types.rs index 640e0c52..93cb03c1 100644 --- a/src/types.rs +++ b/src/types.rs @@ -333,6 +333,60 @@ mod test { assert!(DateTime::from_date_and_time(2107, 12, 32, 0, 0, 0).is_err()); } + #[cfg(feature = "time")] + #[test] + fn datetime_from_time_bounds() { + use super::DateTime; + + // 1979-12-31 23:59:59 + assert!(DateTime::from_time(::time::Tm { + tm_sec: 59, + tm_min: 59, + tm_hour: 23, + tm_mday: 31, + tm_mon: 11, // tm_mon has number range [0, 11] + tm_year: 79, // 1979 - 1900 = 79 + ..::time::empty_tm() + }) + .is_err()); + + // 1980-01-01 00:00:00 + assert!(DateTime::from_time(::time::Tm { + tm_sec: 0, + tm_min: 0, + tm_hour: 0, + tm_mday: 1, + tm_mon: 0, // tm_mon has number range [0, 11] + tm_year: 80, // 1980 - 1900 = 80 + ..::time::empty_tm() + }) + .is_ok()); + + // 2107-12-31 23:59:59 + assert!(DateTime::from_time(::time::Tm { + tm_sec: 59, + tm_min: 59, + tm_hour: 23, + tm_mday: 31, + tm_mon: 11, // tm_mon has number range [0, 11] + tm_year: 207, // 2107 - 1900 = 207 + ..::time::empty_tm() + }) + .is_ok()); + + // 2108-01-01 00:00:00 + assert!(DateTime::from_time(::time::Tm { + tm_sec: 0, + tm_min: 0, + tm_hour: 0, + tm_mday: 1, + tm_mon: 0, // tm_mon has number range [0, 11] + tm_year: 208, // 2108 - 1900 = 208 + ..::time::empty_tm() + }) + .is_err()); + } + #[test] fn time_conversion() { use super::DateTime; From b2a669bd335cd3c49aa3d42003405537d8ba98f6 Mon Sep 17 00:00:00 2001 From: Johannes Maibaum Date: Fri, 3 Jan 2020 10:04:58 +0100 Subject: [PATCH 2/2] Fix bounds check in `DateTime::from_time()` `::time::Tm.tm_mon` has number range [0, 11], see: https://docs.rs/time/0.1.42/time/struct.Tm.html#structfield.tm_mon --- src/types.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types.rs b/src/types.rs index 93cb03c1..29a089e1 100644 --- a/src/types.rs +++ b/src/types.rs @@ -111,7 +111,7 @@ impl DateTime { /// Returns `Err` when this object is out of bounds pub fn from_time(tm: ::time::Tm) -> Result { if tm.tm_year >= 80 && tm.tm_year <= 207 - && tm.tm_mon >= 1 && tm.tm_mon <= 31 + && tm.tm_mon >= 0 && tm.tm_mon <= 11 && tm.tm_mday >= 1 && tm.tm_mday <= 31 && tm.tm_hour >= 0 && tm.tm_hour <= 23 && tm.tm_min >= 0 && tm.tm_min <= 59