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

Respect length in zone fieldsets #6029

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions components/datetime/examples/timezone_picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ fn main() {

let prefs = locale!("en").into();

let offset_formatter = DateTimeFormatter::try_new(prefs, fieldsets::O::new()).unwrap();
let non_location_formatter = DateTimeFormatter::try_new(prefs, fieldsets::V::new()).unwrap();
let offset_formatter = DateTimeFormatter::try_new(prefs, fieldsets::O::long()).unwrap();
let non_location_formatter = DateTimeFormatter::try_new(prefs, fieldsets::V::long()).unwrap();
let city_formatter = DateTimeFormatter::try_new(prefs, fieldsets::X::new()).unwrap();

let reference_date = (Date::try_new_iso(2025, 1, 1).unwrap(), Time::midnight());
Expand Down
31 changes: 16 additions & 15 deletions components/datetime/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@
//!
//! // Time and Time Zone
//! // Short length
//! // Long specific non-location time zone
//! // specific non-location time zone
//! // Display time to the millisecond
//! // Render for column alignment
//!
//! let static_field_set = fieldsets::T::short()
//! .with_time_precision(TimePrecision::FractionalSecond(FractionalSecondDigits::F3))
//! .with_alignment(Alignment::Column)
//! .with_zone_specific_long();
//! .with_zone(fieldsets::Z::short());
robertbastian marked this conversation as resolved.
Show resolved Hide resolved
//!
//! let mut builder = FieldSetBuilder::new();
//! builder.length = Some(Length::Short);
Expand Down Expand Up @@ -155,27 +155,27 @@ pub enum DateFields {
)]
#[non_exhaustive]
pub enum ZoneStyle {
/// The long specific non-location format, as in
/// “Pacific Daylight Time”.
/// The specific non-location format, as in
/// “Pacific Daylight Time” or “PDT”
Z,
/// The short specific non-location format, as in
/// “PDT”.
Zs,
/// The long offset format, as in
/// “GMT−8:00”.
O,
/// The short offset format, as in
/// “GMT−8”.
Os,
/// The long generic non-location format, as in
/// “Pacific Time”.
/// The generic non-location format, as in
/// “Pacific Time” or “PT”.
V,
/// The short generic non-location format, as in
/// “PT”.
Vs,
/// The offset format, as in
/// “GMT−8:00” or “GMT-8”.
O,
/// The location format, as in
/// “Los Angeles time”.
L,
/// The exemplar city format, as in
/// “Los Angeles.
X,
}

/// An error that occurs when creating a [field set](crate::fieldsets) from a builder.
Expand Down Expand Up @@ -438,16 +438,18 @@ impl FieldSetBuilder {
}

fn build_zone_without_checking_options(&mut self) -> Result<ZoneFieldSet, BuilderError> {
let length = self.length;
let zone_field_set = match self.zone_style.take() {
Some(ZoneStyle::Z) => ZoneFieldSet::Z(fieldsets::Z::take_from_builder(self)),
Some(ZoneStyle::Zs) => ZoneFieldSet::Zs(fieldsets::Zs::take_from_builder(self)),
Some(ZoneStyle::O) => ZoneFieldSet::O(fieldsets::O::take_from_builder(self)),
Some(ZoneStyle::Os) => ZoneFieldSet::Os(fieldsets::Os::take_from_builder(self)),
Some(ZoneStyle::V) => ZoneFieldSet::V(fieldsets::V::take_from_builder(self)),
Some(ZoneStyle::Vs) => ZoneFieldSet::Vs(fieldsets::Vs::take_from_builder(self)),
Some(ZoneStyle::O) => ZoneFieldSet::O(fieldsets::O::take_from_builder(self)),
Some(ZoneStyle::L) => ZoneFieldSet::L(fieldsets::L::take_from_builder(self)),
Some(ZoneStyle::X) => ZoneFieldSet::X(fieldsets::X::take_from_builder(self)),
Option::None => return Err(BuilderError::InvalidFields),
};
self.length = length;
Ok(zone_field_set)
}

