_tzset_tzset

设置时间环境变量。Sets time environment variables.

重要

此 API 不能用于在 Windows 运行时中执行的应用程序。This API cannot be used in applications that execute in the Windows Runtime. 有关详细信息,请参阅通用 Windows 平台应用中不支持的 CRT 函数For more information, see CRT functions not supported in Universal Windows Platform apps.

语法Syntax

void _tzset( void );  

备注Remarks

_tzset 函数使用环境变量 TZ 的当前设置将值分配到三个全局变量: _daylight_timezone_tznameThe _tzset function uses the current setting of the environment variable TZ to assign values to three global variables: _daylight, _timezone, and _tzname. _ftimelocaltime 函数使用这些变量以便将协调世界时 (UTC) 更正为本地时间,而 time 函数使用这些变量以从系统时间计算 UTC。These variables are used by the _ftime and localtime functions to make corrections from coordinated universal time (UTC) to local time, and by the time function to compute UTC from system time. 使用以下语法设置 TZ 环境变量:Use the following syntax to set the TZ environment variable:

set TZ=tzn[+ | -]hh[:mm[:ss] ][dzn]set TZ=tzn[+ | -]hh[:mm[:ss] ][dzn]

tzn
三个字母时区名称,如 PST。Three-letter time-zone name, such as PST. 必须指定从本地时间到 UTC 的正确偏移量。You must specify the correct offset from local time to UTC.

hh
UTC 和本地时间之间的差异(以小时为单位)。Difference in hours between UTC and local time. (+) 号对于正值可选。Sign (+) optional for positive values.

mm
分钟。Minutes. 用冒号 ( hh ) 分开:Separated from hh by a colon (:).

ss
秒。Seconds. 用冒号 ( mm ) 分开:Separated from mm by a colon (:).

dzn
三字母夏令时区域,如 PDT。Three-letter daylight-saving-time zone such as PDT. 如果夏令时制从不会在本地生效,则在没有 TZ 的值的情况下设置 dznIf daylight saving time is never in effect in the locality, set TZ without a value for dzn. C 运行时库假设使用美国规则实现夏令时 (DST) 的计算。The C run-time library assumes the United States' rules for implementing the calculation of daylight saving time (DST).

备注

在计算时间差异的符号时一定要小心。Take care in computing the sign of the time difference. 由于时间差异是从本地时间到 UTC 间(不是相反)的偏移量,因此其符号可能会与你直觉上判断的符号相反。Because the time difference is the offset from local time to UTC (rather than the reverse), its sign may be the opposite of what you might intuitively expect. 对于早于 UTC 的时区,时间差异为负;对于晚于 UTC 的时区,时间差异为正。For time zones ahead of UTC, the time difference is negative; for those behind UTC, the difference is positive.

例如,若要设置 TZ 环境变量使其对应于德国的当前时区,则在命令行上输入以下内容:For example, to set the TZ environment variable to correspond to the current time zone in Germany, enter the following on the command line:

set TZ=GST-1GDT  

此命令使用 GST 以指示德国标准时间,假定 UTC 晚于德国时间一小时(即德国时间早于 UTC 一小时),并假定德国遵循夏令时制。This command uses GST to indicate German standard time, assumes that UTC is one hour behind Germany (or in other words, that Germany is one hour ahead of UTC), and assumes that Germany observes daylight-saving time.

如果TZ未设置值,_tzset尝试使用由操作系统指定的时区信息。If the TZ value is not set, _tzset attempts to use the time zone information specified by the operating system. 在 Windows 操作系统中,此信息在控制面板的日期/时间应用程序中指定。In the Windows operating system, this information is specified in the Date/Time application in Control Panel. 如果 _tzset 无法获取此信息,则它默认使用代表太平洋时区的 PST8PDT。If _tzset cannot obtain this information, it uses PST8PDT by default, which signifies the Pacific Time zone.

基于 TZ 环境变量值,调用 _daylight时将以下值分配给全局变量 _timezone_tzname_tzsetBased 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 如果在 TZ 设置中指定了夏令时区域则为非零值;否则为 0。Nonzero value if a daylight-saving-time zone is specified in TZ setting; otherwise, 0. 11
_timezone UTC 和当地时间之间的差异(以秒为单位)。Difference in seconds between local time and UTC. 28800(28800 秒等于 8 小时)28800 (28800 seconds equals 8 hours)
_tzname[0]_tzname[0] TZ 环境变量中时区名称的字符串值;如果尚未设置 TZ ,则为空。String value of time-zone name from TZ environmental variable; empty if TZ has not been set. PSTPST
_tzname[1]_tzname[1] 夏令时区域的字符串值;如果夏令时区域在 TZ 环境变量中省略则为空。String value of daylight-saving-time zone; empty if daylight-saving-time zone is omitted from TZ environmental variable. PDTPDT

前表中显示的 _daylight_tzname 数组默认值对应于“PST8PDT”。The default values shown in the preceding table for _daylight and the _tzname array correspond to "PST8PDT." 如果 DST 区域在 TZ 环境变量中省略,则 _daylight 的值为 0,并且 _ftimegmtimelocaltime 函数将对其 DST 标志返回 0。If the DST zone is omitted from the TZ environmental variable, the value of _daylight is 0 and the _ftime, gmtime, and localtime functions return 0 for their DST flags.

惠?Requirements

例程所返回的值Routine 必需的标头Required header
_tzset <time.h><time.h>

有关详细信息,请参阅 兼容性For more information, see Compatibility.

示例Example

// crt_tzset.cpp  
// This program uses _tzset to set the global variables  
// named _daylight, _timezone, and _tzname. Since TZ is  
// not being explicitly set, it uses the system time.  

#include <time.h>  
#include <stdlib.h>  
#include <stdio.h>  

int main( void )  
{  
    _tzset();  
    int daylight;  
    _get_daylight( &daylight );  
    printf( "_daylight = %d\n", daylight );  
    long timezone;  
    _get_timezone( &timezone );  
    printf( "_timezone = %ld\n", timezone );  
    size_t s;  
    char tzname[100];  
    _get_tzname( &s, tzname, sizeof(tzname), 0 );  
    printf( "_tzname[0] = %s\n", tzname );  
    exit( 0 );  
}  
_daylight = 1  
_timezone = 28800  
_tzname[0] = Pacific Standard Time  

请参阅See Also

时间管理 Time Management
asctime、_wasctime asctime, _wasctime
_ftime、_ftime32、_ftime64 _ftime, _ftime32, _ftime64
gmtime、_gmtime32、_gmtime64 gmtime, _gmtime32, _gmtime64
localtime、_localtime32、_localtime64 localtime, _localtime32, _localtime64
time、_time32、_time64 time, _time32, _time64
_utime、_utime32、_utime64、_wutime、_wutime32、_wutime64_utime, _utime32, _utime64, _wutime, _wutime32, _wutime64