9 Time and timekeeping

 

This chapter covers

  • Understanding how a computer keeps time
  • How operating systems represent timestamps
  • Synchronizing atomic clocks with the Network Time Protocol (NTP)

In this chapter, you’ll produce an NTP (Network Time Protocol) client that requests the current time from the world’s network of public time servers. It’s a fully functioning client that can be included in your own computer’s boot process to keep it in sync with the world.

Understanding how time works within computers supports your efforts to build resilient applications. The system clock jumps both backwards and forwards in time. Knowing why this happens allows you to anticipate and prepare for that eventuality.

Your computer also contains multiple physical and virtual clocks. It takes some knowledge to understand the limitations of each and when these are appropriate. Understanding the limitations of each should foster a healthy skepticism about micro benchmarks and other time-sensitive code.

9.1 Background

9.2 Sources of time

9.3 Definitions

9.4 Encoding time

9.4.1 Representing time zones

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

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

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

9.6.2 Formatting the time

9.6.3 Providing a full command-line interface

9.6.4 clock v0.1.1: Full project

9.7 clock v0.1.2: Setting the time

9.7.1 Common behavior

9.7.2 Setting the time for operating systems that use libc

9.7.3 Setting the time on MS Windows

9.7.4 clock v0.1.2: The full code listing