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

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?

Answers


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.


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

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


Need Your Help

Auto-versioning of static content with JBoss

tomcat caching jboss auto-versioning

As per the Q&amp;A here, I'd like to implement a similar auto-versioning system for a web app running in JBoss 5. Is there anything already out there to do this sort of thing, or will I need to write

WPF: capturing XAML into subclassed control

wpf xaml

i narrowed down what i want my wpf button to look like using XAML.