liveBook · Manning

Chapter 9: Time and timekeeping

What you will learn in chapter 9 of Rust in Action: Systems programming concepts and techniques

The chapter introduction explains why computer time is tricky for applications, measurements, and synchronization across machines.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduction explains why computer time is tricky for applications, measurements, and synchronization across machines.

Key terms: system clock, resilient applications, physical and virtual clocks, micro benchmarks, distributed systems, Network Time Protocol (NTP), local network

9.1 Background

Explains why accurate timekeeping is difficult and contrasts atomic time, adjusted civil time, and the clocks available on a computer.

Key terms: earth’s rotation, leap second, timestamp, TAI, UTC, leap seconds, real-time clock, system time, hardware interrupts, NTP

9.2 Sources of time

Explains how computers obtain time from regular ticks and counters, and why CPU-based timestamps can be unreliable for precise profiling.

Key terms: ticks, counters, epoch, battery-powered clock, quartz crystal, CPU clock frequency, CPU clock, out-of-order execution, timestamps, profiling

9.3 Definitions

This section defines the clocks and clock properties needed to discuss computer timekeeping.

Key terms: absolute time, wall clock time, calendar time, real-time clock, CMOS clock, system clock, monotonically increasing, steady clock, high accuracy, skew

9.4 Encoding time

This section explains how computers encode time using numeric fields and epochs, why fixed-width integers are useful but limited, and how real implementations differ.

Key terms: 32-bit integers, fraction of a second, precision, epoch, fixed-width integers, fixed-range, imprecise, rounding errors, FILETIME, time_t

9.4.1 Representing time zones

The section explains that time zones are political divisions and describes a common way to represent their offset from UTC.

Key terms: time zones, UTC, seconds offset

9.5 clock v0.1.0: Teaching an application how to tell the time

This section introduces a small Rust clock program that reads local system time, prints an ISO 8601 timestamp, and uses a time-zone-aware value.

Key terms: system time, local time zone, chrono::Local, ISO 8601, timestamps, time zones, illegal time arithmetic

9.6 clock v0.1.1: Formatting timestamps to comply with ISO 8601 and email standards

Introduces the clock application and previews a run using the --use-standard timestamp flag, without explaining the formatting yet.

9.6.1 Refactoring the clock v0.1.0 code to support a wider architecture

This section refactors clock around a fieldless Clock struct and explains the Never type used by an unimplemented method.

Key terms: Clock, static methods, namespace, Never type, unimplemented!(), todo!(), zero-sized type, ZST

9.6.2 Formatting the time

The section identifies the clock application's three time formats and explains how its RFC 3339 output relates to ISO 8601.

Key terms: timestamp, UNIX timestamp, epoch, rfc2822, rfc3339, RFC 3339, ISO 8601

9.6.3 Providing a full command-line interface

This section explains why the clock application uses clap to parse and validate command-line arguments and how its arguments are organized.

Key terms: command-line arguments, std::env::args, Vec<String>, clap, clap::Arg, clap::App, --help, usage documentation

9.6.4 clock v0.1.1: Full project

This section explains how to recreate the clock v0.1.1 Cargo project using the provided configuration and source listings.

Key terms: cargo, Cargo.toml, src/main.rs, clock v0.1.1

9.7 clock v0.1.2: Setting the time

9.7.1 Common behavior

This section outlines the shared steps for setting system time on different operating systems and warns that abruptly changing the clock can destabilize applications.

Key terms: DateTime<FixedOffset>, DateTime<Local>, system call, unsafe block, Clock struct, monotonically increasing time, system’s clock

9.7.2 Setting the time for operating systems that use libc

This section explains how the clock application sets system time on non-Windows operating systems through libc and introduces the libc time types involved.

Key terms: libc, settimeofday, system clock, timeval, time_t, suseconds_t, type aliases

9.7.3 Setting the time on MS Windows

This section identifies the Windows API and time structure used by the clock application to set the time.

Key terms: kernel32.dll, winapi crate, SYSTEMTIME, FILETIME

9.7.4 clock v0.1.2: The full code listing

This subsection points readers to the complete clock v0.1.2 project files and notes that platform-specific behavior requires changes to Cargo.toml.

9.8 Improving error handling

The section explains why setting the system time needs error handling and contrasts returning a Result with checking the operating system’s error report.

Key terms: settimeofday(), SetSystemTime(), Clock::set(t), Result, std::io::Error::last_os_error()

9.9 clock v0.1.3: Resolving differences between clocks with the Network Time Protocol (NTP)

Introduces NTP as a way to synchronize clocks, contrasting its two operating modes and explaining why choosing a time source is not straightforward.

Key terms: clock synchronization, always on mode, peer-to-peer, daemon, request/response mode, timestamp, network latency, local clock, hierarchy, atomic clocks

9.9.1 Sending NTP requests and interpreting responses

The section explains how an NTP client-server exchange produces four timestamps used to compare the local clock with a time server.

Key terms: request, response, time server, T1–T4, NTPResult

9.9.2 Adjusting the local time as a result of the server’s response

Explains how the NTP client interprets server responses and makes cautious adjustments to its local clock without claiming to know an absolute, correct time.

Key terms: time offset, θ (theta), δ (delta), relative timestamps, NTP, θ, δ, weighted mean, 1 / θ2, time servers

9.9.3 Converting between time representations that use different precisions and epochs

This section explains why converting between chrono and NTP timestamps may lose precision and how Rust’s From trait enables conversions using .into().

Key terms: chrono, NTP, nanosecond precision, picoseconds, From, from(), Into, .into()

9.9.4 clock v0.1.3: The full code listing

This section points readers to the complete clock v0.1.3 code listing and its source file, without teaching a new concept.

9.10 Summary

Concepts explained in this chapter

← Chapter 8: Networking Chapter 10: Processes, threads, and containers →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.