C++ 参考手册

定义于头文件 <ctime>
int timespec_get( std::timespec* ts, int base)
(C++17 起)
#define TIME_UTC /* implementation-defined */
(C++17 起)
1) 修改 ts 所指向的 std::timespec 对象,以保有时间基底 base 中的当前日历时间。
2) 展开成适合用作 std::timespec_getbase 参数的值。

实现可提供其他以 TIME_ 起始的宏常量,以指示另外的时间基底。

baseTIME_UTC ,则

  • ts->tv_sec 被设为从实现定义的纪元开始的秒数,截断到整数值
  • ts->tv_nsec 成员被设为纳秒的整数,取整到系统时钟的分辨率

参数

ts - 指向 std::timespec 类型对象的指针
base - TIME_UTC 或另一指示时间基底的非零整数值

返回值

若成功则为 base 的值,否则为零。

注意

POSIX 函数 clock_gettime(CLOCK_REALTIME, ts) 亦可用于将从纪元开始的时间植入 std::timespec

示例

#include <cstdio>
#include <ctime>
 
int main()
{
    std::timespec ts;
    std::timespec_get(&ts, TIME_UTC);
    char buff[100];
    std::strftime(buff, sizeof buff, "%D %T", std::gmtime(&ts.tv_sec));
    std::printf("Current time: %s.%09ld UTC\n", buff, ts.tv_nsec);
}

可能的输出:

Current time: 06/24/16 20:07:42.949494132 UTC

参阅

(C++17)
以秒和纳秒表示的时间
(结构体)
返回自纪元起计的系统当前时间
(函数)