Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

:timing
:sccache 1

Comments

  1. 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.690945736s
let start = SystemTime::now();
start.duration_since(UNIX_EPOCH).unwrap()
1638324623.948540298s
let start = SystemTime::now();
start.duration_since(UNIX_EPOCH).unwrap().as_secs()
1638324793
let start = SystemTime::now();
start.duration_since(UNIX_EPOCH).unwrap().as_millis()
1638324649987
let start = SystemTime::now();
start.duration_since(UNIX_EPOCH).unwrap().as_nanos()
1638325403263831531

Format 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:00

The Rust Crate time