Time Zone setting function tzset ()

Source: Internet
Author: User

Tzset

# Incude <time. h>

Void tzset (void );

Set the time environment variable.

Description
The tzset () function uses the current settings of the Environment Variable TZ to assign values to three global variables: daylight, timezone, and tzname.
These variables are used by the ftime and localtime functions to correct Greenwich Mean Time (UTC) to the local time. The time function is used to calculate the UTC time from the system time, And the TZ environment variables are set using the following syntax:

Set TZ = tzn [+ |-] HH [: Mm [: SS] [dzn]

Tzn: Time Zone name with three letters, such as PST. You must specify the offset of the local time from UTC.
Hh: the time difference between UTC and local time. You can select a signed one.
MM: minute, separated by a colon (:) and HH.
SS: seconds, separated by a colon (:) And mm.
Dzn: the time zone, for example, PDT, when the three-letter license is used. If this parameter does not work during compaction, dzn is not set when TZ is set. The C Runtime Library assumes that the United States rules are executed during runtime (DST) computing.

In fact, tzn and dzn can be any three letters, as long as the time difference in the middle is set correctly, localtime () and gmtime () function calls will be handled correctly. Note that, for example, to set the time zone TZ to the UTC + region where China is located, use the following statement if the time zone tz Is not used:

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

Note that it's hard to understand whether it's a GMT-8 instead of GMT + 8, or if it's set to the West eight, it's eight hours slower than UTC.

For example, to set the TZ environment to the current time zone corresponding to Germany, you can use one of the following statements:
Set TZ = GST-1GDT
Set TZ = GST + 1gdt
These strings use GST to point out the German Standard Time. Assume that Germany is one hour ahead of UTC time, and assume that the time period takes effect.
If the TZ value is not set, tzset tries to use the time zone information specified by the operating system. In windwos nt and Windows 95, this information is specified in the date/time application of the control panel, if tzset cannot obtain this information, it uses pst8pdt by default, which is specified as the Pacific Standard Time.

Based on the TZ environment variable value, the following values are assigned to the global variables daylight, timezone, and tzname when tzset is called:
Global variable description Default Value
Daylight: if the time zone 1 is specified in TZ settings, the value is not 0; otherwise, the value is 0.
Time Difference Between timezone UTC and local time, measured in seconds 28800 (28800 seconds equals 8 hours)
Tzname [0] string value of the time zone name of the TZ environment variable. If tz Is not set, it is null PST.
Tzname [1] indicates the string value of the time zone during runtime. If the TZ environment variable ignores the time zone during runtime, the PDT is null. The default values of daylight and tzname arrays in the preceding table correspond to "pst8pdt ".

If the DST time zone is ignored from the TZ environment variable, the daylight value is 0, and the ftime, gmtime, and localtime functions return 0 for their DST flags.
Example

/*
Tzset. C: This program first sets up the time zone *
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 <time. h>
# Include <stdlib. h>
# Include <stdio. h>
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
Time zone = 18000
Tzname [0] = est

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

In-situ conversion: http://hi.baidu.com/peruke/blog/item/b8de06ec6a04583b27979132.html

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.