Module format

Source
Expand description

Formatting (and parsing) utilities for date and time.

This module provides the common types and routines to implement, for example, DateTime::format or DateTime::parse_from_str methods. For most cases you should use these high-level interfaces.

Internally the formatting and parsing shares the same abstract formatting items, which are just an Iterator of the Item type. They are generated from more readable format strings; currently Chrono supports a built-in syntax closely resembling Cโ€™s strftime format. The available options can be found here.

ยงExample

use chrono::{NaiveDateTime, TimeZone, Utc};

let date_time = Utc.with_ymd_and_hms(2020, 11, 10, 0, 1, 32).unwrap();

let formatted = format!("{}", date_time.format("%Y-%m-%d %H:%M:%S"));
assert_eq!(formatted, "2020-11-10 00:01:32");

let parsed = NaiveDateTime::parse_from_str(&formatted, "%Y-%m-%d %H:%M:%S")?.and_utc();
assert_eq!(parsed, date_time);

Re-exportsยง

pub use strftime::StrftimeItems;

Modulesยง

formatting ๐Ÿ”’
Date and time formatting routines.
locales ๐Ÿ”’
parse ๐Ÿ”’
Date and time parsing routines.
parsed ๐Ÿ”’
A collection of parsed date and time items. They can be constructed incrementally while being checked for consistency.
scan ๐Ÿ”’
Various scanning routines for the parser.
strftime
strftime/strptime-inspired date and time formatting syntax.

Structsยง

DelayedFormat
A temporary object which can be used as an argument to format! or others. This is normally constructed via format methods of each date and time type.
InternalFixed
An opaque type representing fixed-format item types for internal uses only.
InternalNumeric
An opaque type representing numeric item types for internal uses only.
OffsetFormat
Type for specifying the format of UTC offsets.
ParseError
An error from the parse function.
Parsed
A type to hold parsed fields of date and time that can check all fields are consistent.

Enumsยง

Colons
The separator between hours and minutes in an offset.
Fixed
Fixed-format item types.
InternalInternal ๐Ÿ”’
Item
A single formatting item. This is used for both formatting and parsing.
Numeric
Numeric item types. They have associated formatting width (FW) and parsing width (PW).
OffsetPrecision
The precision of an offset from UTC formatting item.
Pad
Padding characters for numeric items.
ParseErrorKind
The category of parse error
SecondsFormat
Specific formatting options for seconds. This may be extended in the future, so exhaustive matching in external code is not recommended.
Void ๐Ÿ”’
An uninhabited type used for InternalNumeric and InternalFixed below.

Constantsยง

BAD_FORMAT ๐Ÿ”’
IMPOSSIBLE ๐Ÿ”’
INVALID ๐Ÿ”’
NOT_ENOUGH ๐Ÿ”’
OUT_OF_RANGE ๐Ÿ”’
TOO_LONG ๐Ÿ”’
TOO_SHORT ๐Ÿ”’

Functionsยง

fixed ๐Ÿ”’
formatDeprecated
Tries to format given arguments with given formatting items. Internally used by DelayedFormat.
format_itemDeprecated
Formats single formatting item
internal_fixed ๐Ÿ”’
num ๐Ÿ”’
num0 ๐Ÿ”’
nums ๐Ÿ”’
parse
Tries to parse given string into parsed with given formatting items. Returns Ok when the entire string has been parsed (otherwise parsed should not be used). There should be no trailing string after parsing; use a stray Item::Space to trim whitespaces.
parse_and_remainder
Tries to parse given string into parsed with given formatting items. Returns Ok with a slice of the unparsed remainder.

Type Aliasesยง

ParseResult
Same as Result<T, ParseError>.