std::timespec_get

定义于头文件 <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 - 指向 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 起)
以秒和纳秒表示的时间
(结构体)
将系统当前时间作为纪元起时间返回
(函数)
timespec_getC 文档

版本历史

  • (当前 | 先前) 2017年9月22日 (五) 03:17Fruderica讨论 | 贡献. . (1,761字节) (+1,761). . (以“{{cpp/title|timespec_get}} {{cpp/chrono/c/navbar}} {{dcl begin}} {{dcl header| ctime}} {{dcl | since=c++17| int timespec_get( std::timespec* ts, int base) }} {{dcl |...”为内容创建页面)