|
| 1 | +use chrono::{DateTime, FixedOffset, NaiveDateTime}; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + convert::{impl_convert, ConvertFrom}, |
| 5 | + ext::date::{DateTimeEncode, DateTimeWithOffsetDecode, DateTimeWithOffsetEncode}, |
| 6 | +}; |
| 7 | + |
| 8 | +impl_convert!( |
| 9 | + DateTime<FixedOffset>, |
| 10 | + DateTimeWithOffsetEncode, |
| 11 | + DateTimeWithOffsetDecode |
| 12 | +); |
| 13 | + |
| 14 | +impl ConvertFrom<&DateTime<FixedOffset>> for DateTimeWithOffsetEncode { |
| 15 | + fn convert_from(x: &DateTime<FixedOffset>) -> Self { |
| 16 | + let naive_enc = DateTimeEncode::convert_from(&x.naive_utc()); |
| 17 | + let offset_sec = x.offset().local_minus_utc(); |
| 18 | + |
| 19 | + (naive_enc, offset_sec) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +impl ConvertFrom<DateTimeWithOffsetEncode> for DateTime<FixedOffset> { |
| 24 | + fn convert_from(enc: DateTimeWithOffsetEncode) -> Self { |
| 25 | + let naive = NaiveDateTime::convert_from(enc.0); |
| 26 | + let offset = |
| 27 | + FixedOffset::east_opt(enc.1).unwrap_or_else(|| FixedOffset::east_opt(0).unwrap()); |
| 28 | + |
| 29 | + DateTime::<FixedOffset>::from_naive_utc_and_offset(naive, offset) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +#[cfg(test)] |
| 34 | +mod tests { |
| 35 | + use alloc::vec::Vec; |
| 36 | + use chrono::{DateTime, FixedOffset, NaiveDate}; |
| 37 | + |
| 38 | + #[test] |
| 39 | + fn test_chrono_datetime_fixedoffset() { |
| 40 | + let dates = [ |
| 41 | + (1, 1, 1), |
| 42 | + (1970, 1, 1), // epoch |
| 43 | + (2025, 10, 6), |
| 44 | + (-44, 3, 15), // BCE |
| 45 | + (9999, 12, 31), |
| 46 | + ]; |
| 47 | + |
| 48 | + let offsets = [ |
| 49 | + -12 * 3600, // UTC-12, Baker Island Time |
| 50 | + -11 * 3600, // UTC-11, Niue / Samoa |
| 51 | + -5 * 3600, // UTC-5, EST (Eastern Standard Time, 美东冬令时) |
| 52 | + -3 * 3600, // UTC-3, BRT (Brasilia Time) |
| 53 | + 0, // UTC+0, GMT |
| 54 | + 3600, // UTC+1, CET (Central European Time) |
| 55 | + 3 * 3600, // UTC+3, MSK (Moscow Time) |
| 56 | + 5 * 3600 + 1800, // UTC+5:30, IST (India Standard Time) |
| 57 | + 8 * 3600, // UTC+8, CST (China Standard Time) |
| 58 | + 14 * 3600, // UTC+14, Line Islands Time |
| 59 | + ]; |
| 60 | + |
| 61 | + let times = [(0, 0, 0), (12, 34, 56), (23, 59, 59)]; |
| 62 | + |
| 63 | + for &(y, m, d) in &dates { |
| 64 | + for &(h, mi, s) in × { |
| 65 | + let naive = NaiveDate::from_ymd_opt(y, m, d) |
| 66 | + .unwrap() |
| 67 | + .and_hms_opt(h, mi, s) |
| 68 | + .unwrap(); |
| 69 | + |
| 70 | + for &offset_sec in &offsets { |
| 71 | + let offset = FixedOffset::east_opt(offset_sec).unwrap(); |
| 72 | + let dt_fixed = |
| 73 | + DateTime::<FixedOffset>::from_naive_utc_and_offset(naive, offset); |
| 74 | + |
| 75 | + let enc = crate::encode(&dt_fixed); |
| 76 | + let decoded: DateTime<FixedOffset> = crate::decode(&enc).unwrap(); |
| 77 | + |
| 78 | + assert_eq!( |
| 79 | + dt_fixed, decoded, |
| 80 | + "Failed for datetime {:?} with offset {}", |
| 81 | + dt_fixed, offset |
| 82 | + ); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + fn bench_data() -> Vec<DateTime<FixedOffset>> { |
| 89 | + crate::random_data(1000) |
| 90 | + .into_iter() |
| 91 | + .map( |
| 92 | + |(y, m, d, h, mi, s, n, offset_sec): (i32, u32, u32, u32, u32, u32, u32, i32)| { |
| 93 | + let naive = |
| 94 | + NaiveDate::from_ymd_opt((y % 9999).max(1), (m % 12).max(1), (d % 28) + 1) |
| 95 | + .unwrap() |
| 96 | + .and_hms_nano_opt(h % 24, mi % 60, s % 60, n % 1_000_000_000) |
| 97 | + .unwrap(); |
| 98 | + let offset = FixedOffset::east_opt(offset_sec % 86_400) |
| 99 | + .unwrap_or(FixedOffset::east_opt(0).unwrap()); |
| 100 | + DateTime::<FixedOffset>::from_naive_utc_and_offset(naive, offset) |
| 101 | + }, |
| 102 | + ) |
| 103 | + .collect() |
| 104 | + } |
| 105 | + |
| 106 | + crate::bench_encode_decode!(data: Vec<DateTime<FixedOffset>>); |
| 107 | +} |
0 commit comments