Skip to content

Add test illustrating mismatched time zone time and zoned datetime #6239

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
57 changes: 57 additions & 0 deletions components/datetime/src/combo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,60 @@ where
type Z = Z::Z;
type GluePatternV1 = datetime_marker_helper!(@glue, yes);
}

#[cfg(test)]
mod tests {
use icu_calendar::Iso;
use icu_locale_core::locale;
use icu_time::{
zone::{IanaParser, UtcOffset, UtcOffsetCalculator},
DateTime, ZonedDateTime,
};
use writeable::assert_writeable_eq;

use crate::{
fieldsets::{zone::SpecificLong, YMDT},
DateTimeFormatter,
};

#[test]
fn mismatched_zone_at_time() {
let mapper = IanaParser::new();
let calculator = UtcOffsetCalculator::new();
let formatter =
DateTimeFormatter::try_new(locale!("en").into(), YMDT::medium().zone(SpecificLong))
.unwrap();

let good_zdt = ZonedDateTime::try_from_str(
"2020-01-01T12:00:00-0100[America/Scoresbysund]",
Iso,
mapper,
&calculator,
)
.unwrap();

let dt_2020 = DateTime::try_from_str("2020-01-01T12:00", Iso).unwrap();
let dt_2025 = DateTime::try_from_str("2025-01-01T12:00", Iso).unwrap();

let time_zone = IanaParser::new()
.parse("America/Scoresbysund")
.with_offset(Some(UtcOffset::try_from_str("-0100").unwrap()))
.at_time((dt_2025.date, dt_2025.time))
.infer_zone_variant(&calculator);

let bad_zdt = ZonedDateTime {
date: dt_2020.date,
time: dt_2020.time,
zone: time_zone,
};

assert_writeable_eq!(
formatter.format(&good_zdt),
"Jan 1, 2020, 12:00:00 PM East Greenland Standard Time"
);
assert_writeable_eq!(
formatter.format(&bad_zdt),
"Jan 1, 2020, 12:00:00 PM Greenland Summer Time"
);
}
}
Loading