Debian Codegraph @

third_party/rust/icu_calendar/examples/iso_date_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, Date, Iso};
13
14 const DATES_ISO: &[(i32, u8, u8)] = &[
15 (1970, 1, 1),
16 (1982, 3, 11),
17 (1999, 2, 21),
18 (2000, 12, 29),
19 (2001, 9, 8),
20 (2017, 7, 12),
21 (2020, 2, 29),
22 (2021, 3, 21),
23 (2021, 6, 10),
24 (2021, 9, 2),
25 (2022, 10, 8),
26 (2022, 2, 9),
27 (2033, 6, 10),
28 ];
29
30 fn print<A: Calendar>(_date_input: &Date<A>) {
31 #[cfg(debug_assertions)]
32 {
33 let formatted_date = format!(
34 "Year: {}, Month: {}, Day: {}",
35 _date_input.year().number,
36 _date_input.month().ordinal,
37 _date_input.day_of_month().0,
38 );
39
40 println!("{formatted_date}");
41 }
42 }
43
44 fn tuple_to_iso_date(date: (i32, u8, u8)) -> Result<Date<Iso>, CalendarError> {
45 Date::try_new_iso_date(date.0, date.1, date.2)
46 }
47
48 #[no_mangle]
49 fn main(_argc: isize, _argv: *const *const u8) -> isize {
50 icu_benchmark_macros::main_setup!();
51
52 let dates = DATES_ISO
53 .iter()
54 .copied()
55 .map(tuple_to_iso_date)
56 .collect::<Result<Vec<Date<Iso>>, _>>()
57 .expect("Failed to parse dates.");
58
59 dates.iter().map(print).for_each(drop);
60
61 0
62 }