About time zone issues in Linux

Source: Internet
Author: User
Tags local time

Part of this article is from: http://hi.baidu.com/peruke/blog/item/b8de06ec6a04583b27979132.html

The system is fedora:

GLIBC realizes a conversion from RTC time to human readable time, the general system does not apply environment variable TZ to set the time zone, because there are some flaws in the environment variable, in fedora, the system is the time zone set to time zone file/etc/localtime, this file is a/usr/ share/zoneinfo/a soft connection to the time zone file below the path. The definition of the time zone file is already defined in the GLIBC source code, because there is a separate TimeZone folder in the glibc.

The format of the environment variable TZ and the Tzset () description are as follows:

  

TZ=TZN[+|-]HH[:MM[:SS]][DZN]

eg:tz= "CST-8"

TZN: Three-letter time zone name, such as PST, you must indicate the local time offset from UTC.
HH:UTC and local time difference, optionally with the symbol.
MM: minutes, separated by a colon (:) and HH.
SS: Seconds, separated by a colon (:) and mm.
DZN: Three-letter daylight time zone such as PDT. If daylight saving time does not work, setting TZ does not set the value of DZN. The C runtime assumes that U.S. rules are executed when daylight saving time (DST) is calculated.

In fact TZN and DZN can be any 3 letters, as long as the middle of the time difference is set correctly, LocalTime () and gmtime () function calls will be handled correctly. One thing to note, for example, is to set the time zone TZ to China's east eight (that is, utc+8:00) and not use daylight savings, using the following statement:

Setenv ("TZ", "GMT-8", 1);

It is more puzzling to note that it is GMT-8, not gmt+8, or that it is 8 hours slower than UTC for the West VIII zone.

For example, in order to set the TZ environment for the current time zone for Germany, you can use one of the following statements:
Set TZ=GST-1GDT
Set TZ=GST+1GDT
These strings use GST to indicate German standard time, assuming that Germany is ahead of UTC one hours, and assumes that daylight savings is working.
If the TZ value is not set, Tzset attempts to use the time zone information specified by the operating system, under Windwos NT and WINDOWS95, this information is specified in the Date/time application of the control Panel, and if Tzset cannot obtain this information, it uses PST8PDT by default. It is specified as Pacific Standard Time.

Based on the value of the TZ environment variable, assign the following values to global variables daylight, timezone, and Tzname when calling Tzset:
Global variables     descriptions                                                         Default Value
daylight       if the daylight saving time zone           1 is specified in the TZ setting, the value is not 0; otherwise 0
timezone     the difference between UTC and local time, in seconds    28800 (28,800 seconds equals 8 hours) The string value of the time zone name of the
Tzname[0] TZ environment variable          If TZ is not set then the string value of the empty PST
tzname[1]   Daylight Time zone;                         NULL if the TZ environment variable is omitted in the daylight saving time zone The default values for the daylight and tzname arrays in the table above correspond to "PST8PDT".

If the DST time zone is omitted from the TZ environment variable, the value of daylight is 0,ftime,gmtime and the LocalTime function returns 0 for their DST flag.
Example

/ *
Tzset. C:this program first sets up the time zone by*
Placing the variable named Tz=est5 in the environment*
Table. It then uses Tzset to set the global variables*
Named daylight, timezone, and tzname.*/
#include
#include
#include
void Main (void)
{
if (putenv ("Tz=est5edt") = =-1)
{
printf ("Unable to set tz/n"); exit (1);
}
Else
{
Tzset (); printf ("Daylight =%d\n", daylight);
printf ("timezone =%ld\n", timezone);
printf ("tzname[0] =%s\n", tzname[0]);
}
Exit (0);
}

Output
Daylight = 1
TimeZone = 18000
Tzname[0] = EST

See
Asctime,ftime,gmtime,localtime,time,utime

1, the time is saved in the hardware real-time clock (RTC), the RTC is powered by the motherboard battery, even if the power off source does not cause time loss.

2, the system starts from the RTC to get the time, this step is done in Rc.sysinit:

A, first obtain the RTC related parameter UTC/ARC/SRM from the/etc/sysconfig/clock. UTC is true to indicate that the RTC is saved in UTC time, and False indicates that the local time is saved. Arc and SRM meaning reference Hwclock manpage, general false can be;

b, the parameters of the Hwclock command are constructed according to the content obtained above, and the time is read from RTC to the system clock.

* One point to note is that the output of the Hwclock is local time, and the LOCALTIME/UTC parameter is only used to tell the Hwclock command which time zone the COMs time zone is. For example, the system time zone of Linux system is set to CST (China Standard Time), COMs time using wall time, that is, COMs time zone is CST, if using--UTC parameter, will let 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 the program, it first attempts to get the time zone from the environment variable "TZ", if the TZ environment variable exists, that is, the getenv ("TZ") return value is not NULL, and the containing content is valid, use the TZ value If the variable exists but the content is invalid, such as the time zone name containing the error, UTC is used, and if the variable does not exist, that is, getenv ("TZ") returns NULL, the system time zone settings file is found, typically/etc/localtime. The content format of the file referred to by localtime can be referenced in Tzfile (5).

4,time (): Returns the number of seconds from 00:00:00 UTC, January 1, 1970 to now, note that it is UTC.

5,ctime ()/localtime ()/mktime (): This type of time-zone-related function calls Tzset, but only the first call to Tzset is actually initialized, and subsequent calls are returned directly.

6, when the system shuts down, such as performing shutdown/reboot/poweroff/halt and other operations, the system time will be saved to the RTC, this is done in the halt script, see/etc/init.d/halt content.

About time zone issues in Linux

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.