Skip to main content

DateFields

Struct DateFields 

Source
#[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.

๐Ÿšง This code is considered unstable; it may change at any time, in breaking or non-breaking ways, including in SemVer minor releases. Do not use this type unless you are prepared for things to occasionally break.

Graduation tracking issue: issue #7161.

โœจ Enabled with the unstable Cargo feature.

Fields (Non-exhaustive)ยง

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional 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>

See Date::extended_year().

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>

Trait Implementationsยง

Sourceยง

impl<'a> Clone for DateFields<'a>

Sourceยง

fn clone(&self) -> DateFields<'a>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) ยท Sourceยง

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Sourceยง

impl Debug for DateFields<'_>

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Sourceยง

impl<'a> Default for DateFields<'a>

Sourceยง

fn default() -> DateFields<'a>

Returns the โ€œdefault valueโ€ for a type. Read more
Sourceยง

impl<'a> PartialEq for DateFields<'a>

Sourceยง

fn eq(&self, other: &DateFields<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) ยท Sourceยง

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Sourceยง

impl<'a> Copy for DateFields<'a>

Sourceยง

impl<'a> StructuralPartialEq for DateFields<'a>

Auto Trait Implementationsยง

ยง

impl<'a> Freeze for DateFields<'a>

ยง

impl<'a> RefUnwindSafe for DateFields<'a>

ยง

impl<'a> Send for DateFields<'a>

ยง

impl<'a> Sync for DateFields<'a>

ยง

impl<'a> Unpin for DateFields<'a>

ยง

impl<'a> UnsafeUnpin for DateFields<'a>

ยง

impl<'a> UnwindSafe for DateFields<'a>

Blanket Implementationsยง

Sourceยง

impl<T> Any for T
where T: 'static + ?Sized,

Sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Sourceยง

impl<T> Borrow<T> for T
where T: ?Sized,

Sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Sourceยง

impl<T> BorrowMut<T> for T
where T: ?Sized,

Sourceยง

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Sourceยง

impl<T> CloneToUninit for T
where T: Clone,

Sourceยง

unsafe fn clone_to_uninit(&self, dest: *mut u8)

๐Ÿ”ฌThis is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Sourceยง

impl<T> From<T> for T

Sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

Sourceยง

impl<T, U> Into<U> for T
where U: From<T>,

Sourceยง

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Sourceยง

impl<T> ToOwned for T
where T: Clone,

Sourceยง

type Owned = T

The resulting type after obtaining ownership.
Sourceยง

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Sourceยง

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Sourceยง

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Sourceยง

type Error = Infallible

The type returned in the event of a conversion error.
Sourceยง

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Sourceยง

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Sourceยง

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Sourceยง

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Sourceยง

impl<T> ErasedDestructor for T
where T: 'static,