Debian Codegraph @

third_party/rust/chrono/src/naive/datetime/tests.rs

raw
1 use super::NaiveDateTime;
2 use crate::{Datelike, FixedOffset, MappedLocalTime, NaiveDate, TimeDelta, Utc};
3
4 #[test]
5 fn test_datetime_add() {
6 fn check(
7 (y, m, d, h, n, s): (i32, u32, u32, u32, u32, u32),
8 rhs: TimeDelta,
9 result: Option<(i32, u32, u32, u32, u32, u32)>,
10 ) {
11 let lhs = NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_opt(h, n, s).unwrap();
12 let sum = result.map(|(y, m, d, h, n, s)| {
13 NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_opt(h, n, s).unwrap()
14 });
15 assert_eq!(lhs.checked_add_signed(rhs), sum);
16 assert_eq!(lhs.checked_sub_signed(-rhs), sum);
17 }
18 let seconds = |s| TimeDelta::try_seconds(s).unwrap();
19
20 check((2014, 5, 6, 7, 8, 9), seconds(3600 + 60 + 1), Some((2014, 5, 6, 8, 9, 10)));
21 check((2014, 5, 6, 7, 8, 9), seconds(-(3600 + 60 + 1)), Some((2014, 5, 6, 6, 7, 8)));
22 check((2014, 5, 6, 7, 8, 9), seconds(86399), Some((2014, 5, 7, 7, 8, 8)));
23 check((2014, 5, 6, 7, 8, 9), seconds(86_400 * 10), Some((2014, 5, 16, 7, 8, 9)));
24 check((2014, 5, 6, 7, 8, 9), seconds(-86_400 * 10), Some((2014, 4, 26, 7, 8, 9)));
25 check((2014, 5, 6, 7, 8, 9), seconds(86_400 * 10), Some((2014, 5, 16, 7, 8, 9)));
26
27 // overflow check
28 // assumes that we have correct values for MAX/MIN_DAYS_FROM_YEAR_0 from `naive::date`.
29 // (they are private constants, but the equivalence is tested in that module.)
30 let max_days_from_year_0 =
31 NaiveDate::MAX.signed_duration_since(NaiveDate::from_ymd_opt(0, 1, 1).unwrap());
32 check((0, 1, 1, 0, 0, 0), max_days_from_year_0, Some((NaiveDate::MAX.year(), 12, 31, 0, 0, 0)));
33 check(
34 (0, 1, 1, 0, 0, 0),
35 max_days_from_year_0 + seconds(86399),
36 Some((NaiveDate::MAX.year(), 12, 31, 23, 59, 59)),
37 );
38 check((0, 1, 1, 0, 0, 0), max_days_from_year_0 + seconds(86_400), None);
39 check((0, 1, 1, 0, 0, 0), TimeDelta::MAX, None);
40
41 let min_days_from_year_0 =
42 NaiveDate::MIN.signed_duration_since(NaiveDate::from_ymd_opt(0, 1, 1).unwrap());
43 check((0, 1, 1, 0, 0, 0), min_days_from_year_0, Some((NaiveDate::MIN.year(), 1, 1, 0, 0, 0)));
44 check((0, 1, 1, 0, 0, 0), min_days_from_year_0 - seconds(1), None);
45 check((0, 1, 1, 0, 0, 0), TimeDelta::MIN, None);
46 }
47
48 #[test]
49 fn test_datetime_sub() {
50 let ymdhms =
51 |y, m, d, h, n, s| NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_opt(h, n, s).unwrap();
52 let since = NaiveDateTime::signed_duration_since;
53 assert_eq!(since(ymdhms(2014, 5, 6, 7, 8, 9), ymdhms(2014, 5, 6, 7, 8, 9)), TimeDelta::zero());
54 assert_eq!(
55 since(ymdhms(2014, 5, 6, 7, 8, 10), ymdhms(2014, 5, 6, 7, 8, 9)),
56 TimeDelta::try_seconds(1).unwrap()
57 );
58 assert_eq!(
59 since(ymdhms(2014, 5, 6, 7, 8, 9), ymdhms(2014, 5, 6, 7, 8, 10)),
60 TimeDelta::try_seconds(-1).unwrap()
61 );
62 assert_eq!(
63 since(ymdhms(2014, 5, 7, 7, 8, 9), ymdhms(2014, 5, 6, 7, 8, 10)),
64 TimeDelta::try_seconds(86399).unwrap()
65 );
66 assert_eq!(
67 since(ymdhms(2001, 9, 9, 1, 46, 39), ymdhms(1970, 1, 1, 0, 0, 0)),
68 TimeDelta::try_seconds(999_999_999).unwrap()
69 );
70 }
71
72 #[test]
73 fn test_datetime_addassignment() {
74 let ymdhms =
75 |y, m, d, h, n, s| NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_opt(h, n, s).unwrap();
76 let mut date = ymdhms(2016, 10, 1, 10, 10, 10);
77 date += TimeDelta::try_minutes(10_000_000).unwrap();
78 assert_eq!(date, ymdhms(2035, 10, 6, 20, 50, 10));
79 date += TimeDelta::try_days(10).unwrap();
80 assert_eq!(date, ymdhms(2035, 10, 16, 20, 50, 10));
81 }
82
83 #[test]
84 fn test_datetime_subassignment() {
85 let ymdhms =
86 |y, m, d, h, n, s| NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_opt(h, n, s).unwrap();
87 let mut date = ymdhms(2016, 10, 1, 10, 10, 10);
88 date -= TimeDelta::try_minutes(10_000_000).unwrap();
89 assert_eq!(date, ymdhms(1997, 9, 26, 23, 30, 10));
90 date -= TimeDelta::try_days(10).unwrap();
91 assert_eq!(date, ymdhms(1997, 9, 16, 23, 30, 10));
92 }
93
94 #[test]
95 fn test_core_duration_ops() {
96 use core::time::Duration;
97
98 let mut dt = NaiveDate::from_ymd_opt(2023, 8, 29).unwrap().and_hms_opt(11, 34, 12).unwrap();
99 let same = dt + Duration::ZERO;
100 assert_eq!(dt, same);
101
102 dt += Duration::new(3600, 0);
103 assert_eq!(dt, NaiveDate::from_ymd_opt(2023, 8, 29).unwrap().and_hms_opt(12, 34, 12).unwrap());
104 }
105
106 #[test]
107 #[should_panic]
108 fn test_core_duration_max() {
109 use core::time::Duration;
110
111 let mut utc_dt = NaiveDate::from_ymd_opt(2023, 8, 29).unwrap().and_hms_opt(11, 34, 12).unwrap();
112 utc_dt += Duration::MAX;
113 }
114
115 #[test]
116 fn test_datetime_from_str() {
117 // valid cases
118 let valid = [
119 "2001-02-03T04:05:06",
120 "2012-12-12T12:12:12",
121 "2015-02-18T23:16:09.153",
122 "2015-2-18T23:16:09.153",
123 "-77-02-18T23:16:09",
124 "+82701-05-6T15:9:60.898989898989",
125 " +82701 - 05 - 6 T 15 : 9 : 60.898989898989 ",
126 ];
127 for &s in &valid {
128 eprintln!("test_parse_naivedatetime valid {:?}", s);
129 let d = match s.parse::<NaiveDateTime>() {
130 Ok(d) => d,
131 Err(e) => panic!("parsing `{}` has failed: {}", s, e),
132 };
133 let s_ = format!("{:?}", d);
134 // `s` and `s_` may differ, but `s.parse()` and `s_.parse()` must be same
135 let d_ = match s_.parse::<NaiveDateTime>() {
136 Ok(d) => d,
137 Err(e) => {
138 panic!("`{}` is parsed into `{:?}`, but reparsing that has failed: {}", s, d, e)
139 }
140 };
141 assert!(
142 d == d_,
143 "`{}` is parsed into `{:?}`, but reparsed result \
144 `{:?}` does not match",
145 s,
146 d,
147 d_
148 );
149 }
150
151 // some invalid cases
152 // since `ParseErrorKind` is private, all we can do is to check if there was an error
153 let invalid = [
154 "", // empty
155 "x", // invalid / missing data
156 "15", // missing data
157 "15:8:9", // looks like a time (invalid date)
158 "15-8-9", // looks like a date (invalid)
159 "Fri, 09 Aug 2013 23:54:35 GMT", // valid date, wrong format
160 "Sat Jun 30 23:59:60 2012", // valid date, wrong format
161 "1441497364.649", // valid date, wrong format
162 "+1441497364.649", // valid date, wrong format
163 "+1441497364", // valid date, wrong format
164 "2014/02/03 04:05:06", // valid date, wrong format
165 "2015-15-15T15:15:15", // invalid date
166 "2012-12-12T12:12:12x", // bad timezone / trailing literal
167 "2012-12-12T12:12:12+00:00", // unexpected timezone / trailing literal
168 "2012-12-12T12:12:12 +00:00", // unexpected timezone / trailing literal
169 "2012-12-12T12:12:12 GMT", // unexpected timezone / trailing literal
170 "2012-123-12T12:12:12", // invalid month
171 "2012-12-12t12:12:12", // bad divider 't'
172 "2012-12-12 12:12:12", // missing divider 'T'
173 "2012-12-12T12:12:12Z", // trailing char 'Z'
174 "+ 82701-123-12T12:12:12", // strange year, invalid month
175 "+802701-123-12T12:12:12", // out-of-bound year, invalid month
176 ];
177 for &s in &invalid {
178 eprintln!("test_datetime_from_str invalid {:?}", s);
179 assert!(s.parse::<NaiveDateTime>().is_err());
180 }
181 }
182
183 #[test]
184 fn test_datetime_parse_from_str() {
185 let ymdhms =
186 |y, m, d, h, n, s| NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_opt(h, n, s).unwrap();
187 let ymdhmsn = |y, m, d, h, n, s, nano| {
188 NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_nano_opt(h, n, s, nano).unwrap()
189 };
190 assert_eq!(
191 NaiveDateTime::parse_from_str("2014-5-7T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"),
192 Ok(ymdhms(2014, 5, 7, 12, 34, 56))
193 ); // ignore offset
194 assert_eq!(
195 NaiveDateTime::parse_from_str("2015-W06-1 000000", "%G-W%V-%u%H%M%S"),
196 Ok(ymdhms(2015, 2, 2, 0, 0, 0))
197 );
198 assert_eq!(
199 NaiveDateTime::parse_from_str("Fri, 09 Aug 2013 23:54:35 GMT", "%a, %d %b %Y %H:%M:%S GMT"),
200 Ok(ymdhms(2013, 8, 9, 23, 54, 35))
201 );
202 assert!(
203 NaiveDateTime::parse_from_str("Sat, 09 Aug 2013 23:54:35 GMT", "%a, %d %b %Y %H:%M:%S GMT")
204 .is_err()
205 );
206 assert!(NaiveDateTime::parse_from_str("2014-5-7 Q2 12:3456", "%Y-%m-%d Q%q %H:%M:%S").is_err());
207 assert!(NaiveDateTime::parse_from_str("12:34:56", "%H:%M:%S").is_err()); // insufficient
208 assert_eq!(
209 NaiveDateTime::parse_from_str("1441497364", "%s"),
210 Ok(ymdhms(2015, 9, 5, 23, 56, 4))
211 );
212 assert_eq!(
213 NaiveDateTime::parse_from_str("1283929614.1234", "%s.%f"),
214 Ok(ymdhmsn(2010, 9, 8, 7, 6, 54, 1234))
215 );
216 assert_eq!(
217 NaiveDateTime::parse_from_str("1441497364.649", "%s%.3f"),
218 Ok(ymdhmsn(2015, 9, 5, 23, 56, 4, 649000000))
219 );
220 assert_eq!(
221 NaiveDateTime::parse_from_str("1497854303.087654", "%s%.6f"),
222 Ok(ymdhmsn(2017, 6, 19, 6, 38, 23, 87654000))
223 );
224 assert_eq!(
225 NaiveDateTime::parse_from_str("1437742189.918273645", "%s%.9f"),
226 Ok(ymdhmsn(2015, 7, 24, 12, 49, 49, 918273645))
227 );
228 }
229
230 #[test]
231 fn test_datetime_parse_from_str_with_spaces() {
232 let parse_from_str = NaiveDateTime::parse_from_str;
233 let dt = NaiveDate::from_ymd_opt(2013, 8, 9).unwrap().and_hms_opt(23, 54, 35).unwrap();
234 // with varying spaces - should succeed
235 assert_eq!(parse_from_str(" Aug 09 2013 23:54:35", " %b %d %Y %H:%M:%S"), Ok(dt));
236 assert_eq!(parse_from_str("Aug 09 2013 23:54:35 ", "%b %d %Y %H:%M:%S "), Ok(dt));
237 assert_eq!(parse_from_str(" Aug 09 2013 23:54:35 ", " %b %d %Y %H:%M:%S "), Ok(dt));
238 assert_eq!(parse_from_str(" Aug 09 2013 23:54:35", " %b %d %Y %H:%M:%S"), Ok(dt));
239 assert_eq!(parse_from_str(" Aug 09 2013 23:54:35", " %b %d %Y %H:%M:%S"), Ok(dt));
240 assert_eq!(parse_from_str("\n\tAug 09 2013 23:54:35 ", "\n\t%b %d %Y %H:%M:%S "), Ok(dt));
241 assert_eq!(parse_from_str("\tAug 09 2013 23:54:35\t", "\t%b %d %Y %H:%M:%S\t"), Ok(dt));
242 assert_eq!(parse_from_str("Aug 09 2013 23:54:35", "%b %d %Y %H:%M:%S"), Ok(dt));
243 assert_eq!(parse_from_str("Aug 09 2013 23:54:35", "%b %d %Y %H:%M:%S"), Ok(dt));
244 assert_eq!(parse_from_str("Aug 09 2013\t23:54:35", "%b %d %Y\t%H:%M:%S"), Ok(dt));
245 assert_eq!(parse_from_str("Aug 09 2013\t\t23:54:35", "%b %d %Y\t\t%H:%M:%S"), Ok(dt));
246 assert_eq!(parse_from_str("Aug 09 2013 23:54:35 ", "%b %d %Y %H:%M:%S\n"), Ok(dt));
247 assert_eq!(parse_from_str("Aug 09 2013 23:54:35", "%b %d %Y\t%H:%M:%S"), Ok(dt));
248 assert_eq!(parse_from_str("Aug 09 2013 23:54:35", "%b %d %Y %H:%M:%S "), Ok(dt));
249 assert_eq!(parse_from_str("Aug 09 2013 23:54:35", " %b %d %Y %H:%M:%S"), Ok(dt));
250 assert_eq!(parse_from_str("Aug 09 2013 23:54:35", "%b %d %Y %H:%M:%S\n"), Ok(dt));
251 // with varying spaces - should fail
252 // leading space in data
253 assert!(parse_from_str(" Aug 09 2013 23:54:35", "%b %d %Y %H:%M:%S").is_err());
254 // trailing space in data
255 assert!(parse_from_str("Aug 09 2013 23:54:35 ", "%b %d %Y %H:%M:%S").is_err());
256 // trailing tab in data
257 assert!(parse_from_str("Aug 09 2013 23:54:35\t", "%b %d %Y %H:%M:%S").is_err());
258 // mismatched newlines
259 assert!(parse_from_str("\nAug 09 2013 23:54:35", "%b %d %Y %H:%M:%S\n").is_err());
260 // trailing literal in data
261 assert!(parse_from_str("Aug 09 2013 23:54:35 !!!", "%b %d %Y %H:%M:%S ").is_err());
262 }
263
264 #[test]
265 fn test_datetime_add_sub_invariant() {
266 // issue #37
267 let base = NaiveDate::from_ymd_opt(2000, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap();
268 let t = -946684799990000;
269 let time = base + TimeDelta::microseconds(t);
270 assert_eq!(t, time.signed_duration_since(base).num_microseconds().unwrap());
271 }
272
273 #[test]
274 fn test_and_local_timezone() {
275 let ndt = NaiveDate::from_ymd_opt(2022, 6, 15).unwrap().and_hms_opt(18, 59, 36).unwrap();
276 let dt_utc = ndt.and_utc();
277 assert_eq!(dt_utc.naive_local(), ndt);
278 assert_eq!(dt_utc.timezone(), Utc);
279
280 let offset_tz = FixedOffset::west_opt(4 * 3600).unwrap();
281 let dt_offset = ndt.and_local_timezone(offset_tz).unwrap();
282 assert_eq!(dt_offset.naive_local(), ndt);
283 assert_eq!(dt_offset.timezone(), offset_tz);
284 }
285
286 #[test]
287 fn test_and_utc() {
288 let ndt = NaiveDate::from_ymd_opt(2023, 1, 30).unwrap().and_hms_opt(19, 32, 33).unwrap();
289 let dt_utc = ndt.and_utc();
290 assert_eq!(dt_utc.naive_local(), ndt);
291 assert_eq!(dt_utc.timezone(), Utc);
292 }
293
294 #[test]
295 fn test_checked_add_offset() {
296 let ymdhmsm = |y, m, d, h, mn, s, mi| {
297 NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_milli_opt(h, mn, s, mi)
298 };
299
300 let positive_offset = FixedOffset::east_opt(2 * 60 * 60).unwrap();
301 // regular date
302 let dt = ymdhmsm(2023, 5, 5, 20, 10, 0, 0).unwrap();
303 assert_eq!(dt.checked_add_offset(positive_offset), ymdhmsm(2023, 5, 5, 22, 10, 0, 0));
304 // leap second is preserved
305 let dt = ymdhmsm(2023, 6, 30, 23, 59, 59, 1_000).unwrap();
306 assert_eq!(dt.checked_add_offset(positive_offset), ymdhmsm(2023, 7, 1, 1, 59, 59, 1_000));
307 // out of range
308 assert!(NaiveDateTime::MAX.checked_add_offset(positive_offset).is_none());
309
310 let negative_offset = FixedOffset::west_opt(2 * 60 * 60).unwrap();
311 // regular date
312 let dt = ymdhmsm(2023, 5, 5, 20, 10, 0, 0).unwrap();
313 assert_eq!(dt.checked_add_offset(negative_offset), ymdhmsm(2023, 5, 5, 18, 10, 0, 0));
314 // leap second is preserved
315 let dt = ymdhmsm(2023, 6, 30, 23, 59, 59, 1_000).unwrap();
316 assert_eq!(dt.checked_add_offset(negative_offset), ymdhmsm(2023, 6, 30, 21, 59, 59, 1_000));
317 // out of range
318 assert!(NaiveDateTime::MIN.checked_add_offset(negative_offset).is_none());
319 }
320
321 #[test]
322 fn test_checked_sub_offset() {
323 let ymdhmsm = |y, m, d, h, mn, s, mi| {
324 NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_milli_opt(h, mn, s, mi)
325 };
326
327 let positive_offset = FixedOffset::east_opt(2 * 60 * 60).unwrap();
328 // regular date
329 let dt = ymdhmsm(2023, 5, 5, 20, 10, 0, 0).unwrap();
330 assert_eq!(dt.checked_sub_offset(positive_offset), ymdhmsm(2023, 5, 5, 18, 10, 0, 0));
331 // leap second is preserved
332 let dt = ymdhmsm(2023, 6, 30, 23, 59, 59, 1_000).unwrap();
333 assert_eq!(dt.checked_sub_offset(positive_offset), ymdhmsm(2023, 6, 30, 21, 59, 59, 1_000));
334 // out of range
335 assert!(NaiveDateTime::MIN.checked_sub_offset(positive_offset).is_none());
336
337 let negative_offset = FixedOffset::west_opt(2 * 60 * 60).unwrap();
338 // regular date
339 let dt = ymdhmsm(2023, 5, 5, 20, 10, 0, 0).unwrap();
340 assert_eq!(dt.checked_sub_offset(negative_offset), ymdhmsm(2023, 5, 5, 22, 10, 0, 0));
341 // leap second is preserved
342 let dt = ymdhmsm(2023, 6, 30, 23, 59, 59, 1_000).unwrap();
343 assert_eq!(dt.checked_sub_offset(negative_offset), ymdhmsm(2023, 7, 1, 1, 59, 59, 1_000));
344 // out of range
345 assert!(NaiveDateTime::MAX.checked_sub_offset(negative_offset).is_none());
346
347 assert_eq!(dt.checked_add_offset(positive_offset), Some(dt + positive_offset));
348 assert_eq!(dt.checked_sub_offset(positive_offset), Some(dt - positive_offset));
349 }
350
351 #[test]
352 fn test_overflowing_add_offset() {
353 let ymdhmsm = |y, m, d, h, mn, s, mi| {
354 NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_milli_opt(h, mn, s, mi).unwrap()
355 };
356 let positive_offset = FixedOffset::east_opt(2 * 60 * 60).unwrap();
357 // regular date
358 let dt = ymdhmsm(2023, 5, 5, 20, 10, 0, 0);
359 assert_eq!(dt.overflowing_add_offset(positive_offset), ymdhmsm(2023, 5, 5, 22, 10, 0, 0));
360 // leap second is preserved
361 let dt = ymdhmsm(2023, 6, 30, 23, 59, 59, 1_000);
362 assert_eq!(dt.overflowing_add_offset(positive_offset), ymdhmsm(2023, 7, 1, 1, 59, 59, 1_000));
363 // out of range
364 assert!(NaiveDateTime::MAX.overflowing_add_offset(positive_offset) > NaiveDateTime::MAX);
365
366 let negative_offset = FixedOffset::west_opt(2 * 60 * 60).unwrap();
367 // regular date
368 let dt = ymdhmsm(2023, 5, 5, 20, 10, 0, 0);
369 assert_eq!(dt.overflowing_add_offset(negative_offset), ymdhmsm(2023, 5, 5, 18, 10, 0, 0));
370 // leap second is preserved
371 let dt = ymdhmsm(2023, 6, 30, 23, 59, 59, 1_000);
372 assert_eq!(dt.overflowing_add_offset(negative_offset), ymdhmsm(2023, 6, 30, 21, 59, 59, 1_000));
373 // out of range
374 assert!(NaiveDateTime::MIN.overflowing_add_offset(negative_offset) < NaiveDateTime::MIN);
375 }
376
377 #[test]
378 fn test_and_timezone_min_max_dates() {
379 for offset_hour in -23..=23 {
380 dbg!(offset_hour);
381 let offset = FixedOffset::east_opt(offset_hour * 60 * 60).unwrap();
382
383 let local_max = NaiveDateTime::MAX.and_local_timezone(offset);
384 if offset_hour >= 0 {
385 assert_eq!(local_max.unwrap().naive_local(), NaiveDateTime::MAX);
386 } else {
387 assert_eq!(local_max, MappedLocalTime::None);
388 }
389 let local_min = NaiveDateTime::MIN.and_local_timezone(offset);
390 if offset_hour <= 0 {
391 assert_eq!(local_min.unwrap().naive_local(), NaiveDateTime::MIN);
392 } else {
393 assert_eq!(local_min, MappedLocalTime::None);
394 }
395 }
396 }
397
398 #[test]
399 #[cfg(feature = "rkyv-validation")]
400 fn test_rkyv_validation() {
401 let dt_min = NaiveDateTime::MIN;
402 let bytes = rkyv::to_bytes::<_, 12>(&dt_min).unwrap();
403 assert_eq!(rkyv::from_bytes::<NaiveDateTime>(&bytes).unwrap(), dt_min);
404
405 let dt_max = NaiveDateTime::MAX;
406 let bytes = rkyv::to_bytes::<_, 12>(&dt_max).unwrap();
407 assert_eq!(rkyv::from_bytes::<NaiveDateTime>(&bytes).unwrap(), dt_max);
408 }