liveBook · Manning

Time zone

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

A time zone is a regional convention associating local civil times with offsets or rules relative to a reference time scale.

A time zone supplies context for reading a time as local civil time. Time zones are political divisions, not just technical settings. A common computer representation stores an offset from UTC as a number of seconds. section 9.4.1

Keep the time zone separate from the timestamp’s epoch: an epoch is the starting point used to count elapsed time. UTC also differs from the fixed-length seconds used by atomic clocks because it incorporates leap seconds. Neither an epoch nor a raw count of seconds tells you which local time zone to display. section 9.1 section 9.4

Listing 9.2 Reading the system time and printing it on the screen
1 use chrono::Local;
2  
3 fn main() {
4     let now = Local::now();     #1
5     println!("{}", now);
6 }

This small Rust clock calls `Local::now()` and prints the result. The returned value carries a time zone; the displayed timestamp uses the local zone. section 9.5

When a user supplies a time to set the system clock:

  • Parse it as `DateTime<FixedOffset>` to retain the supplied offset. section 9.7.1
  • Convert it to `DateTime<Local>` for time-zone comparisons before making the OS-specific time-setting call. section 9.7.1

Be precise about what a time value means. A system clock can jump backward, so a time-zone-aware timestamp is not a substitute for a monotonically increasing clock when measuring progress. section 9.10 section 9.7.1

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

How deep it goes
Defines time zones as political divisions, sketches UTC-second offsets, and demonstrates typed timestamps without detailing regional rules.
Examples
Uses a `Local::now()` clock and a command-line time-setting flow that converts `FixedOffset` to `Local`.
What it assumes
Introduces `chrono::Local` through a short Rust program before unpacking its hidden complexity.
What you can do after
Apply it via chrono to print local timestamps and convert user-supplied fixed-offset times for comparison.
How it connects
Connects time zones to UTC offsets, epochs, leap seconds, and system clocks.

Also explained in

Compare how 4 books explain time zone →

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.