Skip to content

Commit

Permalink
Remove ZonedDateTimeParser (#6121)
Browse files Browse the repository at this point in the history
I've not prettified docs, because they'll be touched again by #6069 and
#5999.

Part of #6119
  • Loading branch information
robertbastian authored Feb 14, 2025
1 parent 91dd98b commit ffe8ac3
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 217 deletions.
39 changes: 24 additions & 15 deletions components/datetime/src/combo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{provider::neo::*, scaffold::*};
/// use icu::datetime::fieldsets::{Combo, ET, zone::Location};
/// use icu::datetime::DateTimeFormatter;
/// use icu::locale::locale;
/// use icu::timezone::ZonedDateTimeParser;
/// use icu::timezone::{ZonedDateTime, TimeZoneIdMapper};
/// use writeable::assert_writeable_eq;
///
/// // Note: Combo type can be elided, but it is shown here for demonstration
Expand All @@ -35,9 +35,12 @@ use crate::{provider::neo::*, scaffold::*};
/// )
/// .unwrap();
///
/// let zdt = ZonedDateTimeParser::new()
/// .parse_location_only("2024-10-18T15:44[America/Los_Angeles]", formatter.calendar())
/// .unwrap();
/// let zdt = ZonedDateTime::try_location_only_from_str(
/// "2024-10-18T15:44[America/Los_Angeles]",
/// formatter.calendar(),
/// TimeZoneIdMapper::new(),
/// )
/// .unwrap();
///
/// assert_writeable_eq!(
/// formatter.format(&zdt),
Expand All @@ -52,7 +55,7 @@ use crate::{provider::neo::*, scaffold::*};
/// use icu::datetime::fieldsets::{Combo, ET, zone::Location};
/// use icu::datetime::FixedCalendarDateTimeFormatter;
/// use icu::locale::locale;
/// use icu::timezone::{ZonedDateTimeParser, ZonedDateTime};
/// use icu::timezone::{ZonedDateTime, TimeZoneIdMapper};
/// use writeable::assert_writeable_eq;
///
/// // Note: Combo type can be elided, but it is shown here for demonstration
Expand All @@ -62,8 +65,7 @@ use crate::{provider::neo::*, scaffold::*};
/// )
/// .unwrap();
///
/// let zdt = ZonedDateTimeParser::new()
/// .parse_location_only("2024-10-18T15:44[America/Los_Angeles]", Gregorian)
/// let zdt = ZonedDateTime::try_location_only_from_str("2024-10-18T15:44[America/Los_Angeles]", Gregorian, TimeZoneIdMapper::new())
/// .unwrap();
///
/// assert_writeable_eq!(
Expand All @@ -79,7 +81,7 @@ use crate::{provider::neo::*, scaffold::*};
/// use icu::datetime::fieldsets::{enums::DateFieldSet, Combo, zone::GenericShort, YMD};
/// use icu::datetime::DateTimeFormatter;
/// use icu::locale::locale;
/// use icu::timezone::ZonedDateTimeParser;
/// use icu::timezone::{ZonedDateTime, TimeZoneIdMapper};
/// use writeable::assert_writeable_eq;
///
/// // Note: Combo type can be elided, but it is shown here for demonstration
Expand All @@ -89,9 +91,12 @@ use crate::{provider::neo::*, scaffold::*};
/// )
/// .unwrap();
///
/// let zdt = ZonedDateTimeParser::new()
/// .parse_location_only("2024-10-18T15:44[America/Los_Angeles]", formatter.calendar())
/// .unwrap();
/// let zdt = ZonedDateTime::try_location_only_from_str(
/// "2024-10-18T15:44[America/Los_Angeles]",
/// formatter.calendar(),
/// TimeZoneIdMapper::new(),
/// )
/// .unwrap();
///
/// assert_writeable_eq!(
/// formatter.format(&zdt),
Expand All @@ -106,7 +111,7 @@ use crate::{provider::neo::*, scaffold::*};
/// use icu::datetime::fieldsets::{T, zone::SpecificLong};
/// use icu::datetime::FixedCalendarDateTimeFormatter;
/// use icu::locale::locale;
/// use icu::timezone::{ZonedDateTimeParser, ZonedDateTime};
/// use icu::timezone::{ZonedDateTime, ZoneOffsetCalculator, TimeZoneIdMapper};
/// use writeable::assert_writeable_eq;
///
/// let formatter = FixedCalendarDateTimeFormatter::try_new(
Expand All @@ -115,9 +120,13 @@ use crate::{provider::neo::*, scaffold::*};
/// )
/// .unwrap();
///
/// let zdt = ZonedDateTimeParser::new()
/// .parse("2024-10-18T15:44-0700[America/Los_Angeles]", Gregorian)
/// .unwrap();
/// let zdt = ZonedDateTime::try_from_str(
/// "2024-10-18T15:44-0700[America/Los_Angeles]",
/// Gregorian,
/// TimeZoneIdMapper::new(),
/// &ZoneOffsetCalculator::new(),
/// )
/// .unwrap();
///
/// assert_writeable_eq!(
/// formatter.format(&zdt),
Expand Down
2 changes: 1 addition & 1 deletion components/datetime/src/fieldsets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ pub mod zone {
///
/// ```
/// use icu::calendar::Date;
/// use icu::timezone::{ZonedDateTimeParser, Time, TimeZoneBcp47Id, TimeZoneInfo, UtcOffset, ZoneVariant};
/// use icu::timezone::{Time, TimeZoneBcp47Id, TimeZoneInfo, UtcOffset, ZoneVariant};
/// use icu::calendar::Gregorian;
/// use icu::datetime::FixedCalendarDateTimeFormatter;
/// use icu::datetime::fieldsets::zone::{SpecificLong, SpecificShort};
Expand Down
4 changes: 2 additions & 2 deletions components/datetime/src/parts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//! use icu::datetime::DateTimeFormatter;
//! use icu::decimal::parts as decimal_parts;
//! use icu::locale::locale;
//! use icu::timezone::{ZonedDateTimeParser, Time};
//! use icu::timezone::{ZonedDateTime, Time, TimeZoneIdMapper, ZoneOffsetCalculator};
//! use writeable::assert_writeable_parts_eq;
//!
//! let dtf = DateTimeFormatter::try_new(
Expand All @@ -25,7 +25,7 @@
//! )
//! .unwrap();
//!
//! let dtz = ZonedDateTimeParser::new().parse("2023-11-20T11:35:03.5+00:00[Europe/London]", dtf.calendar()).unwrap();
//! let dtz = ZonedDateTime::try_from_str("2023-11-20T11:35:03.5+00:00[Europe/London]", dtf.calendar(), TimeZoneIdMapper::new(), &ZoneOffsetCalculator::new()).unwrap();
//!
//! // Missing data is filled in on a best-effort basis, and an error is signaled.
//! assert_writeable_parts_eq!(
Expand Down
8 changes: 3 additions & 5 deletions components/datetime/src/pattern/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,12 @@ where
/// use icu::datetime::pattern::DateTimePattern;
/// use icu::datetime::pattern::TypedDateTimeNames;
/// use icu::locale::locale;
/// use icu::timezone::ZonedDateTimeParser;
/// use icu::timezone::{ZonedDateTime, TimeZoneIdMapper, ZoneOffsetCalculator};
/// use writeable::assert_try_writeable_eq;
///
/// let mut london_winter = ZonedDateTimeParser::new()
/// .parse("2024-01-01T00:00:00+00:00[Europe/London]", Gregorian)
/// let mut london_winter = ZonedDateTime::try_from_str("2024-01-01T00:00:00+00:00[Europe/London]", Gregorian, TimeZoneIdMapper::new(), &ZoneOffsetCalculator::new())
/// .unwrap();
/// let mut london_summer = ZonedDateTimeParser::new()
/// .parse("2024-07-01T00:00:00+01:00[Europe/London]", Gregorian)
/// let mut london_summer = ZonedDateTime::try_from_str("2024-07-01T00:00:00+01:00[Europe/London]", Gregorian, TimeZoneIdMapper::new(), &ZoneOffsetCalculator::new())
/// .unwrap();
///
/// let mut names = TypedDateTimeNames::<Gregorian, ZoneFieldSet>::try_new(
Expand Down
54 changes: 21 additions & 33 deletions components/datetime/src/pattern/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ size_test!(
/// use icu::datetime::pattern::{DateTimePattern, PatternLoadError};
/// use icu::datetime::fieldsets::enums::CompositeFieldSet;
/// use icu::locale::locale;
/// use icu::timezone::{Time, TimeZoneInfo, ZonedDateTimeParser, ZonedDateTime};
/// use icu::timezone::{Time, TimeZoneInfo, TimeZoneIdMapper, ZonedDateTime, ZoneOffsetCalculator};
/// use icu_provider_adapters::empty::EmptyDataProvider;
/// use writeable::{Part, assert_try_writeable_parts_eq};
///
Expand All @@ -451,7 +451,7 @@ size_test!(
/// // The pattern string contains lots of symbols including "E", "MMM", and "a",
/// // but we did not load any data!
///
/// let mut dtz = ZonedDateTimeParser::new().parse("2023-11-20T11:35:03+00:00[Europe/London]", Gregorian).unwrap();
/// let mut dtz = ZonedDateTime::try_from_str("2023-11-20T11:35:03+00:00[Europe/London]", Gregorian, TimeZoneIdMapper::new(), &ZoneOffsetCalculator::new()).unwrap();
///
/// // Missing data is filled in on a best-effort basis, and an error is signaled.
/// assert_try_writeable_parts_eq!(
Expand Down Expand Up @@ -1404,15 +1404,13 @@ impl<C, FSet: DateTimeNamesMarker> TypedDateTimeNames<C, FSet> {
/// use icu::datetime::pattern::DateTimePattern;
/// use icu::datetime::pattern::TypedDateTimeNames;
/// use icu::locale::locale;
/// use icu::timezone::ZonedDateTimeParser;
/// use icu::timezone::{ZonedDateTime, TimeZoneIdMapper, ZoneOffsetCalculator};
/// use writeable::assert_try_writeable_eq;
///
/// let mut zone_london_winter = ZonedDateTimeParser::new()
/// .parse("2024-01-01T00:00:00+00:00[Europe/London]", Gregorian)
/// let mut zone_london_winter = ZonedDateTime::try_from_str("2024-01-01T00:00:00+00:00[Europe/London]", Gregorian, TimeZoneIdMapper::new(), &ZoneOffsetCalculator::new())
/// .unwrap()
/// .zone;
/// let mut zone_london_summer = ZonedDateTimeParser::new()
/// .parse("2024-07-01T00:00:00+01:00[Europe/London]", Gregorian)
/// let mut zone_london_summer = ZonedDateTime::try_from_str("2024-07-01T00:00:00+01:00[Europe/London]", Gregorian, TimeZoneIdMapper::new(), &ZoneOffsetCalculator::new())
/// .unwrap()
/// .zone;
///
Expand Down Expand Up @@ -1516,11 +1514,10 @@ impl<C, FSet: DateTimeNamesMarker> TypedDateTimeNames<C, FSet> {
/// use icu::datetime::pattern::DateTimePattern;
/// use icu::datetime::pattern::TypedDateTimeNames;
/// use icu::locale::locale;
/// use icu::timezone::ZonedDateTimeParser;
/// use icu::timezone::{ZonedDateTime, TimeZoneIdMapper, ZoneOffsetCalculator};
/// use writeable::assert_try_writeable_eq;
///
/// let mut zone_london_winter = ZonedDateTimeParser::new()
/// .parse("2024-01-01T00:00:00+00:00[Europe/London]", Gregorian)
/// let mut zone_london_winter = ZonedDateTime::try_from_str("2024-01-01T00:00:00+00:00[Europe/London]", Gregorian, TimeZoneIdMapper::new(), &ZoneOffsetCalculator::new())
/// .unwrap()
/// .zone;
///
Expand Down Expand Up @@ -1580,11 +1577,10 @@ impl<C, FSet: DateTimeNamesMarker> TypedDateTimeNames<C, FSet> {
/// use icu::datetime::pattern::DateTimePattern;
/// use icu::datetime::pattern::TypedDateTimeNames;
/// use icu::locale::locale;
/// use icu::timezone::ZonedDateTimeParser;
/// use icu::timezone::{ZonedDateTime, TimeZoneIdMapper, ZoneOffsetCalculator};
/// use writeable::assert_try_writeable_eq;
///
/// let mut zone_london_winter = ZonedDateTimeParser::new()
/// .parse("2024-01-01T00:00:00+00:00[Europe/London]", Gregorian)
/// let mut zone_london_winter = ZonedDateTime::try_from_str("2024-01-01T00:00:00+00:00[Europe/London]", Gregorian, TimeZoneIdMapper::new(), &ZoneOffsetCalculator::new())
/// .unwrap()
/// .zone;
///
Expand Down Expand Up @@ -1644,15 +1640,13 @@ impl<C, FSet: DateTimeNamesMarker> TypedDateTimeNames<C, FSet> {
/// use icu::datetime::pattern::DateTimePattern;
/// use icu::datetime::pattern::TypedDateTimeNames;
/// use icu::locale::locale;
/// use icu::timezone::ZonedDateTimeParser;
/// use icu::timezone::{ZonedDateTime, TimeZoneIdMapper, ZoneOffsetCalculator};
/// use writeable::assert_try_writeable_eq;
///
/// let mut zone_london_winter = ZonedDateTimeParser::new()
/// .parse("2024-01-01T00:00:00+00:00[Europe/London]", Gregorian)
/// let mut zone_london_winter = ZonedDateTime::try_from_str("2024-01-01T00:00:00+00:00[Europe/London]", Gregorian, TimeZoneIdMapper::new(), &ZoneOffsetCalculator::new())
/// .unwrap()
/// .zone;
/// let mut zone_london_summer = ZonedDateTimeParser::new()
/// .parse("2024-07-01T00:00:00+01:00[Europe/London]", Gregorian)
/// let mut zone_london_summer = ZonedDateTime::try_from_str("2024-07-01T00:00:00+01:00[Europe/London]", Gregorian, TimeZoneIdMapper::new(), &ZoneOffsetCalculator::new())
/// .unwrap()
/// .zone;
///
Expand Down Expand Up @@ -1718,15 +1712,13 @@ impl<C, FSet: DateTimeNamesMarker> TypedDateTimeNames<C, FSet> {
/// use icu::datetime::pattern::DateTimePattern;
/// use icu::datetime::pattern::TypedDateTimeNames;
/// use icu::locale::locale;
/// use icu::timezone::ZonedDateTimeParser;
/// use icu::timezone::{ZonedDateTime, TimeZoneIdMapper, ZoneOffsetCalculator};
/// use writeable::assert_try_writeable_eq;
///
/// let mut zone_london_winter = ZonedDateTimeParser::new()
/// .parse("2024-01-01T00:00:00+00:00[Europe/London]", Gregorian)
/// let mut zone_london_winter = ZonedDateTime::try_from_str("2024-01-01T00:00:00+00:00[Europe/London]", Gregorian, TimeZoneIdMapper::new(), &ZoneOffsetCalculator::new())
/// .unwrap()
/// .zone;
/// let mut zone_london_summer = ZonedDateTimeParser::new()
/// .parse("2024-07-01T00:00:00+01:00[Europe/London]", Gregorian)
/// let mut zone_london_summer = ZonedDateTime::try_from_str("2024-07-01T00:00:00+01:00[Europe/London]", Gregorian, TimeZoneIdMapper::new(), &ZoneOffsetCalculator::new())
/// .unwrap()
/// .zone;
///
Expand Down Expand Up @@ -1792,15 +1784,13 @@ impl<C, FSet: DateTimeNamesMarker> TypedDateTimeNames<C, FSet> {
/// use icu::datetime::pattern::DateTimePattern;
/// use icu::datetime::pattern::TypedDateTimeNames;
/// use icu::locale::locale;
/// use icu::timezone::ZonedDateTimeParser;
/// use icu::timezone::{ZonedDateTime, TimeZoneIdMapper, ZoneOffsetCalculator};
/// use writeable::assert_try_writeable_eq;
///
/// let mut zone_london_winter = ZonedDateTimeParser::new()
/// .parse("2024-01-01T00:00:00+00:00[Europe/London]", Gregorian)
/// let mut zone_london_winter = ZonedDateTime::try_from_str("2024-01-01T00:00:00+00:00[Europe/London]", Gregorian, TimeZoneIdMapper::new(), &ZoneOffsetCalculator::new())
/// .unwrap()
/// .zone;
/// let mut zone_london_summer = ZonedDateTimeParser::new()
/// .parse("2024-07-01T00:00:00+01:00[Europe/London]", Gregorian)
/// let mut zone_london_summer = ZonedDateTime::try_from_str("2024-07-01T00:00:00+01:00[Europe/London]", Gregorian, TimeZoneIdMapper::new(), &ZoneOffsetCalculator::new())
/// .unwrap()
/// .zone;
///
Expand Down Expand Up @@ -1866,15 +1856,13 @@ impl<C, FSet: DateTimeNamesMarker> TypedDateTimeNames<C, FSet> {
/// use icu::datetime::pattern::DateTimePattern;
/// use icu::datetime::pattern::TypedDateTimeNames;
/// use icu::locale::locale;
/// use icu::timezone::ZonedDateTimeParser;
/// use icu::timezone::{ZonedDateTime, TimeZoneIdMapper, ZoneOffsetCalculator};
/// use writeable::assert_try_writeable_eq;
///
/// let mut zone_london_winter = ZonedDateTimeParser::new()
/// .parse("2024-01-01T00:00:00+00:00[Europe/London]", Gregorian)
/// let mut zone_london_winter = ZonedDateTime::try_from_str("2024-01-01T00:00:00+00:00[Europe/London]", Gregorian, TimeZoneIdMapper::new(), &ZoneOffsetCalculator::new())
/// .unwrap()
/// .zone;
/// let mut zone_london_summer = ZonedDateTimeParser::new()
/// .parse("2024-07-01T00:00:00+01:00[Europe/London]", Gregorian)
/// let mut zone_london_summer = ZonedDateTime::try_from_str("2024-07-01T00:00:00+01:00[Europe/London]", Gregorian, TimeZoneIdMapper::new(), &ZoneOffsetCalculator::new())
/// .unwrap()
/// .zone;
///
Expand Down
13 changes: 10 additions & 3 deletions components/datetime/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
//! Some useful parsing functions for tests.
use icu_calendar::Gregorian;
use icu_timezone::{models, TimeZoneInfo, ZoneVariant, ZonedDateTime, ZonedDateTimeParser};
use icu_timezone::{
models, TimeZoneIdMapper, TimeZoneInfo, ZoneOffsetCalculator, ZoneVariant, ZonedDateTime,
};

/// Parse a [`DateTime`] and [`TimeZoneInfo`] from a string.
///
Expand All @@ -28,10 +30,15 @@ use icu_timezone::{models, TimeZoneInfo, ZoneVariant, ZonedDateTime, ZonedDateTi
pub fn parse_zoned_gregorian_from_str(
input: &str,
) -> ZonedDateTime<Gregorian, TimeZoneInfo<models::Full>> {
match ZonedDateTimeParser::new().parse(input, Gregorian) {
match ZonedDateTime::try_from_str(
input,
Gregorian,
TimeZoneIdMapper::new(),
&ZoneOffsetCalculator::new(),
) {
Ok(zdt) => zdt,
Err(icu_timezone::ParseError::MismatchedTimeZoneFields) => {
match ZonedDateTimeParser::new().parse_loose(input, Gregorian) {
match ZonedDateTime::try_loose_from_str(input, Gregorian, TimeZoneIdMapper::new()) {
Ok(zdt) => {
ZonedDateTime {
date: zdt.date,
Expand Down
12 changes: 8 additions & 4 deletions components/icu/examples/jiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use icu::{
calendar::Date,
datetime::{fieldsets, DateTimeFormatter},
locale::locale,
timezone::{Time, TimeZoneIdMapper, UtcOffset, ZonedDateTime, ZonedDateTimeParser},
timezone::{Time, TimeZoneIdMapper, UtcOffset, ZoneOffsetCalculator, ZonedDateTime},
};

fn main() -> Result<(), Box<dyn core::error::Error>> {
Expand Down Expand Up @@ -45,9 +45,13 @@ fn main() -> Result<(), Box<dyn core::error::Error>> {

// Alternatively, the ICU ZonedDateTime can be parsed from a serialized IXDTF string.
assert_eq!(
ZonedDateTimeParser::new()
.parse(&zoned.to_string(), icu::calendar::Iso)
.unwrap(),
ZonedDateTime::try_from_str(
&zoned.to_string(),
icu::calendar::Iso,
TimeZoneIdMapper::new(),
&ZoneOffsetCalculator::new()
)
.unwrap(),
zoned_date_time
);

Expand Down
Loading

0 comments on commit ffe8ac3

Please sign in to comment.