TimeKit
All Articles
Tech7 min readJuly 5, 2024

How Computers Keep Time and Store Dates

Learn how computer time works, from the battery-backed real-time clock and system clock to NTP synchronization, Unix timestamps, and time zones.

How Computers Keep Time and Store Dates editorial illustration

Computer time comes from several layers working together. A battery-backed clock gives the machine a starting point, the operating system maintains a faster system clock while the computer runs, and a network time service corrects the drift. Dates are then stored as numbers and formatted for the person viewing them.

The Three Clocks People Often Confuse

A modern computer deals with at least three different ideas called a clock:

  • The CPU clock controls how quickly a processor executes cycles. It does not know the date.
  • The real-time clock, or RTC, keeps calendar time while the computer is powered off.
  • The system clock is maintained by the operating system while the computer is running.

The CPU clock measures processing speed. The RTC and system clock are the parts that answer questions such as "What time is it?" and "When was this file changed?"

How the Real-Time Clock Works

The RTC is a small, low-power circuit on the motherboard. A coin-cell battery usually keeps it running when the computer is unplugged. When the machine starts, its firmware reads this clock and gives the operating system an initial date and time.

An RTC is convenient, but it is not perfect. Its quartz oscillator can run slightly fast or slow as temperature and age change. A drift of only a few seconds per day becomes minutes over several months.

A dead RTC battery creates familiar symptoms:

  • The date resets after every shutdown
  • Firmware settings are lost
  • The computer starts with a date far in the past
  • Secure websites report certificate date errors

The Operating System System Clock

After startup, the operating system takes over. It counts timer ticks and maintains the system time in memory. This clock is faster to read than the motherboard RTC and can offer much finer precision.

Applications ask the operating system for the current time. They normally do not read the RTC directly. File timestamps, scheduled jobs, browser cookies, logs, and authentication tokens all depend on the system clock.

How Internet Time Synchronization Corrects Drift

Computers connected to a network normally synchronize with a time server. The most common system is the Network Time Protocol, or NTP.

An NTP client exchanges messages with one or more servers, estimates network delay, compares the remote time with the local clock, and applies a correction. Large errors may be corrected immediately. Small errors are often corrected gradually so that time does not jump backward during normal operation.

Windows Time, systemd-timesyncd, chrony, and ntpd are examples of services that perform this job. NIST also distributes official U.S. time through internet time services.

Wall Clock Time vs Monotonic Time

Operating systems expose two useful kinds of time.

Wall clock time represents a calendar date and local time. It can change when NTP corrects the computer, when the user edits the clock, or when daylight saving time begins or ends.

Monotonic time only measures elapsed time since an unspecified starting point. It never moves backward during normal operation. Software should use a monotonic clock for animation, timeouts, benchmarks, and measuring how long a task took.

Using wall clock time to measure a short duration can create bugs. If the system clock is corrected while a timer is running, the result may be too long, too short, or even negative.

How Dates Are Stored

Many systems store an instant as a count from an epoch. Unix time counts seconds from January 1, 1970 at 00:00:00 UTC. JavaScript commonly uses milliseconds from that same epoch.

This numeric representation makes comparison and arithmetic easy. The time zone is usually applied later, when the value is shown to a person. One timestamp can appear as 9:00 AM in New York and 2:00 PM in London while still representing the same instant.

Good software separates three things:

  • The absolute instant
  • The time zone used for display
  • The human-readable format

Why a Computer Clock Can Be Wrong

Common causes include:

  • Automatic time synchronization is disabled
  • The selected time zone is wrong
  • The RTC battery is weak
  • A device has been offline for a long time
  • A firewall blocks network time traffic
  • A virtual machine pauses or resumes incorrectly
  • Software treats UTC as local time

If the clock is off by an exact number of hours, check the time zone first. If it slowly gains or loses seconds, check synchronization. If it resets after shutdown, suspect the RTC battery.

A Reliable Mental Model

At boot, the RTC supplies an estimate. While the machine runs, the operating system advances the system clock. NTP keeps that clock close to an external reference. Applications store instants as timestamps and convert them into local dates only when needed.

That chain explains most questions about computer time. The hardware keeps a rough starting point, the operating system counts forward, the network corrects drift, and software decides how the result should look.

#computer time#system clock#NTP#timestamps