- chrono
- DateTime
- utc
- now
Chrono now
examples/chrono/chrono-demo/src/main.rs
use chrono::{DateTime, Local, Utc}; fn main() { let utc: DateTime<Utc> = Utc::now(); println!("utc: {}", utc); println!("utc timestamp: {}", utc.timestamp()); println!("utc format: {}", utc.format("%Y-%m-%d")); println!(); let local: DateTime<Local> = Local::now(); println!("{}", local); println!("{}", local.timestamp()); let text = String::from("2023-08-30T12:30:01+0000"); let dt = DateTime::parse_from_str(&text, "%Y-%m-%dT%H:%M:%S%z").unwrap(); println!(); println!("dt: {}", dt); println!("dt format: {}", dt.format("%Y-%m-%d")); println!("dt format: {}", dt.format("%H::%M::%S")); }
examples/chrono/chrono-demo/Cargo.toml
[package] name = "chrono-demo" 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"
utc: 2023-08-31 07:11:59.543230392 UTC utc timestamp: 1693465919 utc format: 2023-08-31 2023-08-31 10:11:59.543294786 +03:00 1693465919 dt: 2023-08-30 12:30:01 +00:00 dt format: 2023-08-30 dt format: 12::30::01