设置时间环境变量。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.


void _tzset( void );


_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) 进行更正时间函数从系统时间计算 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:

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

参数Parameter 描述Description
tzntzn 三个字母时区名称,如 PST。Three-letter time-zone name, such as PST. 必须指定从本地时间到 UTC 的正确偏移量。You must specify the correct offset from local time to UTC.
hhhh UTC 和本地时间之间的差异(以小时为单位)。Difference in hours between UTC and local time. (+) 号对于正值可选。Sign (+) optional for positive values.
mmmm 分钟。Minutes. 分开hh用冒号 (:)。Separated from hh by a colon (:).
ssss 秒。Seconds. 分开mm用冒号 (:)。Separated from mm by a colon (:).
dzndzn 三字母夏令时区域,如 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:


此命令使用 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_tzset调用: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_daylight 如果夏令时的时区中指定的非零值TZ设置; 否则为 0。Nonzero value if a daylight-saving-time zone is specified in TZ setting; otherwise, 0. 11
_timezone_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 和_ftimegmtime,和localtime函数返回对其 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.


例程Routine 必需的标头Required header
_tzset_tzset <time.h><time.h>

_Tzset特定于 Microsoft 的技术支持部门。The _tzset function is Microsoft-specific. 有关详细信息,请参阅 兼容性For more information, see Compatibility.


// 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 )
    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、_wasctimeasctime, _wasctime
_ftime, _ftime32, _ftime64_ftime, _ftime32, _ftime64
gmtime、_gmtime32、_gmtime64gmtime, _gmtime32, _gmtime64
localtime、_localtime32、_localtime64localtime, _localtime32, _localtime64
time、_time32、_time64time, _time32, _time64
_utime、_utime32、_utime64、_wutime、_wutime32、_wutime64_utime, _utime32, _utime64, _wutime, _wutime32, _wutime64