Elapsed time



examples/chrono/chrono-elapsed-time/Cargo.toml
[package]
name = "chrono-elapsed-time"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
chrono = "0.4.26"

start:             2023-08-18 10:54:06.606602231 UTC
start timestamp:   1692356046
start timestamp:   1692356046606602

elapsed as_micros: 10158
elapsed as_millis: 10

end:               2023-08-18 10:54:06.616858288 UTC
end timestamp:     1692356046
end timestamp:     1692356046616858

Elapsed (sec):     0
Elapsed (millis):  10
Elapsed (micros):  10256


examples/chrono/chrono-elapsed-time/src/main.rs
use chrono::{DateTime, Utc};
use std::{thread, time};

fn main() {
    let start: DateTime<Utc> = Utc::now();
    println!("start:             {}", start);
    println!("start timestamp:   {}", start.timestamp());
    println!("start timestamp:   {}", start.timestamp_micros());
    println!();

    let ten_millis = time::Duration::from_millis(10);
    let now = time::Instant::now();

    thread::sleep(ten_millis);
    println!("elapsed as_micros: {}", now.elapsed().as_micros());
    println!("elapsed as_millis: {}", now.elapsed().as_millis());

    let end: DateTime<Utc> = Utc::now();
    println!();
    println!("end:               {}", end);
    println!("end timestamp:     {}", end.timestamp());
    println!("end timestamp:     {}", end.timestamp_micros());

    println!();
    println!("Elapsed (sec):     {}", end.timestamp() - start.timestamp());
    println!(
        "Elapsed (millis):  {}",
        end.timestamp_millis() - start.timestamp_millis()
    );
    println!(
        "Elapsed (micros):  {}",
        end.timestamp_micros() - start.timestamp_micros()
    );
}