_mkgmtime、_mkgmtime32、_mkgmtime64

转换 UTC 时间表示 tm 的 struct 设置为 time_t 类型表示的 UTC 时间。

time_t _mkgmtime( 
   struct tm* timeptr 
); 
__time32_t _mkgmtime32( 
   struct tm* timeptr 
); 
__time64_t _mkgmtime64( 
   struct tm* timeptr 
);

参数

  • timeptr
    对 UTC 时间的指针为 struct tm 的转换。

返回值

个类型表示秒数 __time32_t__time64_t 自午夜,1970 年 1 月 1 日。之后,中协调世界时 (UTC) (UTC)。 如果日期超出范围 (请参见"备注"节) 或输入无法被解释为一个有效时间,返回值有 - 1。

备注

_mkgmtime32_mkgmtime64 函数将 UTC 时间转换为与在表示 UTC 的 __time32_t__time64_t 类型。 若要将本地时间转换为 UTC 时间,请使用 mktime_mktime32_mktime64

_mkgmtime 是个内联函数,计算结果为 _mkgmtime64,并且,time_t__time64_t等效。 如果你需要强制编译器将 time_t 编译为旧 的32位 time_t,你可以定义**_USE_32BIT_TIME_T**。 建议不要这样做,因为应用程序可能在 2038 年 1 月 19 日之后失败 (32 位 time_t的最大大小),因此,不允许安装在 64 位平台。

传递的时结构如下更改,与这些更改与 _mktime 函数相同的方式:tm_wdaytm_yday 字段使用设置为基于值的新值为 tm_mdaytm_year。 当指定 tm 结构时,将 tm_isdst 字段设置为:

  • 零 (0) 表示标准时间有效。

  • 大于 0 的值表示夏令时间有效。

  • 小于零的值具有 C 运行库代码来评估标准时间或夏时制时间是否有效。

C 运行库 TZ 使用环境变量以确定正确的夏时制。 如果未设置,TZ 操作系统查询获取正确的区域夏时制行为。 缺少必需的 tm_isdst。 如果未设置,其值是未定义的,并从这些函返回的mktime值是不可预知的。

_mkgmtime32 函数的范围是从 1970 年 1 月 1 日午夜,到 3:14 的 1 月 19 日,UTC: 07 天 2038 年,UTC。 范围 _mkgmtime64 来自午夜,1970 年 1 月 1 日到的 UTC: 23:59,59,3000 年 12 月 31 日,UTC。 一个超出范围的结果日期在返回值 - 1。 范围取决于是否定义了 _USE_32BIT_TIME_T _mkgmtime 如果未定义 (默认) 范围是 _mkgmtime64;否则,范围限于 32 位范围 _mkgmtime32

gmtimelocaltime 转换使用单个静态分配的缓冲区。 如果提供缓冲区给 mkgmtime、_mktime32 或 ,则之前的内容将被破坏。

示例

// crt_mkgmtime.c
#include <stdio.h>
#include <time.h>

int main()
{
    struct tm t1, t2;
    time_t now, mytime, gmtime;
    char buff[30];

    time( & now );

    _localtime64_s( &t1, &now );
    _gmtime64_s( &t2, &now );

    mytime = mktime(&t1);
    gmtime = _mkgmtime(&t2);

    printf("Seconds since midnight, January 1, 1970\n");
    printf("My time: %I64d\nGM time (UTC): %I64d\n\n", mytime, gmtime);

    /* Use asctime_s to display these times. */

    _localtime64_s( &t1, &mytime );
    asctime_s( buff, sizeof(buff), &t1 );
    printf( "Local Time: %s\n", buff );

    _gmtime64_s( &t2, &gmtime );
    asctime_s( buff, sizeof(buff), &t2 );
    printf( "Greenwich Mean Time: %s\n", buff );

}

示例输出

Seconds since midnight, January 1, 1970
My time: 1171588492
GM time (UTC): 1171588492

Local Time: Thu Feb 15 17:14:52 2007

Greenwich Mean Time: Fri Feb 16 01:14:52 2007

下面的示例演示如何在结构未完成与周和年的日的计算值。

// crt_mkgmtime2.c
#include <stdio.h>
#include <time.h>
#include <memory.h>

int main()
{
    struct tm t1, t2;
    time_t gmtime;
    char buff[30];

    memset(&t1, 0, sizeof(struct tm));
    memset(&t2, 0, sizeof(struct tm));

    t1.tm_mon = 1;
    t1.tm_isdst = 0;
    t1.tm_year = 103;
    t1.tm_mday = 12;

    // The day of the week and year will be incorrect in the output here.
    asctime_s( buff, sizeof(buff), &t1);
    printf("Before calling _mkgmtime, t1 = %s t.tm_yday = %d\n",
            buff, t1.tm_yday );

    gmtime = _mkgmtime(&t1);

    // The correct day of the week and year were determined.
    asctime_s( buff, sizeof(buff), &t1);
    printf("After calling _mkgmtime, t1 = %s t.tm_yday = %d\n",
            buff, t1.tm_yday );

}

Output

Before calling _mkgmtime, t1 = Sun Feb 12 00:00:00 2003
 t.tm_yday = 0
After calling _mkgmtime, t1 = Wed Feb 12 00:00:00 2003
 t.tm_yday = 42

请参见

参考

时间管理

asctime、_wasctime

asctime_s、_wasctime_s

gmtime、_gmtime32、_gmtime64

gmtime_s、_gmtime32_s、_gmtime64_s

localtime_s、_localtime32_s、_localtime64_s

mktime、_mktime32、_mktime64

time、_time32、_time64