4
4
5
5
//! High-level entrypoints for Neo DateTime Formatter
6
6
7
- use crate :: error:: { DateTimeFormatterLoadError , UnsupportedCalendarError } ;
7
+ use crate :: error:: DateTimeFormatterLoadError ;
8
8
use crate :: fieldsets:: builder:: FieldSetBuilder ;
9
9
use crate :: fieldsets:: enums:: CompositeFieldSet ;
10
10
use crate :: format:: datetime:: try_write_pattern_items;
@@ -153,7 +153,8 @@ where
153
153
/// Creates a new [`FixedCalendarDateTimeFormatter`] from compiled data with
154
154
/// datetime components specified at build time.
155
155
///
156
- /// This ignores the `calendar_kind` preference and instead uses the static calendar type.
156
+ /// This ignores the `calendar_kind` preference and instead uses the static calendar type,
157
+ /// and supports calendars that are not expressible as preferences, such as [`JapaneseExtended`](icu_calendar::cal::JapaneseExtended).
157
158
///
158
159
/// Use this constructor for optimal data size and memory use
159
160
/// if you know the required datetime components at build time.
@@ -165,7 +166,7 @@ where
165
166
/// Basic usage:
166
167
///
167
168
/// ```
168
- /// use icu::calendar::Gregorian ;
169
+ /// use icu::calendar::cal::JapaneseExtended ;
169
170
/// use icu::datetime::fieldsets::YMD;
170
171
/// use icu::datetime::input::Date;
171
172
/// use icu::datetime::FixedCalendarDateTimeFormatter;
@@ -179,8 +180,8 @@ where
179
180
/// .unwrap();
180
181
///
181
182
/// assert_writeable_eq!(
182
- /// formatter.format(&Date::try_new_gregorian (2023, 12, 20).unwrap()),
183
- /// "20 de diciembre de 2023 "
183
+ /// formatter.format(&Date::try_new_iso (2023, 12, 20).unwrap().to_calendar(JapaneseExtended::new() )),
184
+ /// "20 de diciembre de 5 Reiwa "
184
185
/// );
185
186
/// ```
186
187
#[ cfg( feature = "compiled_data" ) ]
@@ -457,7 +458,7 @@ size_test!(
457
458
pub struct DateTimeFormatter < FSet : DateTimeNamesMarker > {
458
459
selection : DateTimeZonePatternSelectionData ,
459
460
pub ( crate ) names : RawDateTimeNames < FSet > ,
460
- pub ( crate ) calendar : UntaggedAnyCalendarForFormatting ,
461
+ pub ( crate ) calendar : UntaggedFormattableAnyCalendar ,
461
462
}
462
463
463
464
impl < FSet : DateTimeMarkers > DateTimeFormatter < FSet >
@@ -562,33 +563,31 @@ where
562
563
) -> Result < Self , DateTimeFormatterLoadError >
563
564
where
564
565
P : ?Sized + AllAnyCalendarFormattingDataMarkers < FSet > ,
565
- L : DecimalFormatterLoader + AnyCalendarLoader ,
566
+ L : DecimalFormatterLoader + FormattableAnyCalendarLoader ,
566
567
{
567
- let kind = AnyCalendarForFormattingKind :: from_preferences ( prefs) ;
568
- let calendar = AnyCalendarLoader :: load ( loader, kind) ?;
568
+ let kind = FormattableAnyCalendarKind :: from_preferences ( prefs) ;
569
+ let calendar = FormattableAnyCalendarLoader :: load ( loader, kind) ?;
569
570
let names = RawDateTimeNames :: new_without_number_formatting ( ) ;
570
571
Self :: try_new_internal_with_calendar_and_names (
571
572
provider, provider, loader, prefs, field_set, calendar, names,
572
573
)
573
574
. map_err ( |e| e. 0 )
574
575
}
575
576
576
- // calendar.kind() is one of Buddhist, Chinese, Coptic, Dangi, Ethiopian, EthiopianAmeteAlem, Gregorian, Hebrew, Indian,
577
- // HijriCivil, HijriObservationalMecca, HijriTabular, HijriUmmAlQura, Japanese, JapaneseExtended, Persian, Roc
578
577
#[ allow( clippy:: result_large_err) ] // returning ownership of an argument to the caller
579
578
pub ( crate ) fn try_new_internal_with_calendar_and_names < P0 , P1 , L > (
580
579
provider_p : & P0 ,
581
580
provider : & P1 ,
582
581
loader : & L ,
583
582
prefs : DateTimeFormatterPreferences ,
584
583
field_set : CompositeFieldSet ,
585
- calendar : AnyCalendarForFormatting ,
584
+ calendar : FormattableAnyCalendar ,
586
585
mut names : RawDateTimeNames < FSet > ,
587
586
) -> Result <
588
587
Self ,
589
588
(
590
589
DateTimeFormatterLoadError ,
591
- ( AnyCalendarForFormatting , RawDateTimeNames < FSet > ) ,
590
+ ( FormattableAnyCalendar , RawDateTimeNames < FSet > ) ,
592
591
) ,
593
592
>
594
593
where
@@ -597,7 +596,7 @@ where
597
596
L : DecimalFormatterLoader ,
598
597
{
599
598
let selection = DateTimeZonePatternSelectionData :: try_new_with_skeleton (
600
- & AnyCalendarProvider :: < <FSet :: D as DateDataMarkers >:: Skel , _ > :: new (
599
+ & FormattableAnyCalendarNamesLoader :: < <FSet :: D as DateDataMarkers >:: Skel , _ > :: new (
601
600
provider_p,
602
601
calendar. kind ( ) ,
603
602
) ,
@@ -611,11 +610,11 @@ where
611
610
Err ( e) => return Err ( ( DateTimeFormatterLoadError :: Data ( e) , ( calendar, names) ) ) ,
612
611
} ;
613
612
let result = names. load_for_pattern (
614
- & AnyCalendarProvider :: < <FSet :: D as DateDataMarkers >:: Year , _ > :: new (
613
+ & FormattableAnyCalendarNamesLoader :: < <FSet :: D as DateDataMarkers >:: Year , _ > :: new (
615
614
provider,
616
615
calendar. kind ( ) ,
617
616
) ,
618
- & AnyCalendarProvider :: < <FSet :: D as DateDataMarkers >:: Month , _ > :: new (
617
+ & FormattableAnyCalendarNamesLoader :: < <FSet :: D as DateDataMarkers >:: Month , _ > :: new (
619
618
provider,
620
619
calendar. kind ( ) ,
621
620
) ,
@@ -879,29 +878,21 @@ impl<C: CldrCalendar, FSet: DateTimeMarkers> FixedCalendarDateTimeFormatter<C, F
879
878
/// YMD::long(),
880
879
/// )
881
880
/// .unwrap()
882
- /// .try_into_formatter(Hebrew::new())
883
- /// .expect("Hebrew is supported in DateTimeFormatter");
881
+ /// .into_formatter(Hebrew::new());
884
882
///
885
883
/// let date = Date::try_new_iso(2024, 10, 14).unwrap();
886
884
///
887
885
/// assert_writeable_eq!(formatter.format(&date), "12 Tishri 5785");
888
886
/// ```
889
- pub fn try_into_formatter (
890
- self ,
891
- calendar : C ,
892
- ) -> Result < DateTimeFormatter < FSet > , UnsupportedCalendarError >
887
+ pub fn into_formatter ( self , calendar : C ) -> DateTimeFormatter < FSet >
893
888
where
894
- C : IntoAnyCalendar ,
889
+ C : IntoFormattableAnyCalendar ,
895
890
{
896
- let any_calendar = calendar. to_any ( ) ;
897
- let kind = any_calendar. kind ( ) ;
898
- let calendar = AnyCalendarForFormatting :: try_from_any_calendar ( any_calendar)
899
- . ok_or ( UnsupportedCalendarError { kind } ) ?;
900
- Ok ( DateTimeFormatter {
891
+ DateTimeFormatter {
901
892
selection : self . selection ,
902
893
names : self . names ,
903
- calendar : calendar. into_untagged ( ) ,
904
- } )
894
+ calendar : FormattableAnyCalendar :: from_calendar ( calendar) . into_untagged ( ) ,
895
+ }
905
896
}
906
897
907
898
/// Maps a [`FixedCalendarDateTimeFormatter`] of a specific `FSet` to a more general `FSet`.
0 commit comments