Skip to content

Commit

Permalink
Fall back from specific non-location to specific location (#5792)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbastian authored Nov 8, 2024
1 parent 221979f commit e3b882f
Show file tree
Hide file tree
Showing 24 changed files with 316 additions and 3,817 deletions.
42 changes: 27 additions & 15 deletions components/datetime/src/fieldset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,8 @@ impl_datetime_marker!(
);

impl_zone_marker!(
/// When a display name is unavailable, falls back to the offset format:
/// When a display name is unavailable, falls back to the localized offset format for short lengths, and
/// to the location format for long lengths:
///
/// ```
/// use icu::calendar::{Date, Time};
Expand All @@ -965,21 +966,32 @@ impl_zone_marker!(
/// use tinystr::tinystr;
/// use writeable::assert_try_writeable_eq;
///
/// // Time zone info for Europe/Istanbul in the winter
/// let zone = TimeZoneBcp47Id(tinystr!(8, "trist"))
/// .with_offset("+02".parse().ok())
/// .at_time((Date::try_new_iso(2022, 1, 29).unwrap(), Time::midnight()))
/// .with_zone_variant(ZoneVariant::Standard);
///
/// let fmt = FixedCalendarDateTimeFormatter::<Gregorian, _>::try_new(
/// &locale!("en").into(),
/// Z::short(),
/// )
/// .unwrap();
///
/// // Time zone info for America/Sao_Paulo in the winter
/// let zone = TimeZoneBcp47Id(tinystr!(8, "brsao"))
/// .with_offset("-03".parse().ok())
/// .at_time((Date::try_new_iso(2022, 1, 29).unwrap(), Time::midnight()))
/// .with_zone_variant(ZoneVariant::Standard);
/// assert_try_writeable_eq!(
/// fmt.format(&zone),
/// "GMT+2"
/// );
///
/// let fmt = FixedCalendarDateTimeFormatter::<Gregorian, _>::try_new(
/// &locale!("en").into(),
/// Z::long(),
/// )
/// .unwrap();
///
/// assert_try_writeable_eq!(
/// fmt.format(&zone),
/// "GMT-3"
/// "Türkiye Standard Time"
/// );
/// ```
///
Expand Down Expand Up @@ -1015,6 +1027,7 @@ impl_zone_marker!(
sample_length = long,
sample = "Central Daylight Time",
zone_essentials = yes,
zone_locations = yes,
zone_specific_long = yes,
zone_specific_short = yes,
metazone_periods = yes,
Expand Down Expand Up @@ -1184,21 +1197,20 @@ impl_zone_marker!(
/// use tinystr::tinystr;
/// use writeable::assert_try_writeable_eq;
///
/// // Time zone info for Europe/Istanbul
/// let zone = TimeZoneBcp47Id(tinystr!(8, "trist"))
/// .without_offset()
/// .at_time((Date::try_new_iso(2022, 1, 29).unwrap(), Time::midnight()));
///
/// let fmt = FixedCalendarDateTimeFormatter::<Gregorian, _>::try_new(
/// &locale!("en").into(),
/// V::short(),
/// )
/// .unwrap();
///
/// // Time zone info for America/Sao_Paulo in the winter
/// let zone = TimeZoneBcp47Id(tinystr!(8, "brsao"))
/// .with_offset("-03".parse().ok())
/// .at_time((Date::try_new_iso(2022, 1, 29).unwrap(), Time::midnight()))
/// .with_zone_variant(ZoneVariant::Standard);
///
/// assert_try_writeable_eq!(
/// fmt.format(&zone),
/// "Sao Paulo Time"
/// "Türkiye Time"
/// );
/// ```
///
Expand All @@ -1214,7 +1226,7 @@ impl_zone_marker!(
/// use icu::locale::locale;
/// use writeable::assert_try_writeable_eq;
///
/// let time_zone_basic = TimeZoneBcp47Id(tinystr!(8, "uschi")).with_offset("-06".parse().ok());
/// let time_zone_basic = TimeZoneBcp47Id(tinystr!(8, "uschi")).without_offset();
///
/// let formatter = FixedCalendarDateTimeFormatter::try_new(
/// &locale!("en-US").into(),
Expand Down
14 changes: 14 additions & 0 deletions components/datetime/src/format/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,20 @@ where
Ok(s) => Ok(w.write_str(s)?),
}
}
(FieldSymbol::TimeZone(fields::TimeZone::SpecificNonLocation), FieldLength::Four) => {
perform_timezone_fallback(
w,
input,
datetime_names,
fdf,
field,
&[
TimeZoneFormatterUnit::SpecificNonLocation(FieldLength::Four),
TimeZoneFormatterUnit::SpecificLocation,
TimeZoneFormatterUnit::LocalizedOffset(FieldLength::Four),
],
)?
}
(FieldSymbol::TimeZone(fields::TimeZone::SpecificNonLocation), l) => {
perform_timezone_fallback(
w,
Expand Down
1 change: 1 addition & 0 deletions components/datetime/src/format/neo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2110,6 +2110,7 @@ impl<R: DateTimeNamesMarker> RawDateTimeNames<R> {
mz_period_provider,
locale,
)?;
self.load_time_zone_location_names(locations_provider, locale)?;
}

// v
Expand Down
1 change: 0 additions & 1 deletion components/datetime/src/format/time_zone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ pub(super) enum TimeZoneFormatterUnit {
GenericNonLocation(FieldLength),
SpecificNonLocation(FieldLength),
GenericLocation,
#[allow(dead_code)]
SpecificLocation,
#[allow(dead_code)]
GenericPartialLocation(FieldLength),
Expand Down
10 changes: 10 additions & 0 deletions components/timezone/src/time_zone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ impl TimeZoneBcp47Id {
zone_variant: (),
}
}

/// Converts this [`TimeZoneBcp47Id`] into a [`TimeZoneInfo`] without an offset.
pub const fn without_offset(self) -> TimeZoneInfo<models::Base> {
TimeZoneInfo {
offset: None,
time_zone_id: self,
local_time: (),
zone_variant: (),
}
}
}

impl TimeZoneInfo<models::Base> {
Expand Down
2 changes: 1 addition & 1 deletion ffi/capi/bindings/dart/TimeZoneInfo.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ffi/capi/src/timezone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub mod ffi {
/// Clears the `offset` field.
#[diplomat::rust_link(icu::timezone::UtcOffset::offset_seconds, FnInStruct)]
#[diplomat::rust_link(icu::timezone::UtcOffset, Struct, compact)]
#[diplomat::rust_link(icu::timezone::TimeZoneBcp47Id::without_offset, FnInStruct, compact)]
pub fn clear_offset(&mut self) {
self.offset.take();
}
Expand Down

Large diffs are not rendered by default.

Loading

0 comments on commit e3b882f

Please sign in to comment.