This is the 27th day of my participation in the August Genwen Challenge.More challenges in August

There are two types of Linux system time: system time and hardware time. The system time is the Linux kernel time, and the hardware time is the RTC time in the BOIS. During startup, the Linux kernel attempts to read the RTC time in the BIOS and set the system time based on it. After the kernel is started, the system time and the hardware are two independent times, running separately.

Embedded Linux system development, also need to provide hardware to provide time baseline for the system, this hardware is generally RTC chip, for example, RX8010. Linux time Settings include system time Settings, hardware time Settings, and time zone Settings.

Time zone configuration

The tzselect command is used to set the current time zone of the system. On the desktop, such as Ubuntu or Debian, the system supports complete time zone configuration. You can use tzselect to configure the time zone step by step as prompted, for example, to set the Asia/China/Beijing time zone

Run the tzselect command --> Select Asia --> Select China --> Select Beijing Time-->YesCopy the code

Finally, the system will set TZ=’Asia/Shanghai’; Export TZ to the ~/. Profile file.

The embedded Linux operating system does not provide the tzselect command. In this case, you can directly configure the TZ environment variable to set the system time zone. In addition, you need to provide the configuration file related to the time zonein the /usr/share/zoneinfo directory, where only the Asia time zone is configured.

Configure the TZ environment variable $cat /etc/profile TZ='Asia/Shanghai'; export TZ
Copy the code
The /usr/shared/zoneinfo directory must contain the time zone information # /usr/share/zoneinfo$ls Asia PRCCopy the code

You can run the date command to view the current time zone information.

Tue Jul  7 10:00:02 CST 2020#CST stands for East 8th WardCopy the code

System Time Configuration

You can configure the system time in either of the following modes: manual setting and NTP synchronization. In manual setting, you can run the date command to directly set the system time. In NTP mode, you can configure the local system time through the NTP server.

To ensure time accuracy, you are advised to configure the system time in NTP mode.

ntpdate 0.cn.pool.ntp.org
Copy the code

If you do not have high requirements for time accuracy, you can use the date command to directly set the system time. The time configuration format supported by date is very flexible. Here are some simple examples.

1, set the date to2020years07month6Day/root @ Linux - node ~# date -s 07/06/20

2, set the time to10point07points00Seconds [root @ Linux - node ~]# date -s 10:07:00

3, set the time to2020years07month06day10:08
[root@linux-node ~]#  date -s "The 2020-07-07 10:08:00"
Copy the code

Hardware time configuration

The hwclock command is used to manage hardware time. You can use this command to set the hardware time and convert the system time to the hardware time.

Hwclock -r # Displays the current hardware time Tue Jul7 02:10:10 2020  0.000000 seconds
Copy the code
Hwclock-w-u Synchronizes the system time and hardware timeCopy the code

Note: -w indicates that the system time is synchronized to the hardware time, and -u indicates that the hardware time is the UTC time. During the system startup, the system synchronizes the system time with the hardware. The system considers the UTC time obtained from the hardware and converts the UTC time to the local time based on the local time zone. Therefore, you must add the -u option to enable the system to set the correct time zone.

Hwclock-s-u Synchronizes the system time and hardware timeCopy the code

-s indicates that the hardware time is synchronized to the system time. -u indicates that the hardware time is UTC time. If the hardware time is saved in UTC time, you must use the -u option.