Many functions of the Linux system are based on reading time. For example, the log system needs a time stamp to record the log, which provides support for later troubleshooting; the cron service needs the correct time setting to support the periodic, specific time point execution of a task; and so on. Most Linux desktop systems provide a rich and friendly time setting interface, which is very convenient for users to set time. However, in embedded devices with strong customization and particularity, time setting is sometimes not so easy. This article comprehensively analyzes the time setting details of linux-arm embedded devices.
Hardware time and system time
- Hardware time
Hardware time is also called RTC or CMOS clock. It is powered by the mainboard battery and is independent of system operation. It maintains time even when the system is shut down or power off, providing a time reference for the system. - System time
System time is also called kernel clock and software clock. It is obtained from the hardware time in the system startup phase, and then independently maintained through the timer interrupt mechanism. Linux applications and services are based on system time, not hardware time.
hwclock and date commands
hwclock is the hardware time operation command, and date is the system time operation command.
hwclock and date display hardware time and system time respectively.
You can set the system time through date -s.
hwclock -s is used to synchronize the hardware time to the system time.
hwclock -w is used to synchronize the system time to the hardware time.
Linux rtc driver
The prerequisite for hardware time is to ensure that Real Time Clock in the linux kernel is selected.
After rtc is loaded successfully, the corresponding node will be generated in the /dev directory.
Network time synchronization
For embedded devices with Internet access capability, the system time can be synchronized from a network time server, which requires the support of the ntpdate command.
Download ntp source package
Download link: http://download.csdn.net/detail/messidona11/9734990
Unzip
tar xvzf ntp-4.2.4p7.tar.gz
Configure ntp source code
./configure –prefix=$PWD/install –exec-prefix=$PWD/install –host=arm-linux CC=arm-none-linux-gnueabi-gcc
Note: need to change arm-none-linux-gnueabi-gcc Name for your own gcc cross compiler
Compile
After executing the make&make install command, if no error is reported, three folders, bin, lib, and man will be generated in the _install directory, where ntpdate in the bin directory is the command we need.
Copy ntpdate to the environment variable of the linux-arm device, and use chmod u+x ntpdate to give execution permission.
Get network time through ntpdate
Time zone configuration method
- UTC time
Coordinated Universal Time Coordinated Universal Time, also known as Universal Standard Time and Universal Time. - Local time
local time = UTC + /- time zone - One of the time zone setting methods:
Set the environment variable TZ ( or add a file TZ in the etc directory and write the content as CTS-8 )
export TZ=CST-8
date -u displays UTC time, date command displays local time, local time is UTC The time is converted according to the time zone.
It can be seen from the figure above that the local time is 8 hours earlier than UTC time, and CST-8 is Dongba District.
BTW: You can add the time synchronization command to crontab to periodically synchronize from the time server.
Many functions of the Linux system are based on reading time. For example, the log system needs a time stamp to record the log, which provides support for later troubleshooting; the cron service needs the correct time setting to support the periodic, specific time point execution of a task; and so on. Most Linux desktop systems provide a rich and friendly time setting interface, which is very convenient for users to set time. However, in embedded devices with strong customization and particularity, time setting is sometimes not so easy. This article comprehensively analyzes the time setting details of linux-arm embedded devices.