3

C++ 中获取纳秒精度的时间

 3 years ago
source link: https://zhiqiang.org/coding/cpp-get-time-in-nanoseconds.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

C++ 中获取纳秒精度的时间

作者: 张志强

, 发表于 2020-01-06

, 共 398 字 , 共阅读 496 次

主要函数是timespec_get,可参考https://zh.cppreference.com/w/c/chrono/timespec_get

#include <time.h>

std::string time_in_nanoseconds()
{
    struct timespec ts;
    timespec_get(&ts, TIME_UTC);

    static char buff[64];
    int end = strftime(buff, sizeof buff, "%Y-%m-%d %H:%M:%S", gmtime(&ts.tv_sec));
    sprintf(buff + end, " %09ld", ts.tv_nsec);

    return buff;
}

Q. E. D.

avatar-0.jpg

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK