pub struct Date<A> where
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.
This can be constructed constructed
from its fields via Self::try_new_from_codes()
, or can be constructed with one of the
new_<calendar>_datetime()
per-calendar methods (and then freely converted between calendars).
use icu::calendar::Date;
// Example: creation of ISO date from integers.
let date_iso = Date::try_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
sourceimpl<A> Date<A> where
A: AsCalendar,
impl<A> Date<A> where
A: AsCalendar,
sourcepub fn try_new_from_codes(
era: Era,
year: i32,
month_code: MonthCode,
day: u8,
calendar: A
) -> Result<Date<A>, CalendarError>
pub fn try_new_from_codes(
era: Era,
year: i32,
month_code: MonthCode,
day: u8,
calendar: A
) -> Result<Date<A>, CalendarError>
Construct a date from from era/month codes and fields, and some calendar representation
sourcepub fn new_from_iso(iso: Date<Iso>, calendar: A) -> Date<A>
pub fn new_from_iso(iso: Date<Iso>, calendar: A) -> Date<A>
Construct a date from an ISO date and some calendar representation
sourcepub fn to_calendar<A2>(&self, calendar: A2) -> Date<A2> where
A2: AsCalendar,
pub fn to_calendar<A2>(&self, calendar: A2) -> Date<A2> where
A2: AsCalendar,
Convert the Date to a date in a different calendar
sourcepub fn months_in_year(&self) -> u8
pub fn months_in_year(&self) -> u8
The number of months in the year of this date
sourcepub fn days_in_year(&self) -> u32
pub fn days_in_year(&self) -> u32
The number of days in the year of this date
sourcepub fn days_in_month(&self) -> u8
pub fn days_in_month(&self) -> u8
The number of days in the month of this date
sourcepub fn day_of_week(&self) -> IsoWeekday
pub fn day_of_week(&self) -> IsoWeekday
The day of the week for this date
Monday is 1, Sunday is 7, according to ISO
sourcepub fn year(&self) -> FormattableYear
pub fn year(&self) -> FormattableYear
The calendar-specific year represented by self
sourcepub fn month(&self) -> FormattableMonth
pub fn month(&self) -> FormattableMonth
The calendar-specific month represented by self
sourcepub fn day_of_month(&self) -> DayOfMonth
pub fn day_of_month(&self) -> DayOfMonth
The calendar-specific day-of-month represented by self
sourcepub fn day_of_year_info(&self) -> DayOfYearInfo
pub fn day_of_year_info(&self) -> DayOfYearInfo
The calendar-specific day-of-month represented by self
sourcepub fn week_of_month(&self, first_weekday: IsoWeekday) -> WeekOfMonth
pub fn week_of_month(&self, first_weekday: IsoWeekday) -> WeekOfMonth
The week of the month containing this date.
Examples
use icu::calendar::types::IsoWeekday;
use icu::calendar::types::WeekOfMonth;
use icu::calendar::Date;
let date = Date::try_new_iso_date(2022, 8, 10).unwrap(); // second Wednesday
// The following info is usually locale-specific
let first_weekday = IsoWeekday::Sunday;
assert_eq!(date.week_of_month(first_weekday), WeekOfMonth(2));
sourcepub fn week_of_year(
&self,
config: &WeekCalculator
) -> Result<WeekOf, CalendarError>
pub fn week_of_year(
&self,
config: &WeekCalculator
) -> Result<WeekOf, CalendarError>
The week of the year containing this date.
Examples
use icu::calendar::types::IsoWeekday;
use icu::calendar::week::RelativeUnit;
use icu::calendar::week::WeekCalculator;
use icu::calendar::week::WeekOf;
use icu::calendar::Date;
let date = Date::try_new_iso_date(2022, 8, 26).unwrap();
// The following info is usually locale-specific
let week_calculator = WeekCalculator::default();
assert_eq!(
date.week_of_year(&week_calculator),
Ok(WeekOf {
week: 35,
unit: RelativeUnit::Current
})
);
sourcepub fn from_raw(
inner: <<A as AsCalendar>::Calendar as Calendar>::DateInner,
calendar: A
) -> Date<A>
pub fn from_raw(
inner: <<A as AsCalendar>::Calendar as Calendar>::DateInner,
calendar: A
) -> Date<A>
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
sourcepub fn inner(&self) -> &<<A as AsCalendar>::Calendar as Calendar>::DateInner
pub fn inner(&self) -> &<<A as AsCalendar>::Calendar as Calendar>::DateInner
Get the inner date implementation. Should not be called outside of calendar implementations
sourcepub fn calendar(&self) -> &<A as AsCalendar>::Calendar
pub fn calendar(&self) -> &<A as AsCalendar>::Calendar
Get a reference to the contained calendar
sourcepub fn calendar_wrapper(&self) -> &A
pub fn calendar_wrapper(&self) -> &A
Get a reference to the contained calendar wrapper
(Useful in case the user wishes to e.g. clone an Rc)
sourceimpl<C, A> Date<A> where
C: IntoAnyCalendar,
A: AsCalendar<Calendar = C>,
impl<C, A> Date<A> where
C: IntoAnyCalendar,
A: AsCalendar<Calendar = C>,
sourcepub fn to_any(&self) -> Date<AnyCalendar>
pub fn to_any(&self) -> Date<AnyCalendar>
Type-erase the date, converting it to a date for AnyCalendar
sourceimpl<C> Date<C> where
C: Calendar,
impl<C> Date<C> where
C: Calendar,
sourcepub fn wrap_calendar_in_rc(self) -> Date<Rc<C>>
pub fn wrap_calendar_in_rc(self) -> Date<Rc<C>>
Wrap the calendar type in Rc<T>
Useful when paired with Self::to_any()
to obtain a Date<Rc<AnyCalendar>>
sourcepub fn wrap_calendar_in_arc(self) -> Date<Arc<C>>
pub fn wrap_calendar_in_arc(self) -> Date<Arc<C>>
Wrap the calendar type in Arc<T>
Useful when paired with Self::to_any()
to obtain a Date<Rc<AnyCalendar>>
sourceimpl Date<Buddhist>
impl Date<Buddhist>
sourcepub fn try_new_buddhist_date(
year: i32,
month: u8,
day: u8
) -> Result<Date<Buddhist>, CalendarError>
pub fn try_new_buddhist_date(
year: i32,
month: u8,
day: u8
) -> Result<Date<Buddhist>, CalendarError>
Construct a new Buddhist Date.
Years are specified as BE years.
use icu::calendar::Date;
use std::convert::TryFrom;
let date_buddhist = Date::try_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);
sourceimpl Date<Coptic>
impl Date<Coptic>
sourcepub fn try_new_coptic_date(
year: i32,
month: u8,
day: u8
) -> Result<Date<Coptic>, CalendarError>
pub fn try_new_coptic_date(
year: i32,
month: u8,
day: u8
) -> Result<Date<Coptic>, CalendarError>
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::try_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);
sourceimpl Date<Ethiopian>
impl Date<Ethiopian>
sourcepub fn try_new_ethiopian_date(
era_style: EthiopianEraStyle,
year: i32,
month: u8,
day: u8
) -> Result<Date<Ethiopian>, CalendarError>
pub fn try_new_ethiopian_date(
era_style: EthiopianEraStyle,
year: i32,
month: u8,
day: u8
) -> Result<Date<Ethiopian>, CalendarError>
Construct new Ethiopian 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::ethiopian::EthiopianEraStyle;
use icu::calendar::Date;
let date_ethiopian = Date::try_new_ethiopian_date(
EthiopianEraStyle::AmeteMihret,
2014,
8,
25,
)
.expect("Failed to initialize Ethopic Date instance.");
assert_eq!(date_ethiopian.year().number, 2014);
assert_eq!(date_ethiopian.month().ordinal, 8);
assert_eq!(date_ethiopian.day_of_month().0, 25);
sourceimpl Date<Gregorian>
impl Date<Gregorian>
sourcepub fn try_new_gregorian_date(
year: i32,
month: u8,
day: u8
) -> Result<Date<Gregorian>, CalendarError>
pub fn try_new_gregorian_date(
year: i32,
month: u8,
day: u8
) -> Result<Date<Gregorian>, CalendarError>
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::try_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);
sourceimpl Date<Indian>
impl Date<Indian>
sourcepub fn try_new_indian_date(
year: i32,
month: u8,
day: u8
) -> Result<Date<Indian>, CalendarError>
pub fn try_new_indian_date(
year: i32,
month: u8,
day: u8
) -> Result<Date<Indian>, CalendarError>
Construct new Indian Date, with year provided in the Śaka era.
use icu::calendar::Date;
let date_indian = Date::try_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);
sourceimpl Date<Iso>
impl Date<Iso>
sourcepub fn try_new_iso_date(
year: i32,
month: u8,
day: u8
) -> Result<Date<Iso>, CalendarError>
pub fn try_new_iso_date(
year: i32,
month: u8,
day: u8
) -> Result<Date<Iso>, CalendarError>
Construct a new ISO date from integers.
use icu::calendar::Date;
let date_iso = Date::try_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);
sourceimpl Date<Japanese>
impl Date<Japanese>
sourcepub fn try_new_japanese_date<A>(
era: Era,
year: i32,
month: u8,
day: u8,
japanese_calendar: A
) -> Result<Date<A>, CalendarError> where
A: AsCalendar<Calendar = Japanese>,
pub fn try_new_japanese_date<A>(
era: Era,
year: i32,
month: u8,
day: u8,
japanese_calendar: A
) -> Result<Date<A>, CalendarError> where
A: AsCalendar<Calendar = Japanese>,
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::japanese::Japanese;
use icu::calendar::{types, Date, Ref};
use std::convert::TryFrom;
use tinystr::tinystr;
let japanese_calendar =
Japanese::try_new_unstable(&icu_testdata::unstable())
.expect("Cannot load japanese data");
// for easy sharing
let japanese_calendar = Ref(&japanese_calendar);
let era = types::Era(tinystr!(16, "heisei"));
let date = Date::try_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::try_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::try_new_japanese_date(fake_era, 10, 1, 2, japanese_calendar);
assert!(fake_date.is_err());
sourceimpl Date<JapaneseExtended>
impl Date<JapaneseExtended>
sourcepub fn try_new_japanese_extended_date<A>(
era: Era,
year: i32,
month: u8,
day: u8,
japanext_calendar: A
) -> Result<Date<A>, CalendarError> where
A: AsCalendar<Calendar = JapaneseExtended>,
pub fn try_new_japanese_extended_date<A>(
era: Era,
year: i32,
month: u8,
day: u8,
japanext_calendar: A
) -> Result<Date<A>, CalendarError> where
A: AsCalendar<Calendar = JapaneseExtended>,
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::japanese::JapaneseExtended;
use icu::calendar::{types, Date, Ref};
use std::convert::TryFrom;
use tinystr::tinystr;
let japanext_calendar =
JapaneseExtended::try_new_unstable(&icu_testdata::unstable())
.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::try_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);
sourceimpl Date<Julian>
impl Date<Julian>
sourcepub fn try_new_julian_date(
year: i32,
month: u8,
day: u8
) -> Result<Date<Julian>, CalendarError>
pub fn try_new_julian_date(
year: i32,
month: u8,
day: u8
) -> Result<Date<Julian>, CalendarError>
Construct new Julian Date.
Zero and negative years are in BC, with year 0 = 1 BC
use icu::calendar::Date;
let date_julian = Date::try_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
sourceimpl<A> Clone for Date<A> where
A: AsCalendar + Clone,
impl<A> Clone for Date<A> where
A: AsCalendar + Clone,
sourceimpl<C, A> DateInput for Date<A> where
C: Calendar,
A: AsCalendar<Calendar = C>,
impl<C, A> DateInput for Date<A> where
C: Calendar,
A: AsCalendar<Calendar = C>,
sourcefn year(&self) -> Option<FormattableYear>
fn year(&self) -> Option<FormattableYear>
Gets the era and year input.
sourcefn month(&self) -> Option<FormattableMonth>
fn month(&self) -> Option<FormattableMonth>
Gets the month input.
sourcefn day_of_month(&self) -> Option<DayOfMonth>
fn day_of_month(&self) -> Option<DayOfMonth>
Gets the day input.
sourcefn iso_weekday(&self) -> Option<IsoWeekday>
fn iso_weekday(&self) -> Option<IsoWeekday>
Gets the weekday input.
sourcefn day_of_year_info(&self) -> Option<DayOfYearInfo>
fn day_of_year_info(&self) -> Option<DayOfYearInfo>
Gets information on the position of the day within the year.
type Calendar = C
type Calendar = C
The calendar this date relates to
sourcefn any_calendar_kind(&self) -> Option<AnyCalendarKind>
fn any_calendar_kind(&self) -> Option<AnyCalendarKind>
Gets the kind of calendar this date is for, if associated with AnyCalendar In most cases you’ll probably want to return AnyCalendarKind::Iso Read more
sourceimpl<A> Debug for Date<A> where
A: AsCalendar,
impl<A> Debug for Date<A> where
A: AsCalendar,
sourceimpl<C, A, B> PartialEq<Date<B>> for Date<A> where
C: Calendar,
A: AsCalendar<Calendar = C>,
B: AsCalendar<Calendar = C>,
impl<C, A, B> PartialEq<Date<B>> for Date<A> where
C: Calendar,
A: AsCalendar<Calendar = C>,
B: AsCalendar<Calendar = C>,
impl<A> Copy for Date<A> where
A: AsCalendar + Copy,
<<A as AsCalendar>::Calendar as Calendar>::DateInner: Copy,
impl<A> Eq for Date<A> where
A: AsCalendar,
Auto Trait Implementations
impl<A> RefUnwindSafe for Date<A> where
A: RefUnwindSafe,
<<A as AsCalendar>::Calendar as Calendar>::DateInner: RefUnwindSafe,
impl<A> Send for Date<A> where
A: Send,
<<A as AsCalendar>::Calendar as Calendar>::DateInner: Send,
impl<A> Sync for Date<A> where
A: Sync,
<<A as AsCalendar>::Calendar as Calendar>::DateInner: Sync,
impl<A> Unpin for Date<A> where
A: Unpin,
<<A as AsCalendar>::Calendar as Calendar>::DateInner: Unpin,
impl<A> UnwindSafe for Date<A> where
A: UnwindSafe,
<<A as AsCalendar>::Calendar as Calendar>::DateInner: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more