1use crate::calendar::ISO;
6use crate::clock::TimeOfDay;
7use crate::day_count::ToFixed;
8use crate::display::moment::DisplayMomentItem;
9use crate::display::prelude::PresetDisplay;
10use crate::display::prelude::YEAR_WEEK_DAY;
11use crate::display::private::fmt_days_since_epoch;
12use crate::display::private::fmt_number;
13use crate::display::private::fmt_quarter;
14use crate::display::private::fmt_seconds_since_epoch;
15use crate::display::private::fmt_string;
16use crate::display::private::get_dict;
17use crate::display::private::DisplayItem;
18use crate::display::private::DisplayOptions;
19use crate::display::private::NumericContent;
20use crate::display::private::TextContent;
21use crate::display::text::prelude::Language;
22use std::fmt;
23
24impl DisplayItem for ISO {
25 fn supported_lang(lang: Language) -> bool {
26 get_dict(lang).iso.as_ref().is_some()
27 }
28
29 fn fmt_numeric(&self, n: NumericContent, opt: DisplayOptions) -> String {
30 match n {
31 NumericContent::Month | NumericContent::DayOfMonth => "".to_string(),
32 NumericContent::Year => fmt_number(self.year(), opt),
33 NumericContent::DayOfWeek => fmt_number(self.day_num() as i8, opt),
34 NumericContent::DayOfYear => "".to_string(),
35 NumericContent::Hour1to12
36 | NumericContent::Hour0to23
37 | NumericContent::Minute
38 | NumericContent::Second => self.convert::<TimeOfDay>().fmt_numeric(n, opt),
39 NumericContent::SecondsSinceEpoch => fmt_seconds_since_epoch(*self, opt),
40 NumericContent::Quarter => fmt_quarter(*self, opt),
41 NumericContent::DaysSinceEpoch => fmt_days_since_epoch(*self, opt),
42 NumericContent::ComplementaryDay => "".to_string(),
43 NumericContent::WeekOfYear => fmt_number(self.week().get() as i8, opt),
44 }
45 }
46 fn fmt_text(&self, t: TextContent, lang: Language, opt: DisplayOptions) -> String {
47 match (t, get_dict(lang).iso.as_ref()) {
48 (TextContent::DayOfWeekName, _) => self.day().fmt_text(t, lang, opt),
49 (TextContent::HalfDayName | TextContent::HalfDayAbbrev, _) => {
50 self.convert::<TimeOfDay>().fmt_text(t, lang, opt)
51 }
52 (TextContent::EraName, Some(dict)) => {
53 if self.year() < 0 {
54 fmt_string(dict.before_epoch_full, opt)
55 } else {
56 fmt_string(dict.after_epoch_full, opt)
57 }
58 }
59 (TextContent::EraAbbreviation, Some(dict)) => {
60 if self.year() < 0 {
61 fmt_string(dict.before_epoch_abr, opt)
62 } else {
63 fmt_string(dict.after_epoch_abr, opt)
64 }
65 }
66 (_, _) => String::from(""),
67 }
68 }
69}
70
71impl PresetDisplay for ISO {
72 fn long_date(&self) -> String {
73 self.preset_str(Language::EN, YEAR_WEEK_DAY)
74 }
75
76 fn short_date(&self) -> String {
77 self.preset_str(Language::EN, YEAR_WEEK_DAY)
78 }
79}
80
81impl fmt::Display for ISO {
82 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
83 write!(f, "{}", self.long_date())
84 }
85}
86
87impl DisplayMomentItem for ISO {}
88
89#[cfg(test)]
90mod tests {
91 use super::*;
92 use crate::calendar::CommonDate;
93 use crate::calendar::Gregorian;
94 use crate::calendar::ToFromCommonDate;
95
96 #[test]
97 fn expected_languages() {
98 assert!(ISO::supported_lang(Language::EN));
99 }
100
101 #[test]
102 fn w1() {
103 let dg = Gregorian::try_from_common_date(CommonDate::new(2007, 1, 1)).unwrap();
104 let di = dg.convert::<ISO>();
105 let s = di.short_date();
106 assert_eq!(&s, "2007-W01-1")
107 }
108}