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

Support localized offsets with seconds #5674

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
secondss
  • Loading branch information
robertbastian committed Oct 11, 2024
commit c4e1dd00610b7aa800230e22a29dc0fafb22a3f4
17 changes: 16 additions & 1 deletion components/datetime/src/time_zone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,10 @@ impl FormatOffset for LocalizedOffsetFormat {
)
.write_to(sink)?;

if self.length == FieldLength::Wide || self.offset.has_minutes() {
if self.length == FieldLength::Wide
|| self.offset.has_minutes()
|| self.offset.has_seconds()
{
sink.write_char(self.separator)?;
self.fdf
.format(
Expand All @@ -414,6 +417,18 @@ impl FormatOffset for LocalizedOffsetFormat {
.write_to(sink)?;
}

if self.offset.has_seconds() {
sink.write_char(self.separator)?;
self.fdf
.format(
&FixedDecimal::from(
(self.offset.offset_seconds() % 3600).unsigned_abs(),
robertbastian marked this conversation as resolved.
Show resolved Hide resolved
)
.padded_start(2),
)
.write_to(sink)?;
}

Ok(())
}
}
Expand Down
18 changes: 17 additions & 1 deletion components/datetime/tests/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use icu_locale_core::{
locale, LanguageIdentifier, Locale,
};
use icu_provider::prelude::*;
use icu_timezone::{CustomTimeZone, CustomZonedDateTime};
use icu_timezone::{CustomTimeZone, CustomZonedDateTime, UtcOffset};
use patterns::{
dayperiods::{DayPeriodExpectation, DayPeriodTests},
time_zones::{TimeZoneExpectation, TimeZoneFormatterConfig, TimeZoneTests},
Expand Down Expand Up @@ -471,6 +471,22 @@ fn test_time_zone_format_configs() {
}
}

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

let time_zone =
CustomTimeZone::new_with_offset(UtcOffset::try_from_offset_seconds(12).unwrap());
let tzf = TypedNeoFormatter::<NeverCalendar, _>::try_new(
&locale!("en").into(),
NeoTimeZoneOffsetMarker::with_length(NeoSkeletonLength::Medium),
)
.unwrap();
assert_try_writeable_eq!(tzf.format(&time_zone), "GMT+0:00:12",);
}

#[test]
fn test_time_zone_format_offset_not_set_debug_assert_panic() {
use icu_datetime::{
Expand Down