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