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