Skip to content

Commit

Permalink
Add cyclic year to FormattableYear (#3581)
Browse files Browse the repository at this point in the history
  • Loading branch information
atcupps authored Jun 28, 2023
1 parent 243f2c6 commit 03e4e31
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions components/calendar/src/buddhist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ fn iso_year_as_buddhist(year: i32) -> types::FormattableYear {
types::FormattableYear {
era: types::Era(tinystr!(16, "be")),
number: buddhist_year,
cyclic: None,
related_iso: None,
}
}
2 changes: 2 additions & 0 deletions components/calendar/src/coptic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,14 @@ fn year_as_coptic(year: i32) -> types::FormattableYear {
types::FormattableYear {
era: types::Era(tinystr!(16, "ad")),
number: year,
cyclic: None,
related_iso: None,
}
} else {
types::FormattableYear {
era: types::Era(tinystr!(16, "bd")),
number: 1 - year,
cyclic: None,
related_iso: None,
}
}
Expand Down
3 changes: 3 additions & 0 deletions components/calendar/src/ethiopian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,18 +292,21 @@ impl Ethiopian {
types::FormattableYear {
era: types::Era(tinystr!(16, "mundi")),
number: year + AMETE_ALEM_OFFSET,
cyclic: None,
related_iso: None,
}
} else if year > 0 {
types::FormattableYear {
era: types::Era(tinystr!(16, "incar")),
number: year,
cyclic: None,
related_iso: None,
}
} else {
types::FormattableYear {
era: types::Era(tinystr!(16, "pre-incar")),
number: 1 - year,
cyclic: None,
related_iso: None,
}
}
Expand Down
2 changes: 2 additions & 0 deletions components/calendar/src/gregorian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,14 @@ pub(crate) fn year_as_gregorian(year: i32) -> types::FormattableYear {
types::FormattableYear {
era: types::Era(tinystr!(16, "ce")),
number: year,
cyclic: None,
related_iso: None,
}
} else {
types::FormattableYear {
era: types::Era(tinystr!(16, "bce")),
number: 1_i32.saturating_sub(year),
cyclic: None,
related_iso: None,
}
}
Expand Down
3 changes: 3 additions & 0 deletions components/calendar/src/indian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ impl Calendar for Indian {
types::FormattableYear {
era: types::Era(tinystr!(16, "saka")),
number: date.0.year,
cyclic: None,
related_iso: None,
}
}
Expand All @@ -204,11 +205,13 @@ impl Calendar for Indian {
let prev_year = types::FormattableYear {
era: types::Era(tinystr!(16, "saka")),
number: date.0.year - 1,
cyclic: None,
related_iso: None,
};
let next_year = types::FormattableYear {
era: types::Era(tinystr!(16, "saka")),
number: date.0.year + 1,
cyclic: None,
related_iso: None,
};
types::DayOfYearInfo {
Expand Down
1 change: 1 addition & 0 deletions components/calendar/src/iso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ impl Iso {
types::FormattableYear {
era: types::Era(tinystr!(16, "default")),
number: year,
cyclic: None,
related_iso: None,
}
}
Expand Down
1 change: 1 addition & 0 deletions components/calendar/src/japanese.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ impl Calendar for Japanese {
types::FormattableYear {
era: types::Era(date.era),
number: date.adjusted_year,
cyclic: None,
related_iso: None,
}
}
Expand Down
1 change: 1 addition & 0 deletions components/calendar/src/persian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ impl Persian {
types::FormattableYear {
era: types::Era(tinystr!(16, "ah")),
number: year,
cyclic: None,
related_iso: None,
}
}
Expand Down
10 changes: 7 additions & 3 deletions components/calendar/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ impl FromStr for Era {

/// Representation of a formattable year.
///
/// More fields may be added in the future, for things like
/// the cyclic or extended year
/// More fields may be added in the future for things like extended year
#[derive(Copy, Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct FormattableYear {
Expand All @@ -49,6 +48,10 @@ pub struct FormattableYear {
/// The year number in the current era (usually 1-based).
pub number: i32,

/// The year in the current cycle for cyclic calendars;
/// can be set to None for non-cyclic calendars
pub cyclic: Option<i32>,

/// The related ISO year. This is normally the ISO (proleptic Gregorian) year having the greatest
/// overlap with the calendar year. It is used in certain date formatting patterns.
///
Expand All @@ -62,10 +65,11 @@ impl FormattableYear {
///
/// Other fields can be set mutably after construction
/// as needed
pub fn new(era: Era, number: i32) -> Self {
pub fn new(era: Era, number: i32, cyclic: Option<i32>) -> Self {
Self {
era,
number,
cyclic,
related_iso: None,
}
}
Expand Down
4 changes: 2 additions & 2 deletions components/datetime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ mod tests {
check_size_of!(6744 | 5408, TypedDateTimeFormatter::<Gregorian>);

check_size_of!(88, DateTimeError);
check_size_of!(176, FormattedDateTime);
check_size_of!(200, FormattedDateTime);
check_size_of!(16, FormattedTimeZone::<CustomTimeZone>);
check_size_of!(160, FormattedZonedDateTime);
check_size_of!(184, FormattedZonedDateTime);
check_size_of!(13, DateTimeFormatterOptions);

type DP<M> = DataPayload<M>;
Expand Down

0 comments on commit 03e4e31

Please sign in to comment.