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