// This file is part of ICU4X. For terms of use, please see the file // called LICENSE at the top level of the ICU4X source tree // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). use crate::provider::semantic_skeletons::DatetimePatternsGlueV1; use crate::scaffold::*; /// Struct for combining date/time fields with zone fields. /// /// This struct produces "composite field sets" as defined in UTS 35. /// See [`fieldsets`](crate::fieldsets). /// /// # Examples /// /// The only way to construct a combo field set (in this case, weekday with location-based zone): /// /// ``` /// use icu::datetime::fieldsets::{Combo, E, zone::Location}; /// /// let field_set = E::long().with_zone(Location); /// ``` /// /// Format the weekday, hour, and location-based zone: /// /// ``` /// use icu::datetime::DateTimeFormatter; /// use icu::datetime::fieldsets::{self, Combo, zone}; /// use icu::datetime::input::ZonedDateTime; /// use icu::locale::locale; /// use icu::time::zone::IanaParser; /// use writeable::assert_writeable_eq; /// /// // Note: Combo type can be elided, but it is shown here for demonstration /// let formatter = /// DateTimeFormatter::>::try_new( /// locale!("en-US").into(), /// fieldsets::E::short() /// .with_time_hm() /// .with_zone(zone::Location), /// ) /// .unwrap(); /// /// let zdt = ZonedDateTime::try_location_only_from_str( /// "2024-10-18T15:44[America/Los_Angeles]", /// formatter.calendar(), /// IanaParser::new(), /// ) /// .unwrap(); /// /// assert_writeable_eq!( /// formatter.format(&zdt), /// "Fri, 3:44 PM Los Angeles Time" /// ); /// ``` /// /// Same thing with a fixed calendar formatter: /// /// ``` /// use icu::calendar::Gregorian; /// use icu::datetime::FixedCalendarDateTimeFormatter; /// use icu::datetime::fieldsets::{self, Combo, zone}; /// use icu::datetime::input::ZonedDateTime; /// use icu::locale::locale; /// use icu::time::zone::IanaParser; /// use writeable::assert_writeable_eq; /// /// // Note: Combo type can be elided, but it is shown here for demonstration /// let formatter = FixedCalendarDateTimeFormatter::< /// _, /// Combo, /// >::try_new( /// locale!("en-US").into(), /// fieldsets::E::short() /// .with_time_hm() /// .with_zone(zone::Location), /// ) /// .unwrap(); /// /// let zdt = ZonedDateTime::try_location_only_from_str( /// "2024-10-18T15:44[America/Los_Angeles]", /// Gregorian, /// IanaParser::new(), /// ) /// .unwrap(); /// /// assert_writeable_eq!( /// formatter.format(&zdt), /// "Fri, 3:44 PM Los Angeles Time" /// ); /// ``` /// /// Mix a dynamic [`DateFieldSet`](crate::fieldsets::enums::DateFieldSet) /// with a static time zone: /// /// ``` /// use icu::datetime::DateTimeFormatter; /// use icu::datetime::fieldsets::{ /// Combo, YMD, enums::DateFieldSet, zone::GenericShort, /// }; /// use icu::datetime::input::ZonedDateTime; /// use icu::locale::locale; /// use icu::time::zone::IanaParser; /// use writeable::assert_writeable_eq; /// /// // Note: Combo type can be elided, but it is shown here for demonstration /// let formatter = /// DateTimeFormatter::>::try_new( /// locale!("en-US").into(), /// DateFieldSet::YMD(YMD::long()).with_zone(GenericShort), /// ) /// .unwrap(); /// /// let zdt = ZonedDateTime::try_location_only_from_str( /// "2024-10-18T15:44[America/Los_Angeles]", /// formatter.calendar(), /// IanaParser::new(), /// ) /// .unwrap(); /// /// assert_writeable_eq!(formatter.format(&zdt), "October 18, 2024 PT"); /// ``` /// /// Format with a time of day and long time zone: /// /// ``` /// # #[cfg(all(feature = "unstable", feature = "ixdtf"))] { /// use icu::datetime::NoCalendarFormatter; /// use icu::datetime::fieldsets::{T, zone::SpecificLong}; /// use icu::locale::locale; /// use icu::time::ZonedTime; /// use icu::time::zone::iana::IanaParser; /// use writeable::assert_writeable_eq; /// /// let formatter = NoCalendarFormatter::try_new( /// locale!("en-US").into(), /// T::medium().with_zone(SpecificLong), /// ) /// .unwrap(); /// /// let zoned_time = ZonedTime::try_strict_from_str( /// "15:44:00-07:00[America/Los_Angeles]", /// IanaParser::new(), /// ) /// .unwrap(); /// /// assert_writeable_eq!( /// formatter.format(&zoned_time), /// "3:44:00 PM Pacific Daylight Time" /// ); /// # } /// ``` #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub struct Combo { date_time_field_set: DT, zone_field_set: Z, } impl Combo { #[inline] pub(crate) const fn new(date_time_field_set: DT, zone_field_set: Z) -> Self { Self { date_time_field_set, zone_field_set, } } } impl UnstableSealed for Combo {} impl Combo { #[inline] pub(crate) fn dt(self) -> DT { self.date_time_field_set } #[inline] pub(crate) fn z(self) -> Z { self.zone_field_set } } impl DateTimeNamesMarker for Combo where DT: DateTimeNamesMarker, Z: DateTimeNamesMarker, { type YearNames = DT::YearNames; type MonthNames = DT::MonthNames; type WeekdayNames = DT::WeekdayNames; type DayPeriodNames = DT::DayPeriodNames; type ZoneEssentials = Z::ZoneEssentials; type ZoneLocations = Z::ZoneLocations; type ZoneLocationsRoot = Z::ZoneLocationsRoot; type ZoneExemplars = Z::ZoneExemplars; type ZoneExemplarsRoot = Z::ZoneExemplarsRoot; type ZoneGenericLong = Z::ZoneGenericLong; type ZoneGenericShort = Z::ZoneGenericShort; type ZoneStandardLong = Z::ZoneStandardLong; type ZoneSpecificLong = Z::ZoneSpecificLong; type ZoneSpecificShort = Z::ZoneSpecificShort; type MetazoneLookup = Z::MetazoneLookup; } impl DateTimeMarkers for Combo where DT: DateTimeMarkers, Z: DateTimeMarkers, { type D = DT::D; type T = DT::T; type Z = Z::Z; type GluePatternV1 = datetime_marker_helper!(@glue, yes); }