Capturing a time in milliseconds

前端 未结 7 1069
一生所求
一生所求 2020-11-30 07:11

The following piece of code is used to print the time in the logs:

#define PRINTTIME() struct tm  * tmptime;
time_t     tmpGetTime;
time(&tmpGetTime);
tm         


        
相关标签:
7条回答
  • 2020-11-30 07:37

    In Ubuntu 16.04 this worked for me...

    const std::string currentDateTime() {
       char            fmt[64], buf[64];
       struct timeval  tv;
       struct tm       *tm;
    
       gettimeofday(&tv, NULL);
       tm = localtime(&tv.tv_sec);
       strftime(fmt, sizeof fmt, "%Y-%m-%d %H:%M:%S.%%06u", tm);
       snprintf(buf, sizeof buf, fmt, tv.tv_usec);
       return buf;
    }
    

    Then, with...

    std::cout << currentDateTime();
    

    I get...

    2016-12-29 11:09:55.331008
    
    0 讨论(0)
提交回复
热议问题