| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
use crate::provider::semantic_skeletons::DatetimePatternsGlueV1; |
| 6 |
use crate::scaffold::*; |
| 7 |
|
| 8 |
/// Struct for combining date/time fields with zone fields. |
| 9 |
/// |
| 10 |
/// This struct produces "composite field sets" as defined in UTS 35. |
| 11 |
/// See [`fieldsets`](crate::fieldsets). |
| 12 |
/// |
| 13 |
/// # Examples |
| 14 |
/// |
| 15 |
/// The only way to construct a combo field set (in this case, weekday with location-based zone): |
| 16 |
/// |
| 17 |
/// ``` |
| 18 |
/// use icu::datetime::fieldsets::{Combo, E, zone::Location}; |
| 19 |
/// |
| 20 |
/// let field_set = E::long().with_zone(Location); |
| 21 |
/// ``` |
| 22 |
/// |
| 23 |
/// Format the weekday, hour, and location-based zone: |
| 24 |
/// |
| 25 |
/// ``` |
| 26 |
/// use icu::datetime::DateTimeFormatter; |
| 27 |
/// use icu::datetime::fieldsets::{self, Combo, zone}; |
| 28 |
/// use icu::datetime::input::ZonedDateTime; |
| 29 |
/// use icu::locale::locale; |
| 30 |
/// use icu::time::zone::IanaParser; |
| 31 |
/// use writeable::assert_writeable_eq; |
| 32 |
/// |
| 33 |
/// // Note: Combo type can be elided, but it is shown here for demonstration |
| 34 |
/// let formatter = |
| 35 |
/// DateTimeFormatter::<Combo<fieldsets::ET, zone::Location>>::try_new( |
| 36 |
/// locale!("en-US").into(), |
| 37 |
/// fieldsets::E::short() |
| 38 |
/// .with_time_hm() |
| 39 |
/// .with_zone(zone::Location), |
| 40 |
/// ) |
| 41 |
/// .unwrap(); |
| 42 |
/// |
| 43 |
/// let zdt = ZonedDateTime::try_location_only_from_str( |
| 44 |
/// "2024-10-18T15:44[America/Los_Angeles]", |
| 45 |
/// formatter.calendar(), |
| 46 |
/// IanaParser::new(), |
| 47 |
/// ) |
| 48 |
/// .unwrap(); |
| 49 |
/// |
| 50 |
/// assert_writeable_eq!( |
| 51 |
/// formatter.format(&zdt), |
| 52 |
/// "Fri, 3:44 PM Los Angeles Time" |
| 53 |
/// ); |
| 54 |
/// ``` |
| 55 |
/// |
| 56 |
/// Same thing with a fixed calendar formatter: |
| 57 |
/// |
| 58 |
/// ``` |
| 59 |
/// use icu::calendar::Gregorian; |
| 60 |
/// use icu::datetime::FixedCalendarDateTimeFormatter; |
| 61 |
/// use icu::datetime::fieldsets::{self, Combo, zone}; |
| 62 |
/// use icu::datetime::input::ZonedDateTime; |
| 63 |
/// use icu::locale::locale; |
| 64 |
/// use icu::time::zone::IanaParser; |
| 65 |
/// use writeable::assert_writeable_eq; |
| 66 |
/// |
| 67 |
/// // Note: Combo type can be elided, but it is shown here for demonstration |
| 68 |
/// let formatter = FixedCalendarDateTimeFormatter::< |
| 69 |
/// _, |
| 70 |
/// Combo<fieldsets::ET, zone::Location>, |
| 71 |
/// >::try_new( |
| 72 |
/// locale!("en-US").into(), |
| 73 |
/// fieldsets::E::short() |
| 74 |
/// .with_time_hm() |
| 75 |
/// .with_zone(zone::Location), |
| 76 |
/// ) |
| 77 |
/// .unwrap(); |
| 78 |
/// |
| 79 |
/// let zdt = ZonedDateTime::try_location_only_from_str( |
| 80 |
/// "2024-10-18T15:44[America/Los_Angeles]", |
| 81 |
/// Gregorian, |
| 82 |
/// IanaParser::new(), |
| 83 |
/// ) |
| 84 |
/// .unwrap(); |
| 85 |
/// |
| 86 |
/// assert_writeable_eq!( |
| 87 |
/// formatter.format(&zdt), |
| 88 |
/// "Fri, 3:44 PM Los Angeles Time" |
| 89 |
/// ); |
| 90 |
/// ``` |
| 91 |
/// |
| 92 |
/// Mix a dynamic [`DateFieldSet`](crate::fieldsets::enums::DateFieldSet) |
| 93 |
/// with a static time zone: |
| 94 |
/// |
| 95 |
/// ``` |
| 96 |
/// use icu::datetime::DateTimeFormatter; |
| 97 |
/// use icu::datetime::fieldsets::{ |
| 98 |
/// Combo, YMD, enums::DateFieldSet, zone::GenericShort, |
| 99 |
/// }; |
| 100 |
/// use icu::datetime::input::ZonedDateTime; |
| 101 |
/// use icu::locale::locale; |
| 102 |
/// use icu::time::zone::IanaParser; |
| 103 |
/// use writeable::assert_writeable_eq; |
| 104 |
/// |
| 105 |
/// // Note: Combo type can be elided, but it is shown here for demonstration |
| 106 |
/// let formatter = |
| 107 |
/// DateTimeFormatter::<Combo<DateFieldSet, GenericShort>>::try_new( |
| 108 |
/// locale!("en-US").into(), |
| 109 |
/// DateFieldSet::YMD(YMD::long()).with_zone(GenericShort), |
| 110 |
/// ) |
| 111 |
/// .unwrap(); |
| 112 |
/// |
| 113 |
/// let zdt = ZonedDateTime::try_location_only_from_str( |
| 114 |
/// "2024-10-18T15:44[America/Los_Angeles]", |
| 115 |
/// formatter.calendar(), |
| 116 |
/// IanaParser::new(), |
| 117 |
/// ) |
| 118 |
/// .unwrap(); |
| 119 |
/// |
| 120 |
/// assert_writeable_eq!(formatter.format(&zdt), "October 18, 2024 PT"); |
| 121 |
/// ``` |
| 122 |
/// |
| 123 |
/// Format with a time of day and long time zone: |
| 124 |
/// |
| 125 |
/// ``` |
| 126 |
/// # #[cfg(all(feature = "unstable", feature = "ixdtf"))] { |
| 127 |
/// use icu::datetime::NoCalendarFormatter; |
| 128 |
/// use icu::datetime::fieldsets::{T, zone::SpecificLong}; |
| 129 |
/// use icu::locale::locale; |
| 130 |
/// use icu::time::ZonedTime; |
| 131 |
/// use icu::time::zone::iana::IanaParser; |
| 132 |
/// use writeable::assert_writeable_eq; |
| 133 |
/// |
| 134 |
/// let formatter = NoCalendarFormatter::try_new( |
| 135 |
/// locale!("en-US").into(), |
| 136 |
/// T::medium().with_zone(SpecificLong), |
| 137 |
/// ) |
| 138 |
/// .unwrap(); |
| 139 |
/// |
| 140 |
/// let zoned_time = ZonedTime::try_strict_from_str( |
| 141 |
/// "15:44:00-07:00[America/Los_Angeles]", |
| 142 |
/// IanaParser::new(), |
| 143 |
/// ) |
| 144 |
/// .unwrap(); |
| 145 |
/// |
| 146 |
/// assert_writeable_eq!( |
| 147 |
/// formatter.format(&zoned_time), |
| 148 |
/// "3:44:00 PM Pacific Daylight Time" |
| 149 |
/// ); |
| 150 |
/// # } |
| 151 |
/// ``` |
| 152 |
#[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 153 |
pub struct Combo<DT, Z> { |
| 154 |
date_time_field_set: DT, |
| 155 |
zone_field_set: Z, |
| 156 |
} |
| 157 |
|
| 158 |
impl<DT, Z> Combo<DT, Z> { |
| 159 |
#[inline] |
| 160 |
pub(crate) const fn new(date_time_field_set: DT, zone_field_set: Z) -> Self { |
| 161 |
Self { |
| 162 |
date_time_field_set, |
| 163 |
zone_field_set, |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
impl<DT, Z> UnstableSealed for Combo<DT, Z> {} |
| 169 |
|
| 170 |
impl<DT, Z> Combo<DT, Z> { |
| 171 |
#[inline] |
| 172 |
pub(crate) fn dt(self) -> DT { |
| 173 |
self.date_time_field_set |
| 174 |
} |
| 175 |
#[inline] |
| 176 |
pub(crate) fn z(self) -> Z { |
| 177 |
self.zone_field_set |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
impl<DT, Z> DateTimeNamesMarker for Combo<DT, Z> |
| 182 |
where |
| 183 |
DT: DateTimeNamesMarker, |
| 184 |
Z: DateTimeNamesMarker, |
| 185 |
{ |
| 186 |
type YearNames = DT::YearNames; |
| 187 |
type MonthNames = DT::MonthNames; |
| 188 |
type WeekdayNames = DT::WeekdayNames; |
| 189 |
type DayPeriodNames = DT::DayPeriodNames; |
| 190 |
type ZoneEssentials = Z::ZoneEssentials; |
| 191 |
type ZoneLocations = Z::ZoneLocations; |
| 192 |
type ZoneLocationsRoot = Z::ZoneLocationsRoot; |
| 193 |
type ZoneExemplars = Z::ZoneExemplars; |
| 194 |
type ZoneExemplarsRoot = Z::ZoneExemplarsRoot; |
| 195 |
type ZoneGenericLong = Z::ZoneGenericLong; |
| 196 |
type ZoneGenericShort = Z::ZoneGenericShort; |
| 197 |
type ZoneStandardLong = Z::ZoneStandardLong; |
| 198 |
type ZoneSpecificLong = Z::ZoneSpecificLong; |
| 199 |
type ZoneSpecificShort = Z::ZoneSpecificShort; |
| 200 |
type MetazoneLookup = Z::MetazoneLookup; |
| 201 |
} |
| 202 |
|
| 203 |
impl<DT, Z> DateTimeMarkers for Combo<DT, Z> |
| 204 |
where |
| 205 |
DT: DateTimeMarkers, |
| 206 |
Z: DateTimeMarkers, |
| 207 |
{ |
| 208 |
type D = DT::D; |
| 209 |
type T = DT::T; |
| 210 |
type Z = Z::Z; |
| 211 |
type GluePatternV1 = datetime_marker_helper!(@glue, yes); |
| 212 |
} |