Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audit datetime format function #5789

Merged
merged 9 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 35 additions & 27 deletions components/datetime/src/fields/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,29 @@ impl std::error::Error for LengthError {}
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[allow(clippy::exhaustive_enums)] // part of data struct
pub enum FieldLength {
/// Typical style is 1-2 digits. For numeric-only fields.
/// Numeric: minimum digits
///
/// Text: same as [`Self::Three`]
One,
/// Typical style is 2 digits. For numeric-only fields.
TwoDigit,
/// Abbreviated (spellout) format.
Abbreviated,
/// Wide / Long / Full (spellout) format.
Wide,
/// Narrow / Long / Full (spellout) format.
Narrow,
/// Meaning is field-dependent, for patterns that are 6 characters long. Ex: a [`Weekday`](super::Weekday) pattern like
/// `EEEEEE` means "Short", but `jjjjjj` or `CCCCCC` for [`Hour`](super::Hour) may mean
/// "Numeric hour (2 digits, zero pad if needed), narrow dayPeriod if used". See the
/// [LDML documentation in UTS 35](https://unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns)
/// for more details.
/// Numeric: pad to 2 digits
///
/// Text: same as [`Self::Three`]
Two,
/// Numeric: pad to 3 digits
///
/// Text: Abbreviated format.
Three,
/// Numeric: pad to 4 digits
///
/// Text: Wide format.
Four,
/// Numeric: pad to 5 digits
///
/// Text: Narrow format.
Five,
/// Numeric: pad to 6 digits
///
/// Text: Short format.
Six,
/// FieldLength::One (numeric), but overridden with a different numbering system
NumericOverride(FieldNumericOverrides),
Expand All @@ -65,10 +73,10 @@ impl FieldLength {
pub(crate) fn idx(&self) -> u8 {
match self {
FieldLength::One => 1,
FieldLength::TwoDigit => 2,
FieldLength::Abbreviated => 3,
FieldLength::Wide => 4,
FieldLength::Narrow => 5,
FieldLength::Two => 2,
FieldLength::Three => 3,
FieldLength::Four => 4,
FieldLength::Five => 5,
FieldLength::Six => 6,
FieldLength::NumericOverride(o) => FIRST_NUMERIC_OVERRIDE
.saturating_add(*o as u8)
Expand All @@ -80,10 +88,10 @@ impl FieldLength {
pub(crate) fn from_idx(idx: u8) -> Result<Self, LengthError> {
Ok(match idx {
1 => Self::One,
2 => Self::TwoDigit,
3 => Self::Abbreviated,
4 => Self::Wide,
5 => Self::Narrow,
2 => Self::Two,
3 => Self::Three,
4 => Self::Four,
5 => Self::Five,
6 => Self::Six,
idx if (FIRST_NUMERIC_OVERRIDE..=LAST_NUMERIC_OVERRIDE).contains(&idx) => {
Self::NumericOverride((idx - FIRST_NUMERIC_OVERRIDE).try_into()?)
Expand All @@ -96,10 +104,10 @@ impl FieldLength {
pub(crate) fn to_len(self) -> usize {
match self {
FieldLength::One => 1,
FieldLength::TwoDigit => 2,
FieldLength::Abbreviated => 3,
FieldLength::Wide => 4,
FieldLength::Narrow => 5,
FieldLength::Two => 2,
FieldLength::Three => 3,
FieldLength::Four => 4,
FieldLength::Five => 5,
FieldLength::Six => 6,
FieldLength::NumericOverride(o) => FIRST_NUMERIC_OVERRIDE as usize + o as usize,
}
Expand All @@ -111,7 +119,7 @@ impl FieldLength {
/// This function maps field lengths 1 and 2 to field length 3.
pub(crate) fn numeric_to_abbr(self) -> Self {
match self {
FieldLength::One | FieldLength::TwoDigit => FieldLength::Abbreviated,
FieldLength::One | FieldLength::Two => FieldLength::Three,
other => other,
}
}
Expand Down
16 changes: 8 additions & 8 deletions components/datetime/src/fields/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,21 @@ mod test {
fn test_field_as_ule() {
let samples = [
(
Field::from((FieldSymbol::Minute, FieldLength::TwoDigit)),
[FieldSymbol::Minute.idx(), FieldLength::TwoDigit.idx()],
Field::from((FieldSymbol::Minute, FieldLength::Two)),
[FieldSymbol::Minute.idx(), FieldLength::Two.idx()],
),
(
Field::from((FieldSymbol::Year(Year::Calendar), FieldLength::Wide)),
Field::from((FieldSymbol::Year(Year::Calendar), FieldLength::Four)),
[
FieldSymbol::Year(Year::Calendar).idx(),
FieldLength::Wide.idx(),
FieldLength::Four.idx(),
],
),
(
Field::from((FieldSymbol::Year(Year::Cyclic), FieldLength::Wide)),
Field::from((FieldSymbol::Year(Year::Cyclic), FieldLength::Four)),
[
FieldSymbol::Year(Year::Cyclic).idx(),
FieldLength::Wide.idx(),
FieldLength::Four.idx(),
],
),
(
Expand All @@ -151,13 +151,13 @@ mod test {
fn test_field_ule() {
let samples = [(
[
Field::from((FieldSymbol::Year(Year::Calendar), FieldLength::Wide)),
Field::from((FieldSymbol::Year(Year::Calendar), FieldLength::Four)),
Field::from((FieldSymbol::Second(Second::MillisInDay), FieldLength::One)),
],
[
[
FieldSymbol::Year(Year::Calendar).idx(),
FieldLength::Wide.idx(),
FieldLength::Four.idx(),
],
[
FieldSymbol::Second(Second::MillisInDay).idx(),
Expand Down
10 changes: 5 additions & 5 deletions components/datetime/src/fields/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,10 +548,10 @@ impl LengthType for Month {
match length {
FieldLength::One => TextOrNumeric::Numeric,
FieldLength::NumericOverride(_) => TextOrNumeric::Numeric,
FieldLength::TwoDigit => TextOrNumeric::Numeric,
FieldLength::Abbreviated => TextOrNumeric::Text,
FieldLength::Wide => TextOrNumeric::Text,
FieldLength::Narrow => TextOrNumeric::Text,
FieldLength::Two => TextOrNumeric::Numeric,
FieldLength::Three => TextOrNumeric::Text,
FieldLength::Four => TextOrNumeric::Text,
FieldLength::Five => TextOrNumeric::Text,
FieldLength::Six => TextOrNumeric::Text,
}
}
Expand Down Expand Up @@ -690,7 +690,7 @@ impl LengthType for Weekday {
match self {
Self::Format => TextOrNumeric::Text,
Self::Local | Self::StandAlone => match length {
FieldLength::One | FieldLength::TwoDigit => TextOrNumeric::Numeric,
FieldLength::One | FieldLength::Two => TextOrNumeric::Numeric,
_ => TextOrNumeric::Text,
},
}
Expand Down
3 changes: 1 addition & 2 deletions components/datetime/src/fieldset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use icu_calendar::{
types::{
DayOfMonth, IsoHour, IsoMinute, IsoSecond, IsoWeekday, MonthInfo, NanoSecond, YearInfo,
},
AnyCalendarKind, Date, Iso, Time,
Date, Iso, Time,
};
use icu_provider::marker::NeverMarker;
use icu_timezone::{TimeZoneBcp47Id, UtcOffset, ZoneVariant};
Expand Down Expand Up @@ -261,7 +261,6 @@ macro_rules! impl_date_or_calendar_period_marker {
type DayOfMonthInput = datetime_marker_helper!(@input/day_of_month, $($day_of_month_yes)?);
type DayOfYearInput = datetime_marker_helper!(@input/day_of_year, $($day_of_year_yes)?);
type DayOfWeekInput = datetime_marker_helper!(@input/day_of_week, $($day_of_week_yes)?);
type AnyCalendarKindInput = datetime_marker_helper!(@input/any_calendar_kind, $($any_calendar_kind_yes)?);
}
impl<C: CldrCalendar> TypedDateDataMarkers<C> for $type {
type DateSkeletonPatternsV1Marker = datetime_marker_helper!(@dates/typed, yes);
Expand Down
Loading