Skip to content

Commit

Permalink
Add support for chrono FixedOffset
Browse files Browse the repository at this point in the history
  • Loading branch information
willtrnr committed Nov 13, 2023
1 parent 5cd897e commit 59ee18a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 2 additions & 2 deletions fake/src/faker/impls/chrono.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -43,7 +43,7 @@ impl<L: Data> Dummy<DateTime<L>> for chrono::NaiveDateTime {
}
}

impl<L: Data> Dummy<DateTime<L>> for chrono::DateTime<Utc> {
impl<L: Data, Tz: TimeZone + Dummy<Faker>> Dummy<DateTime<L>> for chrono::DateTime<Tz> {
#[inline]
fn dummy_with_rng<R: Rng + ?Sized>(_: &DateTime<L>, rng: &mut R) -> Self {
Faker.fake_with_rng(rng)
Expand Down
16 changes: 15 additions & 1 deletion fake/src/impls/chrono/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -28,6 +30,18 @@ impl Dummy<Faker> for Utc {
}
}

impl Dummy<Faker> for FixedOffset {
fn dummy_with_rng<R: Rng + ?Sized>(_: &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<Tz> Dummy<Faker> for DateTime<Tz>
where
Tz: TimeZone + Dummy<Faker>,
Expand Down

0 comments on commit 59ee18a

Please sign in to comment.