diff --git a/src/format/strftime.rs b/src/format/strftime.rs index b2588fd961..6338194366 100644 --- a/src/format/strftime.rs +++ b/src/format/strftime.rs @@ -663,7 +663,7 @@ fn test_strftime_docs() { #[cfg(feature = "unstable-locales")] #[test] fn test_strftime_docs_localized() { - use crate::{FixedOffset, TimeZone}; + use crate::{FixedOffset, NaiveDate, TimeZone}; let dt = FixedOffset::east_opt(34200).unwrap().ymd_opt(2001, 7, 8).unwrap().and_hms_nano( 0, @@ -696,4 +696,17 @@ fn test_strftime_docs_localized() { dt.format_localized("%c", Locale::fr_BE).to_string(), "dim 08 jui 2001 00:34:60 +09:30" ); + + let nd = NaiveDate::from_ymd_opt(2001, 7, 8).unwrap(); + + // date specifiers + assert_eq!(nd.format_localized("%b", Locale::de_DE).to_string(), "Jul"); + assert_eq!(nd.format_localized("%B", Locale::de_DE).to_string(), "Juli"); + assert_eq!(nd.format_localized("%h", Locale::de_DE).to_string(), "Jul"); + assert_eq!(nd.format_localized("%a", Locale::de_DE).to_string(), "So"); + assert_eq!(nd.format_localized("%A", Locale::de_DE).to_string(), "Sonntag"); + assert_eq!(nd.format_localized("%D", Locale::de_DE).to_string(), "07/08/01"); + assert_eq!(nd.format_localized("%x", Locale::de_DE).to_string(), "08.07.2001"); + assert_eq!(nd.format_localized("%F", Locale::de_DE).to_string(), "2001-07-08"); + assert_eq!(nd.format_localized("%v", Locale::de_DE).to_string(), " 8-Jul-2001"); } diff --git a/src/naive/date.rs b/src/naive/date.rs index f88a247275..3fa2884406 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -14,6 +14,10 @@ use num_traits::ToPrimitive; #[cfg(feature = "rkyv")] use rkyv::{Archive, Deserialize, Serialize}; +/// L10n locales. +#[cfg(feature = "unstable-locales")] +use pure_rust_locales::Locale; + #[cfg(any(feature = "alloc", feature = "std", test))] use crate::format::DelayedFormat; use crate::format::{parse, write_hundreds, ParseError, ParseResult, Parsed, StrftimeItems}; @@ -1144,6 +1148,37 @@ impl NaiveDate { self.format_with_items(StrftimeItems::new(fmt)) } + /// Formats the date with the specified formatting items and locale. + #[cfg(feature = "unstable-locales")] + #[cfg_attr(docsrs, doc(cfg(feature = "unstable-locales")))] + #[inline] + pub fn format_localized_with_items<'a, I, B>( + &self, + items: I, + locale: Locale, + ) -> DelayedFormat + where + I: Iterator + Clone, + B: Borrow>, + { + DelayedFormat::new_with_locale(Some(*self), None, items, locale) + } + + /// Formats the date with the specified format string and locale. + /// + /// See the [`crate::format::strftime`] module on the supported escape + /// sequences. + #[cfg(feature = "unstable-locales")] + #[cfg_attr(docsrs, doc(cfg(feature = "unstable-locales")))] + #[inline] + pub fn format_localized<'a>( + &self, + fmt: &'a str, + locale: Locale, + ) -> DelayedFormat> { + self.format_localized_with_items(StrftimeItems::new_with_locale(fmt, locale), locale) + } + /// Returns an iterator that steps by days across all representable dates. /// /// # Example