1 // This is a part of Chrono.
2 // See README.md and LICENSE.txt for details.
3 
4 //! ISO 8601 date and time with time zone.
5 
6 #[cfg(all(not(feature = "std"), feature = "alloc"))]
7 use alloc::string::String;
8 use core::borrow::Borrow;
9 use core::cmp::Ordering;
10 use core::fmt::Write;
11 use core::ops::{Add, AddAssign, Sub, SubAssign};
12 use core::time::Duration;
13 use core::{fmt, hash, str};
14 #[cfg(feature = "std")]
15 use std::time::{SystemTime, UNIX_EPOCH};
16 
17 #[cfg(all(feature = "unstable-locales", feature = "alloc"))]
18 use crate::format::Locale;
19 use crate::format::{
20     parse, parse_and_remainder, parse_rfc3339, Fixed, Item, ParseError, ParseResult, Parsed,
21     StrftimeItems, TOO_LONG,
22 };
23 #[cfg(feature = "alloc")]
24 use crate::format::{write_rfc2822, write_rfc3339, DelayedFormat, SecondsFormat};
25 use crate::naive::{Days, IsoWeek, NaiveDate, NaiveDateTime, NaiveTime};
26 #[cfg(feature = "clock")]
27 use crate::offset::Local;
28 use crate::offset::{FixedOffset, Offset, TimeZone, Utc};
29 use crate::try_opt;
30 #[allow(deprecated)]
31 use crate::Date;
32 use crate::{Datelike, Months, TimeDelta, Timelike, Weekday};
33 
34 #[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
35 use rkyv::{Archive, Deserialize, Serialize};
36 
37 #[cfg(feature = "rustc-serialize")]
38 pub(super) mod rustc_serialize;
39 
40 /// documented at re-export site
41 #[cfg(feature = "serde")]
42 pub(super) mod serde;
43 
44 #[cfg(test)]
45 mod tests;
46 
47 /// ISO 8601 combined date and time with time zone.
48 ///
49 /// There are some constructors implemented here (the `from_*` methods), but
50 /// the general-purpose constructors are all via the methods on the
51 /// [`TimeZone`](./offset/trait.TimeZone.html) implementations.
52 #[derive(Clone)]
53 #[cfg_attr(
54     any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"),
55     derive(Archive, Deserialize, Serialize),
56     archive(compare(PartialEq, PartialOrd))
57 )]
58 #[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
59 pub struct DateTime<Tz: TimeZone> {
60     datetime: NaiveDateTime,
61     offset: Tz::Offset,
62 }
63 
64 /// The minimum possible `DateTime<Utc>`.
65 #[deprecated(since = "0.4.20", note = "Use DateTime::MIN_UTC instead")]
66 pub const MIN_DATETIME: DateTime<Utc> = DateTime::<Utc>::MIN_UTC;
67 /// The maximum possible `DateTime<Utc>`.
68 #[deprecated(since = "0.4.20", note = "Use DateTime::MAX_UTC instead")]
69 pub const MAX_DATETIME: DateTime<Utc> = DateTime::<Utc>::MAX_UTC;
70 
71 impl<Tz: TimeZone> DateTime<Tz> {
72     /// Makes a new `DateTime` from its components: a `NaiveDateTime` in UTC and an `Offset`.
73     ///
74     /// This is a low-level method, intended for use cases such as deserializing a `DateTime` or
75     /// passing it through FFI.
76     ///
77     /// For regular use you will probably want to use a method such as
78     /// [`TimeZone::from_local_datetime`] or [`NaiveDateTime::and_local_timezone`] instead.
79     ///
80     /// # Example
81     ///
82     #[cfg_attr(not(feature = "clock"), doc = "```ignore")]
83     #[cfg_attr(feature = "clock", doc = "```rust")]
84     /// use chrono::{Local, DateTime};
85     ///
86     /// let dt = Local::now();
87     /// // Get components
88     /// let naive_utc = dt.naive_utc();
89     /// let offset = dt.offset().clone();
90     /// // Serialize, pass through FFI... and recreate the `DateTime`:
91     /// let dt_new = DateTime::<Local>::from_naive_utc_and_offset(naive_utc, offset);
92     /// assert_eq!(dt, dt_new);
93     /// ```
94     #[inline]
95     #[must_use]
from_naive_utc_and_offset( datetime: NaiveDateTime, offset: Tz::Offset, ) -> DateTime<Tz>96     pub const fn from_naive_utc_and_offset(
97         datetime: NaiveDateTime,
98         offset: Tz::Offset,
99     ) -> DateTime<Tz> {
100         DateTime { datetime, offset }
101     }
102 
103     /// Makes a new `DateTime` from its components: a `NaiveDateTime` in UTC and an `Offset`.
104     #[inline]
105     #[must_use]
106     #[deprecated(
107         since = "0.4.27",
108         note = "Use TimeZone::from_utc_datetime() or DateTime::from_naive_utc_and_offset instead"
109     )]
from_utc(datetime: NaiveDateTime, offset: Tz::Offset) -> DateTime<Tz>110     pub fn from_utc(datetime: NaiveDateTime, offset: Tz::Offset) -> DateTime<Tz> {
111         DateTime { datetime, offset }
112     }
113 
114     /// Makes a new `DateTime` from a `NaiveDateTime` in *local* time and an `Offset`.
115     ///
116     /// # Panics
117     ///
118     /// Panics if the local datetime can't be converted to UTC because it would be out of range.
119     ///
120     /// This can happen if `datetime` is near the end of the representable range of `NaiveDateTime`,
121     /// and the offset from UTC pushes it beyond that.
122     #[inline]
123     #[must_use]
124     #[deprecated(
125         since = "0.4.27",
126         note = "Use TimeZone::from_local_datetime() or NaiveDateTime::and_local_timezone instead"
127     )]
from_local(datetime: NaiveDateTime, offset: Tz::Offset) -> DateTime<Tz>128     pub fn from_local(datetime: NaiveDateTime, offset: Tz::Offset) -> DateTime<Tz> {
129         let datetime_utc = datetime - offset.fix();
130 
131         DateTime { datetime: datetime_utc, offset }
132     }
133 
134     /// Retrieves the date component with an associated timezone.
135     ///
136     /// Unless you are immediately planning on turning this into a `DateTime`
137     /// with the same timezone you should use the [`date_naive`](DateTime::date_naive) method.
138     ///
139     /// [`NaiveDate`] is a more well-defined type, and has more traits implemented on it,
140     /// so should be preferred to [`Date`] any time you truly want to operate on dates.
141     ///
142     /// # Panics
143     ///
144     /// [`DateTime`] internally stores the date and time in UTC with a [`NaiveDateTime`]. This
145     /// method will panic if the offset from UTC would push the local date outside of the
146     /// representable range of a [`Date`].
147     #[inline]
148     #[deprecated(since = "0.4.23", note = "Use `date_naive()` instead")]
149     #[allow(deprecated)]
150     #[must_use]
date(&self) -> Date<Tz>151     pub fn date(&self) -> Date<Tz> {
152         Date::from_utc(self.naive_local().date(), self.offset.clone())
153     }
154 
155     /// Retrieves the date component.
156     ///
157     /// # Panics
158     ///
159     /// [`DateTime`] internally stores the date and time in UTC with a [`NaiveDateTime`]. This
160     /// method will panic if the offset from UTC would push the local date outside of the
161     /// representable range of a [`NaiveDate`].
162     ///
163     /// # Example
164     ///
165     /// ```
166     /// use chrono::prelude::*;
167     ///
168     /// let date: DateTime<Utc> = Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap();
169     /// let other: DateTime<FixedOffset> = FixedOffset::east_opt(23).unwrap().with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap();
170     /// assert_eq!(date.date_naive(), other.date_naive());
171     /// ```
172     #[inline]
173     #[must_use]
date_naive(&self) -> NaiveDate174     pub fn date_naive(&self) -> NaiveDate {
175         let local = self.naive_local();
176         NaiveDate::from_ymd_opt(local.year(), local.month(), local.day()).unwrap()
177     }
178 
179     /// Retrieves the time component.
180     #[inline]
181     #[must_use]
time(&self) -> NaiveTime182     pub fn time(&self) -> NaiveTime {
183         self.datetime.time() + self.offset.fix()
184     }
185 
186     /// Returns the number of non-leap seconds since January 1, 1970 0:00:00 UTC
187     /// (aka "UNIX timestamp").
188     ///
189     /// The reverse operation of creating a [`DateTime`] from a timestamp can be performed
190     /// using [`from_timestamp`](DateTime::from_timestamp) or [`TimeZone::timestamp_opt`].
191     ///
192     /// ```
193     /// use chrono::{DateTime, TimeZone, Utc};
194     ///
195     /// let dt: DateTime<Utc> = Utc.with_ymd_and_hms(2015, 5, 15, 0, 0, 0).unwrap();
196     /// assert_eq!(dt.timestamp(), 1431648000);
197     ///
198     /// assert_eq!(DateTime::from_timestamp(dt.timestamp(), dt.timestamp_subsec_nanos()).unwrap(), dt);
199     /// ```
200     #[inline]
201     #[must_use]
timestamp(&self) -> i64202     pub const fn timestamp(&self) -> i64 {
203         self.datetime.timestamp()
204     }
205 
206     /// Returns the number of non-leap-milliseconds since January 1, 1970 UTC.
207     ///
208     /// # Example
209     ///
210     /// ```
211     /// use chrono::{Utc, NaiveDate};
212     ///
213     /// let dt = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().and_hms_milli_opt(0, 0, 1, 444).unwrap().and_local_timezone(Utc).unwrap();
214     /// assert_eq!(dt.timestamp_millis(), 1_444);
215     ///
216     /// let dt = NaiveDate::from_ymd_opt(2001, 9, 9).unwrap().and_hms_milli_opt(1, 46, 40, 555).unwrap().and_local_timezone(Utc).unwrap();
217     /// assert_eq!(dt.timestamp_millis(), 1_000_000_000_555);
218     /// ```
219     #[inline]
220     #[must_use]
timestamp_millis(&self) -> i64221     pub const fn timestamp_millis(&self) -> i64 {
222         self.datetime.timestamp_millis()
223     }
224 
225     /// Returns the number of non-leap-microseconds since January 1, 1970 UTC.
226     ///
227     /// # Example
228     ///
229     /// ```
230     /// use chrono::{Utc, NaiveDate};
231     ///
232     /// let dt = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().and_hms_micro_opt(0, 0, 1, 444).unwrap().and_local_timezone(Utc).unwrap();
233     /// assert_eq!(dt.timestamp_micros(), 1_000_444);
234     ///
235     /// let dt = NaiveDate::from_ymd_opt(2001, 9, 9).unwrap().and_hms_micro_opt(1, 46, 40, 555).unwrap().and_local_timezone(Utc).unwrap();
236     /// assert_eq!(dt.timestamp_micros(), 1_000_000_000_000_555);
237     /// ```
238     #[inline]
239     #[must_use]
timestamp_micros(&self) -> i64240     pub const fn timestamp_micros(&self) -> i64 {
241         self.datetime.timestamp_micros()
242     }
243 
244     /// Returns the number of non-leap-nanoseconds since January 1, 1970 UTC.
245     ///
246     /// # Panics
247     ///
248     /// An `i64` with nanosecond precision can span a range of ~584 years. This function panics on
249     /// an out of range `DateTime`.
250     ///
251     /// The dates that can be represented as nanoseconds are between 1677-09-21T00:12:43.145224192
252     /// and 2262-04-11T23:47:16.854775807.
253     #[deprecated(since = "0.4.31", note = "use `timestamp_nanos_opt()` instead")]
254     #[inline]
255     #[must_use]
256     #[allow(deprecated)]
timestamp_nanos(&self) -> i64257     pub const fn timestamp_nanos(&self) -> i64 {
258         self.datetime.timestamp_nanos()
259     }
260 
261     /// Returns the number of non-leap-nanoseconds since January 1, 1970 UTC.
262     ///
263     /// # Errors
264     ///
265     /// An `i64` with nanosecond precision can span a range of ~584 years. This function returns
266     /// `None` on an out of range `DateTime`.
267     ///
268     /// The dates that can be represented as nanoseconds are between 1677-09-21T00:12:43.145224192
269     /// and 2262-04-11T23:47:16.854775807.
270     ///
271     /// # Example
272     ///
273     /// ```
274     /// use chrono::{Utc, NaiveDate};
275     ///
276     /// let dt = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().and_hms_nano_opt(0, 0, 1, 444).unwrap().and_local_timezone(Utc).unwrap();
277     /// assert_eq!(dt.timestamp_nanos_opt(), Some(1_000_000_444));
278     ///
279     /// let dt = NaiveDate::from_ymd_opt(2001, 9, 9).unwrap().and_hms_nano_opt(1, 46, 40, 555).unwrap().and_local_timezone(Utc).unwrap();
280     /// assert_eq!(dt.timestamp_nanos_opt(), Some(1_000_000_000_000_000_555));
281     ///
282     /// let dt = NaiveDate::from_ymd_opt(1677, 9, 21).unwrap().and_hms_nano_opt(0, 12, 43, 145_224_192).unwrap().and_local_timezone(Utc).unwrap();
283     /// assert_eq!(dt.timestamp_nanos_opt(), Some(-9_223_372_036_854_775_808));
284     ///
285     /// let dt = NaiveDate::from_ymd_opt(2262, 4, 11).unwrap().and_hms_nano_opt(23, 47, 16, 854_775_807).unwrap().and_local_timezone(Utc).unwrap();
286     /// assert_eq!(dt.timestamp_nanos_opt(), Some(9_223_372_036_854_775_807));
287     ///
288     /// let dt = NaiveDate::from_ymd_opt(1677, 9, 21).unwrap().and_hms_nano_opt(0, 12, 43, 145_224_191).unwrap().and_local_timezone(Utc).unwrap();
289     /// assert_eq!(dt.timestamp_nanos_opt(), None);
290     ///
291     /// let dt = NaiveDate::from_ymd_opt(2262, 4, 11).unwrap().and_hms_nano_opt(23, 47, 16, 854_775_808).unwrap().and_local_timezone(Utc).unwrap();
292     /// assert_eq!(dt.timestamp_nanos_opt(), None);
293     /// ```
294     #[inline]
295     #[must_use]
timestamp_nanos_opt(&self) -> Option<i64>296     pub const fn timestamp_nanos_opt(&self) -> Option<i64> {
297         self.datetime.timestamp_nanos_opt()
298     }
299 
300     /// Returns the number of milliseconds since the last second boundary.
301     ///
302     /// In event of a leap second this may exceed 999.
303     #[inline]
304     #[must_use]
timestamp_subsec_millis(&self) -> u32305     pub const fn timestamp_subsec_millis(&self) -> u32 {
306         self.datetime.timestamp_subsec_millis()
307     }
308 
309     /// Returns the number of microseconds since the last second boundary.
310     ///
311     /// In event of a leap second this may exceed 999,999.
312     #[inline]
313     #[must_use]
timestamp_subsec_micros(&self) -> u32314     pub const fn timestamp_subsec_micros(&self) -> u32 {
315         self.datetime.timestamp_subsec_micros()
316     }
317 
318     /// Returns the number of nanoseconds since the last second boundary
319     ///
320     /// In event of a leap second this may exceed 999,999,999.
321     #[inline]
322     #[must_use]
timestamp_subsec_nanos(&self) -> u32323     pub const fn timestamp_subsec_nanos(&self) -> u32 {
324         self.datetime.timestamp_subsec_nanos()
325     }
326 
327     /// Retrieves an associated offset from UTC.
328     #[inline]
329     #[must_use]
offset(&self) -> &Tz::Offset330     pub const fn offset(&self) -> &Tz::Offset {
331         &self.offset
332     }
333 
334     /// Retrieves an associated time zone.
335     #[inline]
336     #[must_use]
timezone(&self) -> Tz337     pub fn timezone(&self) -> Tz {
338         TimeZone::from_offset(&self.offset)
339     }
340 
341     /// Changes the associated time zone.
342     /// The returned `DateTime` references the same instant of time from the perspective of the
343     /// provided time zone.
344     #[inline]
345     #[must_use]
with_timezone<Tz2: TimeZone>(&self, tz: &Tz2) -> DateTime<Tz2>346     pub fn with_timezone<Tz2: TimeZone>(&self, tz: &Tz2) -> DateTime<Tz2> {
347         tz.from_utc_datetime(&self.datetime)
348     }
349 
350     /// Fix the offset from UTC to its current value, dropping the associated timezone information.
351     /// This it useful for converting a generic `DateTime<Tz: Timezone>` to `DateTime<FixedOffset>`.
352     #[inline]
353     #[must_use]
fixed_offset(&self) -> DateTime<FixedOffset>354     pub fn fixed_offset(&self) -> DateTime<FixedOffset> {
355         self.with_timezone(&self.offset().fix())
356     }
357 
358     /// Turn this `DateTime` into a `DateTime<Utc>`, dropping the offset and associated timezone
359     /// information.
360     #[inline]
361     #[must_use]
to_utc(&self) -> DateTime<Utc>362     pub const fn to_utc(&self) -> DateTime<Utc> {
363         DateTime { datetime: self.datetime, offset: Utc }
364     }
365 
366     /// Adds given `TimeDelta` to the current date and time.
367     ///
368     /// # Errors
369     ///
370     /// Returns `None` if the resulting date would be out of range.
371     #[inline]
372     #[must_use]
checked_add_signed(self, rhs: TimeDelta) -> Option<DateTime<Tz>>373     pub fn checked_add_signed(self, rhs: TimeDelta) -> Option<DateTime<Tz>> {
374         let datetime = self.datetime.checked_add_signed(rhs)?;
375         let tz = self.timezone();
376         Some(tz.from_utc_datetime(&datetime))
377     }
378 
379     /// Adds given `Months` to the current date and time.
380     ///
381     /// Uses the last day of the month if the day does not exist in the resulting month.
382     ///
383     /// See [`NaiveDate::checked_add_months`] for more details on behavior.
384     ///
385     /// # Errors
386     ///
387     /// Returns `None` if:
388     /// - The resulting date would be out of range.
389     /// - The local time at the resulting date does not exist or is ambiguous, for example during a
390     ///   daylight saving time transition.
391     #[must_use]
checked_add_months(self, rhs: Months) -> Option<DateTime<Tz>>392     pub fn checked_add_months(self, rhs: Months) -> Option<DateTime<Tz>> {
393         self.naive_local()
394             .checked_add_months(rhs)?
395             .and_local_timezone(Tz::from_offset(&self.offset))
396             .single()
397     }
398 
399     /// Subtracts given `TimeDelta` from the current date and time.
400     ///
401     /// # Errors
402     ///
403     /// Returns `None` if the resulting date would be out of range.
404     #[inline]
405     #[must_use]
checked_sub_signed(self, rhs: TimeDelta) -> Option<DateTime<Tz>>406     pub fn checked_sub_signed(self, rhs: TimeDelta) -> Option<DateTime<Tz>> {
407         let datetime = self.datetime.checked_sub_signed(rhs)?;
408         let tz = self.timezone();
409         Some(tz.from_utc_datetime(&datetime))
410     }
411 
412     /// Subtracts given `Months` from the current date and time.
413     ///
414     /// Uses the last day of the month if the day does not exist in the resulting month.
415     ///
416     /// See [`NaiveDate::checked_sub_months`] for more details on behavior.
417     ///
418     /// # Errors
419     ///
420     /// Returns `None` if:
421     /// - The resulting date would be out of range.
422     /// - The local time at the resulting date does not exist or is ambiguous, for example during a
423     ///   daylight saving time transition.
424     #[must_use]
checked_sub_months(self, rhs: Months) -> Option<DateTime<Tz>>425     pub fn checked_sub_months(self, rhs: Months) -> Option<DateTime<Tz>> {
426         self.naive_local()
427             .checked_sub_months(rhs)?
428             .and_local_timezone(Tz::from_offset(&self.offset))
429             .single()
430     }
431 
432     /// Add a duration in [`Days`] to the date part of the `DateTime`.
433     ///
434     /// # Errors
435     ///
436     /// Returns `None` if:
437     /// - The resulting date would be out of range.
438     /// - The local time at the resulting date does not exist or is ambiguous, for example during a
439     ///   daylight saving time transition.
440     #[must_use]
checked_add_days(self, days: Days) -> Option<Self>441     pub fn checked_add_days(self, days: Days) -> Option<Self> {
442         self.naive_local()
443             .checked_add_days(days)?
444             .and_local_timezone(TimeZone::from_offset(&self.offset))
445             .single()
446     }
447 
448     /// Subtract a duration in [`Days`] from the date part of the `DateTime`.
449     ///
450     /// # Errors
451     ///
452     /// Returns `None` if:
453     /// - The resulting date would be out of range.
454     /// - The local time at the resulting date does not exist or is ambiguous, for example during a
455     ///   daylight saving time transition.
456     #[must_use]
checked_sub_days(self, days: Days) -> Option<Self>457     pub fn checked_sub_days(self, days: Days) -> Option<Self> {
458         self.naive_local()
459             .checked_sub_days(days)?
460             .and_local_timezone(TimeZone::from_offset(&self.offset))
461             .single()
462     }
463 
464     /// Subtracts another `DateTime` from the current date and time.
465     /// This does not overflow or underflow at all.
466     #[inline]
467     #[must_use]
signed_duration_since<Tz2: TimeZone>( self, rhs: impl Borrow<DateTime<Tz2>>, ) -> TimeDelta468     pub fn signed_duration_since<Tz2: TimeZone>(
469         self,
470         rhs: impl Borrow<DateTime<Tz2>>,
471     ) -> TimeDelta {
472         self.datetime.signed_duration_since(rhs.borrow().datetime)
473     }
474 
475     /// Returns a view to the naive UTC datetime.
476     #[inline]
477     #[must_use]
naive_utc(&self) -> NaiveDateTime478     pub const fn naive_utc(&self) -> NaiveDateTime {
479         self.datetime
480     }
481 
482     /// Returns a view to the naive local datetime.
483     ///
484     /// # Panics
485     ///
486     /// [`DateTime`] internally stores the date and time in UTC with a [`NaiveDateTime`]. This
487     /// method will panic if the offset from UTC would push the local datetime outside of the
488     /// representable range of a [`NaiveDateTime`].
489     #[inline]
490     #[must_use]
naive_local(&self) -> NaiveDateTime491     pub fn naive_local(&self) -> NaiveDateTime {
492         self.datetime
493             .checked_add_offset(self.offset.fix())
494             .expect("Local time out of range for `NaiveDateTime`")
495     }
496 
497     /// Returns the naive local datetime.
498     ///
499     /// This makes use of the buffer space outside of the representable range of values of
500     /// `NaiveDateTime`. The result can be used as intermediate value, but should never be exposed
501     /// outside chrono.
502     #[inline]
503     #[must_use]
overflowing_naive_local(&self) -> NaiveDateTime504     pub(crate) fn overflowing_naive_local(&self) -> NaiveDateTime {
505         self.datetime.overflowing_add_offset(self.offset.fix())
506     }
507 
508     /// Retrieve the elapsed years from now to the given [`DateTime`].
509     ///
510     /// # Errors
511     ///
512     /// Returns `None` if `base < self`.
513     #[must_use]
years_since(&self, base: Self) -> Option<u32>514     pub fn years_since(&self, base: Self) -> Option<u32> {
515         let mut years = self.year() - base.year();
516         let earlier_time =
517             (self.month(), self.day(), self.time()) < (base.month(), base.day(), base.time());
518 
519         years -= match earlier_time {
520             true => 1,
521             false => 0,
522         };
523 
524         match years >= 0 {
525             true => Some(years as u32),
526             false => None,
527         }
528     }
529 
530     /// Returns an RFC 2822 date and time string such as `Tue, 1 Jul 2003 10:52:37 +0200`.
531     ///
532     /// # Panics
533     ///
534     /// Panics if the date can not be represented in this format: the year may not be negative and
535     /// can not have more than 4 digits.
536     #[cfg(feature = "alloc")]
537     #[must_use]
to_rfc2822(&self) -> String538     pub fn to_rfc2822(&self) -> String {
539         let mut result = String::with_capacity(32);
540         write_rfc2822(&mut result, self.overflowing_naive_local(), self.offset.fix())
541             .expect("writing rfc2822 datetime to string should never fail");
542         result
543     }
544 
545     /// Returns an RFC 3339 and ISO 8601 date and time string such as `1996-12-19T16:39:57-08:00`.
546     #[cfg(feature = "alloc")]
547     #[must_use]
to_rfc3339(&self) -> String548     pub fn to_rfc3339(&self) -> String {
549         // For some reason a string with a capacity less than 32 is ca 20% slower when benchmarking.
550         let mut result = String::with_capacity(32);
551         let naive = self.overflowing_naive_local();
552         let offset = self.offset.fix();
553         write_rfc3339(&mut result, naive, offset, SecondsFormat::AutoSi, false)
554             .expect("writing rfc3339 datetime to string should never fail");
555         result
556     }
557 
558     /// Return an RFC 3339 and ISO 8601 date and time string with subseconds
559     /// formatted as per `SecondsFormat`.
560     ///
561     /// If `use_z` is true and the timezone is UTC (offset 0), uses `Z` as
562     /// per [`Fixed::TimezoneOffsetColonZ`]. If `use_z` is false, uses
563     /// [`Fixed::TimezoneOffsetColon`]
564     ///
565     /// # Examples
566     ///
567     /// ```rust
568     /// # use chrono::{FixedOffset, SecondsFormat, TimeZone, Utc, NaiveDate};
569     /// let dt = NaiveDate::from_ymd_opt(2018, 1, 26).unwrap().and_hms_micro_opt(18, 30, 9, 453_829).unwrap().and_local_timezone(Utc).unwrap();
570     /// assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Millis, false),
571     ///            "2018-01-26T18:30:09.453+00:00");
572     /// assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Millis, true),
573     ///            "2018-01-26T18:30:09.453Z");
574     /// assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Secs, true),
575     ///            "2018-01-26T18:30:09Z");
576     ///
577     /// let pst = FixedOffset::east_opt(8 * 60 * 60).unwrap();
578     /// let dt = pst.from_local_datetime(&NaiveDate::from_ymd_opt(2018, 1, 26).unwrap().and_hms_micro_opt(10, 30, 9, 453_829).unwrap()).unwrap();
579     /// assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Secs, true),
580     ///            "2018-01-26T10:30:09+08:00");
581     /// ```
582     #[cfg(feature = "alloc")]
583     #[must_use]
to_rfc3339_opts(&self, secform: SecondsFormat, use_z: bool) -> String584     pub fn to_rfc3339_opts(&self, secform: SecondsFormat, use_z: bool) -> String {
585         let mut result = String::with_capacity(38);
586         write_rfc3339(&mut result, self.naive_local(), self.offset.fix(), secform, use_z)
587             .expect("writing rfc3339 datetime to string should never fail");
588         result
589     }
590 
591     /// The minimum possible `DateTime<Utc>`.
592     pub const MIN_UTC: DateTime<Utc> = DateTime { datetime: NaiveDateTime::MIN, offset: Utc };
593     /// The maximum possible `DateTime<Utc>`.
594     pub const MAX_UTC: DateTime<Utc> = DateTime { datetime: NaiveDateTime::MAX, offset: Utc };
595 }
596 
597 impl DateTime<Utc> {
598     /// Makes a new [`DateTime<Utc>`] from the number of non-leap seconds
599     /// since January 1, 1970 0:00:00 UTC (aka "UNIX timestamp")
600     /// and the number of nanoseconds since the last whole non-leap second.
601     ///
602     /// This is guaranteed to round-trip with regard to [`timestamp`](DateTime::timestamp) and
603     /// [`timestamp_subsec_nanos`](DateTime::timestamp_subsec_nanos).
604     ///
605     /// If you need to create a `DateTime` with a [`TimeZone`] different from [`Utc`], use
606     /// [`TimeZone::timestamp_opt`] or [`DateTime::with_timezone`].
607     ///
608     /// The nanosecond part can exceed 1,000,000,000 in order to represent a
609     /// [leap second](NaiveTime#leap-second-handling), but only when `secs % 60 == 59`.
610     /// (The true "UNIX timestamp" cannot represent a leap second unambiguously.)
611     ///
612     /// # Errors
613     ///
614     /// Returns `None` on out-of-range number of seconds and/or
615     /// invalid nanosecond, otherwise returns `Some(DateTime {...})`.
616     ///
617     /// # Example
618     ///
619     /// ```
620     /// use chrono::{DateTime, Utc};
621     ///
622     /// let dt: DateTime<Utc> = DateTime::<Utc>::from_timestamp(1431648000, 0).expect("invalid timestamp");
623     ///
624     /// assert_eq!(dt.to_string(), "2015-05-15 00:00:00 UTC");
625     /// assert_eq!(DateTime::from_timestamp(dt.timestamp(), dt.timestamp_subsec_nanos()).unwrap(), dt);
626     /// ```
627     #[inline]
628     #[must_use]
from_timestamp(secs: i64, nsecs: u32) -> Option<Self>629     pub const fn from_timestamp(secs: i64, nsecs: u32) -> Option<Self> {
630         Some(DateTime {
631             datetime: try_opt!(NaiveDateTime::from_timestamp_opt(secs, nsecs)),
632             offset: Utc,
633         })
634     }
635 
636     /// Makes a new [`DateTime<Utc>`] from the number of non-leap milliseconds
637     /// since January 1, 1970 0:00:00.000 UTC (aka "UNIX timestamp").
638     ///
639     /// This is guaranteed to round-trip with regard to [`timestamp_millis`](DateTime::timestamp_millis).
640     ///
641     /// If you need to create a `DateTime` with a [`TimeZone`] different from [`Utc`], use
642     /// [`TimeZone::timestamp_millis_opt`] or [`DateTime::with_timezone`].
643     ///
644     /// # Errors
645     ///
646     /// Returns `None` on out-of-range number of milliseconds, otherwise returns `Some(DateTime {...})`.
647     ///
648     /// # Example
649     ///
650     /// ```
651     /// use chrono::{DateTime, Utc};
652     ///
653     /// let dt: DateTime<Utc> = DateTime::<Utc>::from_timestamp_millis(947638923004).expect("invalid timestamp");
654     ///
655     /// assert_eq!(dt.to_string(), "2000-01-12 01:02:03.004 UTC");
656     /// assert_eq!(DateTime::from_timestamp_millis(dt.timestamp_millis()).unwrap(), dt);
657     /// ```
658     #[inline]
659     #[must_use]
from_timestamp_millis(millis: i64) -> Option<Self>660     pub const fn from_timestamp_millis(millis: i64) -> Option<Self> {
661         Some(try_opt!(NaiveDateTime::from_timestamp_millis(millis)).and_utc())
662     }
663 
664     /// The Unix Epoch, 1970-01-01 00:00:00 UTC.
665     pub const UNIX_EPOCH: Self = Self { datetime: NaiveDateTime::UNIX_EPOCH, offset: Utc };
666 }
667 
668 impl Default for DateTime<Utc> {
default() -> Self669     fn default() -> Self {
670         Utc.from_utc_datetime(&NaiveDateTime::default())
671     }
672 }
673 
674 #[cfg(feature = "clock")]
675 impl Default for DateTime<Local> {
default() -> Self676     fn default() -> Self {
677         Local.from_utc_datetime(&NaiveDateTime::default())
678     }
679 }
680 
681 impl Default for DateTime<FixedOffset> {
default() -> Self682     fn default() -> Self {
683         FixedOffset::west_opt(0).unwrap().from_utc_datetime(&NaiveDateTime::default())
684     }
685 }
686 
687 /// Convert a `DateTime<Utc>` instance into a `DateTime<FixedOffset>` instance.
688 impl From<DateTime<Utc>> for DateTime<FixedOffset> {
689     /// Convert this `DateTime<Utc>` instance into a `DateTime<FixedOffset>` instance.
690     ///
691     /// Conversion is done via [`DateTime::with_timezone`]. Note that the converted value returned by
692     /// this will be created with a fixed timezone offset of 0.
from(src: DateTime<Utc>) -> Self693     fn from(src: DateTime<Utc>) -> Self {
694         src.with_timezone(&FixedOffset::east_opt(0).unwrap())
695     }
696 }
697 
698 /// Convert a `DateTime<Utc>` instance into a `DateTime<Local>` instance.
699 #[cfg(feature = "clock")]
700 impl From<DateTime<Utc>> for DateTime<Local> {
701     /// Convert this `DateTime<Utc>` instance into a `DateTime<Local>` instance.
702     ///
703     /// Conversion is performed via [`DateTime::with_timezone`], accounting for the difference in timezones.
from(src: DateTime<Utc>) -> Self704     fn from(src: DateTime<Utc>) -> Self {
705         src.with_timezone(&Local)
706     }
707 }
708 
709 /// Convert a `DateTime<FixedOffset>` instance into a `DateTime<Utc>` instance.
710 impl From<DateTime<FixedOffset>> for DateTime<Utc> {
711     /// Convert this `DateTime<FixedOffset>` instance into a `DateTime<Utc>` instance.
712     ///
713     /// Conversion is performed via [`DateTime::with_timezone`], accounting for the timezone
714     /// difference.
from(src: DateTime<FixedOffset>) -> Self715     fn from(src: DateTime<FixedOffset>) -> Self {
716         src.with_timezone(&Utc)
717     }
718 }
719 
720 /// Convert a `DateTime<FixedOffset>` instance into a `DateTime<Local>` instance.
721 #[cfg(feature = "clock")]
722 impl From<DateTime<FixedOffset>> for DateTime<Local> {
723     /// Convert this `DateTime<FixedOffset>` instance into a `DateTime<Local>` instance.
724     ///
725     /// Conversion is performed via [`DateTime::with_timezone`]. Returns the equivalent value in local
726     /// time.
from(src: DateTime<FixedOffset>) -> Self727     fn from(src: DateTime<FixedOffset>) -> Self {
728         src.with_timezone(&Local)
729     }
730 }
731 
732 /// Convert a `DateTime<Local>` instance into a `DateTime<Utc>` instance.
733 #[cfg(feature = "clock")]
734 impl From<DateTime<Local>> for DateTime<Utc> {
735     /// Convert this `DateTime<Local>` instance into a `DateTime<Utc>` instance.
736     ///
737     /// Conversion is performed via [`DateTime::with_timezone`], accounting for the difference in
738     /// timezones.
from(src: DateTime<Local>) -> Self739     fn from(src: DateTime<Local>) -> Self {
740         src.with_timezone(&Utc)
741     }
742 }
743 
744 /// Convert a `DateTime<Local>` instance into a `DateTime<FixedOffset>` instance.
745 #[cfg(feature = "clock")]
746 impl From<DateTime<Local>> for DateTime<FixedOffset> {
747     /// Convert this `DateTime<Local>` instance into a `DateTime<FixedOffset>` instance.
748     ///
749     /// Conversion is performed via [`DateTime::with_timezone`].
from(src: DateTime<Local>) -> Self750     fn from(src: DateTime<Local>) -> Self {
751         src.with_timezone(&src.offset().fix())
752     }
753 }
754 
755 /// Maps the local datetime to other datetime with given conversion function.
map_local<Tz: TimeZone, F>(dt: &DateTime<Tz>, mut f: F) -> Option<DateTime<Tz>> where F: FnMut(NaiveDateTime) -> Option<NaiveDateTime>,756 fn map_local<Tz: TimeZone, F>(dt: &DateTime<Tz>, mut f: F) -> Option<DateTime<Tz>>
757 where
758     F: FnMut(NaiveDateTime) -> Option<NaiveDateTime>,
759 {
760     f(dt.overflowing_naive_local())
761         .and_then(|datetime| dt.timezone().from_local_datetime(&datetime).single())
762         .filter(|dt| dt >= &DateTime::<Utc>::MIN_UTC && dt <= &DateTime::<Utc>::MAX_UTC)
763 }
764 
765 impl DateTime<FixedOffset> {
766     /// Parses an RFC 2822 date-and-time string into a `DateTime<FixedOffset>` value.
767     ///
768     /// This parses valid RFC 2822 datetime strings (such as `Tue, 1 Jul 2003 10:52:37 +0200`)
769     /// and returns a new [`DateTime`] instance with the parsed timezone as the [`FixedOffset`].
770     ///
771     /// RFC 2822 is the internet message standard that specifies the representation of times in HTTP
772     /// and email headers. It is the 2001 revision of RFC 822, and is itself revised as RFC 5322 in
773     /// 2008.
774     ///
775     /// # Support for the obsolete date format
776     ///
777     /// - A 2-digit year is interpreted to be a year in 1950-2049.
778     /// - The standard allows comments and whitespace between many of the tokens. See [4.3] and
779     ///   [Appendix A.5]
780     /// - Single letter 'military' time zone names are parsed as a `-0000` offset.
781     ///   They were defined with the wrong sign in RFC 822 and corrected in RFC 2822. But because
782     ///   the meaning is now ambiguous, the standard says they should be be considered as `-0000`
783     ///   unless there is out-of-band information confirming their meaning.
784     ///   The exception is `Z`, which remains identical to `+0000`.
785     ///
786     /// [4.3]: https://www.rfc-editor.org/rfc/rfc2822#section-4.3
787     /// [Appendix A.5]: https://www.rfc-editor.org/rfc/rfc2822#appendix-A.5
788     ///
789     /// # Example
790     ///
791     /// ```
792     /// # use chrono::{DateTime, FixedOffset, TimeZone};
793     /// assert_eq!(
794     ///     DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:16:09 GMT").unwrap(),
795     ///     FixedOffset::east_opt(0).unwrap().with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap()
796     /// );
797     /// ```
parse_from_rfc2822(s: &str) -> ParseResult<DateTime<FixedOffset>>798     pub fn parse_from_rfc2822(s: &str) -> ParseResult<DateTime<FixedOffset>> {
799         const ITEMS: &[Item<'static>] = &[Item::Fixed(Fixed::RFC2822)];
800         let mut parsed = Parsed::new();
801         parse(&mut parsed, s, ITEMS.iter())?;
802         parsed.to_datetime()
803     }
804 
805     /// Parses an RFC 3339 date-and-time string into a `DateTime<FixedOffset>` value.
806     ///
807     /// Parses all valid RFC 3339 values (as well as the subset of valid ISO 8601 values that are
808     /// also valid RFC 3339 date-and-time values) and returns a new [`DateTime`] with a
809     /// [`FixedOffset`] corresponding to the parsed timezone. While RFC 3339 values come in a wide
810     /// variety of shapes and sizes, `1996-12-19T16:39:57-08:00` is an example of the most commonly
811     /// encountered variety of RFC 3339 formats.
812     ///
813     /// Why isn't this named `parse_from_iso8601`? That's because ISO 8601 allows representing
814     /// values in a wide range of formats, only some of which represent actual date-and-time
815     /// instances (rather than periods, ranges, dates, or times). Some valid ISO 8601 values are
816     /// also simultaneously valid RFC 3339 values, but not all RFC 3339 values are valid ISO 8601
817     /// values (or the other way around).
parse_from_rfc3339(s: &str) -> ParseResult<DateTime<FixedOffset>>818     pub fn parse_from_rfc3339(s: &str) -> ParseResult<DateTime<FixedOffset>> {
819         let mut parsed = Parsed::new();
820         let (s, _) = parse_rfc3339(&mut parsed, s)?;
821         if !s.is_empty() {
822             return Err(TOO_LONG);
823         }
824         parsed.to_datetime()
825     }
826 
827     /// Parses a string from a user-specified format into a `DateTime<FixedOffset>` value.
828     ///
829     /// Note that this method *requires a timezone* in the input string. See
830     /// [`NaiveDateTime::parse_from_str`](./naive/struct.NaiveDateTime.html#method.parse_from_str)
831     /// for a version that does not require a timezone in the to-be-parsed str. The returned
832     /// [`DateTime`] value will have a [`FixedOffset`] reflecting the parsed timezone.
833     ///
834     /// See the [`format::strftime` module](./format/strftime/index.html) for supported format
835     /// sequences.
836     ///
837     /// # Example
838     ///
839     /// ```rust
840     /// use chrono::{DateTime, FixedOffset, TimeZone, NaiveDate};
841     ///
842     /// let dt = DateTime::parse_from_str(
843     ///     "1983 Apr 13 12:09:14.274 +0000", "%Y %b %d %H:%M:%S%.3f %z");
844     /// assert_eq!(dt, Ok(FixedOffset::east_opt(0).unwrap().from_local_datetime(&NaiveDate::from_ymd_opt(1983, 4, 13).unwrap().and_hms_milli_opt(12, 9, 14, 274).unwrap()).unwrap()));
845     /// ```
parse_from_str(s: &str, fmt: &str) -> ParseResult<DateTime<FixedOffset>>846     pub fn parse_from_str(s: &str, fmt: &str) -> ParseResult<DateTime<FixedOffset>> {
847         let mut parsed = Parsed::new();
848         parse(&mut parsed, s, StrftimeItems::new(fmt))?;
849         parsed.to_datetime()
850     }
851 
852     /// Parses a string from a user-specified format into a `DateTime<FixedOffset>` value, and a
853     /// slice with the remaining portion of the string.
854     ///
855     /// Note that this method *requires a timezone* in the input string. See
856     /// [`NaiveDateTime::parse_and_remainder`] for a version that does not
857     /// require a timezone in `s`. The returned [`DateTime`] value will have a [`FixedOffset`]
858     /// reflecting the parsed timezone.
859     ///
860     /// See the [`format::strftime` module](./format/strftime/index.html) for supported format
861     /// sequences.
862     ///
863     /// Similar to [`parse_from_str`](#method.parse_from_str).
864     ///
865     /// # Example
866     ///
867     /// ```rust
868     /// # use chrono::{DateTime, FixedOffset, TimeZone};
869     /// let (datetime, remainder) = DateTime::parse_and_remainder(
870     ///     "2015-02-18 23:16:09 +0200 trailing text", "%Y-%m-%d %H:%M:%S %z").unwrap();
871     /// assert_eq!(
872     ///     datetime,
873     ///     FixedOffset::east_opt(2*3600).unwrap().with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap()
874     /// );
875     /// assert_eq!(remainder, " trailing text");
876     /// ```
parse_and_remainder<'a>( s: &'a str, fmt: &str, ) -> ParseResult<(DateTime<FixedOffset>, &'a str)>877     pub fn parse_and_remainder<'a>(
878         s: &'a str,
879         fmt: &str,
880     ) -> ParseResult<(DateTime<FixedOffset>, &'a str)> {
881         let mut parsed = Parsed::new();
882         let remainder = parse_and_remainder(&mut parsed, s, StrftimeItems::new(fmt))?;
883         parsed.to_datetime().map(|d| (d, remainder))
884     }
885 }
886 
887 impl<Tz: TimeZone> DateTime<Tz>
888 where
889     Tz::Offset: fmt::Display,
890 {
891     /// Formats the combined date and time with the specified formatting items.
892     #[cfg(feature = "alloc")]
893     #[inline]
894     #[must_use]
format_with_items<'a, I, B>(&self, items: I) -> DelayedFormat<I> where I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>,895     pub fn format_with_items<'a, I, B>(&self, items: I) -> DelayedFormat<I>
896     where
897         I: Iterator<Item = B> + Clone,
898         B: Borrow<Item<'a>>,
899     {
900         let local = self.overflowing_naive_local();
901         DelayedFormat::new_with_offset(Some(local.date()), Some(local.time()), &self.offset, items)
902     }
903 
904     /// Formats the combined date and time per the specified format string.
905     ///
906     /// See the [`crate::format::strftime`] module for the supported escape sequences.
907     ///
908     /// # Example
909     /// ```rust
910     /// use chrono::prelude::*;
911     ///
912     /// let date_time: DateTime<Utc> = Utc.with_ymd_and_hms(2017, 04, 02, 12, 50, 32).unwrap();
913     /// let formatted = format!("{}", date_time.format("%d/%m/%Y %H:%M"));
914     /// assert_eq!(formatted, "02/04/2017 12:50");
915     /// ```
916     #[cfg(feature = "alloc")]
917     #[inline]
918     #[must_use]
format<'a>(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>>919     pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>> {
920         self.format_with_items(StrftimeItems::new(fmt))
921     }
922 
923     /// Formats the combined date and time with the specified formatting items and locale.
924     #[cfg(all(feature = "unstable-locales", feature = "alloc"))]
925     #[inline]
926     #[must_use]
format_localized_with_items<'a, I, B>( &self, items: I, locale: Locale, ) -> DelayedFormat<I> where I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>,927     pub fn format_localized_with_items<'a, I, B>(
928         &self,
929         items: I,
930         locale: Locale,
931     ) -> DelayedFormat<I>
932     where
933         I: Iterator<Item = B> + Clone,
934         B: Borrow<Item<'a>>,
935     {
936         let local = self.overflowing_naive_local();
937         DelayedFormat::new_with_offset_and_locale(
938             Some(local.date()),
939             Some(local.time()),
940             &self.offset,
941             items,
942             locale,
943         )
944     }
945 
946     /// Formats the combined date and time per the specified format string and
947     /// locale.
948     ///
949     /// See the [`crate::format::strftime`] module on the supported escape
950     /// sequences.
951     #[cfg(all(feature = "unstable-locales", feature = "alloc"))]
952     #[inline]
953     #[must_use]
format_localized<'a>( &self, fmt: &'a str, locale: Locale, ) -> DelayedFormat<StrftimeItems<'a>>954     pub fn format_localized<'a>(
955         &self,
956         fmt: &'a str,
957         locale: Locale,
958     ) -> DelayedFormat<StrftimeItems<'a>> {
959         self.format_localized_with_items(StrftimeItems::new_with_locale(fmt, locale), locale)
960     }
961 }
962 
963 impl<Tz: TimeZone> Datelike for DateTime<Tz> {
964     #[inline]
year(&self) -> i32965     fn year(&self) -> i32 {
966         self.overflowing_naive_local().year()
967     }
968     #[inline]
month(&self) -> u32969     fn month(&self) -> u32 {
970         self.overflowing_naive_local().month()
971     }
972     #[inline]
month0(&self) -> u32973     fn month0(&self) -> u32 {
974         self.overflowing_naive_local().month0()
975     }
976     #[inline]
day(&self) -> u32977     fn day(&self) -> u32 {
978         self.overflowing_naive_local().day()
979     }
980     #[inline]
day0(&self) -> u32981     fn day0(&self) -> u32 {
982         self.overflowing_naive_local().day0()
983     }
984     #[inline]
ordinal(&self) -> u32985     fn ordinal(&self) -> u32 {
986         self.overflowing_naive_local().ordinal()
987     }
988     #[inline]
ordinal0(&self) -> u32989     fn ordinal0(&self) -> u32 {
990         self.overflowing_naive_local().ordinal0()
991     }
992     #[inline]
weekday(&self) -> Weekday993     fn weekday(&self) -> Weekday {
994         self.overflowing_naive_local().weekday()
995     }
996     #[inline]
iso_week(&self) -> IsoWeek997     fn iso_week(&self) -> IsoWeek {
998         self.overflowing_naive_local().iso_week()
999     }
1000 
1001     #[inline]
1002     /// Makes a new `DateTime` with the year number changed, while keeping the same month and day.
1003     ///
1004     /// See also the [`NaiveDate::with_year`] method.
1005     ///
1006     /// # Errors
1007     ///
1008     /// Returns `None` if:
1009     /// - The resulting date does not exist.
1010     /// - When the `NaiveDateTime` would be out of range.
1011     /// - The local time at the resulting date does not exist or is ambiguous, for example during a
1012     ///   daylight saving time transition.
with_year(&self, year: i32) -> Option<DateTime<Tz>>1013     fn with_year(&self, year: i32) -> Option<DateTime<Tz>> {
1014         map_local(self, |datetime| datetime.with_year(year))
1015     }
1016 
1017     /// Makes a new `DateTime` with the month number (starting from 1) changed.
1018     ///
1019     /// See also the [`NaiveDate::with_month`] method.
1020     ///
1021     /// # Errors
1022     ///
1023     /// Returns `None` if:
1024     /// - The resulting date does not exist.
1025     /// - The value for `month` is invalid.
1026     /// - The local time at the resulting date does not exist or is ambiguous, for example during a
1027     ///   daylight saving time transition.
1028     #[inline]
with_month(&self, month: u32) -> Option<DateTime<Tz>>1029     fn with_month(&self, month: u32) -> Option<DateTime<Tz>> {
1030         map_local(self, |datetime| datetime.with_month(month))
1031     }
1032 
1033     /// Makes a new `DateTime` with the month number (starting from 0) changed.
1034     ///
1035     /// See also the [`NaiveDate::with_month0`] method.
1036     ///
1037     /// # Errors
1038     ///
1039     /// Returns `None` if:
1040     /// - The resulting date does not exist.
1041     /// - The value for `month0` is invalid.
1042     /// - The local time at the resulting date does not exist or is ambiguous, for example during a
1043     ///   daylight saving time transition.
1044     #[inline]
with_month0(&self, month0: u32) -> Option<DateTime<Tz>>1045     fn with_month0(&self, month0: u32) -> Option<DateTime<Tz>> {
1046         map_local(self, |datetime| datetime.with_month0(month0))
1047     }
1048 
1049     /// Makes a new `DateTime` with the day of month (starting from 1) changed.
1050     ///
1051     /// See also the [`NaiveDate::with_day`] method.
1052     ///
1053     /// # Errors
1054     ///
1055     /// Returns `None` if:
1056     /// - The resulting date does not exist.
1057     /// - The value for `day` is invalid.
1058     /// - The local time at the resulting date does not exist or is ambiguous, for example during a
1059     ///   daylight saving time transition.
1060     #[inline]
with_day(&self, day: u32) -> Option<DateTime<Tz>>1061     fn with_day(&self, day: u32) -> Option<DateTime<Tz>> {
1062         map_local(self, |datetime| datetime.with_day(day))
1063     }
1064 
1065     /// Makes a new `DateTime` with the day of month (starting from 0) changed.
1066     ///
1067     /// See also the [`NaiveDate::with_day0`] method.
1068     ///
1069     /// # Errors
1070     ///
1071     /// Returns `None` if:
1072     /// - The resulting date does not exist.
1073     /// - The value for `day0` is invalid.
1074     /// - The local time at the resulting date does not exist or is ambiguous, for example during a
1075     ///   daylight saving time transition.
1076     #[inline]
with_day0(&self, day0: u32) -> Option<DateTime<Tz>>1077     fn with_day0(&self, day0: u32) -> Option<DateTime<Tz>> {
1078         map_local(self, |datetime| datetime.with_day0(day0))
1079     }
1080 
1081     /// Makes a new `DateTime` with the day of year (starting from 1) changed.
1082     ///
1083     /// See also the [`NaiveDate::with_ordinal`] method.
1084     ///
1085     /// # Errors
1086     ///
1087     /// Returns `None` if:
1088     /// - The resulting date does not exist.
1089     /// - The value for `ordinal` is invalid.
1090     /// - The local time at the resulting date does not exist or is ambiguous, for example during a
1091     ///   daylight saving time transition.
1092     #[inline]
with_ordinal(&self, ordinal: u32) -> Option<DateTime<Tz>>1093     fn with_ordinal(&self, ordinal: u32) -> Option<DateTime<Tz>> {
1094         map_local(self, |datetime| datetime.with_ordinal(ordinal))
1095     }
1096 
1097     /// Makes a new `DateTime` with the day of year (starting from 0) changed.
1098     ///
1099     /// See also the [`NaiveDate::with_ordinal0`] method.
1100     ///
1101     /// # Errors
1102     ///
1103     /// Returns `None` if:
1104     /// - The resulting date does not exist.
1105     /// - The value for `ordinal0` is invalid.
1106     /// - The local time at the resulting date does not exist or is ambiguous, for example during a
1107     ///   daylight saving time transition.
1108     #[inline]
with_ordinal0(&self, ordinal0: u32) -> Option<DateTime<Tz>>1109     fn with_ordinal0(&self, ordinal0: u32) -> Option<DateTime<Tz>> {
1110         map_local(self, |datetime| datetime.with_ordinal0(ordinal0))
1111     }
1112 }
1113 
1114 impl<Tz: TimeZone> Timelike for DateTime<Tz> {
1115     #[inline]
hour(&self) -> u321116     fn hour(&self) -> u32 {
1117         self.overflowing_naive_local().hour()
1118     }
1119     #[inline]
minute(&self) -> u321120     fn minute(&self) -> u32 {
1121         self.overflowing_naive_local().minute()
1122     }
1123     #[inline]
second(&self) -> u321124     fn second(&self) -> u32 {
1125         self.overflowing_naive_local().second()
1126     }
1127     #[inline]
nanosecond(&self) -> u321128     fn nanosecond(&self) -> u32 {
1129         self.overflowing_naive_local().nanosecond()
1130     }
1131 
1132     /// Makes a new `DateTime` with the hour number changed.
1133     ///
1134     /// See also the [`NaiveTime::with_hour`] method.
1135     ///
1136     /// # Errors
1137     ///
1138     /// Returns `None` if:
1139     /// - The value for `hour` is invalid.
1140     /// - The local time at the resulting date does not exist or is ambiguous, for example during a
1141     ///   daylight saving time transition.
1142     #[inline]
with_hour(&self, hour: u32) -> Option<DateTime<Tz>>1143     fn with_hour(&self, hour: u32) -> Option<DateTime<Tz>> {
1144         map_local(self, |datetime| datetime.with_hour(hour))
1145     }
1146 
1147     /// Makes a new `DateTime` with the minute number changed.
1148     ///
1149     /// See also the [`NaiveTime::with_minute`] method.
1150     ///
1151     /// # Errors
1152     ///
1153     /// - The value for `minute` is invalid.
1154     /// - The local time at the resulting date does not exist or is ambiguous, for example during a
1155     ///   daylight saving time transition.
1156     #[inline]
with_minute(&self, min: u32) -> Option<DateTime<Tz>>1157     fn with_minute(&self, min: u32) -> Option<DateTime<Tz>> {
1158         map_local(self, |datetime| datetime.with_minute(min))
1159     }
1160 
1161     /// Makes a new `DateTime` with the second number changed.
1162     ///
1163     /// As with the [`second`](#method.second) method,
1164     /// the input range is restricted to 0 through 59.
1165     ///
1166     /// See also the [`NaiveTime::with_second`] method.
1167     ///
1168     /// # Errors
1169     ///
1170     /// Returns `None` if:
1171     /// - The value for `second` is invalid.
1172     /// - The local time at the resulting date does not exist or is ambiguous, for example during a
1173     ///   daylight saving time transition.
1174     #[inline]
with_second(&self, sec: u32) -> Option<DateTime<Tz>>1175     fn with_second(&self, sec: u32) -> Option<DateTime<Tz>> {
1176         map_local(self, |datetime| datetime.with_second(sec))
1177     }
1178 
1179     /// Makes a new `DateTime` with nanoseconds since the whole non-leap second changed.
1180     ///
1181     /// Returns `None` when the resulting `NaiveDateTime` would be invalid.
1182     /// As with the [`NaiveDateTime::nanosecond`] method,
1183     /// the input range can exceed 1,000,000,000 for leap seconds.
1184     ///
1185     /// See also the [`NaiveTime::with_nanosecond`] method.
1186     ///
1187     /// # Errors
1188     ///
1189     /// Returns `None` if `nanosecond >= 2,000,000,000`.
1190     #[inline]
with_nanosecond(&self, nano: u32) -> Option<DateTime<Tz>>1191     fn with_nanosecond(&self, nano: u32) -> Option<DateTime<Tz>> {
1192         map_local(self, |datetime| datetime.with_nanosecond(nano))
1193     }
1194 }
1195 
1196 // we need them as automatic impls cannot handle associated types
1197 impl<Tz: TimeZone> Copy for DateTime<Tz> where <Tz as TimeZone>::Offset: Copy {}
1198 unsafe impl<Tz: TimeZone> Send for DateTime<Tz> where <Tz as TimeZone>::Offset: Send {}
1199 
1200 impl<Tz: TimeZone, Tz2: TimeZone> PartialEq<DateTime<Tz2>> for DateTime<Tz> {
eq(&self, other: &DateTime<Tz2>) -> bool1201     fn eq(&self, other: &DateTime<Tz2>) -> bool {
1202         self.datetime == other.datetime
1203     }
1204 }
1205 
1206 impl<Tz: TimeZone> Eq for DateTime<Tz> {}
1207 
1208 impl<Tz: TimeZone, Tz2: TimeZone> PartialOrd<DateTime<Tz2>> for DateTime<Tz> {
1209     /// Compare two DateTimes based on their true time, ignoring time zones
1210     ///
1211     /// # Example
1212     ///
1213     /// ```
1214     /// use chrono::prelude::*;
1215     ///
1216     /// let earlier = Utc.with_ymd_and_hms(2015, 5, 15, 2, 0, 0).unwrap().with_timezone(&FixedOffset::west_opt(1 * 3600).unwrap());
1217     /// let later   = Utc.with_ymd_and_hms(2015, 5, 15, 3, 0, 0).unwrap().with_timezone(&FixedOffset::west_opt(5 * 3600).unwrap());
1218     ///
1219     /// assert_eq!(earlier.to_string(), "2015-05-15 01:00:00 -01:00");
1220     /// assert_eq!(later.to_string(), "2015-05-14 22:00:00 -05:00");
1221     ///
1222     /// assert!(later > earlier);
1223     /// ```
partial_cmp(&self, other: &DateTime<Tz2>) -> Option<Ordering>1224     fn partial_cmp(&self, other: &DateTime<Tz2>) -> Option<Ordering> {
1225         self.datetime.partial_cmp(&other.datetime)
1226     }
1227 }
1228 
1229 impl<Tz: TimeZone> Ord for DateTime<Tz> {
cmp(&self, other: &DateTime<Tz>) -> Ordering1230     fn cmp(&self, other: &DateTime<Tz>) -> Ordering {
1231         self.datetime.cmp(&other.datetime)
1232     }
1233 }
1234 
1235 impl<Tz: TimeZone> hash::Hash for DateTime<Tz> {
hash<H: hash::Hasher>(&self, state: &mut H)1236     fn hash<H: hash::Hasher>(&self, state: &mut H) {
1237         self.datetime.hash(state)
1238     }
1239 }
1240 
1241 /// Add `TimeDelta` to `DateTime`.
1242 ///
1243 /// As a part of Chrono's [leap second handling], the addition assumes that **there is no leap
1244 /// second ever**, except when the `NaiveDateTime` itself represents a leap  second in which case
1245 /// the assumption becomes that **there is exactly a single leap second ever**.
1246 ///
1247 /// # Panics
1248 ///
1249 /// Panics if the resulting date would be out of range.
1250 /// Consider using [`DateTime<Tz>::checked_add_signed`] to get an `Option` instead.
1251 impl<Tz: TimeZone> Add<TimeDelta> for DateTime<Tz> {
1252     type Output = DateTime<Tz>;
1253 
1254     #[inline]
add(self, rhs: TimeDelta) -> DateTime<Tz>1255     fn add(self, rhs: TimeDelta) -> DateTime<Tz> {
1256         self.checked_add_signed(rhs).expect("`DateTime + TimeDelta` overflowed")
1257     }
1258 }
1259 
1260 /// Add `std::time::Duration` to `DateTime`.
1261 ///
1262 /// As a part of Chrono's [leap second handling], the addition assumes that **there is no leap
1263 /// second ever**, except when the `NaiveDateTime` itself represents a leap  second in which case
1264 /// the assumption becomes that **there is exactly a single leap second ever**.
1265 ///
1266 /// # Panics
1267 ///
1268 /// Panics if the resulting date would be out of range.
1269 /// Consider using [`DateTime<Tz>::checked_add_signed`] to get an `Option` instead.
1270 impl<Tz: TimeZone> Add<Duration> for DateTime<Tz> {
1271     type Output = DateTime<Tz>;
1272 
1273     #[inline]
add(self, rhs: Duration) -> DateTime<Tz>1274     fn add(self, rhs: Duration) -> DateTime<Tz> {
1275         let rhs = TimeDelta::from_std(rhs)
1276             .expect("overflow converting from core::time::Duration to TimeDelta");
1277         self.checked_add_signed(rhs).expect("`DateTime + TimeDelta` overflowed")
1278     }
1279 }
1280 
1281 /// Add-assign `chrono::Duration` to `DateTime`.
1282 ///
1283 /// As a part of Chrono's [leap second handling], the addition assumes that **there is no leap
1284 /// second ever**, except when the `NaiveDateTime` itself represents a leap  second in which case
1285 /// the assumption becomes that **there is exactly a single leap second ever**.
1286 ///
1287 /// # Panics
1288 ///
1289 /// Panics if the resulting date would be out of range.
1290 /// Consider using [`DateTime<Tz>::checked_add_signed`] to get an `Option` instead.
1291 impl<Tz: TimeZone> AddAssign<TimeDelta> for DateTime<Tz> {
1292     #[inline]
add_assign(&mut self, rhs: TimeDelta)1293     fn add_assign(&mut self, rhs: TimeDelta) {
1294         let datetime =
1295             self.datetime.checked_add_signed(rhs).expect("`DateTime + TimeDelta` overflowed");
1296         let tz = self.timezone();
1297         *self = tz.from_utc_datetime(&datetime);
1298     }
1299 }
1300 
1301 /// Add-assign `std::time::Duration` to `DateTime`.
1302 ///
1303 /// As a part of Chrono's [leap second handling], the addition assumes that **there is no leap
1304 /// second ever**, except when the `NaiveDateTime` itself represents a leap  second in which case
1305 /// the assumption becomes that **there is exactly a single leap second ever**.
1306 ///
1307 /// # Panics
1308 ///
1309 /// Panics if the resulting date would be out of range.
1310 /// Consider using [`DateTime<Tz>::checked_add_signed`] to get an `Option` instead.
1311 impl<Tz: TimeZone> AddAssign<Duration> for DateTime<Tz> {
1312     #[inline]
add_assign(&mut self, rhs: Duration)1313     fn add_assign(&mut self, rhs: Duration) {
1314         let rhs = TimeDelta::from_std(rhs)
1315             .expect("overflow converting from core::time::Duration to TimeDelta");
1316         *self += rhs;
1317     }
1318 }
1319 
1320 /// Add `FixedOffset` to the datetime value of `DateTime` (offset remains unchanged).
1321 ///
1322 /// # Panics
1323 ///
1324 /// Panics if the resulting date would be out of range.
1325 impl<Tz: TimeZone> Add<FixedOffset> for DateTime<Tz> {
1326     type Output = DateTime<Tz>;
1327 
1328     #[inline]
add(mut self, rhs: FixedOffset) -> DateTime<Tz>1329     fn add(mut self, rhs: FixedOffset) -> DateTime<Tz> {
1330         self.datetime =
1331             self.naive_utc().checked_add_offset(rhs).expect("`DateTime + FixedOffset` overflowed");
1332         self
1333     }
1334 }
1335 
1336 /// Add `Months` to `DateTime`.
1337 ///
1338 /// The result will be clamped to valid days in the resulting month, see `checked_add_months` for
1339 /// details.
1340 ///
1341 /// # Panics
1342 ///
1343 /// Panics if:
1344 /// - The resulting date would be out of range.
1345 /// - The local time at the resulting date does not exist or is ambiguous, for example during a
1346 ///   daylight saving time transition.
1347 ///
1348 /// Strongly consider using [`DateTime<Tz>::checked_add_months`] to get an `Option` instead.
1349 impl<Tz: TimeZone> Add<Months> for DateTime<Tz> {
1350     type Output = DateTime<Tz>;
1351 
add(self, rhs: Months) -> Self::Output1352     fn add(self, rhs: Months) -> Self::Output {
1353         self.checked_add_months(rhs).expect("`DateTime + Months` out of range")
1354     }
1355 }
1356 
1357 /// Subtract `TimeDelta` from `DateTime`.
1358 ///
1359 /// This is the same as the addition with a negated `TimeDelta`.
1360 ///
1361 /// As a part of Chrono's [leap second handling] the subtraction assumes that **there is no leap
1362 /// second ever**, except when the `DateTime` itself represents a leap second in which case
1363 /// the assumption becomes that **there is exactly a single leap second ever**.
1364 ///
1365 /// # Panics
1366 ///
1367 /// Panics if the resulting date would be out of range.
1368 /// Consider using [`DateTime<Tz>::checked_sub_signed`] to get an `Option` instead.
1369 impl<Tz: TimeZone> Sub<TimeDelta> for DateTime<Tz> {
1370     type Output = DateTime<Tz>;
1371 
1372     #[inline]
sub(self, rhs: TimeDelta) -> DateTime<Tz>1373     fn sub(self, rhs: TimeDelta) -> DateTime<Tz> {
1374         self.checked_sub_signed(rhs).expect("`DateTime - TimeDelta` overflowed")
1375     }
1376 }
1377 
1378 /// Subtract `std::time::Duration` from `DateTime`.
1379 ///
1380 /// As a part of Chrono's [leap second handling] the subtraction assumes that **there is no leap
1381 /// second ever**, except when the `DateTime` itself represents a leap second in which case
1382 /// the assumption becomes that **there is exactly a single leap second ever**.
1383 ///
1384 /// # Panics
1385 ///
1386 /// Panics if the resulting date would be out of range.
1387 /// Consider using [`DateTime<Tz>::checked_sub_signed`] to get an `Option` instead.
1388 impl<Tz: TimeZone> Sub<Duration> for DateTime<Tz> {
1389     type Output = DateTime<Tz>;
1390 
1391     #[inline]
sub(self, rhs: Duration) -> DateTime<Tz>1392     fn sub(self, rhs: Duration) -> DateTime<Tz> {
1393         let rhs = TimeDelta::from_std(rhs)
1394             .expect("overflow converting from core::time::Duration to TimeDelta");
1395         self.checked_sub_signed(rhs).expect("`DateTime - TimeDelta` overflowed")
1396     }
1397 }
1398 
1399 /// Subtract-assign `TimeDelta` from `DateTime`.
1400 ///
1401 /// This is the same as the addition with a negated `TimeDelta`.
1402 ///
1403 /// As a part of Chrono's [leap second handling], the addition assumes that **there is no leap
1404 /// second ever**, except when the `DateTime` itself represents a leap  second in which case
1405 /// the assumption becomes that **there is exactly a single leap second ever**.
1406 ///
1407 /// # Panics
1408 ///
1409 /// Panics if the resulting date would be out of range.
1410 /// Consider using [`DateTime<Tz>::checked_sub_signed`] to get an `Option` instead.
1411 impl<Tz: TimeZone> SubAssign<TimeDelta> for DateTime<Tz> {
1412     #[inline]
sub_assign(&mut self, rhs: TimeDelta)1413     fn sub_assign(&mut self, rhs: TimeDelta) {
1414         let datetime =
1415             self.datetime.checked_sub_signed(rhs).expect("`DateTime - TimeDelta` overflowed");
1416         let tz = self.timezone();
1417         *self = tz.from_utc_datetime(&datetime)
1418     }
1419 }
1420 
1421 /// Subtract-assign `std::time::Duration` from `DateTime`.
1422 ///
1423 /// As a part of Chrono's [leap second handling], the addition assumes that **there is no leap
1424 /// second ever**, except when the `DateTime` itself represents a leap  second in which case
1425 /// the assumption becomes that **there is exactly a single leap second ever**.
1426 ///
1427 /// # Panics
1428 ///
1429 /// Panics if the resulting date would be out of range.
1430 /// Consider using [`DateTime<Tz>::checked_sub_signed`] to get an `Option` instead.
1431 impl<Tz: TimeZone> SubAssign<Duration> for DateTime<Tz> {
1432     #[inline]
sub_assign(&mut self, rhs: Duration)1433     fn sub_assign(&mut self, rhs: Duration) {
1434         let rhs = TimeDelta::from_std(rhs)
1435             .expect("overflow converting from core::time::Duration to TimeDelta");
1436         *self -= rhs;
1437     }
1438 }
1439 
1440 /// Subtract `FixedOffset` from the datetime value of `DateTime` (offset remains unchanged).
1441 ///
1442 /// # Panics
1443 ///
1444 /// Panics if the resulting date would be out of range.
1445 impl<Tz: TimeZone> Sub<FixedOffset> for DateTime<Tz> {
1446     type Output = DateTime<Tz>;
1447 
1448     #[inline]
sub(mut self, rhs: FixedOffset) -> DateTime<Tz>1449     fn sub(mut self, rhs: FixedOffset) -> DateTime<Tz> {
1450         self.datetime =
1451             self.naive_utc().checked_sub_offset(rhs).expect("`DateTime - FixedOffset` overflowed");
1452         self
1453     }
1454 }
1455 
1456 /// Subtract `Months` from `DateTime`.
1457 ///
1458 /// The result will be clamped to valid days in the resulting month, see
1459 /// [`DateTime<Tz>::checked_sub_months`] for details.
1460 ///
1461 /// # Panics
1462 ///
1463 /// Panics if:
1464 /// - The resulting date would be out of range.
1465 /// - The local time at the resulting date does not exist or is ambiguous, for example during a
1466 ///   daylight saving time transition.
1467 ///
1468 /// Strongly consider using [`DateTime<Tz>::checked_sub_months`] to get an `Option` instead.
1469 impl<Tz: TimeZone> Sub<Months> for DateTime<Tz> {
1470     type Output = DateTime<Tz>;
1471 
sub(self, rhs: Months) -> Self::Output1472     fn sub(self, rhs: Months) -> Self::Output {
1473         self.checked_sub_months(rhs).expect("`DateTime - Months` out of range")
1474     }
1475 }
1476 
1477 impl<Tz: TimeZone> Sub<DateTime<Tz>> for DateTime<Tz> {
1478     type Output = TimeDelta;
1479 
1480     #[inline]
sub(self, rhs: DateTime<Tz>) -> TimeDelta1481     fn sub(self, rhs: DateTime<Tz>) -> TimeDelta {
1482         self.signed_duration_since(rhs)
1483     }
1484 }
1485 
1486 impl<Tz: TimeZone> Sub<&DateTime<Tz>> for DateTime<Tz> {
1487     type Output = TimeDelta;
1488 
1489     #[inline]
sub(self, rhs: &DateTime<Tz>) -> TimeDelta1490     fn sub(self, rhs: &DateTime<Tz>) -> TimeDelta {
1491         self.signed_duration_since(rhs)
1492     }
1493 }
1494 
1495 /// Add `Days` to `NaiveDateTime`.
1496 ///
1497 /// # Panics
1498 ///
1499 /// Panics if:
1500 /// - The resulting date would be out of range.
1501 /// - The local time at the resulting date does not exist or is ambiguous, for example during a
1502 ///   daylight saving time transition.
1503 ///
1504 /// Strongly consider using `DateTime<Tz>::checked_sub_days` to get an `Option` instead.
1505 impl<Tz: TimeZone> Add<Days> for DateTime<Tz> {
1506     type Output = DateTime<Tz>;
1507 
add(self, days: Days) -> Self::Output1508     fn add(self, days: Days) -> Self::Output {
1509         self.checked_add_days(days).expect("`DateTime + Days` out of range")
1510     }
1511 }
1512 
1513 /// Subtract `Days` from `DateTime`.
1514 ///
1515 /// # Panics
1516 ///
1517 /// Panics if:
1518 /// - The resulting date would be out of range.
1519 /// - The local time at the resulting date does not exist or is ambiguous, for example during a
1520 ///   daylight saving time transition.
1521 ///
1522 /// Strongly consider using `DateTime<Tz>::checked_sub_days` to get an `Option` instead.
1523 impl<Tz: TimeZone> Sub<Days> for DateTime<Tz> {
1524     type Output = DateTime<Tz>;
1525 
sub(self, days: Days) -> Self::Output1526     fn sub(self, days: Days) -> Self::Output {
1527         self.checked_sub_days(days).expect("`DateTime - Days` out of range")
1528     }
1529 }
1530 
1531 impl<Tz: TimeZone> fmt::Debug for DateTime<Tz> {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result1532     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1533         self.overflowing_naive_local().fmt(f)?;
1534         self.offset.fmt(f)
1535     }
1536 }
1537 
1538 // `fmt::Debug` is hand implemented for the `rkyv::Archive` variant of `DateTime` because
1539 // deriving a trait recursively does not propagate trait defined associated types with their own
1540 // constraints:
1541 // In our case `<<Tz as offset::TimeZone>::Offset as Archive>::Archived`
1542 // cannot be formatted using `{:?}` because it doesn't implement `Debug`.
1543 // See below for further discussion:
1544 // * https://github.com/rust-lang/rust/issues/26925
1545 // * https://github.com/rkyv/rkyv/issues/333
1546 // * https://github.com/dtolnay/syn/issues/370
1547 #[cfg(feature = "rkyv-validation")]
1548 impl<Tz: TimeZone> fmt::Debug for ArchivedDateTime<Tz>
1549 where
1550     Tz: Archive,
1551     <Tz as Archive>::Archived: fmt::Debug,
1552     <<Tz as TimeZone>::Offset as Archive>::Archived: fmt::Debug,
1553     <Tz as TimeZone>::Offset: fmt::Debug + Archive,
1554 {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result1555     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1556         f.debug_struct("ArchivedDateTime")
1557             .field("datetime", &self.datetime)
1558             .field("offset", &self.offset)
1559             .finish()
1560     }
1561 }
1562 
1563 impl<Tz: TimeZone> fmt::Display for DateTime<Tz>
1564 where
1565     Tz::Offset: fmt::Display,
1566 {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result1567     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1568         self.overflowing_naive_local().fmt(f)?;
1569         f.write_char(' ')?;
1570         self.offset.fmt(f)
1571     }
1572 }
1573 
1574 /// Accepts a relaxed form of RFC3339.
1575 /// A space or a 'T' are accepted as the separator between the date and time
1576 /// parts.
1577 ///
1578 /// All of these examples are equivalent:
1579 /// ```
1580 /// # use chrono::{DateTime, Utc};
1581 /// "2012-12-12T12:12:12Z".parse::<DateTime<Utc>>()?;
1582 /// "2012-12-12 12:12:12Z".parse::<DateTime<Utc>>()?;
1583 /// "2012-12-12 12:12:12+0000".parse::<DateTime<Utc>>()?;
1584 /// "2012-12-12 12:12:12+00:00".parse::<DateTime<Utc>>()?;
1585 /// # Ok::<(), chrono::ParseError>(())
1586 /// ```
1587 impl str::FromStr for DateTime<Utc> {
1588     type Err = ParseError;
1589 
from_str(s: &str) -> ParseResult<DateTime<Utc>>1590     fn from_str(s: &str) -> ParseResult<DateTime<Utc>> {
1591         s.parse::<DateTime<FixedOffset>>().map(|dt| dt.with_timezone(&Utc))
1592     }
1593 }
1594 
1595 /// Accepts a relaxed form of RFC3339.
1596 /// A space or a 'T' are accepted as the separator between the date and time
1597 /// parts.
1598 ///
1599 /// All of these examples are equivalent:
1600 /// ```
1601 /// # use chrono::{DateTime, Local};
1602 /// "2012-12-12T12:12:12Z".parse::<DateTime<Local>>()?;
1603 /// "2012-12-12 12:12:12Z".parse::<DateTime<Local>>()?;
1604 /// "2012-12-12 12:12:12+0000".parse::<DateTime<Local>>()?;
1605 /// "2012-12-12 12:12:12+00:00".parse::<DateTime<Local>>()?;
1606 /// # Ok::<(), chrono::ParseError>(())
1607 /// ```
1608 #[cfg(feature = "clock")]
1609 impl str::FromStr for DateTime<Local> {
1610     type Err = ParseError;
1611 
from_str(s: &str) -> ParseResult<DateTime<Local>>1612     fn from_str(s: &str) -> ParseResult<DateTime<Local>> {
1613         s.parse::<DateTime<FixedOffset>>().map(|dt| dt.with_timezone(&Local))
1614     }
1615 }
1616 
1617 #[cfg(feature = "std")]
1618 impl From<SystemTime> for DateTime<Utc> {
from(t: SystemTime) -> DateTime<Utc>1619     fn from(t: SystemTime) -> DateTime<Utc> {
1620         let (sec, nsec) = match t.duration_since(UNIX_EPOCH) {
1621             Ok(dur) => (dur.as_secs() as i64, dur.subsec_nanos()),
1622             Err(e) => {
1623                 // unlikely but should be handled
1624                 let dur = e.duration();
1625                 let (sec, nsec) = (dur.as_secs() as i64, dur.subsec_nanos());
1626                 if nsec == 0 {
1627                     (-sec, 0)
1628                 } else {
1629                     (-sec - 1, 1_000_000_000 - nsec)
1630                 }
1631             }
1632         };
1633         Utc.timestamp_opt(sec, nsec).unwrap()
1634     }
1635 }
1636 
1637 #[cfg(feature = "clock")]
1638 impl From<SystemTime> for DateTime<Local> {
from(t: SystemTime) -> DateTime<Local>1639     fn from(t: SystemTime) -> DateTime<Local> {
1640         DateTime::<Utc>::from(t).with_timezone(&Local)
1641     }
1642 }
1643 
1644 #[cfg(feature = "std")]
1645 impl<Tz: TimeZone> From<DateTime<Tz>> for SystemTime {
from(dt: DateTime<Tz>) -> SystemTime1646     fn from(dt: DateTime<Tz>) -> SystemTime {
1647         let sec = dt.timestamp();
1648         let nsec = dt.timestamp_subsec_nanos();
1649         if sec < 0 {
1650             // unlikely but should be handled
1651             UNIX_EPOCH - Duration::new(-sec as u64, 0) + Duration::new(0, nsec)
1652         } else {
1653             UNIX_EPOCH + Duration::new(sec as u64, nsec)
1654         }
1655     }
1656 }
1657 
1658 #[cfg(all(
1659     target_arch = "wasm32",
1660     feature = "wasmbind",
1661     not(any(target_os = "emscripten", target_os = "wasi"))
1662 ))]
1663 impl From<js_sys::Date> for DateTime<Utc> {
from(date: js_sys::Date) -> DateTime<Utc>1664     fn from(date: js_sys::Date) -> DateTime<Utc> {
1665         DateTime::<Utc>::from(&date)
1666     }
1667 }
1668 
1669 #[cfg(all(
1670     target_arch = "wasm32",
1671     feature = "wasmbind",
1672     not(any(target_os = "emscripten", target_os = "wasi"))
1673 ))]
1674 impl From<&js_sys::Date> for DateTime<Utc> {
from(date: &js_sys::Date) -> DateTime<Utc>1675     fn from(date: &js_sys::Date) -> DateTime<Utc> {
1676         Utc.timestamp_millis_opt(date.get_time() as i64).unwrap()
1677     }
1678 }
1679 
1680 #[cfg(all(
1681     target_arch = "wasm32",
1682     feature = "wasmbind",
1683     not(any(target_os = "emscripten", target_os = "wasi"))
1684 ))]
1685 impl From<DateTime<Utc>> for js_sys::Date {
1686     /// Converts a `DateTime<Utc>` to a JS `Date`. The resulting value may be lossy,
1687     /// any values that have a millisecond timestamp value greater/less than ±8,640,000,000,000,000
1688     /// (April 20, 271821 BCE ~ September 13, 275760 CE) will become invalid dates in JS.
from(date: DateTime<Utc>) -> js_sys::Date1689     fn from(date: DateTime<Utc>) -> js_sys::Date {
1690         let js_millis = wasm_bindgen::JsValue::from_f64(date.timestamp_millis() as f64);
1691         js_sys::Date::new(&js_millis)
1692     }
1693 }
1694 
1695 // Note that implementation of Arbitrary cannot be simply derived for DateTime<Tz>, due to
1696 // the nontrivial bound <Tz as TimeZone>::Offset: Arbitrary.
1697 #[cfg(all(feature = "arbitrary", feature = "std"))]
1698 impl<'a, Tz> arbitrary::Arbitrary<'a> for DateTime<Tz>
1699 where
1700     Tz: TimeZone,
1701     <Tz as TimeZone>::Offset: arbitrary::Arbitrary<'a>,
1702 {
arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<DateTime<Tz>>1703     fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<DateTime<Tz>> {
1704         let datetime = NaiveDateTime::arbitrary(u)?;
1705         let offset = <Tz as TimeZone>::Offset::arbitrary(u)?;
1706         Ok(DateTime::from_naive_utc_and_offset(datetime, offset))
1707     }
1708 }
1709 
1710 #[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))]
test_encodable_json<FUtc, FFixed, E>(to_string_utc: FUtc, to_string_fixed: FFixed) where FUtc: Fn(&DateTime<Utc>) -> Result<String, E>, FFixed: Fn(&DateTime<FixedOffset>) -> Result<String, E>, E: ::core::fmt::Debug,1711 fn test_encodable_json<FUtc, FFixed, E>(to_string_utc: FUtc, to_string_fixed: FFixed)
1712 where
1713     FUtc: Fn(&DateTime<Utc>) -> Result<String, E>,
1714     FFixed: Fn(&DateTime<FixedOffset>) -> Result<String, E>,
1715     E: ::core::fmt::Debug,
1716 {
1717     assert_eq!(
1718         to_string_utc(&Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()).ok(),
1719         Some(r#""2014-07-24T12:34:06Z""#.into())
1720     );
1721 
1722     assert_eq!(
1723         to_string_fixed(
1724             &FixedOffset::east_opt(3660).unwrap().with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()
1725         )
1726         .ok(),
1727         Some(r#""2014-07-24T12:34:06+01:01""#.into())
1728     );
1729     assert_eq!(
1730         to_string_fixed(
1731             &FixedOffset::east_opt(3650).unwrap().with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()
1732         )
1733         .ok(),
1734         // An offset with seconds is not allowed by RFC 3339, so we round it to the nearest minute.
1735         // In this case `+01:00:50` becomes `+01:01`
1736         Some(r#""2014-07-24T12:34:06+01:01""#.into())
1737     );
1738 }
1739 
1740 #[cfg(all(test, feature = "clock", any(feature = "rustc-serialize", feature = "serde")))]
test_decodable_json<FUtc, FFixed, FLocal, E>( utc_from_str: FUtc, fixed_from_str: FFixed, local_from_str: FLocal, ) where FUtc: Fn(&str) -> Result<DateTime<Utc>, E>, FFixed: Fn(&str) -> Result<DateTime<FixedOffset>, E>, FLocal: Fn(&str) -> Result<DateTime<Local>, E>, E: ::core::fmt::Debug,1741 fn test_decodable_json<FUtc, FFixed, FLocal, E>(
1742     utc_from_str: FUtc,
1743     fixed_from_str: FFixed,
1744     local_from_str: FLocal,
1745 ) where
1746     FUtc: Fn(&str) -> Result<DateTime<Utc>, E>,
1747     FFixed: Fn(&str) -> Result<DateTime<FixedOffset>, E>,
1748     FLocal: Fn(&str) -> Result<DateTime<Local>, E>,
1749     E: ::core::fmt::Debug,
1750 {
1751     // should check against the offset as well (the normal DateTime comparison will ignore them)
1752     fn norm<Tz: TimeZone>(dt: &Option<DateTime<Tz>>) -> Option<(&DateTime<Tz>, &Tz::Offset)> {
1753         dt.as_ref().map(|dt| (dt, dt.offset()))
1754     }
1755 
1756     assert_eq!(
1757         norm(&utc_from_str(r#""2014-07-24T12:34:06Z""#).ok()),
1758         norm(&Some(Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()))
1759     );
1760     assert_eq!(
1761         norm(&utc_from_str(r#""2014-07-24T13:57:06+01:23""#).ok()),
1762         norm(&Some(Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()))
1763     );
1764 
1765     assert_eq!(
1766         norm(&fixed_from_str(r#""2014-07-24T12:34:06Z""#).ok()),
1767         norm(&Some(
1768             FixedOffset::east_opt(0).unwrap().with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()
1769         ))
1770     );
1771     assert_eq!(
1772         norm(&fixed_from_str(r#""2014-07-24T13:57:06+01:23""#).ok()),
1773         norm(&Some(
1774             FixedOffset::east_opt(60 * 60 + 23 * 60)
1775                 .unwrap()
1776                 .with_ymd_and_hms(2014, 7, 24, 13, 57, 6)
1777                 .unwrap()
1778         ))
1779     );
1780 
1781     // we don't know the exact local offset but we can check that
1782     // the conversion didn't change the instant itself
1783     assert_eq!(
1784         local_from_str(r#""2014-07-24T12:34:06Z""#).expect("local should parse"),
1785         Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()
1786     );
1787     assert_eq!(
1788         local_from_str(r#""2014-07-24T13:57:06+01:23""#).expect("local should parse with offset"),
1789         Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()
1790     );
1791 
1792     assert!(utc_from_str(r#""2014-07-32T12:34:06Z""#).is_err());
1793     assert!(fixed_from_str(r#""2014-07-32T12:34:06Z""#).is_err());
1794 }
1795 
1796 #[cfg(all(test, feature = "clock", feature = "rustc-serialize"))]
test_decodable_json_timestamps<FUtc, FFixed, FLocal, E>( utc_from_str: FUtc, fixed_from_str: FFixed, local_from_str: FLocal, ) where FUtc: Fn(&str) -> Result<rustc_serialize::TsSeconds<Utc>, E>, FFixed: Fn(&str) -> Result<rustc_serialize::TsSeconds<FixedOffset>, E>, FLocal: Fn(&str) -> Result<rustc_serialize::TsSeconds<Local>, E>, E: ::core::fmt::Debug,1797 fn test_decodable_json_timestamps<FUtc, FFixed, FLocal, E>(
1798     utc_from_str: FUtc,
1799     fixed_from_str: FFixed,
1800     local_from_str: FLocal,
1801 ) where
1802     FUtc: Fn(&str) -> Result<rustc_serialize::TsSeconds<Utc>, E>,
1803     FFixed: Fn(&str) -> Result<rustc_serialize::TsSeconds<FixedOffset>, E>,
1804     FLocal: Fn(&str) -> Result<rustc_serialize::TsSeconds<Local>, E>,
1805     E: ::core::fmt::Debug,
1806 {
1807     fn norm<Tz: TimeZone>(dt: &Option<DateTime<Tz>>) -> Option<(&DateTime<Tz>, &Tz::Offset)> {
1808         dt.as_ref().map(|dt| (dt, dt.offset()))
1809     }
1810 
1811     assert_eq!(
1812         norm(&utc_from_str("0").ok().map(DateTime::from)),
1813         norm(&Some(Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap()))
1814     );
1815     assert_eq!(
1816         norm(&utc_from_str("-1").ok().map(DateTime::from)),
1817         norm(&Some(Utc.with_ymd_and_hms(1969, 12, 31, 23, 59, 59).unwrap()))
1818     );
1819 
1820     assert_eq!(
1821         norm(&fixed_from_str("0").ok().map(DateTime::from)),
1822         norm(&Some(
1823             FixedOffset::east_opt(0).unwrap().with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap()
1824         ))
1825     );
1826     assert_eq!(
1827         norm(&fixed_from_str("-1").ok().map(DateTime::from)),
1828         norm(&Some(
1829             FixedOffset::east_opt(0).unwrap().with_ymd_and_hms(1969, 12, 31, 23, 59, 59).unwrap()
1830         ))
1831     );
1832 
1833     assert_eq!(
1834         *fixed_from_str("0").expect("0 timestamp should parse"),
1835         Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap()
1836     );
1837     assert_eq!(
1838         *local_from_str("-1").expect("-1 timestamp should parse"),
1839         Utc.with_ymd_and_hms(1969, 12, 31, 23, 59, 59).unwrap()
1840     );
1841 }
1842