Debian Codegraph @

.cargo/vendor/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(
738 &FixedOffset::east_opt(-3 * 60 * 60 - 30 * 60).unwrap(),
739 1969, 2, 13, 23, 32, 0,
740 )
741 )
742 );
743 // example from RFC 2822 Appendix A.5. without trailing " (Newfoundland Time)"
744 assert_eq!(
745 DateTime::parse_from_rfc2822(
746 "Thu,\n\t13\n Feb\n 1969\n 23:32\n -0330"
747 ),
748 Ok(
749 ymdhms(&FixedOffset::east_opt(-3 * 60 * 60 - 30 * 60).unwrap(), 1969, 2, 13, 23, 32, 0,)
750 )
751 );
752
753 // bad year
754 assert!(DateTime::parse_from_rfc2822("31 DEC 262143 23:59 -2359").is_err());
755 // wrong format
756 assert!(DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:16:09 +00:00").is_err());
757 // full name day of week
758 assert!(DateTime::parse_from_rfc2822("Wednesday, 18 Feb 2015 23:16:09 +0000").is_err());
759 // full name day of week
760 assert!(DateTime::parse_from_rfc2822("Wednesday 18 Feb 2015 23:16:09 +0000").is_err());
761 // wrong day of week separator '.'
762 assert!(DateTime::parse_from_rfc2822("Wed. 18 Feb 2015 23:16:09 +0000").is_err());
763 // *trailing* space causes failure
764 assert!(DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:16:09 +0000 ").is_err());
765 }
766
767 #[test]
768 #[cfg(feature = "alloc")]
769 fn test_datetime_rfc3339() {
770 let edt5 = FixedOffset::east_opt(5 * 60 * 60).unwrap();
771 let edt0 = FixedOffset::east_opt(0).unwrap();
772
773 // timezone 0
774 assert_eq!(
775 Utc.with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap().to_rfc3339(),
776 "2015-02-18T23:16:09+00:00"
777 );
778 // timezone +05
779 assert_eq!(
780 edt5.from_local_datetime(
781 &NaiveDate::from_ymd_opt(2015, 2, 18)
782 .unwrap()
783 .and_hms_milli_opt(23, 16, 9, 150)
784 .unwrap()
785 )
786 .unwrap()
787 .to_rfc3339(),
788 "2015-02-18T23:16:09.150+05:00"
789 );
790
791 assert_eq!(ymdhms_utc(2015, 2, 18, 23, 16, 9).to_rfc3339(), "2015-02-18T23:16:09+00:00");
792 assert_eq!(
793 ymdhms_milli(&edt5, 2015, 2, 18, 23, 16, 9, 150).to_rfc3339(),
794 "2015-02-18T23:16:09.150+05:00"
795 );
796 assert_eq!(
797 ymdhms_micro(&edt5, 2015, 2, 18, 23, 59, 59, 1_234_567).to_rfc3339(),
798 "2015-02-18T23:59:60.234567+05:00"
799 );
800 assert_eq!(
801 DateTime::parse_from_rfc3339("2015-02-18T23:59:59.123+05:00"),
802 Ok(ymdhms_micro(&edt5, 2015, 2, 18, 23, 59, 59, 123_000))
803 );
804 assert_eq!(
805 DateTime::parse_from_rfc3339("2015-02-18T23:59:59.123456+05:00"),
806 Ok(ymdhms_micro(&edt5, 2015, 2, 18, 23, 59, 59, 123_456))
807 );
808 assert_eq!(
809 DateTime::parse_from_rfc3339("2015-02-18T23:59:59.123456789+05:00"),
810 Ok(ymdhms_nano(&edt5, 2015, 2, 18, 23, 59, 59, 123_456_789))
811 );
812 assert_eq!(
813 DateTime::parse_from_rfc3339("2015-02-18T23:16:09Z"),
814 Ok(ymdhms(&edt0, 2015, 2, 18, 23, 16, 9))
815 );
816
817 assert_eq!(
818 ymdhms_micro(&edt5, 2015, 2, 18, 23, 59, 59, 1_234_567).to_rfc3339(),
819 "2015-02-18T23:59:60.234567+05:00"
820 );
821 assert_eq!(
822 ymdhms_milli(&edt5, 2015, 2, 18, 23, 16, 9, 150).to_rfc3339(),
823 "2015-02-18T23:16:09.150+05:00"
824 );
825 assert_eq!(
826 DateTime::parse_from_rfc3339("2015-02-18T00:00:00.234567+05:00"),
827 Ok(ymdhms_micro(&edt5, 2015, 2, 18, 0, 0, 0, 234_567))
828 );
829 assert_eq!(
830 DateTime::parse_from_rfc3339("2015-02-18T23:16:09Z"),
831 Ok(ymdhms(&edt0, 2015, 2, 18, 23, 16, 9))
832 );
833 assert_eq!(
834 DateTime::parse_from_rfc3339("2015-02-18 23:59:60.234567+05:00"),
835 Ok(ymdhms_micro(&edt5, 2015, 2, 18, 23, 59, 59, 1_234_567))
836 );
837 assert_eq!(ymdhms_utc(2015, 2, 18, 23, 16, 9).to_rfc3339(), "2015-02-18T23:16:09+00:00");
838
839 assert!(DateTime::parse_from_rfc3339("2015-02-18T23:59:60.234567 +05:00").is_err());
840 assert!(DateTime::parse_from_rfc3339("2015-02-18T23:059:60.234567+05:00").is_err());
841 assert!(DateTime::parse_from_rfc3339("2015-02-18T23:59:60.234567+05:00PST").is_err());
842 assert!(DateTime::parse_from_rfc3339("2015-02-18T23:59:60.234567+PST").is_err());
843 assert!(DateTime::parse_from_rfc3339("2015-02-18T23:59:60.234567PST").is_err());
844 assert!(DateTime::parse_from_rfc3339("2015-02-18T23:59:60.234567+0500").is_err());
845 assert!(DateTime::parse_from_rfc3339("2015-02-18T23:59:60.234567+05:00: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.234567+05:00 ").is_err());
848 assert!(DateTime::parse_from_rfc3339(" 2015-02-18T23:59:60.234567+05:00").is_err());
849 assert!(DateTime::parse_from_rfc3339("2015- 02-18T23:59:60.234567+05:00").is_err());
850 assert!(DateTime::parse_from_rfc3339("2015-02-18T23:59:60.234567A+05:00").is_err());
851 }
852
853 #[test]
854 #[cfg(feature = "alloc")]
855 fn test_rfc3339_opts() {
856 use crate::SecondsFormat::*;
857 let pst = FixedOffset::east_opt(8 * 60 * 60).unwrap();
858 let dt = pst
859 .from_local_datetime(
860 &NaiveDate::from_ymd_opt(2018, 1, 11)
861 .unwrap()
862 .and_hms_nano_opt(10, 5, 13, 84_660_000)
863 .unwrap(),
864 )
865 .unwrap();
866 assert_eq!(dt.to_rfc3339_opts(Secs, false), "2018-01-11T10:05:13+08:00");
867 assert_eq!(dt.to_rfc3339_opts(Secs, true), "2018-01-11T10:05:13+08:00");
868 assert_eq!(dt.to_rfc3339_opts(Millis, false), "2018-01-11T10:05:13.084+08:00");
869 assert_eq!(dt.to_rfc3339_opts(Micros, false), "2018-01-11T10:05:13.084660+08:00");
870 assert_eq!(dt.to_rfc3339_opts(Nanos, false), "2018-01-11T10:05:13.084660000+08:00");
871 assert_eq!(dt.to_rfc3339_opts(AutoSi, false), "2018-01-11T10:05:13.084660+08:00");
872
873 let ut = dt.naive_utc().and_utc();
874 assert_eq!(ut.to_rfc3339_opts(Secs, false), "2018-01-11T02:05:13+00:00");
875 assert_eq!(ut.to_rfc3339_opts(Secs, true), "2018-01-11T02:05:13Z");
876 assert_eq!(ut.to_rfc3339_opts(Millis, false), "2018-01-11T02:05:13.084+00:00");
877 assert_eq!(ut.to_rfc3339_opts(Millis, true), "2018-01-11T02:05:13.084Z");
878 assert_eq!(ut.to_rfc3339_opts(Micros, true), "2018-01-11T02:05:13.084660Z");
879 assert_eq!(ut.to_rfc3339_opts(Nanos, true), "2018-01-11T02:05:13.084660000Z");
880 assert_eq!(ut.to_rfc3339_opts(AutoSi, true), "2018-01-11T02:05:13.084660Z");
881 }
882
883 #[test]
884 #[should_panic]
885 #[cfg(feature = "alloc")]
886 fn test_rfc3339_opts_nonexhaustive() {
887 use crate::SecondsFormat;
888 let dt = Utc.with_ymd_and_hms(1999, 10, 9, 1, 2, 3).unwrap();
889 let _ = dt.to_rfc3339_opts(SecondsFormat::__NonExhaustive, true);
890 }
891
892 #[test]
893 fn test_datetime_from_str() {
894 assert_eq!(
895 "2015-02-18T23:16:9.15Z".parse::<DateTime<FixedOffset>>(),
896 Ok(FixedOffset::east_opt(0)
897 .unwrap()
898 .from_local_datetime(
899 &NaiveDate::from_ymd_opt(2015, 2, 18)
900 .unwrap()
901 .and_hms_milli_opt(23, 16, 9, 150)
902 .unwrap()
903 )
904 .unwrap())
905 );
906 assert_eq!(
907 "2015-02-18T23:16:9.15Z".parse::<DateTime<Utc>>(),
908 Ok(Utc
909 .from_local_datetime(
910 &NaiveDate::from_ymd_opt(2015, 2, 18)
911 .unwrap()
912 .and_hms_milli_opt(23, 16, 9, 150)
913 .unwrap()
914 )
915 .unwrap())
916 );
917 assert_eq!(
918 "2015-02-18T23:16:9.15 UTC".parse::<DateTime<Utc>>(),
919 Ok(Utc
920 .from_local_datetime(
921 &NaiveDate::from_ymd_opt(2015, 2, 18)
922 .unwrap()
923 .and_hms_milli_opt(23, 16, 9, 150)
924 .unwrap()
925 )
926 .unwrap())
927 );
928 assert_eq!(
929 "2015-02-18T23:16:9.15UTC".parse::<DateTime<Utc>>(),
930 Ok(Utc
931 .from_local_datetime(
932 &NaiveDate::from_ymd_opt(2015, 2, 18)
933 .unwrap()
934 .and_hms_milli_opt(23, 16, 9, 150)
935 .unwrap()
936 )
937 .unwrap())
938 );
939 assert_eq!(
940 "2015-02-18T23:16:9.15Utc".parse::<DateTime<Utc>>(),
941 Ok(Utc
942 .from_local_datetime(
943 &NaiveDate::from_ymd_opt(2015, 2, 18)
944 .unwrap()
945 .and_hms_milli_opt(23, 16, 9, 150)
946 .unwrap()
947 )
948 .unwrap())
949 );
950
951 assert_eq!(
952 "2015-2-18T23:16:9.15Z".parse::<DateTime<FixedOffset>>(),
953 Ok(FixedOffset::east_opt(0)
954 .unwrap()
955 .from_local_datetime(
956 &NaiveDate::from_ymd_opt(2015, 2, 18)
957 .unwrap()
958 .and_hms_milli_opt(23, 16, 9, 150)
959 .unwrap()
960 )
961 .unwrap())
962 );
963 assert_eq!(
964 "2015-2-18T13:16:9.15-10:00".parse::<DateTime<FixedOffset>>(),
965 Ok(FixedOffset::west_opt(10 * 3600)
966 .unwrap()
967 .from_local_datetime(
968 &NaiveDate::from_ymd_opt(2015, 2, 18)
969 .unwrap()
970 .and_hms_milli_opt(13, 16, 9, 150)
971 .unwrap()
972 )
973 .unwrap())
974 );
975 assert!("2015-2-18T23:16:9.15".parse::<DateTime<FixedOffset>>().is_err());
976
977 assert_eq!(
978 "2015-2-18T23:16:9.15Z".parse::<DateTime<Utc>>(),
979 Ok(Utc
980 .from_local_datetime(
981 &NaiveDate::from_ymd_opt(2015, 2, 18)
982 .unwrap()
983 .and_hms_milli_opt(23, 16, 9, 150)
984 .unwrap()
985 )
986 .unwrap())
987 );
988 assert_eq!(
989 "2015-2-18T13:16:9.15-10:00".parse::<DateTime<Utc>>(),
990 Ok(Utc
991 .from_local_datetime(
992 &NaiveDate::from_ymd_opt(2015, 2, 18)
993 .unwrap()
994 .and_hms_milli_opt(23, 16, 9, 150)
995 .unwrap()
996 )
997 .unwrap())
998 );
999 assert!("2015-2-18T23:16:9.15".parse::<DateTime<Utc>>().is_err());
1000 assert!("2015-02-18T23:16:9.15øøø".parse::<DateTime<Utc>>().is_err());
1001
1002 // no test for `DateTime<Local>`, we cannot verify that much.
1003 }
1004
1005 #[test]
1006 fn test_parse_datetime_utc() {
1007 // valid cases
1008 let valid = [
1009 "2001-02-03T04:05:06Z",
1010 "2001-02-03T04:05:06+0000",
1011 "2001-02-03T04:05:06-00:00",
1012 "2001-02-03T04:05:06-01:00",
1013 "2012-12-12 12:12:12Z",
1014 "2012-12-12t12:12:12Z",
1015 "2012-12-12T12:12:12Z",
1016 "2012 -12-12T12:12:12Z",
1017 "2012 -12-12T12:12:12Z",
1018 "2012- 12-12T12:12:12Z",
1019 "2012- 12-12T12:12:12Z",
1020 "2012-12-12T 12: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 "2012-12-12T12 : 12:12Z",
1026 "2012-12-12T12:12:12Z ",
1027 " 2012-12-12T12:12:12Z",
1028 "2015-02-18T23:16:09.153Z",
1029 "2015-2-18T23:16:09.153Z",
1030 "+2015-2-18T23:16:09.153Z",
1031 "-77-02-18T23:16:09Z",
1032 "+82701-05-6T15:9:60.898989898989Z",
1033 ];
1034 for &s in &valid {
1035 eprintln!("test_parse_datetime_utc valid {:?}", s);
1036 let d = match s.parse::<DateTime<Utc>>() {
1037 Ok(d) => d,
1038 Err(e) => panic!("parsing `{}` has failed: {}", s, e),
1039 };
1040 let s_ = format!("{:?}", d);
1041 // `s` and `s_` may differ, but `s.parse()` and `s_.parse()` must be same
1042 let d_ = match s_.parse::<DateTime<Utc>>() {
1043 Ok(d) => d,
1044 Err(e) => {
1045 panic!("`{}` is parsed into `{:?}`, but reparsing that has failed: {}", s, d, e)
1046 }
1047 };
1048 assert!(
1049 d == d_,
1050 "`{}` is parsed into `{:?}`, but reparsed result `{:?}` does not match",
1051 s,
1052 d,
1053 d_
1054 );
1055 }
1056
1057 // some invalid cases
1058 // since `ParseErrorKind` is private, all we can do is to check if there was an error
1059 let invalid = [
1060 "", // empty
1061 "Z", // missing data
1062 "15Z", // missing data
1063 "15:8:9Z", // missing date
1064 "15-8-9Z", // missing time or date
1065 "Fri, 09 Aug 2013 23:54:35 GMT", // valid datetime, wrong format
1066 "Sat Jun 30 23:59:60 2012", // valid datetime, wrong format
1067 "1441497364.649", // valid datetime, wrong format
1068 "+1441497364.649", // valid datetime, wrong format
1069 "+1441497364", // valid datetime, wrong format
1070 "+1441497364Z", // valid datetime, wrong format
1071 "2014/02/03 04:05:06Z", // valid datetime, wrong format
1072 "2001-02-03T04:05:0600:00", // valid datetime, timezone too close
1073 "2015-15-15T15:15:15Z", // invalid datetime
1074 "2012-12-12T12:12:12x", // invalid timezone
1075 "2012-123-12T12:12:12Z", // invalid month
1076 "2012-12-77T12:12:12Z", // invalid day
1077 "2012-12-12T26:12:12Z", // invalid hour
1078 "2012-12-12T12:61:12Z", // invalid minute
1079 "2012-12-12T12:12:62Z", // invalid second
1080 "2012-12-12 T12:12:12Z", // space after date
1081 "2012-12-12T12:12:12ZZ", // trailing literal 'Z'
1082 "+802701-12-12T12:12:12Z", // invalid year (out of bounds)
1083 "+ 2012-12-12T12:12:12Z", // invalid space before year
1084 " +82701 - 05 - 6 T 15 : 9 : 60.898989898989 Z", // valid datetime, wrong format
1085 ];
1086 for &s in &invalid {
1087 eprintln!("test_parse_datetime_utc invalid {:?}", s);
1088 assert!(s.parse::<DateTime<Utc>>().is_err());
1089 }
1090 }
1091
1092 #[test]
1093 fn test_parse_from_str() {
1094 let edt = FixedOffset::east_opt(570 * 60).unwrap();
1095 let edt0 = FixedOffset::east_opt(0).unwrap();
1096 let wdt = FixedOffset::west_opt(10 * 3600).unwrap();
1097 assert_eq!(
1098 DateTime::parse_from_str("2014-5-7T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"),
1099 Ok(ymdhms(&edt, 2014, 5, 7, 12, 34, 56))
1100 ); // ignore offset
1101 assert!(DateTime::parse_from_str("20140507000000", "%Y%m%d%H%M%S").is_err()); // no offset
1102 assert!(DateTime::parse_from_str("Fri, 09 Aug 2013 23:54:35 GMT", "%a, %d %b %Y %H:%M:%S GMT")
1103 .is_err());
1104 assert_eq!(
1105 DateTime::parse_from_str("0", "%s").unwrap(),
1106 DateTime::from_timestamp(0, 0).unwrap().fixed_offset()
1107 );
1108
1109 assert_eq!(
1110 "2015-02-18T23:16:9.15Z".parse::<DateTime<FixedOffset>>(),
1111 Ok(ymdhms_milli(&edt0, 2015, 2, 18, 23, 16, 9, 150))
1112 );
1113 assert_eq!(
1114 "2015-02-18T23:16:9.15Z".parse::<DateTime<Utc>>(),
1115 Ok(ymdhms_milli_utc(2015, 2, 18, 23, 16, 9, 150)),
1116 );
1117 assert_eq!(
1118 "2015-02-18T23:16:9.15 UTC".parse::<DateTime<Utc>>(),
1119 Ok(ymdhms_milli_utc(2015, 2, 18, 23, 16, 9, 150))
1120 );
1121 assert_eq!(
1122 "2015-02-18T23:16:9.15UTC".parse::<DateTime<Utc>>(),
1123 Ok(ymdhms_milli_utc(2015, 2, 18, 23, 16, 9, 150))
1124 );
1125
1126 assert_eq!(
1127 "2015-2-18T23:16:9.15Z".parse::<DateTime<FixedOffset>>(),
1128 Ok(ymdhms_milli(&edt0, 2015, 2, 18, 23, 16, 9, 150))
1129 );
1130 assert_eq!(
1131 "2015-2-18T13:16:9.15-10:00".parse::<DateTime<FixedOffset>>(),
1132 Ok(ymdhms_milli(&wdt, 2015, 2, 18, 13, 16, 9, 150))
1133 );
1134 assert!("2015-2-18T23:16:9.15".parse::<DateTime<FixedOffset>>().is_err());
1135
1136 assert_eq!(
1137 "2015-2-18T23:16:9.15Z".parse::<DateTime<Utc>>(),
1138 Ok(ymdhms_milli_utc(2015, 2, 18, 23, 16, 9, 150))
1139 );
1140 assert_eq!(
1141 "2015-2-18T13:16:9.15-10:00".parse::<DateTime<Utc>>(),
1142 Ok(ymdhms_milli_utc(2015, 2, 18, 23, 16, 9, 150))
1143 );
1144 assert!("2015-2-18T23:16:9.15".parse::<DateTime<Utc>>().is_err());
1145
1146 // no test for `DateTime<Local>`, we cannot verify that much.
1147 }
1148
1149 #[test]
1150 fn test_datetime_parse_from_str() {
1151 let dt = ymdhms(&FixedOffset::east_opt(-9 * 60 * 60).unwrap(), 2013, 8, 9, 23, 54, 35);
1152 let parse = DateTime::parse_from_str;
1153
1154 // timezone variations
1155
1156 //
1157 // %Z
1158 //
1159 // wrong timezone format
1160 assert!(parse("Aug 09 2013 23:54:35 -0900", "%b %d %Y %H:%M:%S %Z").is_err());
1161 // bad timezone data?
1162 assert!(parse("Aug 09 2013 23:54:35 PST", "%b %d %Y %H:%M:%S %Z").is_err());
1163 // bad timezone data
1164 assert!(parse("Aug 09 2013 23:54:35 XXXXX", "%b %d %Y %H:%M:%S %Z").is_err());
1165
1166 //
1167 // %z
1168 //
1169 assert_eq!(parse("Aug 09 2013 23:54:35 -0900", "%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 -09 : 00", "%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 +-0900", "%b %d %Y %H:%M:%S +%z"), Ok(dt));
1175 assert_eq!(parse("Aug 09 2013 23:54:35 -09:00 ", "%b %d %Y %H:%M:%S %z "), Ok(dt));
1176 // trailing newline after timezone
1177 assert!(parse("Aug 09 2013 23:54:35 -09:00\n", "%b %d %Y %H:%M:%S %z").is_err());
1178 assert_eq!(parse("Aug 09 2013 23:54:35 -09:00\n", "%b %d %Y %H:%M:%S %z "), Ok(dt));
1179 // trailing colon
1180 assert!(parse("Aug 09 2013 23:54:35 -09:00:", "%b %d %Y %H:%M:%S %z").is_err());
1181 // trailing colon with space
1182 assert!(parse("Aug 09 2013 23:54:35 -09:00: ", "%b %d %Y %H:%M:%S %z ").is_err());
1183 // trailing colon, mismatch space
1184 assert!(parse("Aug 09 2013 23:54:35 -09:00:", "%b %d %Y %H:%M:%S %z ").is_err());
1185 // wrong timezone data
1186 assert!(parse("Aug 09 2013 23:54:35 -09", "%b %d %Y %H:%M:%S %z").is_err());
1187 assert_eq!(parse("Aug 09 2013 23:54:35 -09::00", "%b %d %Y %H:%M:%S %z"), Ok(dt));
1188 assert_eq!(parse("Aug 09 2013 23:54:35 -0900::", "%b %d %Y %H:%M:%S %z::"), 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 assert_eq!(parse("Aug 09 2013 23:54:35 -09:00:00 ", "%b %d %Y %H:%M:%S %z:00 "), Ok(dt));
1191
1192 //
1193 // %:z
1194 //
1195 assert_eq!(parse("Aug 09 2013 23:54:35 -09:00", "%b %d %Y %H:%M:%S %:z"), Ok(dt));
1196 assert_eq!(parse("Aug 09 2013 23:54:35 -0900", "%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 assert_eq!(parse("Aug 09 2013 23:54:35 -09 : 00:", "%b %d %Y %H:%M:%S %:z:"), Ok(dt));
1200 // wrong timezone data
1201 assert!(parse("Aug 09 2013 23:54:35 -09", "%b %d %Y %H:%M:%S %:z").is_err());
1202 assert_eq!(parse("Aug 09 2013 23:54:35 -09::00", "%b %d %Y %H:%M:%S %:z"), Ok(dt));
1203 // timezone data hs too many colons
1204 assert!(parse("Aug 09 2013 23:54:35 -09:00:", "%b %d %Y %H:%M:%S %:z").is_err());
1205 // timezone data hs too many colons
1206 assert!(parse("Aug 09 2013 23:54:35 -09:00::", "%b %d %Y %H:%M:%S %:z").is_err());
1207 assert_eq!(parse("Aug 09 2013 23:54:35 -09:00::", "%b %d %Y %H:%M:%S %:z::"), Ok(dt));
1208
1209 //
1210 // %::z
1211 //
1212 assert_eq!(parse("Aug 09 2013 23:54:35 -0900", "%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 assert_eq!(parse("Aug 09 2013 23:54:35 -09 : 00", "%b %d %Y %H:%M:%S %::z"), Ok(dt));
1215 // mismatching colon expectations
1216 assert!(parse("Aug 09 2013 23:54:35 -09:00:00", "%b %d %Y %H:%M:%S %::z").is_err());
1217 assert_eq!(parse("Aug 09 2013 23:54:35 -09::00", "%b %d %Y %H:%M:%S %::z"), Ok(dt));
1218 assert_eq!(parse("Aug 09 2013 23:54:35 -09::00", "%b %d %Y %H:%M:%S %:z"), Ok(dt));
1219 // wrong timezone data
1220 assert!(parse("Aug 09 2013 23:54:35 -09", "%b %d %Y %H:%M:%S %::z").is_err());
1221 assert_eq!(parse("Aug 09 2013 23:54:35 -09001234", "%b %d %Y %H:%M:%S %::z1234"), Ok(dt));
1222 assert_eq!(parse("Aug 09 2013 23:54:35 -09:001234", "%b %d %Y %H:%M:%S %::z1234"), Ok(dt));
1223 assert_eq!(parse("Aug 09 2013 23:54:35 -0900 ", "%b %d %Y %H:%M:%S %::z "), Ok(dt));
1224 assert_eq!(parse("Aug 09 2013 23:54:35 -0900\t\n", "%b %d %Y %H:%M:%S %::z\t\n"), Ok(dt));
1225 assert_eq!(parse("Aug 09 2013 23:54:35 -0900:", "%b %d %Y %H:%M:%S %::z:"), Ok(dt));
1226 assert_eq!(parse("Aug 09 2013 23:54:35 :-0900:0", "%b %d %Y %H:%M:%S :%::z:0"), Ok(dt));
1227 // mismatching colons and spaces
1228 assert!(parse("Aug 09 2013 23:54:35 :-0900: ", "%b %d %Y %H:%M:%S :%::z::").is_err());
1229 // mismatching colons expectations
1230 assert!(parse("Aug 09 2013 23:54:35 -09:00:00", "%b %d %Y %H:%M:%S %::z").is_err());
1231 assert_eq!(parse("Aug 09 2013 -0900: 23:54:35", "%b %d %Y %::z: %H:%M:%S"), Ok(dt));
1232 assert_eq!(parse("Aug 09 2013 :-0900:0 23:54:35", "%b %d %Y :%::z:0 %H:%M:%S"), Ok(dt));
1233 // mismatching colons expectations mid-string
1234 assert!(parse("Aug 09 2013 :-0900: 23:54:35", "%b %d %Y :%::z %H:%M:%S").is_err());
1235 // mismatching colons expectations, before end
1236 assert!(parse("Aug 09 2013 23:54:35 -09:00:00 ", "%b %d %Y %H:%M:%S %::z ").is_err());
1237
1238 //
1239 // %:::z
1240 //
1241 assert_eq!(parse("Aug 09 2013 23:54:35 -09:00", "%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 assert_eq!(parse("Aug 09 2013 23:54:35 -0900 ", "%b %d %Y %H:%M:%S %:::z "), Ok(dt));
1244 // wrong timezone data
1245 assert!(parse("Aug 09 2013 23:54:35 -09", "%b %d %Y %H:%M:%S %:::z").is_err());
1246
1247 //
1248 // %::::z
1249 //
1250 // too many colons
1251 assert!(parse("Aug 09 2013 23:54:35 -0900", "%b %d %Y %H:%M:%S %::::z").is_err());
1252 // too many colons
1253 assert!(parse("Aug 09 2013 23:54:35 -09:00", "%b %d %Y %H:%M:%S %::::z").is_err());
1254 // too many colons
1255 assert!(parse("Aug 09 2013 23:54:35 -09:00:", "%b %d %Y %H:%M:%S %::::z").is_err());
1256 // too many colons
1257 assert!(parse("Aug 09 2013 23:54:35 -09:00:00", "%b %d %Y %H:%M:%S %::::z").is_err());
1258
1259 //
1260 // %#z
1261 //
1262 assert_eq!(parse("Aug 09 2013 23:54:35 -09:00", "%b %d %Y %H:%M:%S %#z"), Ok(dt));
1263 assert_eq!(parse("Aug 09 2013 23:54:35 -0900", "%b %d %Y %H:%M:%S %#z"), Ok(dt));
1264 assert_eq!(parse("Aug 09 2013 23:54:35 -09:00 ", "%b %d %Y %H:%M:%S %#z "), Ok(dt));
1265 assert_eq!(parse("Aug 09 2013 23:54:35 -0900 ", "%b %d %Y %H:%M:%S %#z "), Ok(dt));
1266 assert_eq!(parse("Aug 09 2013 23:54:35 -09", "%b %d %Y %H:%M:%S %#z"), Ok(dt));
1267 assert_eq!(parse("Aug 09 2013 23:54:35 -0900", "%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 23:54:35--09", "%b %d %Y %H:%M:%S-%#z"), Ok(dt));
1272 assert_eq!(parse("Aug 09 2013 -09:00 23:54:35", "%b %d %Y %#z%H:%M:%S"), Ok(dt));
1273 assert_eq!(parse("Aug 09 2013 -0900 23:54:35", "%b %d %Y %#z%H:%M:%S"), Ok(dt));
1274 assert_eq!(parse("Aug 09 2013 -090023:54:35", "%b %d %Y %#z%H:%M:%S"), Ok(dt));
1275 assert_eq!(parse("Aug 09 2013 -09:0023:54:35", "%b %d %Y %#z%H:%M:%S"), Ok(dt));
1276 // timezone with partial minutes adjacent hours
1277 assert_ne!(parse("Aug 09 2013 -09023:54:35", "%b %d %Y %#z%H:%M:%S"), Ok(dt));
1278 // bad timezone data
1279 assert!(parse("Aug 09 2013 23:54:35 -09:00:00", "%b %d %Y %H:%M:%S %#z").is_err());
1280 // bad timezone data (partial minutes)
1281 assert!(parse("Aug 09 2013 23:54:35 -090", "%b %d %Y %H:%M:%S %#z").is_err());
1282 // bad timezone data (partial minutes) with trailing space
1283 assert!(parse("Aug 09 2013 23:54:35 -090 ", "%b %d %Y %H:%M:%S %#z ").is_err());
1284 // bad timezone data (partial minutes) mid-string
1285 assert!(parse("Aug 09 2013 -090 23:54:35", "%b %d %Y %#z %H:%M:%S").is_err());
1286 // bad timezone data
1287 assert!(parse("Aug 09 2013 -09:00:00 23:54:35", "%b %d %Y %#z %H:%M:%S").is_err());
1288 // timezone data ambiguous with hours
1289 assert!(parse("Aug 09 2013 -09:00:23:54:35", "%b %d %Y %#z%H:%M:%S").is_err());
1290 }
1291
1292 #[test]
1293 fn test_to_string_round_trip() {
1294 let dt = Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).unwrap();
1295 let _dt: DateTime<Utc> = dt.to_string().parse().unwrap();
1296
1297 let ndt_fixed = dt.with_timezone(&FixedOffset::east_opt(3600).unwrap());
1298 let _dt: DateTime<FixedOffset> = ndt_fixed.to_string().parse().unwrap();
1299
1300 let ndt_fixed = dt.with_timezone(&FixedOffset::east_opt(0).unwrap());
1301 let _dt: DateTime<FixedOffset> = ndt_fixed.to_string().parse().unwrap();
1302 }
1303
1304 #[test]
1305 #[cfg(feature = "clock")]
1306 fn test_to_string_round_trip_with_local() {
1307 let ndt = Local::now();
1308 let _dt: DateTime<FixedOffset> = ndt.to_string().parse().unwrap();
1309 }
1310
1311 #[test]
1312 #[cfg(feature = "clock")]
1313 fn test_datetime_format_with_local() {
1314 // if we are not around the year boundary, local and UTC date should have the same year
1315 let dt = Local::now().with_month(5).unwrap();
1316 assert_eq!(dt.format("%Y").to_string(), dt.with_timezone(&Utc).format("%Y").to_string());
1317 }
1318
1319 #[test]
1320 fn test_datetime_is_send_and_copy() {
1321 #[derive(Clone)]
1322 struct Tz {
1323 _not_send: *const i32,
1324 }
1325 impl TimeZone for Tz {
1326 type Offset = Off;
1327
1328 fn from_offset(_: &Self::Offset) -> Self {
1329 unimplemented!()
1330 }
1331 fn offset_from_local_date(&self, _: &NaiveDate) -> crate::MappedLocalTime<Self::Offset> {
1332 unimplemented!()
1333 }
1334 fn offset_from_local_datetime(
1335 &self,
1336 _: &NaiveDateTime,
1337 ) -> crate::MappedLocalTime<Self::Offset> {
1338 unimplemented!()
1339 }
1340 fn offset_from_utc_date(&self, _: &NaiveDate) -> Self::Offset {
1341 unimplemented!()
1342 }
1343 fn offset_from_utc_datetime(&self, _: &NaiveDateTime) -> Self::Offset {
1344 unimplemented!()
1345 }
1346 }
1347
1348 #[derive(Copy, Clone, Debug)]
1349 struct Off(());
1350 impl Offset for Off {
1351 fn fix(&self) -> FixedOffset {
1352 unimplemented!()
1353 }
1354 }
1355
1356 fn _assert_send_copy<T: Send + Copy>() {}
1357 // `DateTime` is `Send + Copy` if the offset is.
1358 _assert_send_copy::<DateTime<Tz>>();
1359 }
1360
1361 #[test]
1362 fn test_subsecond_part() {
1363 let datetime = Utc
1364 .from_local_datetime(
1365 &NaiveDate::from_ymd_opt(2014, 7, 8)
1366 .unwrap()
1367 .and_hms_nano_opt(9, 10, 11, 1234567)
1368 .unwrap(),
1369 )
1370 .unwrap();
1371
1372 assert_eq!(1, datetime.timestamp_subsec_millis());
1373 assert_eq!(1234, datetime.timestamp_subsec_micros());
1374 assert_eq!(1234567, datetime.timestamp_subsec_nanos());
1375 }
1376
1377 // Some targets, such as `wasm32-wasi`, have a problematic definition of `SystemTime`, such as an
1378 // `i32` (year 2035 problem), or an `u64` (no values before `UNIX-EPOCH`).
1379 // See https://github.com/rust-lang/rust/issues/44394.
1380 #[test]
1381 #[cfg(all(feature = "std", not(all(target_arch = "wasm32", target_os = "wasi"))))]
1382 fn test_from_system_time() {
1383 use std::time::{Duration, SystemTime, UNIX_EPOCH};
1384
1385 let nanos = 999_999_000;
1386
1387 let epoch = Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap();
1388
1389 // SystemTime -> DateTime<Utc>
1390 assert_eq!(DateTime::<Utc>::from(UNIX_EPOCH), epoch);
1391 assert_eq!(
1392 DateTime::<Utc>::from(UNIX_EPOCH + Duration::new(999_999_999, nanos)),
1393 Utc.from_local_datetime(
1394 &NaiveDate::from_ymd_opt(2001, 9, 9)
1395 .unwrap()
1396 .and_hms_nano_opt(1, 46, 39, nanos)
1397 .unwrap()
1398 )
1399 .unwrap()
1400 );
1401 assert_eq!(
1402 DateTime::<Utc>::from(UNIX_EPOCH - Duration::new(999_999_999, nanos)),
1403 Utc.from_local_datetime(
1404 &NaiveDate::from_ymd_opt(1938, 4, 24)
1405 .unwrap()
1406 .and_hms_nano_opt(22, 13, 20, 1_000)
1407 .unwrap()
1408 )
1409 .unwrap()
1410 );
1411
1412 // DateTime<Utc> -> SystemTime
1413 assert_eq!(SystemTime::from(epoch), UNIX_EPOCH);
1414 assert_eq!(
1415 SystemTime::from(
1416 Utc.from_local_datetime(
1417 &NaiveDate::from_ymd_opt(2001, 9, 9)
1418 .unwrap()
1419 .and_hms_nano_opt(1, 46, 39, nanos)
1420 .unwrap()
1421 )
1422 .unwrap()
1423 ),
1424 UNIX_EPOCH + Duration::new(999_999_999, nanos)
1425 );
1426 assert_eq!(
1427 SystemTime::from(
1428 Utc.from_local_datetime(
1429 &NaiveDate::from_ymd_opt(1938, 4, 24)
1430 .unwrap()
1431 .and_hms_nano_opt(22, 13, 20, 1_000)
1432 .unwrap()
1433 )
1434 .unwrap()
1435 ),
1436 UNIX_EPOCH - Duration::new(999_999_999, nanos)
1437 );
1438
1439 // DateTime<any tz> -> SystemTime (via `with_timezone`)
1440 #[cfg(feature = "clock")]
1441 {
1442 assert_eq!(SystemTime::from(epoch.with_timezone(&Local)), UNIX_EPOCH);
1443 }
1444 assert_eq!(
1445 SystemTime::from(epoch.with_timezone(&FixedOffset::east_opt(32400).unwrap())),
1446 UNIX_EPOCH
1447 );
1448 assert_eq!(
1449 SystemTime::from(epoch.with_timezone(&FixedOffset::west_opt(28800).unwrap())),
1450 UNIX_EPOCH
1451 );
1452 }
1453
1454 #[test]
1455 #[allow(deprecated)]
1456 fn test_datetime_from_local() {
1457 // 2000-01-12T02:00:00Z
1458 let naivedatetime_utc =
1459 NaiveDate::from_ymd_opt(2000, 1, 12).unwrap().and_hms_opt(2, 0, 0).unwrap();
1460 let datetime_utc = DateTime::<Utc>::from_utc(naivedatetime_utc, Utc);
1461
1462 // 2000-01-12T10:00:00+8:00:00
1463 let timezone_east = FixedOffset::east_opt(8 * 60 * 60).unwrap();
1464 let naivedatetime_east =
1465 NaiveDate::from_ymd_opt(2000, 1, 12).unwrap().and_hms_opt(10, 0, 0).unwrap();
1466 let datetime_east = DateTime::<FixedOffset>::from_local(naivedatetime_east, timezone_east);
1467
1468 // 2000-01-11T19:00:00-7:00:00
1469 let timezone_west = FixedOffset::west_opt(7 * 60 * 60).unwrap();
1470 let naivedatetime_west =
1471 NaiveDate::from_ymd_opt(2000, 1, 11).unwrap().and_hms_opt(19, 0, 0).unwrap();
1472 let datetime_west = DateTime::<FixedOffset>::from_local(naivedatetime_west, timezone_west);
1473
1474 assert_eq!(datetime_east, datetime_utc.with_timezone(&timezone_east));
1475 assert_eq!(datetime_west, datetime_utc.with_timezone(&timezone_west));
1476 }
1477
1478 #[test]
1479 #[cfg(feature = "clock")]
1480 fn test_datetime_before_windows_api_limits() {
1481 // dt corresponds to `FILETIME = 147221225472` from issue 651.
1482 // (https://github.com/chronotope/chrono/issues/651)
1483 // This used to fail on Windows for timezones with an offset of -5:00 or greater.
1484 // The API limits years to 1601..=30827.
1485 let dt = NaiveDate::from_ymd_opt(1601, 1, 1).unwrap().and_hms_milli_opt(4, 5, 22, 122).unwrap();
1486 let local_dt = Local.from_utc_datetime(&dt);
1487 dbg!(local_dt);
1488 }
1489
1490 #[test]
1491 #[cfg(feature = "clock")]
1492 fn test_years_elapsed() {
1493 // A bit more than 1 year
1494 let one_year_ago = Utc::now().date_naive() - Days::new(400);
1495 // A bit more than 2 years
1496 let two_year_ago = Utc::now().date_naive() - Days::new(750);
1497
1498 assert_eq!(Utc::now().date_naive().years_since(one_year_ago), Some(1));
1499 assert_eq!(Utc::now().date_naive().years_since(two_year_ago), Some(2));
1500
1501 // If the given DateTime is later than now, the function will always return 0.
1502 let future = Utc::now().date_naive() + Days(100);
1503 assert_eq!(Utc::now().date_naive().years_since(future), None);
1504 }
1505
1506 #[test]
1507 fn test_datetime_add_assign() {
1508 let naivedatetime = NaiveDate::from_ymd_opt(2000, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap();
1509 let datetime = naivedatetime.and_utc();
1510 let mut datetime_add = datetime;
1511
1512 datetime_add += TimeDelta::try_seconds(60).unwrap();
1513 assert_eq!(datetime_add, datetime + TimeDelta::try_seconds(60).unwrap());
1514
1515 let timezone = FixedOffset::east_opt(60 * 60).unwrap();
1516 let datetime = datetime.with_timezone(&timezone);
1517 let datetime_add = datetime_add.with_timezone(&timezone);
1518
1519 assert_eq!(datetime_add, datetime + TimeDelta::try_seconds(60).unwrap());
1520
1521 let timezone = FixedOffset::west_opt(2 * 60 * 60).unwrap();
1522 let datetime = datetime.with_timezone(&timezone);
1523 let datetime_add = datetime_add.with_timezone(&timezone);
1524
1525 assert_eq!(datetime_add, datetime + TimeDelta::try_seconds(60).unwrap());
1526 }
1527
1528 #[test]
1529 #[cfg(feature = "clock")]
1530 fn test_datetime_add_assign_local() {
1531 let naivedatetime = NaiveDate::from_ymd_opt(2022, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap();
1532
1533 let datetime = Local.from_utc_datetime(&naivedatetime);
1534 let mut datetime_add = Local.from_utc_datetime(&naivedatetime);
1535
1536 // ensure we cross a DST transition
1537 for i in 1..=365 {
1538 datetime_add += TimeDelta::try_days(1).unwrap();
1539 assert_eq!(datetime_add, datetime + TimeDelta::try_days(i).unwrap())
1540 }
1541 }
1542
1543 #[test]
1544 fn test_datetime_sub_assign() {
1545 let naivedatetime = NaiveDate::from_ymd_opt(2000, 1, 1).unwrap().and_hms_opt(12, 0, 0).unwrap();
1546 let datetime = naivedatetime.and_utc();
1547 let mut datetime_sub = datetime;
1548
1549 datetime_sub -= TimeDelta::try_minutes(90).unwrap();
1550 assert_eq!(datetime_sub, datetime - TimeDelta::try_minutes(90).unwrap());
1551
1552 let timezone = FixedOffset::east_opt(60 * 60).unwrap();
1553 let datetime = datetime.with_timezone(&timezone);
1554 let datetime_sub = datetime_sub.with_timezone(&timezone);
1555
1556 assert_eq!(datetime_sub, datetime - TimeDelta::try_minutes(90).unwrap());
1557
1558 let timezone = FixedOffset::west_opt(2 * 60 * 60).unwrap();
1559 let datetime = datetime.with_timezone(&timezone);
1560 let datetime_sub = datetime_sub.with_timezone(&timezone);
1561
1562 assert_eq!(datetime_sub, datetime - TimeDelta::try_minutes(90).unwrap());
1563 }
1564
1565 #[test]
1566 fn test_min_max_getters() {
1567 let offset_min = FixedOffset::west_opt(2 * 60 * 60).unwrap();
1568 let beyond_min = offset_min.from_utc_datetime(&NaiveDateTime::MIN);
1569 let offset_max = FixedOffset::east_opt(2 * 60 * 60).unwrap();
1570 let beyond_max = offset_max.from_utc_datetime(&NaiveDateTime::MAX);
1571
1572 assert_eq!(format!("{:?}", beyond_min), "-262144-12-31T22:00:00-02:00");
1573 // RFC 2822 doesn't support years with more than 4 digits.
1574 // assert_eq!(beyond_min.to_rfc2822(), "");
1575 #[cfg(feature = "alloc")]
1576 assert_eq!(beyond_min.to_rfc3339(), "-262144-12-31T22:00:00-02:00");
1577 #[cfg(feature = "alloc")]
1578 assert_eq!(
1579 beyond_min.format("%Y-%m-%dT%H:%M:%S%:z").to_string(),
1580 "-262144-12-31T22:00:00-02:00"
1581 );
1582 assert_eq!(beyond_min.year(), -262144);
1583 assert_eq!(beyond_min.month(), 12);
1584 assert_eq!(beyond_min.month0(), 11);
1585 assert_eq!(beyond_min.day(), 31);
1586 assert_eq!(beyond_min.day0(), 30);
1587 assert_eq!(beyond_min.ordinal(), 366);
1588 assert_eq!(beyond_min.ordinal0(), 365);
1589 assert_eq!(beyond_min.weekday(), Weekday::Wed);
1590 assert_eq!(beyond_min.iso_week().year(), -262143);
1591 assert_eq!(beyond_min.iso_week().week(), 1);
1592 assert_eq!(beyond_min.hour(), 22);
1593 assert_eq!(beyond_min.minute(), 0);
1594 assert_eq!(beyond_min.second(), 0);
1595 assert_eq!(beyond_min.nanosecond(), 0);
1596
1597 assert_eq!(format!("{:?}", beyond_max), "+262143-01-01T01:59:59.999999999+02:00");
1598 // RFC 2822 doesn't support years with more than 4 digits.
1599 // assert_eq!(beyond_max.to_rfc2822(), "");
1600 #[cfg(feature = "alloc")]
1601 assert_eq!(beyond_max.to_rfc3339(), "+262143-01-01T01:59:59.999999999+02:00");
1602 #[cfg(feature = "alloc")]
1603 assert_eq!(
1604 beyond_max.format("%Y-%m-%dT%H:%M:%S%.9f%:z").to_string(),
1605 "+262143-01-01T01:59:59.999999999+02:00"
1606 );
1607 assert_eq!(beyond_max.year(), 262143);
1608 assert_eq!(beyond_max.month(), 1);
1609 assert_eq!(beyond_max.month0(), 0);
1610 assert_eq!(beyond_max.day(), 1);
1611 assert_eq!(beyond_max.day0(), 0);
1612 assert_eq!(beyond_max.ordinal(), 1);
1613 assert_eq!(beyond_max.ordinal0(), 0);
1614 assert_eq!(beyond_max.weekday(), Weekday::Tue);
1615 assert_eq!(beyond_max.iso_week().year(), 262143);
1616 assert_eq!(beyond_max.iso_week().week(), 1);
1617 assert_eq!(beyond_max.hour(), 1);
1618 assert_eq!(beyond_max.minute(), 59);
1619 assert_eq!(beyond_max.second(), 59);
1620 assert_eq!(beyond_max.nanosecond(), 999_999_999);
1621 }
1622
1623 #[test]
1624 fn test_min_max_setters() {
1625 let offset_min = FixedOffset::west_opt(2 * 60 * 60).unwrap();
1626 let beyond_min = offset_min.from_utc_datetime(&NaiveDateTime::MIN);
1627 let offset_max = FixedOffset::east_opt(2 * 60 * 60).unwrap();
1628 let beyond_max = offset_max.from_utc_datetime(&NaiveDateTime::MAX);
1629
1630 assert_eq!(beyond_min.with_year(2020).unwrap().year(), 2020);
1631 assert_eq!(beyond_min.with_year(beyond_min.year()), Some(beyond_min));
1632 assert_eq!(beyond_min.with_month(beyond_min.month()), Some(beyond_min));
1633 assert_eq!(beyond_min.with_month(3), None);
1634 assert_eq!(beyond_min.with_month0(beyond_min.month0()), Some(beyond_min));
1635 assert_eq!(beyond_min.with_month0(3), None);
1636 assert_eq!(beyond_min.with_day(beyond_min.day()), Some(beyond_min));
1637 assert_eq!(beyond_min.with_day(15), None);
1638 assert_eq!(beyond_min.with_day0(beyond_min.day0()), Some(beyond_min));
1639 assert_eq!(beyond_min.with_day0(15), None);
1640 assert_eq!(beyond_min.with_ordinal(beyond_min.ordinal()), Some(beyond_min));
1641 assert_eq!(beyond_min.with_ordinal(200), None);
1642 assert_eq!(beyond_min.with_ordinal0(beyond_min.ordinal0()), Some(beyond_min));
1643 assert_eq!(beyond_min.with_ordinal0(200), None);
1644 assert_eq!(beyond_min.with_hour(beyond_min.hour()), Some(beyond_min));
1645 assert_eq!(
1646 beyond_min.with_hour(23),
1647 beyond_min.checked_add_signed(TimeDelta::try_hours(1).unwrap())
1648 );
1649 assert_eq!(beyond_min.with_hour(5), None);
1650 assert_eq!(beyond_min.with_minute(0), Some(beyond_min));
1651 assert_eq!(beyond_min.with_second(0), Some(beyond_min));
1652 assert_eq!(beyond_min.with_nanosecond(0), Some(beyond_min));
1653
1654 assert_eq!(beyond_max.with_year(2020).unwrap().year(), 2020);
1655 assert_eq!(beyond_max.with_year(beyond_max.year()), Some(beyond_max));
1656 assert_eq!(beyond_max.with_month(beyond_max.month()), Some(beyond_max));
1657 assert_eq!(beyond_max.with_month(3), None);
1658 assert_eq!(beyond_max.with_month0(beyond_max.month0()), Some(beyond_max));
1659 assert_eq!(beyond_max.with_month0(3), None);
1660 assert_eq!(beyond_max.with_day(beyond_max.day()), Some(beyond_max));
1661 assert_eq!(beyond_max.with_day(15), None);
1662 assert_eq!(beyond_max.with_day0(beyond_max.day0()), Some(beyond_max));
1663 assert_eq!(beyond_max.with_day0(15), None);
1664 assert_eq!(beyond_max.with_ordinal(beyond_max.ordinal()), Some(beyond_max));
1665 assert_eq!(beyond_max.with_ordinal(200), None);
1666 assert_eq!(beyond_max.with_ordinal0(beyond_max.ordinal0()), Some(beyond_max));
1667 assert_eq!(beyond_max.with_ordinal0(200), None);
1668 assert_eq!(beyond_max.with_hour(beyond_max.hour()), Some(beyond_max));
1669 assert_eq!(
1670 beyond_max.with_hour(0),
1671 beyond_max.checked_sub_signed(TimeDelta::try_hours(1).unwrap())
1672 );
1673 assert_eq!(beyond_max.with_hour(5), None);
1674 assert_eq!(beyond_max.with_minute(beyond_max.minute()), Some(beyond_max));
1675 assert_eq!(beyond_max.with_second(beyond_max.second()), Some(beyond_max));
1676 assert_eq!(beyond_max.with_nanosecond(beyond_max.nanosecond()), Some(beyond_max));
1677 }
1678
1679 #[test]
1680 fn test_min_max_add_days() {
1681 let offset_min = FixedOffset::west_opt(2 * 60 * 60).unwrap();
1682 let beyond_min = offset_min.from_utc_datetime(&NaiveDateTime::MIN);
1683 let offset_max = FixedOffset::east_opt(2 * 60 * 60).unwrap();
1684 let beyond_max = offset_max.from_utc_datetime(&NaiveDateTime::MAX);
1685 let max_time = NaiveTime::from_hms_nano_opt(23, 59, 59, 999_999_999).unwrap();
1686
1687 assert_eq!(beyond_min.checked_add_days(Days::new(0)), Some(beyond_min));
1688 assert_eq!(
1689 beyond_min.checked_add_days(Days::new(1)),
1690 Some(offset_min.from_utc_datetime(&(NaiveDate::MIN + Days(1)).and_time(NaiveTime::MIN)))
1691 );
1692 assert_eq!(beyond_min.checked_sub_days(Days::new(0)), Some(beyond_min));
1693 assert_eq!(beyond_min.checked_sub_days(Days::new(1)), None);
1694
1695 assert_eq!(beyond_max.checked_add_days(Days::new(0)), Some(beyond_max));
1696 assert_eq!(beyond_max.checked_add_days(Days::new(1)), None);
1697 assert_eq!(beyond_max.checked_sub_days(Days::new(0)), Some(beyond_max));
1698 assert_eq!(
1699 beyond_max.checked_sub_days(Days::new(1)),
1700 Some(offset_max.from_utc_datetime(&(NaiveDate::MAX - Days(1)).and_time(max_time)))
1701 );
1702 }
1703
1704 #[test]
1705 fn test_min_max_add_months() {
1706 let offset_min = FixedOffset::west_opt(2 * 60 * 60).unwrap();
1707 let beyond_min = offset_min.from_utc_datetime(&NaiveDateTime::MIN);
1708 let offset_max = FixedOffset::east_opt(2 * 60 * 60).unwrap();
1709 let beyond_max = offset_max.from_utc_datetime(&NaiveDateTime::MAX);
1710 let max_time = NaiveTime::from_hms_nano_opt(23, 59, 59, 999_999_999).unwrap();
1711
1712 assert_eq!(beyond_min.checked_add_months(Months::new(0)), Some(beyond_min));
1713 assert_eq!(
1714 beyond_min.checked_add_months(Months::new(1)),
1715 Some(offset_min.from_utc_datetime(&(NaiveDate::MIN + Months(1)).and_time(NaiveTime::MIN)))
1716 );
1717 assert_eq!(beyond_min.checked_sub_months(Months::new(0)), Some(beyond_min));
1718 assert_eq!(beyond_min.checked_sub_months(Months::new(1)), None);
1719
1720 assert_eq!(beyond_max.checked_add_months(Months::new(0)), Some(beyond_max));
1721 assert_eq!(beyond_max.checked_add_months(Months::new(1)), None);
1722 assert_eq!(beyond_max.checked_sub_months(Months::new(0)), Some(beyond_max));
1723 assert_eq!(
1724 beyond_max.checked_sub_months(Months::new(1)),
1725 Some(offset_max.from_utc_datetime(&(NaiveDate::MAX - Months(1)).and_time(max_time)))
1726 );
1727 }
1728
1729 #[test]
1730 #[should_panic]
1731 fn test_local_beyond_min_datetime() {
1732 let min = FixedOffset::west_opt(2 * 60 * 60).unwrap().from_utc_datetime(&NaiveDateTime::MIN);
1733 let _ = min.naive_local();
1734 }
1735
1736 #[test]
1737 #[should_panic]
1738 fn test_local_beyond_max_datetime() {
1739 let max = FixedOffset::east_opt(2 * 60 * 60).unwrap().from_utc_datetime(&NaiveDateTime::MAX);
1740 let _ = max.naive_local();
1741 }
1742
1743 #[test]
1744 #[cfg(feature = "clock")]
1745 fn test_datetime_sub_assign_local() {
1746 let naivedatetime = NaiveDate::from_ymd_opt(2022, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap();
1747
1748 let datetime = Local.from_utc_datetime(&naivedatetime);
1749 let mut datetime_sub = Local.from_utc_datetime(&naivedatetime);
1750
1751 // ensure we cross a DST transition
1752 for i in 1..=365 {
1753 datetime_sub -= TimeDelta::try_days(1).unwrap();
1754 assert_eq!(datetime_sub, datetime - TimeDelta::try_days(i).unwrap())
1755 }
1756 }
1757
1758 #[test]
1759 fn test_core_duration_ops() {
1760 use core::time::Duration;
1761
1762 let mut utc_dt = Utc.with_ymd_and_hms(2023, 8, 29, 11, 34, 12).unwrap();
1763 let same = utc_dt + Duration::ZERO;
1764 assert_eq!(utc_dt, same);
1765
1766 utc_dt += Duration::new(3600, 0);
1767 assert_eq!(utc_dt, Utc.with_ymd_and_hms(2023, 8, 29, 12, 34, 12).unwrap());
1768 }
1769
1770 #[test]
1771 #[should_panic]
1772 fn test_core_duration_max() {
1773 use core::time::Duration;
1774
1775 let mut utc_dt = Utc.with_ymd_and_hms(2023, 8, 29, 11, 34, 12).unwrap();
1776 utc_dt += Duration::MAX;
1777 }
1778
1779 #[test]
1780 #[cfg(feature = "clock")]
1781 fn test_datetime_local_from_preserves_offset() {
1782 let naivedatetime = NaiveDate::from_ymd_opt(2023, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap();
1783
1784 let datetime = Local.from_utc_datetime(&naivedatetime);
1785 let offset = datetime.offset().fix();
1786
1787 let datetime_fixed: DateTime<FixedOffset> = datetime.into();
1788 assert_eq!(&offset, datetime_fixed.offset());
1789 assert_eq!(datetime.fixed_offset(), datetime_fixed);
1790 }
1791
1792 #[test]
1793 fn test_datetime_fixed_offset() {
1794 let naivedatetime = NaiveDate::from_ymd_opt(2023, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap();
1795
1796 let datetime = Utc.from_utc_datetime(&naivedatetime);
1797 let fixed_utc = FixedOffset::east_opt(0).unwrap();
1798 assert_eq!(datetime.fixed_offset(), fixed_utc.from_local_datetime(&naivedatetime).unwrap());
1799
1800 let fixed_offset = FixedOffset::east_opt(3600).unwrap();
1801 let datetime_fixed = fixed_offset.from_local_datetime(&naivedatetime).unwrap();
1802 assert_eq!(datetime_fixed.fixed_offset(), datetime_fixed);
1803 }
1804
1805 #[test]
1806 fn test_datetime_to_utc() {
1807 let dt =
1808 FixedOffset::east_opt(3600).unwrap().with_ymd_and_hms(2020, 2, 22, 23, 24, 25).unwrap();
1809 let dt_utc: DateTime<Utc> = dt.to_utc();
1810 assert_eq!(dt, dt_utc);
1811 }
1812
1813 #[test]
1814 fn test_add_sub_months() {
1815 let utc_dt = Utc.with_ymd_and_hms(2018, 9, 5, 23, 58, 0).unwrap();
1816 assert_eq!(utc_dt + Months::new(15), Utc.with_ymd_and_hms(2019, 12, 5, 23, 58, 0).unwrap());
1817
1818 let utc_dt = Utc.with_ymd_and_hms(2020, 1, 31, 23, 58, 0).unwrap();
1819 assert_eq!(utc_dt + Months::new(1), Utc.with_ymd_and_hms(2020, 2, 29, 23, 58, 0).unwrap());
1820 assert_eq!(utc_dt + Months::new(2), Utc.with_ymd_and_hms(2020, 3, 31, 23, 58, 0).unwrap());
1821
1822 let utc_dt = Utc.with_ymd_and_hms(2018, 9, 5, 23, 58, 0).unwrap();
1823 assert_eq!(utc_dt - Months::new(15), Utc.with_ymd_and_hms(2017, 6, 5, 23, 58, 0).unwrap());
1824
1825 let utc_dt = Utc.with_ymd_and_hms(2020, 3, 31, 23, 58, 0).unwrap();
1826 assert_eq!(utc_dt - Months::new(1), Utc.with_ymd_and_hms(2020, 2, 29, 23, 58, 0).unwrap());
1827 assert_eq!(utc_dt - Months::new(2), Utc.with_ymd_and_hms(2020, 1, 31, 23, 58, 0).unwrap());
1828 }
1829
1830 #[test]
1831 fn test_auto_conversion() {
1832 let utc_dt = Utc.with_ymd_and_hms(2018, 9, 5, 23, 58, 0).unwrap();
1833 let cdt_dt = FixedOffset::west_opt(5 * 60 * 60)
1834 .unwrap()
1835 .with_ymd_and_hms(2018, 9, 5, 18, 58, 0)
1836 .unwrap();
1837 let utc_dt2: DateTime<Utc> = cdt_dt.into();
1838 assert_eq!(utc_dt, utc_dt2);
1839 }
1840
1841 #[test]
1842 #[cfg(feature = "clock")]
1843 #[allow(deprecated)]
1844 fn test_test_deprecated_from_offset() {
1845 let now = Local::now();
1846 let naive = now.naive_local();
1847 let utc = now.naive_utc();
1848 let offset: FixedOffset = *now.offset();
1849
1850 assert_eq!(DateTime::<Local>::from_local(naive, offset), now);
1851 assert_eq!(DateTime::<Local>::from_utc(utc, offset), now);
1852 }
1853
1854 #[test]
1855 #[cfg(all(feature = "unstable-locales", feature = "alloc"))]
1856 fn locale_decimal_point() {
1857 use crate::Locale::{ar_SY, nl_NL};
1858 let dt =
1859 Utc.with_ymd_and_hms(2018, 9, 5, 18, 58, 0).unwrap().with_nanosecond(123456780).unwrap();
1860
1861 assert_eq!(dt.format_localized("%T%.f", nl_NL).to_string(), "18:58:00,123456780");
1862 assert_eq!(dt.format_localized("%T%.3f", nl_NL).to_string(), "18:58:00,123");
1863 assert_eq!(dt.format_localized("%T%.6f", nl_NL).to_string(), "18:58:00,123456");
1864 assert_eq!(dt.format_localized("%T%.9f", nl_NL).to_string(), "18:58:00,123456780");
1865
1866 assert_eq!(dt.format_localized("%T%.f", ar_SY).to_string(), "18:58:00.123456780");
1867 assert_eq!(dt.format_localized("%T%.3f", ar_SY).to_string(), "18:58:00.123");
1868 assert_eq!(dt.format_localized("%T%.6f", ar_SY).to_string(), "18:58:00.123456");
1869 assert_eq!(dt.format_localized("%T%.9f", ar_SY).to_string(), "18:58:00.123456780");
1870 }
1871
1872 /// This is an extended test for <https://github.com/chronotope/chrono/issues/1289>.
1873 #[test]
1874 fn nano_roundrip() {
1875 const BILLION: i64 = 1_000_000_000;
1876
1877 for nanos in [
1878 i64::MIN,
1879 i64::MIN + 1,
1880 i64::MIN + 2,
1881 i64::MIN + BILLION - 1,
1882 i64::MIN + BILLION,
1883 i64::MIN + BILLION + 1,
1884 -BILLION - 1,
1885 -BILLION,
1886 -BILLION + 1,
1887 0,
1888 BILLION - 1,
1889 BILLION,
1890 BILLION + 1,
1891 i64::MAX - BILLION - 1,
1892 i64::MAX - BILLION,
1893 i64::MAX - BILLION + 1,
1894 i64::MAX - 2,
1895 i64::MAX - 1,
1896 i64::MAX,
1897 ] {
1898 println!("nanos: {}", nanos);
1899 let dt = Utc.timestamp_nanos(nanos);
1900 let nanos2 = dt.timestamp_nanos_opt().expect("value roundtrips");
1901 assert_eq!(nanos, nanos2);
1902 }
1903 }