Skip to content

Commit 79de122

Browse files
committed
Add more documentation to StrftimeItems::new_with_locale
1 parent 5b7cf85 commit 79de122

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

src/format/strftime.rs

+45-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,51 @@ impl<'a> StrftimeItems<'a> {
228228
}
229229
}
230230

231-
/// Creates a new parsing iterator from the `strftime`-like format string.
231+
/// Creates a new parsing iterator from a `strftime`-like format string, with some formatting
232+
/// specifiers adjusted to match [`Locale`].
233+
///
234+
/// Note: `StrftimeItems::new_with_locale` only localizes the *format*. You usually want to
235+
/// combine it with other locale-aware methods such as
236+
/// [`DateTime::format_localized_with_items`] to get things like localized month or day names.
237+
///
238+
/// The `%x` formatting specifier will use the local date format, `%X` the local time format,
239+
/// and `%c` the local format for date and time.
240+
/// `%r` will use the local 12-hour clock format (e.g., 11:11:04 PM). Not all locales have such
241+
/// a format, in which case we fall back to a 24-hour clock (`%X`).
242+
///
243+
/// See the [`format::strftime` module](crate::format::strftime) for all supported formatting
244+
/// specifiers.
245+
///
246+
/// [`DateTime::format_localized_with_items`]: crate::DateTime::format_localized_with_items
247+
///
248+
/// # Errors
249+
///
250+
/// While iterating [`Item::Error`] will be returned if the format string contains an invalid
251+
/// or unrecognized formatting specifier.
252+
///
253+
/// # Example
254+
///
255+
#[cfg_attr(not(any(feature = "alloc", feature = "std")), doc = "```ignore")]
256+
#[cfg_attr(any(feature = "alloc", feature = "std"), doc = "```rust")]
257+
/// use chrono::format::{Locale, StrftimeItems};
258+
/// use chrono::{FixedOffset, TimeZone};
259+
///
260+
/// let dt = FixedOffset::east_opt(9 * 60 * 60)
261+
/// .unwrap()
262+
/// .with_ymd_and_hms(2023, 7, 11, 0, 34, 59)
263+
/// .unwrap();
264+
///
265+
/// // Note: you usually want to combine `StrftimeItems::new_with_locale` with other
266+
/// // locale-aware methods such as `DateTime::format_localized_with_items`.
267+
/// // We use the regular `format_with_items` to show only how the formatting changes.
268+
///
269+
/// let fmtr = dt.format_with_items(StrftimeItems::new_with_locale("%x", Locale::en_US));
270+
/// assert_eq!(fmtr.to_string(), "07/11/2023");
271+
/// let fmtr = dt.format_with_items(StrftimeItems::new_with_locale("%x", Locale::ko_KR));
272+
/// assert_eq!(fmtr.to_string(), "2023년 07월 11일");
273+
/// let fmtr = dt.format_with_items(StrftimeItems::new_with_locale("%x", Locale::ja_JP));
274+
/// assert_eq!(fmtr.to_string(), "2023年07月11日");
275+
/// ```
232276
#[cfg(feature = "unstable-locales")]
233277
#[must_use]
234278
pub const fn new_with_locale(s: &'a str, locale: Locale) -> StrftimeItems<'a> {

0 commit comments

Comments
 (0)