Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTimeFormatter AnyCalendarKind getter #5907

Merged
merged 4 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}
}
Loading