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