Linux hardware time (RTC time), system time (UTC time, Universal time), local time (Local time), time zone (Time zone) and daylight saving time (DST) analysis

Article directory

  • Understand time: hardware time, system time (UTC time), local time, time zone and daylight saving time
    • 1. Hardware time (RTC time)
      • 1.1 Introduction to hardware time
      • 1.2 How to use hardware time
    • 2. System time (UTC time) (Universal time)
      • 2.1 Introduction to system time
      • 2.2 UTC time
    • 3. Local time
      • 3.1 Introduction to local time
      • 3.2 How to use local time
    • 4. Time zone
      • 4.1 Introduction to time zones
      • 4.2 How to set the time zone
      • 4.3 Time zone in docker container (synchronized with host date and time)
    • 5. Daylight Saving Time (DST)
      • 5.1 Introduction to Daylight Saving Time
      • 5.2 Daylight Saving Time Area
        • Regions using daylight saving time
        • Areas that do not observe daylight saving time:
      • 5.3 How to deal with daylight saving time
    • 6. Ubuntu timedatectl command example: View time-related information

Understand time: hardware time, system time (UTC time), local time, time zone and daylight saving time

Processing and managing time is an important aspect of computer science, but it is also one of the most complex and confusing. This article will introduce hardware time, system time (UTC time), local time, time zone and daylight saving time in detail, hoping to help readers better understand these concepts.

1. Hardware time (RTC time)

1.1 Introduction to hardware time

Hardware time, also known as the real-time clock (RTC), is an operating-system-independent device on your computer’s motherboard that keeps running even when the power is turned off or even during a power outage. Its function is to record the current date and time.

1.2 How to use hardware time

Most operating systems read the time from the RTC when booting and then set the system time. Likewise, the operating system can also write the system time back to the RTC.

In Unix and Unix-like systems, the hwclock command can be used to query and set the RTC time. For example:

# Query hardware time
hwclock --show

# Set system time to hardware time
hwclock --systohc

# Set hardware time to system time
hwclock --hctosys

In Windows systems, you can query and set the RTC time through the date and time setting dialog box or the time command.

2. System time (UTC time) (Universal time)

2.1 Introduction to system time

System time is the time used internally by the computer, it is usually set from the RTC at startup and then tracked by the system clock. The system clock is part of the operating system kernel and can provide precise time in milliseconds or nanoseconds.

2.2 UTC time

System time is usually expressed in Coordinated Universal Time (UTC). UTC is a time standard based on atomic clocks, maintained by scientists around the world through precision measurements. It is very close to Greenwich Mean Time (GMT) but is not affected by changes in the Earth’s rotation speed.

In Unix and Unix-like systems, the date command can be used to query and set the system time:

# Query system time (UTC)
date -u

# Query local time (determined by system time UTC and time zone)
date

# Set the local time (the following string is the local time) (setting the local time will also set the system time, but it will automatically do the time zone conversion for you)
date -s "2022-01-01 00:00:00"

# Set the system time directly (not recommended, generally set the system time by setting the local time, because with this setting you have to calculate the time zone for the subsequent times)
date -u -s "2022-01-01 00:00:00"

In Windows systems, you can query and set the system time through the date and time setting dialog box or the time command.

3. Local time

3.1 Introduction to local time

Local time is the system time after time zone conversion. Time zones are determined based on geographical location. The world is divided into 24 time zones, each of which represents approximately 15 degrees of longitude. For example, Beijing time is UTC+8, while London time is UTC+0.

3.2 How to use local time

In Unix and Unix-like systems, the date command displays local time by default:

# Query local time
date

In Windows systems, the date and time setting dialog box or the time command also displays the local time by default.

4. Time zone

4.1 Introduction to time zones

A time zone is a geographical area divided to facilitate unified time. The world is divided into 24 time zones, each representing approximately 15 degrees of longitude. The time in each time zone is based on UTC (Coordinated Universal Time). The time zone in the east is later than UTC time, and the time zone in the west is earlier than UTC time.

4.2 How to set the time zone

On Unix and Unix-like systems, you can select a time zone using the tzselect command and then link the selected time zone to /etc/localtime:

# Select time zone
tzselect

# Set time zone
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

In Windows systems, the time zone can be changed through the Date and Time Settings dialog box.

