icu_datetime/parts.rs
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//! Parts of a formatted date/time.
6//!
7//! # Examples
8//!
9//! ```
10//! use icu::calendar::Gregorian;
11//! use icu::calendar::Date;
12//! use icu::datetime::parts as datetime_parts;
13//! use icu::datetime::fieldsets;
14//! use icu::datetime::options::SubsecondDigits;
15//! use icu::datetime::options::TimePrecision;
16//! use icu::datetime::DateTimeFormatter;
17//! use icu::decimal::parts as decimal_parts;
18//! use icu::locale::locale;
19//! use icu::datetime::input::{ZonedDateTime, Time};
20//! use icu::time::zone::{IanaParser, VariantOffsetsCalculator};
21//! use writeable::assert_writeable_parts_eq;
22//!
23//! let dtf = DateTimeFormatter::try_new(
24//! locale!("en-u-ca-buddhist").into(),
25//! fieldsets::YMDT::medium().with_time_precision(TimePrecision::Subsecond(SubsecondDigits::S2)).with_zone(fieldsets::zone::SpecificShort),
26//! )
27//! .unwrap();
28//!
29//! let dtz = ZonedDateTime::try_full_from_str("2023-11-20T11:35:03.5+00:00[Europe/London]", dtf.calendar(), IanaParser::new(), VariantOffsetsCalculator::new()).unwrap();
30//!
31//! // Missing data is filled in on a best-effort basis, and an error is signaled.
32//! assert_writeable_parts_eq!(
33//! dtf.format(&dtz),
34//! "Nov 20, 2566 BE, 11:35:03.50 AM GMT",
35//! [
36//! (0, 3, datetime_parts::MONTH),
37//! (4, 6, decimal_parts::INTEGER),
38//! (4, 6, datetime_parts::DAY),
39//! (8, 12, decimal_parts::INTEGER),
40//! (8, 12, datetime_parts::YEAR),
41//! (13, 15, datetime_parts::ERA),
42//! (17, 19, decimal_parts::INTEGER),
43//! (17, 19, datetime_parts::HOUR),
44//! (20, 22, decimal_parts::INTEGER),
45//! (20, 22, datetime_parts::MINUTE),
46//! (23, 28, datetime_parts::SECOND),
47//! (23, 25, decimal_parts::INTEGER),
48//! (25, 26, decimal_parts::DECIMAL),
49//! (26, 28, decimal_parts::FRACTION),
50//! // note: from 28 to 31 is a NNBSP
51//! (31, 33, datetime_parts::DAY_PERIOD),
52//! (34, 37, datetime_parts::TIME_ZONE_NAME),
53//! ]
54//! );
55//! ```
56
57use writeable::Part;
58
59/// A [`Part`] used by [`FormattedDateTime`](super::FormattedDateTime).
60pub const ERA: Part = Part {
61 category: "datetime",
62 value: "era",
63};
64
65/// A [`Part`] used by [`FormattedDateTime`](super::FormattedDateTime).
66pub const YEAR: Part = Part {
67 category: "datetime",
68 value: "year",
69};
70
71/// A [`Part`] used by [`FormattedDateTime`](super::FormattedDateTime).
72pub const RELATED_YEAR: Part = Part {
73 category: "datetime",
74 value: "relatedYear",
75};
76
77/// A [`Part`] used by [`FormattedDateTime`](super::FormattedDateTime).
78pub const YEAR_NAME: Part = Part {
79 category: "datetime",
80 value: "yearName",
81};
82
83/// A [`Part`] used by [`FormattedDateTime`](super::FormattedDateTime).
84pub const MONTH: Part = Part {
85 category: "datetime",
86 value: "month",
87};
88
89/// A [`Part`] used by [`FormattedDateTime`](super::FormattedDateTime).
90pub const DAY: Part = Part {
91 category: "datetime",
92 value: "day",
93};
94
95/// A [`Part`] used by [`FormattedDateTime`](super::FormattedDateTime).
96pub const WEEKDAY: Part = Part {
97 category: "datetime",
98 value: "weekday",
99};
100
101/// A [`Part`] used by [`FormattedDateTime`](super::FormattedDateTime).
102pub const DAY_PERIOD: Part = Part {
103 category: "datetime",
104 value: "dayPeriod",
105};
106
107/// A [`Part`] used by [`FormattedDateTime`](super::FormattedDateTime).
108pub const HOUR: Part = Part {
109 category: "datetime",
110 value: "hour",
111};
112
113/// A [`Part`] used by [`FormattedDateTime`](super::FormattedDateTime).
114pub const MINUTE: Part = Part {
115 category: "datetime",
116 value: "minute",
117};
118
119/// A [`Part`] used by [`FormattedDateTime`](super::FormattedDateTime).
120pub const SECOND: Part = Part {
121 category: "datetime",
122 value: "second",
123};
124
125/// A [`Part`] used by [`FormattedDateTime`](super::FormattedDateTime).
126pub const TIME_ZONE_NAME: Part = Part {
127 category: "datetime",
128 value: "timeZoneName",
129};