Debian Codegraph @

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

raw
1 use super::DateTime;
2 use crate::naive::{NaiveDate, NaiveTime};
3 #[cfg(feature = "clock")]
4 use crate::offset::Local;
5 use crate::offset::{FixedOffset, Offset, TimeZone, Utc};
6 use crate::{Datelike, Days, MappedLocalTime, Months, NaiveDateTime, TimeDelta, Timelike, Weekday};
7
8 #[derive(Clone)]
9 struct DstTester;
10
11 impl DstTester {
12 fn winter_offset() -> FixedOffset {
13 FixedOffset::east_opt(8 * 60 * 60).unwrap()
14 }
15 fn summer_offset() -> FixedOffset {
16 FixedOffset::east_opt(9 * 60 * 60).unwrap()
17 }
18
19 const TO_WINTER_MONTH_DAY: (u32, u32) = (4, 15);
20 const TO_SUMMER_MONTH_DAY: (u32, u32) = (9, 15);
21
22 fn transition_start_local() -> NaiveTime {
23 NaiveTime::from_hms_opt(2, 0, 0).unwrap()
24 }
25 }
26
27 impl TimeZone for DstTester {
28 type Offset = FixedOffset;
29
30 fn from_offset(_: &Self::Offset) -> Self {
31 DstTester
32 }
33
34 fn offset_from_local_date(&self, _: &NaiveDate) -> crate::MappedLocalTime<Self::Offset> {
35 unimplemented!()
36 }
37
38 fn offset_from_local_datetime(
39 &self,
40 local: &NaiveDateTime,
41 ) -> crate::MappedLocalTime<Self::Offset> {
42 let local_to_winter_transition_start = NaiveDate::from_ymd_opt(
43 local.year(),
44 DstTester::TO_WINTER_MONTH_DAY.0,
45 DstTester::TO_WINTER_MONTH_DAY.1,
46 )
47 .unwrap()
48 .and_time(DstTester::transition_start_local());
49
50 let local_to_winter_transition_end = NaiveDate::from_ymd_opt(
51 local.year(),
52 DstTester::TO_WINTER_MONTH_DAY.0,
53 DstTester::TO_WINTER_MONTH_DAY.1,
54 )
55 .unwrap()
56 .and_time(DstTester::transition_start_local() - TimeDelta::try_hours(1).unwrap());
57
58 let local_to_summer_transition_start = NaiveDate::from_ymd_opt(
59 local.year(),
60 DstTester::TO_SUMMER_MONTH_DAY.0,
61 DstTester::TO_SUMMER_MONTH_DAY.1,
62 )
63 .unwrap()
64 .and_time(DstTester::transition_start_local());
65
66 let local_to_summer_transition_end = NaiveDate::from_ymd_opt(
67 local.year(),
68 DstTester::TO_SUMMER_MONTH_DAY.0,
69 DstTester::TO_SUMMER_MONTH_DAY.1,
70 )
71 .unwrap()
72 .and_time(DstTester::transition_start_local() + TimeDelta::try_hours(1).unwrap());
73
74 if *local < local_to_winter_transition_end || *local >= local_to_summer_transition_end {
75 MappedLocalTime::Single(DstTester::summer_offset())
76 } else if *local >= local_to_winter_transition_start
77 && *local < local_to_summer_transition_start
78 {
79 MappedLocalTime::Single(DstTester::winter_offset())
80 } else if *local >= local_to_winter_transition_end
81 && *local < local_to_winter_transition_start
82 {
83 MappedLocalTime::Ambiguous(DstTester::winter_offset(), DstTester::summer_offset())
84 } else if *local >= local_to_summer_transition_start
85 && *local < local_to_summer_transition_end
86 {
87 MappedLocalTime::None
88 } else {
89 panic!("Unexpected local time {}", local)
90 }
91 }
92
93 fn offset_from_utc_date(&self, _: &NaiveDate) -> Self::Offset {
94 unimplemented!()
95 }
96
97 fn offset_from_utc_datetime(&self, utc: &NaiveDateTime) -> Self::Offset {
98 let utc_to_winter_transition = NaiveDate::from_ymd_opt(
99 utc.year(),
100 DstTester::TO_WINTER_MONTH_DAY.0,
101 DstTester::TO_WINTER_MONTH_DAY.1,
102 )
103 .unwrap()
104 .and_time(DstTester::transition_start_local())
105 - DstTester::summer_offset();
106
107 let utc_to_summer_transition = NaiveDate::from_ymd_opt(
108 utc.year(),
109 DstTester::TO_SUMMER_MONTH_DAY.0,
110 DstTester::TO_SUMMER_MONTH_DAY.1,
111 )
112 .unwrap()
113 .and_time(DstTester::transition_start_local())
114 - DstTester::winter_offset();
115
116 if *utc < utc_to_winter_transition || *utc >= utc_to_summer_transition {
117 DstTester::summer_offset()
118 } else if *utc >= utc_to_winter_transition && *utc < utc_to_summer_transition {
119 DstTester::winter_offset()
120 } else {
121 panic!("Unexpected utc time {}", utc)
122 }
123 }
124 }
125
126 #[test]
127 fn test_datetime_from_timestamp_millis() {
128 let valid_map = [
129 (1662921288000, "2022-09-11 18:34:48.000000000"),
130 (1662921288123, "2022-09-11 18:34:48.123000000"),
131 (1662921287890, "2022-09-11 18:34:47.890000000"),
132 (-2208936075000, "1900-01-01 14:38:45.000000000"),
133 (0, "1970-01-01 00:00:00.000000000"),
134 (119731017000, "1973-10-17 18:36:57.000000000"),
135 (1234567890000, "2009-02-13 23:31:30.000000000"),
136 (2034061609000, "2034-06-16 09:06:49.000000000"),
137 ];
138
139 for (timestamp_millis, _formatted) in valid_map.iter().copied() {
140 let datetime = DateTime::from_timestamp_millis(timestamp_millis).unwrap();
141 assert_eq!(timestamp_millis, datetime.timestamp_millis());
142 #[cfg(feature = "alloc")]
143 assert_eq!(datetime.format("%F %T%.9f").to_string(), _formatted);
144 }
145
146 let invalid = [i64::MAX, i64::MIN];
147
148 for timestamp_millis in invalid.iter().copied() {
149 let datetime = DateTime::from_timestamp_millis(timestamp_millis);
150 assert!(datetime.is_none());
151 }
152
153 // Test that the result of `from_timestamp_millis` compares equal to
154 // that of `from_timestamp_opt`.
155 let secs_test = [0, 1, 2, 1000, 1234, 12345678, -1, -2, -1000, -12345678];
156 for secs in secs_test.iter().cloned() {
157 assert_eq!(DateTime::from_timestamp_millis(secs * 1000), DateTime::from_timestamp(secs, 0));
158 }
159 }
160
161 #[test]
162 fn test_datetime_from_timestamp_micros() {
163 let valid_map = [
164 (1662921288000000, "2022-09-11 18:34:48.000000000"),
165 (1662921288123456, "2022-09-11 18:34:48.123456000"),
166 (1662921287890000, "2022-09-11 18:34:47.890000000"),
167 (-2208936075000000, "1900-01-01 14:38:45.000000000"),
168 (0, "1970-01-01 00:00:00.000000000"),
169 (119731017000000, "1973-10-17 18:36:57.000000000"),
170 (1234567890000000, "2009-02-13 23:31:30.000000000"),
171 (2034061609000000, "2034-06-16 09:06:49.000000000"),
172 ];
173
174 for (timestamp_micros, _formatted) in valid_map.iter().copied() {
175 let datetime = DateTime::from_timestamp_micros(timestamp_micros).unwrap();
176 assert_eq!(timestamp_micros, datetime.timestamp_micros());
177 #[cfg(feature = "alloc")]
178 assert_eq!(datetime.format("%F %T%.9f").to_string(), _formatted);
179 }
180
181 let invalid = [i64::MAX, i64::MIN];
182
183 for timestamp_micros in invalid.iter().copied() {
184 let datetime = DateTime::from_timestamp_micros(timestamp_micros);
185 assert!(datetime.is_none());
186 }
187
188 // Test that the result of `TimeZone::timestamp_micros` compares equal to
189 // that of `TimeZone::timestamp_opt`.
190 let secs_test = [0, 1, 2, 1000, 1234, 12345678, -1, -2, -1000, -12345678];
191 for secs in secs_test.iter().copied() {
192 assert_eq!(
193 DateTime::from_timestamp_micros(secs * 1_000_000),
194 DateTime::from_timestamp(secs, 0)
195 );
196 }
197 }
198
199 #[test]
200 fn test_datetime_from_timestamp_nanos() {
201 let valid_map = [
202 (1662921288000000000, "2022-09-11 18:34:48.000000000"),
203 (1662921288123456000, "2022-09-11 18:34:48.123456000"),
204 (1662921288123456789, "2022-09-11 18:34:48.123456789"),
205 (1662921287890000000, "2022-09-11 18:34:47.890000000"),
206 (-2208936075000000000, "1900-01-01 14:38:45.000000000"),
207 (-5337182663000000000, "1800-11-15 01:15:37.000000000"),
208 (0, "1970-01-01 00:00:00.000000000"),
209 (119731017000000000, "1973-10-17 18:36:57.000000000"),
210 (1234567890000000000, "2009-02-13 23:31:30.000000000"),
211 (2034061609000000000, "2034-06-16 09:06:49.000000000"),
212 ];
213
214 for (timestamp_nanos, _formatted) in valid_map.iter().copied() {
215 let datetime = DateTime::from_timestamp_nanos(timestamp_nanos);
216 assert_eq!(timestamp_nanos, datetime.timestamp_nanos_opt().unwrap());
217 #[cfg(feature = "alloc")]
218 assert_eq!(datetime.format("%F %T%.9f").to_string(), _formatted);
219 }
220
221 const A_BILLION: i64 = 1_000_000_000;
222 // Maximum datetime in nanoseconds
223 let maximum = "2262-04-11T23:47:16.854775804UTC";
224 let parsed: DateTime<Utc> = maximum.parse().unwrap();
225 let nanos = parsed.timestamp_nanos_opt().unwrap();
226 assert_eq!(
227 Some(DateTime::from_timestamp_nanos(nanos)),
228 DateTime::from_timestamp(nanos / A_BILLION, (nanos % A_BILLION) as u32)
229 );
230 // Minimum datetime in nanoseconds
231 let minimum = "1677-09-21T00:12:44.000000000UTC";
232 let parsed: DateTime<Utc> = minimum.parse().unwrap();
233 let nanos = parsed.timestamp_nanos_opt().unwrap();
234 assert_eq!(
235 Some(DateTime::from_timestamp_nanos(nanos)),
236 DateTime::from_timestamp(nanos / A_BILLION, (nanos % A_BILLION) as u32)
237 );
238
239 // Test that the result of `TimeZone::timestamp_nanos` compares equal to
240 // that of `TimeZone::timestamp_opt`.
241 let secs_test = [0, 1, 2, 1000, 1234, 12345678, -1, -2, -1000, -12345678];
242 for secs in secs_test.iter().copied() {
243 assert_eq!(
244 Some(DateTime::from_timestamp_nanos(secs * 1_000_000_000)),
245 DateTime::from_timestamp(secs, 0)
246 );
247 }
248 }
249
250 #[test]
251 fn test_datetime_from_timestamp() {
252 let from_timestamp = |secs| DateTime::from_timestamp(secs, 0);
253 let ymdhms = |y, m, d, h, n, s| {
254 NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_opt(h, n, s).unwrap().and_utc()
255 };
256 assert_eq!(from_timestamp(-1), Some(ymdhms(1969, 12, 31, 23, 59, 59)));
257 assert_eq!(from_timestamp(0), Some(ymdhms(1970, 1, 1, 0, 0, 0)));
258 assert_eq!(from_timestamp(1), Some(ymdhms(1970, 1, 1, 0, 0, 1)));
259 assert_eq!(from_timestamp(1_000_000_000), Some(ymdhms(2001, 9, 9, 1, 46, 40)));
260 assert_eq!(from_timestamp(0x7fffffff), Some(ymdhms(2038, 1, 19, 3, 14, 7)));
261 assert_eq!(from_timestamp(i64::MIN), None);
262 assert_eq!(from_timestamp(i64::MAX), None);
263 }
264
265 #[test]
266 fn test_datetime_timestamp() {
267 let to_timestamp = |y, m, d, h, n, s| {
268 NaiveDate::from_ymd_opt(y, m, d)
269 .unwrap()
270 .and_hms_opt(h, n, s)
271 .unwrap()
272 .and_utc()
273 .timestamp()
274 };
275 assert_eq!(to_timestamp(1969, 12, 31, 23, 59, 59), -1);
276 assert_eq!(to_timestamp(1970, 1, 1, 0, 0, 0), 0);
277 assert_eq!(to_timestamp(1970, 1, 1, 0, 0, 1), 1);
278 assert_eq!(to_timestamp(2001, 9, 9, 1, 46, 40), 1_000_000_000);
279 assert_eq!(to_timestamp(2038, 1, 19, 3, 14, 7), 0x7fffffff);
280 }
281
282 #[test]
283 fn test_nanosecond_range() {
284 const A_BILLION: i64 = 1_000_000_000;
285 let maximum = "2262-04-11T23:47:16.854775804UTC";
286 let parsed: DateTime<Utc> = maximum.parse().unwrap();
287 let nanos = parsed.timestamp_nanos_opt().unwrap();
288 assert_eq!(
289 parsed,
290 DateTime::<Utc>::from_timestamp(nanos / A_BILLION, (nanos % A_BILLION) as u32).unwrap()
291 );
292
293 let minimum = "1677-09-21T00:12:44.000000000UTC";
294 let parsed: DateTime<Utc> = minimum.parse().unwrap();
295 let nanos = parsed.timestamp_nanos_opt().unwrap();
296 assert_eq!(
297 parsed,
298 DateTime::<Utc>::from_timestamp(nanos / A_BILLION, (nanos % A_BILLION) as u32).unwrap()
299 );
300
301 // Just beyond range
302 let maximum = "2262-04-11T23:47:16.854775804UTC";
303 let parsed: DateTime<Utc> = maximum.parse().unwrap();
304 let beyond_max = parsed + TimeDelta::try_milliseconds(300).unwrap();
305 assert!(beyond_max.timestamp_nanos_opt().is_none());
306
307 // Far beyond range
308 let maximum = "2262-04-11T23:47:16.854775804UTC";
309 let parsed: DateTime<Utc> = maximum.parse().unwrap();
310 let beyond_max = parsed + Days::new(365);
311 assert!(beyond_max.timestamp_nanos_opt().is_none());
312 }
313
314 #[test]
315 fn test_datetime_add_days() {
316 let est = FixedOffset::west_opt(5 * 60 * 60).unwrap();
317 let kst = FixedOffset::east_opt(9 * 60 * 60).unwrap();
318
319 assert_eq!(
320 format!("{}", est.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap() + Days::new(5)),
321 "2014-05-11 07:08:09 -05:00"
322 );
323 assert_eq!(
324 format!("{}", kst.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap() + Days::new(5)),
325 "2014-05-11 07:08:09 +09:00"
326 );
327
328 assert_eq!(
329 format!("{}", est.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap() + Days::new(35)),
330 "2014-06-10 07:08:09 -05:00"
331 );
332 assert_eq!(
333 format!("{}", kst.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap() + Days::new(35)),
334 "2014-06-10 07:08:09 +09:00"
335 );
336
337 assert_eq!(
338 format!("{}", DstTester.with_ymd_and_hms(2014, 4, 6, 7, 8, 9).unwrap() + Days::new(5)),
339 "2014-04-11 07:08:09 +09:00"
340 );
341 assert_eq!(
342 format!("{}", DstTester.with_ymd_and_hms(2014, 4, 6, 7, 8, 9).unwrap() + Days::new(10)),
343 "2014-04-16 07:08:09 +08:00"
344 );
345
346 assert_eq!(
347 format!("{}", DstTester.with_ymd_and_hms(2014, 9, 6, 7, 8, 9).unwrap() + Days::new(5)),
348 "2014-09-11 07:08:09 +08:00"
349 );
350 assert_eq!(
351 format!("{}", DstTester.with_ymd_and_hms(2014, 9, 6, 7, 8, 9).unwrap() + Days::new(10)),
352 "2014-09-16 07:08:09 +09:00"
353 );
354 }
355
356 #[test]
357 fn test_datetime_sub_days() {
358 let est = FixedOffset::west_opt(5 * 60 * 60).unwrap();
359 let kst = FixedOffset::east_opt(9 * 60 * 60).unwrap();
360
361 assert_eq!(
362 format!("{}", est.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap() - Days::new(5)),
363 "2014-05-01 07:08:09 -05:00"
364 );
365 assert_eq!(
366 format!("{}", kst.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap() - Days::new(5)),
367 "2014-05-01 07:08:09 +09:00"
368 );
369
370 assert_eq!(
371 format!("{}", est.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap() - Days::new(35)),
372 "2014-04-01 07:08:09 -05:00"
373 );
374 assert_eq!(
375 format!("{}", kst.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap() - Days::new(35)),
376 "2014-04-01 07:08:09 +09:00"
377 );
378 }
379
380 #[test]
381 fn test_datetime_add_months() {
382 let est = FixedOffset::west_opt(5 * 60 * 60).unwrap();
383 let kst = FixedOffset::east_opt(9 * 60 * 60).unwrap();
384
385 assert_eq!(
386 format!("{}", est.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap() + Months::new(1)),
387 "2014-06-06 07:08:09 -05:00"
388 );
389 assert_eq!(
390 format!("{}", kst.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap() + Months::new(1)),
391 "2014-06-06 07:08:09 +09:00"
392 );
393
394 assert_eq!(
395 format!("{}", est.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap() + Months::new(5)),
396 "2014-10-06 07:08:09 -05:00"
397 );
398 assert_eq!(
399 format!("{}", kst.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap() + Months::new(5)),
400 "2014-10-06 07:08:09 +09:00"
401 );
402 }
403
404 #[test]
405 fn test_datetime_sub_months() {
406 let est = FixedOffset::west_opt(5 * 60 * 60).unwrap();
407 let kst = FixedOffset::east_opt(9 * 60 * 60).unwrap();
408
409 assert_eq!(
410 format!("{}", est.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap() - Months::new(1)),
411 "2014-04-06 07:08:09 -05:00"
412 );
413 assert_eq!(
414 format!("{}", kst.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap() - Months::new(1)),
415 "2014-04-06 07:08:09 +09:00"
416 );
417
418 assert_eq!(
419 format!("{}", est.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap() - Months::new(5)),
420 "2013-12-06 07:08:09 -05:00"
421 );
422 assert_eq!(
423 format!("{}", kst.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap() - Months::new(5)),
424 "2013-12-06 07:08:09 +09:00"
425 );
426 }
427
428 // local helper function to easily create a DateTime<FixedOffset>
429 #[allow(clippy::too_many_arguments)]
430 fn ymdhms(
431 fixedoffset: &FixedOffset,
432 year: i32,
433 month: u32,
434 day: u32,
435 hour: u32,
436 min: u32,
437 sec: u32,
438 ) -> DateTime<FixedOffset> {
439 fixedoffset.with_ymd_and_hms(year, month, day, hour, min, sec).unwrap()
440 }
441
442 // local helper function to easily create a DateTime<FixedOffset>
443 #[allow(clippy::too_many_arguments)]
444 fn ymdhms_milli(
445 fixedoffset: &FixedOffset,
446 year: i32,
447 month: u32,
448 day: u32,
449 hour: u32,
450 min: u32,
451 sec: u32,
452 milli: u32,
453 ) -> DateTime<FixedOffset> {
454 fixedoffset
455 .with_ymd_and_hms(year, month, day, hour, min, sec)
456 .unwrap()
457 .with_nanosecond(milli * 1_000_000)
458 .unwrap()
459 }
460
461 // local helper function to easily create a DateTime<FixedOffset>
462 #[allow(clippy::too_many_arguments)]
463 #[cfg(feature = "alloc")]
464 fn ymdhms_micro(
465 fixedoffset: &FixedOffset,
466 year: i32,
467 month: u32,
468 day: u32,
469 hour: u32,
470 min: u32,
471 sec: u32,
472 micro: u32,
473 ) -> DateTime<FixedOffset> {
474 fixedoffset
475 .with_ymd_and_hms(year, month, day, hour, min, sec)
476 .unwrap()
477 .with_nanosecond(micro * 1000)
478 .unwrap()
479 }
480
481 // local helper function to easily create a DateTime<FixedOffset>
482 #[allow(clippy::too_many_arguments)]
483 #[cfg(feature = "alloc")]
484 fn ymdhms_nano(
485 fixedoffset: &FixedOffset,
486 year: i32,
487 month: u32,
488 day: u32,
489 hour: u32,
490 min: u32,
491 sec: u32,
492 nano: u32,
493 ) -> DateTime<FixedOffset> {
494 fixedoffset
495 .with_ymd_and_hms(year, month, day, hour, min, sec)
496 .unwrap()
497 .with_nanosecond(nano)
498 .unwrap()
499 }
500
501 // local helper function to easily create a DateTime<Utc>
502 #[cfg(feature = "alloc")]
503 fn ymdhms_utc(year: i32, month: u32, day: u32, hour: u32, min: u32, sec: u32) -> DateTime<Utc> {
504 Utc.with_ymd_and_hms(year, month, day, hour, min, sec).unwrap()
505 }
506
507 // local helper function to easily create a DateTime<Utc>
508 fn ymdhms_milli_utc(
509 year: i32,
510 month: u32,
511 day: u32,
512 hour: u32,
513 min: u32,
514 sec: u32,
515 milli: u32,
516 ) -> DateTime<Utc> {
517 Utc.with_ymd_and_hms(year, month, day, hour, min, sec)
518 .unwrap()
519 .with_nanosecond(milli * 1_000_000)
520 .unwrap()
521 }
522
523 #[test]
524 fn test_datetime_offset() {
525 let est = FixedOffset::west_opt(5 * 60 * 60).unwrap();
526 let edt = FixedOffset::west_opt(4 * 60 * 60).unwrap();
527 let kst = FixedOffset::east_opt(9 * 60 * 60).unwrap();
528
529 assert_eq!(
530 format!("{}", Utc.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap()),
531 "2014-05-06 07:08:09 UTC"
532 );
533 assert_eq!(
534 format!("{}", edt.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap()),
535 "2014-05-06 07:08:09 -04:00"
536 );
537 assert_eq!(
538 format!("{}", kst.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap()),
539 "2014-05-06 07:08:09 +09:00"
540 );
541 assert_eq!(
542 format!("{:?}", Utc.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap()),
543 "2014-05-06T07:08:09Z"
544 );
545 assert_eq!(
546 format!("{:?}", edt.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap()),
547 "2014-05-06T07:08:09-04:00"
548 );
549 assert_eq!(
550 format!("{:?}", kst.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap()),
551 "2014-05-06T07:08:09+09:00"
552 );
553
554 // edge cases
555 assert_eq!(
556 format!("{:?}", Utc.with_ymd_and_hms(2014, 5, 6, 0, 0, 0).unwrap()),
557 "2014-05-06T00:00:00Z"
558 );
559 assert_eq!(
560 format!("{:?}", edt.with_ymd_and_hms(2014, 5, 6, 0, 0, 0).unwrap()),
561 "2014-05-06T00:00:00-04:00"
562 );
563 assert_eq!(
564 format!("{:?}", kst.with_ymd_and_hms(2014, 5, 6, 0, 0, 0).unwrap()),
565 "2014-05-06T00:00:00+09:00"
566 );
567 assert_eq!(
568 format!("{:?}", Utc.with_ymd_and_hms(2014, 5, 6, 23, 59, 59).unwrap()),
569 "2014-05-06T23:59:59Z"
570 );
571 assert_eq!(
572 format!("{:?}", edt.with_ymd_and_hms(2014, 5, 6, 23, 59, 59).unwrap()),
573 "2014-05-06T23:59:59-04:00"
574 );
575 assert_eq!(
576 format!("{:?}", kst.with_ymd_and_hms(2014, 5, 6, 23, 59, 59).unwrap()),
577 "2014-05-06T23:59:59+09:00"
578 );
579
580 let dt = Utc.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap();
581 assert_eq!(dt, edt.with_ymd_and_hms(2014, 5, 6, 3, 8, 9).unwrap());
582 assert_eq!(
583 dt + TimeDelta::try_seconds(3600 + 60 + 1).unwrap(),
584 Utc.with_ymd_and_hms(2014, 5, 6, 8, 9, 10).unwrap()
585 );
586 assert_eq!(
587 dt.signed_duration_since(edt.with_ymd_and_hms(2014, 5, 6, 10, 11, 12).unwrap()),
588 TimeDelta::try_seconds(-7 * 3600 - 3 * 60 - 3).unwrap()
589 );
590
591 assert_eq!(*Utc.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap().offset(), Utc);
592 assert_eq!(*edt.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap().offset(), edt);
593 assert!(*edt.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap().offset() != est);
594 }
595
596 #[test]
597 #[allow(clippy::needless_borrow, clippy::op_ref)]
598 fn signed_duration_since_autoref() {
599 let dt1 = Utc.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap();
600 let dt2 = Utc.with_ymd_and_hms(2014, 3, 4, 5, 6, 7).unwrap();
601 let diff1 = dt1.signed_duration_since(dt2); // Copy/consume
602 #[allow(clippy::needless_borrows_for_generic_args)]
603 let diff2 = dt2.signed_duration_since(&dt1); // Take by reference
604 assert_eq!(diff1, -diff2);
605
606 let diff1 = dt1 - &dt2; // We can choose to subtract rhs by reference
607 let diff2 = dt2 - dt1; // Or consume rhs
608 assert_eq!(diff1, -diff2);
609 }
610
611 #[test]
612 fn test_datetime_date_and_time() {
613 let tz = FixedOffset::east_opt(5 * 60 * 60).unwrap();
614 let d = tz.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap();
615 assert_eq!(d.time(), NaiveTime::from_hms_opt(7, 8, 9).unwrap());
616 assert_eq!(d.date_naive(), NaiveDate::from_ymd_opt(2014, 5, 6).unwrap());
617
618 let tz = FixedOffset::east_opt(4 * 60 * 60).unwrap();
619 let d = tz.with_ymd_and_hms(2016, 5, 4, 3, 2, 1).unwrap();
620 assert_eq!(d.time(), NaiveTime::from_hms_opt(3, 2, 1).unwrap());
621 assert_eq!(d.date_naive(), NaiveDate::from_ymd_opt(2016, 5, 4).unwrap());
622
623 let tz = FixedOffset::west_opt(13 * 60 * 60).unwrap();
624 let d = tz.with_ymd_and_hms(2017, 8, 9, 12, 34, 56).unwrap();
625 assert_eq!(d.time(), NaiveTime::from_hms_opt(12, 34, 56).unwrap());
626 assert_eq!(d.date_naive(), NaiveDate::from_ymd_opt(2017, 8, 9).unwrap());
627
628 let utc_d = Utc.with_ymd_and_hms(2017, 8, 9, 12, 34, 56).unwrap();
629 assert!(utc_d < d);
630 }
631
632 #[test]
633 #[cfg(feature = "clock")]
634 fn test_datetime_with_timezone() {
635 let local_now = Local::now();
636 let utc_now = local_now.with_timezone(&Utc);
637 let local_now2 = utc_now.with_timezone(&Local);
638 assert_eq!(local_now, local_now2);
639 }
640
641 #[test]
642 #[cfg(feature = "alloc")]
643 fn test_datetime_rfc2822() {
644 let edt = FixedOffset::east_opt(5 * 60 * 60).unwrap();
645
646 // timezone 0
647 assert_eq!(
648 Utc.with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap().to_rfc2822(),
649 "Wed, 18 Feb 2015 23:16:09 +0000"
650 );
651 assert_eq!(
652 Utc.with_ymd_and_hms(2015, 2, 1, 23, 16, 9).unwrap().to_rfc2822(),
653 "Sun, 1 Feb 2015 23:16:09 +0000"
654 );
655 // timezone +05
656 assert_eq!(
657 edt.from_local_datetime(
658 &NaiveDate::from_ymd_opt(2015, 2, 18)
659 .unwrap()
660 .and_hms_milli_opt(23, 16, 9, 150)
661 .unwrap()
662 )
663 .unwrap()
664 .to_rfc2822(),
665 "Wed, 18 Feb 2015 23:16:09 +0500"
666 );
667 assert_eq!(
668 DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:59:60 +0500"),
669 Ok(edt
670 .from_local_datetime(
671 &NaiveDate::from_ymd_opt(2015, 2, 18)
672 .unwrap()
673 .and_hms_milli_opt(23, 59, 59, 1_000)
674 .unwrap()
675 )
676 .unwrap())
677 );
678 assert!(DateTime::parse_from_rfc2822("31 DEC 262143 23:59 -2359").is_err());
679 assert_eq!(
680 DateTime::parse_from_rfc3339("2015-02-18T23:59:60.234567+05:00"),
681 Ok(edt
682 .from_local_datetime(
683 &NaiveDate::from_ymd_opt(2015, 2, 18)
684 .unwrap()
685 .and_hms_micro_opt(23, 59, 59, 1_234_567)
686 .unwrap()
687 )
688 .unwrap())
689 );
690 // seconds 60
691 assert_eq!(
692 edt.from_local_datetime(
693 &NaiveDate::from_ymd_opt(2015, 2, 18)
694 .unwrap()
695 .and_hms_micro_opt(23, 59, 59, 1_234_567)
696 .unwrap()
697 )
698 .unwrap()
699 .to_rfc2822(),
700 "Wed, 18 Feb 2015 23:59:60 +0500"
701 );
702
703 assert_eq!(
704 DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:16:09 +0000"),
705 Ok(FixedOffset::east_opt(0).unwrap().with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap())
706 );
707 assert_eq!(
708 DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:16:09 -0000"),
709 Ok(FixedOffset::east_opt(0).unwrap().with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap())
710 );
711 assert_eq!(
712 ymdhms_micro(&edt, 2015, 2, 18, 23, 59, 59, 1_234_567).to_rfc2822(),
713 "Wed, 18 Feb 2015 23:59:60 +0500"
714 );
715 assert_eq!(
716 DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:59:58 +0500"),
717 Ok(ymdhms(&edt, 2015, 2, 18, 23, 59, 58))
718 );
719 assert_ne!(
720 DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:59:58 +0500"),
721 Ok(ymdhms_milli(&edt, 2015, 2, 18, 23, 59, 58, 500))
722 );
723
724 // many varying whitespace intermixed
725 assert_eq!(
726 DateTime::parse_from_rfc2822(
727 "\t\t\tWed,\n\t\t18 \r\n\t\tFeb \u{3000} 2015\r\n\t\t\t23:59:58 \t+0500"
728 ),
729 Ok(ymdhms(&edt, 2015, 2, 18, 23, 59, 58))
730 );
731 // example from RFC 2822 Appendix A.5.
732 assert_eq!(
733 DateTime::parse_from_rfc2822(
734 "Thu,\n\t13\n Feb\n 1969\n 23:32\n -0330 (Newfoundland Time)"
735 ),
736 Ok(
737 ymdhms(&FixedOffset::east_opt(-3 * 60 * 60 - 30 * 60).unwrap(), 1969, 2, 13, 23, 32, 0,)
738 )
739 );
740 // example from RFC 2822 Appendix A.5. without trailing " (Newfoundland Time)"
741 assert_eq!(
742 DateTime::parse_from_rfc2822(
743 "Thu,\n\t13\n Feb\n 1969\n 23:32\n -0330"
744 ),
745 Ok(
746 ymdhms(&FixedOffset::east_opt(-3 * 60 * 60 - 30 * 60).unwrap(), 1969, 2, 13, 23, 32, 0,)
747 )
748 );
749
750 // bad year
751 assert!(DateTime::parse_from_rfc2822("31 DEC 262143 23:59 -2359").is_err());
752 // wrong format
753 assert!(DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:16:09 +00:00").is_err());
754 // full name day of week
755 assert!(DateTime::parse_from_rfc2822("Wednesday, 18 Feb 2015 23:16:09 +0000").is_err());
756 // full name day of week
757 assert!(DateTime::parse_from_rfc2822("Wednesday 18 Feb 2015 23:16:09 +0000").is_err());
758 // wrong day of week separator '.'
759 assert!(DateTime::parse_from_rfc2822("Wed. 18 Feb 2015 23:16:09 +0000").is_err());
760 // *trailing* space causes failure
761 assert!(DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:16:09 +0000 ").is_err());
762 }
763
764 #[test]
765 #[cfg(feature = "alloc")]
766 fn test_datetime_rfc3339() {
767 let edt5 = FixedOffset::east_opt(5 * 60 * 60).unwrap();
768 let edt0 = FixedOffset::east_opt(0).unwrap();
769
770 // timezone 0
771 assert_eq!(
772 Utc.with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap().to_rfc3339(),
773 "2015-02-18T23:16:09+00:00"
774 );
775 // timezone +05
776 assert_eq!(
777 edt5.from_local_datetime(
778 &NaiveDate::from_ymd_opt(2015, 2, 18)
779 .unwrap()
780 .and_hms_milli_opt(23, 16, 9, 150)
781 .unwrap()
782 )
783 .unwrap()
784 .to_rfc3339(),
785 "2015-02-18T23:16:09.150+05:00"
786 );
787
788 assert_eq!(ymdhms_utc(2015, 2, 18, 23, 16, 9).to_rfc3339(), "2015-02-18T23:16:09+00:00");
789 assert_eq!(
790 ymdhms_milli(&edt5, 2015, 2, 18, 23, 16, 9, 150).to_rfc3339(),
791 "2015-02-18T23:16:09.150+05:00"
792 );
793 assert_eq!(
794 ymdhms_micro(&edt5, 2015, 2, 18, 23, 59, 59, 1_234_567).to_rfc3339(),
795 "2015-02-18T23:59:60.234567+05:00"
796 );
797 assert_eq!(
798 DateTime::parse_from_rfc3339("2015-02-18T23:59:59.123+05:00"),
799 Ok(ymdhms_micro(&edt5, 2015, 2, 18, 23, 59, 59, 123_000))
800 );
801 assert_eq!(
802 DateTime::parse_from_rfc3339("2015-02-18T23:59:59.123456+05:00"),
803 Ok(ymdhms_micro(&edt5, 2015, 2, 18, 23, 59, 59, 123_456))
804 );
805 assert_eq!(
806 DateTime::parse_from_rfc3339("2015-02-18T23:59:59.123456789+05:00"),
807 Ok(ymdhms_nano(&edt5, 2015, 2, 18, 23, 59, 59, 123_456_789))
808 );
809 assert_eq!(
810 DateTime::parse_from_rfc3339("2015-02-18T23:16:09Z"),
811 Ok(ymdhms(&edt0, 2015, 2, 18, 23, 16, 9))
812 );
813
814 assert_eq!(
815 ymdhms_micro(&edt5, 2015, 2, 18, 23, 59, 59, 1_234_567).to_rfc3339(),
816 "2015-02-18T23:59:60.234567+05:00"
817 );
818 assert_eq!(
819 ymdhms_milli(&edt5, 2015, 2, 18, 23, 16, 9, 150).to_rfc3339(),
820 "2015-02-18T23:16:09.150+05:00"
821 );
822 assert_eq!(
823 DateTime::parse_from_rfc3339("2015-02-18T00:00:00.234567+05:00"),
824 Ok(ymdhms_micro(&edt5, 2015, 2, 18, 0, 0, 0, 234_567))
825 );
826 assert_eq!(
827 DateTime::parse_from_rfc3339("2015-02-18T23:16:09Z"),
828 Ok(ymdhms(&edt0, 2015, 2, 18, 23, 16, 9))
829 );
830 assert_eq!(
831 DateTime::parse_from_rfc3339("2015-02-18 23:59:60.234567+05:00"),
832 Ok(ymdhms_micro(&edt5, 2015, 2, 18, 23, 59, 59, 1_234_567))
833 );
834 assert_eq!(ymdhms_utc(2015, 2, 18, 23, 16, 9).to_rfc3339(), "2015-02-18T23:16:09+00:00");
835
836 assert!(DateTime::parse_from_rfc3339("2015-02-18T23:59:60.234567 +05:00").is_err());
837 assert!(DateTime::parse_from_rfc3339("2015-02-18T23:059:60.234567+05:00").is_err());
838 assert!(DateTime::parse_from_rfc3339("2015-02-18T23:59:60.234567+05:00PST").is_err());
839 assert!(DateTime::parse_from_rfc3339("2015-02-18T23:59:60.234567+PST").is_err());
840 assert!(DateTime::parse_from_rfc3339("2015-02-18T23:59:60.234567PST").is_err());
841 assert!(DateTime::parse_from_rfc3339("2015-02-18T23:59:60.234567+0500").is_err());
842 assert!(DateTime::parse_from_rfc3339("2015-02-18T23:59:60.234567+05:00:00").is_err());
843 assert!(DateTime::parse_from_rfc3339("2015-02-18T23:59:60.234567:+05:00").is_err());
844 assert!(DateTime::parse_from_rfc3339("2015-02-18T23:59:60.234567+05:00 ").is_err());
845 assert!(DateTime::parse_from_rfc3339(" 2015-02-18T23:59:60.234567+05:00").is_err());
846 assert!(DateTime::parse_from_rfc3339("2015- 02-18T23:59:60.234567+05:00").is_err());
847 assert!(DateTime::parse_from_rfc3339("2015-02-18T23:59:60.234567A+05:00").is_err());
848 }
849
850 #[test]
851 #[cfg(feature = "alloc")]
852 fn test_rfc3339_opts() {
853 use crate::SecondsFormat::*;
854 let pst = FixedOffset::east_opt(8 * 60 * 60).unwrap();
855 let dt = pst
856 .from_local_datetime(
857 &NaiveDate::from_ymd_opt(2018, 1, 11)
858 .unwrap()
859 .and_hms_nano_opt(10, 5, 13, 84_660_000)
860 .unwrap(),
861 )
862 .unwrap();
863 assert_eq!(dt.to_rfc3339_opts(Secs, false), "2018-01-11T10:05:13+08:00");
864 assert_eq!(dt.to_rfc3339_opts(Secs, true), "2018-01-11T10:05:13+08:00");
865 assert_eq!(dt.to_rfc3339_opts(Millis, false), "2018-01-11T10:05:13.084+08:00");
866 assert_eq!(dt.to_rfc3339_opts(Micros, false), "2018-01-11T10:05:13.084660+08:00");
867 assert_eq!(dt.to_rfc3339_opts(Nanos, false), "2018-01-11T10:05:13.084660000+08:00");
868 assert_eq!(dt.to_rfc3339_opts(AutoSi, false), "2018-01-11T10:05:13.084660+08:00");
869
870 let ut = dt.naive_utc().and_utc();
871 assert_eq!(ut.to_rfc3339_opts(Secs, false), "2018-01-11T02:05:13+00:00");
872 assert_eq!(ut.to_rfc3339_opts(Secs, true), "2018-01-11T02:05:13Z");
873 assert_eq!(ut.to_rfc3339_opts(Millis, false), "2018-01-11T02:05:13.084+00:00");
874 assert_eq!(ut.to_rfc3339_opts(Millis, true), "2018-01-11T02:05:13.084Z");
875 assert_eq!(ut.to_rfc3339_opts(Micros, true), "2018-01-11T02:05:13.084660Z");
876 assert_eq!(ut.to_rfc3339_opts(Nanos, true), "2018-01-11T02:05:13.084660000Z");
877 assert_eq!(ut.to_rfc3339_opts(AutoSi, true), "2018-01-11T02:05:13.084660Z");
878 }
879
880 #[test]
881 #[should_panic]
882 #[cfg(feature = "alloc")]
883 fn test_rfc3339_opts_nonexhaustive() {
884 use crate::SecondsFormat;
885 let dt = Utc.with_ymd_and_hms(1999, 10, 9, 1, 2, 3).unwrap();
886 let _ = dt.to_rfc3339_opts(SecondsFormat::__NonExhaustive, true);
887 }
888
889 #[test]
890 fn test_datetime_from_str() {
891 assert_eq!(
892 "2015-02-18T23:16:9.15Z".parse::<DateTime<FixedOffset>>(),
893 Ok(FixedOffset::east_opt(0)
894 .unwrap()
895 .from_local_datetime(
896 &NaiveDate::from_ymd_opt(2015, 2, 18)
897 .unwrap()
898 .and_hms_milli_opt(23, 16, 9, 150)
899 .unwrap()
900 )
901 .unwrap())
902 );
903 assert_eq!(
904 "2015-02-18T23:16:9.15Z".parse::<DateTime<Utc>>(),
905 Ok(Utc
906 .from_local_datetime(
907 &NaiveDate::from_ymd_opt(2015, 2, 18)
908 .unwrap()
909 .and_hms_milli_opt(23, 16, 9, 150)
910 .unwrap()
911 )
912 .unwrap())
913 );
914 assert_eq!(
915 "2015-02-18T23:16:9.15 UTC".parse::<DateTime<Utc>>(),
916 Ok(Utc
917 .from_local_datetime(
918 &NaiveDate::from_ymd_opt(2015, 2, 18)
919 .unwrap()
920 .and_hms_milli_opt(23, 16, 9, 150)
921 .unwrap()
922 )
923 .unwrap())
924 );
925 assert_eq!(
926 "2015-02-18T23:16:9.15UTC".parse::<DateTime<Utc>>(),
927 Ok(Utc
928 .from_local_datetime(
929 &NaiveDate::from_ymd_opt(2015, 2, 18)
930 .unwrap()
931 .and_hms_milli_opt(23, 16, 9, 150)
932 .unwrap()
933 )
934 .unwrap())
935 );
936 assert_eq!(
937 "2015-02-18T23:16:9.15Utc".parse::<DateTime<Utc>>(),
938 Ok(Utc
939 .from_local_datetime(
940 &NaiveDate::from_ymd_opt(2015, 2, 18)
941 .unwrap()
942 .and_hms_milli_opt(23, 16, 9, 150)
943 .unwrap()
944 )
945 .unwrap())
946 );
947
948 assert_eq!(
949 "2015-2-18T23:16:9.15Z".parse::<DateTime<FixedOffset>>(),
950 Ok(FixedOffset::east_opt(0)
951 .unwrap()
952 .from_local_datetime(
953 &NaiveDate::from_ymd_opt(2015, 2, 18)
954 .unwrap()
955 .and_hms_milli_opt(23, 16, 9, 150)
956 .unwrap()
957 )
958 .unwrap())
959 );
960 assert_eq!(
961 "2015-2-18T13:16:9.15-10:00".parse::<DateTime<FixedOffset>>(),
962 Ok(FixedOffset::west_opt(10 * 3600)
963 .unwrap()
964 .from_local_datetime(
965 &NaiveDate::from_ymd_opt(2015, 2, 18)
966 .unwrap()
967 .and_hms_milli_opt(13, 16, 9, 150)
968 .unwrap()
969 )
970 .unwrap())
971 );
972 assert!("2015-2-18T23:16:9.15".parse::<DateTime<FixedOffset>>().is_err());
973
974 assert_eq!(
975 "2015-2-18T23:16:9.15Z".parse::<DateTime<Utc>>(),
976 Ok(Utc
977 .from_local_datetime(
978 &NaiveDate::from_ymd_opt(2015, 2, 18)
979 .unwrap()
980 .and_hms_milli_opt(23, 16, 9, 150)
981 .unwrap()
982 )
983 .unwrap())
984 );
985 assert_eq!(
986 "2015-2-18T13:16:9.15-10:00".parse::<DateTime<Utc>>(),
987 Ok(Utc
988 .from_local_datetime(
989 &NaiveDate::from_ymd_opt(2015, 2, 18)
990 .unwrap()
991 .and_hms_milli_opt(23, 16, 9, 150)
992 .unwrap()
993 )
994 .unwrap())
995 );
996 assert!("2015-2-18T23:16:9.15".parse::<DateTime<Utc>>().is_err());
997 assert!("2015-02-18T23:16:9.15øøø".parse::<DateTime<Utc>>().is_err());
998
999 // no test for `DateTime<Local>`, we cannot verify that much.
1000 }
1001
1002 #[test]
1003 fn test_parse_datetime_utc() {
1004 // valid cases
1005 let valid = [
1006 "2001-02-03T04:05:06Z",
1007 "2001-02-03T04:05:06+0000",
1008 "2001-02-03T04:05:06-00:00",
1009 "2001-02-03T04:05:06-01:00",
1010 "2012-12-12 12:12:12Z",
1011 "2012-12-12t12:12:12Z",
1012 "2012-12-12T12:12:12Z",
1013 "2012 -12-12T12:12:12Z",
1014 "2012 -12-12T12:12:12Z",
1015 "2012- 12-12T12:12:12Z",
1016 "2012- 12-12T12:12:12Z",
1017 "2012-12-12T 12:12:12Z",
1018 "2012-12-12T12 :12:12Z",
1019 "2012-12-12T12 :12:12Z",
1020 "2012-12-12T12: 12:12Z",
1021 "2012-12-12T12: 12:12Z",
1022 "2012-12-12T12 : 12:12Z",
1023 "2012-12-12T12:12:12Z ",
1024 " 2012-12-12T12:12:12Z",
1025 "2015-02-18T23:16:09.153Z",
1026 "2015-2-18T23:16:09.153Z",
1027 "+2015-2-18T23:16:09.153Z",
1028 "-77-02-18T23:16:09Z",
1029 "+82701-05-6T15:9:60.898989898989Z",
1030 ];
1031 for &s in &valid {
1032 eprintln!("test_parse_datetime_utc valid {:?}", s);
1033 let d = match s.parse::<DateTime<Utc>>() {
1034 Ok(d) => d,
1035 Err(e) => panic!("parsing `{}` has failed: {}", s, e),
1036 };
1037 let s_ = format!("{:?}", d);
1038 // `s` and `s_` may differ, but `s.parse()` and `s_.parse()` must be same
1039 let d_ = match s_.parse::<DateTime<Utc>>() {
1040 Ok(d) => d,
1041 Err(e) => {
1042 panic!("`{}` is parsed into `{:?}`, but reparsing that has failed: {}", s, d, e)
1043 }
1044 };
1045 assert!(
1046 d == d_,
1047 "`{}` is parsed into `{:?}`, but reparsed result `{:?}` does not match",
1048 s,
1049 d,
1050 d_
1051 );
1052 }
1053
1054 // some invalid cases
1055 // since `ParseErrorKind` is private, all we can do is to check if there was an error
1056 let invalid = [
1057 "", // empty
1058 "Z", // missing data
1059 "15Z", // missing data
1060 "15:8:9Z", // missing date
1061 "15-8-9Z", // missing time or date
1062 "Fri, 09 Aug 2013 23:54:35 GMT", // valid datetime, wrong format
1063 "Sat Jun 30 23:59:60 2012", // valid datetime, wrong format
1064 "1441497364.649", // valid datetime, wrong format
1065 "+1441497364.649", // valid datetime, wrong format
1066 "+1441497364", // valid datetime, wrong format
1067 "+1441497364Z", // valid datetime, wrong format
1068 "2014/02/03 04:05:06Z", // valid datetime, wrong format
1069 "2001-02-03T04:05:0600:00", // valid datetime, timezone too close
1070 "2015-15-15T15:15:15Z", // invalid datetime
1071 "2012-12-12T12:12:12x", // invalid timezone
1072 "2012-123-12T12:12:12Z", // invalid month
1073 "2012-12-77T12:12:12Z", // invalid day
1074 "2012-12-12T26:12:12Z", // invalid hour
1075 "2012-12-12T12:61:12Z", // invalid minute
1076 "2012-12-12T12:12:62Z", // invalid second
1077 "2012-12-12 T12:12:12Z", // space after date
1078 "2012-12-12T12:12:12ZZ", // trailing literal 'Z'
1079 "+802701-12-12T12:12:12Z", // invalid year (out of bounds)
1080 "+ 2012-12-12T12:12:12Z", // invalid space before year
1081 " +82701 - 05 - 6 T 15 : 9 : 60.898989898989 Z", // valid datetime, wrong format
1082 ];
1083 for &s in &invalid {
1084 eprintln!("test_parse_datetime_utc invalid {:?}", s);
1085 assert!(s.parse::<DateTime<Utc>>().is_err());
1086 }
1087 }
1088
1089 #[test]
1090 fn test_parse_from_str() {
1091 let edt = FixedOffset::east_opt(570 * 60).unwrap();
1092 let edt0 = FixedOffset::east_opt(0).unwrap();
1093 let wdt = FixedOffset::west_opt(10 * 3600).unwrap();
1094 assert_eq!(
1095 DateTime::parse_from_str("2014-5-7T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"),
1096 Ok(ymdhms(&edt, 2014, 5, 7, 12, 34, 56))
1097 ); // ignore offset
1098 assert!(DateTime::parse_from_str("20140507000000", "%Y%m%d%H%M%S").is_err()); // no offset
1099 assert!(
1100 DateTime::parse_from_str("Fri, 09 Aug 2013 23:54:35 GMT", "%a, %d %b %Y %H:%M:%S GMT")
1101 .is_err()
1102 );
1103 assert_eq!(
1104 DateTime::parse_from_str("0", "%s").unwrap(),
1105 DateTime::from_timestamp(0, 0).unwrap().fixed_offset()
1106 );
1107
1108 assert_eq!(
1109 "2015-02-18T23:16:9.15Z".parse::<DateTime<FixedOffset>>(),
1110 Ok(ymdhms_milli(&edt0, 2015, 2, 18, 23, 16, 9, 150))
1111 );
1112 assert_eq!(
1113 "2015-02-18T23:16:9.15Z".parse::<DateTime<Utc>>(),
1114 Ok(ymdhms_milli_utc(2015, 2, 18, 23, 16, 9, 150)),
1115 );
1116 assert_eq!(
1117 "2015-02-18T23:16:9.15 UTC".parse::<DateTime<Utc>>(),
1118 Ok(ymdhms_milli_utc(2015, 2, 18, 23, 16, 9, 150))
1119 );
1120 assert_eq!(
1121 "2015-02-18T23:16:9.15UTC".parse::<DateTime<Utc>>(),
1122 Ok(ymdhms_milli_utc(2015, 2, 18, 23, 16, 9, 150))
1123 );
1124
1125 assert_eq!(
1126 "2015-2-18T23:16:9.15Z".parse::<DateTime<FixedOffset>>(),
1127 Ok(ymdhms_milli(&edt0, 2015, 2, 18, 23, 16, 9, 150))
1128 );
1129 assert_eq!(
1130 "2015-2-18T13:16:9.15-10:00".parse::<DateTime<FixedOffset>>(),
1131 Ok(ymdhms_milli(&wdt, 2015, 2, 18, 13, 16, 9, 150))
1132 );
1133 assert!("2015-2-18T23:16:9.15".parse::<DateTime<FixedOffset>>().is_err());
1134
1135 assert_eq!(
1136 "2015-2-18T23:16:9.15Z".parse::<DateTime<Utc>>(),
1137 Ok(ymdhms_milli_utc(2015, 2, 18, 23, 16, 9, 150))
1138 );
1139 assert_eq!(
1140 "2015-2-18T13:16:9.15-10:00".parse::<DateTime<Utc>>(),
1141 Ok(ymdhms_milli_utc(2015, 2, 18, 23, 16, 9, 150))
1142 );
1143 assert!("2015-2-18T23:16:9.15".parse::<DateTime<Utc>>().is_err());
1144
1145 // no test for `DateTime<Local>`, we cannot verify that much.
1146 }
1147
1148 #[test]
1149 fn test_datetime_parse_from_str() {
1150 let dt = ymdhms(&FixedOffset::east_opt(-9 * 60 * 60).unwrap(), 2013, 8, 9, 23, 54, 35);
1151 let parse = DateTime::parse_from_str;
1152
1153 // timezone variations
1154
1155 //
1156 // %Z
1157 //
1158 // wrong timezone format
1159 assert!(parse("Aug 09 2013 23:54:35 -0900", "%b %d %Y %H:%M:%S %Z").is_err());
1160 // bad timezone data?
1161 assert!(parse("Aug 09 2013 23:54:35 PST", "%b %d %Y %H:%M:%S %Z").is_err());
1162 // bad timezone data
1163 assert!(parse("Aug 09 2013 23:54:35 XXXXX", "%b %d %Y %H:%M:%S %Z").is_err());
1164
1165 //
1166 // %z
1167 //
1168 assert_eq!(parse("Aug 09 2013 23:54:35 -0900", "%b %d %Y %H:%M:%S %z"), Ok(dt));
1169 assert_eq!(parse("Aug 09 2013 23:54:35 -09 00", "%b %d %Y %H:%M:%S %z"), Ok(dt));
1170 assert_eq!(parse("Aug 09 2013 23:54:35 -09:00", "%b %d %Y %H:%M:%S %z"), Ok(dt));
1171 assert_eq!(parse("Aug 09 2013 23:54:35 -09 : 00", "%b %d %Y %H:%M:%S %z"), Ok(dt));
1172 assert_eq!(parse("Aug 09 2013 23:54:35 --0900", "%b %d %Y %H:%M:%S -%z"), Ok(dt));
1173 assert_eq!(parse("Aug 09 2013 23:54:35 +-0900", "%b %d %Y %H:%M:%S +%z"), Ok(dt));
1174 assert_eq!(parse("Aug 09 2013 23:54:35 -09:00 ", "%b %d %Y %H:%M:%S %z "), Ok(dt));
1175 // trailing newline after timezone
1176 assert!(parse("Aug 09 2013 23:54:35 -09:00\n", "%b %d %Y %H:%M:%S %z").is_err());
1177 assert_eq!(parse("Aug 09 2013 23:54:35 -09:00\n", "%b %d %Y %H:%M:%S %z "), Ok(dt));
1178 // trailing colon
1179 assert!(parse("Aug 09 2013 23:54:35 -09:00:", "%b %d %Y %H:%M:%S %z").is_err());
1180 // trailing colon with space
1181 assert!(parse("Aug 09 2013 23:54:35 -09:00: ", "%b %d %Y %H:%M:%S %z ").is_err());
1182 // trailing colon, mismatch space
1183 assert!(parse("Aug 09 2013 23:54:35 -09:00:", "%b %d %Y %H:%M:%S %z ").is_err());
1184 // wrong timezone data
1185 assert!(parse("Aug 09 2013 23:54:35 -09", "%b %d %Y %H:%M:%S %z").is_err());
1186 assert_eq!(parse("Aug 09 2013 23:54:35 -09::00", "%b %d %Y %H:%M:%S %z"), Ok(dt));
1187 assert_eq!(parse("Aug 09 2013 23:54:35 -0900::", "%b %d %Y %H:%M:%S %z::"), Ok(dt));
1188 assert_eq!(parse("Aug 09 2013 23:54:35 -09:00:00", "%b %d %Y %H:%M:%S %z:00"), Ok(dt));
1189 assert_eq!(parse("Aug 09 2013 23:54:35 -09:00:00 ", "%b %d %Y %H:%M:%S %z:00 "), Ok(dt));
1190
1191 //
1192 // %:z
1193 //
1194 assert_eq!(parse("Aug 09 2013 23:54:35 -09:00", "%b %d %Y %H:%M:%S %:z"), Ok(dt));
1195 assert_eq!(parse("Aug 09 2013 23:54:35 -0900", "%b %d %Y %H:%M:%S %:z"), Ok(dt));
1196 assert_eq!(parse("Aug 09 2013 23:54:35 -09 00", "%b %d %Y %H:%M:%S %:z"), Ok(dt));
1197 assert_eq!(parse("Aug 09 2013 23:54:35 -09 : 00", "%b %d %Y %H:%M:%S %:z"), Ok(dt));
1198 assert_eq!(parse("Aug 09 2013 23:54:35 -09 : 00:", "%b %d %Y %H:%M:%S %:z:"), Ok(dt));
1199 // wrong timezone data
1200 assert!(parse("Aug 09 2013 23:54:35 -09", "%b %d %Y %H:%M:%S %:z").is_err());
1201 assert_eq!(parse("Aug 09 2013 23:54:35 -09::00", "%b %d %Y %H:%M:%S %:z"), Ok(dt));
1202 // timezone data hs too many colons
1203 assert!(parse("Aug 09 2013 23:54:35 -09:00:", "%b %d %Y %H:%M:%S %:z").is_err());
1204 // timezone data hs too many colons
1205 assert!(parse("Aug 09 2013 23:54:35 -09:00::", "%b %d %Y %H:%M:%S %:z").is_err());
1206 assert_eq!(parse("Aug 09 2013 23:54:35 -09:00::", "%b %d %Y %H:%M:%S %:z::"), Ok(dt));
1207
1208 //
1209 // %::z
1210 //
1211 assert_eq!(parse("Aug 09 2013 23:54:35 -0900", "%b %d %Y %H:%M:%S %::z"), Ok(dt));
1212 assert_eq!(parse("Aug 09 2013 23:54:35 -09:00", "%b %d %Y %H:%M:%S %::z"), Ok(dt));
1213 assert_eq!(parse("Aug 09 2013 23:54:35 -09 : 00", "%b %d %Y %H:%M:%S %::z"), Ok(dt));
1214 // mismatching colon expectations
1215 assert!(parse("Aug 09 2013 23:54:35 -09:00:00", "%b %d %Y %H:%M:%S %::z").is_err());
1216 assert_eq!(parse("Aug 09 2013 23:54:35 -09::00", "%b %d %Y %H:%M:%S %::z"), Ok(dt));
1217 assert_eq!(parse("Aug 09 2013 23:54:35 -09::00", "%b %d %Y %H:%M:%S %:z"), Ok(dt));
1218 // wrong timezone data
1219 assert!(parse("Aug 09 2013 23:54:35 -09", "%b %d %Y %H:%M:%S %::z").is_err());
1220 assert_eq!(parse("Aug 09 2013 23:54:35 -09001234", "%b %d %Y %H:%M:%S %::z1234"), Ok(dt));
1221 assert_eq!(parse("Aug 09 2013 23:54:35 -09:001234", "%b %d %Y %H:%M:%S %::z1234"), Ok(dt));
1222 assert_eq!(parse("Aug 09 2013 23:54:35 -0900 ", "%b %d %Y %H:%M:%S %::z "), Ok(dt));
1223 assert_eq!(parse("Aug 09 2013 23:54:35 -0900\t\n", "%b %d %Y %H:%M:%S %::z\t\n"), Ok(dt));
1224 assert_eq!(parse("Aug 09 2013 23:54:35 -0900:", "%b %d %Y %H:%M:%S %::z:"), Ok(dt));
1225 assert_eq!(parse("Aug 09 2013 23:54:35 :-0900:0", "%b %d %Y %H:%M:%S :%::z:0"), Ok(dt));
1226 // mismatching colons and spaces
1227 assert!(parse("Aug 09 2013 23:54:35 :-0900: ", "%b %d %Y %H:%M:%S :%::z::").is_err());
1228 // mismatching colons expectations
1229 assert!(parse("Aug 09 2013 23:54:35 -09:00:00", "%b %d %Y %H:%M:%S %::z").is_err());
1230 assert_eq!(parse("Aug 09 2013 -0900: 23:54:35", "%b %d %Y %::z: %H:%M:%S"), Ok(dt));
1231 assert_eq!(parse("Aug 09 2013 :-0900:0 23:54:35", "%b %d %Y :%::z:0 %H:%M:%S"), Ok(dt));
1232 // mismatching colons expectations mid-string
1233 assert!(parse("Aug 09 2013 :-0900: 23:54:35", "%b %d %Y :%::z %H:%M:%S").is_err());
1234 // mismatching colons expectations, before end
1235 assert!(parse("Aug 09 2013 23:54:35 -09:00:00 ", "%b %d %Y %H:%M:%S %::z ").is_err());
1236
1237 //
1238 // %:::z
1239 //
1240 assert_eq!(parse("Aug 09 2013 23:54:35 -09:00", "%b %d %Y %H:%M:%S %:::z"), Ok(dt));
1241 assert_eq!(parse("Aug 09 2013 23:54:35 -0900", "%b %d %Y %H:%M:%S %:::z"), Ok(dt));
1242 assert_eq!(parse("Aug 09 2013 23:54:35 -0900 ", "%b %d %Y %H:%M:%S %:::z "), Ok(dt));
1243 // wrong timezone data
1244 assert!(parse("Aug 09 2013 23:54:35 -09", "%b %d %Y %H:%M:%S %:::z").is_err());
1245
1246 //
1247 // %::::z
1248 //
1249 // too many colons
1250 assert!(parse("Aug 09 2013 23:54:35 -0900", "%b %d %Y %H:%M:%S %::::z").is_err());
1251 // too many colons
1252 assert!(parse("Aug 09 2013 23:54:35 -09:00", "%b %d %Y %H:%M:%S %::::z").is_err());
1253 // too many colons
1254 assert!(parse("Aug 09 2013 23:54:35 -09:00:", "%b %d %Y %H:%M:%S %::::z").is_err());
1255 // too many colons
1256 assert!(parse("Aug 09 2013 23:54:35 -09:00:00", "%b %d %Y %H:%M:%S %::::z").is_err());
1257
1258 //
1259 // %#z
1260 //
1261 assert_eq!(parse("Aug 09 2013 23:54:35 -09:00", "%b %d %Y %H:%M:%S %#z"), Ok(dt));
1262 assert_eq!(parse("Aug 09 2013 23:54:35 -0900", "%b %d %Y %H:%M:%S %#z"), Ok(dt));
1263 assert_eq!(parse("Aug 09 2013 23:54:35 -09:00 ", "%b %d %Y %H:%M:%S %#z "), Ok(dt));
1264 assert_eq!(parse("Aug 09 2013 23:54:35 -0900 ", "%b %d %Y %H:%M:%S %#z "), Ok(dt));
1265 assert_eq!(parse("Aug 09 2013 23:54:35 -09", "%b %d %Y %H:%M:%S %#z"), Ok(dt));
1266 assert_eq!(parse("Aug 09 2013 23:54:35 -0900", "%b %d %Y %H:%M:%S %#z"), Ok(dt));
1267 assert_eq!(parse("Aug 09 2013 23:54:35 -09:", "%b %d %Y %H:%M:%S %#z"), Ok(dt));
1268 assert_eq!(parse("Aug 09 2013 23:54:35 -09: ", "%b %d %Y %H:%M:%S %#z "), Ok(dt));
1269 assert_eq!(parse("Aug 09 2013 23:54:35+-09", "%b %d %Y %H:%M:%S+%#z"), Ok(dt));
1270 assert_eq!(parse("Aug 09 2013 23:54:35--09", "%b %d %Y %H:%M:%S-%#z"), Ok(dt));
1271 assert_eq!(parse("Aug 09 2013 -09:00 23:54:35", "%b %d %Y %#z%H:%M:%S"), Ok(dt));
1272 assert_eq!(parse("Aug 09 2013 -0900 23:54:35", "%b %d %Y %#z%H:%M:%S"), Ok(dt));
1273 assert_eq!(parse("Aug 09 2013 -090023:54:35", "%b %d %Y %#z%H:%M:%S"), Ok(dt));
1274 assert_eq!(parse("Aug 09 2013 -09:0023:54:35", "%b %d %Y %#z%H:%M:%S"), Ok(dt));
1275 // timezone with partial minutes adjacent hours
1276 assert_ne!(parse("Aug 09 2013 -09023:54:35", "%b %d %Y %#z%H:%M:%S"), Ok(dt));
1277 // bad timezone data
1278 assert!(parse("Aug 09 2013 23:54:35 -09:00:00", "%b %d %Y %H:%M:%S %#z").is_err());
1279 // bad timezone data (partial minutes)
1280 assert!(parse("Aug 09 2013 23:54:35 -090", "%b %d %Y %H:%M:%S %#z").is_err());
1281 // bad timezone data (partial minutes) with trailing space
1282 assert!(parse("Aug 09 2013 23:54:35 -090 ", "%b %d %Y %H:%M:%S %#z ").is_err());
1283 // bad timezone data (partial minutes) mid-string
1284 assert!(parse("Aug 09 2013 -090 23:54:35", "%b %d %Y %#z %H:%M:%S").is_err());
1285 // bad timezone data
1286 assert!(parse("Aug 09 2013 -09:00:00 23:54:35", "%b %d %Y %#z %H:%M:%S").is_err());
1287 // timezone data ambiguous with hours
1288 assert!(parse("Aug 09 2013 -09:00:23:54:35", "%b %d %Y %#z%H:%M:%S").is_err());
1289 }
1290
1291 #[test]
1292 fn test_to_string_round_trip() {
1293 let dt = Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).unwrap();
1294 let _dt: DateTime<Utc> = dt.to_string().parse().unwrap();
1295
1296 let ndt_fixed = dt.with_timezone(&FixedOffset::east_opt(3600).unwrap());
1297 let _dt: DateTime<FixedOffset> = ndt_fixed.to_string().parse().unwrap();
1298
1299 let ndt_fixed = dt.with_timezone(&FixedOffset::east_opt(0).unwrap());
1300 let _dt: DateTime<FixedOffset> = ndt_fixed.to_string().parse().unwrap();
1301 }
1302
1303 #[test]
1304 #[cfg(feature = "clock")]
1305 fn test_to_string_round_trip_with_local() {
1306 let ndt = Local::now();
1307 let _dt: DateTime<FixedOffset> = ndt.to_string().parse().unwrap();
1308 }
1309
1310 #[test]
1311 #[cfg(feature = "clock")]
1312 fn test_datetime_format_with_local() {
1313 // if we are not around the year boundary, local and UTC date should have the same year
1314 let dt = Local::now().with_month(5).unwrap();
1315 assert_eq!(dt.format("%Y").to_string(), dt.with_timezone(&Utc).format("%Y").to_string());
1316 }
1317
1318 #[test]
1319 fn test_datetime_is_send_and_copy() {
1320 #[derive(Clone)]
1321 struct Tz {
1322 _not_send: *const i32,
1323 }
1324 impl TimeZone for Tz {
1325 type Offset = Off;
1326
1327 fn from_offset(_: &Self::Offset) -> Self {
1328 unimplemented!()
1329 }
1330 fn offset_from_local_date(&self, _: &NaiveDate) -> crate::MappedLocalTime<Self::Offset> {
1331 unimplemented!()
1332 }
1333 fn offset_from_local_datetime(
1334 &self,
1335 _: &NaiveDateTime,
1336 ) -> crate::MappedLocalTime<Self::Offset> {
1337 unimplemented!()
1338 }
1339 fn offset_from_utc_date(&self, _: &NaiveDate) -> Self::Offset {
1340 unimplemented!()
1341 }
1342 fn offset_from_utc_datetime(&self, _: &NaiveDateTime) -> Self::Offset {
1343 unimplemented!()
1344 }
1345 }
1346
1347 #[derive(Copy, Clone, Debug)]
1348 struct Off(());
1349 impl Offset for Off {
1350 fn fix(&self) -> FixedOffset {
1351 unimplemented!()
1352 }
1353 }
1354
1355 fn _assert_send_copy<T: Send + Copy>() {}
1356 // `DateTime` is `Send + Copy` if the offset is.
1357 _assert_send_copy::<DateTime<Tz>>();
1358 }
1359
1360 #[test]
1361 fn test_subsecond_part() {
1362 let datetime = Utc
1363 .from_local_datetime(
1364 &NaiveDate::from_ymd_opt(2014, 7, 8)
1365 .unwrap()
1366 .and_hms_nano_opt(9, 10, 11, 1234567)
1367 .unwrap(),
1368 )
1369 .unwrap();
1370
1371 assert_eq!(1, datetime.timestamp_subsec_millis());
1372 assert_eq!(1234, datetime.timestamp_subsec_micros());
1373 assert_eq!(1234567, datetime.timestamp_subsec_nanos());
1374 }
1375
1376 // Some targets, such as `wasm32-wasi`, have a problematic definition of `SystemTime`, such as an
1377 // `i32` (year 2035 problem), or an `u64` (no values before `UNIX-EPOCH`).
1378 // See https://github.com/rust-lang/rust/issues/44394.
1379 #[test]
1380 #[cfg(all(feature = "std", not(all(target_arch = "wasm32", target_os = "wasi"))))]
1381 fn test_from_system_time() {
1382 use std::time::{Duration, SystemTime, UNIX_EPOCH};
1383
1384 let nanos = 999_999_000;
1385
1386 let epoch = Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap();
1387
1388 // SystemTime -> DateTime<Utc>
1389 assert_eq!(DateTime::<Utc>::from(UNIX_EPOCH), epoch);
1390 assert_eq!(
1391 DateTime::<Utc>::from(UNIX_EPOCH + Duration::new(999_999_999, nanos)),
1392 Utc.from_local_datetime(
1393 &NaiveDate::from_ymd_opt(2001, 9, 9)
1394 .unwrap()
1395 .and_hms_nano_opt(1, 46, 39, nanos)
1396 .unwrap()
1397 )
1398 .unwrap()
1399 );
1400 assert_eq!(
1401 DateTime::<Utc>::from(UNIX_EPOCH - Duration::new(999_999_999, nanos)),
1402 Utc.from_local_datetime(
1403 &NaiveDate::from_ymd_opt(1938, 4, 24)
1404 .unwrap()
1405 .and_hms_nano_opt(22, 13, 20, 1_000)
1406 .unwrap()
1407 )
1408 .unwrap()
1409 );
1410
1411 // DateTime<Utc> -> SystemTime
1412 assert_eq!(SystemTime::from(epoch), UNIX_EPOCH);
1413 assert_eq!(
1414 SystemTime::from(
1415 Utc.from_local_datetime(
1416 &NaiveDate::from_ymd_opt(2001, 9, 9)
1417 .unwrap()
1418 .and_hms_nano_opt(1, 46, 39, nanos)
1419 .unwrap()
1420 )
1421 .unwrap()
1422 ),
1423 UNIX_EPOCH + Duration::new(999_999_999, nanos)
1424 );
1425 assert_eq!(
1426 SystemTime::from(
1427 Utc.from_local_datetime(
1428 &NaiveDate::from_ymd_opt(1938, 4, 24)
1429 .unwrap()
1430 .and_hms_nano_opt(22, 13, 20, 1_000)
1431 .unwrap()
1432 )
1433 .unwrap()
1434 ),
1435 UNIX_EPOCH - Duration::new(999_999_999, nanos)
1436 );
1437
1438 // DateTime<any tz> -> SystemTime (via `with_timezone`)
1439 #[cfg(feature = "clock")]
1440 {
1441 assert_eq!(SystemTime::from(epoch.with_timezone(&Local)), UNIX_EPOCH);
1442 }
1443 assert_eq!(
1444 SystemTime::from(epoch.with_timezone(&FixedOffset::east_opt(32400).unwrap())),
1445 UNIX_EPOCH
1446 );
1447 assert_eq!(
1448 SystemTime::from(epoch.with_timezone(&FixedOffset::west_opt(28800).unwrap())),
1449 UNIX_EPOCH
1450 );
1451 }
1452
1453 #[test]
1454 #[allow(deprecated)]
1455 fn test_datetime_from_local() {
1456 // 2000-01-12T02:00:00Z
1457 let naivedatetime_utc =
1458 NaiveDate::from_ymd_opt(2000, 1, 12).unwrap().and_hms_opt(2, 0, 0).unwrap();
1459 let datetime_utc = DateTime::<Utc>::from_utc(naivedatetime_utc, Utc);
1460
1461 // 2000-01-12T10:00:00+8:00:00
1462 let timezone_east = FixedOffset::east_opt(8 * 60 * 60).unwrap();
1463 let naivedatetime_east =
1464 NaiveDate::from_ymd_opt(2000, 1, 12).unwrap().and_hms_opt(10, 0, 0).unwrap();
1465 let datetime_east = DateTime::<FixedOffset>::from_local(naivedatetime_east, timezone_east);
1466
1467 // 2000-01-11T19:00:00-7:00:00
1468 let timezone_west = FixedOffset::west_opt(7 * 60 * 60).unwrap();
1469 let naivedatetime_west =
1470 NaiveDate::from_ymd_opt(2000, 1, 11).unwrap().and_hms_opt(19, 0, 0).unwrap();
1471 let datetime_west = DateTime::<FixedOffset>::from_local(naivedatetime_west, timezone_west);
1472
1473 assert_eq!(datetime_east, datetime_utc.with_timezone(&timezone_east));
1474 assert_eq!(datetime_west, datetime_utc.with_timezone(&timezone_west));
1475 }
1476
1477 #[test]
1478 #[cfg(feature = "clock")]
1479 fn test_datetime_before_windows_api_limits() {
1480 // dt corresponds to `FILETIME = 147221225472` from issue 651.
1481 // (https://github.com/chronotope/chrono/issues/651)
1482 // This used to fail on Windows for timezones with an offset of -5:00 or greater.
1483 // The API limits years to 1601..=30827.
1484 let dt = NaiveDate::from_ymd_opt(1601, 1, 1).unwrap().and_hms_milli_opt(4, 5, 22, 122).unwrap();
1485 let local_dt = Local.from_utc_datetime(&dt);
1486 dbg!(local_dt);
1487 }
1488
1489 #[test]
1490 #[cfg(feature = "clock")]
1491 fn test_years_elapsed() {
1492 // A bit more than 1 year
1493 let one_year_ago = Utc::now().date_naive() - Days::new(400);
1494 // A bit more than 2 years
1495 let two_year_ago = Utc::now().date_naive() - Days::new(750);
1496
1497 assert_eq!(Utc::now().date_naive().years_since(one_year_ago), Some(1));
1498 assert_eq!(Utc::now().date_naive().years_since(two_year_ago), Some(2));
1499
1500 // If the given DateTime is later than now, the function will always return 0.
1501 let future = Utc::now().date_naive() + Days(100);
1502 assert_eq!(Utc::now().date_naive().years_since(future), None);
1503 }
1504
1505 #[test]
1506 fn test_datetime_add_assign() {
1507 let naivedatetime = NaiveDate::from_ymd_opt(2000, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap();
1508 let datetime = naivedatetime.and_utc();
1509 let mut datetime_add = datetime;
1510
1511 datetime_add += TimeDelta::try_seconds(60).unwrap();
1512 assert_eq!(datetime_add, datetime + TimeDelta::try_seconds(60).unwrap());
1513
1514 let timezone = FixedOffset::east_opt(60 * 60).unwrap();
1515 let datetime = datetime.with_timezone(&timezone);
1516 let datetime_add = datetime_add.with_timezone(&timezone);
1517
1518 assert_eq!(datetime_add, datetime + TimeDelta::try_seconds(60).unwrap());
1519
1520 let timezone = FixedOffset::west_opt(2 * 60 * 60).unwrap();
1521 let datetime = datetime.with_timezone(&timezone);
1522 let datetime_add = datetime_add.with_timezone(&timezone);
1523
1524 assert_eq!(datetime_add, datetime + TimeDelta::try_seconds(60).unwrap());
1525 }
1526
1527 #[test]
1528 #[cfg(feature = "clock")]
1529 fn test_datetime_add_assign_local() {
1530 let naivedatetime = NaiveDate::from_ymd_opt(2022, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap();
1531
1532 let datetime = Local.from_utc_datetime(&naivedatetime);
1533 let mut datetime_add = Local.from_utc_datetime(&naivedatetime);
1534
1535 // ensure we cross a DST transition
1536 for i in 1..=365 {
1537 datetime_add += TimeDelta::try_days(1).unwrap();
1538 assert_eq!(datetime_add, datetime + TimeDelta::try_days(i).unwrap())
1539 }
1540 }
1541
1542 #[test]
1543 fn test_datetime_sub_assign() {
1544 let naivedatetime = NaiveDate::from_ymd_opt(2000, 1, 1).unwrap().and_hms_opt(12, 0, 0).unwrap();
1545 let datetime = naivedatetime.and_utc();
1546 let mut datetime_sub = datetime;
1547
1548 datetime_sub -= TimeDelta::try_minutes(90).unwrap();
1549 assert_eq!(datetime_sub, datetime - TimeDelta::try_minutes(90).unwrap());
1550
1551 let timezone = FixedOffset::east_opt(60 * 60).unwrap();
1552 let datetime = datetime.with_timezone(&timezone);
1553 let datetime_sub = datetime_sub.with_timezone(&timezone);
1554
1555 assert_eq!(datetime_sub, datetime - TimeDelta::try_minutes(90).unwrap());
1556
1557 let timezone = FixedOffset::west_opt(2 * 60 * 60).unwrap();
1558 let datetime = datetime.with_timezone(&timezone);
1559 let datetime_sub = datetime_sub.with_timezone(&timezone);
1560
1561 assert_eq!(datetime_sub, datetime - TimeDelta::try_minutes(90).unwrap());
1562 }
1563
1564 #[test]
1565 fn test_min_max_getters() {
1566 let offset_min = FixedOffset::west_opt(2 * 60 * 60).unwrap();
1567 let beyond_min = offset_min.from_utc_datetime(&NaiveDateTime::MIN);
1568 let offset_max = FixedOffset::east_opt(2 * 60 * 60).unwrap();
1569 let beyond_max = offset_max.from_utc_datetime(&NaiveDateTime::MAX);
1570
1571 assert_eq!(format!("{:?}", beyond_min), "-262144-12-31T22:00:00-02:00");
1572 // RFC 2822 doesn't support years with more than 4 digits.
1573 // assert_eq!(beyond_min.to_rfc2822(), "");
1574 #[cfg(feature = "alloc")]
1575 assert_eq!(beyond_min.to_rfc3339(), "-262144-12-31T22:00:00-02:00");
1576 #[cfg(feature = "alloc")]
1577 assert_eq!(
1578 beyond_min.format("%Y-%m-%dT%H:%M:%S%:z").to_string(),
1579 "-262144-12-31T22:00:00-02:00"
1580 );
1581 assert_eq!(beyond_min.year(), -262144);
1582 assert_eq!(beyond_min.month(), 12);
1583 assert_eq!(beyond_min.month0(), 11);
1584 assert_eq!(beyond_min.day(), 31);
1585 assert_eq!(beyond_min.day0(), 30);
1586 assert_eq!(beyond_min.ordinal(), 366);
1587 assert_eq!(beyond_min.ordinal0(), 365);
1588 assert_eq!(beyond_min.weekday(), Weekday::Wed);
1589 assert_eq!(beyond_min.iso_week().year(), -262143);
1590 assert_eq!(beyond_min.iso_week().week(), 1);
1591 assert_eq!(beyond_min.hour(), 22);
1592 assert_eq!(beyond_min.minute(), 0);
1593 assert_eq!(beyond_min.second(), 0);
1594 assert_eq!(beyond_min.nanosecond(), 0);
1595
1596 assert_eq!(format!("{:?}", beyond_max), "+262143-01-01T01:59:59.999999999+02:00");
1597 // RFC 2822 doesn't support years with more than 4 digits.
1598 // assert_eq!(beyond_max.to_rfc2822(), "");
1599 #[cfg(feature = "alloc")]
1600 assert_eq!(beyond_max.to_rfc3339(), "+262143-01-01T01:59:59.999999999+02:00");
1601 #[cfg(feature = "alloc")]
1602 assert_eq!(
1603 beyond_max.format("%Y-%m-%dT%H:%M:%S%.9f%:z").to_string(),
1604 "+262143-01-01T01:59:59.999999999+02:00"
1605 );
1606 assert_eq!(beyond_max.year(), 262143);
1607 assert_eq!(beyond_max.month(), 1);
1608 assert_eq!(beyond_max.month0(), 0);
1609 assert_eq!(beyond_max.day(), 1);
1610 assert_eq!(beyond_max.day0(), 0);
1611 assert_eq!(beyond_max.ordinal(), 1);
1612 assert_eq!(beyond_max.ordinal0(), 0);
1613 assert_eq!(beyond_max.weekday(), Weekday::Tue);
1614 assert_eq!(beyond_max.iso_week().year(), 262143);
1615 assert_eq!(beyond_max.iso_week().week(), 1);
1616 assert_eq!(beyond_max.hour(), 1);
1617 assert_eq!(beyond_max.minute(), 59);
1618 assert_eq!(beyond_max.second(), 59);
1619 assert_eq!(beyond_max.nanosecond(), 999_999_999);
1620 }
1621
1622 #[test]
1623 fn test_min_max_setters() {
1624 let offset_min = FixedOffset::west_opt(2 * 60 * 60).unwrap();
1625 let beyond_min = offset_min.from_utc_datetime(&NaiveDateTime::MIN);
1626 let offset_max = FixedOffset::east_opt(2 * 60 * 60).unwrap();
1627 let beyond_max = offset_max.from_utc_datetime(&NaiveDateTime::MAX);
1628
1629 assert_eq!(beyond_min.with_year(2020).unwrap().year(), 2020);
1630 assert_eq!(beyond_min.with_year(beyond_min.year()), Some(beyond_min));
1631 assert_eq!(beyond_min.with_month(beyond_min.month()), Some(beyond_min));
1632 assert_eq!(beyond_min.with_month(3), None);
1633 assert_eq!(beyond_min.with_month0(beyond_min.month0()), Some(beyond_min));
1634 assert_eq!(beyond_min.with_month0(3), None);
1635 assert_eq!(beyond_min.with_day(beyond_min.day()), Some(beyond_min));
1636 assert_eq!(beyond_min.with_day(15), None);
1637 assert_eq!(beyond_min.with_day0(beyond_min.day0()), Some(beyond_min));
1638 assert_eq!(beyond_min.with_day0(15), None);
1639 assert_eq!(beyond_min.with_ordinal(beyond_min.ordinal()), Some(beyond_min));
1640 assert_eq!(beyond_min.with_ordinal(200), None);
1641 assert_eq!(beyond_min.with_ordinal0(beyond_min.ordinal0()), Some(beyond_min));
1642 assert_eq!(beyond_min.with_ordinal0(200), None);
1643 assert_eq!(beyond_min.with_hour(beyond_min.hour()), Some(beyond_min));
1644 assert_eq!(
1645 beyond_min.with_hour(23),
1646 beyond_min.checked_add_signed(TimeDelta::try_hours(1).unwrap())
1647 );
1648 assert_eq!(beyond_min.with_hour(5), None);
1649 assert_eq!(beyond_min.with_minute(0), Some(beyond_min));
1650 assert_eq!(beyond_min.with_second(0), Some(beyond_min));
1651 assert_eq!(beyond_min.with_nanosecond(0), Some(beyond_min));
1652
1653 assert_eq!(beyond_max.with_year(2020).unwrap().year(), 2020);
1654 assert_eq!(beyond_max.with_year(beyond_max.year()), Some(beyond_max));
1655 assert_eq!(beyond_max.with_month(beyond_max.month()), Some(beyond_max));
1656 assert_eq!(beyond_max.with_month(3), None);
1657 assert_eq!(beyond_max.with_month0(beyond_max.month0()), Some(beyond_max));
1658 assert_eq!(beyond_max.with_month0(3), None);
1659 assert_eq!(beyond_max.with_day(beyond_max.day()), Some(beyond_max));
1660 assert_eq!(beyond_max.with_day(15), None);
1661 assert_eq!(beyond_max.with_day0(beyond_max.day0()), Some(beyond_max));
1662 assert_eq!(beyond_max.with_day0(15), None);
1663 assert_eq!(beyond_max.with_ordinal(beyond_max.ordinal()), Some(beyond_max));
1664 assert_eq!(beyond_max.with_ordinal(200), None);
1665 assert_eq!(beyond_max.with_ordinal0(beyond_max.ordinal0()), Some(beyond_max));
1666 assert_eq!(beyond_max.with_ordinal0(200), None);
1667 assert_eq!(beyond_max.with_hour(beyond_max.hour()), Some(beyond_max));
1668 assert_eq!(
1669 beyond_max.with_hour(0),
1670 beyond_max.checked_sub_signed(TimeDelta::try_hours(1).unwrap())
1671 );
1672 assert_eq!(beyond_max.with_hour(5), None);
1673 assert_eq!(beyond_max.with_minute(beyond_max.minute()), Some(beyond_max));
1674 assert_eq!(beyond_max.with_second(beyond_max.second()), Some(beyond_max));
1675 assert_eq!(beyond_max.with_nanosecond(beyond_max.nanosecond()), Some(beyond_max));
1676 }
1677
1678 #[test]
1679 fn test_min_max_add_days() {
1680 let offset_min = FixedOffset::west_opt(2 * 60 * 60).unwrap();
1681 let beyond_min = offset_min.from_utc_datetime(&NaiveDateTime::MIN);
1682 let offset_max = FixedOffset::east_opt(2 * 60 * 60).unwrap();
1683 let beyond_max = offset_max.from_utc_datetime(&NaiveDateTime::MAX);
1684 let max_time = NaiveTime::from_hms_nano_opt(23, 59, 59, 999_999_999).unwrap();
1685
1686 assert_eq!(beyond_min.checked_add_days(Days::new(0)), Some(beyond_min));
1687 assert_eq!(
1688 beyond_min.checked_add_days(Days::new(1)),
1689 Some(offset_min.from_utc_datetime(&(NaiveDate::MIN + Days(1)).and_time(NaiveTime::MIN)))
1690 );
1691 assert_eq!(beyond_min.checked_sub_days(Days::new(0)), Some(beyond_min));
1692 assert_eq!(beyond_min.checked_sub_days(Days::new(1)), None);
1693
1694 assert_eq!(beyond_max.checked_add_days(Days::new(0)), Some(beyond_max));
1695 assert_eq!(beyond_max.checked_add_days(Days::new(1)), None);
1696 assert_eq!(beyond_max.checked_sub_days(Days::new(0)), Some(beyond_max));
1697 assert_eq!(
1698 beyond_max.checked_sub_days(Days::new(1)),
1699 Some(offset_max.from_utc_datetime(&(NaiveDate::MAX - Days(1)).and_time(max_time)))
1700 );
1701 }
1702
1703 #[test]
1704 fn test_min_max_add_months() {
1705 let offset_min = FixedOffset::west_opt(2 * 60 * 60).unwrap();
1706 let beyond_min = offset_min.from_utc_datetime(&NaiveDateTime::MIN);
1707 let offset_max = FixedOffset::east_opt(2 * 60 * 60).unwrap();
1708 let beyond_max = offset_max.from_utc_datetime(&NaiveDateTime::MAX);
1709 let max_time = NaiveTime::from_hms_nano_opt(23, 59, 59, 999_999_999).unwrap();
1710
1711 assert_eq!(beyond_min.checked_add_months(Months::new(0)), Some(beyond_min));
1712 assert_eq!(
1713 beyond_min.checked_add_months(Months::new(1)),
1714 Some(offset_min.from_utc_datetime(&(NaiveDate::MIN + Months(1)).and_time(NaiveTime::MIN)))
1715 );
1716 assert_eq!(beyond_min.checked_sub_months(Months::new(0)), Some(beyond_min));
1717 assert_eq!(beyond_min.checked_sub_months(Months::new(1)), None);
1718
1719 assert_eq!(beyond_max.checked_add_months(Months::new(0)), Some(beyond_max));
1720 assert_eq!(beyond_max.checked_add_months(Months::new(1)), None);
1721 assert_eq!(beyond_max.checked_sub_months(Months::new(0)), Some(beyond_max));
1722 assert_eq!(
1723 beyond_max.checked_sub_months(Months::new(1)),
1724 Some(offset_max.from_utc_datetime(&(NaiveDate::MAX - Months(1)).and_time(max_time)))
1725 );
1726 }
1727
1728 #[test]
1729 #[should_panic]
1730 fn test_local_beyond_min_datetime() {
1731 let min = FixedOffset::west_opt(2 * 60 * 60).unwrap().from_utc_datetime(&NaiveDateTime::MIN);
1732 let _ = min.naive_local();
1733 }
1734
1735 #[test]
1736 #[should_panic]
1737 fn test_local_beyond_max_datetime() {
1738 let max = FixedOffset::east_opt(2 * 60 * 60).unwrap().from_utc_datetime(&NaiveDateTime::MAX);
1739 let _ = max.naive_local();
1740 }
1741
1742 #[test]
1743 #[cfg(feature = "clock")]
1744 fn test_datetime_sub_assign_local() {
1745 let naivedatetime = NaiveDate::from_ymd_opt(2022, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap();
1746
1747 let datetime = Local.from_utc_datetime(&naivedatetime);
1748 let mut datetime_sub = Local.from_utc_datetime(&naivedatetime);
1749
1750 // ensure we cross a DST transition
1751 for i in 1..=365 {
1752 datetime_sub -= TimeDelta::try_days(1).unwrap();
1753 assert_eq!(datetime_sub, datetime - TimeDelta::try_days(i).unwrap())
1754 }
1755 }
1756
1757 #[test]
1758 fn test_core_duration_ops() {
1759 use core::time::Duration;
1760
1761 let mut utc_dt = Utc.with_ymd_and_hms(2023, 8, 29, 11, 34, 12).unwrap();
1762 let same = utc_dt + Duration::ZERO;
1763 assert_eq!(utc_dt, same);
1764
1765 utc_dt += Duration::new(3600, 0);
1766 assert_eq!(utc_dt, Utc.with_ymd_and_hms(2023, 8, 29, 12, 34, 12).unwrap());
1767 }
1768
1769 #[test]
1770 #[should_panic]
1771 fn test_core_duration_max() {
1772 use core::time::Duration;
1773
1774 let mut utc_dt = Utc.with_ymd_and_hms(2023, 8, 29, 11, 34, 12).unwrap();
1775 utc_dt += Duration::MAX;
1776 }
1777
1778 #[test]
1779 #[cfg(feature = "clock")]
1780 fn test_datetime_local_from_preserves_offset() {
1781 let naivedatetime = NaiveDate::from_ymd_opt(2023, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap();
1782
1783 let datetime = Local.from_utc_datetime(&naivedatetime);
1784 let offset = datetime.offset().fix();
1785
1786 let datetime_fixed: DateTime<FixedOffset> = datetime.into();
1787 assert_eq!(&offset, datetime_fixed.offset());
1788 assert_eq!(datetime.fixed_offset(), datetime_fixed);
1789 }
1790
1791 #[test]
1792 fn test_datetime_fixed_offset() {
1793 let naivedatetime = NaiveDate::from_ymd_opt(2023, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap();
1794
1795 let datetime = Utc.from_utc_datetime(&naivedatetime);
1796 let fixed_utc = FixedOffset::east_opt(0).unwrap();
1797 assert_eq!(datetime.fixed_offset(), fixed_utc.from_local_datetime(&naivedatetime).unwrap());
1798
1799 let fixed_offset = FixedOffset::east_opt(3600).unwrap();
1800 let datetime_fixed = fixed_offset.from_local_datetime(&naivedatetime).unwrap();
1801 assert_eq!(datetime_fixed.fixed_offset(), datetime_fixed);
1802 }
1803
1804 #[test]
1805 fn test_datetime_to_utc() {
1806 let dt =
1807 FixedOffset::east_opt(3600).unwrap().with_ymd_and_hms(2020, 2, 22, 23, 24, 25).unwrap();
1808 let dt_utc: DateTime<Utc> = dt.to_utc();
1809 assert_eq!(dt, dt_utc);
1810 }
1811
1812 #[test]
1813 fn test_add_sub_months() {
1814 let utc_dt = Utc.with_ymd_and_hms(2018, 9, 5, 23, 58, 0).unwrap();
1815 assert_eq!(utc_dt + Months::new(15), Utc.with_ymd_and_hms(2019, 12, 5, 23, 58, 0).unwrap());
1816
1817 let utc_dt = Utc.with_ymd_and_hms(2020, 1, 31, 23, 58, 0).unwrap();
1818 assert_eq!(utc_dt + Months::new(1), Utc.with_ymd_and_hms(2020, 2, 29, 23, 58, 0).unwrap());
1819 assert_eq!(utc_dt + Months::new(2), Utc.with_ymd_and_hms(2020, 3, 31, 23, 58, 0).unwrap());
1820
1821 let utc_dt = Utc.with_ymd_and_hms(2018, 9, 5, 23, 58, 0).unwrap();
1822 assert_eq!(utc_dt - Months::new(15), Utc.with_ymd_and_hms(2017, 6, 5, 23, 58, 0).unwrap());
1823
1824 let utc_dt = Utc.with_ymd_and_hms(2020, 3, 31, 23, 58, 0).unwrap();
1825 assert_eq!(utc_dt - Months::new(1), Utc.with_ymd_and_hms(2020, 2, 29, 23, 58, 0).unwrap());
1826 assert_eq!(utc_dt - Months::new(2), Utc.with_ymd_and_hms(2020, 1, 31, 23, 58, 0).unwrap());
1827 }
1828
1829 #[test]
1830 fn test_auto_conversion() {
1831 let utc_dt = Utc.with_ymd_and_hms(2018, 9, 5, 23, 58, 0).unwrap();
1832 let cdt_dt = FixedOffset::west_opt(5 * 60 * 60)
1833 .unwrap()
1834 .with_ymd_and_hms(2018, 9, 5, 18, 58, 0)
1835 .unwrap();
1836 let utc_dt2: DateTime<Utc> = cdt_dt.into();
1837 assert_eq!(utc_dt, utc_dt2);
1838 }
1839
1840 #[test]
1841 #[cfg(feature = "clock")]
1842 #[allow(deprecated)]
1843 fn test_test_deprecated_from_offset() {
1844 let now = Local::now();
1845 let naive = now.naive_local();
1846 let utc = now.naive_utc();
1847 let offset: FixedOffset = *now.offset();
1848
1849 assert_eq!(DateTime::<Local>::from_local(naive, offset), now);
1850 assert_eq!(DateTime::<Local>::from_utc(utc, offset), now);
1851 }
1852
1853 #[test]
1854 #[cfg(all(feature = "unstable-locales", feature = "alloc"))]
1855 fn locale_decimal_point() {
1856 use crate::Locale::{ar_SY, nl_NL};
1857 let dt =
1858 Utc.with_ymd_and_hms(2018, 9, 5, 18, 58, 0).unwrap().with_nanosecond(123456780).unwrap();
1859
1860 assert_eq!(dt.format_localized("%T%.f", nl_NL).to_string(), "18:58:00,123456780");
1861 assert_eq!(dt.format_localized("%T%.3f", nl_NL).to_string(), "18:58:00,123");
1862 assert_eq!(dt.format_localized("%T%.6f", nl_NL).to_string(), "18:58:00,123456");
1863 assert_eq!(dt.format_localized("%T%.9f", nl_NL).to_string(), "18:58:00,123456780");
1864
1865 assert_eq!(dt.format_localized("%T%.f", ar_SY).to_string(), "18:58:00.123456780");
1866 assert_eq!(dt.format_localized("%T%.3f", ar_SY).to_string(), "18:58:00.123");
1867 assert_eq!(dt.format_localized("%T%.6f", ar_SY).to_string(), "18:58:00.123456");
1868 assert_eq!(dt.format_localized("%T%.9f", ar_SY).to_string(), "18:58:00.123456780");
1869 }
1870
1871 /// This is an extended test for <https://github.com/chronotope/chrono/issues/1289>.
1872 #[test]
1873 fn nano_roundrip() {
1874 const BILLION: i64 = 1_000_000_000;
1875
1876 for nanos in [
1877 i64::MIN,
1878 i64::MIN + 1,
1879 i64::MIN + 2,
1880 i64::MIN + BILLION - 1,
1881 i64::MIN + BILLION,
1882 i64::MIN + BILLION + 1,
1883 -BILLION - 1,
1884 -BILLION,
1885 -BILLION + 1,
1886 0,
1887 BILLION - 1,
1888 BILLION,
1889 BILLION + 1,
1890 i64::MAX - BILLION - 1,
1891 i64::MAX - BILLION,
1892 i64::MAX - BILLION + 1,
1893 i64::MAX - 2,
1894 i64::MAX - 1,
1895 i64::MAX,
1896 ] {
1897 println!("nanos: {}", nanos);
1898 let dt = Utc.timestamp_nanos(nanos);
1899 let nanos2 = dt.timestamp_nanos_opt().expect("value roundtrips");
1900 assert_eq!(nanos, nanos2);
1901 }
1902 }