Debian Codegraph @

debian/rust-vendor/icu_calendar/src/time_crate.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<time::Date> for Date<Gregorian> {
12 fn from(other: time::Date) -> Self {
13 // time stores dates as year + day of year. we work with that instead
14 // of using its conversion code
15 let (y, day_of_year) = other.to_ordinal_date();
16 let (m, d) = calendrical_calculations::gregorian::year_day(y, day_of_year);
17
18 // time's MIN/MAX value are inside VALID_RD_RANGE
19 Self::from_raw(
20 GregorianDateInner(ArithmeticDate::new_unchecked(
21 AbstractGregorianYear::from_iso_year(y),
22 m,
23 d,
24 )),
25 Gregorian,
26 )
27 }
28 }
29
30 #[test]
31 fn assert_range() {
32 use crate::calendar_arithmetic::VALID_RD_RANGE;
33
34 let julian_day_epoch = calendrical_calculations::gregorian::fixed_from_gregorian(-4713, 11, 24);
35
36 assert!(VALID_RD_RANGE.contains(&(julian_day_epoch + time::Date::MIN.to_julian_day() as i64)));
37 assert!(VALID_RD_RANGE.contains(&(julian_day_epoch + time::Date::MAX.to_julian_day() as i64)));
38 }
39
40 impl From<time::Weekday> for crate::types::Weekday {
41 fn from(value: time::Weekday) -> Self {
42 match value {
43 time::Weekday::Monday => Self::Monday,
44 time::Weekday::Tuesday => Self::Tuesday,
45 time::Weekday::Wednesday => Self::Wednesday,
46 time::Weekday::Thursday => Self::Thursday,
47 time::Weekday::Friday => Self::Friday,
48 time::Weekday::Saturday => Self::Saturday,
49 time::Weekday::Sunday => Self::Sunday,
50 }
51 }
52 }