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

Add fixed-length localized offset format #6090

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion components/datetime/examples/timezone_picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {
let prefs = locale!("en").into();

let offset_formatter =
TimeFormatter::try_new(prefs, fieldsets::zone::LocalizedOffsetLong).unwrap();
TimeFormatter::try_new(prefs, fieldsets::zone::LocalizedOffsetFixed).unwrap();
let non_location_formatter =
TimeFormatter::try_new(prefs, fieldsets::zone::GenericLong).unwrap();
let city_formatter = TimeFormatter::try_new(prefs, fieldsets::zone::ExemplarCity).unwrap();
Expand Down
4 changes: 4 additions & 0 deletions components/datetime/src/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ pub enum ZoneFieldSet {
/// The short offset format, as in
/// “GMT−8”.
LocalizedOffsetShort(fieldsets::zone::LocalizedOffsetShort),
/// The fixed-length offset format, as in
/// “GMT−00:00”.
LocalizedOffsetFixed(fieldsets::zone::LocalizedOffsetFixed),
/// The long generic non-location format, as in
/// “Pacific Time”.
GenericLong(fieldsets::zone::GenericLong),
Expand Down Expand Up @@ -476,6 +479,7 @@ impl_attrs! {
SpecificShort,
LocalizedOffsetLong,
LocalizedOffsetShort,
LocalizedOffsetFixed,
GenericLong,
GenericShort,
Location,
Expand Down
9 changes: 9 additions & 0 deletions components/datetime/src/fieldsets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,15 @@ pub mod zone {
zone_essentials = yes,
);

impl_zone_marker!(
LocalizedOffsetFixed,
description = "UTC offset, fixed-length",
length_override = Long,
sample = "GMT-05:00",
field = (fields::TimeZone::LocalizedOffset, fields::FieldLength::Two),
zone_essentials = yes,
);

// TODO: Add short/long UTC offset?

impl_zone_marker!(
Expand Down
6 changes: 3 additions & 3 deletions components/datetime/src/format/time_zone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ impl FormatTimeZone for LocalizedOffsetFormat {
sink.write_str(&essentials.offset_unknown)?;
return Ok(Ok(()));
};
Ok(if offset.is_zero() {
Ok(if offset.is_zero() && self.0 != FieldLength::Two {
sink.write_str(&essentials.offset_zero)?;
Ok(())
} else {
Expand All @@ -246,7 +246,7 @@ impl FormatTimeZone for LocalizedOffsetFormat {
let fd = {
let mut fd = SignedFixedDecimal::from(self.offset.hours_part())
.with_sign_display(fixed_decimal::SignDisplay::Always);
fd.pad_start(if self.length == FieldLength::Four {
fd.pad_start(if self.length != FieldLength::One {
2
} else {
0
Expand All @@ -255,7 +255,7 @@ impl FormatTimeZone for LocalizedOffsetFormat {
};
self.fdf.format(&fd).write_to(sink)?;

if self.length == FieldLength::Four
if self.length != FieldLength::One
|| self.offset.minutes_part() != 0
|| self.offset.seconds_part() != 0
{
Expand Down
2 changes: 2 additions & 0 deletions components/datetime/src/neo_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ impl FieldSetSerde {
(ZoneFieldSet::SpecificShort(..), true) => (Self::ZONE_SPECIFIC, short),
(ZoneFieldSet::LocalizedOffsetLong(..), true) => (Self::ZONE_OFFSET, long),
(ZoneFieldSet::LocalizedOffsetShort(..), true) => (Self::ZONE_OFFSET, short),
(ZoneFieldSet::LocalizedOffsetFixed(..), true) => (Self::ZONE_OFFSET, long),
(ZoneFieldSet::GenericLong(..), true) => (Self::ZONE_GENERIC, long),
(ZoneFieldSet::GenericShort(..), true) => (Self::ZONE_GENERIC, short),
(ZoneFieldSet::Location(..), true) => (Self::ZONE_LOCATION, long),
Expand All @@ -607,6 +608,7 @@ impl FieldSetSerde {
(ZoneFieldSet::SpecificShort(..), false) => (Self::ZONE_SPECIFIC, short),
(ZoneFieldSet::LocalizedOffsetLong(..), false) => (Self::ZONE_OFFSET_LONG, long),
(ZoneFieldSet::LocalizedOffsetShort(..), false) => (Self::ZONE_OFFSET, short),
(ZoneFieldSet::LocalizedOffsetFixed(..), false) => (Self::ZONE_OFFSET, long),
(ZoneFieldSet::GenericLong(..), false) => (Self::ZONE_GENERIC_LONG, long),
(ZoneFieldSet::GenericShort(..), false) => (Self::ZONE_GENERIC, short),
(ZoneFieldSet::Location(..), false) => (Self::ZONE_LOCATION, long),
Expand Down
4 changes: 2 additions & 2 deletions components/datetime/src/pattern/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2405,8 +2405,8 @@ impl<FSet: DateTimeNamesMarker> RawDateTimeNames<FSet> {
)?;
}

// O, OOOO
(FS::TimeZone(TimeZone::LocalizedOffset), One | Four) => {
// O, OO, OOOO
(FS::TimeZone(TimeZone::LocalizedOffset), One | Two | Four) => {
self.load_time_zone_essentials(zone_essentials_provider, prefs)?;
numeric_field = Some(field);
}
Expand Down
12 changes: 12 additions & 0 deletions components/datetime/tests/patterns/tests/time_zones.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"VVVV": "Los Angeles Time",

"O": "GMT-7",
"OO": "GMT-07:00",
"OOOO": "GMT-07:00",

"x": "-07",
Expand Down Expand Up @@ -46,6 +47,7 @@
"VVVV": "UK Time",

"O": "GMT+1",
"OO": "GMT+01:00",
"OOOO": "GMT+01:00"
}
},
Expand Down Expand Up @@ -80,6 +82,7 @@
"VVVV": "日本時間",

"O": "GMT+9",
"OO": "GMT+09:00",
"OOOO": "GMT+09:00"
}
},
Expand All @@ -97,6 +100,7 @@
"VVVV": "Тролль",

"O": "GMT",
"OO": "GMT+00:00",
"OOOO": "GMT",

"X": "Z"
Expand All @@ -116,6 +120,7 @@
"VVVV": "Непал",

"O": "GMT+5.45",
"OO": "GMT+05.45",
"OOOO": "GMT+05.45"
}
},
Expand All @@ -124,6 +129,7 @@
"datetime": "2021-01-11T12:00:00.000+05:45[Asia/Kathmandu]",
"expectations": {
"O": "GMT +৫:৪৫",
"OO": "GMT +০৫:৪৫",
"OOOO": "GMT +০৫:৪৫",
"Z": "+0545"
}
Expand All @@ -142,6 +148,7 @@
"VVVV": "שעון ישראל",

"O": "GMT\u200e+3\u200e",
"OO": "GMT\u200e+03:00\u200e",
"OOOO": "GMT\u200e+03:00\u200e"
}
},
Expand All @@ -159,6 +166,7 @@
"VVVV": "GMT-07:00",

"O": "GMT-7",
"OO": "GMT-07:00",
"OOOO": "GMT-07:00",

"Z": "-0700",
Expand All @@ -179,6 +187,7 @@
"VVVV": "GMT-07:00",

"O": "GMT-7",
"OO": "GMT-07:00",
"OOOO": "GMT-07:00",

"Z": "-0700",
Expand All @@ -199,6 +208,7 @@
"VVVV": "GMT+?",

"O": "GMT+?",
"OO": "GMT+?",
"OOOO": "GMT+?",

"Z": "+?",
Expand All @@ -219,6 +229,7 @@
"VVVV": "GMT",

"O": "GMT",
"OO": "GMT+00:00",
"OOOO": "GMT",

"Z": "+0000",
Expand All @@ -239,6 +250,7 @@
"VVVV": "GMT",

"O": "GMT",
"OO": "GMT+00:00",
"OOOO": "GMT",

"Z": "+0000",
Expand Down
1 change: 1 addition & 0 deletions components/datetime/tests/patterns/time_zones.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub fn pattern_to_semantic_skeleton(p: &str) -> Option<ZoneFieldSet> {
"zzzz" => ZoneFieldSet::SpecificLong(fieldsets::zone::SpecificLong),
"z" => ZoneFieldSet::SpecificShort(fieldsets::zone::SpecificShort),
"OOOO" => ZoneFieldSet::LocalizedOffsetLong(fieldsets::zone::LocalizedOffsetLong),
"OO" => ZoneFieldSet::LocalizedOffsetFixed(fieldsets::zone::LocalizedOffsetFixed),
"O" => ZoneFieldSet::LocalizedOffsetShort(fieldsets::zone::LocalizedOffsetShort),
"VVV" => ZoneFieldSet::ExemplarCity(fieldsets::zone::ExemplarCity),
_ => return None,
Expand Down
1 change: 1 addition & 0 deletions ffi/capi/tests/missing_apis.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ icu::datetime::fieldsets::enums::TimeFieldSet::zone#FnInEnum
icu::datetime::fieldsets::enums::ZoneFieldSet#Enum
icu::datetime::fieldsets::zone::ExemplarCity#Struct
icu::datetime::fieldsets::zone::GenericLong#Struct
icu::datetime::fieldsets::zone::LocalizedOffsetFixed#Struct
icu::datetime::fieldsets::zone::LocalizedOffsetLong#Struct
icu::datetime::fieldsets::zone::LocalizedOffsetShort#Struct
icu::datetime::fieldsets::zone::Location#Struct
Expand Down