Debian Codegraph @

third_party/rust/icu_calendar/examples/iso_datetime_manipulations.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 // An example application which uses icu_datetime to format entries
6 // from a log into human readable dates and times.
7
8 #![no_main] // https://github.com/unicode-org/icu4x/issues/395
9
10 icu_benchmark_macros::static_setup!();
11
12 use icu_calendar::{Calendar, CalendarError, DateTime, Iso};
13
14 const DATETIMES_ISO: &[(i32, u8, u8, u8, u8, u8)] = &[
15 (1970, 1, 1, 3, 5, 12),
16 (1982, 3, 11, 2, 25, 59),
17 (1999, 2, 21, 13, 12, 23),
18 (2000, 12, 29, 10, 50, 23),
19 (2001, 9, 8, 11, 5, 5),
20 (2017, 7, 12, 3, 1, 1),
21 (2020, 2, 29, 23, 12, 23),
22 (2021, 3, 21, 18, 35, 34),
23 (2021, 6, 10, 13, 12, 23),
24 (2021, 9, 2, 5, 50, 22),
25 (2022, 10, 8, 9, 45, 32),
26 (2022, 2, 9, 10, 32, 45),
27 (2033, 6, 10, 17, 22, 22),
28 ];
29
30 fn print<A: Calendar>(_datetime_input: &DateTime<A>) {
31 #[cfg(debug_assertions)]
32 {
33 let formatted_datetime = format!(
34 "Year: {}, Month: {}, Day: {}, Hour: {}, Minute: {}, Second: {}",
35 _datetime_input.date.year().number,
36 _datetime_input.date.month().ordinal,
37 _datetime_input.date.day_of_month().0,
38 u8::from(_datetime_input.time.hour),
39 u8::from(_datetime_input.time.minute),
40 u8::from(_datetime_input.time.second),
41 );
42
43 println!("{formatted_datetime}");
44 }
45 }
46
47 fn tuple_to_iso_datetime(date: (i32, u8, u8, u8, u8, u8)) -> Result<DateTime<Iso>, CalendarError> {
48 DateTime::try_new_iso_datetime(date.0, date.1, date.2, date.3, date.4, date.5)
49 }
50
51 #[no_mangle]
52 fn main(_argc: isize, _argv: *const *const u8) -> isize {
53 icu_benchmark_macros::main_setup!();
54
55 let datetimes = DATETIMES_ISO
56 .iter()
57 .copied()
58 .map(tuple_to_iso_datetime)
59 .collect::<Result<Vec<DateTime<Iso>>, _>>()
60 .expect("Failed to parse datetimes.");
61
62 datetimes.iter().map(print).for_each(drop);
63
64 0
65 }