I want to display the current time in a certain format.
I am trying to avoid the time crate since it's flagged as deprecated on its the GitHub repo.
I want to use this exact format time::now().strftime("%Y-%m-%d][%H:%M:%S").unwrap()
using std::time
, but it doesn't seems to have a strftime
.
There are (currently) two now
methods: Instant::now
and SystemTime::now
.
Instant
says:
A measurement of a monotonically increasing clock. Opaque and useful only with Duration
.
SystemTime
says:
A measurement of the system clock, useful for talking to external entities like the file system or other processes.
Neither of these is truly appropriate for showing to a human. Time is hard, and formatting time is additional complexity. It's really a good thing that it's not part of the standard library, otherwise it would have a fixed API that couldn't be improved.
As mentioned elsewhere, I'd recommend using chrono, the heir apparent to the time
crate.
You can use the crate chrono
to achieve the same result:
extern crate chrono;
use chrono::Local;
fn main() {
let date = Local::now();
println!("{}", date.format("%Y-%m-%d][%H:%M:%S"));
}
Edit:
The time crate is not deprecated: it is unmaintained.
Besides, it is not possible to format a time using only the standard library.