Skip to content

Commit 7997d1c

Browse files
Simplify EraYear (#6443)
This removes the duplication between `standard_era` and `formatting_era`. Effectively, whenever `formatting_era` is `FormattingEra::Code`, it is the same code as `standard_era`, so it could just be `Option<(u8, TinyStr16)>`. But the fallback era name isn't really needed, as it's always the same as code up to capitalization, and we don't really care about capitalization in gigo paths. The new `era_index` field can be documented without deferring to whatever datagen does.
1 parent 3ce2ca7 commit 7997d1c

File tree

25 files changed

+124
-193
lines changed

25 files changed

+124
-193
lines changed

components/calendar/src/any_calendar.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ define_preferences!(
4444
///
4545
/// There are many ways of constructing an AnyCalendar'd date:
4646
/// ```
47-
/// use icu::calendar::{AnyCalendar, AnyCalendarKind, Date, cal::Japanese, types::{Era, MonthCode}};
47+
/// use icu::calendar::{AnyCalendar, AnyCalendarKind, Date, cal::Japanese, types::MonthCode};
4848
/// use icu::locale::locale;
4949
/// use tinystr::tinystr;
5050
/// # use std::rc::Rc;

components/calendar/src/cal/buddhist.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ impl Calendar for Buddhist {
118118
/// The calendar-specific year represented by `date`
119119
fn year_info(&self, date: &Self::DateInner) -> Self::Year {
120120
types::EraYear {
121-
standard_era: tinystr!(16, "be").into(),
122-
formatting_era: types::FormattingEra::Index(0, tinystr!(16, "BE")),
121+
era: tinystr!(16, "be"),
122+
era_index: Some(0),
123123
year: self.extended_year(date),
124124
ambiguity: types::YearAmbiguity::CenturyRequired,
125125
}

components/calendar/src/cal/coptic.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,15 @@ impl Calendar for Coptic {
165165
let year = self.extended_year(date);
166166
if year > 0 {
167167
types::EraYear {
168-
standard_era: tinystr!(16, "am").into(),
169-
formatting_era: types::FormattingEra::Index(1, tinystr!(16, "AM")),
168+
era: tinystr!(16, "am"),
169+
era_index: Some(1),
170170
year,
171171
ambiguity: types::YearAmbiguity::CenturyRequired,
172172
}
173173
} else {
174174
types::EraYear {
175-
standard_era: tinystr!(16, "bd").into(),
176-
formatting_era: types::FormattingEra::Index(0, tinystr!(16, "BD")),
175+
era: tinystr!(16, "bd"),
176+
era_index: Some(0),
177177
year: 1 - year,
178178
ambiguity: types::YearAmbiguity::EraAndCenturyRequired,
179179
}

components/calendar/src/cal/ethiopian.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,15 @@ impl Calendar for Ethiopian {
199199
let year = date.0.year;
200200
if self.0 || year <= INCARNATION_OFFSET {
201201
types::EraYear {
202-
standard_era: tinystr!(16, "aa").into(),
203-
formatting_era: types::FormattingEra::Index(0, tinystr!(16, "AA")),
202+
era: tinystr!(16, "aa"),
203+
era_index: Some(0),
204204
year,
205205
ambiguity: types::YearAmbiguity::CenturyRequired,
206206
}
207207
} else {
208208
types::EraYear {
209-
standard_era: tinystr!(16, "am").into(),
210-
formatting_era: types::FormattingEra::Index(1, tinystr!(16, "AM")),
209+
era: tinystr!(16, "am"),
210+
era_index: Some(1),
211211
year: year - INCARNATION_OFFSET,
212212
ambiguity: types::YearAmbiguity::CenturyRequired,
213213
}

components/calendar/src/cal/gregorian.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ impl Calendar for Gregorian {
110110
let extended_year = self.extended_year(date);
111111
if extended_year > 0 {
112112
types::EraYear {
113-
standard_era: tinystr!(16, "ce").into(),
114-
formatting_era: types::FormattingEra::Index(1, tinystr!(16, "CE")),
113+
era: tinystr!(16, "ce"),
114+
era_index: Some(1),
115115
year: extended_year,
116116
ambiguity: match extended_year {
117117
..=999 => types::YearAmbiguity::EraAndCenturyRequired,
@@ -122,8 +122,8 @@ impl Calendar for Gregorian {
122122
}
123123
} else {
124124
types::EraYear {
125-
standard_era: tinystr!(16, "bce").into(),
126-
formatting_era: types::FormattingEra::Index(0, tinystr!(16, "BCE")),
125+
era: tinystr!(16, "bce"),
126+
era_index: Some(0),
127127
year: 1_i32.saturating_sub(extended_year),
128128
ambiguity: types::YearAmbiguity::EraAndCenturyRequired,
129129
}
@@ -188,7 +188,6 @@ mod test {
188188
use calendrical_calculations::rata_die::RataDie;
189189

190190
use super::*;
191-
use types::Era;
192191

193192
#[derive(Debug)]
194193
struct TestCase {
@@ -197,7 +196,7 @@ mod test {
197196
iso_month: u8,
198197
iso_day: u8,
199198
expected_year: i32,
200-
expected_era: Era,
199+
expected_era: &'static str,
201200
expected_month: u8,
202201
expected_day: u8,
203202
}
@@ -208,7 +207,7 @@ mod test {
208207
assert_eq!(greg_date_from_rd.era_year().year, case.expected_year,
209208
"Failed year check from RD: {case:?}\nISO: {iso_from_rd:?}\nGreg: {greg_date_from_rd:?}");
210209
assert_eq!(
211-
greg_date_from_rd.era_year().standard_era,
210+
greg_date_from_rd.era_year().era,
212211
case.expected_era,
213212
"Failed era check from RD: {case:?}\nISO: {iso_from_rd:?}\nGreg: {greg_date_from_rd:?}"
214213
);
@@ -242,7 +241,7 @@ mod test {
242241
iso_month: 1,
243242
iso_day: 1,
244243
expected_year: 1,
245-
expected_era: Era(tinystr!(16, "ce")),
244+
expected_era: "ce",
246245
expected_month: 1,
247246
expected_day: 1,
248247
},
@@ -252,7 +251,7 @@ mod test {
252251
iso_month: 6,
253252
iso_day: 30,
254253
expected_year: 1,
255-
expected_era: Era(tinystr!(16, "ce")),
254+
expected_era: "ce",
256255
expected_month: 6,
257256
expected_day: 30,
258257
},
@@ -262,7 +261,7 @@ mod test {
262261
iso_month: 2,
263262
iso_day: 29,
264263
expected_year: 4,
265-
expected_era: Era(tinystr!(16, "ce")),
264+
expected_era: "ce",
266265
expected_month: 2,
267266
expected_day: 29,
268267
},
@@ -272,7 +271,7 @@ mod test {
272271
iso_month: 9,
273272
iso_day: 5,
274273
expected_year: 4,
275-
expected_era: Era(tinystr!(16, "ce")),
274+
expected_era: "ce",
276275
expected_month: 9,
277276
expected_day: 5,
278277
},
@@ -282,7 +281,7 @@ mod test {
282281
iso_month: 3,
283282
iso_day: 1,
284283
expected_year: 100,
285-
expected_era: Era(tinystr!(16, "ce")),
284+
expected_era: "ce",
286285
expected_month: 3,
287286
expected_day: 1,
288287
},
@@ -305,7 +304,7 @@ mod test {
305304
iso_month: 12,
306305
iso_day: 31,
307306
expected_year: 1,
308-
expected_era: Era(tinystr!(16, "bce")),
307+
expected_era: "bce",
309308
expected_month: 12,
310309
expected_day: 31,
311310
},
@@ -315,7 +314,7 @@ mod test {
315314
iso_month: 1,
316315
iso_day: 1,
317316
expected_year: 1,
318-
expected_era: Era(tinystr!(16, "bce")),
317+
expected_era: "bce",
319318
expected_month: 1,
320319
expected_day: 1,
321320
},
@@ -325,7 +324,7 @@ mod test {
325324
iso_month: 12,
326325
iso_day: 31,
327326
expected_year: 2,
328-
expected_era: Era(tinystr!(16, "bce")),
327+
expected_era: "bce",
329328
expected_month: 12,
330329
expected_day: 31,
331330
},
@@ -335,7 +334,7 @@ mod test {
335334
iso_month: 12,
336335
iso_day: 31,
337336
expected_year: 5,
338-
expected_era: Era(tinystr!(16, "bce")),
337+
expected_era: "bce",
339338
expected_month: 12,
340339
expected_day: 31,
341340
},
@@ -345,7 +344,7 @@ mod test {
345344
iso_month: 1,
346345
iso_day: 1,
347346
expected_year: 5,
348-
expected_era: Era(tinystr!(16, "bce")),
347+
expected_era: "bce",
349348
expected_month: 1,
350349
expected_day: 1,
351350
},

components/calendar/src/cal/hebrew.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ impl Calendar for Hebrew {
261261

262262
fn year_info(&self, date: &Self::DateInner) -> Self::Year {
263263
types::EraYear {
264-
formatting_era: types::FormattingEra::Index(0, tinystr!(16, "AM")),
265-
standard_era: tinystr!(16, "am").into(),
264+
era_index: Some(0),
265+
era: tinystr!(16, "am"),
266266
year: self.extended_year(date),
267267
ambiguity: types::YearAmbiguity::CenturyRequired,
268268
}
@@ -489,13 +489,13 @@ mod tests {
489489
let greg_date = Date::try_new_gregorian(-5000, 1, 1).unwrap();
490490
let greg_year = greg_date.era_year();
491491
assert_eq!(greg_date.inner.0 .0.year, -5000);
492-
assert_eq!(greg_year.standard_era.0, "bce");
492+
assert_eq!(greg_year.era, "bce");
493493
// In Gregorian, era year is 1 - extended year
494494
assert_eq!(greg_year.year, 5001);
495495
let hebr_date = greg_date.to_calendar(Hebrew);
496496
let hebr_year = hebr_date.era_year();
497497
assert_eq!(hebr_date.inner.0.year.value, -1240);
498-
assert_eq!(hebr_year.standard_era.0, "am");
498+
assert_eq!(hebr_year.era, "am");
499499
// In Hebrew, there is no inverse era, so negative extended years are negative era years
500500
assert_eq!(hebr_year.year, -1240);
501501
}

components/calendar/src/cal/hijri.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ use tinystr::tinystr;
3636

3737
fn era_year(year: i32) -> EraYear {
3838
types::EraYear {
39-
standard_era: types::Era(tinystr!(16, "ah")),
40-
formatting_era: types::FormattingEra::Index(0, tinystr!(16, "AH")),
39+
era: tinystr!(16, "ah"),
40+
era_index: Some(0),
4141
year,
4242
ambiguity: types::YearAmbiguity::CenturyRequired,
4343
}

components/calendar/src/cal/indian.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ impl Calendar for Indian {
184184

185185
fn year_info(&self, date: &Self::DateInner) -> Self::Year {
186186
types::EraYear {
187-
formatting_era: types::FormattingEra::Index(0, tinystr!(16, "Saka")),
188-
standard_era: tinystr!(16, "saka").into(),
187+
era_index: Some(0),
188+
era: tinystr!(16, "saka"),
189189
year: self.extended_year(date),
190190
ambiguity: types::YearAmbiguity::CenturyRequired,
191191
}

components/calendar/src/cal/iso.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ impl Calendar for Iso {
145145

146146
fn year_info(&self, date: &Self::DateInner) -> Self::Year {
147147
types::EraYear {
148-
formatting_era: types::FormattingEra::Index(0, tinystr!(16, "")),
149-
standard_era: tinystr!(16, "default").into(),
148+
era_index: Some(0),
149+
era: tinystr!(16, "default"),
150150
year: self.extended_year(date),
151151
ambiguity: types::YearAmbiguity::Unambiguous,
152152
}

components/calendar/src/cal/japanese.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//!
77
//! ```rust
88
//! use icu::calendar::cal::Japanese;
9-
//! use icu::calendar::{types::Era, Date};
9+
//! use icu::calendar::Date;
1010
//! use tinystr::tinystr;
1111
//!
1212
//! let japanese_calendar = Japanese::new();
@@ -18,10 +18,7 @@
1818
//! assert_eq!(date_japanese.era_year().year, 45);
1919
//! assert_eq!(date_japanese.month().ordinal, 1);
2020
//! assert_eq!(date_japanese.day_of_month().0, 2);
21-
//! assert_eq!(
22-
//! date_japanese.era_year().standard_era.0,
23-
//! "showa"
24-
//! );
21+
//! assert_eq!(date_japanese.era_year().era, "showa");
2522
//! ```
2623
2724
use crate::cal::iso::{Iso, IsoDateInner};
@@ -251,8 +248,8 @@ impl Calendar for Japanese {
251248

252249
fn year_info(&self, date: &Self::DateInner) -> Self::Year {
253250
types::EraYear {
254-
formatting_era: types::FormattingEra::Code(date.era.into()),
255-
standard_era: date.era.into(),
251+
era: date.era,
252+
era_index: None,
256253
year: date.adjusted_year,
257254
ambiguity: types::YearAmbiguity::CenturyRequired,
258255
}
@@ -414,7 +411,7 @@ impl Date<Japanese> {
414411
/// Date::try_new_japanese_with_calendar(era, 14, 1, 2, japanese_calendar)
415412
/// .expect("Constructing a date should succeed");
416413
///
417-
/// assert_eq!(date.era_year().standard_era.0, era);
414+
/// assert_eq!(date.era_year().era, era);
418415
/// assert_eq!(date.era_year().year, 14);
419416
/// assert_eq!(date.month().ordinal, 1);
420417
/// assert_eq!(date.day_of_month().0, 2);
@@ -479,7 +476,7 @@ impl Date<JapaneseExtended> {
479476
/// )
480477
/// .expect("Constructing a date should succeed");
481478
///
482-
/// assert_eq!(date.era_year().standard_era.0, era);
479+
/// assert_eq!(date.era_year().era, era);
483480
/// assert_eq!(date.era_year().year, 7);
484481
/// assert_eq!(date.month().ordinal, 1);
485482
/// assert_eq!(date.day_of_month().0, 2);
@@ -754,7 +751,7 @@ mod tests {
754751
);
755752

756753
// Extra coverage for https://github.com/unicode-org/icu4x/issues/4968
757-
assert_eq!(reconstructed.era_year().standard_era.0, era);
754+
assert_eq!(reconstructed.era_year().era, era);
758755
assert_eq!(reconstructed.era_year().year, year);
759756
}
760757

0 commit comments

Comments
 (0)