diesel/pg/types/date_and_time/
quickcheck_impls.rs

1// it's test code
2#![allow(
3    clippy::cast_possible_wrap,
4    clippy::cast_sign_loss,
5    clippy::cast_possible_truncation
6)]
7use quickcheck::{Arbitrary, Gen};
8
9use super::{PgDate, PgInterval, PgTime, PgTimestamp};
10
11// see https://www.postgresql.org/docs/current/datatype-datetime.html
12// for the specific limits
13
14impl Arbitrary for PgDate {
15    fn arbitrary(g: &mut Gen) -> Self {
16        const MIN_DAY: i32 = (-4713 * 365) - (2000 * 365);
17        const MAX_DAY: i32 = 5874897 * 365 - (2000 * 365);
18
19        let mut day = i32::arbitrary(g);
20
21        if day <= MIN_DAY {
22            day %= MIN_DAY;
23        }
24
25        if day >= MAX_DAY {
26            day %= MAX_DAY;
27        }
28
29        PgDate(day)
30    }
31}
32
33impl Arbitrary for PgTime {
34    fn arbitrary(g: &mut Gen) -> Self {
35        // 24:00:00 in microseconds
36        const MAX_TIME: i64 = 24 * 60 * 60 * 1_000_000;
37
38        let time = u64::arbitrary(g);
39
40        let mut time = if time > i64::MAX as u64 {
41            (time / 2) as i64
42        } else {
43            time as i64
44        };
45
46        if time > MAX_TIME {
47            time %= MAX_TIME;
48        }
49
50        PgTime(time)
51    }
52}
53
54impl Arbitrary for PgTimestamp {
55    fn arbitrary(g: &mut Gen) -> Self {
56        const MIN_TIMESTAMP: i64 = -4713 * 365 * 24 * 60 * 60 * 100_000;
57        const MAX_TIMESTAMP: i64 = 294276 * 365 * 24 * 60 * 60 * 100_000;
58
59        let mut timestamp = i64::arbitrary(g);
60
61        if timestamp <= MIN_TIMESTAMP {
62            timestamp %= MIN_TIMESTAMP;
63        }
64
65        if timestamp >= MAX_TIMESTAMP {
66            timestamp %= MAX_TIMESTAMP;
67        }
68
69        PgTimestamp(timestamp)
70    }
71}
72
73impl Arbitrary for PgInterval {
74    fn arbitrary(g: &mut Gen) -> Self {
75        PgInterval {
76            microseconds: i64::arbitrary(g),
77            days: i32::arbitrary(g),
78            months: i32::arbitrary(g),
79        }
80    }
81}