fake/faker/impls/
time.rs

1use crate::faker::time::raw::*;
2use crate::locales::Data;
3use crate::{Dummy, Fake, Faker};
4use rand::Rng;
5
6const MINUTES_MAX_BOUND: i64 = 1_000_000;
7
8impl<L: Data> Dummy<Time<L>> for time::Time {
9    #[inline]
10    fn dummy_with_rng<R: Rng + ?Sized>(_: &Time<L>, rng: &mut R) -> Self {
11        Faker.fake_with_rng(rng)
12    }
13}
14
15impl<L: Data> Dummy<Time<L>> for String {
16    #[inline]
17    fn dummy_with_rng<R: Rng + ?Sized>(_: &Time<L>, rng: &mut R) -> Self {
18        let time: time::Time = Faker.fake_with_rng(rng);
19        time.format(&time::format_description::parse(L::TIME_DEFAULT_TIME_FORMAT).unwrap())
20            .unwrap()
21    }
22}
23
24impl<L: Data> Dummy<Date<L>> for time::Date {
25    #[inline]
26    fn dummy_with_rng<R: Rng + ?Sized>(_: &Date<L>, rng: &mut R) -> Self {
27        Faker.fake_with_rng(rng)
28    }
29}
30
31impl<L: Data> Dummy<Date<L>> for String {
32    #[inline]
33    fn dummy_with_rng<R: Rng + ?Sized>(_: &Date<L>, rng: &mut R) -> Self {
34        let date: time::Date = Faker.fake_with_rng(rng);
35        date.format(&time::format_description::parse(L::TIME_DEFAULT_DATE_FORMAT).unwrap())
36            .unwrap()
37    }
38}
39
40impl<L: Data> Dummy<DateTime<L>> for time::PrimitiveDateTime {
41    #[inline]
42    fn dummy_with_rng<R: Rng + ?Sized>(_: &DateTime<L>, rng: &mut R) -> Self {
43        Faker.fake_with_rng(rng)
44    }
45}
46
47impl<L: Data> Dummy<DateTime<L>> for time::OffsetDateTime {
48    #[inline]
49    fn dummy_with_rng<R: Rng + ?Sized>(_: &DateTime<L>, rng: &mut R) -> Self {
50        Faker.fake_with_rng(rng)
51    }
52}
53
54impl<L: Data> Dummy<DateTime<L>> for String {
55    #[inline]
56    fn dummy_with_rng<R: Rng + ?Sized>(_: &DateTime<L>, rng: &mut R) -> Self {
57        let datetime: time::OffsetDateTime = Faker.fake_with_rng(rng);
58        datetime
59            .format(&time::format_description::parse(L::TIME_DEFAULT_DATETIME_FORMAT).unwrap())
60            .unwrap()
61    }
62}
63
64impl<L: Data> Dummy<Duration<L>> for time::Duration {
65    #[inline]
66    fn dummy_with_rng<R: Rng + ?Sized>(_: &Duration<L>, rng: &mut R) -> Self {
67        Faker.fake_with_rng(rng)
68    }
69}
70
71impl<L: Data> Dummy<DateTimeBefore<L>> for time::OffsetDateTime {
72    fn dummy_with_rng<R: Rng + ?Sized>(c: &DateTimeBefore<L>, rng: &mut R) -> Self {
73        let mins: i64 = (1..MINUTES_MAX_BOUND).fake_with_rng(rng);
74        let duration = time::Duration::minutes(mins);
75        c.1 - duration
76    }
77}
78
79impl<L: Data> Dummy<DateTimeBefore<L>> for String {
80    fn dummy_with_rng<R: Rng + ?Sized>(c: &DateTimeBefore<L>, rng: &mut R) -> Self {
81        let datetime: time::OffsetDateTime = c.fake_with_rng(rng);
82        datetime
83            .format(&time::format_description::parse(L::TIME_DEFAULT_DATETIME_FORMAT).unwrap())
84            .unwrap()
85    }
86}
87
88impl<L: Data> Dummy<DateTimeAfter<L>> for time::OffsetDateTime {
89    fn dummy_with_rng<R: Rng + ?Sized>(c: &DateTimeAfter<L>, rng: &mut R) -> Self {
90        let mins: i64 = (1..MINUTES_MAX_BOUND).fake_with_rng(rng);
91        let duration = time::Duration::minutes(mins);
92        c.1 + duration
93    }
94}
95
96impl<L: Data> Dummy<DateTimeAfter<L>> for String {
97    #[inline]
98    fn dummy_with_rng<R: Rng + ?Sized>(c: &DateTimeAfter<L>, rng: &mut R) -> Self {
99        let datetime: time::OffsetDateTime = c.fake_with_rng(rng);
100        datetime
101            .format(&time::format_description::parse(L::TIME_DEFAULT_DATETIME_FORMAT).unwrap())
102            .unwrap()
103    }
104}
105
106impl<L: Data> Dummy<DateTimeBetween<L>> for time::OffsetDateTime {
107    #[inline]
108    fn dummy_with_rng<R: Rng + ?Sized>(c: &DateTimeBetween<L>, rng: &mut R) -> Self {
109        let diff = c.2 - c.1;
110        let max_minutes = diff.whole_minutes();
111
112        let from_start: i64 = (0..max_minutes).fake_with_rng(rng);
113        let duration = time::Duration::minutes(from_start);
114        c.1 + duration
115    }
116}
117
118impl<L: Data> Dummy<DateTimeBetween<L>> for String {
119    #[inline]
120    fn dummy_with_rng<R: Rng + ?Sized>(c: &DateTimeBetween<L>, rng: &mut R) -> Self {
121        let datetime: time::OffsetDateTime = c.fake_with_rng(rng);
122        datetime
123            .format(&time::format_description::parse(L::TIME_DEFAULT_DATETIME_FORMAT).unwrap())
124            .unwrap()
125    }
126}