From 756afb600a625784dc5600fe8b7108593c24ecb1 Mon Sep 17 00:00:00 2001 From: "Shane F. Carr" Date: Mon, 16 Dec 2024 19:29:16 -0800 Subject: [PATCH] DateTimeFormatter AnyCalendarKind getter (#5907) Another part of #5900 --- components/datetime/src/neo.rs | 32 ++++++++++++++++++- ffi/capi/bindings/c/DateFormatter.h | 3 ++ ffi/capi/bindings/c/DateTimeFormatter.h | 3 ++ .../bindings/cpp/icu4x/DateFormatter.d.hpp | 3 ++ ffi/capi/bindings/cpp/icu4x/DateFormatter.hpp | 8 +++++ .../cpp/icu4x/DateTimeFormatter.d.hpp | 3 ++ .../bindings/cpp/icu4x/DateTimeFormatter.hpp | 8 +++++ ffi/capi/bindings/dart/DateFormatter.g.dart | 13 ++++++++ .../bindings/dart/DateTimeFormatter.g.dart | 13 ++++++++ ffi/capi/bindings/js/DateFormatter.d.ts | 3 ++ ffi/capi/bindings/js/DateFormatter.mjs | 11 +++++++ ffi/capi/bindings/js/DateTimeFormatter.d.ts | 3 ++ ffi/capi/bindings/js/DateTimeFormatter.mjs | 11 +++++++ ffi/capi/src/datetime_formatter.rs | 13 ++++++++ 14 files changed, 126 insertions(+), 1 deletion(-) diff --git a/components/datetime/src/neo.rs b/components/datetime/src/neo.rs index 988877f82a0..9a1c1599658 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. diff --git a/ffi/capi/bindings/c/DateFormatter.h b/ffi/capi/bindings/c/DateFormatter.h index d872bd3f509..3b5532aceb3 100644 --- a/ffi/capi/bindings/c/DateFormatter.h +++ b/ffi/capi/bindings/c/DateFormatter.h @@ -7,6 +7,7 @@ #include #include "diplomat_runtime.h" +#include "AnyCalendarKind.d.h" #include "DataProvider.d.h" #include "Date.d.h" #include "DateTime.d.h" @@ -42,6 +43,8 @@ icu4x_DateFormatter_format_datetime_mv1_result icu4x_DateFormatter_format_dateti typedef struct icu4x_DateFormatter_format_iso_datetime_mv1_result {union { DateTimeFormatError err;}; bool is_ok;} icu4x_DateFormatter_format_iso_datetime_mv1_result; icu4x_DateFormatter_format_iso_datetime_mv1_result icu4x_DateFormatter_format_iso_datetime_mv1(const DateFormatter* self, const IsoDateTime* value, DiplomatWrite* write); +AnyCalendarKind icu4x_DateFormatter_calendar_kind_mv1(const DateFormatter* self); + void icu4x_DateFormatter_destroy_mv1(DateFormatter* self); diff --git a/ffi/capi/bindings/c/DateTimeFormatter.h b/ffi/capi/bindings/c/DateTimeFormatter.h index 045724563e2..306caee11ca 100644 --- a/ffi/capi/bindings/c/DateTimeFormatter.h +++ b/ffi/capi/bindings/c/DateTimeFormatter.h @@ -7,6 +7,7 @@ #include #include "diplomat_runtime.h" +#include "AnyCalendarKind.d.h" #include "DataProvider.d.h" #include "DateTime.d.h" #include "DateTimeFormatError.d.h" @@ -34,6 +35,8 @@ icu4x_DateTimeFormatter_format_datetime_mv1_result icu4x_DateTimeFormatter_forma typedef struct icu4x_DateTimeFormatter_format_iso_datetime_mv1_result {union { DateTimeFormatError err;}; bool is_ok;} icu4x_DateTimeFormatter_format_iso_datetime_mv1_result; icu4x_DateTimeFormatter_format_iso_datetime_mv1_result icu4x_DateTimeFormatter_format_iso_datetime_mv1(const DateTimeFormatter* self, const IsoDateTime* value, DiplomatWrite* write); +AnyCalendarKind icu4x_DateTimeFormatter_calendar_kind_mv1(const DateTimeFormatter* self); + void icu4x_DateTimeFormatter_destroy_mv1(DateTimeFormatter* self); diff --git a/ffi/capi/bindings/cpp/icu4x/DateFormatter.d.hpp b/ffi/capi/bindings/cpp/icu4x/DateFormatter.d.hpp index 708f49b74fa..dc8b56c4bb5 100644 --- a/ffi/capi/bindings/cpp/icu4x/DateFormatter.d.hpp +++ b/ffi/capi/bindings/cpp/icu4x/DateFormatter.d.hpp @@ -24,6 +24,7 @@ namespace capi { struct IsoDateTime; } class IsoDateTime; namespace capi { struct Locale; } class Locale; +class AnyCalendarKind; class DateTimeFormatError; class DateTimeFormatterLoadError; class DateTimeLength; @@ -52,6 +53,8 @@ class DateFormatter { inline diplomat::result format_iso_datetime(const icu4x::IsoDateTime& value) const; + inline icu4x::AnyCalendarKind calendar_kind() const; + inline const icu4x::capi::DateFormatter* AsFFI() const; inline icu4x::capi::DateFormatter* AsFFI(); inline static const icu4x::DateFormatter* FromFFI(const icu4x::capi::DateFormatter* ptr); diff --git a/ffi/capi/bindings/cpp/icu4x/DateFormatter.hpp b/ffi/capi/bindings/cpp/icu4x/DateFormatter.hpp index 7879bf7fa5c..aad249f7e98 100644 --- a/ffi/capi/bindings/cpp/icu4x/DateFormatter.hpp +++ b/ffi/capi/bindings/cpp/icu4x/DateFormatter.hpp @@ -10,6 +10,7 @@ #include #include #include "../diplomat_runtime.hpp" +#include "AnyCalendarKind.hpp" #include "DataProvider.hpp" #include "Date.hpp" #include "DateTime.hpp" @@ -43,6 +44,8 @@ namespace capi { typedef struct icu4x_DateFormatter_format_iso_datetime_mv1_result {union { icu4x::capi::DateTimeFormatError err;}; bool is_ok;} icu4x_DateFormatter_format_iso_datetime_mv1_result; icu4x_DateFormatter_format_iso_datetime_mv1_result icu4x_DateFormatter_format_iso_datetime_mv1(const icu4x::capi::DateFormatter* self, const icu4x::capi::IsoDateTime* value, diplomat::capi::DiplomatWrite* write); + icu4x::capi::AnyCalendarKind icu4x_DateFormatter_calendar_kind_mv1(const icu4x::capi::DateFormatter* self); + void icu4x_DateFormatter_destroy_mv1(DateFormatter* self); @@ -99,6 +102,11 @@ inline diplomat::result icu4x::DateForm return result.is_ok ? diplomat::result(diplomat::Ok(std::move(output))) : diplomat::result(diplomat::Err(icu4x::DateTimeFormatError::FromFFI(result.err))); } +inline icu4x::AnyCalendarKind icu4x::DateFormatter::calendar_kind() const { + auto result = icu4x::capi::icu4x_DateFormatter_calendar_kind_mv1(this->AsFFI()); + return icu4x::AnyCalendarKind::FromFFI(result); +} + inline const icu4x::capi::DateFormatter* icu4x::DateFormatter::AsFFI() const { return reinterpret_cast(this); } diff --git a/ffi/capi/bindings/cpp/icu4x/DateTimeFormatter.d.hpp b/ffi/capi/bindings/cpp/icu4x/DateTimeFormatter.d.hpp index be7dcdd276e..56c4d5eb5f9 100644 --- a/ffi/capi/bindings/cpp/icu4x/DateTimeFormatter.d.hpp +++ b/ffi/capi/bindings/cpp/icu4x/DateTimeFormatter.d.hpp @@ -20,6 +20,7 @@ namespace capi { struct IsoDateTime; } class IsoDateTime; namespace capi { struct Locale; } class Locale; +class AnyCalendarKind; class DateTimeFormatError; class DateTimeFormatterLoadError; class DateTimeLength; @@ -44,6 +45,8 @@ class DateTimeFormatter { inline diplomat::result format_iso_datetime(const icu4x::IsoDateTime& value) const; + inline icu4x::AnyCalendarKind calendar_kind() const; + inline const icu4x::capi::DateTimeFormatter* AsFFI() const; inline icu4x::capi::DateTimeFormatter* AsFFI(); inline static const icu4x::DateTimeFormatter* FromFFI(const icu4x::capi::DateTimeFormatter* ptr); diff --git a/ffi/capi/bindings/cpp/icu4x/DateTimeFormatter.hpp b/ffi/capi/bindings/cpp/icu4x/DateTimeFormatter.hpp index e33aa20b0ce..65246177659 100644 --- a/ffi/capi/bindings/cpp/icu4x/DateTimeFormatter.hpp +++ b/ffi/capi/bindings/cpp/icu4x/DateTimeFormatter.hpp @@ -10,6 +10,7 @@ #include #include #include "../diplomat_runtime.hpp" +#include "AnyCalendarKind.hpp" #include "DataProvider.hpp" #include "DateTime.hpp" #include "DateTimeFormatError.hpp" @@ -35,6 +36,8 @@ namespace capi { typedef struct icu4x_DateTimeFormatter_format_iso_datetime_mv1_result {union { icu4x::capi::DateTimeFormatError err;}; bool is_ok;} icu4x_DateTimeFormatter_format_iso_datetime_mv1_result; icu4x_DateTimeFormatter_format_iso_datetime_mv1_result icu4x_DateTimeFormatter_format_iso_datetime_mv1(const icu4x::capi::DateTimeFormatter* self, const icu4x::capi::IsoDateTime* value, diplomat::capi::DiplomatWrite* write); + icu4x::capi::AnyCalendarKind icu4x_DateTimeFormatter_calendar_kind_mv1(const icu4x::capi::DateTimeFormatter* self); + void icu4x_DateTimeFormatter_destroy_mv1(DateTimeFormatter* self); @@ -73,6 +76,11 @@ inline diplomat::result icu4x::DateTime return result.is_ok ? diplomat::result(diplomat::Ok(std::move(output))) : diplomat::result(diplomat::Err(icu4x::DateTimeFormatError::FromFFI(result.err))); } +inline icu4x::AnyCalendarKind icu4x::DateTimeFormatter::calendar_kind() const { + auto result = icu4x::capi::icu4x_DateTimeFormatter_calendar_kind_mv1(this->AsFFI()); + return icu4x::AnyCalendarKind::FromFFI(result); +} + inline const icu4x::capi::DateTimeFormatter* icu4x::DateTimeFormatter::AsFFI() const { return reinterpret_cast(this); } diff --git a/ffi/capi/bindings/dart/DateFormatter.g.dart b/ffi/capi/bindings/dart/DateFormatter.g.dart index 27ffae6a5f5..3635269998e 100644 --- a/ffi/capi/bindings/dart/DateFormatter.g.dart +++ b/ffi/capi/bindings/dart/DateFormatter.g.dart @@ -98,6 +98,14 @@ final class DateFormatter implements ffi.Finalizable { } return write.finalize(); } + + /// Returns the calendar system used in this formatter. + /// + /// See the [Rust documentation for `calendar_kind`](https://docs.rs/icu/latest/icu/datetime/struct.DateTimeFormatter.html#method.calendar_kind) for more information. + AnyCalendarKind calendarKind() { + final result = _icu4x_DateFormatter_calendar_kind_mv1(_ffi); + return AnyCalendarKind.values[result]; + } } @meta.RecordUse() @@ -134,3 +142,8 @@ external _ResultVoidInt32 _icu4x_DateFormatter_format_datetime_mv1(ffi.Pointer, ffi.Pointer, ffi.Pointer)>(isLeaf: true, symbol: 'icu4x_DateFormatter_format_iso_datetime_mv1') // ignore: non_constant_identifier_names external _ResultVoidInt32 _icu4x_DateFormatter_format_iso_datetime_mv1(ffi.Pointer self, ffi.Pointer value, ffi.Pointer write); + +@meta.RecordUse() +@ffi.Native)>(isLeaf: true, symbol: 'icu4x_DateFormatter_calendar_kind_mv1') +// ignore: non_constant_identifier_names +external int _icu4x_DateFormatter_calendar_kind_mv1(ffi.Pointer self); diff --git a/ffi/capi/bindings/dart/DateTimeFormatter.g.dart b/ffi/capi/bindings/dart/DateTimeFormatter.g.dart index e803c83b190..fb2bc1c92f4 100644 --- a/ffi/capi/bindings/dart/DateTimeFormatter.g.dart +++ b/ffi/capi/bindings/dart/DateTimeFormatter.g.dart @@ -72,6 +72,14 @@ final class DateTimeFormatter implements ffi.Finalizable { } return write.finalize(); } + + /// Returns the calendar system used in this formatter. + /// + /// See the [Rust documentation for `calendar_kind`](https://docs.rs/icu/latest/icu/datetime/struct.DateTimeFormatter.html#method.calendar_kind) for more information. + AnyCalendarKind calendarKind() { + final result = _icu4x_DateTimeFormatter_calendar_kind_mv1(_ffi); + return AnyCalendarKind.values[result]; + } } @meta.RecordUse() @@ -98,3 +106,8 @@ external _ResultVoidInt32 _icu4x_DateTimeFormatter_format_datetime_mv1(ffi.Point @ffi.Native<_ResultVoidInt32 Function(ffi.Pointer, ffi.Pointer, ffi.Pointer)>(isLeaf: true, symbol: 'icu4x_DateTimeFormatter_format_iso_datetime_mv1') // ignore: non_constant_identifier_names external _ResultVoidInt32 _icu4x_DateTimeFormatter_format_iso_datetime_mv1(ffi.Pointer self, ffi.Pointer value, ffi.Pointer write); + +@meta.RecordUse() +@ffi.Native)>(isLeaf: true, symbol: 'icu4x_DateTimeFormatter_calendar_kind_mv1') +// ignore: non_constant_identifier_names +external int _icu4x_DateTimeFormatter_calendar_kind_mv1(ffi.Pointer self); diff --git a/ffi/capi/bindings/js/DateFormatter.d.ts b/ffi/capi/bindings/js/DateFormatter.d.ts index 97ca1f354c9..7a8c8880897 100644 --- a/ffi/capi/bindings/js/DateFormatter.d.ts +++ b/ffi/capi/bindings/js/DateFormatter.d.ts @@ -1,4 +1,5 @@ // generated by diplomat-tool +import type { AnyCalendarKind } from "./AnyCalendarKind" import type { DataProvider } from "./DataProvider" import type { Date } from "./Date" import type { DateTime } from "./DateTime" @@ -32,4 +33,6 @@ export class DateFormatter { formatDatetime(value: DateTime): string; formatIsoDatetime(value: IsoDateTime): string; + + calendarKind(): AnyCalendarKind; } \ No newline at end of file diff --git a/ffi/capi/bindings/js/DateFormatter.mjs b/ffi/capi/bindings/js/DateFormatter.mjs index 8d721c3e78a..4e1e7639161 100644 --- a/ffi/capi/bindings/js/DateFormatter.mjs +++ b/ffi/capi/bindings/js/DateFormatter.mjs @@ -1,4 +1,5 @@ // generated by diplomat-tool +import { AnyCalendarKind } from "./AnyCalendarKind.mjs" import { DataProvider } from "./DataProvider.mjs" import { Date } from "./Date.mjs" import { DateTime } from "./DateTime.mjs" @@ -171,4 +172,14 @@ export class DateFormatter { write.free(); } } + + calendarKind() { + const result = wasm.icu4x_DateFormatter_calendar_kind_mv1(this.ffiValue); + + try { + return new AnyCalendarKind(diplomatRuntime.internalConstructor, result); + } + + finally {} + } } \ No newline at end of file diff --git a/ffi/capi/bindings/js/DateTimeFormatter.d.ts b/ffi/capi/bindings/js/DateTimeFormatter.d.ts index 7658b058e76..78fbb2fbb16 100644 --- a/ffi/capi/bindings/js/DateTimeFormatter.d.ts +++ b/ffi/capi/bindings/js/DateTimeFormatter.d.ts @@ -1,4 +1,5 @@ // generated by diplomat-tool +import type { AnyCalendarKind } from "./AnyCalendarKind" import type { DataProvider } from "./DataProvider" import type { DateTime } from "./DateTime" import type { DateTimeFormatError } from "./DateTimeFormatError" @@ -26,4 +27,6 @@ export class DateTimeFormatter { formatDatetime(value: DateTime): string; formatIsoDatetime(value: IsoDateTime): string; + + calendarKind(): AnyCalendarKind; } \ No newline at end of file diff --git a/ffi/capi/bindings/js/DateTimeFormatter.mjs b/ffi/capi/bindings/js/DateTimeFormatter.mjs index 5d6e1359ac1..524e6bfad15 100644 --- a/ffi/capi/bindings/js/DateTimeFormatter.mjs +++ b/ffi/capi/bindings/js/DateTimeFormatter.mjs @@ -1,4 +1,5 @@ // generated by diplomat-tool +import { AnyCalendarKind } from "./AnyCalendarKind.mjs" import { DataProvider } from "./DataProvider.mjs" import { DateTime } from "./DateTime.mjs" import { DateTimeFormatError } from "./DateTimeFormatError.mjs" @@ -125,4 +126,14 @@ export class DateTimeFormatter { write.free(); } } + + calendarKind() { + const result = wasm.icu4x_DateTimeFormatter_calendar_kind_mv1(this.ffiValue); + + try { + return new AnyCalendarKind(diplomatRuntime.internalConstructor, result); + } + + finally {} + } } \ No newline at end of file diff --git a/ffi/capi/src/datetime_formatter.rs b/ffi/capi/src/datetime_formatter.rs index 98bb427d1a4..b4eeba75df7 100644 --- a/ffi/capi/src/datetime_formatter.rs +++ b/ffi/capi/src/datetime_formatter.rs @@ -13,6 +13,7 @@ pub mod ffi { }; use crate::{ + calendar::ffi::AnyCalendarKind, date::ffi::{Date, IsoDate}, datetime::ffi::{DateTime, IsoDateTime}, errors::ffi::{DateTimeFormatError, DateTimeFormatterLoadError}, @@ -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] @@ -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() + } } }