Debian Codegraph @

debian/rust-vendor/icu_calendar/src/chrono.rs

raw
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 use crate::Date;
6 use crate::Gregorian;
7 use crate::cal::abstract_gregorian::AbstractGregorianYear;
8 use crate::cal::gregorian::GregorianDateInner;
9 use crate::calendar_arithmetic::ArithmeticDate;
10
11 impl From<chrono::NaiveDate> for Date<Gregorian> {
12 fn from(chrono: chrono::NaiveDate) -> Self {
13 use chrono::Datelike;
14
15 // chrono stores dates as year + day of year. we work with that instead
16 // of using its conversion code
17 let y = chrono.year();
18 let (m, d) = calendrical_calculations::gregorian::year_day(y, chrono.ordinal() as u16);
19
20 // chrono's MIN/MAX value are inside VALID_RD_RANGE
21 Self::from_raw(
22 GregorianDateInner(ArithmeticDate::new_unchecked(
23 AbstractGregorianYear::from_iso_year(y),
24 m,
25 d,
26 )),
27 Gregorian,
28 )
29 }
30 }
31
32 #[test]
33 fn assert_range() {
34 use crate::calendar_arithmetic::VALID_RD_RANGE;
35 use crate::types::RataDie;
36 use chrono::Datelike;
37
38 assert!(
39 VALID_RD_RANGE
40 .contains(&(RataDie::new(0) + chrono::NaiveDate::MIN.num_days_from_ce() as i64))
41 );
42 assert!(
43 VALID_RD_RANGE
44 .contains(&(RataDie::new(0) + chrono::NaiveDate::MAX.num_days_from_ce() as i64))
45 );
46 }
47
48 impl From<chrono::Weekday> for crate::types::Weekday {
49 fn from(value: chrono::Weekday) -> Self {
50 match value {
51 chrono::Weekday::Mon => Self::Monday,
52 chrono::Weekday::Tue => Self::Tuesday,
53 chrono::Weekday::Wed => Self::Wednesday,
54 chrono::Weekday::Thu => Self::Thursday,
55 chrono::Weekday::Fri => Self::Friday,
56 chrono::Weekday::Sat => Self::Saturday,
57 chrono::Weekday::Sun => Self::Sunday,
58 }
59 }
60 }