Namespaces
Variants

std:: timespec_get

From cppreference.net
< cpp ‎ | chrono ‎ | c
定义于头文件 <ctime>
int timespec_get ( std:: timespec * ts, int base ) ;
(1) (C++17 起)
#define TIME_UTC /* 由实现定义 */
(2) (C++17 起)
1) 修改 std::timespec 对象所指向的内容,使其持有以时间基准 base 表示的当前日历时间。
2) 扩展为适合用作 std::timespec_get 函数 base 参数的数值。

其他以 TIME_ 开头的宏常量可能由实现提供,用于指示额外的时间基准。

如果 base TIME_UTC ,那么

  • ts - > tv_sec 被设置为自实现定义纪元以来的秒数,截断为整数值,
  • ts - > tv_nsec 成员被设置为纳秒的整数值,舍入到系统时钟的分辨率。

目录

参数

ts - 指向 std::timespec 类型对象的指针
base - TIME_UTC 或其他表示时间基准的非零整数值

返回值

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

注释

POSIX函数 clock_gettime(CLOCK_REALTIME, ts) 也可用于将自纪元以来的时间填充到 std::timespec 结构中。

示例

#include <ctime>
#include <iostream>
int main()
{
    std::timespec ts;
    std::timespec_get(&ts, TIME_UTC);
    char buf[100];
    std::strftime(buf, sizeof buf, "%D %T", std::gmtime(&ts.tv_sec));
    std::cout << "Current time: " << buf << '.' << ts.tv_nsec << " UTC\n";
}

可能的输出:

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

参见

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