liveBook · Manning

Timestamp

As explained in Rust in Action: Systems programming concepts and techniques

A timestamp is a representation of a point in time, commonly expressed as an elapsed value or formatted date and time.

A timestamp represents a point in time. A program can read one from the system clock, store it as an elapsed value, or print it as a date and time. The printed form may include a time zone; the stored value needs an agreed starting point, called an epoch. section 9.4 section 9.5

An integer encoding is simple and efficient, but its details matter:

  • The epoch determines where counting begins. A UNIX timestamp counts seconds since its epoch; Windows FILETIME counts 100-nanosecond increments since 1 January 1601 UTC. section 9.4 section 9.6.2
  • A fixed-width integer has a finite range, and discrete units can lose subsecond precision. Do not assume two timestamp formats use the same units or epoch. section 9.4

Read and format the current time

In Rust, `chrono::Local::now()` reads the current system time as a value that includes a time zone. Printing that value gives a formatted timestamp. For a specific output format, use `now.timestamp()` for UNIX seconds, `now.to_rfc2822()` for an email-style date, or `now.to_rfc3339()` for an ISO 8601-compatible date. section 9.5 section 9.6.2

Listing 9.4 Showing the methods used to format timestamps
48     let now = Clock::get();
49     match std {
50         "timestamp" => println!("{}", now.timestamp()),
51         "rfc2822"   => println!("{}", now.to_rfc2822()),
52         "rfc3339"   => println!("{}", now.to_rfc3339()),
53         _ => unreachable!(),
54     }

The formatting branches show how the same `now` value becomes epoch seconds or either of two standardized date strings. section 9.6.2

A timestamp is not necessarily a perfect account of elapsed time. System clocks can jump, while CPU-based timing can be misleading when instructions shift during execution or CPUs run at slightly different rates. Across computers, an NTP client compares its timestamp with a server’s and accounts for network delay before adjusting the local clock. section 9.0 section 9.2 section 9.9

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Works through integer-encoding trade-offs, reading system time, formatting, and comparing timestamps during NTP synchronization.
Examples
Uses a Rust clock program that prints local time, UNIX seconds, and RFC-formatted dates.
What it assumes
Starts with a short chrono program and defers the operating-system complexity behind reading time.
What you can do after
Apply it via chrono to read system time and print UNIX, RFC 2822, or RFC 3339 timestamps.
How it connects
Connects epochs and time zones to system clocks, date formatting, CPU profiling, and Network Time Protocol synchronization.

Also explained in

Compare how 10 books explain timestamp →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.