Skip to content

Commit b27aa37

Browse files
authored
Add datetime formatting impls on Weekday with test (#7719)
See #7710
1 parent b83a1c5 commit b27aa37

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

components/datetime/src/fieldsets.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,26 @@ impl_date_marker!(
10221022
);
10231023

10241024
impl_date_marker!(
1025+
/// Format a weekday using [`Weekday`]:
1026+
///
1027+
/// ```
1028+
/// use icu::calendar::Gregorian;
1029+
/// use icu::calendar::types::Weekday;
1030+
/// use icu::datetime::fieldsets;
1031+
/// use icu::datetime::FixedCalendarDateTimeFormatter;
1032+
/// use icu::locale::locale;
1033+
/// use writeable::assert_writeable_eq;
1034+
///
1035+
/// let formatter = FixedCalendarDateTimeFormatter::<Gregorian, _>::try_new(
1036+
/// locale!("fr").into(),
1037+
/// fieldsets::E::long(),
1038+
/// ).unwrap();
1039+
///
1040+
/// assert_writeable_eq!(
1041+
/// formatter.format(&Weekday::Monday),
1042+
/// "lundi"
1043+
/// );
1044+
/// ```
10251045
E,
10261046
ET,
10271047
description = "weekday (standalone)",

components/datetime/src/scaffold/calendar.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::MismatchedCalendarError;
1010
use core::marker::PhantomData;
1111
use icu_calendar::cal;
1212
use icu_calendar::preferences::{CalendarAlgorithm, CalendarPreferences, HijriCalendarAlgorithm};
13+
use icu_calendar::types::Weekday;
1314
use icu_calendar::{AnyCalendar, AnyCalendarKind, AsCalendar, Date, IntoAnyCalendar, Ref};
1415
use icu_provider::marker::NeverMarker;
1516
use icu_provider::prelude::*;
@@ -600,6 +601,11 @@ impl CalMarkers<ErasedPackedPatterns> for FullDataCalMarkers {
600601
}
601602

602603
/// A type that can be converted into a specific calendar system.
604+
///
605+
/// You often want to set `Converted` to an ICU4X built-in type.
606+
///
607+
/// If the type is not calendar-specific, such as a time or time zone, set `Converted`
608+
/// to the same type and return it in the implementation.
603609
// This trait is implementable
604610
pub trait ConvertCalendar {
605611
/// The converted type. This can be the same as the receiver type.
@@ -649,6 +655,14 @@ impl<C: IntoAnyCalendar, A: AsCalendar<Calendar = C>, Z: Copy> ConvertCalendar
649655
}
650656
}
651657

658+
impl ConvertCalendar for UtcOffset {
659+
type Converted<'a> = UtcOffset;
660+
#[inline]
661+
fn to_calendar<'a>(&self, _: &'a AnyCalendar) -> Self::Converted<'a> {
662+
*self
663+
}
664+
}
665+
652666
impl<O: TimeZoneModel> ConvertCalendar for TimeZoneInfo<O> {
653667
type Converted<'a> = TimeZoneInfo<O>;
654668
#[inline]
@@ -657,6 +671,14 @@ impl<O: TimeZoneModel> ConvertCalendar for TimeZoneInfo<O> {
657671
}
658672
}
659673

674+
impl ConvertCalendar for Weekday {
675+
type Converted<'a> = Weekday;
676+
#[inline]
677+
fn to_calendar<'a>(&self, _: &'a AnyCalendar) -> Self::Converted<'a> {
678+
*self
679+
}
680+
}
681+
660682
/// An input that may be associated with a specific runtime calendar.
661683
// This trait is implementable
662684
pub trait InSameCalendar {
@@ -727,7 +749,16 @@ impl<O: TimeZoneModel> InSameCalendar for TimeZoneInfo<O> {
727749
}
728750
}
729751

752+
impl InSameCalendar for Weekday {
753+
#[inline]
754+
fn check_any_calendar_kind(&self, _: AnyCalendarKind) -> Result<(), MismatchedCalendarError> {
755+
Ok(())
756+
}
757+
}
758+
730759
/// An input associated with a fixed, static calendar.
760+
///
761+
/// Inputs that are not calendar-specific should blanket-impl this for all `C`.
731762
// This trait is implementable
732763
pub trait InFixedCalendar<C> {}
733764

@@ -745,3 +776,5 @@ impl<C, Z> InFixedCalendar<C> for ZonedTime<Z> {}
745776
impl<C> InFixedCalendar<C> for UtcOffset {}
746777

747778
impl<C, O: TimeZoneModel> InFixedCalendar<C> for TimeZoneInfo<O> {}
779+
780+
impl<C> InFixedCalendar<C> for Weekday {}

components/datetime/src/scaffold/get_field.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,8 @@ where
390390
}
391391
}
392392

393+
impl UnstableSealed for Weekday {}
394+
393395
impl<C: Calendar, A: AsCalendar<Calendar = C>> GetField<()> for Date<A> {
394396
#[inline]
395397
fn get_field(&self) {}
@@ -425,3 +427,8 @@ impl<O: TimeZoneModel> GetField<()> for TimeZoneInfo<O> {
425427
#[inline]
426428
fn get_field(&self) {}
427429
}
430+
431+
impl GetField<()> for Weekday {
432+
#[inline]
433+
fn get_field(&self) {}
434+
}

components/datetime/src/scaffold/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@
66
//!
77
//! Items in this module are mostly for trait bounds. Most users should not need to reference
88
//! these items in userland code.
9+
//!
10+
//! # Implementing a custom input type
11+
//!
12+
//! One reason you might find yourself in this module is because you have a custom datetime
13+
//! type, and you want to plug it into ICU4X for formatting.
14+
//!
15+
//! To properly equip your type for ICU4X, implement the following traits:
16+
//!
17+
//! - [`UnstableSealed`] must always be implemented to acknowledge that these traits are
18+
//! subject to change, even across minor releases.
19+
//! - [`GetField<()>`] must almost always be implemented. It is used as a placeholder for input fields that a particular fieldset does not require.
20+
//! - Additional [`GetField`] impls based on what fields your type contains. For a list of all possible fields, see [`AllInputMarkers`].
21+
//! - [`ConvertCalendar`], for [`DateTimeFormatter::format`](crate::DateTimeFormatter::format)
22+
//! - [`InSameCalendar`], for [`DateTimeFormatter::format_same_calendar`](crate::DateTimeFormatter::format_same_calendar)
23+
//! - [`InFixedCalendar`], for [`FixedCalendarDateTimeFormatter::format`](crate::FixedCalendarDateTimeFormatter::format)
924
1025
mod calendar;
1126
mod dynamic_impls;

0 commit comments

Comments
 (0)