Skip to main content

icu_locale_core/preferences/extensions/unicode/keywords/
calendar.rs

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#![allow(non_snake_case)]
6
7use crate::extensions::unicode::{Value, value};
8use crate::preferences::extensions::unicode::enum_keyword;
9use crate::preferences::extensions::unicode::errors::PreferencesParseError;
10use crate::subtags::{Subtag, subtag};
11
12enum_keyword!(
13    /// Hijri Calendar sub-type
14    ///
15    /// The list is based on [`CLDR Calendars`](https://github.com/unicode-org/cldr/blob/main/common/bcp47/calendar.xml)
16    HijriCalendarAlgorithm {
17        /// Hijri calendar, Umm al-Qura
18        Umalqura,
19        /// Hijri calendar, tabular (intercalary years \[2,5,7,10,13,16,18,21,24,26,29] - astronomical epoch)
20        Tbla,
21        /// Hijri calendar, tabular (intercalary years \[2,5,7,10,13,16,18,21,24,26,29] - civil epoch)
22        Civil,
23        /// Hijri calendar, Saudi Arabia sighting
24        Rgsa
25    }
26);
27
28/// Handles aliases present in `v`. If found, returns a [`CalendarAlgorithm`], else returns None
29fn handle_aliases(v: &Value) -> Option<CalendarAlgorithm> {
30    if *v == value!("islamicc") {
31        return Some(CalendarAlgorithm::Hijri(Some(
32            HijriCalendarAlgorithm::Civil,
33        )));
34    } else if v.as_subtags_slice() == [subtag!("ethiopic"), subtag!("amete"), subtag!("alem")] {
35        return Some(CalendarAlgorithm::Ethioaa);
36    }
37    None
38}
39
40enum_keyword!(
41    /// A Unicode Calendar Identifier defines a type of calendar.
42    ///
43    /// This selects calendar-specific data within a locale used for formatting and parsing,
44    /// such as date/time symbols and patterns; it also selects supplemental calendarData used
45    /// for calendrical calculations. The value can affect the computation of the first day of the week.
46    ///
47    /// The valid values are listed in [LDML](https://unicode.org/reports/tr35/#UnicodeCalendarIdentifier).
48    CalendarAlgorithm {
49        /// Thai Buddhist calendar (same as Gregorian except for the year)
50        ("buddhist" => Buddhist),
51        /// Traditional Chinese calendar
52        ("chinese" => Chinese),
53        /// Coptic calendar
54        ("coptic" => Coptic),
55        /// Traditional Korean calendar
56        ("dangi" => Dangi),
57        /// Ethiopic calendar, Amete Alem (epoch approx. 5493 B.C.E)
58        ("ethioaa" => Ethioaa),
59        /// Ethiopic calendar, Amete Mihret (epoch approx, 8 C.E.)
60        ("ethiopic" => Ethiopic),
61        /// Gregorian calendar
62        ("gregory" => Gregory),
63        /// Traditional Hebrew calendar
64        ("hebrew" => Hebrew),
65        /// Indian calendar
66        ("indian" => Indian),
67        /// Hijri calendar
68        ("islamic" => Hijri(HijriCalendarAlgorithm) {
69             ("umalqura" => Umalqura),
70             ("tbla" => Tbla),
71             ("civil" => Civil),
72             ("rgsa" => Rgsa)
73        }),
74        /// ISO calendar (Gregorian calendar using the ISO 8601 calendar week rules)
75        ("iso8601" => Iso8601),
76        /// Japanese Imperial calendar
77        ("japanese" => Japanese),
78        /// Persian calendar
79        ("persian" => Persian),
80        /// Republic of China calendar
81        ("roc" => Roc)
82}, "ca", s, if let Some(s) = handle_aliases(s) { return Ok(s) });
83
84#[test]
85fn test_calendar_aliases() {
86    fn test(s: &str, expectation: CalendarAlgorithm) {
87        let v = Value::try_from_str(s).unwrap();
88        let parsed = CalendarAlgorithm::try_from(&v).unwrap();
89        assert_eq!(parsed, expectation, "{s} must parse to {expectation:?}");
90    }
91
92    test("ethiopic-amete-alem", CalendarAlgorithm::Ethioaa);
93    test("ethiopic", CalendarAlgorithm::Ethiopic);
94    test("ethioaa", CalendarAlgorithm::Ethioaa);
95    test(
96        "islamicc",
97        CalendarAlgorithm::Hijri(Some(HijriCalendarAlgorithm::Civil)),
98    );
99    test(
100        "islamic-civil",
101        CalendarAlgorithm::Hijri(Some(HijriCalendarAlgorithm::Civil)),
102    );
103    test(
104        "islamic-rgsa",
105        CalendarAlgorithm::Hijri(Some(HijriCalendarAlgorithm::Rgsa)),
106    );
107    test("islamic", CalendarAlgorithm::Hijri(None));
108}