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