Debian Codegraph @

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