liveBook · Manning

Unix epoch

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

An epoch is the defined reference point from which elapsed time is measured.

A clock needs a starting point as well as a way to count time. That starting point is its epoch. The Unix epoch is 1 January 1970 UTC, so a Unix timestamp expresses elapsed time relative to that date. section 9.2 section 9.4 section 9.6.2

An epoch alone does not specify a complete time format:

  • A representation may store whole seconds and a separate fraction of a second. Fixed-width integers make arithmetic straightforward, but limit the range and can introduce rounding. section 9.4
  • Other systems choose different starting dates or units. NTP uses 1 January 1900 as its epoch, while Windows FILETIME counts 100-nanosecond increments from 1 January 1601 UTC. section 9.4

A clock starting without a real-time clock

A Raspberry Pi has no battery-backed real-time clock. At startup, its clock begins at epoch time and uses NTP during boot to find the current time. A device that needs accurate timestamps without a network connection can instead add a real-time clock. section 9.1

In a Rust clock application, chrono can print a numeric Unix timestamp or format the same time as an RFC 2822 or RFC 3339 string. For operating systems using libc, time_t holds elapsed seconds since the epoch; setting the system clock also requires a fractional-second value. Do not assume an epoch-based system clock always moves forward: request a monotonic clock when an application requires one. section 9.6.2 section 9.7.2 section 9.10

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     }

Compare the chrono calls that print seconds since the epoch with those that print formatted dates. section 9.6.2

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

How deep it goes
Defines the reference date and explains counters, integer fields, and representation trade-offs without prescribing one universal encoding.
Examples
Uses Raspberry Pi startup and chrono timestamp-formatting code as concrete examples.
What it assumes
Builds the starting-point idea from digital-clock ticks and counters before using Rust and chrono in the clock example.
What you can do after
Apply it via chrono to print a Unix timestamp and distinguish it from formatted date strings.
How it connects
Connects epochs to Unix timestamps, real-time clocks, NTP, time zones, and monotonic clocks.

Also explained in

Compare how 2 books explain Unix epoch →

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.