How to Get Time in Milliseconds in C++

  1. Use the std::chrono::system_clock::now() Method to Get Time in Milliseconds in C++
  2. Use the gettimeofday() Function to Get Time in Milliseconds in C++
  3. Use the time() Function to Get Time in Milliseconds in C++

This article will introduce multiple C++ methods on how to get time in milliseconds.

Use the std::chrono::system_clock::now() Method to Get Time in Milliseconds in C++

The std::chrono::system_clock class is the interface in C++ to get system-wide real-time wall clock. Most systems use Unix time, which is represented as seconds past from 00:00:00 UTC on 1 January 1970 (an arbitrary date), called Unix epoch. Note that leap seconds are ignored. Thus Unix time is not truly an accurate representation of UTC.

Firstly, the now() method is called to return the current point in time. The next method called is time_since_epoch to retrieve the amount of time between *this and the clock’s epoch, but it returns an std::chrono::duration class object. This object should call the count method to return the actual number of ticks, and to represent it as milliseconds. The result is cast using duration_cast<milliseconds>.

#include <chrono>
#include <iostream>
#include <sys/time.h>
#include <ctime>

using std::cout; using std::endl;
using std::chrono::duration_cast;
using std::chrono::milliseconds;
using std::chrono::seconds;
using std::chrono::system_clock;

int main() {
    auto millisec_since_epoch = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
    auto sec_since_epoch = duration_cast<seconds>(system_clock::now().time_since_epoch()).count();

    cout << "seconds since epoch: " << sec_since_epoch << endl;
    cout << "milliseconds since epoch: " << millisec_since_epoch << endl;

    return EXIT_SUCCESS;
}

Output:

seconds since epoch: 1603612778
milliseconds since epoch: 1603612778389

Use the gettimeofday() Function to Get Time in Milliseconds in C++

gettimeofday is the POSIX compliant function to retrieve the system clock. It takes an address of struct timeval object as the first argument to store time values. The values are tv_sec representing the number of seconds and tv_usec as microseconds passed since Unix epoch. gettimeofday returns int value 0 for success and -1 for failure to provide error handling facilities. The second argument of the function is struct timezone, but since it’s been depreciated, you should just pass a nullptr. Note that you would need to include <sys/time.h> header for the function definition.

#include <chrono>
#include <iostream>
#include <sys/time.h>
#include <ctime>

using std::cout; using std::endl;

int main() {
    struct timeval time_now{};
    gettimeofday(&time_now, nullptr);
    time_t msecs_time = (time_now.tv_sec * 1000) + (time_now.tv_usec / 1000);

    cout << "seconds since epoch: " << time_now.tv_sec << endl;
    cout << "milliseconds since epoch: "  << msecs_time << endl << endl;

    return EXIT_SUCCESS;
}

Output:

seconds since epoch: 1603612778
milliseconds since epoch: 1603612778389

Use the time() Function to Get Time in Milliseconds in C++

Another POSIX compliant method to retrieve system time in C++ is to call the time function. time takes an optional argument of type time_t*, where the returned time value is stored. Alternatively, we can use the function return value to store in the separately declared variable. In the latter case, you may pass nullptr as the argument. Notice that this call doesn’t return time in the same precision as the previous calls.

#include <chrono>
#include <iostream>
#include <sys/time.h>
#include <ctime>

using std::cout; using std::endl;

int main() {
    time_t now = time(nullptr);
    time_t mnow = now * 1000;

    cout << "seconds since epoch: " << now << endl;
    cout << "milliseconds since epoch: " << mnow << endl << endl;

    return EXIT_SUCCESS;
}

Output:

seconds since epoch: 1603612778
milliseconds since epoch: 1603612778000