Linux time and time zone (Memo)

Source: Internet
Author: User
Linux time and time zone (Memo)-Linux general technology-Linux programming and kernel information. The following is a detailed description. 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 when the system starts. This step is done in rc. sysinit:

A. 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 1, 1970 to the present. Note that it is UTC.

5, ctime ()/localtime ()/mktime (): This type of Time Zone-related functions call tzset internally, but only the first call of tzset will perform actual initialization, 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.

Add one tzset: The application must call tzset () before the chroot to a non-"/" directory. Otherwise, the chroot may not be able to obtain the correct time zone information. Because/etc/localtime cannot be found.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.