From 44cee53a18ea2bdfb6270b64cd599a69d9f7f1d9 Mon Sep 17 00:00:00 2001 From: Sergey Potapov Date: Mon, 17 Oct 2022 17:17:45 +0200 Subject: [PATCH] Implement Arbitrary for DateTime --- src/datetime/mod.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/datetime/mod.rs b/src/datetime/mod.rs index 28b3bd64..c5a4c721 100644 --- a/src/datetime/mod.rs +++ b/src/datetime/mod.rs @@ -1150,6 +1150,21 @@ impl From> for js_sys::Date { } } +// Note that implementation of Arbitrary cannot be simply derived for DateTime, due to +// the nontrivial bound ::Offset: Arbitrary. +#[cfg(feature = "arbitrary")] +impl<'a, Tz> arbitrary::Arbitrary<'a> for DateTime +where + Tz: TimeZone, + ::Offset: arbitrary::Arbitrary<'a>, +{ + fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result> { + let datetime = NaiveDateTime::arbitrary(u)?; + let offset = ::Offset::arbitrary(u)?; + Ok(DateTime::from_utc(datetime, offset)) + } +} + #[test] fn test_add_sub_months() { let utc_dt = Utc.ymd_opt(2018, 9, 5).unwrap().and_hms_opt(23, 58, 0).unwrap();