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