diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-18 02:49:50 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-18 02:49:50 +0000 |
commit | 9835e2ae736235810b4ea1c162ca5e65c547e770 (patch) | |
tree | 3fcebf40ed70e581d776a8a4c65923e8ec20e026 /vendor/der/src/datetime.rs | |
parent | Releasing progress-linux version 1.70.0+dfsg2-1~progress7.99u1. (diff) | |
download | rustc-9835e2ae736235810b4ea1c162ca5e65c547e770.tar.xz rustc-9835e2ae736235810b4ea1c162ca5e65c547e770.zip |
Merging upstream version 1.71.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/der/src/datetime.rs')
-rw-r--r-- | vendor/der/src/datetime.rs | 48 |
1 files changed, 30 insertions, 18 deletions
diff --git a/vendor/der/src/datetime.rs b/vendor/der/src/datetime.rs index 2b4c504bf..de28d53bb 100644 --- a/vendor/der/src/datetime.rs +++ b/vendor/der/src/datetime.rs @@ -240,7 +240,6 @@ impl DateTime { /// Instantiate from [`SystemTime`]. #[cfg(feature = "std")] - #[cfg_attr(docsrs, doc(cfg(feature = "std")))] pub fn from_system_time(time: SystemTime) -> Result<Self> { time.duration_since(UNIX_EPOCH) .map_err(|_| ErrorKind::DateTime.into()) @@ -249,7 +248,6 @@ impl DateTime { /// Convert to [`SystemTime`]. #[cfg(feature = "std")] - #[cfg_attr(docsrs, doc(cfg(feature = "std")))] pub fn to_system_time(&self) -> SystemTime { UNIX_EPOCH + self.unix_duration() } @@ -258,19 +256,12 @@ impl DateTime { impl FromStr for DateTime { type Err = Error; - // TODO(tarcieri): checked arithmetic - #[allow(clippy::integer_arithmetic)] fn from_str(s: &str) -> Result<Self> { match *s.as_bytes() { [year1, year2, year3, year4, b'-', month1, month2, b'-', day1, day2, b'T', hour1, hour2, b':', min1, min2, b':', sec1, sec2, b'Z'] => { let tag = Tag::GeneralizedTime; - let year = - u16::from(decode_decimal(tag, year1, year2).map_err(|_| ErrorKind::DateTime)?) - * 100 - + u16::from( - decode_decimal(tag, year3, year4).map_err(|_| ErrorKind::DateTime)?, - ); + let year = decode_year(&[year1, year2, year3, year4])?; let month = decode_decimal(tag, month1, month2).map_err(|_| ErrorKind::DateTime)?; let day = decode_decimal(tag, day1, day2).map_err(|_| ErrorKind::DateTime)?; let hour = decode_decimal(tag, hour1, hour2).map_err(|_| ErrorKind::DateTime)?; @@ -294,7 +285,6 @@ impl fmt::Display for DateTime { } #[cfg(feature = "std")] -#[cfg_attr(docsrs, doc(cfg(feature = "std")))] impl From<DateTime> for SystemTime { fn from(time: DateTime) -> SystemTime { time.to_system_time() @@ -302,7 +292,6 @@ impl From<DateTime> for SystemTime { } #[cfg(feature = "std")] -#[cfg_attr(docsrs, doc(cfg(feature = "std")))] impl From<&DateTime> for SystemTime { fn from(time: &DateTime) -> SystemTime { time.to_system_time() @@ -310,7 +299,6 @@ impl From<&DateTime> for SystemTime { } #[cfg(feature = "std")] -#[cfg_attr(docsrs, doc(cfg(feature = "std")))] impl TryFrom<SystemTime> for DateTime { type Error = Error; @@ -320,7 +308,6 @@ impl TryFrom<SystemTime> for DateTime { } #[cfg(feature = "std")] -#[cfg_attr(docsrs, doc(cfg(feature = "std")))] impl TryFrom<&SystemTime> for DateTime { type Error = Error; @@ -330,12 +317,11 @@ impl TryFrom<&SystemTime> for DateTime { } #[cfg(feature = "time")] -#[cfg_attr(docsrs, doc(cfg(feature = "time")))] impl TryFrom<DateTime> for PrimitiveDateTime { type Error = Error; fn try_from(time: DateTime) -> Result<PrimitiveDateTime> { - let month = (time.month() as u8).try_into()?; + let month = time.month().try_into()?; let date = time::Date::from_calendar_date(i32::from(time.year()), month, time.day())?; let time = time::Time::from_hms(time.hour(), time.minutes(), time.seconds())?; @@ -344,7 +330,6 @@ impl TryFrom<DateTime> for PrimitiveDateTime { } #[cfg(feature = "time")] -#[cfg_attr(docsrs, doc(cfg(feature = "time")))] impl TryFrom<PrimitiveDateTime> for DateTime { type Error = Error; @@ -360,11 +345,28 @@ impl TryFrom<PrimitiveDateTime> for DateTime { } } +// Implement by hand because the derive would create invalid values. +// Use the conversion from Duration to create a valid value. +#[cfg(feature = "arbitrary")] +impl<'a> arbitrary::Arbitrary<'a> for DateTime { + fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> { + Self::from_unix_duration(Duration::new( + u.int_in_range(0..=MAX_UNIX_DURATION.as_secs().saturating_sub(1))?, + u.int_in_range(0..=999_999_999)?, + )) + .map_err(|_| arbitrary::Error::IncorrectFormat) + } + + fn size_hint(depth: usize) -> (usize, Option<usize>) { + arbitrary::size_hint::and(u64::size_hint(depth), u32::size_hint(depth)) + } +} + /// Decode 2-digit decimal value // TODO(tarcieri): checked arithmetic #[allow(clippy::integer_arithmetic)] pub(crate) fn decode_decimal(tag: Tag, hi: u8, lo: u8) -> Result<u8> { - if (b'0'..=b'9').contains(&hi) && (b'0'..=b'9').contains(&lo) { + if hi.is_ascii_digit() && lo.is_ascii_digit() { Ok((hi - b'0') * 10 + (lo - b'0')) } else { Err(tag.value_error()) @@ -386,6 +388,16 @@ where writer.write_byte(b'0'.checked_add(value % 10).ok_or(ErrorKind::Overflow)?) } +/// Decode 4-digit year. +// TODO(tarcieri): checked arithmetic +#[allow(clippy::integer_arithmetic)] +fn decode_year(year: &[u8; 4]) -> Result<u16> { + let tag = Tag::GeneralizedTime; + let hi = decode_decimal(tag, year[0], year[1]).map_err(|_| ErrorKind::DateTime)?; + let lo = decode_decimal(tag, year[2], year[3]).map_err(|_| ErrorKind::DateTime)?; + Ok(u16::from(hi) * 100 + u16::from(lo)) +} + #[cfg(test)] mod tests { use super::DateTime; |