How can I get current time of day in milliseconds in C++?

帅比萌擦擦* 提交于 2020-04-30 06:49:25

问题


The thing is, I have to somehow get current time of day in milliseconds in convenient format.

Example of desired output:

21 h 04 min 12 s 512 ms

I know how to get this format in seconds, but I have no idea how to get my hands on milliseconds?


回答1:


Using the portable std::chrono

auto now = std::chrono::system_clock::now();
auto time = std::chrono::system_clock::to_time_t(now);
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) -
          std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch());

std::cout << std::put_time(std::localtime(&time), "%H h %M m %S s ");
std::cout << ms.count() << " ms" << std::endl;

Output:

21 h 24 m 22 s 428 ms

Live example


Note for systems with clocks that doesn't support millisecond resolution

As pointed out by @user4581301, on some systems std::system_clock might not have enough resolution for accurately representing current time in milliseconds. If that is the case, try using std::high_resolution_clock for calculating the number of milliseconds since the last second. This will ensure the highest available resolution provided by your implementation.

Taking the time from two clocks will inevitably lead you to get two separate points in time (however small the time difference will be). So keep in mind that using a separate clock for calculating the milliseconds will not yield perfect synchronization between the second, and millisecond periods.

// Use system clock for time.
auto now = std::chrono::system_clock::now();

/* A small amount of time passes between storing the time points. */

// Use separate high resolution clock for calculating milliseconds.
auto hnow = std::chrono::high_resolution_clock::now();
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(hnow.time_since_epoch()) -
          std::chrono::duration_cast<std::chrono::seconds>(hnow.time_since_epoch());

Also, there seems to be no guarantee that the tick events of std::high_resolution_clock and std::system_clock are synchronized, and because of this the millisecond period might not be in sync with the periodic update of the current second given by the system clock.

Because of these reasons, using a separate high resolution clock for millisecond resolution should not be used when <1 second precision is critical.




回答2:


With the exception of using boost::chrono, I am not aware of any system independent method. I have implemented the following for windows and posix:

   LgrDate LgrDate::gmt()
   {
      LgrDate rtn;
#ifdef _WIN32
      SYSTEMTIME sys;
      GetSystemTime(&sys);
      rtn.setDate(
         sys.wYear,
         sys.wMonth,
         sys.wDay);
      rtn.setTime(
         sys.wHour,
         sys.wMinute,
         sys.wSecond,
         sys.wMilliseconds*uint4(nsecPerMSec));
#else
      struct timeval time_of_day;
      struct tm broken_down;
      gettimeofday(&time_of_day,0);
      gmtime_r(
         &time_of_day.tv_sec,
         &broken_down);
      rtn.setDate(
         broken_down.tm_year + 1900,
         broken_down.tm_mon + 1,
         broken_down.tm_mday);
      rtn.setTime(
         broken_down.tm_hour,
         broken_down.tm_min,
         broken_down.tm_sec,
         time_of_day.tv_usec * nsecPerUSec);
#endif
      return rtn;
   } // gmt



回答3:


On a POSIX system I would do

#include <sys/time.h>
#include <sys/resource.h>

struct timespec tspec;
clock_gettime(CLOCK_REALTIME, &tspec);
int sec = (int) tspec.tv_sec;
int msec = (int) ((double) tspec.tv_nsec) / 1000000.0;

Note, CLOCK_REALTIME is used to get the wall clock, which is adjusted using NTP

and then use whatever you have for the h:m:s part



来源:https://stackoverflow.com/questions/32873659/how-can-i-get-current-time-of-day-in-milliseconds-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!