Role of tzset

Source: Internet
Author: User

Function Name: tzset
Function prototype: void tzset (void)
Function: A Unix-compatible function used to obtain the time zone. It is useless in a DOS environment.
Function return:
Parameter description:
File: <time. h>

# Include <time. h>
# Include <stdlib. h>
# Include <stdio. h>
Int main ()
{
Time_t TD;
Putenv ("TZ = pst8pdt ");
Tzset ();
Time (& TD );
Printf ("current time = % s", asctime (localtime (& TD )));
Return 0;
}

 

 

 

For a long time, Linux is also a random write point program. Building a server seldom pays attention to a basic setting-time zone. I believe most of the fans share this. We live in one place, one country, and one region, and at least will not change frequently. So... our machine time settings are rarely changed, and now UTP-time network synchronization protocol is available in many cases, not to mention changing the time zone.
However, for a product that uses Linux as a platform, it may change the time zone. Even if there are not many opportunities, this part cannot be ignored by designers, engineers, and project managers. So when I encountered this problem for the second time, I chose to thoroughly understand it, so I had such a record. The problem is described as follows: we can use the time call to get the current time. Note that this is the machine time in UTC-the number of seconds since, January 1, January 1, 1970, then, we can use the localtime call to convert the time obtained by time to the local time, and the time zone information will be used to adjust the conversion from UTC to the local time. For a daemon process, if you call time and localtime at intervals, you can obtain the value of the current time on a regular basis. But what if the time zone is converted during this period?
Maybe you will think that there will be a big problem-the time zone has changed, and localtime will be greatly adjusted. Well, the next conclusion is that the subsequent program needs to handle this change with caution ......
Unfortunately, we feel that it is wrong. If the time zone settings change, the localtime conversion time remains the same as before unless the program starts again. Linux time zone settings are completed in several different ways. On the one hand, you can set the environment variable TZ to specify the time zone, such as TZ = Asia/Shanghai. You can specify the time zone as the time zone of Shanghai, the Time Zone configuration appears in the/usr/share/zoneinfo/Asia/Shanghai file (RedHat Environment). If no TZ environment variable is specified, the Default Time Zone configuration file can be obtained using/etc/localtime. This file may be a symbolic link pointing to a real file, it is also possible that the file under/usr/share/zoneinfo is copied to achieve the desired result. Since the environment variables are inherited by each process group separately, it is difficult to change the environment variables of other process groups after the time zone is set. Therefore, the system rarely directly sets the TZ environment variable, instead, the/etc/localtime file indicates the time zone location.
In this case, why does the running daemon program not use the new zone when the localtime function is called after the time zone is set? This can be answered through the glibc source code. Localtime and other functions related to the local time zone call the tzset function first, which can be seen through the manpage of the tzset function. The work completed by tzset is to read and buffer the current time zone information (through the TZ environment variable or/etc/localtime. In fact, tzset is implemented through the internal tzset_internal function. The explicit call of tzset will explicitly inform tzset_internal, when localtime is called separately, tzset_internal is notified implicitly. The former forces the tzset to reload the TZ information or/etc/localtime in whatever circumstances, the latter means that the time zone information will be loaded again only when TZ changes or the file name changes. Therefore, if the content of/etc/localtime is changed but the file name "/etc/localtime" is not changed, the time zone information will not be loaded again, as a result, the localtime function still converts the UTC time to the local time in the old time zone.
Conclusion: for repeated calls to localtime, it is best to explicitly call tzset once to consider the time zone change factors. Maybe the glibc library will fix this bug in the future? At least I see this problem in glibc-2.3.5 and 2.4.

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.