Parsing and Displaying

Examine the date and time

chrono cat-date-and-time

Gets the current UTC chrono::DateTime⮳ and its hour/minute/second via chrono::Timelike⮳ and its year/month/day/weekday via chrono::Datelike

use chrono::Datelike;
use chrono::Timelike;
use chrono::Utc;

fn main() {
    let now = Utc::now();

    let (is_pm, hour) = now.hour12();
    println!(
        "The current UTC time is {:02}:{:02}:{:02} {}",
        hour,
        now.minute(),
        now.second(),
        if is_pm { "PM" } else { "AM" }
    );
    println!(
        "And there have been {} seconds since midnight",
        now.num_seconds_from_midnight()
    );

    let (is_common_era, year) = now.year_ce();
    println!(
        "The current UTC date is {}-{:02}-{:02} {:?} ({})",
        year,
        now.month(),
        now.day(),
        now.weekday(),
        if is_common_era { "CE" } else { "BCE" }
    );
    println!(
        "And the Common Era began {} days ago",
        now.num_days_from_ce()
    );
}

Convert date to UNIX timestamp and vice versa

chrono cat-date-and-time

Converts a date given by chrono::naive::NaiveDate::from_ymd⮳ and chrono::naive::NaiveTime::from_hms⮳ to UNIX time stamp⮳ using chrono::naive::NaiveDateTime::timestamp

Then it calculates what was the date after one billion seconds since January 1, 1970 0:00:00 UTC, using chrono::naive::NaiveDateTime::from_timestamp⮳.

use chrono::DateTime;
use chrono::NaiveDate;
use chrono::NaiveDateTime;

fn main() {
    let date_time: NaiveDateTime = NaiveDate::from_ymd_opt(2017, 11, 12)
        .unwrap()
        .and_hms_opt(17, 33, 44)
        .unwrap();
    println!(
        "Number of seconds between 1970-01-01 00:00:00 and {} is
    {}.",
        date_time,
        date_time.and_utc().timestamp()
    );

    let date_time_after_a_billion_seconds =
        DateTime::from_timestamp(1_000_000_000, 0).unwrap();
    println!(
        "Date after a billion seconds since 1970-01-01 00:00:00 was
    {}.",
        date_time_after_a_billion_seconds
    );
}

Display formatted date and time

chrono cat-date-and-time

Gets and displays the current time in UTC using chrono::offset::Utc::now⮳.

Formats the current time in the well-known RFC 2822 format⮳ using chrono::DateTime::to_rfc2822⮳ and RFC 3339⮳ using chrono::DateTime::to_rfc3339⮳ and in a custom format using chrono::DateTime::format⮳.

use chrono::DateTime;
use chrono::Utc;

fn main() {
    let now: DateTime<Utc> = Utc::now();

    println!("UTC now is: {}", now);
    println!("UTC now in RFC 2822 is: {}", now.to_rfc2822());
    println!("UTC now in RFC 3339 is: {}", now.to_rfc3339());
    println!(
        "UTC now in a custom format is: {}",
        now.format("%a %b %e %T %Y")
    );
}

Parse string into DateTime struct

chrono cat-date-and-time

Parses a chrono::DateTime⮳ struct from strings representing the well-known RFC 2822 format⮳ and RFC 3339 format⮳, and a custom format, using chrono::DateTime::parse_from_rfc2822chrono::DateTime::parse_from_rfc2822⮳ and chrono::DateTime::parse_from_str⮳ respectively.

Escape sequences that are available for the chrono::DateTime::parse_from_str⮳ can be found at chrono::format::strftime⮳. Note that the chrono::DateTime::parse_from_str⮳ requires that such a DateTime struct must be creatable that it uniquely identifies a date and a time. For parsing dates and times without timezones use chrono::naive::NaiveDatechrono::naive::NaiveTime⮳ and chrono::naive::NaiveDateTime⮳.

#![allow(unused)]

fn main() {
}