A common requirement in programs is getting the number of seconds, milliseconds, or nanoseconds since the Unix epoch. Here’s how to do it in Rust.
To run the program:
In Rust, we use the std::time::SystemTime and std::time::UNIX_EPOCH to work with timestamps. The duration_since method gives us the time elapsed since the Unix epoch, which we can then convert to seconds, milliseconds, or nanoseconds using the appropriate methods.
To convert back from seconds or nanoseconds to SystemTime, we add the duration to UNIX_EPOCH.
Next, we’ll look at another time-related task: time parsing and formatting.