liveBook · Manning

SystemTime

As explained in Learn Rust in a Month of Lunches

SystemTime is a Rust standard-library type representing measurements from the system clock for interaction with external entities such as files and processes.

SystemTime measures the system clock. Use it when communicating with external entities such as the file system or other processes, or when you need time relative to the Unix epoch. section 17.2

Keep these differences in mind:

  • SystemTime is not monotonic: a later real-world operation can have an earlier recorded time if the system clock changes. section 17.2
  • Instant measures elapsed time for program operations, while SystemTime corresponds to calendar-like system time. section 17.2
  • Compare SystemTime with UNIX_EPOCH through duration_since to obtain elapsed seconds and smaller time units since January 1, 1970. section 17.2
use std::time::{SystemTime, UNIX_EPOCH};
 
fn main() {
    println!("{:?}", SystemTime::now().duration_since(UNIX_EPOCH).unwrap());
}

This minimal Rust example converts the current SystemTime into a duration since UNIX_EPOCH; unwrap handles the demonstrated successful result. section 17.2

How Learn Rust in a Month of Lunches covers it

How deep it goes
Defines SystemTime, contrasts it with Instant, explains non-monotonic behavior, and demonstrates epoch conversion.
Examples
Demonstrates printing Instant and SystemTime, then calculating SystemTime duration since UNIX_EPOCH.
What it assumes
Assumes basic Rust syntax and introduces the distinction between system-clock time and elapsed-time measurement.
What you can do after
Apply it via Rust’s standard library to obtain current system time and calculate elapsed time since UNIX_EPOCH.
How it connects
Connects SystemTime with Instant, UNIX_EPOCH, Duration, the file system, and processes.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.