Expand Down Expand Up @@ -605,7 +607,6 @@ mod tests {
ZoneStyle::Z,
ZoneStyle::Zs,
ZoneStyle::O,
ZoneStyle::Os,
ZoneStyle::V,
ZoneStyle::Vs,
ZoneStyle::L,
Expand Down
13 changes: 7 additions & 6 deletions components/datetime/src/combo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{provider::neo::*, scaffold::*};
/// ```
/// use icu::datetime::fieldsets::{Combo, E, L};
///
/// let field_set = E::long().with_zone_location();
/// let field_set = E::long().with_zone(L::new());
/// ```
///
/// Format the weekday, hour, and location-based zone:
Expand All @@ -31,7 +31,7 @@ use crate::{provider::neo::*, scaffold::*};
/// // Note: Combo type can be elided, but it is shown here for demonstration
/// let formatter = DateTimeFormatter::<Combo<ET, L>>::try_new(
/// locale!("en-US").into(),
/// ET::short().hm().with_zone_location(),
/// ET::short().hm().with_zone(L::new()),
/// )
/// .unwrap();
///
Expand All @@ -58,7 +58,7 @@ use crate::{provider::neo::*, scaffold::*};
/// // Note: Combo type can be elided, but it is shown here for demonstration
/// let formatter = FixedCalendarDateTimeFormatter::<_, Combo<ET, L>>::try_new(
/// locale!("en-US").into(),
/// ET::short().hm().with_zone_location(),
/// ET::short().hm().with_zone(L::new()),
/// )
/// .unwrap();
///
Expand All @@ -85,7 +85,7 @@ use crate::{provider::neo::*, scaffold::*};
/// // Note: Combo type can be elided, but it is shown here for demonstration
/// let formatter = DateTimeFormatter::<Combo<DateFieldSet, Vs>>::try_new(
/// locale!("en-US").into(),
/// DateFieldSet::YMD(YMD::long()).with_zone_generic(),
/// DateFieldSet::YMD(YMD::long()).with_zone(Vs::short()),
/// )
/// .unwrap();
///
Expand All @@ -103,15 +103,16 @@ use crate::{provider::neo::*, scaffold::*};
///
/// ```
/// use icu::calendar::Gregorian;
/// use icu::datetime::fieldsets::T;
/// use icu::datetime::fieldsets::{T, Z};
/// use icu::datetime::FixedCalendarDateTimeFormatter;
/// use icu::datetime::options::Length;
/// use icu::locale::locale;
/// use icu::timezone::{ZonedDateTimeParser, ZonedDateTime};
/// use writeable::assert_writeable_eq;
///
/// let formatter = FixedCalendarDateTimeFormatter::try_new(
/// locale!("en-US").into(),
/// T::medium().with_zone_specific_long(),
/// T::medium().with_zone(Z::long()),
/// )
/// .unwrap();
///
Expand Down
41 changes: 28 additions & 13 deletions components/datetime/src/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
use crate::fieldsets::Combo;
use crate::raw::neo::RawOptions;
use crate::scaffold::GetField;
use crate::{fieldsets, provider};
use crate::{fieldsets, provider, Length};
use icu_provider::prelude::*;

/// An enumeration over all possible date field sets.
Expand Down Expand Up @@ -141,27 +141,24 @@ pub enum TimeFieldSet {
///
/// - [`fieldsets::Zs`]
/// - [`fieldsets::O`]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[derive(Debug, Copy, Clone, Eq)]
#[non_exhaustive]
pub enum ZoneFieldSet {
/// The long specific non-location format, as in
/// “Pacific Daylight Time”.
/// The specific non-location format, as in
/// “Pacific Daylight Time” or “PDT”
Z(fieldsets::Z),
/// The short specific non-location format, as in
/// “PDT”.
Zs(fieldsets::Zs),
/// The long offset format, as in
/// “GMT−8:00”.
O(fieldsets::O),
/// The short offset format, as in
/// “GMT−8”.
Os(fieldsets::Os),
/// The long generic non-location format, as in
/// “Pacific Time”.
/// The generic non-location format, as in
/// “Pacific Time” or “PT”.
V(fieldsets::V),
/// The short generic non-location format, as in
/// “PT”.
Vs(fieldsets::Vs),
/// The offset format, as in
/// “GMT−8:00” or “GMT-8”.
O(fieldsets::O),
/// The location format, as in
/// “Los Angeles Time”.
L(fieldsets::L),
Expand All @@ -170,6 +167,25 @@ pub enum ZoneFieldSet {
X(fieldsets::X),
}

impl PartialEq for ZoneFieldSet {
fn eq(&self, other: &Self) -> bool {
use fieldsets as fs;
use ZoneFieldSet::*;
match (*self, *other) {
(Z(a), Z(b)) => a == b,
(Z(fs::Z { length }), Zs(_)) | (Zs(_), Z(fs::Z { length })) => length == Length::Short,
(Zs(a), Zs(b)) => a == b,
(V(a), V(b)) => a == b,
(V(fs::V { length }), Vs(_)) | (Vs(_), V(fs::V { length })) => length == Length::Short,
(Vs(a), Vs(b)) => a == b,
(O(a), O(b)) => a == b,
(L(a), L(b)) => a == b,
(X(a), X(b)) => a == b,
_ => false,
}
}
}

/// An enumeration over all possible date+time composite field sets.
///
/// This is a dynamic field set. For more information, see [`enums`](crate::fieldsets::enums).
Expand Down Expand Up @@ -475,7 +491,6 @@ impl_attrs! {
Z,
Zs,
O,
Os,
V,
Vs,
L,
Expand Down
Loading
Loading