Skip to content

Instantly share code, notes, and snippets.

@gembin
Created July 12, 2020 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gembin/64e08199562303117d6eca788b5879ee to your computer and use it in GitHub Desktop.
Save gembin/64e08199562303117d6eca788b5879ee to your computer and use it in GitHub Desktop.
Notes: Chrono - Date and Time for Rust

Notes for chrono

use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Utc};

fn main() {
    // ************************************************
    // RFC2822 = Date + Time + TimeZone
    // ************************************************
    let date_str = "Tue, 1 Jul 2003 10:52:37 +0200";
    let datetime = DateTime::parse_from_rfc2822(date_str).unwrap();
    println!("DateTime RFC 2822: {:?}", datetime);

    // ************************************************
    // RFC3339 = Date + Time + TimeZone
    // ************************************************
    let date_str = "2020-04-12T22:10:57+02:00";
    let datetime = DateTime::parse_from_rfc3339(date_str).unwrap();
    println!("DateTime RFC 3339: {:?}", datetime);
    // convert the string into DateTime<Utc> or other timezone
    let datetime_utc = datetime.with_timezone(&Utc);
    println!("DateTime RFC 3339 UTC: {:?}", datetime_utc);

    // ************************************************
    // Date + Time + Timezone (custom format)
    // ************************************************
    let date_str = "2020-04-12 22:10:57 +02:00";
    let datetime = DateTime::parse_from_str(&date_str, "%Y-%m-%d %H:%M:%S %z").unwrap();
    println!("Custom DateTime: {:?}", datetime);

    // ************************************************
    // Date + Time
    // ************************************************
    // When you do not have a TimeZone you need to use NaiveDateTime. This object does not store a timezone:
    let date_str = "2020-04-12 22:10:57";
    let naive_datetime = NaiveDateTime::parse_from_str(&date_str, "%Y-%m-%d %H:%M:%S").unwrap();
    println!("NaiveDateTime: {:?}", naive_datetime);

    // ************************************************
    // Date
    // ************************************************
    // If we where parsing a date (with no time) we can store it in a NaiveDate. This object does not store time or a timezone:
    let date_str = "2020-04-12";
    let naive_date = NaiveDate::parse_from_str(&date_str, "%Y-%m-%d").unwrap();
    println!("NaiveDate: {:?}", naive_date);

    // ************************************************
    // Time
    // ************************************************
    // If we where parsing a time (with no date) we can store it in a NaiveTime. This object does not store a date or a timezone:
    let time_str = "22:10:32";
    let naive_time = NaiveTime::parse_from_str(&time_str, "%H:%M:%S").unwrap();
    println!("NaiveTime: {:?}", naive_time);

    // ************************************************
    // Add Date, Time and/or Timezone
    // ************************************************
    let date_str = "2020-04-12";
    // From string to a NaiveDate
    let naive_date = NaiveDate::parse_from_str(&date_str, "%Y-%m-%d").unwrap();
    // Add some default time to convert it into a NaiveDateTime
    let naive_datetime: NaiveDateTime = naive_date.and_hms(0, 0, 0);
    // Add a timezone to the object to convert it into a DateTime<UTC>
    let datetime_utc = DateTime::<Utc>::from_utc(naive_datetime, Utc);
    println!("{:?}", datetime_utc);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment