1use core::fmt;
4
5#[derive(Debug, PartialEq, Eq, Clone, Copy)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10#[cfg_attr(feature = "defmt", derive(defmt::Format))]
11pub struct OutOfRangeError(pub(crate) ());
12
13impl fmt::Display for OutOfRangeError {
14 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
15 "timestamp out of representable range".fmt(fmt)
16 }
17}
18
19#[cfg(feature = "std")]
20impl std::error::Error for OutOfRangeError {}
21
22#[derive(Debug, PartialEq, Eq, Clone, Copy)]
25#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
26#[cfg_attr(feature = "defmt", derive(defmt::Format))]
27pub enum DateTimeError {
28 InvalidMonth(u8),
30 InvalidDayOfMonth(u8),
33 InvalidHour(u8),
35 InvalidMinute(u8),
37 InvalidSecond(u8),
39 InvalidNanosecond(u32),
41 OutOfRange,
44}
45
46impl fmt::Display for DateTimeError {
47 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
48 match self {
49 Self::InvalidMonth(month) => write!(fmt, "month numeral '{}' is not valid", month),
50 Self::InvalidDayOfMonth(day) => {
51 write!(fmt, "day of month '{}' is not valid for this date", day)
52 }
53 Self::InvalidHour(hour) => write!(fmt, "hour numeral '{}' is not valid", hour),
54 Self::InvalidMinute(min) => write!(fmt, "minute numeral '{}' is not valid", min),
55 Self::InvalidSecond(sec) => write!(fmt, "second numeral '{}' is not valid", sec),
56 Self::InvalidNanosecond(nanosec) => {
57 write!(fmt, "nanosecond value '{}' is not valid", nanosec)
58 }
59 Self::OutOfRange => "timestamp outside representable range".fmt(fmt),
60 }
61 }
62}
63
64#[cfg(feature = "std")]
65impl std::error::Error for DateTimeError {}
66
67#[derive(Debug, PartialEq, Eq, Clone)]
70#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
71#[cfg_attr(feature = "defmt", derive(defmt::Format))]
72pub enum ParseDateTimeError {
73 InvalidFieldValue,
76 InvalidFieldWidth,
78 MissingField,
80 RangeError(DateTimeError),
83}
84
85impl fmt::Display for ParseDateTimeError {
86 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
87 match self {
88 Self::InvalidFieldValue => "one of the fields is invalid".fmt(fmt),
89 Self::InvalidFieldWidth => "the width of one of the fields is invalid".fmt(fmt),
90 Self::MissingField => "a fields is missing".fmt(fmt),
91 Self::RangeError(err) => err.fmt(fmt),
92 }
93 }
94}
95
96#[cfg(feature = "std")]
97impl std::error::Error for ParseDateTimeError {}