-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathnaive_date.rs
More file actions
57 lines (48 loc) · 1.63 KB
/
naive_date.rs
File metadata and controls
57 lines (48 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use chrono::{Datelike, NaiveDate};
use crate::{
convert::{impl_convert, ConvertFrom},
ext::date::{DateDecode, DateEncode},
};
impl_convert!(NaiveDate, DateEncode, DateDecode);
impl ConvertFrom<&NaiveDate> for DateEncode {
fn convert_from(days: &NaiveDate) -> Self {
days.num_days_from_ce() - 719_163 // 1970-1-1
}
}
impl ConvertFrom<DateDecode> for NaiveDate {
fn convert_from(days: DateDecode) -> Self {
NaiveDate::from_num_days_from_ce_opt(days + 719_163).unwrap() // 1970-1-1
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_chrono_naive_date() {
let dates = [
NaiveDate::from_ymd_opt(1970, 1, 1).unwrap(), // epoch
NaiveDate::from_ymd_opt(2025, 10, 6).unwrap(),
NaiveDate::from_ymd_opt(1, 1, 1).unwrap(),
NaiveDate::from_ymd_opt(-44, 3, 15).unwrap(), // BCE
NaiveDate::from_ymd_opt(9999, 12, 31).unwrap(),
];
for x in dates {
let enc = crate::encode(&x);
let date: NaiveDate = crate::decode(&enc).unwrap();
assert_eq!(x, date, "failed for date {:?}", x);
}
}
use alloc::vec::Vec;
use chrono::NaiveDate;
fn bench_data() -> Vec<NaiveDate> {
crate::random_data(1000)
.into_iter()
.map(|(y, m, d): (i32, u32, u32)| {
let year = (y % 9999).max(1); // 1 ~ 9998
let month = (m % 12).max(1); // 1 ~ 12
let day = (d % 28) + 1; // 1 ~ 28
NaiveDate::from_ymd_opt(year, month, day).unwrap()
})
.collect()
}
crate::bench_encode_decode!(data: Vec<_>);
}