pub struct Date<A: AsCalendar> { /* private fields */ }
Expand description

A date for a given calendar.

This can work with wrappers around Calendar types, e.g. Rc<C>, via the AsCalendar trait.

use icu::calendar::Date;

// Example: creation of ISO date from integers.
let date_iso = Date::new_iso_date(1970, 1, 2)
    .expect("Failed to initialize ISO Date instance.");

assert_eq!(date_iso.year().number, 1970);
assert_eq!(date_iso.month().ordinal, 1);
assert_eq!(date_iso.day_of_month().0, 2);

Implementations

Construct a new Buddhist Date.

Years are specified as BE years.

use icu::calendar::Date;
use std::convert::TryFrom;

let date_buddhist =
    Date::new_buddhist_date(1970, 1, 2).expect("Failed to initialize Buddhist Date instance.");

assert_eq!(date_buddhist.year().number, 1970);
assert_eq!(date_buddhist.month().ordinal, 1);
assert_eq!(date_buddhist.day_of_month().0, 2);

Construct new Coptic Date.

Negative years are in the B.D. era, starting with 0 = 1 B.D.

use icu::calendar::Date;

let date_coptic =
    Date::new_coptic_date(1686, 5, 6).expect("Failed to initialize Coptic Date instance.");

assert_eq!(date_coptic.year().number, 1686);
assert_eq!(date_coptic.month().ordinal, 5);
assert_eq!(date_coptic.day_of_month().0, 6);

Construct a date from from era/month codes and fields, and some calendar representation

Construct a date from an ISO date and some calendar representation

Convert the Date to an ISO Date

Convert the Date to a date in a different calendar

The number of months in the year of this date

The number of days in the year of this date

The number of days in the month of this date

The day of the week for this date

Monday is 1, Sunday is 7, according to ISO

The calendar-specific year represented by self

The calendar-specific month represented by self

The calendar-specific day-of-month represented by self

The calendar-specific day-of-month represented by self

Construct a date from raw values for a given calendar. This does not check any invariants for the date and calendar, and should only be called by calendar implementations.

Calling this outside of calendar implementations is sound, but calendar implementations are not expected to do anything sensible with such invalid dates.

AnyCalendar will panic if AnyCalendar Date objects with mismatching date and calendar types are constructed

Get the inner date implementation. Should not be called outside of calendar implementations

Get a reference to the contained calendar

Type-erase the date, converting it to a date for AnyCalendar

Wrap the calendar type in Rc<T>

Useful when paired with Self::to_any() to obtain a Date<Rc<AnyCalendar>>

Construct new Ethiopic Date.

For the Amete Mihret era style, negative years work with year 0 as 1 pre-Incarnation, year -1 as 2 pre-Incarnation, and so on.

use icu::calendar::Date;
use icu::calendar::ethiopic::EthiopicEraStyle;

let date_ethiopic =
    Date::new_ethiopic_date(EthiopicEraStyle::AmeteMihret, 2014, 8, 25)
    .expect("Failed to initialize Ethopic Date instance.");

assert_eq!(date_ethiopic.year().number, 2014);
assert_eq!(date_ethiopic.month().ordinal, 8);
assert_eq!(date_ethiopic.day_of_month().0, 25);

Construct a new Gregorian Date.

Years are specified as ISO years.

use icu::calendar::{Date};
use std::convert::TryFrom;

// Conversion from ISO to Gregorian
let date_gregorian = Date::new_gregorian_date(1970, 1, 2)
    .expect("Failed to initialize Gregorian Date instance.");

assert_eq!(date_gregorian.year().number, 1970);
assert_eq!(date_gregorian.month().ordinal, 1);
assert_eq!(date_gregorian.day_of_month().0, 2);

Construct new Indian Date, with year provided in the Śaka era.

use icu::calendar::Date;

let date_indian =
    Date::new_indian_date(1891, 10, 12).expect("Failed to initialize Indian Date instance.");

assert_eq!(date_indian.year().number, 1891);
assert_eq!(date_indian.month().ordinal, 10);
assert_eq!(date_indian.day_of_month().0, 12);

Construct a new ISO date from integers.

use icu::calendar::Date;

let date_iso = Date::new_iso_date(1970, 1, 2)
    .expect("Failed to initialize ISO Date instance.");

assert_eq!(date_iso.year().number, 1970);
assert_eq!(date_iso.month().ordinal, 1);
assert_eq!(date_iso.day_of_month().0, 2);

Construct a new Japanese Date.

Years are specified in the era provided, and must be in range for Japanese eras (e.g. dates past April 30 Heisei 31 must be in Reiwa; “Jun 5 Heisei 31” and “Jan 1 Heisei 32” will not be adjusted to being in Reiwa 1 and 2 respectively)

However, dates may always be specified in “bce” or “ce” and they will be adjusted as necessary.

use icu::calendar::{types, Date, Ref};
use icu::calendar::japanese::Japanese;
use std::convert::TryFrom;
use tinystr::tinystr;

let provider = icu_testdata::get_provider();
let japanese_calendar = Japanese::try_new_with_buffer_provider(&provider).expect("Cannot load japanese data");
// for easy sharing
let japanese_calendar = Ref(&japanese_calendar);

let era = types::Era(tinystr!(16, "heisei"));

let date = Date::new_japanese_date(era, 14, 1, 2, japanese_calendar)
    .expect("Constructing a date should succeed");

assert_eq!(date.year().era, era);
assert_eq!(date.year().number, 14);
assert_eq!(date.month().ordinal, 1);
assert_eq!(date.day_of_month().0, 2);

// This function will error for eras that are out of bounds:
// (Heisei was 32 years long, Heisei 33 is in Reiwa)
let oob_date = Date::new_japanese_date(era, 33, 1, 2, japanese_calendar);
assert!(oob_date.is_err());

// and for unknown eras
let fake_era = types::Era(tinystr!(16, "neko")); // 🐱
let fake_date = Date::new_japanese_date(fake_era, 10, 1, 2, japanese_calendar);
assert!(fake_date.is_err());

Construct a new Japanese Date with all eras.

Years are specified in the era provided, and must be in range for Japanese eras (e.g. dates past April 30 Heisei 31 must be in Reiwa; “Jun 5 Heisei 31” and “Jan 1 Heisei 32” will not be adjusted to being in Reiwa 1 and 2 respectively)

However, dates may always be specified in “bce” or “ce” and they will be adjusted as necessary.

use icu::calendar::{types, Date, Ref};
use icu::calendar::japanese::{JapaneseExtended};
use std::convert::TryFrom;
use tinystr::tinystr;

let provider = icu_testdata::get_provider();
let japanext_calendar = JapaneseExtended::try_new_with_buffer_provider(&provider).expect("Cannot load japanese data");
// for easy sharing
let japanext_calendar = Ref(&japanext_calendar);

let era = types::Era(tinystr!(16, "kansei-1789"));

let date = Date::new_japanese_extended_date(era, 7, 1, 2, japanext_calendar)
    .expect("Constructing a date should succeed");

assert_eq!(date.year().era, era);
assert_eq!(date.year().number, 7);
assert_eq!(date.month().ordinal, 1);
assert_eq!(date.day_of_month().0, 2);

Construct new Julian Date.

Zero and negative years are in BC, with year 0 = 1 BC

use icu::calendar::Date;

let date_julian =
    Date::new_julian_date(1969, 12, 20).expect("Failed to initialize Julian Date instance.");

assert_eq!(date_julian.year().number, 1969);
assert_eq!(date_julian.month().ordinal, 12);
assert_eq!(date_julian.day_of_month().0, 20);

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.