Module chrono
Date and time library.
chrono provides comprehensive date and time functionality for Rust.
It supports timezone-aware and timezone-naive date and time types,
parsing and formatting with standard formats like RFC 3339 and ISO 8601,
and arithmetic operations on dates and times.
Note that while widely used in the Rust ecosystem its API is unwieldy.
Most users that just need to work with dates should prefer jiff.
The library centers around several core types:
NaiveDate for dates without timezones,
NaiveTime for times without timezones,
NaiveDateTime for combined date-time without timezones,
and DateTime<Tz> for timezone-aware date-times.
Chrono uses the TimeZone trait to handle timezone conversions,
with built-in support for UTC and local time,
plus optional support for the IANA timezone database through the chrono-tz crate.
The design emphasizes correctness and type safety, making invalid states unrepresentable where possible. All types are designed to be space-efficient and performant.
Examples
Working with dates and times:
use ;
// Current time in UTC
let now_utc: = now;
println!;
// Current local time
let now_local: = now;
println!;
// Create a specific date
let date = from_ymd_opt.unwrap;
let datetime = date.and_hms_opt.unwrap;
println!;
// Add duration
let future = now_utc + days;
println!;
Parsing and formatting dates:
use ;
// Parse RFC 3339 / ISO 8601
let parsed = "2024-03-15T14:30:00Z"..unwrap;
println!;
// Custom formatting
let formatted = parsed.format;
println!;
// Parse custom format
let naive = parse_from_str.unwrap;
println!;
Modules
- format Formatting (and parsing) utilities for date and time.
- naive Date and time types unconcerned with timezones.
- offset The time zone, which calculates offsets from the local time to UTC.
-
prelude
A convenience module appropriate for glob imports (
use chrono::prelude::*;). -
round
Functionality for rounding or truncating a
DateTimeby aTimeDelta.
Structs
- OutOfRange Out of range error type used in various converting APIs