Time in Rust

use std::time::{Duration, SystemTime, UNIX_EPOCH};
use chrono::{DateTime, Utc, Datelike, Timelike, Weekday};

fn main() {
    let p = |x| println!("{:?}", x);

    // We'll start by getting the current time.
    let now = SystemTime::now();
    p(now);

    // You can build a DateTime struct by providing the
    // year, month, day, etc. Times are always associated
    // with a time zone.
    let then = DateTime::parse_from_rfc3339("2009-11-17T20:34:58.651387237Z").unwrap();
    p(then);

    // You can extract the various components of the time
    // value as expected.
    p(then.year());
    p(then.month());
    p(then.day());
    p(then.hour());
    p(then.minute());
    p(then.second());
    p(then.nanosecond());
    p(then.timezone());

    // The Monday-Sunday Weekday is also available.
    p(then.weekday());

    // These methods compare two times, testing if the
    // first occurs before, after, or at the same time
    // as the second, respectively.
    p(then < now);
    p(then > now);
    p(then == now);

    // The duration method returns a Duration representing
    // the interval between two times.
    let diff = now.duration_since(UNIX_EPOCH).unwrap() - then.timestamp_nanos() as u64;
    p(Duration::from_nanos(diff));

    // We can compute the length of the duration in
    // various units.
    p(Duration::from_nanos(diff).as_hours());
    p(Duration::from_nanos(diff).as_minutes());
    p(Duration::from_nanos(diff).as_secs());
    p(Duration::from_nanos(diff).subsec_nanos());

    // You can use checked_add to advance a time by a given
    // duration, or with a - to move backwards by a duration.
    p(then + chrono::Duration::from_std(Duration::from_nanos(diff)).unwrap());
    p(then - chrono::Duration::from_std(Duration::from_nanos(diff)).unwrap());
}

This Rust code demonstrates various operations with times and durations. Here’s a breakdown of what’s happening:

  1. We start by importing necessary modules from the standard library and the chrono crate.

  2. We get the current time using SystemTime::now().

  3. We create a specific datetime using DateTime::parse_from_rfc3339().

  4. We extract various components of the datetime such as year, month, day, etc.

  5. We compare two times using the standard comparison operators.

  6. We calculate the duration between two times.

  7. We compute the duration in various units like hours, minutes, seconds, and nanoseconds.

  8. Finally, we demonstrate how to add and subtract durations from a datetime.

To run this program, you would typically save it as a .rs file (e.g., time_example.rs) and use the Rust compiler:

$ rustc time_example.rs
$ ./time_example

Or if you’re using Cargo:

$ cargo run

This will compile and run the program, showing output similar to the original example but with Rust-specific time and date representations.

Note that Rust’s standard library provides basic time functionality, but for more advanced features, we’re using the chrono crate, which is a popular choice for date and time operations in Rust.