Skip to content

Commit

Permalink
Add "GMT+?" field to data struct, fall back to it (#5694)
Browse files Browse the repository at this point in the history
This will be added to CLDR in the future.
  • Loading branch information
robertbastian authored Oct 16, 2024
1 parent ad99dd4 commit 6279b08
Show file tree
Hide file tree
Showing 46 changed files with 337 additions and 557 deletions.
6 changes: 3 additions & 3 deletions components/datetime/src/provider/time_zones.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ pub struct TimeZoneEssentialsV1<'data> {
/// The localized zero-offset format.
#[cfg_attr(feature = "serde", serde(borrow))]
pub offset_zero: Cow<'data, str>,
/// The localized unknown-offset format.
#[cfg_attr(feature = "serde", serde(borrow))]
pub offset_unknown: Cow<'data, str>,
}

/// An ICU4X mapping to the CLDR timeZoneNames exemplar cities.
Expand All @@ -82,9 +85,6 @@ pub struct LocationsV1<'data> {
/// Per-zone location display name
#[cfg_attr(feature = "serde", serde(borrow))]
pub locations: ZeroMap<'data, TimeZoneBcp47Id, str>,
/// The display name for an unknown time zone. This is not combined with a pattern.
#[cfg_attr(feature = "serde", serde(borrow))]
pub unknown: Cow<'data, str>,
/// The format string for a region's generic time.
#[cfg_attr(
feature = "serde",
Expand Down
62 changes: 35 additions & 27 deletions components/datetime/src/time_zone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,18 @@ impl ResolvedNeoTimeZoneSkeleton {
self.to_field().length,
)),
Some(TimeZoneFormatterUnit::GenericLocation),
None,
Some(TimeZoneFormatterUnit::LocalizedOffset(
self.to_field().length,
)),
],
// 'VVVV'
ResolvedNeoTimeZoneSkeleton::Location => {
[Some(TimeZoneFormatterUnit::GenericLocation), None, None]
}
ResolvedNeoTimeZoneSkeleton::Location => [
Some(TimeZoneFormatterUnit::GenericLocation),
Some(TimeZoneFormatterUnit::LocalizedOffset(
self.to_field().length,
)),
None,
],
// `O`, `OOOO`, `ZZZZ`
ResolvedNeoTimeZoneSkeleton::OffsetShort | ResolvedNeoTimeZoneSkeleton::OffsetLong => [
Some(TimeZoneFormatterUnit::LocalizedOffset(
Expand Down Expand Up @@ -459,15 +465,16 @@ impl FormatTimeZone for LocalizedOffsetFormat {
data_payloads: TimeZoneDataPayloadsBorrowed,
fdf: Option<&FixedDecimalFormatter>,
) -> Result<Result<(), FormatTimeZoneError>, fmt::Error> {
let Some(offset) = input.offset else {
return Ok(Err(FormatTimeZoneError::MissingInputField("zone_offset")));
};
let Some(essentials) = data_payloads.essentials else {
return Ok(Err(FormatTimeZoneError::MissingZoneSymbols));
};
let Some(fdf) = fdf else {
return Ok(Err(FormatTimeZoneError::MissingFixedDecimalFormatter));
};
let Some(offset) = input.offset else {
sink.write_str(&essentials.offset_unknown)?;
return Ok(Ok(()));
};
Ok(if offset.is_zero() {
sink.write_str(&essentials.offset_zero)?;
Ok(())
Expand Down Expand Up @@ -554,14 +561,14 @@ impl FormatTimeZone for GenericLocationFormat {
return Ok(Err(FormatTimeZoneError::MissingZoneSymbols));
};

if let Some(location) = locations.locations.get(&time_zone_id) {
locations
.pattern_generic
.interpolate([location])
.write_to(sink)?;
} else {
sink.write_str(&locations.unknown)?;
}
let Some(location) = locations.locations.get(&time_zone_id) else {
return Ok(Err(FormatTimeZoneError::Fallback));
};

locations
.pattern_generic
.interpolate([location])
.write_to(sink)?;

Ok(Ok(()))
}
Expand Down Expand Up @@ -591,19 +598,19 @@ impl FormatTimeZone for SpecificLocationFormat {
return Ok(Err(FormatTimeZoneError::MissingZoneSymbols));
};

if let Some(location) = locations.locations.get(&time_zone_id) {
if zone_variant == ZoneVariant::daylight() {
&locations.pattern_daylight
} else if zone_variant == ZoneVariant::standard() {
&locations.pattern_standard
} else {
&locations.pattern_generic
}
.interpolate([location])
.write_to(sink)?;
let Some(location) = locations.locations.get(&time_zone_id) else {
return Ok(Err(FormatTimeZoneError::Fallback));
};

if zone_variant == ZoneVariant::daylight() {
&locations.pattern_daylight
} else if zone_variant == ZoneVariant::standard() {
&locations.pattern_standard
} else {
sink.write_str(&locations.unknown)?;
&locations.pattern_generic
}
.interpolate([location])
.write_to(sink)?;

Ok(Ok(()))
}
Expand Down Expand Up @@ -691,7 +698,8 @@ impl FormatTimeZone for Iso8601Format {
_fdf: Option<&FixedDecimalFormatter>,
) -> Result<Result<(), FormatTimeZoneError>, fmt::Error> {
let Some(offset) = input.offset else {
return Ok(Err(FormatTimeZoneError::MissingInputField("zone_offset")));
sink.write_str("+?")?;
return Ok(Ok(()));
};
self.format_infallible(sink, offset).map(|()| Ok(()))
}
Expand Down
10 changes: 2 additions & 8 deletions components/datetime/tests/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,7 @@ fn test_time_zone_format_offset_seconds() {

#[test]
fn test_time_zone_format_offset_not_set_debug_assert_panic() {
use icu_datetime::{
neo_marker::NeoTimeZoneOffsetMarker, neo_skeleton::NeoSkeletonLength, DateTimeWriteError,
};
use icu_datetime::{neo_marker::NeoTimeZoneOffsetMarker, neo_skeleton::NeoSkeletonLength};

let time_zone = TimeZoneInfo {
time_zone_id: TimeZoneIdMapper::new()
Expand All @@ -502,11 +500,7 @@ fn test_time_zone_format_offset_not_set_debug_assert_panic() {
NeoTimeZoneOffsetMarker::with_length(NeoSkeletonLength::Medium),
)
.unwrap();
assert_try_writeable_eq!(
tzf.format(&time_zone),
"{O}",
Err(DateTimeWriteError::MissingInputField("zone_offset"))
);
assert_try_writeable_eq!(tzf.format(&time_zone), "GMT+?",);
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"output": {
"values": {
"en": "Tuesday, January 21, 2020, 08:25:07 Unknown City Time"
"en": "Tuesday, January 21, 2020, 08:25:07 GMT+5"
}
}
},
Expand Down Expand Up @@ -51,7 +51,7 @@
},
"output": {
"values": {
"en": "Tuesday, January 21, 2020, 08:25:07 Unknown City Time"
"en": "Tuesday, January 21, 2020, 08:25:07 GMT+5"
}
}
},
Expand Down
38 changes: 10 additions & 28 deletions components/datetime/tests/patterns/tests/time_zones.json
Original file line number Diff line number Diff line change
Expand Up @@ -987,13 +987,16 @@
"z",
"zz",
"zzz",
"v",
"O"
],
"configs": [],
"expected": ["GMT-7"]
},
{
"patterns": [
"vvvv",
"VVVV",
"OOOO",
"zzzz",
"ZZZZ"
Expand All @@ -1016,15 +1019,6 @@
],
"configs": [],
"expected": ["-07:00"]
},
{
"patterns": [
"v",
"vvvv",
"VVVV"
],
"configs": [],
"expected": ["Unknown City Time"]
}
]
},
Expand All @@ -1037,6 +1031,7 @@
"z",
"zz",
"zzz",
"v",
"O"
],
"configs": [],
Expand All @@ -1045,6 +1040,8 @@
{
"patterns": [
"ZZZZ",
"vvvv",
"VVVV",
"zzzz",
"OOOO"
],
Expand All @@ -1066,15 +1063,6 @@
],
"configs": [],
"expected": ["-07:00"]
},
{
"patterns": [
"v",
"vvvv",
"VVVV"
],
"configs": [],
"expected": ["Unknown City Time"]
}
]
},
Expand All @@ -1097,28 +1085,22 @@
"Z",
"ZZ",
"ZZZ",
"ZZZZ",
"ZZZZZ"
],
"configs": [],
"expected": ["{Z}"]
"expected": ["+?"]
},
{
"patterns": [
"O",
"OOOO"
],
"configs": [],
"expected": ["{O}"]
},
{
"patterns": [
"ZZZZ",
"OOOO",
"v",
"vvvv",
"VVVV"
],
"configs": [],
"expected": ["Unknown City Time"]
"expected": ["GMT+?"]
}
]
}
Expand Down
Loading

0 comments on commit 6279b08

Please sign in to comment.