Technical skill

Linux time: system time

Subject: .../LINUX

Ask what kind of time you need

A computer uses time to answer two different questions: “What date and time is it?” and “How long has this task been running?” The first is calendar time, like the clock on a wall. The second is elapsed time, like a stopwatch. The system clock can be corrected to stay close to real-world time, so it is a poor stopwatch if the clock changes while a program is measuring.

On Linux, these commands show several views of time:

date -u
date +%s
timedatectl status
cat /proc/uptime

date -u prints the calendar clock in UTC, a shared time standard. date +%s prints seconds since the Unix epoch, the starting point of 1970-01-01 UTC. timedatectl summarizes local time, time zone, hardware clock and synchronization state. /proc/uptime reports how long the system has been running and its idle time.

Calendar time and elapsed time

Programs read the system's calendar clock with CLOCK_REALTIME. It counts time relative to the Unix epoch, while the date command formats that instant for people, usually in the configured time zone. Event timestamps are often stored in UTC so the same instant can be compared across regions.

The real-time clock can be set or adjusted. Network synchronization or an explicit change can make the displayed time jump, or gradually change its rate; the gradual correction is called slewing. If a program reads this clock before and after a long operation, the difference may include a clock correction as well as the operation's duration.

For durations and timeouts, software can use a monotonic clock such as CLOCK_MONOTONIC through clock_gettime(). It does not jump backward when calendar time is reset, though frequency adjustments can affect its rate. Linux also provides CLOCK_BOOTTIME, which is monotonic and includes time spent suspended. Choose it when time asleep should count; otherwise use the monotonic clock suited to the task.

Hardware clock and local display

A real-time clock (RTC) is hardware that keeps approximate time while the computer is off, often using a battery. Firmware or the operating system can use it to set the system clock during startup. The RTC may store UTC or local time. On systems using systemd, timedatectl can display and configure this policy.

A time zone is a set of rules that turns an instant into local calendar time. Changing the zone changes the displayed date and time, not the instant itself. Daylight-saving rules are part of time-zone data and can change. For local civil time, use a named zone such as Europe/Moscow instead of a fixed offset.

Network time protocols synchronize the system clock with outside sources. Synchronization does not make every timestamp exact; network delay, clock hardware, configuration, and the running time service all affect accuracy. For software, record events with calendar time and measure durations with a monotonic clock. When comparing logs from different computers, check synchronization and normalize time zones. Output formats and time services differ among Linux distributions.

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