liveBook · Manning

Chrono

As explained in Learn Rust in a Month of Lunches

chrono is a Rust date-and-time library providing temporal types, arithmetic, formatting, and time-zone support.

chrono is a Rust crate for working with dates, times, and time zones. Rust’s std::time is useful for measuring elapsed time with Instant and Duration, but an Instant does not tell you today’s date. Reach for chrono when you need calendar dates or time-zone-aware timestamps. section 17.2

Start with the simpler types: NaiveDate is a date, NaiveTime is a time of day, and NaiveDateTime combines them. “Naive” means there is no time-zone information attached. The _opt constructors return an Option, so creating a date or time can fail rather than silently producing an invalid value. section 17.3 section 17.3.1

use chrono::naive::{NaiveDate, NaiveTime};
 
fn main() {
    println!("{:?}", NaiveDate::from_ymd_opt(2023, 3, 25));
    println!("{:?}", NaiveTime::from_hms_opt(12, 5, 30));
    println!("{:?}", NaiveDate::from_ymd_opt(2023, 3, 25).unwrap().and_hms_opt(12, 5, 30));
}

Try the three constructors in order: create a date, create a time, then add hours, minutes, and seconds to a date. The printed values are wrapped in Some because these calls return Options. section 17.3

The _opt suffix is worth noticing. An older constructor, from_ymd(), was deprecated because it could panic for an invalid date; it called from_ymd_opt() and then used expect(). When choosing a crate method, check its documentation or source for possible panics. The names may change as chrono evolves. section 17.3.1

From a timestamp to a local clock time

A practical path starts with seconds since the Unix epoch from SystemTime. Convert those seconds to a NaiveDateTime, attach UTC to make a DateTime<Utc>, and use a FixedOffset for a clock time three hours ahead of UTC. The example prints 02:23:19 UTC and 05:23:19 +03:00; removing the time-zone information leaves a naive 05:23:19. section 17.3.2

The same pattern can serve application data: parse an event’s UTC timestamp and produce an event with a +09:00 timestamp for Korea/Japan, keeping its data. Small tests check the conversion. Expect to consult chrono’s documentation when moving between its date-and-time types. section 17.3.2

How Learn Rust in a Month of Lunches covers it

How deep it goes
Works through naive constructors and timestamp conversions, while directing readers to documentation for further type conversions.
Examples
Uses date-and-time constructors, seconds since the Unix epoch, and a tested UTC-to-Korea/Japan event conversion.
What it assumes
Builds from std::time examples and explains naive types and the _opt suffix before using time-zone-aware types.
What you can do after
Apply it via chrono to construct dates and convert UTC timestamps into fixed-offset event timestamps.
How it connects
Connects Option and panic handling to NaiveDate, SystemTime, UTC, and FixedOffset.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.