Coverage Report

Created: 2026-03-31 07:09

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