From de57f5617a1cfb61105e65437697bebe2916cd70 Mon Sep 17 00:00:00 2001 From: Sergey Potapov Date: Mon, 17 Oct 2022 17:11:05 +0200 Subject: [PATCH] Implement Arbitrary for timezones --- src/offset/fixed.rs | 10 ++++++++++ src/offset/local/mod.rs | 1 + src/offset/utc.rs | 1 + 3 files changed, 12 insertions(+) diff --git a/src/offset/fixed.rs b/src/offset/fixed.rs index 0c6ccae7..18896173 100644 --- a/src/offset/fixed.rs +++ b/src/offset/fixed.rs @@ -152,6 +152,16 @@ impl fmt::Display for FixedOffset { } } +#[cfg(feature = "arbitrary")] +impl arbitrary::Arbitrary<'_> for FixedOffset { + fn arbitrary(u: &mut arbitrary::Unstructured) -> arbitrary::Result { + let secs = u.int_in_range(-86_399..=86_399)?; + let fixed_offset = FixedOffset::east_opt(secs) + .expect("Could not generate a valid chrono::FixedOffset. It looks like implementation of Arbitrary for FixedOffset is erroneous."); + Ok(fixed_offset) + } +} + // addition or subtraction of FixedOffset to/from Timelike values is the same as // adding or subtracting the offset's local_minus_utc value // but keep keeps the leap second information. diff --git a/src/offset/local/mod.rs b/src/offset/local/mod.rs index 29a3cdd4..6abfebad 100644 --- a/src/offset/local/mod.rs +++ b/src/offset/local/mod.rs @@ -52,6 +52,7 @@ mod tz_info; /// ``` #[derive(Copy, Clone, Debug)] #[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))] +#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] pub struct Local; impl Local { diff --git a/src/offset/utc.rs b/src/offset/utc.rs index 76f24a41..8cf03daa 100644 --- a/src/offset/utc.rs +++ b/src/offset/utc.rs @@ -41,6 +41,7 @@ use crate::{Date, DateTime}; /// ``` #[derive(Copy, Clone, PartialEq, Eq)] #[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))] +#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] pub struct Utc; #[cfg(feature = "clock")]