1// This is a part of Chrono.
2// See README.md and LICENSE.txt for details.
34//! The time zone, which calculates offsets from the local time to UTC.
5//!
6//! There are four operations provided by the `TimeZone` trait:
7//!
8//! 1. Converting the local `NaiveDateTime` to `DateTime<Tz>`
9//! 2. Converting the UTC `NaiveDateTime` to `DateTime<Tz>`
10//! 3. Converting `DateTime<Tz>` to the local `NaiveDateTime`
11//! 4. Constructing `DateTime<Tz>` objects from various offsets
12//!
13//! 1 is used for constructors. 2 is used for the `with_timezone` method of date and time types.
14//! 3 is used for other methods, e.g. `year()` or `format()`, and provided by an associated type
15//! which implements `Offset` (which then passed to `TimeZone` for actual implementations).
16//! Technically speaking `TimeZone` has a total knowledge about given timescale,
17//! but `Offset` is used as a cache to avoid the repeated conversion
18//! and provides implementations for 1 and 3.
19//! An `TimeZone` instance can be reconstructed from the corresponding `Offset` instance.
2021use core::fmt;
2223use crate::Weekday;
24use crate::format::{ParseResult, Parsed, StrftimeItems, parse};
25use crate::naive::{NaiveDate, NaiveDateTime, NaiveTime};
26#[allow(deprecated)]
27use crate::{Date, DateTime};
2829pub(crate) mod fixed;
30pub use self::fixed::FixedOffset;
3132#[cfg(feature = "clock")]
33pub(crate) mod local;
34#[cfg(feature = "clock")]
35pub use self::local::Local;
3637pub(crate) mod utc;
38pub use self::utc::Utc;
3940/// The result of mapping a local time to a concrete instant in a given time zone.
41///
42/// The calculation to go from a local time (wall clock time) to an instant in UTC can end up in
43/// three cases:
44/// * A single, simple result.
45/// * An ambiguous result when the clock is turned backwards during a transition due to for example
46/// DST.
47/// * No result when the clock is turned forwards during a transition due to for example DST.
48///
49/// <div class="warning">
50///
51/// In wasm, when using [`Local`], only the [`LocalResult::Single`] variant is returned.
52/// Specifically:
53///
54/// * When the clock is turned backwards, where `Ambiguous(earliest, latest)` would be expected,
55/// `Single(earliest)` is returned instead.
56/// * When the clock is turned forwards, where `None` would be expected, `Single(t)` is returned,
57/// with `t` being the requested local time represented as though there is no transition on that
58/// day (i.e. still "summer time")
59///
60/// This is caused because of limitations in the JavaScript
61/// [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)
62/// API, which always parses a local time as a single, valid time - even for an
63/// input which describes a nonexistent or ambiguous time.
64///
65/// See further discussion and workarounds in <https://github.com/chronotope/chrono/issues/1701>.
66///
67/// </div>
68///
69/// When the clock is turned backwards it creates a _fold_ in local time, during which the local
70/// time is _ambiguous_. When the clock is turned forwards it creates a _gap_ in local time, during
71/// which the local time is _missing_, or does not exist.
72///
73/// Chrono does not return a default choice or invalid data during time zone transitions, but has
74/// the `MappedLocalTime` type to help deal with the result correctly.
75///
76/// The type of `T` is usually a [`DateTime`] but may also be only an offset.
77pub type MappedLocalTime<T> = LocalResult<T>;
78#[derive(#[automatically_derived]
impl<T: ::core::clone::Clone> ::core::clone::Clone for LocalResult<T> {
#[inline]
fn clone(&self) -> LocalResult<T> {
match self {
LocalResult::Single(__self_0) =>
LocalResult::Single(::core::clone::Clone::clone(__self_0)),
LocalResult::Ambiguous(__self_0, __self_1) =>
LocalResult::Ambiguous(::core::clone::Clone::clone(__self_0),
::core::clone::Clone::clone(__self_1)),
LocalResult::None => LocalResult::None,
}
}
}Clone, #[automatically_derived]
impl<T: ::core::cmp::PartialEq> ::core::cmp::PartialEq for LocalResult<T> {
#[inline]
fn eq(&self, other: &LocalResult<T>) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(LocalResult::Single(__self_0), LocalResult::Single(__arg1_0))
=> __self_0 == __arg1_0,
(LocalResult::Ambiguous(__self_0, __self_1),
LocalResult::Ambiguous(__arg1_0, __arg1_1)) =>
__self_0 == __arg1_0 && __self_1 == __arg1_1,
_ => true,
}
}
}PartialEq, #[automatically_derived]
impl<T: ::core::fmt::Debug> ::core::fmt::Debug for LocalResult<T> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
LocalResult::Single(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Single",
&__self_0),
LocalResult::Ambiguous(__self_0, __self_1) =>
::core::fmt::Formatter::debug_tuple_field2_finish(f,
"Ambiguous", __self_0, &__self_1),
LocalResult::None => ::core::fmt::Formatter::write_str(f, "None"),
}
}
}Debug, #[automatically_derived]
impl<T: ::core::marker::Copy> ::core::marker::Copy for LocalResult<T> { }Copy, #[automatically_derived]
impl<T: ::core::cmp::Eq> ::core::cmp::Eq for LocalResult<T> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) -> () {
let _: ::core::cmp::AssertParamIsEq<T>;
}
}Eq, #[automatically_derived]
impl<T: ::core::hash::Hash> ::core::hash::Hash for LocalResult<T> {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
let __self_discr = ::core::intrinsics::discriminant_value(self);
::core::hash::Hash::hash(&__self_discr, state);
match self {
LocalResult::Single(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
LocalResult::Ambiguous(__self_0, __self_1) => {
::core::hash::Hash::hash(__self_0, state);
::core::hash::Hash::hash(__self_1, state)
}
_ => {}
}
}
}Hash)]
7980/// Old name of [`MappedLocalTime`]. See that type for more documentation.
81pub enum LocalResult<T> {
82/// The local time maps to a single unique result.
83Single(T),
8485/// The local time is _ambiguous_ because there is a _fold_ in the local time.
86 ///
87 /// This variant contains the two possible results, in the order `(earliest, latest)`.
88Ambiguous(T, T),
8990/// The local time does not exist because there is a _gap_ in the local time.
91 ///
92 /// This variant may also be returned if there was an error while resolving the local time,
93 /// caused by for example missing time zone data files, an error in an OS API, or overflow.
94None,
95}
9697impl<T> MappedLocalTime<T> {
98/// Returns `Some` if the time zone mapping has a single result.
99 ///
100 /// # Errors
101 ///
102 /// Returns `None` if local time falls in a _fold_ or _gap_ in the local time, or if there was
103 /// an error.
104#[must_use]
105pub fn single(self) -> Option<T> {
106match self {
107MappedLocalTime::Single(t) => Some(t),
108_ => None,
109 }
110 }
111112/// Returns the earliest possible result of the time zone mapping.
113 ///
114 /// # Errors
115 ///
116 /// Returns `None` if local time falls in a _gap_ in the local time, or if there was an error.
117#[must_use]
118pub fn earliest(self) -> Option<T> {
119match self {
120MappedLocalTime::Single(t) | MappedLocalTime::Ambiguous(t, _) => Some(t),
121_ => None,
122 }
123 }
124125/// Returns the latest possible result of the time zone mapping.
126 ///
127 /// # Errors
128 ///
129 /// Returns `None` if local time falls in a _gap_ in the local time, or if there was an error.
130#[must_use]
131pub fn latest(self) -> Option<T> {
132match self {
133MappedLocalTime::Single(t) | MappedLocalTime::Ambiguous(_, t) => Some(t),
134_ => None,
135 }
136 }
137138/// Maps a `MappedLocalTime<T>` into `MappedLocalTime<U>` with given function.
139#[must_use]
140pub fn map<U, F: FnMut(T) -> U>(self, mut f: F) -> MappedLocalTime<U> {
141match self {
142MappedLocalTime::None => MappedLocalTime::None,
143MappedLocalTime::Single(v) => MappedLocalTime::Single(f(v)),
144MappedLocalTime::Ambiguous(min, max) => MappedLocalTime::Ambiguous(f(min), f(max)),
145 }
146 }
147148/// Maps a `MappedLocalTime<T>` into `MappedLocalTime<U>` with given function.
149 ///
150 /// Returns `MappedLocalTime::None` if the function returns `None`.
151#[must_use]
152pub(crate) fn and_then<U, F: FnMut(T) -> Option<U>>(self, mut f: F) -> MappedLocalTime<U> {
153match self {
154MappedLocalTime::None => MappedLocalTime::None,
155MappedLocalTime::Single(v) => match f(v) {
156Some(new) => MappedLocalTime::Single(new),
157None => MappedLocalTime::None,
158 },
159MappedLocalTime::Ambiguous(min, max) => match (f(min), f(max)) {
160 (Some(min), Some(max)) => MappedLocalTime::Ambiguous(min, max),
161_ => MappedLocalTime::None,
162 },
163 }
164 }
165}
166167#[allow(deprecated)]
168impl<Tz: TimeZone> MappedLocalTime<Date<Tz>> {
169/// Makes a new `DateTime` from the current date and given `NaiveTime`.
170 /// The offset in the current date is preserved.
171 ///
172 /// Propagates any error. Ambiguous result would be discarded.
173#[inline]
174 #[must_use]
175pub fn and_time(self, time: NaiveTime) -> MappedLocalTime<DateTime<Tz>> {
176match self {
177MappedLocalTime::Single(d) => {
178d.and_time(time).map_or(MappedLocalTime::None, MappedLocalTime::Single)
179 }
180_ => MappedLocalTime::None,
181 }
182 }
183184/// Makes a new `DateTime` from the current date, hour, minute and second.
185 /// The offset in the current date is preserved.
186 ///
187 /// Propagates any error. Ambiguous result would be discarded.
188#[inline]
189 #[must_use]
190pub fn and_hms_opt(self, hour: u32, min: u32, sec: u32) -> MappedLocalTime<DateTime<Tz>> {
191match self {
192MappedLocalTime::Single(d) => {
193d.and_hms_opt(hour, min, sec).map_or(MappedLocalTime::None, MappedLocalTime::Single)
194 }
195_ => MappedLocalTime::None,
196 }
197 }
198199/// Makes a new `DateTime` from the current date, hour, minute, second and millisecond.
200 /// The millisecond part can exceed 1,000 in order to represent the leap second.
201 /// The offset in the current date is preserved.
202 ///
203 /// Propagates any error. Ambiguous result would be discarded.
204#[inline]
205 #[must_use]
206pub fn and_hms_milli_opt(
207self,
208 hour: u32,
209 min: u32,
210 sec: u32,
211 milli: u32,
212 ) -> MappedLocalTime<DateTime<Tz>> {
213match self {
214MappedLocalTime::Single(d) => d215 .and_hms_milli_opt(hour, min, sec, milli)
216 .map_or(MappedLocalTime::None, MappedLocalTime::Single),
217_ => MappedLocalTime::None,
218 }
219 }
220221/// Makes a new `DateTime` from the current date, hour, minute, second and microsecond.
222 /// The microsecond part can exceed 1,000,000 in order to represent the leap second.
223 /// The offset in the current date is preserved.
224 ///
225 /// Propagates any error. Ambiguous result would be discarded.
226#[inline]
227 #[must_use]
228pub fn and_hms_micro_opt(
229self,
230 hour: u32,
231 min: u32,
232 sec: u32,
233 micro: u32,
234 ) -> MappedLocalTime<DateTime<Tz>> {
235match self {
236MappedLocalTime::Single(d) => d237 .and_hms_micro_opt(hour, min, sec, micro)
238 .map_or(MappedLocalTime::None, MappedLocalTime::Single),
239_ => MappedLocalTime::None,
240 }
241 }
242243/// Makes a new `DateTime` from the current date, hour, minute, second and nanosecond.
244 /// The nanosecond part can exceed 1,000,000,000 in order to represent the leap second.
245 /// The offset in the current date is preserved.
246 ///
247 /// Propagates any error. Ambiguous result would be discarded.
248#[inline]
249 #[must_use]
250pub fn and_hms_nano_opt(
251self,
252 hour: u32,
253 min: u32,
254 sec: u32,
255 nano: u32,
256 ) -> MappedLocalTime<DateTime<Tz>> {
257match self {
258MappedLocalTime::Single(d) => d259 .and_hms_nano_opt(hour, min, sec, nano)
260 .map_or(MappedLocalTime::None, MappedLocalTime::Single),
261_ => MappedLocalTime::None,
262 }
263 }
264}
265266impl<T: fmt::Debug> MappedLocalTime<T> {
267/// Returns a single unique conversion result or panics.
268 ///
269 /// `unwrap()` is best combined with time zone types where the mapping can never fail like
270 /// [`Utc`] and [`FixedOffset`]. Note that for [`FixedOffset`] there is a rare case where a
271 /// resulting [`DateTime`] can be out of range.
272 ///
273 /// # Panics
274 ///
275 /// Panics if the local time falls within a _fold_ or a _gap_ in the local time, and on any
276 /// error that may have been returned by the type implementing [`TimeZone`].
277#[must_use]
278 #[track_caller]
279pub fn unwrap(self) -> T {
280match self {
281MappedLocalTime::None => { ::core::panicking::panic_fmt(format_args!("No such local time")); }panic!("No such local time"),
282MappedLocalTime::Single(t) => t,
283MappedLocalTime::Ambiguous(t1, t2) => {
284{
::core::panicking::panic_fmt(format_args!("Ambiguous local time, ranging from {0:?} to {1:?}",
t1, t2));
}panic!("Ambiguous local time, ranging from {t1:?} to {t2:?}")285 }
286 }
287 }
288}
289290/// The offset from the local time to UTC.
291pub trait Offset: Sized + Clone + fmt::Debug {
292/// Returns the fixed offset from UTC to the local time stored.
293fn fix(&self) -> FixedOffset;
294}
295296/// The time zone.
297///
298/// The methods here are the primary constructors for the [`DateTime`] type.
299pub trait TimeZone: Sized + Clone {
300/// An associated offset type.
301 /// This type is used to store the actual offset in date and time types.
302 /// The original `TimeZone` value can be recovered via `TimeZone::from_offset`.
303type Offset: Offset;
304305/// Make a new `DateTime` from year, month, day, time components and current time zone.
306 ///
307 /// This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.
308 ///
309 /// Returns `MappedLocalTime::None` on invalid input data.
310fn with_ymd_and_hms(
311&self,
312 year: i32,
313 month: u32,
314 day: u32,
315 hour: u32,
316 min: u32,
317 sec: u32,
318 ) -> MappedLocalTime<DateTime<Self>> {
319match NaiveDate::from_ymd_opt(year, month, day).and_then(|d| d.and_hms_opt(hour, min, sec))
320 {
321Some(dt) => self.from_local_datetime(&dt),
322None => MappedLocalTime::None,
323 }
324 }
325326/// Makes a new `Date` from year, month, day and the current time zone.
327 /// This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.
328 ///
329 /// The time zone normally does not affect the date (unless it is between UTC-24 and UTC+24),
330 /// but it will propagate to the `DateTime` values constructed via this date.
331 ///
332 /// Panics on the out-of-range date, invalid month and/or day.
333#[deprecated(since = "0.4.23", note = "use `with_ymd_and_hms()` instead")]
334 #[allow(deprecated)]
335fn ymd(&self, year: i32, month: u32, day: u32) -> Date<Self> {
336self.ymd_opt(year, month, day).unwrap()
337 }
338339/// Makes a new `Date` from year, month, day and the current time zone.
340 /// This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.
341 ///
342 /// The time zone normally does not affect the date (unless it is between UTC-24 and UTC+24),
343 /// but it will propagate to the `DateTime` values constructed via this date.
344 ///
345 /// Returns `None` on the out-of-range date, invalid month and/or day.
346#[deprecated(since = "0.4.23", note = "use `with_ymd_and_hms()` instead")]
347 #[allow(deprecated)]
348fn ymd_opt(&self, year: i32, month: u32, day: u32) -> MappedLocalTime<Date<Self>> {
349match NaiveDate::from_ymd_opt(year, month, day) {
350Some(d) => self.from_local_date(&d),
351None => MappedLocalTime::None,
352 }
353 }
354355/// Makes a new `Date` from year, day of year (DOY or "ordinal") and the current time zone.
356 /// This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.
357 ///
358 /// The time zone normally does not affect the date (unless it is between UTC-24 and UTC+24),
359 /// but it will propagate to the `DateTime` values constructed via this date.
360 ///
361 /// Panics on the out-of-range date and/or invalid DOY.
362#[deprecated(
363 since = "0.4.23",
364 note = "use `from_local_datetime()` with a `NaiveDateTime` instead"
365)]
366 #[allow(deprecated)]
367fn yo(&self, year: i32, ordinal: u32) -> Date<Self> {
368self.yo_opt(year, ordinal).unwrap()
369 }
370371/// Makes a new `Date` from year, day of year (DOY or "ordinal") and the current time zone.
372 /// This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.
373 ///
374 /// The time zone normally does not affect the date (unless it is between UTC-24 and UTC+24),
375 /// but it will propagate to the `DateTime` values constructed via this date.
376 ///
377 /// Returns `None` on the out-of-range date and/or invalid DOY.
378#[deprecated(
379 since = "0.4.23",
380 note = "use `from_local_datetime()` with a `NaiveDateTime` instead"
381)]
382 #[allow(deprecated)]
383fn yo_opt(&self, year: i32, ordinal: u32) -> MappedLocalTime<Date<Self>> {
384match NaiveDate::from_yo_opt(year, ordinal) {
385Some(d) => self.from_local_date(&d),
386None => MappedLocalTime::None,
387 }
388 }
389390/// Makes a new `Date` from ISO week date (year and week number), day of the week (DOW) and
391 /// the current time zone.
392 /// This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.
393 /// The resulting `Date` may have a different year from the input year.
394 ///
395 /// The time zone normally does not affect the date (unless it is between UTC-24 and UTC+24),
396 /// but it will propagate to the `DateTime` values constructed via this date.
397 ///
398 /// Panics on the out-of-range date and/or invalid week number.
399#[deprecated(
400 since = "0.4.23",
401 note = "use `from_local_datetime()` with a `NaiveDateTime` instead"
402)]
403 #[allow(deprecated)]
404fn isoywd(&self, year: i32, week: u32, weekday: Weekday) -> Date<Self> {
405self.isoywd_opt(year, week, weekday).unwrap()
406 }
407408/// Makes a new `Date` from ISO week date (year and week number), day of the week (DOW) and
409 /// the current time zone.
410 /// This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.
411 /// The resulting `Date` may have a different year from the input year.
412 ///
413 /// The time zone normally does not affect the date (unless it is between UTC-24 and UTC+24),
414 /// but it will propagate to the `DateTime` values constructed via this date.
415 ///
416 /// Returns `None` on the out-of-range date and/or invalid week number.
417#[deprecated(
418 since = "0.4.23",
419 note = "use `from_local_datetime()` with a `NaiveDateTime` instead"
420)]
421 #[allow(deprecated)]
422fn isoywd_opt(&self, year: i32, week: u32, weekday: Weekday) -> MappedLocalTime<Date<Self>> {
423match NaiveDate::from_isoywd_opt(year, week, weekday) {
424Some(d) => self.from_local_date(&d),
425None => MappedLocalTime::None,
426 }
427 }
428429/// Makes a new `DateTime` from the number of non-leap seconds
430 /// since January 1, 1970 0:00:00 UTC (aka "UNIX timestamp")
431 /// and the number of nanoseconds since the last whole non-leap second.
432 ///
433 /// The nanosecond part can exceed 1,000,000,000 in order to represent a
434 /// [leap second](crate::NaiveTime#leap-second-handling), but only when `secs % 60 == 59`.
435 /// (The true "UNIX timestamp" cannot represent a leap second unambiguously.)
436 ///
437 /// # Panics
438 ///
439 /// Panics on the out-of-range number of seconds and/or invalid nanosecond,
440 /// for a non-panicking version see [`timestamp_opt`](#method.timestamp_opt).
441#[deprecated(since = "0.4.23", note = "use `timestamp_opt()` instead")]
442fn timestamp(&self, secs: i64, nsecs: u32) -> DateTime<Self> {
443self.timestamp_opt(secs, nsecs).unwrap()
444 }
445446/// Makes a new `DateTime` from the number of non-leap seconds
447 /// since January 1, 1970 0:00:00 UTC (aka "UNIX timestamp")
448 /// and the number of nanoseconds since the last whole non-leap second.
449 ///
450 /// The nanosecond part can exceed 1,000,000,000 in order to represent a
451 /// [leap second](crate::NaiveTime#leap-second-handling), but only when `secs % 60 == 59`.
452 /// (The true "UNIX timestamp" cannot represent a leap second unambiguously.)
453 ///
454 /// # Errors
455 ///
456 /// Returns `MappedLocalTime::None` on out-of-range number of seconds and/or
457 /// invalid nanosecond, otherwise always returns `MappedLocalTime::Single`.
458 ///
459 /// # Example
460 ///
461 /// ```
462 /// use chrono::{TimeZone, Utc};
463 ///
464 /// assert_eq!(Utc.timestamp_opt(1431648000, 0).unwrap().to_string(), "2015-05-15 00:00:00 UTC");
465 /// ```
466fn timestamp_opt(&self, secs: i64, nsecs: u32) -> MappedLocalTime<DateTime<Self>> {
467match DateTime::from_timestamp(secs, nsecs) {
468Some(dt) => MappedLocalTime::Single(self.from_utc_datetime(&dt.naive_utc())),
469None => MappedLocalTime::None,
470 }
471 }
472473/// Makes a new `DateTime` from the number of non-leap milliseconds
474 /// since January 1, 1970 0:00:00 UTC (aka "UNIX timestamp").
475 ///
476 /// Panics on out-of-range number of milliseconds for a non-panicking
477 /// version see [`timestamp_millis_opt`](#method.timestamp_millis_opt).
478#[deprecated(since = "0.4.23", note = "use `timestamp_millis_opt()` instead")]
479fn timestamp_millis(&self, millis: i64) -> DateTime<Self> {
480self.timestamp_millis_opt(millis).unwrap()
481 }
482483/// Makes a new `DateTime` from the number of non-leap milliseconds
484 /// since January 1, 1970 0:00:00 UTC (aka "UNIX timestamp").
485 ///
486 ///
487 /// Returns `MappedLocalTime::None` on out-of-range number of milliseconds
488 /// and/or invalid nanosecond, otherwise always returns
489 /// `MappedLocalTime::Single`.
490 ///
491 /// # Example
492 ///
493 /// ```
494 /// use chrono::{MappedLocalTime, TimeZone, Utc};
495 /// match Utc.timestamp_millis_opt(1431648000) {
496 /// MappedLocalTime::Single(dt) => assert_eq!(dt.timestamp(), 1431648),
497 /// _ => panic!("Incorrect timestamp_millis"),
498 /// };
499 /// ```
500fn timestamp_millis_opt(&self, millis: i64) -> MappedLocalTime<DateTime<Self>> {
501match DateTime::from_timestamp_millis(millis) {
502Some(dt) => MappedLocalTime::Single(self.from_utc_datetime(&dt.naive_utc())),
503None => MappedLocalTime::None,
504 }
505 }
506507/// Makes a new `DateTime` from the number of non-leap nanoseconds
508 /// since January 1, 1970 0:00:00 UTC (aka "UNIX timestamp").
509 ///
510 /// Unlike [`timestamp_millis_opt`](#method.timestamp_millis_opt), this never fails.
511 ///
512 /// # Example
513 ///
514 /// ```
515 /// use chrono::{TimeZone, Utc};
516 ///
517 /// assert_eq!(Utc.timestamp_nanos(1431648000000000).timestamp(), 1431648);
518 /// ```
519fn timestamp_nanos(&self, nanos: i64) -> DateTime<Self> {
520self.from_utc_datetime(&DateTime::from_timestamp_nanos(nanos).naive_utc())
521 }
522523/// Makes a new `DateTime` from the number of non-leap microseconds
524 /// since January 1, 1970 0:00:00 UTC (aka "UNIX timestamp").
525 ///
526 /// # Example
527 ///
528 /// ```
529 /// use chrono::{TimeZone, Utc};
530 ///
531 /// assert_eq!(Utc.timestamp_micros(1431648000000).unwrap().timestamp(), 1431648);
532 /// ```
533fn timestamp_micros(&self, micros: i64) -> MappedLocalTime<DateTime<Self>> {
534match DateTime::from_timestamp_micros(micros) {
535Some(dt) => MappedLocalTime::Single(self.from_utc_datetime(&dt.naive_utc())),
536None => MappedLocalTime::None,
537 }
538 }
539540/// Parses a string with the specified format string and returns a
541 /// `DateTime` with the current offset.
542 ///
543 /// See the [`crate::format::strftime`] module on the
544 /// supported escape sequences.
545 ///
546 /// If the to-be-parsed string includes an offset, it *must* match the
547 /// offset of the TimeZone, otherwise an error will be returned.
548 ///
549 /// See also [`DateTime::parse_from_str`] which gives a [`DateTime`] with
550 /// parsed [`FixedOffset`].
551 ///
552 /// See also [`NaiveDateTime::parse_from_str`] which gives a [`NaiveDateTime`] without
553 /// an offset, but can be converted to a [`DateTime`] with [`NaiveDateTime::and_utc`] or
554 /// [`NaiveDateTime::and_local_timezone`].
555#[deprecated(
556 since = "0.4.29",
557 note = "use `DateTime::parse_from_str` or `NaiveDateTime::parse_from_str` with `and_utc()` or `and_local_timezone()` instead"
558)]
559fn datetime_from_str(&self, s: &str, fmt: &str) -> ParseResult<DateTime<Self>> {
560let mut parsed = Parsed::new();
561parse(&mut parsed, s, StrftimeItems::new(fmt))?;
562parsed.to_datetime_with_timezone(self)
563 }
564565/// Reconstructs the time zone from the offset.
566fn from_offset(offset: &Self::Offset) -> Self;
567568/// Creates the offset(s) for given local `NaiveDate` if possible.
569fn offset_from_local_date(&self, local: &NaiveDate) -> MappedLocalTime<Self::Offset>;
570571/// Creates the offset(s) for given local `NaiveDateTime` if possible.
572fn offset_from_local_datetime(&self, local: &NaiveDateTime) -> MappedLocalTime<Self::Offset>;
573574/// Converts the local `NaiveDate` to the timezone-aware `Date` if possible.
575#[allow(clippy::wrong_self_convention)]
576 #[deprecated(since = "0.4.23", note = "use `from_local_datetime()` instead")]
577 #[allow(deprecated)]
578fn from_local_date(&self, local: &NaiveDate) -> MappedLocalTime<Date<Self>> {
579self.offset_from_local_date(local).map(|offset| {
580// since FixedOffset is within +/- 1 day, the date is never affected
581Date::from_utc(*local, offset)
582 })
583 }
584585/// Converts the local `NaiveDateTime` to the timezone-aware `DateTime` if possible.
586#[allow(clippy::wrong_self_convention)]
587fn from_local_datetime(&self, local: &NaiveDateTime) -> MappedLocalTime<DateTime<Self>> {
588self.offset_from_local_datetime(local).and_then(|off| {
589local590 .checked_sub_offset(off.fix())
591 .map(|dt| DateTime::from_naive_utc_and_offset(dt, off))
592 })
593 }
594595/// Creates the offset for given UTC `NaiveDate`. This cannot fail.
596fn offset_from_utc_date(&self, utc: &NaiveDate) -> Self::Offset;
597598/// Creates the offset for given UTC `NaiveDateTime`. This cannot fail.
599fn offset_from_utc_datetime(&self, utc: &NaiveDateTime) -> Self::Offset;
600601/// Converts the UTC `NaiveDate` to the local time.
602 /// The UTC is continuous and thus this cannot fail (but can give the duplicate local time).
603#[allow(clippy::wrong_self_convention)]
604 #[deprecated(since = "0.4.23", note = "use `from_utc_datetime()` instead")]
605 #[allow(deprecated)]
606fn from_utc_date(&self, utc: &NaiveDate) -> Date<Self> {
607Date::from_utc(*utc, self.offset_from_utc_date(utc))
608 }
609610/// Converts the UTC `NaiveDateTime` to the local time.
611 /// The UTC is continuous and thus this cannot fail (but can give the duplicate local time).
612#[allow(clippy::wrong_self_convention)]
613fn from_utc_datetime(&self, utc: &NaiveDateTime) -> DateTime<Self> {
614DateTime::from_naive_utc_and_offset(*utc, self.offset_from_utc_datetime(utc))
615 }
616}
617618#[cfg(test)]
619mod tests {
620use super::*;
621622#[test]
623fn test_fixed_offset_min_max_dates() {
624for offset_hour in -23..=23 {
625dbg!(offset_hour);
626let offset = FixedOffset::east_opt(offset_hour * 60 * 60).unwrap();
627628let local_max = offset.from_utc_datetime(&NaiveDateTime::MAX);
629assert_eq!(local_max.naive_utc(), NaiveDateTime::MAX);
630let local_min = offset.from_utc_datetime(&NaiveDateTime::MIN);
631assert_eq!(local_min.naive_utc(), NaiveDateTime::MIN);
632633let local_max = offset.from_local_datetime(&NaiveDateTime::MAX);
634if offset_hour >= 0 {
635assert_eq!(local_max.unwrap().naive_local(), NaiveDateTime::MAX);
636 } else {
637assert_eq!(local_max, MappedLocalTime::None);
638 }
639let local_min = offset.from_local_datetime(&NaiveDateTime::MIN);
640if offset_hour <= 0 {
641assert_eq!(local_min.unwrap().naive_local(), NaiveDateTime::MIN);
642 } else {
643assert_eq!(local_min, MappedLocalTime::None);
644 }
645 }
646 }
647648#[test]
649fn test_negative_millis() {
650let dt = Utc.timestamp_millis_opt(-1000).unwrap();
651assert_eq!(dt.to_string(), "1969-12-31 23:59:59 UTC");
652let dt = Utc.timestamp_millis_opt(-7000).unwrap();
653assert_eq!(dt.to_string(), "1969-12-31 23:59:53 UTC");
654let dt = Utc.timestamp_millis_opt(-7001).unwrap();
655assert_eq!(dt.to_string(), "1969-12-31 23:59:52.999 UTC");
656let dt = Utc.timestamp_millis_opt(-7003).unwrap();
657assert_eq!(dt.to_string(), "1969-12-31 23:59:52.997 UTC");
658let dt = Utc.timestamp_millis_opt(-999).unwrap();
659assert_eq!(dt.to_string(), "1969-12-31 23:59:59.001 UTC");
660let dt = Utc.timestamp_millis_opt(-1).unwrap();
661assert_eq!(dt.to_string(), "1969-12-31 23:59:59.999 UTC");
662let dt = Utc.timestamp_millis_opt(-60000).unwrap();
663assert_eq!(dt.to_string(), "1969-12-31 23:59:00 UTC");
664let dt = Utc.timestamp_millis_opt(-3600000).unwrap();
665assert_eq!(dt.to_string(), "1969-12-31 23:00:00 UTC");
666667for (millis, expected) in &[
668 (-7000, "1969-12-31 23:59:53 UTC"),
669 (-7001, "1969-12-31 23:59:52.999 UTC"),
670 (-7003, "1969-12-31 23:59:52.997 UTC"),
671 ] {
672match Utc.timestamp_millis_opt(*millis) {
673 MappedLocalTime::Single(dt) => {
674assert_eq!(dt.to_string(), *expected);
675 }
676 e => panic!("Got {e:?} instead of an okay answer"),
677 }
678 }
679 }
680681#[test]
682fn test_negative_nanos() {
683let dt = Utc.timestamp_nanos(-1_000_000_000);
684assert_eq!(dt.to_string(), "1969-12-31 23:59:59 UTC");
685let dt = Utc.timestamp_nanos(-999_999_999);
686assert_eq!(dt.to_string(), "1969-12-31 23:59:59.000000001 UTC");
687let dt = Utc.timestamp_nanos(-1);
688assert_eq!(dt.to_string(), "1969-12-31 23:59:59.999999999 UTC");
689let dt = Utc.timestamp_nanos(-60_000_000_000);
690assert_eq!(dt.to_string(), "1969-12-31 23:59:00 UTC");
691let dt = Utc.timestamp_nanos(-3_600_000_000_000);
692assert_eq!(dt.to_string(), "1969-12-31 23:00:00 UTC");
693 }
694695#[test]
696fn test_nanos_never_panics() {
697 Utc.timestamp_nanos(i64::MAX);
698 Utc.timestamp_nanos(i64::default());
699 Utc.timestamp_nanos(i64::MIN);
700 }
701702#[test]
703fn test_negative_micros() {
704let dt = Utc.timestamp_micros(-1_000_000).unwrap();
705assert_eq!(dt.to_string(), "1969-12-31 23:59:59 UTC");
706let dt = Utc.timestamp_micros(-999_999).unwrap();
707assert_eq!(dt.to_string(), "1969-12-31 23:59:59.000001 UTC");
708let dt = Utc.timestamp_micros(-1).unwrap();
709assert_eq!(dt.to_string(), "1969-12-31 23:59:59.999999 UTC");
710let dt = Utc.timestamp_micros(-60_000_000).unwrap();
711assert_eq!(dt.to_string(), "1969-12-31 23:59:00 UTC");
712let dt = Utc.timestamp_micros(-3_600_000_000).unwrap();
713assert_eq!(dt.to_string(), "1969-12-31 23:00:00 UTC");
714 }
715}