Chrono: Rust Date and Time Library
Chrono aims to provide all functionality needed to do correct operations on dates and times in the proleptic Gregorian calendar:
- The
DateTimetype is timezone-aware by default, with separate timezone-naive types. - Operations that may produce an invalid or ambiguous date and time return
OptionorMappedLocalTime. - Configurable parsing and formatting with a strftime-inspired date and time formatting syntax.
- The
Localtimezone works with the current local timezone of the OS. - Types and operations are implemented to be reasonably efficient.
- To avoid increasing the binary size, Chrono does not ship with timezone data by default. Use the companion crates
Chrono-TZortzfilefor full timezone support.
Features
Chrono supports various runtime environments and operating systems, with several features that can be enabled or disabled.
Default Features:
alloc: Enables features that depend on allocation (primarily string formatting).std: Enables features that depend on the standard library. This is a superset ofalloc, adding interoperation with standard library types and traits.clock: Enables reading the local timezone (Local). This is a superset ofnow.now: Enables reading the system time (now).wasmbind: Provides an interface to the JS Date API for the wasm32 target.
Optional Features:
serde: Enables serialization/deserialization via serde.rkyv: Deprecated, use therkyv-*features.rkyv-16,rkyv-32,rkyv-64: Enables serialization/deserialization via rkyv, using 16-bit, 32-bit, or 64-bit integers respectively.rkyv-validation: Enables rkyv validation support using bytecheck.arbitrary: Constructs arbitrary instances of types with the Arbitrary crate.unstable-locales: Enables localization. This adds various methods with the_localizedsuffix.
Overview
Time Difference/Duration
Chrono provides the TimeDelta type to represent the magnitude of a time span. This is an "exact" duration represented in seconds and nanoseconds, and does not represent "nominal" components such as days or months.
The TimeDelta type was previously named Duration (still available as a type alias). A notable difference from the similar core::time::Duration is that it is a signed value rather than unsigned.
Date and Time
Chrono provides the DateTime type to represent a date and time in a timezone.
DateTime is timezone-aware and must be constructed from a TimeZone object, which defines how the local date is converted to and from UTC. There are three well-known TimeZone implementations:
Utcspecifies the UTC timezone. It is most efficient.Localspecifies the system local timezone.FixedOffsetspecifies an arbitrary fixed offset timezone, such as UTC+09:00 or UTC-10:30.
DateTime values with different TimeZone types are distinct and cannot be mixed, but can be converted to each other using the DateTime::with_timezone method.
You can get the current date and time in the UTC timezone (Utc::now()) or in the local timezone (Local::now()).
Additionally, you can create your own date and time:
Formatting and Parsing
Formatting is done via the format method, which format is equivalent to the familiar strftime format.
The default to_string method and the {:?} specifier also give a reasonable representation. Chrono also provides to_rfc2822 and to_rfc3339 methods for common formats.
Chrono now also provides date formatting in almost any language without additional C libraries. This feature is available under the unstable-locales feature:
Parsing can be done in two ways:
- The standard
FromStrtrait (and theparsemethod on strings) can be used to parseDateTime<FixedOffset>,DateTime<Utc>, andDateTime<Local>values. DateTime::parse_from_strparses a date and time with an offset and returnsDateTime<FixedOffset>.
Conversion to/from Epoch Timestamps
Construct DateTime<Utc> from a UNIX timestamp using DateTime::from_timestamp(seconds, nanoseconds).
Get the timestamp (in seconds) from a DateTime using DateTime.timestamp. Additionally, you can get the additional number of nanoseconds with DateTime.timestamp_subsec_nanos.
Limitations
- Only the proleptic Gregorian calendar is supported (i.e., extended to support dates before the era).
- Date types are limited to approximately +/- 262,000 years from the common era.
- Time types are limited to nanosecond precision.
- Leap seconds can be represented, but Chrono does not fully support them.