@@ -228,7 +228,51 @@ impl<'a> StrftimeItems<'a> {
228
228
}
229
229
}
230
230
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
+ /// ```
232
276
#[ cfg( feature = "unstable-locales" ) ]
233
277
#[ must_use]
234
278
pub const fn new_with_locale ( s : & ' a str , locale : Locale ) -> StrftimeItems < ' a > {
0 commit comments