_tzset

设置时间环境变量。

重要

此 API 不能用于在 Windows 运行时中执行的应用程序。 有关详细信息,请参阅通用 Windows 平台应用中不支持的 CRT 函数

语法

void _tzset( void );

备注

_tzset 函数使用环境变量 TZ 的当前设置将值分配到三个全局变量: _daylight_timezone_tzname_ftimelocaltime 函数使用这些变量以便将协调世界时 (UTC) 更正为本地时间,而 time 函数使用这些变量以从系统时间计算 UTC。 使用以下语法设置 TZ 环境变量:

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

tzn
三个字母时区名称,如 PST。 必须指定从本地时间到 UTC 的正确偏移量。

hh
UTC 和本地时间之间的差异(以小时为单位)。 (+) 号对于正值可选。

mm
分钟。 用冒号 ( hh ) 分开:

ss
秒。 用冒号 ( mm ) 分开:

dzn
三字母夏令时区域,如 PDT。 如果夏令时制从不会在本地生效,则在没有 TZ 的值的情况下设置 dzn。 C 运行时库假设使用美国规则实现夏令时 (DST) 的计算。

注意

在计算时间差异的符号时一定要小心。 由于时间差异是从本地时间到 UTC 间(不是相反)的偏移量,因此其符号可能会与你直觉上判断的符号相反。 对于早于 UTC 的时区,时间差异为负;对于晚于 UTC 的时区,时间差异为正。

例如,若要设置 TZ 环境变量使其对应于德国的当前时区,则在命令行上输入此命令:

set TZ=GST-1GDT

此命令使用 GST 来指示德国标准时间。 它假定 UTC 比德国时间晚一小时(换句话说,德国时间比 UTC 早一小时)。 而且,它假定德国实行夏令时。

如果未设置 TZ 值,则 _tzset 将尝试使用由操作系统指定的时区信息。 在 Windows 操作系统中,此信息在控制面板的日期/时间应用程序中指定。 如果 _tzset 无法获取此信息,则它默认使用代表太平洋时区的 PST8PDT。

基于 TZ 环境变量值,调用 _daylight时将以下值分配给全局变量 _timezone_tzname_tzset

全局变量 说明 默认值
_daylight 如果在 TZ 设置中指定了夏令时区域则为非零值;否则为 0。 1
_timezone UTC 和当地时间之间的差异(以秒为单位)。 28800(28,800 秒等于 8 小时)
_tzname[0] TZ 环境变量中时区名称的字符串值;如果尚未设置 TZ,则为空。 PST
_tzname[1] 夏令时区域的字符串值;如果夏令时区域在 TZ 环境变量中省略则为空。 PDT

前表中显示的 _daylight_tzname 数组默认值对应于“PST8PDT”。如果 DST 区域在 TZ 环境变量中省略,则 _daylight 的值为 0,并且 _ftimegmtimelocaltime 函数将对其 DST 标志返回 0。

默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态

要求

例程 必需的标头
_tzset <time.h>

_tzset 函数特定于 Microsoft。 有关详细信息,请参阅兼容性

示例

// 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

另请参阅

工时管理
asctime_wasctime
_ftime_ftime32_ftime64
gmtime_gmtime32_gmtime64
localtime_localtime32_localtime64
time_time32_time64
_utime_utime32_utime64_wutime_wutime32_wutime64