From 2f8eca940fc54672f2b7abe3963f453db3ce27e6 Mon Sep 17 00:00:00 2001 From: "Shane F. Carr" Date: Mon, 16 Dec 2024 14:51:43 -0800 Subject: [PATCH] DateTimeFormatter AnyCalendarKind getter --- components/datetime/src/neo.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/components/datetime/src/neo.rs b/components/datetime/src/neo.rs index 988877f82a0..51a6590cb97 100644 --- a/components/datetime/src/neo.rs +++ b/components/datetime/src/neo.rs @@ -21,7 +21,7 @@ use crate::MismatchedCalendarError; use core::fmt; use core::marker::PhantomData; use icu_calendar::any_calendar::IntoAnyCalendar; -use icu_calendar::{AnyCalendar, AnyCalendarPreferences}; +use icu_calendar::{AnyCalendar, AnyCalendarKind, AnyCalendarPreferences}; use icu_decimal::FixedDecimalFormatterPreferences; use icu_locale_core::preferences::extensions::unicode::keywords::{ CalendarAlgorithm, HourCycle, NumberingSystem, @@ -765,6 +765,36 @@ impl DateTimeFormatter { _calendar: PhantomData, }) } + + /// Returns the calendar system used in this formatter. + /// + /// # Examples + /// + /// ``` + /// use icu::calendar::AnyCalendarKind; + /// use icu::calendar::Date; + /// use icu::datetime::fieldsets::YMD; + /// use icu::datetime::DateTimeFormatter; + /// use icu::locale::locale; + /// use writeable::assert_writeable_eq; + /// + /// let formatter = DateTimeFormatter::try_new( + /// locale!("th").into(), + /// YMD::long(), + /// ) + /// .unwrap(); + /// + /// assert_writeable_eq!( + /// formatter.format_any_calendar(&Date::try_new_iso(2024, 12, 16).unwrap()), + /// "16 ธันวาคม 2567" + /// ); + /// + /// assert_eq!(formatter.calendar_kind(), AnyCalendarKind::Buddhist); + /// assert_eq!(formatter.calendar_kind().as_bcp47_string(), "buddhist"); + /// ``` + pub fn calendar_kind(&self) -> AnyCalendarKind { + self.calendar.kind() + } } /// A formatter optimized for time and time zone formatting.