time() 函数定义在 time.h 头文件中。它是一个实用函数,有助于测量经过的时间。此函数返回自 Epoch(世界标准时间 1970 年 1 月 1 日 00:00:00)以来经过的秒数。
time_t time(time_t *second); #where second should be a pointer
time() 函数接受一个参数 'seconds',它有助于设置 time_t 对象来存储时间。
| 参数 | 描述 | 必需/可选 |
|---|---|---|
| seconds | 指向 time_t 类型对象的指针,秒值将存储在此处。 | 必需 |
该函数返回当前日历时间作为 time_t 对象。如果参数 seconds 为 NULL 指针,则返回值不存储在任何地方,否则,返回值存储在 seconds 指向的对象中。
| 输入 | 返回值 |
|---|---|
| 如果参数 | 当前日历时间 |
#include <stdio.h>
#include <time.h>
int main()
{
time_t sec;
sec = time(NULL);
printf("The hours since January 1, 1970 = %ld\n", sec/3600);
return(0);
}
输出
The hours since January 1, 1970 = 393923
#include <stdio.h>
#include <time.h>
int main (){
time_t secnd;
secnd = time(NULL);
printf("The seconds since January 1, 1970 = %ld\n", secnd);
return(0);
}
输出
The seconds since January 1, 1970 = 1538123990