Debian Codegraph @

debian/rust-vendor/icu_datetime/src/time_crate.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 use crate::scaffold::{ConvertCalendar, GetField, InFixedCalendar, UnstableSealed};
6 use icu_calendar::types::{DayOfMonth, DayOfYear, MonthInfo, RataDie, Weekday, YearInfo};
7 use icu_calendar::{AnyCalendar, Date, Gregorian};
8 use icu_time::zone::{UtcOffset, ZoneNameTimestamp};
9 use icu_time::{DateTime, Hour, Minute, Nanosecond, Second, Time, ZonedDateTime};
10
11 impl UnstableSealed for time::Time {}
12 impl<C> InFixedCalendar<C> for time::Time {}
13 impl GetField<()> for time::Time {
14 fn get_field(&self) {}
15 }
16
17 impl GetField<Hour> for time::Time {
18 fn get_field(&self) -> Hour {
19 Time::from(*self).hour
20 }
21 }
22
23 impl GetField<Minute> for time::Time {
24 fn get_field(&self) -> Minute {
25 Time::from(*self).minute
26 }
27 }
28
29 impl GetField<Second> for time::Time {
30 fn get_field(&self) -> Second {
31 Time::from(*self).second
32 }
33 }
34
35 impl GetField<Nanosecond> for time::Time {
36 fn get_field(&self) -> Nanosecond {
37 Time::from(*self).subsecond
38 }
39 }
40
41 impl UnstableSealed for time::Date {}
42 impl GetField<()> for time::Date {
43 fn get_field(&self) {}
44 }
45
46 impl GetField<YearInfo> for time::Date {
47 #[inline]
48 fn get_field(&self) -> YearInfo {
49 Date::from(*self).year()
50 }
51 }
52
53 impl GetField<MonthInfo> for time::Date {
54 #[inline]
55 fn get_field(&self) -> MonthInfo {
56 Date::from(*self).month()
57 }
58 }
59
60 impl GetField<DayOfMonth> for time::Date {
61 #[inline]
62 fn get_field(&self) -> DayOfMonth {
63 DayOfMonth(self.day())
64 }
65 }
66
67 impl GetField<Weekday> for time::Date {
68 #[inline]
69 fn get_field(&self) -> Weekday {
70 self.weekday().get_field()
71 }
72 }
73
74 impl GetField<DayOfYear> for time::Date {
75 #[inline]
76 fn get_field(&self) -> DayOfYear {
77 DayOfYear(self.ordinal())
78 }
79 }
80
81 impl GetField<RataDie> for time::Date {
82 #[inline]
83 fn get_field(&self) -> RataDie {
84 Date::from(*self).to_rata_die()
85 }
86 }
87
88 impl UnstableSealed for time::UtcOffset {}
89 impl<C> InFixedCalendar<C> for time::UtcOffset {}
90 impl GetField<()> for time::UtcOffset {
91 fn get_field(&self) {}
92 }
93
94 impl GetField<Option<UtcOffset>> for time::UtcOffset {
95 #[inline]
96 fn get_field(&self) -> Option<UtcOffset> {
97 Some((*self).into())
98 }
99 }
100
101 impl UnstableSealed for time::PrimitiveDateTime {}
102 impl GetField<()> for time::PrimitiveDateTime {
103 fn get_field(&self) {}
104 }
105
106 impl GetField<Hour> for time::PrimitiveDateTime {
107 fn get_field(&self) -> Hour {
108 self.time().get_field()
109 }
110 }
111
112 impl GetField<Minute> for time::PrimitiveDateTime {
113 fn get_field(&self) -> Minute {
114 self.time().get_field()
115 }
116 }
117
118 impl GetField<Second> for time::PrimitiveDateTime {
119 fn get_field(&self) -> Second {
120 self.time().get_field()
121 }
122 }
123
124 impl GetField<Nanosecond> for time::PrimitiveDateTime {
125 fn get_field(&self) -> Nanosecond {
126 self.time().get_field()
127 }
128 }
129
130 impl GetField<YearInfo> for time::PrimitiveDateTime {
131 #[inline]
132 fn get_field(&self) -> YearInfo {
133 self.date().get_field()
134 }
135 }
136
137 impl GetField<MonthInfo> for time::PrimitiveDateTime {
138 #[inline]
139 fn get_field(&self) -> MonthInfo {
140 self.date().get_field()
141 }
142 }
143
144 impl GetField<DayOfMonth> for time::PrimitiveDateTime {
145 #[inline]
146 fn get_field(&self) -> DayOfMonth {
147 self.date().get_field()
148 }
149 }
150
151 impl GetField<Weekday> for time::PrimitiveDateTime {
152 #[inline]
153 fn get_field(&self) -> Weekday {
154 self.date().get_field()
155 }
156 }
157
158 impl GetField<DayOfYear> for time::PrimitiveDateTime {
159 #[inline]
160 fn get_field(&self) -> DayOfYear {
161 self.date().get_field()
162 }
163 }
164
165 impl GetField<RataDie> for time::PrimitiveDateTime {
166 #[inline]
167 fn get_field(&self) -> RataDie {
168 self.date().get_field()
169 }
170 }
171
172 impl UnstableSealed for time::Weekday {}
173 impl<C> InFixedCalendar<C> for time::Weekday {}
174 impl GetField<()> for time::Weekday {
175 fn get_field(&self) {}
176 }
177
178 impl GetField<Weekday> for time::Weekday {
179 fn get_field(&self) -> Weekday {
180 (*self).into()
181 }
182 }
183
184 impl UnstableSealed for time::OffsetDateTime {}
185 impl GetField<()> for time::OffsetDateTime {
186 fn get_field(&self) {}
187 }
188
189 impl GetField<Hour> for time::OffsetDateTime {
190 fn get_field(&self) -> Hour {
191 self.time().get_field()
192 }
193 }
194
195 impl GetField<Minute> for time::OffsetDateTime {
196 fn get_field(&self) -> Minute {
197 self.time().get_field()
198 }
199 }
200
201 impl GetField<Second> for time::OffsetDateTime {
202 fn get_field(&self) -> Second {
203 self.time().get_field()
204 }
205 }
206
207 impl GetField<Nanosecond> for time::OffsetDateTime {
208 fn get_field(&self) -> Nanosecond {
209 self.time().get_field()
210 }
211 }
212
213 impl GetField<YearInfo> for time::OffsetDateTime {
214 #[inline]
215 fn get_field(&self) -> YearInfo {
216 self.date().get_field()
217 }
218 }
219
220 impl GetField<MonthInfo> for time::OffsetDateTime {
221 #[inline]
222 fn get_field(&self) -> MonthInfo {
223 self.date().get_field()
224 }
225 }
226
227 impl GetField<DayOfMonth> for time::OffsetDateTime {
228 #[inline]
229 fn get_field(&self) -> DayOfMonth {
230 self.date().get_field()
231 }
232 }
233
234 impl GetField<Weekday> for time::OffsetDateTime {
235 #[inline]
236 fn get_field(&self) -> Weekday {
237 self.date().get_field()
238 }
239 }
240
241 impl GetField<DayOfYear> for time::OffsetDateTime {
242 #[inline]
243 fn get_field(&self) -> DayOfYear {
244 self.date().get_field()
245 }
246 }
247
248 impl GetField<RataDie> for time::OffsetDateTime {
249 #[inline]
250 fn get_field(&self) -> RataDie {
251 self.date().get_field()
252 }
253 }
254
255 impl GetField<Option<UtcOffset>> for time::OffsetDateTime {
256 #[inline]
257 fn get_field(&self) -> Option<UtcOffset> {
258 self.offset().get_field()
259 }
260 }
261
262 impl GetField<ZoneNameTimestamp> for time::OffsetDateTime {
263 #[inline]
264 fn get_field(&self) -> ZoneNameTimestamp {
265 ZoneNameTimestamp::from_epoch_seconds(self.unix_timestamp())
266 }
267 }
268
269 impl ConvertCalendar for time::Time {
270 type Converted<'a> = Self;
271 #[inline]
272 fn to_calendar<'a>(&self, _: &'a AnyCalendar) -> Self::Converted<'a> {
273 *self
274 }
275 }
276
277 impl ConvertCalendar for time::Weekday {
278 type Converted<'a> = Self;
279 #[inline]
280 fn to_calendar<'a>(&self, _: &'a AnyCalendar) -> Self::Converted<'a> {
281 *self
282 }
283 }
284
285 impl ConvertCalendar for time::Date {
286 type Converted<'a> = <Date<Gregorian> as ConvertCalendar>::Converted<'a>;
287
288 fn to_calendar<'a>(&self, calendar: &'a AnyCalendar) -> Self::Converted<'a> {
289 ConvertCalendar::to_calendar(&Date::from(*self), calendar)
290 }
291 }
292
293 impl ConvertCalendar for time::PrimitiveDateTime {
294 type Converted<'a> = <DateTime<Gregorian> as ConvertCalendar>::Converted<'a>;
295
296 fn to_calendar<'a>(&self, calendar: &'a AnyCalendar) -> Self::Converted<'a> {
297 ConvertCalendar::to_calendar(&DateTime::from(*self), calendar)
298 }
299 }
300
301 impl ConvertCalendar for time::OffsetDateTime {
302 type Converted<'a> = <ZonedDateTime<Gregorian, UtcOffset> as ConvertCalendar>::Converted<'a>;
303
304 fn to_calendar<'a>(&self, calendar: &'a AnyCalendar) -> Self::Converted<'a> {
305 ConvertCalendar::to_calendar(&ZonedDateTime::from(self), calendar)
306 }
307 }
308
309 impl ConvertCalendar for time::UtcOffset {
310 type Converted<'a> = Self;
311 #[inline]
312 fn to_calendar<'a>(&self, _: &'a AnyCalendar) -> Self::Converted<'a> {
313 *self
314 }
315 }
316
317 #[test]
318 fn time() {
319 use crate::{DateTimeFormatter, fieldsets};
320 use icu_locale_core::locale;
321 use writeable::assert_writeable_eq;
322
323 let time = time::OffsetDateTime::from_unix_timestamp_nanos(1726011440123456789)
324 .unwrap()
325 .to_offset(time::UtcOffset::from_hms(9, 0, 0).unwrap());
326
327 let ymdto = DateTimeFormatter::try_new(
328 locale!("ja-u-ca-japanese").into(),
329 fieldsets::YMDT::medium().with_zone(fieldsets::zone::LocalizedOffsetShort),
330 )
331 .unwrap();
332 assert_writeable_eq!(ymdto.format(&time), "令和6年9月11日 8:37:20 GMT+9");
333
334 let ymdt = DateTimeFormatter::try_new(
335 locale!("ja-u-ca-japanese").into(),
336 fieldsets::YMDT::medium(),
337 )
338 .unwrap();
339 assert_writeable_eq!(
340 ymdt.format(&time::PrimitiveDateTime::new(time.date(), time.time())),
341 "令和6年9月11日 8:37:20"
342 );
343
344 let ymd =
345 DateTimeFormatter::try_new(locale!("ja-u-ca-japanese").into(), fieldsets::YMD::medium())
346 .unwrap();
347 assert_writeable_eq!(ymd.format(&time.date()), "令和6年9月11日");
348
349 let t = DateTimeFormatter::try_new(locale!("ja-u-ca-japanese").into(), fieldsets::T::medium())
350 .unwrap();
351 assert_writeable_eq!(t.format(&time.time()), "8:37:20");
352
353 let e = DateTimeFormatter::try_new(locale!("ja-u-ca-japanese").into(), fieldsets::E::long())
354 .unwrap();
355 assert_writeable_eq!(e.format(&time.weekday()), "水曜日");
356
357 let offset = DateTimeFormatter::try_new(
358 locale!("ja-u-ca-japanese").into(),
359 fieldsets::zone::LocalizedOffsetLong,
360 )
361 .unwrap();
362 assert_writeable_eq!(offset.format(&time.offset()), "GMT+09:00");
363 }
364
365 #[test]
366 fn time_fixed_calendar() {
367 use crate::{DateTimeFormatter, FixedCalendarDateTimeFormatter, fieldsets};
368 use icu_locale_core::locale;
369 use writeable::assert_writeable_eq;
370
371 let time = time::OffsetDateTime::from_unix_timestamp_nanos(1726011440123456789)
372 .unwrap()
373 .to_offset(time::UtcOffset::from_hms(2, 0, 0).unwrap());
374
375 let ymdto = DateTimeFormatter::try_new(
376 locale!("fr").into(),
377 fieldsets::YMDT::medium().with_zone(fieldsets::zone::LocalizedOffsetShort),
378 )
379 .unwrap();
380 assert_writeable_eq!(ymdto.format(&time), "11 sept. 2024, 01:37:20 UTC+2");
381
382 let ymdt = DateTimeFormatter::try_new(locale!("fr").into(), fieldsets::YMDT::medium()).unwrap();
383 assert_writeable_eq!(
384 ymdt.format(&time::PrimitiveDateTime::new(time.date(), time.time())),
385 "11 sept. 2024, 01:37:20"
386 );
387
388 let ymd = DateTimeFormatter::try_new(locale!("fr").into(), fieldsets::YMD::medium()).unwrap();
389 assert_writeable_eq!(ymd.format(&time.date()), "11 sept. 2024");
390
391 let t = FixedCalendarDateTimeFormatter::<Gregorian, _>::try_new(
392 locale!("fr").into(),
393 fieldsets::T::medium(),
394 )
395 .unwrap();
396 assert_writeable_eq!(t.format(&time.time()), "01:37:20");
397
398 let e = FixedCalendarDateTimeFormatter::<Gregorian, _>::try_new(
399 locale!("fr").into(),
400 fieldsets::E::long(),
401 )
402 .unwrap();
403 assert_writeable_eq!(e.format(&time.weekday()), "mercredi");
404
405 let offset = FixedCalendarDateTimeFormatter::<Gregorian, _>::try_new(
406 locale!("fr").into(),
407 fieldsets::zone::LocalizedOffsetLong,
408 )
409 .unwrap();
410 assert_writeable_eq!(offset.format(&time.offset()), "UTC+02:00");
411 }
412
413 #[test]
414 fn time_no_calendar() {
415 use crate::{NoCalendarFormatter, fieldsets};
416 use icu_locale_core::locale;
417 use writeable::assert_writeable_eq;
418
419 let time = time::OffsetDateTime::from_unix_timestamp_nanos(1726011440123456789)
420 .unwrap()
421 .to_offset(time::UtcOffset::from_hms(2, 0, 0).unwrap());
422
423 let t = NoCalendarFormatter::try_new(locale!("de-CH").into(), fieldsets::T::medium()).unwrap();
424 assert_writeable_eq!(t.format(&time.time()), "01:37:20");
425
426 let offset =
427 NoCalendarFormatter::try_new(locale!("de").into(), fieldsets::zone::LocalizedOffsetLong)
428 .unwrap();
429 assert_writeable_eq!(offset.format(&time.offset()), "GMT+02:00");
430 }