Skip to content

Commit

Permalink
DateTimeFormatter AnyCalendarKind getter (unicode-org#5907)
Browse files Browse the repository at this point in the history
Another part of unicode-org#5900
  • Loading branch information
sffc authored Dec 17, 2024
1 parent a2a0060 commit 756afb6
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 1 deletion.
32 changes: 31 additions & 1 deletion components/datetime/src/neo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -765,6 +765,36 @@ impl<FSet: DateTimeMarkers> DateTimeFormatter<FSet> {
_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.
Expand Down
3 changes: 3 additions & 0 deletions ffi/capi/bindings/c/DateFormatter.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions ffi/capi/bindings/c/DateTimeFormatter.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions ffi/capi/bindings/cpp/icu4x/DateFormatter.d.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions ffi/capi/bindings/cpp/icu4x/DateFormatter.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions ffi/capi/bindings/cpp/icu4x/DateTimeFormatter.d.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions ffi/capi/bindings/cpp/icu4x/DateTimeFormatter.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions ffi/capi/bindings/dart/DateFormatter.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions ffi/capi/bindings/dart/DateTimeFormatter.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions ffi/capi/bindings/js/DateFormatter.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions ffi/capi/bindings/js/DateFormatter.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions ffi/capi/bindings/js/DateTimeFormatter.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions ffi/capi/bindings/js/DateTimeFormatter.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions ffi/capi/src/datetime_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod ffi {
};

use crate::{
calendar::ffi::AnyCalendarKind,
date::ffi::{Date, IsoDate},
datetime::ffi::{DateTime, IsoDateTime},
errors::ffi::{DateTimeFormatError, DateTimeFormatterLoadError},
Expand Down Expand Up @@ -306,6 +307,12 @@ pub mod ffi {
let _infallible = self.0.format_any_calendar(&any).write_to(write);
Ok(())
}

/// Returns the calendar system used in this formatter.
#[diplomat::rust_link(icu::datetime::DateTimeFormatter::calendar_kind, FnInStruct)]
pub fn calendar_kind(&self) -> AnyCalendarKind {
self.0.calendar_kind().into()
}
}

#[diplomat::opaque]
Expand Down Expand Up @@ -372,5 +379,11 @@ pub mod ffi {
let _infallible = self.0.format_any_calendar(&any).write_to(write);
Ok(())
}

/// Returns the calendar system used in this formatter.
#[diplomat::rust_link(icu::datetime::DateTimeFormatter::calendar_kind, FnInStruct)]
pub fn calendar_kind(&self) -> AnyCalendarKind {
self.0.calendar_kind().into()
}
}
}

0 comments on commit 756afb6

Please sign in to comment.