Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
:timing
:sccache 1Comments¶
There are a few 3rd-party Rust crates time, quanta and chrono for handling time/date. The Rust crate time, is recommended. For detailed discussions, please refer to No Time for Chrono .
use std::time::{SystemTime, UNIX_EPOCH};SystemTime::now()SystemTime { tv_sec: 1638324570, tv_nsec: 811168245 }let start = SystemTime::now();
start.duration_since(UNIX_EPOCH).expect("Time went backwards")1638324597.690945736slet start = SystemTime::now();
start.duration_since(UNIX_EPOCH).unwrap()1638324623.948540298slet start = SystemTime::now();
start.duration_since(UNIX_EPOCH).unwrap().as_secs()1638324793let start = SystemTime::now();
start.duration_since(UNIX_EPOCH).unwrap().as_millis()1638324649987let start = SystemTime::now();
start.duration_since(UNIX_EPOCH).unwrap().as_nanos()1638325403263831531Format SystemTime (as String)¶
SystemTime itself does not support formatting as string. However, it can be done indirectly by casting SystemTime to chrono::Datetime first.
:dep chrono = "0.4.19"use std::time::SystemTime;
use chrono::offset::Local;
use chrono::DateTime;let datetime: DateTime<Local> = SystemTime::now().into();
println!("{}", datetime.format("%Y-%m-%d %H:%M:%S.%f"));2021-11-30 18:15:06.485079503
Local::now()2021-11-30T18:15:12.199268942-08:00The Rust Crate time¶