Skip to main content

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