#[non_exhaustive]pub struct DateFields<'a> {
pub era: Option<&'a [u8]>,
pub era_year: Option<i32>,
pub extended_year: Option<i32>,
pub month_code: Option<&'a [u8]>,
pub ordinal_month: Option<u8>,
pub day: Option<u8>,
}Expand description
A bag of various ways of expressing the year, month, and/or day.
Pass this into Date::try_from_fields.
Graduation tracking issue: issue #7161.
โจ Enabled with the unstable Cargo feature.
Fields (Non-exhaustive)ยง
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.era: Option<&'a [u8]>The era code as a UTF-8 string.
The acceptable codes are defined by CLDR and documented on each calendar.
If set, Self::era_year must also be set.
ยงExamples
To set the era field, use a byte string:
use icu::calendar::types::DateFields;
let mut fields = DateFields::default();
// As a byte string literal:
fields.era = Some(b"reiwa");
// Using str::as_bytes:
fields.era = Some("reiwa".as_bytes());For a full example, see Self::extended_year.
era_year: Option<i32>The numeric year in Self::era.
If set, Self::era must also be set.
For an example, see Self::extended_year.
extended_year: Option<i32>If both this and Self::era/Self::era_year are set, they must
refer to the same year.
ยงExamples
Either extended_year or era + era_year can be used in DateFields:
use icu::calendar::cal::Japanese;
use icu::calendar::types::DateFields;
use icu::calendar::Date;
let mut fields1 = DateFields::default();
fields1.era = Some(b"reiwa");
fields1.era_year = Some(7);
fields1.ordinal_month = Some(1);
fields1.day = Some(1);
let date1 =
Date::try_from_fields(fields1, Default::default(), Japanese::new())
.expect("a well-defined Japanese date from era year");
let mut fields2 = DateFields::default();
fields2.extended_year = Some(2025);
fields2.ordinal_month = Some(1);
fields2.day = Some(1);
let date2 =
Date::try_from_fields(fields2, Default::default(), Japanese::new())
.expect("a well-defined Japanese date from extended year");
assert_eq!(date1, date2);
let year_info = date1.year().era().unwrap();
assert_eq!(year_info.year, 7);
assert_eq!(year_info.era.as_str(), "reiwa");
assert_eq!(year_info.extended_year, 2025);month_code: Option<&'a [u8]>The month code representing a valid month in this calendar year, as a UTF-8 string.
See MonthCode for information on the syntax.
ยงExamples
To set the month code field, use a byte string:
use icu::calendar::types::DateFields;
let mut fields = DateFields::default();
// As a byte string literal:
fields.era = Some(b"M02L");
// Using str::as_bytes:
fields.era = Some("M02L".as_bytes());For a full example, see Self::ordinal_month.
ordinal_month: Option<u8>See MonthInfo::ordinal.
If both this and Self::month_code are set, they must refer to
the same month.
Note: using Self::month_code is recommended, because the ordinal month numbers
can vary from year to year, as illustrated in the following example.
ยงExamples
Either month_code or ordinal_month can be used in DateFields, but they
might not resolve to the same month number:
use icu::calendar::cal::ChineseTraditional;
use icu::calendar::types::DateFields;
use icu::calendar::Date;
// The 2023 Year of the Rabbit had a leap month after the 2nd month.
let mut fields1 = DateFields::default();
fields1.extended_year = Some(2023);
fields1.month_code = Some(b"M02L");
fields1.day = Some(1);
let date1 = Date::try_from_fields(
fields1,
Default::default(),
ChineseTraditional::new(),
)
.expect("a well-defined Chinese date from month code");
let mut fields2 = DateFields::default();
fields2.extended_year = Some(2023);
fields2.ordinal_month = Some(3);
fields2.day = Some(1);
let date2 = Date::try_from_fields(
fields2,
Default::default(),
ChineseTraditional::new(),
)
.expect("a well-defined Chinese date from ordinal month");
assert_eq!(date1, date2);
let month_info = date1.month();
assert_eq!(month_info.ordinal, 3);
assert_eq!(month_info.standard_code.0, "M02L");day: Option<u8>See DayOfMonth.
Trait Implementationsยง
Sourceยงimpl<'a> Clone for DateFields<'a>
impl<'a> Clone for DateFields<'a>
Sourceยงfn clone(&self) -> DateFields<'a>
fn clone(&self) -> DateFields<'a>
1.0.0 (const: unstable) ยท Sourceยงfn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSourceยงimpl Debug for DateFields<'_>
impl Debug for DateFields<'_>
Sourceยงimpl<'a> Default for DateFields<'a>
impl<'a> Default for DateFields<'a>
Sourceยงfn default() -> DateFields<'a>
fn default() -> DateFields<'a>
Sourceยงimpl<'a> PartialEq for DateFields<'a>
impl<'a> PartialEq for DateFields<'a>
Sourceยงfn eq(&self, other: &DateFields<'a>) -> bool
fn eq(&self, other: &DateFields<'a>) -> bool
self and other values to be equal, and is used by ==.