Skip to content

Commit 472f237

Browse files
Address icu::time feedback (#6460)
#6354
1 parent 45638ab commit 472f237

File tree

78 files changed

+782
-703
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+782
-703
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/calendar/src/ixdtf.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ use crate::{AsCalendar, Calendar, Date, Iso, RangeError};
88
use icu_locale_core::preferences::extensions::unicode::keywords::CalendarAlgorithm;
99
use ixdtf::parsers::records::IxdtfParseRecord;
1010
use ixdtf::parsers::IxdtfParser;
11-
use ixdtf::ParseError as IxdtfError;
11+
use ixdtf::ParseError as Rfc9557Error;
1212

13-
/// An error returned from parsing an IXDTF string to an `icu_calendar` type.
13+
/// An error returned from parsing an RFC 9557 string to an `icu_calendar` type.
1414
#[derive(Debug, displaydoc::Display)]
1515
#[non_exhaustive]
1616
pub enum ParseError {
17-
/// Syntax error in the IXDTF string.
18-
#[displaydoc("Syntax error in the IXDTF string: {0}")]
19-
Syntax(IxdtfError),
17+
/// Syntax error.
18+
#[displaydoc("Syntax error in the RFC 9557 string: {0}")]
19+
Syntax(Rfc9557Error),
2020
/// Value is out of range.
2121
#[displaydoc("Value out of range: {0}")]
2222
Range(RangeError),
23-
/// The IXDTF is missing fields required for parsing into the chosen type.
23+
/// The RFC 9557 string is missing fields required for parsing into the chosen type.
2424
MissingFields,
25-
/// The IXDTF specifies an unknown calendar.
25+
/// The RFC 9557 string specifies an unknown calendar.
2626
UnknownCalendar,
2727
/// Expected a different calendar.
2828
#[displaydoc("Expected calendar {0:?} but found calendar {1:?}")]
@@ -35,21 +35,21 @@ impl From<RangeError> for ParseError {
3535
}
3636
}
3737

38-
impl From<IxdtfError> for ParseError {
39-
fn from(value: IxdtfError) -> Self {
38+
impl From<Rfc9557Error> for ParseError {
39+
fn from(value: Rfc9557Error) -> Self {
4040
Self::Syntax(value)
4141
}
4242
}
4343

4444
impl FromStr for Date<Iso> {
4545
type Err = ParseError;
46-
fn from_str(ixdtf_str: &str) -> Result<Self, Self::Err> {
47-
Self::try_from_str(ixdtf_str, Iso)
46+
fn from_str(rfc_9557_str: &str) -> Result<Self, Self::Err> {
47+
Self::try_from_str(rfc_9557_str, Iso)
4848
}
4949
}
5050

5151
impl<A: AsCalendar> Date<A> {
52-
/// Creates a [`Date`] in the given calendar from an IXDTF syntax string.
52+
/// Creates a [`Date`] in the given calendar from an RFC 9557 string.
5353
///
5454
/// Returns an error if the string has a calendar annotation that does not
5555
/// match the calendar argument, unless the argument is [`Iso`].
@@ -74,20 +74,20 @@ impl<A: AsCalendar> Date<A> {
7474
/// );
7575
/// assert_eq!(date.day_of_month().0, 17);
7676
/// ```
77-
pub fn try_from_str(ixdtf_str: &str, calendar: A) -> Result<Self, ParseError> {
78-
Self::try_from_utf8(ixdtf_str.as_bytes(), calendar)
77+
pub fn try_from_str(rfc_9557_str: &str, calendar: A) -> Result<Self, ParseError> {
78+
Self::try_from_utf8(rfc_9557_str.as_bytes(), calendar)
7979
}
8080

81-
/// Creates a [`Date`] in the given calendar from an IXDTF syntax string.
81+
/// Creates a [`Date`] in the given calendar from an RFC 9557 string.
8282
///
8383
/// Returns an error if the string has a calendar annotation that does not
8484
/// match the calendar argument.
8585
///
8686
/// See [`Self::try_from_str()`].
8787
///
8888
/// ✨ *Enabled with the `ixdtf` Cargo feature.*
89-
pub fn try_from_utf8(ixdtf_str: &[u8], calendar: A) -> Result<Self, ParseError> {
90-
let ixdtf_record = IxdtfParser::from_utf8(ixdtf_str).parse()?;
89+
pub fn try_from_utf8(rfc_9557_str: &[u8], calendar: A) -> Result<Self, ParseError> {
90+
let ixdtf_record = IxdtfParser::from_utf8(rfc_9557_str).parse()?;
9191
Self::try_from_ixdtf_record(&ixdtf_record, calendar)
9292
}
9393

components/datetime/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ bincode = { workspace = true }
6363
criterion = { workspace = true }
6464

6565
[features]
66-
default = ["compiled_data"]
66+
default = ["compiled_data", "ixdtf"]
6767
serde = [
6868
"dep:serde",
6969
"icu_calendar/serde",

components/datetime/benches/datetime.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ fn datetime_benches(c: &mut Criterion) {
3535
time,
3636
// zone is unused but we need to make the types match
3737
zone: TimeZoneInfo::utc()
38-
.at_time((Date::try_new_iso(2024, 1, 1).unwrap(), Time::midnight()))
39-
.with_zone_variant(TimeZoneVariant::Standard),
38+
.at_time((Date::try_new_iso(2024, 1, 1).unwrap(), Time::start_of_day()))
39+
.with_variant(TimeZoneVariant::Standard),
4040
}
4141
}
4242
})

components/datetime/examples/timezone_picker.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use icu::calendar::Date;
88
use icu::datetime::{fieldsets, NoCalendarFormatter};
99
use icu::locale::locale;
1010
use icu::time::Time;
11+
use icu_time::TimeZone;
1112

1213
fn main() {
1314
let parser = icu::time::zone::IanaParser::new();
@@ -22,12 +23,12 @@ fn main() {
2223
let city_formatter =
2324
NoCalendarFormatter::try_new(prefs, fieldsets::zone::ExemplarCity).unwrap();
2425

25-
let reference_date = (Date::try_new_iso(2025, 1, 1).unwrap(), Time::midnight());
26+
let reference_date = (Date::try_new_iso(2025, 1, 1).unwrap(), Time::start_of_day());
2627

2728
let mut grouped_tzs = BTreeMap::<_, Vec<_>>::new();
2829

2930
for tz in parser.iter() {
30-
if tz.0 == "unk" || tz.starts_with("utc") || tz.0 == "gmt" {
31+
if tz == TimeZone::unknown() || tz.as_str().starts_with("utc") || tz.as_str() == "gmt" {
3132
continue;
3233
}
3334

@@ -58,9 +59,7 @@ fn main() {
5859
format!(
5960
"/{}",
6061
offset_formatter.format(
61-
&tzi.time_zone_id()
62-
.with_offset(Some(daylight))
63-
.at_time(reference_date)
62+
&tzi.id().with_offset(Some(daylight)).at_time(reference_date)
6463
)
6564
)
6665
} else {

components/datetime/src/fieldsets.rs

+28-36
Original file line numberDiff line numberDiff line change
@@ -891,8 +891,7 @@ macro_rules! impl_zone_marker {
891891
/// use icu::datetime::NoCalendarFormatter;
892892
/// use icu::time::zone::TimeZoneVariant;
893893
#[doc = concat!("use icu::datetime::fieldsets::zone::", stringify!($type), ";")]
894-
/// use icu::locale::locale;
895-
/// use tinystr::tinystr;
894+
/// use icu::locale::{locale, subtags::subtag};
896895
/// use writeable::assert_writeable_eq;
897896
///
898897
/// let fmt = NoCalendarFormatter::try_new(
@@ -902,10 +901,10 @@ macro_rules! impl_zone_marker {
902901
/// .unwrap();
903902
///
904903
/// // Time zone info for America/Chicago in the summer
905-
/// let zone = TimeZone(tinystr!(8, "uschi"))
904+
/// let zone = TimeZone(subtag!("uschi"))
906905
/// .with_offset("-05".parse().ok())
907-
/// .at_time((Date::try_new_iso(2022, 8, 29).unwrap(), Time::midnight()))
908-
/// .with_zone_variant(TimeZoneVariant::Daylight);
906+
/// .at_time((Date::try_new_iso(2022, 8, 29).unwrap(), Time::start_of_day()))
907+
/// .with_variant(TimeZoneVariant::Daylight);
909908
///
910909
/// assert_writeable_eq!(
911910
/// fmt.format(&zone),
@@ -1218,16 +1217,15 @@ pub mod zone {
12181217
/// use icu::calendar::Gregorian;
12191218
/// use icu::datetime::FixedCalendarDateTimeFormatter;
12201219
/// use icu::datetime::fieldsets::zone::{SpecificLong, SpecificShort};
1221-
/// use icu::locale::locale;
1220+
/// use icu::locale::{locale, subtags::subtag};
12221221
/// use icu::time::zone::TimeZoneVariant;
1223-
/// use tinystr::tinystr;
12241222
/// use writeable::assert_writeable_eq;
12251223
///
12261224
/// // Time zone info for Europe/Istanbul in the winter
1227-
/// let zone = TimeZone(tinystr!(8, "trist"))
1225+
/// let zone = TimeZone(subtag!("trist"))
12281226
/// .with_offset("+02".parse().ok())
1229-
/// .at_time((Date::try_new_iso(2022, 1, 29).unwrap(), Time::midnight()))
1230-
/// .with_zone_variant(TimeZoneVariant::Standard);
1227+
/// .at_time((Date::try_new_iso(2022, 1, 29).unwrap(), Time::start_of_day()))
1228+
/// .with_variant(TimeZoneVariant::Standard);
12311229
///
12321230
/// let fmt = FixedCalendarDateTimeFormatter::<Gregorian, _>::try_new(
12331231
/// locale!("en").into(),
@@ -1260,14 +1258,13 @@ pub mod zone {
12601258
/// use icu::datetime::input::{Date, Iso};
12611259
/// use icu::datetime::FixedCalendarDateTimeFormatter;
12621260
/// use icu::datetime::fieldsets::zone::SpecificLong;
1263-
/// use icu::locale::locale;
1261+
/// use icu::locale::{locale, subtags::subtag};
12641262
/// use icu::datetime::input::{DateTime, Time, TimeZone, UtcOffset};
12651263
/// use icu::time::zone::TimeZoneVariant;
1266-
/// use tinystr::tinystr;
12671264
/// use writeable::assert_writeable_eq;
12681265
///
1269-
/// let datetime = DateTime { date: Date::try_new_gregorian(2024, 10, 18).unwrap(), time: Time::midnight() };
1270-
/// let time_zone_basic = TimeZone(tinystr!(8, "uschi")).with_offset("-06".parse().ok());
1266+
/// let datetime = DateTime { date: Date::try_new_gregorian(2024, 10, 18).unwrap(), time: Time::start_of_day() };
1267+
/// let time_zone_basic = TimeZone(subtag!("uschi")).with_offset("-06".parse().ok());
12711268
/// let time_zone_at_time = time_zone_basic.at_time((datetime.date.to_iso(), datetime.time));
12721269
///
12731270
/// let formatter = FixedCalendarDateTimeFormatter::try_new(
@@ -1303,14 +1300,13 @@ pub mod zone {
13031300
/// use icu::datetime::input::{Date, Iso};
13041301
/// use icu::datetime::FixedCalendarDateTimeFormatter;
13051302
/// use icu::datetime::fieldsets::{T, zone::SpecificShort};
1306-
/// use icu::locale::locale;
1303+
/// use icu::locale::{locale, subtags::subtag};
13071304
/// use icu::datetime::input::{DateTime, Time, TimeZone, UtcOffset};
13081305
/// use icu::time::zone::TimeZoneVariant;
1309-
/// use tinystr::tinystr;
13101306
/// use writeable::assert_writeable_eq;
13111307
///
1312-
/// let datetime = DateTime { Date::try_new_gregorian(2024, 10, 18).unwrap(), time: Time::midnight() };
1313-
/// let time_zone_basic = TimeZone(tinystr!(8, "uschi")).with_offset("-06".parse().ok());
1308+
/// let datetime = DateTime { Date::try_new_gregorian(2024, 10, 18).unwrap(), time: Time::start_of_day() };
1309+
/// let time_zone_basic = TimeZone(subtag!("uschi")).with_offset("-06".parse().ok());
13141310
/// let time_zone_at_time = time_zone_basic.at_time((datetime.date.to_iso(), datetime.time));
13151311
///
13161312
/// let formatter = FixedCalendarDateTimeFormatter::try_new(
@@ -1345,18 +1341,17 @@ pub mod zone {
13451341
/// use icu::datetime::fieldsets::zone::LocalizedOffsetLong;
13461342
/// use icu::datetime::input::{Time, TimeZone, UtcOffset};
13471343
/// use icu::time::zone::TimeZoneVariant;
1348-
/// use tinystr::tinystr;
1349-
/// use icu::locale::locale;
1344+
/// use icu::locale::{locale, subtags::subtag};
13501345
/// use writeable::assert_writeable_eq;
13511346
///
13521347
/// let utc_offset = "-06".parse().unwrap();
1353-
/// let time_zone_basic = TimeZone(tinystr!(8, "uschi")).with_offset(Some(utc_offset));
1348+
/// let time_zone_basic = TimeZone(subtag!("uschi")).with_offset(Some(utc_offset));
13541349
///
13551350
/// let date = Date::try_new_iso(2024, 10, 18).unwrap();
1356-
/// let time = Time::midnight();
1351+
/// let time = Time::start_of_day();
13571352
/// let time_zone_at_time = time_zone_basic.at_time((date, time));
13581353
///
1359-
/// let time_zone_full = time_zone_at_time.with_zone_variant(TimeZoneVariant::Standard);
1354+
/// let time_zone_full = time_zone_at_time.with_variant(TimeZoneVariant::Standard);
13601355
///
13611356
/// let formatter = NoCalendarFormatter::try_new(
13621357
/// locale!("en-US").into(),
@@ -1412,14 +1407,13 @@ pub mod zone {
14121407
/// use icu::calendar::Gregorian;
14131408
/// use icu::datetime::FixedCalendarDateTimeFormatter;
14141409
/// use icu::datetime::fieldsets::zone::GenericShort;
1415-
/// use icu::locale::locale;
1416-
/// use tinystr::tinystr;
1410+
/// use icu::locale::{locale, subtags::subtag};
14171411
/// use writeable::assert_writeable_eq;
14181412
///
14191413
/// // Time zone info for Europe/Istanbul
1420-
/// let zone = TimeZone(tinystr!(8, "trist"))
1414+
/// let zone = TimeZone(subtag!("trist"))
14211415
/// .without_offset()
1422-
/// .at_time((Date::try_new_iso(2022, 1, 29).unwrap(), Time::midnight()));
1416+
/// .at_time((Date::try_new_iso(2022, 1, 29).unwrap(), Time::start_of_day()));
14231417
///
14241418
/// let fmt = FixedCalendarDateTimeFormatter::<Gregorian, _>::try_new(
14251419
/// locale!("en").into(),
@@ -1457,7 +1451,7 @@ pub mod zone {
14571451
/// let time_zone = IanaParser::new()
14581452
/// .parse("America/Chicago")
14591453
/// .with_offset("-05".parse().ok())
1460-
/// .at_time((Date::try_new_iso(2022, 8, 29).unwrap(), Time::midnight()));
1454+
/// .at_time((Date::try_new_iso(2022, 8, 29).unwrap(), Time::start_of_day()));
14611455
/// assert_writeable_eq!(
14621456
/// tzf.format(&time_zone),
14631457
/// "CT"
@@ -1467,7 +1461,7 @@ pub mod zone {
14671461
/// let time_zone = IanaParser::new()
14681462
/// .parse("Pacific/Honolulu")
14691463
/// .with_offset("-10".parse().ok())
1470-
/// .at_time((Date::try_new_iso(2022, 8, 29).unwrap(), Time::midnight()));
1464+
/// .at_time((Date::try_new_iso(2022, 8, 29).unwrap(), Time::start_of_day()));
14711465
/// assert_writeable_eq!(
14721466
/// tzf.format(&time_zone),
14731467
/// "HST"
@@ -1477,7 +1471,7 @@ pub mod zone {
14771471
/// let time_zone = IanaParser::new()
14781472
/// .parse("America/Chigagou")
14791473
/// .with_offset("-05".parse().ok())
1480-
/// .at_time((Date::try_new_iso(2022, 8, 29).unwrap(), Time::midnight()));
1474+
/// .at_time((Date::try_new_iso(2022, 8, 29).unwrap(), Time::start_of_day()));
14811475
/// assert_writeable_eq!(
14821476
/// tzf.format(&time_zone),
14831477
/// "GMT-5"
@@ -1491,11 +1485,10 @@ pub mod zone {
14911485
/// use icu::datetime::NoCalendarFormatter;
14921486
/// use icu::datetime::fieldsets::zone::GenericLong;
14931487
/// use icu::datetime::input::TimeZone;
1494-
/// use tinystr::tinystr;
1495-
/// use icu::locale::locale;
1488+
/// use icu::locale::{locale, subtags::subtag};
14961489
/// use writeable::assert_writeable_eq;
14971490
///
1498-
/// let time_zone_basic = TimeZone(tinystr!(8, "uschi")).without_offset();
1491+
/// let time_zone_basic = TimeZone(subtag!("uschi")).without_offset();
14991492
///
15001493
/// let formatter = NoCalendarFormatter::try_new(
15011494
/// locale!("en-US").into(),
@@ -1533,11 +1526,10 @@ pub mod zone {
15331526
/// use icu::datetime::FixedCalendarDateTimeFormatter;
15341527
/// use icu::datetime::fieldsets::zone::GenericShort;
15351528
/// use icu::datetime::input::TimeZone;
1536-
/// use tinystr::tinystr;
1537-
/// use icu::locale::locale;
1529+
/// use icu::locale::{locale, subtags::subtag};
15381530
/// use writeable::assert_writeable_eq;
15391531
///
1540-
/// let time_zone_basic = TimeZone(tinystr!(8, "uschi")).with_offset("-06".parse().ok());
1532+
/// let time_zone_basic = TimeZone(subtag!("uschi")).with_offset("-06".parse().ok());
15411533
///
15421534
/// let formatter = FixedCalendarDateTimeFormatter::try_new(
15431535
/// locale!("en-US").into(),

components/datetime/src/format/datetime.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ fn perform_timezone_fallback(
566566
Ok(match r {
567567
Ok(()) => Ok(()),
568568
Err(e) => {
569-
if let Some(offset) = input.offset {
569+
if let Some(offset) = input.zone_offset {
570570
w.with_part(PART, |w| {
571571
w.with_part(Part::ERROR, |w| {
572572
Iso8601Format::without_z(field.length).format_infallible(w, offset)

components/datetime/src/format/input.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ pub struct DateTimeInputUnchecked {
4747
pub(crate) subsecond: Option<Nanosecond>,
4848
/// The time zone ID, required for field sets with
4949
/// certain time zone styles.
50-
pub(crate) time_zone_id: Option<TimeZone>,
50+
pub(crate) zone_id: Option<TimeZone>,
5151
/// The time zone UTC offset, required for field sets with
5252
/// certain time zone styles.
53-
pub(crate) offset: Option<UtcOffset>,
53+
pub(crate) zone_offset: Option<UtcOffset>,
5454
/// The time zone variant, required for field sets with
5555
/// certain time zone styles.
5656
pub(crate) zone_variant: Option<TimeZoneVariant>,
5757
/// The local ISO time, required for field sets with
5858
/// certain time zone styles.
59-
pub(crate) local_time: Option<(Date<Iso>, Time)>,
59+
pub(crate) zone_local_time: Option<(Date<Iso>, Time)>,
6060
}
6161

6262
impl DateTimeInputUnchecked {
@@ -79,17 +79,17 @@ impl DateTimeInputUnchecked {
7979

8080
/// Sets the time zone UTC offset.
8181
pub fn set_time_zone_utc_offset(&mut self, offset: UtcOffset) {
82-
self.offset = Some(offset);
82+
self.zone_offset = Some(offset);
8383
}
8484

8585
/// Sets the time zone ID.
8686
pub fn set_time_zone_id(&mut self, id: TimeZone) {
87-
self.time_zone_id = Some(id);
87+
self.zone_id = Some(id);
8888
}
8989

9090
/// Sets the local time for time zone name resolution.
9191
pub fn set_time_zone_local_time(&mut self, local_time: (Date<Iso>, Time)) {
92-
self.local_time = Some(local_time);
92+
self.zone_local_time = Some(local_time);
9393
}
9494

9595
/// Sets the time zone variant.
@@ -128,10 +128,10 @@ impl DateTimeInputUnchecked {
128128
minute: GetField::<T::MinuteInput>::get_field(input).into_option(),
129129
second: GetField::<T::SecondInput>::get_field(input).into_option(),
130130
subsecond: GetField::<T::NanosecondInput>::get_field(input).into_option(),
131-
time_zone_id: GetField::<Z::TimeZoneIdInput>::get_field(input).into_option(),
132-
offset: GetField::<Z::TimeZoneOffsetInput>::get_field(input).into_option(),
131+
zone_id: GetField::<Z::TimeZoneIdInput>::get_field(input).into_option(),
132+
zone_offset: GetField::<Z::TimeZoneOffsetInput>::get_field(input).into_option(),
133133
zone_variant: GetField::<Z::TimeZoneVariantInput>::get_field(input).into_option(),
134-
local_time: GetField::<Z::TimeZoneLocalTimeInput>::get_field(input).into_option(),
134+
zone_local_time: GetField::<Z::TimeZoneLocalTimeInput>::get_field(input).into_option(),
135135
}
136136
}
137137
}

0 commit comments

Comments
 (0)