gmtime、_gmtime32、_gmtime64
时间值转换为结构。 有关这些函数的更多安全版本,请参见 gmtime_s、_gmtime32_s、_gmtime64_s。
struct tm *gmtime(
const time_t *timer
);
struct tm *_gmtime32(
const __time32_t *timer
);
struct tm *_gmtime64(
const __time64_t *timer
);
参数
- timer
指向存储的时间。 该时间表示为自1970年1月1号午夜(00:00:00)开始的秒数,世界标准时间(UTC)。
返回值
一个指向 TM类型的结构的指针。 返回的结构的字段表示按照 UTC timer 参数的计算的值而不是按本地时间。 类型 int的每个结构字段,如下所示:
tm_sec
分后的秒 (0 – 59)。tm_min
时后的分 (0 – 59)。tm_hour
午夜后的时 (0 – 23)。tm_mday
一月中的一天(1 – 31)。tm_mon
月份 (0 – 11;一月 = 0)。tm_year
年份 (当前年份减 1900)。tm_wday
一周中的一天 (0 – 6;周日 = 0)。tm_yday
一年中的一天(0 – 365; 1 月 1 日 = 0 )。tm_isdst
对于 gmtime,总是为0。
对于gmtime、mktime、mkgmtime和 localtime 所有的 32 位和 64 位版本的每个转换线程都使用同一个通用 tm 结构。 每次调用这些函数之一就要销毁所有以前调用的结果。 如果 timer 表示午夜之前的日期,1970 年 1 月 1 日,gmtime 返回 NULL。 无错误返回。
_gmtime64,使用 __time64_t 结构,启用到达3000年12月31号 23:59:59 的日期,UTC,而 _gmtime32 启用到达2038年1月19号 03:14:07 的日期,UTC。 1970 年 1 月 1 日 00:00:00,是这两个函数的下限的日期范围。
gmtime 是计算 _gmtime64 的内联函数,并且time_t 与 __time64_t 等效,除非 _USE_32BIT_TIME_T 被定义。 如果必须按旧 32 位 time_t 强制编译器解释 time_t,可以定义 _USE_32BIT_TIME_T,但是,这样做会导致 gmtime 内联到 _gmtime32并 time_t 被定义为 __time32_t。 我们建议您不要这样做,因为它在 64 位平台不被允许,任何应用程序可以在 2038 年 1 月 18 日之后可能失败。
这些函数验证其参数。 如果 timer 是无效的指针,或者时间值是负的,则这些函数将调用无效参数处理程序,如 参数验证 中所述。 如果允许执行继续,则函数返回 NULL 并设置 errno 为 EINVAL。
备注
_gmtime32 函数分解 timer 并存储在 tm 类型的一个静态分配结构,定义在 TIME.H。 通常是从一个调用到 time 函数获得 timer 的值。
备注
在大多数情况下,目标环境尝试确定夏时制时间是否有效。C 运行库假设使用实现的夏时制时间 (DST) 的美国规则是有用的。
要求
例程 |
必需的标头 |
---|---|
gmtime |
<time.h> |
_gmtime32 |
<time.h> |
_gmtime64 |
<time.h> |
有关其他兼容性信息,请参见兼容性。
示例
// crt_gmtime.c
// compile with: /W3
// This program uses _gmtime64 to convert a long-
// integer representation of coordinated universal time
// to a structure named newtime, then uses asctime to
// convert this structure to an output string.
#include <time.h>
#include <stdio.h>
int main( void )
{
struct tm *newtime;
__int64 ltime;
char buff[80];
_time64( <ime );
// Obtain coordinated universal time:
newtime = _gmtime64( <ime ); // C4996
// Note: _gmtime64 is deprecated; consider using _gmtime64_s
asctime_s( buff, sizeof(buff), newtime );
printf( "Coordinated universal time is %s\n", buff );
}
.NET Framework 等效项
请参见
参考
ctime、_ctime32、_ctime64、_wctime、_wctime32、_wctime64
gmtime_s、_gmtime32_s、_gmtime64_s
localtime、_localtime32、_localtime64