1use crate::{
2 error,
3 tz::{Offset, TimeZone},
4};
5
6#[derive(Clone, Debug)]
7pub(crate) enum Error {
8 ConvertDateTimeToTimestamp {
9 offset: Offset,
10 },
11 OverflowAddSignedDuration,
12 OverflowSignedDuration,
13 ResolveRejectFold {
14 given: Offset,
15 before: Offset,
16 after: Offset,
17 tz: TimeZone,
18 },
19 ResolveRejectGap {
20 given: Offset,
21 before: Offset,
22 after: Offset,
23 tz: TimeZone,
24 },
25 ResolveRejectUnambiguous {
26 given: Offset,
27 offset: Offset,
28 tz: TimeZone,
29 },
30 RoundOverflow,
31}
32
33impl From<Error> for error::Error {
34 #[cold]
35 #[inline(never)]
36 fn from(err: Error) -> error::Error {
37 error::ErrorKind::TzOffset(err).into()
38 }
39}
40
41impl error::IntoError for Error {
42 fn into_error(self) -> error::Error {
43 self.into()
44 }
45}
46
47impl core::fmt::Display for Error {
48 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
49 use self::Error::*;
50
51 match *self {
52 ConvertDateTimeToTimestamp { offset } => write!(
53 f,
54 "converting datetime with time zone offset `{offset}` \
55 to timestamp overflowed",
56 ),
57 OverflowAddSignedDuration => f.write_str(
58 "adding signed duration to time zone offset overflowed",
59 ),
60 OverflowSignedDuration => {
61 f.write_str("signed duration overflows time zone offset")
62 }
63 ResolveRejectFold { given, before, after, ref tz } => write!(
64 f,
65 "datetime could not resolve to timestamp \
66 since `reject` conflict resolution was chosen, and \
67 because datetime has offset `{given}`, but the time \
68 zone `{tzname}` for the given datetime falls in a fold \
69 between offsets `{before}` and `{after}`, neither of which \
70 match the offset",
71 tzname = tz.diagnostic_name(),
72 ),
73 ResolveRejectGap { given, before, after, ref tz } => write!(
74 f,
75 "datetime could not resolve to timestamp \
76 since `reject` conflict resolution was chosen, and \
77 because datetime has offset `{given}`, but the time \
78 zone `{tzname}` for the given datetime falls in a gap \
79 (between offsets `{before}` and `{after}`), and all \
80 offsets for a gap are regarded as invalid",
81 tzname = tz.diagnostic_name(),
82 ),
83 ResolveRejectUnambiguous { given, offset, ref tz } => write!(
84 f,
85 "datetime could not resolve to a timestamp since \
86 `reject` conflict resolution was chosen, and because \
87 datetime has offset `{given}`, but the time \
88 zone `{tzname}` for the given datetime \
89 unambiguously has offset `{offset}`",
90 tzname = tz.diagnostic_name(),
91 ),
92 RoundOverflow => f.write_str(
93 "rounding time zone offset resulted in a duration \
94 that overflows",
95 ),
96 }
97 }
98}