The first thing to say is that my system is fedora and other systems may not be exactly the same.
1, the time is kept in the hardware Real Time Clock (RTC), the RTC is powered by the motherboard battery, even if the power off source will not cause time loss.
2, when the system starts to get time from the RTC, this step is done in Rc.sysinit:
A, first obtain the RTC correlation parameter utc/arc/srm from the/etc/sysconfig/clock. UTC to TRUE indicates that the RTC was saved in UTC time, and False indicates that the local time was saved. The meaning of Arc and SRM refer to the manpage of Hwclock, generally false;
b, construct the parameters of the Hwclock command based on the content obtained above, and read the time from the RTC to the system clock.
* The point to note is that the Hwclock output is local time and the LOCALTIME/UTC parameter is used to tell the hwclock command coms the time zone. For example, the Linux system's system time zone is set to CST (China Standard Time), COMs time to use the wall time, that is, COMs time zone is also a CST, then if the--UTC parameter, it will make Hwclock mistakenly think COMs time zone is UTC, so the output time will automatically +8.
3,tzset (): Tzset Initializes global variables such as Tzname in a program that first attempts to obtain the time zone from the environment variable "TZ", using the TZ value if the GETENV environment variable exists, that is, TZ ("TZ") returns a value that is not NULL, and contains content that is valid If the variable exists but the contents are invalid, such as the name of the time zone that contains the error, use UTC, or if the variable does not exist, that is, getenv ("TZ") returns NULL, the system time zone settings file is found, typically/etc/localtime. LocalTime refers to the content format of the file referred to Tzfile (5).
4,time (): Returns the number of seconds elapsed since 00:00:00 UTC, January 1, 1970, and note that it is UTC.
5,ctime ()/localtime ()/mktime (): This type of time zone-related functions call Tzset, but only the first call to Tzset does the actual initialization, and the subsequent calls are returned directly.
6, when the system shuts down, such as performing shutdown/reboot/poweroff/halt operations, the system time is saved to the RTC, which is done in the halt script, see/etc/init.d/halt content.