C 标准库 - <time.h>

C 语言 time() 函数用于返回当前的日历时间(自 1970-01-01 00:00:00 UTC 起的秒数)。

原型

time_t time(time_t *arg);

参数可为 NULL;也常用作随机种子 srand(time(NULL))。

示例

#include <stdio.h>
#include <time.h>

int main() {
    time_t t = time(NULL);
    printf("当前时间戳: %ld\n", (long)t);
    return 0;
}

输出:

当前时间戳: 1727948143

注意事项

返回值依赖系统时钟。转成可读字符串用 ctime 或 strftime。