/home/a220/proj/radnelac/src/display/julian.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::CommonWeekOfYear; |
6 | | use crate::calendar::Julian; |
7 | | use crate::calendar::ToFromCommonDate; |
8 | | use crate::calendar::ToFromOrdinalDate; |
9 | | use crate::clock::TimeOfDay; |
10 | | use crate::day_count::ToFixed; |
11 | | use crate::day_cycle::Weekday; |
12 | | use crate::display::moment::DisplayMomentItem; |
13 | | use crate::display::prelude::PresetDisplay; |
14 | | use crate::display::private::fmt_days_since_epoch; |
15 | | use crate::display::private::fmt_number; |
16 | | use crate::display::private::fmt_quarter; |
17 | | use crate::display::private::fmt_seconds_since_epoch; |
18 | | use crate::display::private::fmt_string; |
19 | | use crate::display::private::get_dict; |
20 | | use crate::display::private::DisplayItem; |
21 | | use crate::display::private::DisplayOptions; |
22 | | use crate::display::private::NumericContent; |
23 | | use crate::display::private::TextContent; |
24 | | use crate::display::text::prelude::Language; |
25 | | use std::fmt; |
26 | | |
27 | | impl DisplayItem for Julian { |
28 | 514 | fn supported_lang(lang: Language) -> bool { |
29 | 514 | get_dict(lang).julian.as_ref().is_some() |
30 | 514 | } |
31 | | |
32 | 18.6k | fn fmt_numeric(&self, n: NumericContent, opt: DisplayOptions) -> String { |
33 | 18.6k | match n { |
34 | | NumericContent::Month | NumericContent::DayOfMonth | NumericContent::Year => { |
35 | 16.3k | self.to_common_date().fmt_numeric(n, opt) |
36 | | } |
37 | 0 | NumericContent::DayOfWeek => self.convert::<Weekday>().fmt_numeric(n, opt), |
38 | 512 | NumericContent::DayOfYear => self.to_ordinal().fmt_numeric(n, opt), |
39 | | NumericContent::Hour1to12 |
40 | | | NumericContent::Hour0to23 |
41 | | | NumericContent::Minute |
42 | 768 | | NumericContent::Second => self.convert::<TimeOfDay>().fmt_numeric(n, opt), |
43 | 512 | NumericContent::SecondsSinceEpoch => fmt_seconds_since_epoch(*self, opt), |
44 | 0 | NumericContent::Quarter => fmt_quarter(*self, opt), |
45 | 512 | NumericContent::DaysSinceEpoch => fmt_days_since_epoch(*self, opt), |
46 | 0 | NumericContent::ComplementaryDay => String::from(""), |
47 | 0 | NumericContent::WeekOfYear => fmt_number(self.week_of_year() as i16, opt), |
48 | | } |
49 | 18.6k | } |
50 | | |
51 | 12.5k | fn fmt_text(&self, t: TextContent, lang: Language, opt: DisplayOptions) -> String { |
52 | 12.5k | match (t, get_dict(lang).julian.as_ref()) { |
53 | 4.09k | (TextContent::MonthName, Some(dict)) => { |
54 | 4.09k | let months: [&str; 12] = [ |
55 | 4.09k | dict.january, |
56 | 4.09k | dict.february, |
57 | 4.09k | dict.march, |
58 | 4.09k | dict.april, |
59 | 4.09k | dict.may, |
60 | 4.09k | dict.june, |
61 | 4.09k | dict.july, |
62 | 4.09k | dict.august, |
63 | 4.09k | dict.september, |
64 | 4.09k | dict.october, |
65 | 4.09k | dict.november, |
66 | 4.09k | dict.december, |
67 | 4.09k | ]; |
68 | 4.09k | let name = months[self.to_common_date().month as usize - 1]; |
69 | 4.09k | fmt_string(name, opt) |
70 | | } |
71 | 0 | (TextContent::DayOfMonthName, _) => fmt_string("", opt), |
72 | 4.09k | (TextContent::DayOfWeekName, _) => self.convert::<Weekday>().fmt_text(t, lang, opt), |
73 | | (TextContent::HalfDayName | TextContent::HalfDayAbbrev, _) => { |
74 | 0 | self.convert::<TimeOfDay>().fmt_text(t, lang, opt) |
75 | | } |
76 | 3.07k | (TextContent::EraName, Some(dict)) => { |
77 | 3.07k | if self.to_common_date().year < 0 { |
78 | 1.54k | fmt_string(dict.before_christ_full, opt) |
79 | | } else { |
80 | 1.52k | fmt_string(dict.anno_domini_full, opt) |
81 | | } |
82 | | } |
83 | 1.02k | (TextContent::EraAbbreviation, Some(dict)) => { |
84 | 1.02k | if self.to_common_date().year < 0 { |
85 | 494 | fmt_string(dict.before_christ_abr, opt) |
86 | | } else { |
87 | 530 | fmt_string(dict.anno_domini_abr, opt) |
88 | | } |
89 | | } |
90 | 256 | (_, _) => String::from(""), |
91 | | } |
92 | 12.5k | } |
93 | | } |
94 | | |
95 | | impl PresetDisplay for Julian {} |
96 | | |
97 | | impl fmt::Display for Julian { |
98 | 2.56k | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
99 | 2.56k | write!(f, "{}", self.long_date()) |
100 | 2.56k | } |
101 | | } |
102 | | |
103 | | impl DisplayMomentItem for Julian {} |
104 | | |
105 | | #[cfg(test)] |
106 | | mod tests { |
107 | | use super::*; |
108 | | |
109 | | #[test] |
110 | 1 | fn expected_languages() { |
111 | 1 | assert!(Julian::supported_lang(Language::EN)); |
112 | 1 | assert!(Julian::supported_lang(Language::FR)); |
113 | 1 | } |
114 | | } |