Skip to content

Commit

Permalink
Add format_localized for chrono::naive::NaiveDate
Browse files Browse the repository at this point in the history
  • Loading branch information
mseele committed Nov 18, 2022
1 parent ff9db15 commit 3aa6d7f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/format/strftime.rs
Expand Up @@ -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,
Expand Down Expand Up @@ -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");
}
35 changes: 35 additions & 0 deletions src/naive/date.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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<I>
where
I: Iterator<Item = B> + Clone,
B: Borrow<Item<'a>>,
{
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<StrftimeItems<'a>> {
self.format_localized_with_items(StrftimeItems::new_with_locale(fmt, locale), locale)
}

/// Returns an iterator that steps by days across all representable dates.
///
/// # Example
Expand Down

0 comments on commit 3aa6d7f

Please sign in to comment.