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




