Knowledge

Linux: time

Subject: .../LINUX

Use a calendar clock and a stopwatch clock

People use one clock to say when something happened and a stopwatch to measure how long it took. Programs need the same distinction. Linux's wall clock gives a date and time that can be shown on a calendar. A monotonic clock measures elapsed time from an arbitrary starting point. Pick the clock that answers the question you have.

CLOCK_REALTIME represents civil, or wall-clock, time. It can jump forward or backward when someone sets the clock or a time service corrects it. File timestamps, log entries, and calendar events generally use this clock, displayed according to a time zone.

For measuring an interval, CLOCK_MONOTONIC is usually the right choice. It advances from an unspecified starting point and does not jump when wall-clock time changes, although Linux may adjust its rate to correct clock drift. Since its starting point is unspecified, compare two readings from the same system; do not use one reading as a date. For example, an app can record the clock before and after a download, then subtract the readings to find elapsed time even if wall-clock time was corrected. Linux also provides CLOCK_BOOTTIME, which includes time spent suspended.

Turn a moment into a calendar date

A Unix timestamp counts seconds from an epoch, the agreed starting time of 1970-01-01 UTC. A time zone turns that same instant into local calendar fields; it does not change the instant itself. UTC gives logs from different places a shared reference. Local daylight-saving rules can make a clock hour repeat or disappear when the rules change.

The real-time clock (RTC) is a hardware clock that can retain time while a computer is powered off. During startup, Linux can initialize the system clock from it, then synchronize system time over a network. The RTC can be set to represent UTC or local time; that choice must match the operating system's expectation. Keeping it in UTC avoids daylight-saving ambiguity during startup.

Choose the source for the question

date --iso-8601=seconds prints a readable timestamp. On a system using systemd, timedatectl status shows the configured time zone and a synchronization indicator. These commands display system state; an application can choose a different timestamp format or time source.

The shell keyword time reports how long a command ran, including elapsed time and CPU time. It is different from the clock APIs a program uses to measure its own intervals. Use CLOCK_MONOTONIC for a duration and CLOCK_REALTIME for a timestamp tied to a date.

When comparing events, check the time zone and synchronization state before deciding which came first. Small clock differences between computers can reorder close events, and wall-clock corrections can make timestamps run backward. If a log looks wrong, compare UTC with the local rendering and check the application's timestamp format before changing the system clock.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful