time/
rand08.rs

1//! Implementation of [`Distribution`] for various structs.
2
3use rand08::Rng;
4use rand08::distributions::{Distribution, Standard};
5
6use crate::{
7    Date, Duration, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcDateTime, UtcOffset, Weekday,
8};
9
10impl Distribution<Time> for Standard {
11    #[inline]
12    fn sample<R>(&self, rng: &mut R) -> Time
13    where
14        R: Rng + ?Sized,
15    {
16        Time::from_hms_nanos_ranged(rng.r#gen(), rng.r#gen(), rng.r#gen(), rng.r#gen())
17    }
18}
19
20impl Distribution<Date> for Standard {
21    #[inline]
22    fn sample<R>(&self, rng: &mut R) -> Date
23    where
24        R: Rng + ?Sized,
25    {
26        // Safety: The Julian day number is in range.
27        unsafe {
28            Date::from_julian_day_unchecked(
29                rng.gen_range(Date::MIN.to_julian_day()..=Date::MAX.to_julian_day()),
30            )
31        }
32    }
33}
34
35impl Distribution<UtcOffset> for Standard {
36    #[inline]
37    fn sample<R>(&self, rng: &mut R) -> UtcOffset
38    where
39        R: Rng + ?Sized,
40    {
41        UtcOffset::from_hms_ranged(rng.r#gen(), rng.r#gen(), rng.r#gen())
42    }
43}
44
45impl Distribution<PrimitiveDateTime> for Standard {
46    #[inline]
47    fn sample<R>(&self, rng: &mut R) -> PrimitiveDateTime
48    where
49        R: Rng + ?Sized,
50    {
51        PrimitiveDateTime::new(Self.sample(rng), Self.sample(rng))
52    }
53}
54
55impl Distribution<UtcDateTime> for Standard {
56    #[inline]
57    fn sample<R>(&self, rng: &mut R) -> UtcDateTime
58    where
59        R: Rng + ?Sized,
60    {
61        UtcDateTime::new(Self.sample(rng), Self.sample(rng))
62    }
63}
64
65impl Distribution<OffsetDateTime> for Standard {
66    #[inline]
67    fn sample<R>(&self, rng: &mut R) -> OffsetDateTime
68    where
69        R: Rng + ?Sized,
70    {
71        let date_time: PrimitiveDateTime = Self.sample(rng);
72        date_time.assume_offset(Self.sample(rng))
73    }
74}
75
76impl Distribution<Duration> for Standard {
77    #[inline]
78    fn sample<R>(&self, rng: &mut R) -> Duration
79    where
80        R: Rng + ?Sized,
81    {
82        Duration::new_ranged(rng.r#gen(), rng.r#gen())
83    }
84}
85
86impl Distribution<Weekday> for Standard {
87    #[inline]
88    fn sample<R>(&self, rng: &mut R) -> Weekday
89    where
90        R: Rng + ?Sized,
91    {
92        use Weekday::*;
93
94        match rng.gen_range(0u8..7) {
95            0 => Monday,
96            1 => Tuesday,
97            2 => Wednesday,
98            3 => Thursday,
99            4 => Friday,
100            5 => Saturday,
101            val => {
102                debug_assert!(val == 6);
103                Sunday
104            }
105        }
106    }
107}
108
109impl Distribution<Month> for Standard {
110    #[inline]
111    fn sample<R>(&self, rng: &mut R) -> Month
112    where
113        R: Rng + ?Sized,
114    {
115        use Month::*;
116        match rng.gen_range(1u8..=12) {
117            1 => January,
118            2 => February,
119            3 => March,
120            4 => April,
121            5 => May,
122            6 => June,
123            7 => July,
124            8 => August,
125            9 => September,
126            10 => October,
127            11 => November,
128            val => {
129                debug_assert!(val == 12);
130                December
131            }
132        }
133    }
134}