1use 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;
38pub use self::utc::Utc;
39
40pub 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)]
79
80pub enum LocalResult<T> {
82 Single(T),
84
85 Ambiguous(T, T),
89
90 None,
95}
96
97impl<T> MappedLocalTime<T> {
98 #[must_use]
105 pub fn single(self) -> Option<T> {
106 match self {
107 MappedLocalTime::Single(t) => Some(t),
108 _ => None,
109 }
110 }
111
112 #[must_use]
118 pub fn earliest(self) -> Option<T> {
119 match self {
120 MappedLocalTime::Single(t) | MappedLocalTime::Ambiguous(t, _) => Some(t),
121 _ => None,
122 }
123 }
124
125 #[must_use]
131 pub fn latest(self) -> Option<T> {
132 match self {
133 MappedLocalTime::Single(t) | MappedLocalTime::Ambiguous(_, t) => Some(t),
134 _ => None,
135 }
136 }
137
138 #[must_use]
140 pub fn map<U, F: FnMut(T) -> U>(self, mut f: F) -> MappedLocalTime<U> {
141 match self {
142 MappedLocalTime::None => MappedLocalTime::None,
143 MappedLocalTime::Single(v) => MappedLocalTime::Single(f(v)),
144 MappedLocalTime::Ambiguous(min, max) => MappedLocalTime::Ambiguous(f(min), f(max)),
145 }
146 }
147
148 #[must_use]
152 pub(crate) fn and_then<U, F: FnMut(T) -> Option<U>>(self, mut f: F) -> MappedLocalTime<U> {
153 match self {
154 MappedLocalTime::None => MappedLocalTime::None,
155 MappedLocalTime::Single(v) => match f(v) {
156 Some(new) => MappedLocalTime::Single(new),
157 None => MappedLocalTime::None,
158 },
159 MappedLocalTime::Ambiguous(min, max) => match (f(min), f(max)) {
160 (Some(min), Some(max)) => MappedLocalTime::Ambiguous(min, max),
161 _ => MappedLocalTime::None,
162 },
163 }
164 }
165}
166
167#[allow(deprecated)]
168impl<Tz: TimeZone> MappedLocalTime<Date<Tz>> {
169 #[inline]
174 #[must_use]
175 pub fn and_time(self, time: NaiveTime) -> MappedLocalTime<DateTime<Tz>> {
176 match self {
177 MappedLocalTime::Single(d) => {
178 d.and_time(time).map_or(MappedLocalTime::None, MappedLocalTime::Single)
179 }
180 _ => MappedLocalTime::None,
181 }
182 }
183
184 #[inline]
189 #[must_use]
190 pub fn and_hms_opt(self, hour: u32, min: u32, sec: u32) -> MappedLocalTime<DateTime<Tz>> {
191 match self {
192 MappedLocalTime::Single(d) => {
193 d.and_hms_opt(hour, min, sec).map_or(MappedLocalTime::None, MappedLocalTime::Single)
194 }
195 _ => MappedLocalTime::None,
196 }
197 }
198
199 #[inline]
205 #[must_use]
206 pub fn and_hms_milli_opt(
207 self,
208 hour: u32,
209 min: u32,
210 sec: u32,
211 milli: u32,
212 ) -> MappedLocalTime<DateTime<Tz>> {
213 match self {
214 MappedLocalTime::Single(d) => d
215 .and_hms_milli_opt(hour, min, sec, milli)
216 .map_or(MappedLocalTime::None, MappedLocalTime::Single),
217 _ => MappedLocalTime::None,
218 }
219 }
220
221 #[inline]
227 #[must_use]
228 pub fn and_hms_micro_opt(
229 self,
230 hour: u32,
231 min: u32,
232 sec: u32,
233 micro: u32,
234 ) -> MappedLocalTime<DateTime<Tz>> {
235 match self {
236 MappedLocalTime::Single(d) => d
237 .and_hms_micro_opt(hour, min, sec, micro)
238 .map_or(MappedLocalTime::None, MappedLocalTime::Single),
239 _ => MappedLocalTime::None,
240 }
241 }
242
243 #[inline]
249 #[must_use]
250 pub fn and_hms_nano_opt(
251 self,
252 hour: u32,
253 min: u32,
254 sec: u32,
255 nano: u32,
256 ) -> MappedLocalTime<DateTime<Tz>> {
257 match self {
258 MappedLocalTime::Single(d) => d
259 .and_hms_nano_opt(hour, min, sec, nano)
260 .map_or(MappedLocalTime::None, MappedLocalTime::Single),
261 _ => MappedLocalTime::None,
262 }
263 }
264}
265
266impl<T: fmt::Debug> MappedLocalTime<T> {
267 #[must_use]
278 #[track_caller]
279 pub fn unwrap(self) -> T {
280 match self {
281 MappedLocalTime::None => { ::core::panicking::panic_fmt(format_args!("No such local time")); }panic!("No such local time"),
282 MappedLocalTime::Single(t) => t,
283 MappedLocalTime::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}
289
290pub trait Offset: Sized + Clone + fmt::Debug {
292 fn fix(&self) -> FixedOffset;
294}
295
296pub trait TimeZone: Sized + Clone {
300 type Offset: Offset;
304
305 fn 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>> {
319 match NaiveDate::from_ymd_opt(year, month, day).and_then(|d| d.and_hms_opt(hour, min, sec))
320 {
321 Some(dt) => self.from_local_datetime(&dt),
322 None => MappedLocalTime::None,
323 }
324 }
325
326 #[deprecated(since = "0.4.23", note = "use `with_ymd_and_hms()` instead")]
334 #[allow(deprecated)]
335 fn ymd(&self, year: i32, month: u32, day: u32) -> Date<Self> {
336 self.ymd_opt(year, month, day).unwrap()
337 }
338
339 #[deprecated(since = "0.4.23", note = "use `with_ymd_and_hms()` instead")]
347 #[allow(deprecated)]
348 fn ymd_opt(&self, year: i32, month: u32, day: u32) -> MappedLocalTime<Date<Self>> {
349 match NaiveDate::from_ymd_opt(year, month, day) {
350 Some(d) => self.from_local_date(&d),
351 None => MappedLocalTime::None,
352 }
353 }
354
355 #[deprecated(
363 since = "0.4.23",
364 note = "use `from_local_datetime()` with a `NaiveDateTime` instead"
365 )]
366 #[allow(deprecated)]
367 fn yo(&self, year: i32, ordinal: u32) -> Date<Self> {
368 self.yo_opt(year, ordinal).unwrap()
369 }
370
371 #[deprecated(
379 since = "0.4.23",
380 note = "use `from_local_datetime()` with a `NaiveDateTime` instead"
381 )]
382 #[allow(deprecated)]
383 fn yo_opt(&self, year: i32, ordinal: u32) -> MappedLocalTime<Date<Self>> {
384 match NaiveDate::from_yo_opt(year, ordinal) {
385 Some(d) => self.from_local_date(&d),
386 None => MappedLocalTime::None,
387 }
388 }
389
390 #[deprecated(
400 since = "0.4.23",
401 note = "use `from_local_datetime()` with a `NaiveDateTime` instead"
402 )]
403 #[allow(deprecated)]
404 fn isoywd(&self, year: i32, week: u32, weekday: Weekday) -> Date<Self> {
405 self.isoywd_opt(year, week, weekday).unwrap()
406 }
407
408 #[deprecated(
418 since = "0.4.23",
419 note = "use `from_local_datetime()` with a `NaiveDateTime` instead"
420 )]
421 #[allow(deprecated)]
422 fn isoywd_opt(&self, year: i32, week: u32, weekday: Weekday) -> MappedLocalTime<Date<Self>> {
423 match NaiveDate::from_isoywd_opt(year, week, weekday) {
424 Some(d) => self.from_local_date(&d),
425 None => MappedLocalTime::None,
426 }
427 }
428
429 #[deprecated(since = "0.4.23", note = "use `timestamp_opt()` instead")]
442 fn timestamp(&self, secs: i64, nsecs: u32) -> DateTime<Self> {
443 self.timestamp_opt(secs, nsecs).unwrap()
444 }
445
446 fn timestamp_opt(&self, secs: i64, nsecs: u32) -> MappedLocalTime<DateTime<Self>> {
467 match DateTime::from_timestamp(secs, nsecs) {
468 Some(dt) => MappedLocalTime::Single(self.from_utc_datetime(&dt.naive_utc())),
469 None => MappedLocalTime::None,
470 }
471 }
472
473 #[deprecated(since = "0.4.23", note = "use `timestamp_millis_opt()` instead")]
479 fn timestamp_millis(&self, millis: i64) -> DateTime<Self> {
480 self.timestamp_millis_opt(millis).unwrap()
481 }
482
483 fn timestamp_millis_opt(&self, millis: i64) -> MappedLocalTime<DateTime<Self>> {
501 match DateTime::from_timestamp_millis(millis) {
502 Some(dt) => MappedLocalTime::Single(self.from_utc_datetime(&dt.naive_utc())),
503 None => MappedLocalTime::None,
504 }
505 }
506
507 fn timestamp_nanos(&self, nanos: i64) -> DateTime<Self> {
520 self.from_utc_datetime(&DateTime::from_timestamp_nanos(nanos).naive_utc())
521 }
522
523 fn timestamp_micros(&self, micros: i64) -> MappedLocalTime<DateTime<Self>> {
534 match DateTime::from_timestamp_micros(micros) {
535 Some(dt) => MappedLocalTime::Single(self.from_utc_datetime(&dt.naive_utc())),
536 None => MappedLocalTime::None,
537 }
538 }
539
540 #[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 )]
559 fn datetime_from_str(&self, s: &str, fmt: &str) -> ParseResult<DateTime<Self>> {
560 let mut parsed = Parsed::new();
561 parse(&mut parsed, s, StrftimeItems::new(fmt))?;
562 parsed.to_datetime_with_timezone(self)
563 }
564
565 fn from_offset(offset: &Self::Offset) -> Self;
567
568 fn offset_from_local_date(&self, local: &NaiveDate) -> MappedLocalTime<Self::Offset>;
570
571 fn offset_from_local_datetime(&self, local: &NaiveDateTime) -> MappedLocalTime<Self::Offset>;
573
574 #[allow(clippy::wrong_self_convention)]
576 #[deprecated(since = "0.4.23", note = "use `from_local_datetime()` instead")]
577 #[allow(deprecated)]
578 fn from_local_date(&self, local: &NaiveDate) -> MappedLocalTime<Date<Self>> {
579 self.offset_from_local_date(local).map(|offset| {
580 Date::from_utc(*local, offset)
582 })
583 }
584
585 #[allow(clippy::wrong_self_convention)]
587 fn from_local_datetime(&self, local: &NaiveDateTime) -> MappedLocalTime<DateTime<Self>> {
588 self.offset_from_local_datetime(local).and_then(|off| {
589 local
590 .checked_sub_offset(off.fix())
591 .map(|dt| DateTime::from_naive_utc_and_offset(dt, off))
592 })
593 }
594
595 fn offset_from_utc_date(&self, utc: &NaiveDate) -> Self::Offset;
597
598 fn offset_from_utc_datetime(&self, utc: &NaiveDateTime) -> Self::Offset;
600
601 #[allow(clippy::wrong_self_convention)]
604 #[deprecated(since = "0.4.23", note = "use `from_utc_datetime()` instead")]
605 #[allow(deprecated)]
606 fn from_utc_date(&self, utc: &NaiveDate) -> Date<Self> {
607 Date::from_utc(*utc, self.offset_from_utc_date(utc))
608 }
609
610 #[allow(clippy::wrong_self_convention)]
613 fn from_utc_datetime(&self, utc: &NaiveDateTime) -> DateTime<Self> {
614 DateTime::from_naive_utc_and_offset(*utc, self.offset_from_utc_datetime(utc))
615 }
616}
617
618#[cfg(test)]
619mod tests {
620 use super::*;
621
622 #[test]
623 fn test_fixed_offset_min_max_dates() {
624 for offset_hour in -23..=23 {
625 dbg!(offset_hour);
626 let offset = FixedOffset::east_opt(offset_hour * 60 * 60).unwrap();
627
628 let local_max = offset.from_utc_datetime(&NaiveDateTime::MAX);
629 assert_eq!(local_max.naive_utc(), NaiveDateTime::MAX);
630 let local_min = offset.from_utc_datetime(&NaiveDateTime::MIN);
631 assert_eq!(local_min.naive_utc(), NaiveDateTime::MIN);
632
633 let local_max = offset.from_local_datetime(&NaiveDateTime::MAX);
634 if offset_hour >= 0 {
635 assert_eq!(local_max.unwrap().naive_local(), NaiveDateTime::MAX);
636 } else {
637 assert_eq!(local_max, MappedLocalTime::None);
638 }
639 let local_min = offset.from_local_datetime(&NaiveDateTime::MIN);
640 if offset_hour <= 0 {
641 assert_eq!(local_min.unwrap().naive_local(), NaiveDateTime::MIN);
642 } else {
643 assert_eq!(local_min, MappedLocalTime::None);
644 }
645 }
646 }
647
648 #[test]
649 fn test_negative_millis() {
650 let dt = Utc.timestamp_millis_opt(-1000).unwrap();
651 assert_eq!(dt.to_string(), "1969-12-31 23:59:59 UTC");
652 let dt = Utc.timestamp_millis_opt(-7000).unwrap();
653 assert_eq!(dt.to_string(), "1969-12-31 23:59:53 UTC");
654 let dt = Utc.timestamp_millis_opt(-7001).unwrap();
655 assert_eq!(dt.to_string(), "1969-12-31 23:59:52.999 UTC");
656 let dt = Utc.timestamp_millis_opt(-7003).unwrap();
657 assert_eq!(dt.to_string(), "1969-12-31 23:59:52.997 UTC");
658 let dt = Utc.timestamp_millis_opt(-999).unwrap();
659 assert_eq!(dt.to_string(), "1969-12-31 23:59:59.001 UTC");
660 let dt = Utc.timestamp_millis_opt(-1).unwrap();
661 assert_eq!(dt.to_string(), "1969-12-31 23:59:59.999 UTC");
662 let dt = Utc.timestamp_millis_opt(-60000).unwrap();
663 assert_eq!(dt.to_string(), "1969-12-31 23:59:00 UTC");
664 let dt = Utc.timestamp_millis_opt(-3600000).unwrap();
665 assert_eq!(dt.to_string(), "1969-12-31 23:00:00 UTC");
666
667 for (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 ] {
672 match Utc.timestamp_millis_opt(*millis) {
673 MappedLocalTime::Single(dt) => {
674 assert_eq!(dt.to_string(), *expected);
675 }
676 e => panic!("Got {e:?} instead of an okay answer"),
677 }
678 }
679 }
680
681 #[test]
682 fn test_negative_nanos() {
683 let dt = Utc.timestamp_nanos(-1_000_000_000);
684 assert_eq!(dt.to_string(), "1969-12-31 23:59:59 UTC");
685 let dt = Utc.timestamp_nanos(-999_999_999);
686 assert_eq!(dt.to_string(), "1969-12-31 23:59:59.000000001 UTC");
687 let dt = Utc.timestamp_nanos(-1);
688 assert_eq!(dt.to_string(), "1969-12-31 23:59:59.999999999 UTC");
689 let dt = Utc.timestamp_nanos(-60_000_000_000);
690 assert_eq!(dt.to_string(), "1969-12-31 23:59:00 UTC");
691 let dt = Utc.timestamp_nanos(-3_600_000_000_000);
692 assert_eq!(dt.to_string(), "1969-12-31 23:00:00 UTC");
693 }
694
695 #[test]
696 fn test_nanos_never_panics() {
697 Utc.timestamp_nanos(i64::MAX);
698 Utc.timestamp_nanos(i64::default());
699 Utc.timestamp_nanos(i64::MIN);
700 }
701
702 #[test]
703 fn test_negative_micros() {
704 let dt = Utc.timestamp_micros(-1_000_000).unwrap();
705 assert_eq!(dt.to_string(), "1969-12-31 23:59:59 UTC");
706 let dt = Utc.timestamp_micros(-999_999).unwrap();
707 assert_eq!(dt.to_string(), "1969-12-31 23:59:59.000001 UTC");
708 let dt = Utc.timestamp_micros(-1).unwrap();
709 assert_eq!(dt.to_string(), "1969-12-31 23:59:59.999999 UTC");
710 let dt = Utc.timestamp_micros(-60_000_000).unwrap();
711 assert_eq!(dt.to_string(), "1969-12-31 23:59:00 UTC");
712 let dt = Utc.timestamp_micros(-3_600_000_000).unwrap();
713 assert_eq!(dt.to_string(), "1969-12-31 23:00:00 UTC");
714 }
715}