4.3 Time zone in docker container (synchronized with host date and time)

The default time zone for a Docker container depends on the base image used. The default time zone for many common base images (e.g. Debian, Ubuntu, etc.) is usually UTC (Coordinated Universal Time). This means that if a specific time zone is not explicitly set when creating or running a Docker container, the time inside the container will be displayed in UTC.

If you want the time zone in the docker container to be consistent with the host machine, you can add the following mount when the container is docker run:

-v /etc/localtime:/etc/localtime:ro \
-v /etc/timezone:/etc/timezone:ro\
  • /etc/timezone: This file contains the currently set time zone information. For example, if the system is set to the United States Eastern Time Zone, the contents of this file might be America/New_York.

  • /etc/localtime: This file is a copy or soft link of the data file corresponding to the currently set time zone. This file is used to provide localized date and time display.

These two files play different roles in the system, and both are important factors affecting system time performance. If you want the time of the Docker container to be exactly the same as that of the host, you need to mount these two files at the same time when running the container:

docker run -v /etc/timezone:/etc/timezone:ro -v /etc/localtime:/etc/localtime:ro your_image

Mounting only /etc/timezone or /etc/localtime may cause problems, such as possible errors when handling dates and times across time zones.

By default, Docker containers do not automatically mount /etc/localtime files. The container obtains the physical time through the host’s kernel.

Although Docker containers are isolated from the host machine in many ways, they share the same kernel. This means that when a container needs to obtain the current physical time (for example, by calling the time() system call), it will obtain this information directly from the host’s kernel. This is why even if /etc/localtime is not mounted, the program in the container can still get the correct physical time.

However, time zone information is not just physical time, it also involves logical time processing, including calendar calculations, daylight saving time adjustments, and so on. This information is stored in the files /etc/timezone and /etc/localtime. If you want the program in the container to correctly handle these time zone-related issues, you need to mount these two files into the container at the same time. One is indispensable.

5. Daylight Saving Time (DST)

5.1 Introduction to Daylight Saving Time

Daylight saving time is the practice of adjusting time during the summer to take advantage of daylight hours. Typically, daylight saving time moves the time forward one hour in late spring and then moves it back one hour in early fall. This allows people to have more daylight at night, thus saving electricity.

5.2 Daylight Saving Time Region

The adoption of daylight saving time is not uniform around the world, and many countries or regions have their own specific regulations.

Regions that adopt daylight saving time
  1. Europe: Most European countries start daylight saving time on the last Sunday in March and end on the last Sunday in October. This includes the UK, France, Germany, Spain and other countries.

  2. North America: Includes parts of the United States (except Arizona and Hawaii), Canada (except Saskatchewan and some places), and some areas of Mexico. Daylight Saving Time in North America begins on the second Sunday in March and ends on the first Sunday in November.

  3. Australia: Only South Australia, New South Wales, Tasmania and Victoria observe daylight saving time, which starts on the first Sunday in October each year and ends on the third day of April the following year. Ending on a Sunday.

Regions that do not adopt daylight saving time:
  1. China: China has not implemented daylight saving time since 1991.

  2. Japan: Japan has not observed daylight saving time since 1952.

  3. India: India does not observe daylight saving time.

  4. Arizona and Hawaii: These two U.S. regions do not observe daylight saving time.

  5. Most countries in Africa and South America: Except for some specific countries or regions, most countries in Africa and South America do not observe daylight saving time.

5.3 How to deal with daylight saving time

The time libraries of most modern operating systems and programming languages can handle daylight saving time automatically. For example, in Python, the pytz library provides time zone data around the world and can automatically handle daylight saving time:

from datetime import datetime
import pytz

# Get the current UTC time
now = datetime.now(pytz.utc)

# Convert to New York time (automatically handles daylight saving time)
ny_time = now.astimezone(pytz.timezone('America/New_York'))

print(ny_time)

Likewise, in Unix and Unix-like systems, daylight saving time is automatically handled by the system as long as the time zone is set correctly.

6. Ubuntu timedatectl command example: View time-related information

In ubuntu, you can use timedatectl to view the above corresponding concepts:

As shown in the figure, the command results display local time (Local time), system time (Universal time), hardware time (RTC time), time zone (Time zone) and other information.