Debian Codegraph @

debian/rust-vendor/icu_datetime/src/format/input.rs

raw
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 //! A collection of utilities for representing and working with dates as an input to
6 //! formatting operations.
7
8 use crate::scaffold::*;
9 use icu_calendar::types::{DayOfYear, RataDie};
10 use icu_calendar::{AsCalendar, Calendar};
11 use icu_time::scaffold::IntoOption;
12 use icu_time::zone::ZoneNameTimestamp;
13 use icu_time::zone::models::{AtTime, Base};
14 use icu_time::{Hour, Minute, Nanosecond, Second, TimeZoneInfo};
15
16 use icu_calendar::Date;
17 use icu_time::{Time, TimeZone, zone::UtcOffset};
18
19 // TODO(#2630) fix up imports to directly import from icu_calendar
20 pub(crate) use icu_calendar::types::{DayOfMonth, MonthInfo, Weekday, YearInfo};
21
22 #[cfg(doc)]
23 use crate::input::*;
24
25 /// An input bag with all possible datetime input fields.
26 ///
27 /// Each input field may or may not be required, depending on the field set
28 /// and the options.
29 #[derive(Debug, Copy, Clone, Default)]
30 #[non_exhaustive]
31 pub struct DateTimeInputUnchecked {
32 /// The year, required for field sets with years (`Y`).
33 pub(crate) year: Option<YearInfo>,
34 /// The month, required for field sets with months (`M`)
35 pub(crate) month: Option<MonthInfo>,
36 /// The day-of-month, required for field sets with days (`D`).
37 pub(crate) day_of_month: Option<DayOfMonth>,
38 /// The weekday, required for field sets with weekdays (`E`).
39 pub(crate) weekday: Option<Weekday>,
40 /// The day-of-year, required for field sets with weeks.
41 pub(crate) day_of_year: Option<DayOfYear>,
42 /// The [`RataDie`] of the day
43 pub(crate) rata_die: Option<RataDie>,
44 /// The hour, required for field sets with times (`T`).
45 pub(crate) hour: Option<Hour>,
46 /// The minute, required for field sets with times (`T`).
47 pub(crate) minute: Option<Minute>,
48 /// The second, required for field sets with times (`T`).
49 pub(crate) second: Option<Second>,
50 /// The subsecond, required for field sets with times (`T`).
51 pub(crate) subsecond: Option<Nanosecond>,
52 /// The time zone ID, required for field sets with
53 /// certain time zone styles.
54 pub(crate) zone_id: Option<TimeZone>,
55 /// The time zone UTC offset, required for field sets with
56 /// certain time zone styles.
57 pub(crate) zone_offset: Option<UtcOffset>,
58 /// The local ISO time, required for field sets with
59 /// certain time zone styles.
60 pub(crate) zone_name_timestamp: Option<ZoneNameTimestamp>,
61 }
62
63 impl DateTimeInputUnchecked {
64 /// Sets all fields from a [`Date`] input.
65 ///
66 /// This method does not check the calendar of the date! The caller is
67 /// responsible for making sure the calendar matches the formatter.
68 pub fn set_date_fields_unchecked<C: Calendar, A: AsCalendar<Calendar = C>>(
69 &mut self,
70 date_in_calendar: Date<A>,
71 ) {
72 self.year = Some(date_in_calendar.year());
73 self.month = Some(date_in_calendar.month());
74 self.day_of_month = Some(date_in_calendar.day_of_month());
75 self.weekday = Some(date_in_calendar.weekday());
76 self.day_of_year = Some(date_in_calendar.day_of_year());
77 }
78
79 /// Sets all fields from a [`Time`] input.
80 pub fn set_time_fields(&mut self, time: Time) {
81 self.hour = Some(time.hour);
82 self.minute = Some(time.minute);
83 self.second = Some(time.second);
84 self.subsecond = Some(time.subsecond);
85 }
86
87 /// Sets the time zone UTC offset.
88 pub fn set_time_zone_utc_offset(&mut self, utc_offset: UtcOffset) {
89 self.zone_offset = Some(utc_offset);
90 }
91
92 /// Sets all fields from a [`TimeZoneInfo<Base>`] input.
93 pub fn set_time_zone_info_base_fields(&mut self, info: TimeZoneInfo<Base>) {
94 self.zone_id = Some(info.id());
95 self.zone_offset = self.zone_offset.or_else(|| info.offset());
96 }
97
98 /// Sets all fields from a [`TimeZoneInfo<AtTime>`] input.
99 pub fn set_time_zone_info_at_time_fields(&mut self, info: TimeZoneInfo<AtTime>) {
100 self.zone_id = Some(info.id());
101 self.zone_offset = self.zone_offset.or_else(|| info.offset());
102 self.zone_name_timestamp = Some(info.zone_name_timestamp());
103 }
104
105 /// Sets the time zone ID.
106 pub fn set_time_zone_id(&mut self, id: TimeZone) {
107 self.zone_id = Some(id);
108 }
109
110 /// Sets the local time for time zone name resolution.
111 pub fn set_time_zone_name_timestamp(&mut self, timestamp: ZoneNameTimestamp) {
112 self.zone_name_timestamp = Some(timestamp);
113 }
114
115 /// No-op
116 #[deprecated]
117 pub fn set_time_zone_variant(&mut self, _zone_variant: icu_time::zone::TimeZoneVariant) {}
118
119 /// Construct given neo date input instances.
120 pub(crate) fn extract_from_neo_input<D, T, Z, I>(input: &I) -> Self
121 where
122 D: DateInputMarkers,
123 T: TimeMarkers,
124 Z: ZoneMarkers,
125 I: ?Sized
126 + GetField<D::YearInput>
127 + GetField<D::MonthInput>
128 + GetField<D::DayOfMonthInput>
129 + GetField<D::DayOfWeekInput>
130 + GetField<D::DayOfYearInput>
131 + GetField<D::RataDieInput>
132 + GetField<T::HourInput>
133 + GetField<T::MinuteInput>
134 + GetField<T::SecondInput>
135 + GetField<T::NanosecondInput>
136 + GetField<Z::TimeZoneIdInput>
137 + GetField<Z::TimeZoneOffsetInput>
138 + GetField<Z::TimeZoneNameTimestampInput>,
139 {
140 Self {
141 year: GetField::<D::YearInput>::get_field(input).into_option(),
142 month: GetField::<D::MonthInput>::get_field(input).into_option(),
143 day_of_month: GetField::<D::DayOfMonthInput>::get_field(input).into_option(),
144 weekday: GetField::<D::DayOfWeekInput>::get_field(input).into_option(),
145 day_of_year: GetField::<D::DayOfYearInput>::get_field(input).into_option(),
146 rata_die: GetField::<D::RataDieInput>::get_field(input).into_option(),
147 hour: GetField::<T::HourInput>::get_field(input).into_option(),
148 minute: GetField::<T::MinuteInput>::get_field(input).into_option(),
149 second: GetField::<T::SecondInput>::get_field(input).into_option(),
150 subsecond: GetField::<T::NanosecondInput>::get_field(input).into_option(),
151 zone_id: GetField::<Z::TimeZoneIdInput>::get_field(input).into_option(),
152 zone_offset: GetField::<Z::TimeZoneOffsetInput>::get_field(input).into_option(),
153 zone_name_timestamp: GetField::<Z::TimeZoneNameTimestampInput>::get_field(input)
154 .into_option(),
155 }
156 }
157
158 /// Checks if all the time zone fields (currently: offset, ID, and name timestamp) are the same.
159 #[allow(dead_code)] // TODO(#5448): range formatting is WIP
160 pub(crate) fn has_same_zone(&self, other: &Self) -> bool {
161 self.zone_offset == other.zone_offset
162 && self.zone_id == other.zone_id
163 && self.zone_name_timestamp == other.zone_name_timestamp
164 }
165 }