. Advertisement .
..3..
. Advertisement .
..4..
There are multiple ways to get time in milliseconds in C++, and they all use only standard libraries. Read on to find out how they work.
Get Time In Milliseconds In C++
About Time Measurement In C++
Like C, the standard library of C++ retains some basic time facilities that have been around for decades. It provides a clock for the current time, the duration between two points, and the concept of a point in time.
The last notion is represented in each computer system by its epoch: the timestamp from which the system measures time. Most operating systems measure this duration in seconds. For example, Unix and Linux systems measure time from the Unix epoch (Thursday 1 January 1970 00:00:00). Meanwhile, Windows systems mark 1 January 1601 00:00:00 as their epoch, measuring time from that point as 100-nanosecond intervals.
std::chrono::system_clock
While std::time provides simple C-style date and time functions, you will need to use the chrono library to display time in milliseconds. You will need C++11 to take advantage of this library.
It supports all three main types of time notions (durations, time points, and clocks) with various levels of precisions:
- Durations: measured in time spans like two minutes, a hundred milliseconds, or one hour. Durations are presented with the duration class in the chrono library. This class template couples a period precision and a count representation.
- Time points: this concept refers to a certain point in time, such as someone’s birthday or the departure time of a train. They are represented by the time_point class template. The chrono library expresses a time point as the duration from the epoch.
- Clocks: There are three clocks in the chrono
library: system_clock
,steady_clock
, andhigh_resolution_clock
.
You will need to use the std::chrono::system_clock
class to represent your system time. It is chrono’s only clock that you can map to C-style time points.
To convert a duration to milliseconds, you will need the duration_cast class. It converts a std::chrono::duration
to another type.
This is how you can print your current timestamp in milliseconds with the chrono library:
#include <chrono>
#include <iomanip>
#include <iostream>
int main() {
const auto now = std::chrono::system_clock::now();
const auto nowAsTimeT = std::chrono::system_clock::to_time_t(now);
const auto nowMs = std::chrono::duration_cast<std::chrono::milliseconds>(
now.time_since_epoch()) % 1000;
std::stringstream nowSs;
nowSs
<< std::put_time(std::localtime(&nowAsTimeT), "%a %b %d %Y %T")
<< '.' << std::setfill('0') << std::setw(3) << nowMs.count();
std::cout << nowSs.str() << '\n';
return 0;
}
Output:
Tue Jul 26 2022 19:27:27.762
In this example, we use std::chrono::system_clock::now to return the current wall-clock time of our system in the form of a time_point object. Before we can display this time in milliseconds, we get the time lapse from the epoch with the time_since_epoch
method of that object.
Note: check out this guide if you want to learn more about variable declaration and initialization in C++.
gettimeofday()
The sys/time.h header file provides the gettimeofday()
function, which can help obtain the current time of your system. It returns two structures, one of which contains two members representing the duration from the current time point to the epoch in seconds and microseconds.
Here is how we can combine std::chrono::system_clock
and gettimeofday()
:
#include <chrono>
#include <iomanip>
#include <iostream>
#include <sys/time.h>
int main() {
const auto now = std::chrono::system_clock::now();
const auto nowAsTimeT = std::chrono::system_clock::to_time_t(now);
struct timeval time_now{};
gettimeofday(&time_now, nullptr);
time_t msecs_time = time_now.tv_usec / 1000;
std::stringstream nowSs;
nowSs
<< std::put_time(std::localtime(&nowAsTimeT), "%a %b %d %Y %T")
<< '.' << std::setfill('0') << std::setw(3) << msecs_time;
std::cout << nowSs.str() << '\n';
return 0;
}
Output:
Tue Jul 26 2022 20:10:01.631
Since tv_usec represents the current time, we can convert it to milliseconds with a simple division.
Conclusion
You can get time in milliseconds in C++ with the chrono library or the sys/time.h header time. They offer the same similar time concepts that we are already familiar with from C.
Leave a comment