zvariant/type/
time.rs

1use crate::impl_type_with_repr;
2
3impl_type_with_repr! {
4    std::time::SystemTime => (u64, u32) {
5        system_time {
6            samples = [std::time::SystemTime::now()],
7            repr(t) = {
8                let since_epoch = t.duration_since(std::time::SystemTime::UNIX_EPOCH).unwrap();
9                (since_epoch.as_secs(), since_epoch.subsec_nanos())
10            },
11        }
12    }
13}
14
15#[cfg(feature = "time")]
16impl_type_with_repr! {
17    time::Date => (i32, u16) {
18        time_date {
19            samples = [time::Date::MIN, time::Date::MAX, time::Date::from_calendar_date(2011, time::Month::June, 21).unwrap()],
20            // https://github.com/time-rs/time/blob/f9398b9598757508ca3815694f23203843e0011b/src/serde/mod.rs#L92
21            repr(d) = (d.year(), d.ordinal()),
22        }
23    }
24}
25
26#[cfg(feature = "time")]
27impl_type_with_repr! {
28    time::Duration => (i64, i32) {
29        time_duration {
30            samples = [time::Duration::MIN, time::Duration::MAX, time::Duration::new(42, 123456789)],
31            // https://github.com/time-rs/time/blob/f9398b9598757508ca3815694f23203843e0011b/src/serde/mod.rs#L119
32            repr(d) = (d.whole_seconds(), d.subsec_nanoseconds()),
33        }
34    }
35}
36
37#[cfg(feature = "time")]
38impl_type_with_repr! {
39    time::OffsetDateTime => (i32, u16, u8, u8, u8, u32, i8, i8, i8) {
40        time_offset_date_time {
41            samples = [
42                time::OffsetDateTime::now_utc(),
43                time::OffsetDateTime::new_in_offset(
44                    time::Date::from_calendar_date(2024, time::Month::May, 4).unwrap(),
45                    time::Time::from_hms_nano(15, 32, 43, 2_000).unwrap(),
46                    time::UtcOffset::from_hms(1, 2, 3).unwrap())
47            ],
48            // https://github.com/time-rs/time/blob/f9398b9598757508ca3815694f23203843e0011b/src/serde/mod.rs#L155
49            repr(d) = (
50                d.year(),
51                d.ordinal(),
52                d.hour(),
53                d.minute(),
54                d.second(),
55                d.nanosecond(),
56                d.offset().whole_hours(),
57                d.offset().minutes_past_hour(),
58                d.offset().seconds_past_minute()
59            ),
60        }
61    }
62}
63
64#[cfg(feature = "time")]
65impl_type_with_repr! {
66    time::PrimitiveDateTime => (i32, u16, u8, u8, u8, u32) {
67        time_primitive_date_time {
68            samples = [
69                time::PrimitiveDateTime::MIN,
70                time::PrimitiveDateTime::MAX,
71                time::PrimitiveDateTime::new(
72                    time::Date::from_calendar_date(2024, time::Month::May, 4).unwrap(),
73                    time::Time::from_hms_nano(15, 32, 43, 2_000).unwrap())
74            ],
75            // https://github.com/time-rs/time/blob/f9398b9598757508ca3815694f23203843e0011b/src/serde/mod.rs#L200
76            repr(d) = (
77                d.year(),
78                d.ordinal(),
79                d.hour(),
80                d.minute(),
81                d.second(),
82                d.nanosecond()
83            ),
84        }
85    }
86}
87
88#[cfg(feature = "time")]
89impl_type_with_repr! {
90    time::Time => (u8, u8, u8, u32) {
91        time_time {
92            samples = [time::Time::MIDNIGHT, time::Time::from_hms(23, 42, 59).unwrap(), time::Time::from_hms_nano(15, 32, 43, 2_000).unwrap()],
93            // https://github.com/time-rs/time/blob/f9398b9598757508ca3815694f23203843e0011b/src/serde/mod.rs#L246
94            repr(t) = (t.hour(), t.minute(), t.second(), t.nanosecond()),
95        }
96    }
97}
98
99#[cfg(feature = "time")]
100impl_type_with_repr! {
101    time::UtcOffset => (i8, i8, i8) {
102        time_utc_offset {
103            samples = [time::UtcOffset::UTC, time::UtcOffset::from_hms(1, 2, 3).unwrap()],
104            // https://github.com/time-rs/time/blob/f9398b9598757508ca3815694f23203843e0011b/src/serde/mod.rs#L282
105            repr(offset) = (offset.whole_hours(), offset.minutes_past_hour(), offset.seconds_past_minute()),
106        }
107    }
108}
109
110#[cfg(feature = "time")]
111impl_type_with_repr! {
112    time::Weekday => u8 {
113        time_weekday {
114            samples = [time::Weekday::Monday, time::Weekday::Wednesday, time::Weekday::Friday],
115            // https://github.com/time-rs/time/blob/f9398b9598757508ca3815694f23203843e0011b/src/serde/mod.rs#L312
116            repr(weekday) = weekday.number_from_monday(),
117        }
118    }
119}
120
121#[cfg(feature = "time")]
122impl_type_with_repr! {
123    time::Month => u8 {
124        time_month {
125            samples = [time::Month::January, time::Month::July, time::Month::December],
126            // Serialized as month number:
127            // https://github.com/time-rs/time/blob/f9398b9598757508ca3815694f23203843e0011b/src/serde/mod.rs#L337
128            repr(month) = month as u8,
129        }
130    }
131}
132
133#[cfg(feature = "chrono")]
134impl_type_with_repr! {
135    chrono::DateTime<Tz: chrono::TimeZone> => &str {
136        chrono_date_time <Tz = chrono::offset::Utc> {
137            samples = [chrono::DateTime::<Tz>::MIN_UTC, chrono::DateTime::<Tz>::MAX_UTC],
138            repr(date) = &date.format("%Y-%m-%dT%H:%M:%S%.fZ").to_string(),
139        }
140    }
141}
142
143#[cfg(feature = "chrono")]
144impl_type_with_repr! {
145    chrono::Month => &str {
146        chrono_month {
147            samples = [chrono::Month::January, chrono::Month::December],
148            repr(month) = month.name(),
149        }
150    }
151}
152
153#[cfg(feature = "chrono")]
154impl_type_with_repr! {
155    chrono::NaiveDate => &str {
156        chrono_naive_date {
157            samples = [chrono::NaiveDate::from_ymd_opt(2016, 7, 8).unwrap()],
158            repr(d) = &format!("{d:?}"),
159        }
160    }
161}
162
163#[cfg(feature = "chrono")]
164impl_type_with_repr! {
165    chrono::NaiveDateTime => &str {
166        chrono_naive_date_time {
167            samples = [chrono::NaiveDate::from_ymd_opt(2016, 7, 8).unwrap().and_hms_opt(9, 10, 11).unwrap()],
168            repr(dt) = &format!("{dt:?}"),
169        }
170    }
171}
172
173#[cfg(feature = "chrono")]
174impl_type_with_repr! {
175    chrono::NaiveTime => &str {
176        chrono_naive_time {
177            samples = [chrono::NaiveTime::from_hms_opt(9, 10, 11).unwrap()],
178            repr(t) = &format!("{t:?}"),
179        }
180    }
181}
182
183#[cfg(feature = "chrono")]
184impl_type_with_repr! {
185    chrono::Weekday => &str {
186        chrono_weekday {
187            samples = [chrono::Weekday::Mon, chrono::Weekday::Fri],
188            // Serialized as the weekday's name.
189            repr(weekday) = &weekday.to_string(),
190        }
191    }
192}