chrono/format/mod.rs
1// This is a part of Chrono.
2// See README.md and LICENSE.txt for details.
3
4//! Formatting (and parsing) utilities for date and time.
5//!
6//! This module provides the common types and routines to implement,
7//! for example, [`DateTime::format`](../struct.DateTime.html#method.format) or
8//! [`DateTime::parse_from_str`](../struct.DateTime.html#method.parse_from_str) methods.
9//! For most cases you should use these high-level interfaces.
10//!
11//! Internally the formatting and parsing shares the same abstract **formatting items**,
12//! which are just an [`Iterator`](https://doc.rust-lang.org/std/iter/trait.Iterator.html) of
13//! the [`Item`](./enum.Item.html) type.
14//! They are generated from more readable **format strings**;
15//! currently Chrono supports a built-in syntax closely resembling
16//! C's `strftime` format. The available options can be found [here](./strftime/index.html).
17//!
18//! # Example
19#![cfg_attr(not(feature = "std"), doc = "```ignore")]
20#![cfg_attr(feature = "std", doc = "```rust")]
21//! use chrono::{NaiveDateTime, TimeZone, Utc};
22//!
23//! let date_time = Utc.with_ymd_and_hms(2020, 11, 10, 0, 1, 32).unwrap();
24//!
25//! let formatted = format!("{}", date_time.format("%Y-%m-%d %H:%M:%S"));
26//! assert_eq!(formatted, "2020-11-10 00:01:32");
27//!
28//! let parsed = NaiveDateTime::parse_from_str(&formatted, "%Y-%m-%d %H:%M:%S")?.and_utc();
29//! assert_eq!(parsed, date_time);
30//! # Ok::<(), chrono::ParseError>(())
31//! ```
32
33#[cfg(feature = "alloc")]
34use alloc::boxed::Box;
35use core::fmt;
36use core::str::FromStr;
37#[cfg(feature = "std")]
38use std::error::Error;
39
40use crate::{Month, ParseMonthError, ParseWeekdayError, Weekday};
41
42mod formatting;
43mod parsed;
44
45// due to the size of parsing routines, they are in separate modules.
46mod parse;
47pub(crate) mod scan;
48
49pub mod strftime;
50
51#[cfg(any(feature = "alloc", feature = "std"))]
52pub(crate) mod locales;
53
54pub(crate) use formatting::write_hundreds;
55#[cfg(any(feature = "alloc", feature = "std"))]
56pub(crate) use formatting::write_rfc2822;
57#[cfg(any(
58 feature = "alloc",
59 feature = "std",
60 feature = "serde",
61 feature = "rustc-serialize"
62))]
63pub(crate) use formatting::write_rfc3339;
64#[cfg(any(feature = "alloc", feature = "std"))]
65pub use formatting::{format, format_item, DelayedFormat};
66#[cfg(feature = "unstable-locales")]
67pub use formatting::{format_item_localized, format_localized};
68#[cfg(all(feature = "unstable-locales", any(feature = "alloc", feature = "std")))]
69pub use locales::Locale;
70#[cfg(all(not(feature = "unstable-locales"), any(feature = "alloc", feature = "std")))]
71pub(crate) use locales::Locale;
72pub(crate) use parse::parse_rfc3339;
73pub use parse::{parse, parse_and_remainder};
74pub use parsed::Parsed;
75pub use strftime::StrftimeItems;
76
77/// An uninhabited type used for `InternalNumeric` and `InternalFixed` below.
78#[derive(Clone, PartialEq, Eq, Hash)]
79enum Void {}
80
81/// Padding characters for numeric items.
82#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
83pub enum Pad {
84 /// No padding.
85 None,
86 /// Zero (`0`) padding.
87 Zero,
88 /// Space padding.
89 Space,
90}
91
92/// Numeric item types.
93/// They have associated formatting width (FW) and parsing width (PW).
94///
95/// The **formatting width** is the minimal width to be formatted.
96/// If the number is too short, and the padding is not [`Pad::None`](./enum.Pad.html#variant.None),
97/// then it is left-padded.
98/// If the number is too long or (in some cases) negative, it is printed as is.
99///
100/// The **parsing width** is the maximal width to be scanned.
101/// The parser only tries to consume from one to given number of digits (greedily).
102/// It also trims the preceding whitespace if any.
103/// It cannot parse the negative number, so some date and time cannot be formatted then
104/// parsed with the same formatting items.
105#[derive(Clone, PartialEq, Eq, Debug, Hash)]
106pub enum Numeric {
107 /// Full Gregorian year (FW=4, PW=∞).
108 /// May accept years before 1 BCE or after 9999 CE, given an initial sign (+/-).
109 Year,
110 /// Gregorian year divided by 100 (century number; FW=PW=2). Implies the non-negative year.
111 YearDiv100,
112 /// Gregorian year modulo 100 (FW=PW=2). Cannot be negative.
113 YearMod100,
114 /// Year in the ISO week date (FW=4, PW=∞).
115 /// May accept years before 1 BCE or after 9999 CE, given an initial sign.
116 IsoYear,
117 /// Year in the ISO week date, divided by 100 (FW=PW=2). Implies the non-negative year.
118 IsoYearDiv100,
119 /// Year in the ISO week date, modulo 100 (FW=PW=2). Cannot be negative.
120 IsoYearMod100,
121 /// Month (FW=PW=2).
122 Month,
123 /// Day of the month (FW=PW=2).
124 Day,
125 /// Week number, where the week 1 starts at the first Sunday of January (FW=PW=2).
126 WeekFromSun,
127 /// Week number, where the week 1 starts at the first Monday of January (FW=PW=2).
128 WeekFromMon,
129 /// Week number in the ISO week date (FW=PW=2).
130 IsoWeek,
131 /// Day of the week, where Sunday = 0 and Saturday = 6 (FW=PW=1).
132 NumDaysFromSun,
133 /// Day of the week, where Monday = 1 and Sunday = 7 (FW=PW=1).
134 WeekdayFromMon,
135 /// Day of the year (FW=PW=3).
136 Ordinal,
137 /// Hour number in the 24-hour clocks (FW=PW=2).
138 Hour,
139 /// Hour number in the 12-hour clocks (FW=PW=2).
140 Hour12,
141 /// The number of minutes since the last whole hour (FW=PW=2).
142 Minute,
143 /// The number of seconds since the last whole minute (FW=PW=2).
144 Second,
145 /// The number of nanoseconds since the last whole second (FW=PW=9).
146 /// Note that this is *not* left-aligned;
147 /// see also [`Fixed::Nanosecond`](./enum.Fixed.html#variant.Nanosecond).
148 Nanosecond,
149 /// The number of non-leap seconds since the midnight UTC on January 1, 1970 (FW=1, PW=∞).
150 /// For formatting, it assumes UTC upon the absence of time zone offset.
151 Timestamp,
152
153 /// Internal uses only.
154 ///
155 /// This item exists so that one can add additional internal-only formatting
156 /// without breaking major compatibility (as enum variants cannot be selectively private).
157 Internal(InternalNumeric),
158}
159
160/// An opaque type representing numeric item types for internal uses only.
161#[derive(Clone, Eq, Hash, PartialEq)]
162pub struct InternalNumeric {
163 _dummy: Void,
164}
165
166impl fmt::Debug for InternalNumeric {
167 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
168 write!(f, "<InternalNumeric>")
169 }
170}
171
172/// Fixed-format item types.
173///
174/// They have their own rules of formatting and parsing.
175/// Otherwise noted, they print in the specified cases but parse case-insensitively.
176#[derive(Clone, PartialEq, Eq, Debug, Hash)]
177pub enum Fixed {
178 /// Abbreviated month names.
179 ///
180 /// Prints a three-letter-long name in the title case, reads the same name in any case.
181 ShortMonthName,
182 /// Full month names.
183 ///
184 /// Prints a full name in the title case, reads either a short or full name in any case.
185 LongMonthName,
186 /// Abbreviated day of the week names.
187 ///
188 /// Prints a three-letter-long name in the title case, reads the same name in any case.
189 ShortWeekdayName,
190 /// Full day of the week names.
191 ///
192 /// Prints a full name in the title case, reads either a short or full name in any case.
193 LongWeekdayName,
194 /// AM/PM.
195 ///
196 /// Prints in lower case, reads in any case.
197 LowerAmPm,
198 /// AM/PM.
199 ///
200 /// Prints in upper case, reads in any case.
201 UpperAmPm,
202 /// An optional dot plus one or more digits for left-aligned nanoseconds.
203 /// May print nothing, 3, 6 or 9 digits according to the available accuracy.
204 /// See also [`Numeric::Nanosecond`](./enum.Numeric.html#variant.Nanosecond).
205 Nanosecond,
206 /// Same as [`Nanosecond`](#variant.Nanosecond) but the accuracy is fixed to 3.
207 Nanosecond3,
208 /// Same as [`Nanosecond`](#variant.Nanosecond) but the accuracy is fixed to 6.
209 Nanosecond6,
210 /// Same as [`Nanosecond`](#variant.Nanosecond) but the accuracy is fixed to 9.
211 Nanosecond9,
212 /// Timezone name.
213 ///
214 /// It does not support parsing, its use in the parser is an immediate failure.
215 TimezoneName,
216 /// Offset from the local time to UTC (`+09:00` or `-04:00` or `+00:00`).
217 ///
218 /// In the parser, the colon can be omitted and/or surrounded with any amount of whitespace.
219 /// The offset is limited from `-24:00` to `+24:00`,
220 /// which is the same as [`FixedOffset`](../offset/struct.FixedOffset.html)'s range.
221 TimezoneOffsetColon,
222 /// Offset from the local time to UTC with seconds (`+09:00:00` or `-04:00:00` or `+00:00:00`).
223 ///
224 /// In the parser, the colon can be omitted and/or surrounded with any amount of whitespace.
225 /// The offset is limited from `-24:00:00` to `+24:00:00`,
226 /// which is the same as [`FixedOffset`](../offset/struct.FixedOffset.html)'s range.
227 TimezoneOffsetDoubleColon,
228 /// Offset from the local time to UTC without minutes (`+09` or `-04` or `+00`).
229 ///
230 /// In the parser, the colon can be omitted and/or surrounded with any amount of whitespace.
231 /// The offset is limited from `-24` to `+24`,
232 /// which is the same as [`FixedOffset`](../offset/struct.FixedOffset.html)'s range.
233 TimezoneOffsetTripleColon,
234 /// Offset from the local time to UTC (`+09:00` or `-04:00` or `Z`).
235 ///
236 /// In the parser, the colon can be omitted and/or surrounded with any amount of whitespace,
237 /// and `Z` can be either in upper case or in lower case.
238 /// The offset is limited from `-24:00` to `+24:00`,
239 /// which is the same as [`FixedOffset`](../offset/struct.FixedOffset.html)'s range.
240 TimezoneOffsetColonZ,
241 /// Same as [`TimezoneOffsetColon`](#variant.TimezoneOffsetColon) but prints no colon.
242 /// Parsing allows an optional colon.
243 TimezoneOffset,
244 /// Same as [`TimezoneOffsetColonZ`](#variant.TimezoneOffsetColonZ) but prints no colon.
245 /// Parsing allows an optional colon.
246 TimezoneOffsetZ,
247 /// RFC 2822 date and time syntax. Commonly used for email and MIME date and time.
248 RFC2822,
249 /// RFC 3339 & ISO 8601 date and time syntax.
250 RFC3339,
251
252 /// Internal uses only.
253 ///
254 /// This item exists so that one can add additional internal-only formatting
255 /// without breaking major compatibility (as enum variants cannot be selectively private).
256 Internal(InternalFixed),
257}
258
259/// An opaque type representing fixed-format item types for internal uses only.
260#[derive(Debug, Clone, PartialEq, Eq, Hash)]
261pub struct InternalFixed {
262 val: InternalInternal,
263}
264
265#[derive(Debug, Clone, PartialEq, Eq, Hash)]
266enum InternalInternal {
267 /// Same as [`TimezoneOffsetColonZ`](#variant.TimezoneOffsetColonZ), but
268 /// allows missing minutes (per [ISO 8601][iso8601]).
269 ///
270 /// # Panics
271 ///
272 /// If you try to use this for printing.
273 ///
274 /// [iso8601]: https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC
275 TimezoneOffsetPermissive,
276 /// Same as [`Nanosecond`](#variant.Nanosecond) but the accuracy is fixed to 3 and there is no leading dot.
277 Nanosecond3NoDot,
278 /// Same as [`Nanosecond`](#variant.Nanosecond) but the accuracy is fixed to 6 and there is no leading dot.
279 Nanosecond6NoDot,
280 /// Same as [`Nanosecond`](#variant.Nanosecond) but the accuracy is fixed to 9 and there is no leading dot.
281 Nanosecond9NoDot,
282}
283
284/// Type for specifying the format of UTC offsets.
285#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
286pub struct OffsetFormat {
287 /// See `OffsetPrecision`.
288 pub precision: OffsetPrecision,
289 /// Separator between hours, minutes and seconds.
290 pub colons: Colons,
291 /// Represent `+00:00` as `Z`.
292 pub allow_zulu: bool,
293 /// Pad the hour value to two digits.
294 pub padding: Pad,
295}
296
297/// The precision of an offset from UTC formatting item.
298#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
299pub enum OffsetPrecision {
300 /// Format offset from UTC as only hours. Not recommended, it is not uncommon for timezones to
301 /// have an offset of 30 minutes, 15 minutes, etc.
302 /// Any minutes and seconds get truncated.
303 Hours,
304 /// Format offset from UTC as hours and minutes.
305 /// Any seconds will be rounded to the nearest minute.
306 Minutes,
307 /// Format offset from UTC as hours, minutes and seconds.
308 Seconds,
309 /// Format offset from UTC as hours, and optionally with minutes.
310 /// Any seconds will be rounded to the nearest minute.
311 OptionalMinutes,
312 /// Format offset from UTC as hours and minutes, and optionally seconds.
313 OptionalSeconds,
314 /// Format offset from UTC as hours and optionally minutes and seconds.
315 OptionalMinutesAndSeconds,
316}
317
318/// The separator between hours and minutes in an offset.
319#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
320pub enum Colons {
321 /// No separator
322 None,
323 /// Colon (`:`) as separator
324 Colon,
325 /// No separator when formatting, colon allowed when parsing.
326 Maybe,
327}
328
329/// A single formatting item. This is used for both formatting and parsing.
330#[derive(Clone, PartialEq, Eq, Debug, Hash)]
331pub enum Item<'a> {
332 /// A literally printed and parsed text.
333 Literal(&'a str),
334 /// Same as `Literal` but with the string owned by the item.
335 #[cfg(any(feature = "alloc", feature = "std"))]
336 #[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
337 OwnedLiteral(Box<str>),
338 /// Whitespace. Prints literally but reads zero or more whitespace.
339 Space(&'a str),
340 /// Same as `Space` but with the string owned by the item.
341 #[cfg(any(feature = "alloc", feature = "std"))]
342 #[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
343 OwnedSpace(Box<str>),
344 /// Numeric item. Can be optionally padded to the maximal length (if any) when formatting;
345 /// the parser simply ignores any padded whitespace and zeroes.
346 Numeric(Numeric, Pad),
347 /// Fixed-format item.
348 Fixed(Fixed),
349 /// Issues a formatting error. Used to signal an invalid format string.
350 Error,
351}
352
353const fn num(numeric: Numeric) -> Item<'static> {
354 Item::Numeric(numeric, Pad::None)
355}
356
357const fn num0(numeric: Numeric) -> Item<'static> {
358 Item::Numeric(numeric, Pad::Zero)
359}
360
361const fn nums(numeric: Numeric) -> Item<'static> {
362 Item::Numeric(numeric, Pad::Space)
363}
364
365const fn fixed(fixed: Fixed) -> Item<'static> {
366 Item::Fixed(fixed)
367}
368
369const fn internal_fixed(val: InternalInternal) -> Item<'static> {
370 Item::Fixed(Fixed::Internal(InternalFixed { val }))
371}
372
373/// An error from the `parse` function.
374#[derive(Debug, Clone, PartialEq, Eq, Copy, Hash)]
375pub struct ParseError(ParseErrorKind);
376
377impl ParseError {
378 /// The category of parse error
379 pub const fn kind(&self) -> ParseErrorKind {
380 self.0
381 }
382}
383
384/// The category of parse error
385#[allow(clippy::manual_non_exhaustive)]
386#[derive(Debug, Clone, PartialEq, Eq, Copy, Hash)]
387pub enum ParseErrorKind {
388 /// Given field is out of permitted range.
389 OutOfRange,
390
391 /// There is no possible date and time value with given set of fields.
392 ///
393 /// This does not include the out-of-range conditions, which are trivially invalid.
394 /// It includes the case that there are one or more fields that are inconsistent to each other.
395 Impossible,
396
397 /// Given set of fields is not enough to make a requested date and time value.
398 ///
399 /// Note that there *may* be a case that given fields constrain the possible values so much
400 /// that there is a unique possible value. Chrono only tries to be correct for
401 /// most useful sets of fields however, as such constraint solving can be expensive.
402 NotEnough,
403
404 /// The input string has some invalid character sequence for given formatting items.
405 Invalid,
406
407 /// The input string has been prematurely ended.
408 TooShort,
409
410 /// All formatting items have been read but there is a remaining input.
411 TooLong,
412
413 /// There was an error on the formatting string, or there were non-supported formating items.
414 BadFormat,
415
416 // TODO: Change this to `#[non_exhaustive]` (on the enum) with the next breaking release.
417 #[doc(hidden)]
418 __Nonexhaustive,
419}
420
421/// Same as `Result<T, ParseError>`.
422pub type ParseResult<T> = Result<T, ParseError>;
423
424impl fmt::Display for ParseError {
425 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
426 match self.0 {
427 ParseErrorKind::OutOfRange => write!(f, "input is out of range"),
428 ParseErrorKind::Impossible => write!(f, "no possible date and time matching input"),
429 ParseErrorKind::NotEnough => write!(f, "input is not enough for unique date and time"),
430 ParseErrorKind::Invalid => write!(f, "input contains invalid characters"),
431 ParseErrorKind::TooShort => write!(f, "premature end of input"),
432 ParseErrorKind::TooLong => write!(f, "trailing input"),
433 ParseErrorKind::BadFormat => write!(f, "bad or unsupported format string"),
434 _ => unreachable!(),
435 }
436 }
437}
438
439#[cfg(feature = "std")]
440#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
441impl Error for ParseError {
442 #[allow(deprecated)]
443 fn description(&self) -> &str {
444 "parser error, see to_string() for details"
445 }
446}
447
448// to be used in this module and submodules
449pub(crate) const OUT_OF_RANGE: ParseError = ParseError(ParseErrorKind::OutOfRange);
450const IMPOSSIBLE: ParseError = ParseError(ParseErrorKind::Impossible);
451const NOT_ENOUGH: ParseError = ParseError(ParseErrorKind::NotEnough);
452const INVALID: ParseError = ParseError(ParseErrorKind::Invalid);
453const TOO_SHORT: ParseError = ParseError(ParseErrorKind::TooShort);
454pub(crate) const TOO_LONG: ParseError = ParseError(ParseErrorKind::TooLong);
455const BAD_FORMAT: ParseError = ParseError(ParseErrorKind::BadFormat);
456
457// this implementation is here only because we need some private code from `scan`
458
459/// Parsing a `str` into a `Weekday` uses the format [`%A`](./format/strftime/index.html).
460///
461/// # Example
462///
463/// ```
464/// use chrono::Weekday;
465///
466/// assert_eq!("Sunday".parse::<Weekday>(), Ok(Weekday::Sun));
467/// assert!("any day".parse::<Weekday>().is_err());
468/// ```
469///
470/// The parsing is case-insensitive.
471///
472/// ```
473/// # use chrono::Weekday;
474/// assert_eq!("mON".parse::<Weekday>(), Ok(Weekday::Mon));
475/// ```
476///
477/// Only the shortest form (e.g. `sun`) and the longest form (e.g. `sunday`) is accepted.
478///
479/// ```
480/// # use chrono::Weekday;
481/// assert!("thurs".parse::<Weekday>().is_err());
482/// ```
483impl FromStr for Weekday {
484 type Err = ParseWeekdayError;
485
486 fn from_str(s: &str) -> Result<Self, Self::Err> {
487 if let Ok(("", w)) = scan::short_or_long_weekday(s) {
488 Ok(w)
489 } else {
490 Err(ParseWeekdayError { _dummy: () })
491 }
492 }
493}
494
495/// Parsing a `str` into a `Month` uses the format [`%B`](./format/strftime/index.html).
496///
497/// # Example
498///
499/// ```
500/// use chrono::Month;
501///
502/// assert_eq!("January".parse::<Month>(), Ok(Month::January));
503/// assert!("any day".parse::<Month>().is_err());
504/// ```
505///
506/// The parsing is case-insensitive.
507///
508/// ```
509/// # use chrono::Month;
510/// assert_eq!("fEbruARy".parse::<Month>(), Ok(Month::February));
511/// ```
512///
513/// Only the shortest form (e.g. `jan`) and the longest form (e.g. `january`) is accepted.
514///
515/// ```
516/// # use chrono::Month;
517/// assert!("septem".parse::<Month>().is_err());
518/// assert!("Augustin".parse::<Month>().is_err());
519/// ```
520impl FromStr for Month {
521 type Err = ParseMonthError;
522
523 fn from_str(s: &str) -> Result<Self, Self::Err> {
524 if let Ok(("", w)) = scan::short_or_long_month0(s) {
525 match w {
526 0 => Ok(Month::January),
527 1 => Ok(Month::February),
528 2 => Ok(Month::March),
529 3 => Ok(Month::April),
530 4 => Ok(Month::May),
531 5 => Ok(Month::June),
532 6 => Ok(Month::July),
533 7 => Ok(Month::August),
534 8 => Ok(Month::September),
535 9 => Ok(Month::October),
536 10 => Ok(Month::November),
537 11 => Ok(Month::December),
538 _ => Err(ParseMonthError { _dummy: () }),
539 }
540 } else {
541 Err(ParseMonthError { _dummy: () })
542 }
543 }
544}