liveBook · Manning

Clock

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

A system clock is the operating system's maintained representation of the current time, from which applications obtain time values.

The system clock is the operating system’s view of the current time. Applications obtain their time values from it. At boot, the operating system takes over timekeeping from the battery-powered real-time clock, which can keep running while the computer is off. As the computer runs, system time advances using hardware interrupts. section 9.3 section 9.1

A clock needs regular ticks and a starting point, or epoch, to turn counts into a time value. But the system clock can be set to a different time, so it may jump forward or backward. Do not assume two readings will always increase. For measuring a duration, a steady clock is more suitable: it increases monotonically, though its values need not match the time of day. section 9.2 section 9.3

Starting without a battery-backed clock

A Raspberry Pi without a real-time clock starts with its system clock set to the epoch and uses NTP during boot to find the current time. If a device must timestamp footage without a network connection, an added real-time clock offers another source of time. section 9.1

Setting the system clock is an operating-system operation, not just a change to an application variable. One Rust approach parses a supplied time with chrono, converts it for the local time zone, and passes an OS-specific value to a system call inside an unsafe block. An abrupt change can disrupt applications that expect time to increase. section 9.7.1

Listing 9.8 Setting the time in a libc environment
62 #[cfg(not(windows))]
63 fn set<Tz: TimeZone>(t: DateTime<Tz>) -> () {          #1
64   use libc::{timeval, time_t, suseconds_t};            #2
65   use libc::{settimeofday, timezone }                  #2
66  
67   let t = t.with_timezone(&Local);
68   let mut u: timeval = unsafe { zeroed() };
69  
70   u.tv_sec = t.timestamp() as time_t;
71   u.tv_usec =
72     t.timestamp_subsec_micros() as suseconds_t;
73  
74   unsafe {
75     let mock_tz: *const timezone = std::ptr::null();   #1
76     settimeofday(&u as *const timeval, mock_tz);
77   }
78 }

The non-Windows example passes seconds and microseconds to libc::settimeofday. It does not check whether the call succeeds; a reliable version needs that check. section 9.7.2

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

How deep it goes
Defines the clock, explains its hardware relationship and jumps, and outlines OS-specific setting; the shown libc call omits success checking.
Examples
Uses a Raspberry Pi starting at the epoch and a Rust libc::settimeofday implementation to make clock behavior concrete.
What it assumes
Introduces clock components, epochs, and clock types rather than assuming prior knowledge of computer timekeeping.
What you can do after
Build it from scratch: create an NTP client and set system time through OS-specific calls while accounting for clock jumps.
How it connects
Connects system time to real-time clocks, epochs, NTP, monotonic clocks, and steady clocks.

Also explained in

Compare how 5 books explain clock →

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.