usecrate::any_calendar::{AnyCalendar, IntoAnyCalendar}; usecrate::week::{WeekCalculator, WeekOf}; usecrate::{types, Calendar, CalendarError, DateDuration, DateDurationUnit, Iso}; use alloc::rc::Rc; use alloc::sync::Arc; use core::fmt; use core::ops::Deref;
/// Types that contain a calendar /// /// This allows one to use [`Date`] with wrappers around calendars, /// e.g. reference counted calendars. pubtrait AsCalendar { /// The calendar being wrapped type Calendar: Calendar; /// Obtain the inner calendar fn as_calendar(&self) -> &Self::Calendar;
}
impl<C: Calendar> AsCalendar for C { type Calendar = C; #[inline] fn as_calendar(&self) -> &Self { self
}
}
impl<C: Calendar> AsCalendar for Rc<C> { type Calendar = C; #[inline] fn as_calendar(&self) -> &C { self
}
}
impl<C: Calendar> AsCalendar for Arc<C> { type Calendar = C; #[inline] fn as_calendar(&self) -> &C { self
}
}
/// This exists as a wrapper around `&'a T` so that /// `Date<&'a C>` is possible for calendar `C`. /// /// Unfortunately, /// [`AsCalendar`] cannot be implemented on `&'a T` directly because /// `&'a T` is `#[fundamental]` and the impl would clash with the one above with /// `AsCalendar` for `C: Calendar`. /// /// Use `Date<Ref<'a, C>>` where you would use `Date<&'a C>` #[allow(clippy::exhaustive_structs)] // newtype #[derive(PartialEq, Eq, Debug)] pubstructRef<'a, C>(pub &'a C);
impl<'a, C> Deref for Ref<'a, C> { type Target = C; fn deref(&self) -> &C { self.0
}
}
/// 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). /// /// ```rust /// 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); /// ``` pubstruct Date<A: AsCalendar> { pub(crate) inner: <A::Calendar as Calendar>::DateInner, pub(crate) calendar: A,
}
impl<A: AsCalendar> Date<A> { /// Construct a date from from era/month codes and fields, and some calendar representation #[inline] pubfn try_new_from_codes(
era: types::Era,
year: i32,
month_code: types::MonthCode,
day: u8,
calendar: A,
) -> Result<Self, CalendarError> { let inner = calendar
.as_calendar()
.date_from_codes(era, year, month_code, day)?;
Ok(Date { inner, calendar })
}
/// Construct a date from an ISO date and some calendar representation #[inline] pubfn new_from_iso(iso: Date<Iso>, calendar: A) -> Self { let inner = calendar.as_calendar().date_from_iso(iso);
Date { inner, calendar }
}
/// Convert the Date to an ISO Date #[inline] pubfn to_iso(&self) -> Date<Iso> { self.calendar.as_calendar().date_to_iso(self.inner())
}
/// Convert the Date to a date in a different calendar #[inline] pubfn to_calendar<A2: AsCalendar>(&self, calendar: A2) -> Date<A2> {
Date::new_from_iso(self.to_iso(), calendar)
}
/// The number of months in the year of this date #[inline] pubfn months_in_year(&self) -> u8 { self.calendar.as_calendar().months_in_year(self.inner())
}
/// The number of days in the year of this date #[inline] pubfn days_in_year(&self) -> u16 { self.calendar.as_calendar().days_in_year(self.inner())
}
/// The number of days in the month of this date #[inline] pubfn days_in_month(&self) -> u8 { self.calendar.as_calendar().days_in_month(self.inner())
}
/// The day of the week for this date /// /// Monday is 1, Sunday is 7, according to ISO #[inline] pubfn day_of_week(&self) -> types::IsoWeekday { self.calendar.as_calendar().day_of_week(self.inner())
}
/// Add a `duration` to this date, mutating it /// /// Currently unstable for ICU4X 1.0 #[doc(hidden)] #[inline] pubfn add(&mutself, duration: DateDuration<A::Calendar>) { self.calendar
.as_calendar()
.offset_date(&mutself.inner, duration)
}
/// Add a `duration` to this date, returning the new one /// /// Currently unstable for ICU4X 1.0 #[doc(hidden)] #[inline] pubfn added(mutself, duration: DateDuration<A::Calendar>) -> Self { self.add(duration); self
}
/// The calendar-specific year represented by `self` #[inline] pubfn year(&self) -> types::FormattableYear { self.calendar.as_calendar().year(&self.inner)
}
/// Returns whether `self` is in a calendar-specific leap year #[inline] pubfn is_in_leap_year(&self) -> bool { self.calendar.as_calendar().is_in_leap_year(&self.inner)
}
/// The calendar-specific month represented by `self` #[inline] pubfn month(&self) -> types::FormattableMonth { self.calendar.as_calendar().month(&self.inner)
}
/// The calendar-specific day-of-month represented by `self` #[inline] pubfn day_of_month(&self) -> types::DayOfMonth { self.calendar.as_calendar().day_of_month(&self.inner)
}
/// The calendar-specific day-of-month represented by `self` #[inline] pubfn day_of_year_info(&self) -> types::DayOfYearInfo { self.calendar.as_calendar().day_of_year_info(&self.inner)
}
/// 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)); /// ``` pubfn week_of_month(&self, first_weekday: types::IsoWeekday) -> types::WeekOfMonth { let config = WeekCalculator {
first_weekday,
min_week_days: 0, // ignored
weekend: None,
};
config.week_of_month(self.day_of_month(), self.day_of_week())
}
/// The week of the year containing this date. /// /// # Examples /// /// ``` /// 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 /// }) /// ); /// ``` pubfn week_of_year(&self, config: &WeekCalculator) -> Result<WeekOf, CalendarError> {
config.week_of_year(self.day_of_year_info(), self.day_of_week())
}
/// 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 #[inline] pubfn from_raw(inner: <A::Calendar as Calendar>::DateInner, calendar: A) -> Self { Self { inner, calendar }
}
/// Get the inner date implementation. Should not be called outside of calendar implementations #[inline] pubfn inner(&self) -> &<A::Calendar as Calendar>::DateInner {
&self.inner
}
/// Get a reference to the contained calendar #[inline] pubfn calendar(&self) -> &A::Calendar { self.calendar.as_calendar()
}
/// Get a reference to the contained calendar wrapper /// /// (Useful in case the user wishes to e.g. clone an Rc) #[inline] pubfn calendar_wrapper(&self) -> &A {
&self.calendar
}
impl<C: IntoAnyCalendar, A: AsCalendar<Calendar = C>> Date<A> { /// Type-erase the date, converting it to a date for [`AnyCalendar`] pubfn to_any(&self) -> Date<AnyCalendar> { let cal = self.calendar();
Date::from_raw(cal.date_to_any(self.inner()), cal.to_any_cloned())
}
}
impl<C: Calendar> Date<C> { /// Wrap the calendar type in `Rc<T>` /// /// Useful when paired with [`Self::to_any()`] to obtain a `Date<Rc<AnyCalendar>>` pubfn wrap_calendar_in_rc(self) -> Date<Rc<C>> {
Date::from_raw(self.inner, Rc::new(self.calendar))
}
/// Wrap the calendar type in `Arc<T>` /// /// Useful when paired with [`Self::to_any()`] to obtain a `Date<Rc<AnyCalendar>>` pubfn wrap_calendar_in_arc(self) -> Date<Arc<C>> {
Date::from_raw(self.inner, Arc::new(self.calendar))
}
}
impl<C, A, B> PartialEq<Date<B>> for Date<A> where
C: Calendar,
A: AsCalendar<Calendar = C>,
B: AsCalendar<Calendar = C>,
{ fn eq(&self, other: &Date<B>) -> bool { self.inner.eq(&other.inner)
}
}
impl<A: AsCalendar> Eq for Date<A> {}
impl<C, A, B> PartialOrd<Date<B>> for Date<A> where
C: Calendar,
C::DateInner: PartialOrd,
A: AsCalendar<Calendar = C>,
B: AsCalendar<Calendar = C>,
{ fn partial_cmp(&self, other: &Date<B>) -> Option<core::cmp::Ordering> { self.inner.partial_cmp(&other.inner)
}
}
impl<C, A> Ord for Date<A> where
C: Calendar,
C::DateInner: Ord,
A: AsCalendar<Calendar = C>,
{ fn cmp(&self, other: &Self) -> core::cmp::Ordering { self.inner.cmp(&other.inner)
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.