C++获取毫秒级时间

3

我想获取包含六位数字的秒数的日期和时间,例如2017-07-03 15:01:01.123456。如何获得?我试过用time、timeval和gettimeofday,但都报错了。

time_t timer;
char timestr[29];
struct timeval *tm_info;

time(&timer);

tm_info = gettimeofday(&timer);

strftime(timestr, 29, "%Y-%m-%d %H:%M:%S", tm_info);

ss << timestr;

什么是错误?ss是什么? - 463035818_is_not_a_number
3
你能使用C++11的std::chrono吗? - YSC
1
请查看Howard Hinnant在此处的回答中的库。 - Vishaal Shankar
ss 是字符串流。 - Yoekleng Kuy
无法将'time_t* {aka long int*}'转换为' timeval ',用于参数'1'到'int gettimeofday(timeval, __timezone_ptr_t)'。 tm_info = gettimeofday(&timer);错误:无法将'timeval '转换为'const tm ',用于参数'4'到'size_t strftime(char,size_t,const char,const tm*)' strftime(timestr, 29, "%Y-%m-%d %H:%M:%S", tm_info); - Yoekleng Kuy
显示剩余4条评论
2个回答

2
首先,如果您想在当前秒上获得六位小数的分辨率,我们谈论的是微秒(1us = 10E-6),而不是毫秒(1ms = 10E-3)。此外,您应该使用std::chrono,因为gettimeofday()是特定于Linux的操作系统,并且在其他系统上无法工作。
要获取日期,您可以使用<time.h>及其gmtime()(UTC),它返回一个struct tm。至于时间,一个简单的方法是利用time_t仅具有1秒分辨率的事实,在当前秒之后经过的时间以微秒为单位计算(例如14:31:09.590721)。
#include <iostream>
#include <string>
#include <chrono>
#include <time.h>

int main() {
    for (int i = 0; i < 1000; ++i) {
        auto now = std::chrono::system_clock::now();
        auto now_t = std::chrono::system_clock::to_time_t(now);

        //Exploit now_t (time_t): resolution is only 1 second - get microseconds
        auto dur = now - std::chrono::system_clock::from_time_t(now_t);
        auto micros = std::chrono::duration_cast<std::chrono::microseconds>(dur).count();


        //Print the date and time as yyyy-mm-dd hh::mm::ss.xxxxxx
        tm& now_tm = *(gmtime(&now_t));
        std::string year = std::to_string(now_tm.tm_year + 1900) + "-";
        std::string mon = std::to_string(now_tm.tm_mon + 1) + "-";
        std::string day = std::to_string(now_tm.tm_mday) + " ";
        std::string hour = std::to_string(now_tm.tm_hour);
        std::string min = std::to_string(now_tm.tm_min);
        std::string sec = std::to_string(now_tm.tm_sec);

        if (hour.size() == 1) hour.insert(0, "0");
        if (min.size() == 1) min.insert(0, "0");
        if (sec.size() == 1) sec.insert(0, "0");

        std::cout << year << mon << day
            << hour << ":" << min << ":" << sec << "." << micros << "\n";
    }
    return 1;
}

在我的时区运行10次迭代,结果如下:
2018-5-4 12:36:11.47578
2018-5-4 12:36:11.48663
2018-5-4 12:36:11.49516
2018-5-4 12:36:11.50392
2018-5-4 12:36:11.51261
2018-5-4 12:36:11.52455
2018-5-4 12:36:11.53316
2018-5-4 12:36:11.54213
2018-5-4 12:36:11.55113
2018-5-4 12:36:11.55969

PS:当你处理这种粒度的内容时,你应该知道时钟源的准确性和漂移。这取决于系统(硬件、操作系统等)。严格的时间保留需要特殊的硬件(例如GPSDO)和软件。


1

使用<chrono>Howard Hinnant的日期/时间库是更简单的方法。以下代码会以微秒精度打印出当前UTC时间10次:

#include "date/date.h"
#include <iostream>

int
main()
{
    using namespace std::chrono;
    using namespace date;
    for (auto i = 0; i < 10; ++i)
        std::cout << floor<microseconds>(system_clock::now()) << '\n';
}

示例输出:

2018-05-04 14:01:04.473663
2018-05-04 14:01:04.473770
2018-05-04 14:01:04.473787
2018-05-04 14:01:04.473805
2018-05-04 14:01:04.473824
2018-05-04 14:01:04.473842
2018-05-04 14:01:04.473860
2018-05-04 14:01:04.473878
2018-05-04 14:01:04.473895
2018-05-04 14:01:04.473914

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接