From 59ee18abaac3c5ec812077834d8450fe039f0b86 Mon Sep 17 00:00:00 2001 From: William Turner Date: Fri, 10 Nov 2023 17:35:28 -0500 Subject: [PATCH] Add support for chrono FixedOffset --- fake/src/faker/impls/chrono.rs | 4 ++-- fake/src/impls/chrono/mod.rs | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/fake/src/faker/impls/chrono.rs b/fake/src/faker/impls/chrono.rs index db9556f..3400415 100644 --- a/fake/src/faker/impls/chrono.rs +++ b/fake/src/faker/impls/chrono.rs @@ -1,7 +1,7 @@ use crate::faker::chrono::raw::*; use crate::locales::Data; use crate::{Dummy, Fake, Faker}; -use chrono::Utc; +use chrono::{TimeZone, Utc}; use rand::Rng; const MINUTES_MAX_BOUND: i64 = 1_000_000; @@ -43,7 +43,7 @@ impl Dummy> for chrono::NaiveDateTime { } } -impl Dummy> for chrono::DateTime { +impl> Dummy> for chrono::DateTime { #[inline] fn dummy_with_rng(_: &DateTime, rng: &mut R) -> Self { Faker.fake_with_rng(rng) diff --git a/fake/src/impls/chrono/mod.rs b/fake/src/impls/chrono/mod.rs index 773ed02..9c6dbdd 100644 --- a/fake/src/impls/chrono/mod.rs +++ b/fake/src/impls/chrono/mod.rs @@ -1,7 +1,9 @@ #![allow(deprecated)] use crate::{Dummy, Fake, Faker}; -use chrono::{Date, DateTime, Duration, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc}; +use chrono::{ + Date, DateTime, Duration, FixedOffset, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc, +}; use rand::Rng; const YEAR_MAG: i32 = 3_000i32; @@ -28,6 +30,18 @@ impl Dummy for Utc { } } +impl Dummy for FixedOffset { + fn dummy_with_rng(_: &Faker, rng: &mut R) -> Self { + if rng.gen_bool(0.5) { + let halfs: i32 = (0..=28).fake_with_rng(rng); + FixedOffset::east_opt(halfs * 30 * 60).expect("failed to create FixedOffset") + } else { + let halfs: i32 = (0..=24).fake_with_rng(rng); + FixedOffset::west_opt(halfs * 30 * 60).expect("failed to create FixedOffset") + } + } +} + impl Dummy for DateTime where Tz: TimeZone + Dummy,