The first thing to note is that my system is fedora, and other systems may be different. ◆ 1. the time is stored in the hardware real-time clock (RTC). The RTC is powered by the motherboard battery and will not be caused even if the power is turned off.
The first thing to note is that my system is fedora, and other systems may be different.
◆ 1. the time is stored in the hardware real-time clock (RTC). The RTC is powered by the motherboard battery and will not cause time loss even if the power is turned off.
◆ 2. get the time from RTC at system startup. This step is done in rc. sysinit:
A) first, obtain the RTC parameter UTC/ARC/SRM from/etc/sysconfig/clock. If the value of UTC is true, the time that RTC saves is UTC time, and false indicates that the local time is saved. For the meanings of ARC and SRM, refer to the hwclock manpage. generally, it is false;
B) construct the parameters of the hwclock command based on the obtained content, and read the time from RTC to the system clock.
* It must be noted that the hwclock output is local time, and the localtime/utc parameter is only used to tell the COMS time zone of the hwclock command. For example, in Linux, the system time zone is set to CST (China Standard Time), and the COMS time adopts the wall time, that is, the COMS time zone is also CST. if the -- utc parameter is used, it will make hwclock mistakenly think that the COMS time zone is UTC, so the output time is automatically + 8.
◆ 3, tzset (): tzset is used in the program to initialize global variables such as tzname. it first tries to get the time zone from the environment variable "TZ". if the TZ environment variable exists, that is, if the returned value of getenv ("TZ") is not NULL and the content is valid, the TZ value is used. if the variable exists but the content is invalid, for example, it contains an incorrect time zone name, UTC is used. if the variable does not exist, that is, getenv ("TZ") returns NULL, you can find the system time zone setting file, which is generally/etc/localtime. For the content format of the file referred to by localtime, see tzfile (5 ).
◆ 4, time (): returns the number of seconds since 00:00:00 UTC, January to the present. Note that it is UTC.
◆ 5, ctime ()/localtime ()/mktime (): these time zone-related functions call tzset internally, but the actual initialization is only performed when tzset is called for the first time, all subsequent calls are directly returned.
◆ 6. when the system is shut down, such as shutdown/reboot/poweroff/halt, the system time will be saved to RTC, which is completed in the halt script, see/etc/init. d/halt content.