C参考手册
日期与时间 | Date and time
time
在头文件<time.h>中定义 |
|
|
---|---|---|
time_t time(time_t * arg); |
|
|
返回编码为time_t
对象的当前日历时间,并将其存储在time_t
指向的对象中arg
(除非arg
是空指针)。
参数
ARG |
- |
指向时间将被存储的time_t对象的指针或空指针 |
---|
返回值
当前日历时间编码为time_t
成功时的对象,(time_t)(-1)
错误。如果arg
不是空指针,返回值也存储在指向的对象中arg
。
注释
日历时间的编码time_t
是未指定的,但大多数系统符合POSIX规范,并返回一个整数类型的值,它保存自Epoch以来的秒数。time_t
2038年的实现中有一个32位有符号整数(许多历史实现)失败。
例
#include <stdio.h>
#include <time.h>
#include <stdint.h>
int main(void)
{
time_t result = time(NULL);
if(result != -1)
printf("The current time is %s(%ju seconds since the Epoch)\n",
asctime(gmtime(&result)), (uintmax_t)result);
}
可能的输出:
The current time is Fri Apr 24 15:05:25 2015
(1429887925 seconds since the Epoch)
参考
- C11标准(ISO / IEC 9899:2011):
- 7.27.2.4时间函数(p:391)
- C99标准(ISO / IEC 9899:1999):
- 7.23.2.4时间函数(p:341)
- C89 / C90标准(ISO / IEC 9899:1990):
- 4.12.2.4时间函数