Chrono aims to provide all functionality needed to perform correct date and time operations in the Gregorian calendar:
DateTime
type is timezone-aware by default, with separate timezone-naive types also available.Option
or MappedLocalTime
.Local
timezone works with the operating system's current timezone.Chrono-TZ
or tzfile
for full timezone support.Chrono supports various runtime environments and operating systems, with several features that can be enabled or disabled.
alloc
: Enables features that depend on memory allocation (primarily string formatting).std
: Enables features that depend on the standard library. This is a superset of alloc
, adding interoperability with standard library types and traits.clock
: Enables reading the local timezone (Local
). This is a superset of now
.now
: Enables reading the system time (now
).wasmbind
: Provides an interface to the JS Date API for wasm32 targets.serde
: Enables serialization/deserialization via serde.rkyv
: Deprecated; use the rkyv-*
features instead.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 using the Arbitrary crate.unstable-locales
: Enables localization. This adds various methods with the _localized
suffix.Chrono provides the TimeDelta
type to represent the magnitude of a time span. This is an "exact" duration represented in seconds and nanoseconds, not expressing "nominal" components like 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 signed rather than unsigned.
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 local dates are converted to and from UTC dates. There are three well-known TimeZone
implementations:
Utc
specifies the UTC timezone. It is the most efficient.Local
specifies the system's local timezone.FixedOffset
specifies an arbitrary fixed 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 between each other using the DateTime::with_timezone
method.
You can obtain the current date and time in the UTC timezone (Utc::now()
) or the local timezone (Local::now()
).
Additionally, you can create your own dates and times:
Formatting is done via the format
method, which uses a format equivalent to the familiar strftime format.
The default to_string
method and the {:?}
specifier also provide reasonable representations. Chrono also provides to_rfc2822
and to_rfc3339
methods for common formats.
Chrono now also offers date formatting in nearly any language without requiring additional C libraries. This feature is available under the unstable-locales
feature:
Parsing can be done in two ways:
FromStr
trait (and the parse
method on strings) can be used to parse DateTime<FixedOffset>
, DateTime<Utc>
, and DateTime<Local>
values.DateTime::parse_from_str
parses a date and time with an offset and returns a DateTime<FixedOffset>
.Use DateTime::from_timestamp(seconds, nanoseconds)
to construct a DateTime<Utc>
from a UNIX timestamp.
Use DateTime.timestamp
to get the timestamp (in seconds) from a DateTime
. Additionally, you can use DateTime.timestamp_subsec_nanos
to get the additional nanoseconds.