C++

Get current Timestamp in Milliseconds since Epoch in C++

Obtaining the current timestamp within milliseconds from the Unix epoch is a typical task in C++ programming. It can be achieved by converting the current time to duration from the epoch using the <chrono> library. C++ typically uses a current timestamp of milliseconds from the Unix epoch for calculating time durations, recording information changes, and clock synchronization across remote systems. In this guide, we will discuss how to get a current time stamp in milliseconds since epoch Unix in C++.

What Exactly is the Unix Epoch

The Unix epoch is a time-based reference specified as January 1, 1970, at 00:00:00 UTC. It is important for calculating timestamps in C++ as well as many other programming languages as it offers a constant reference point for calculating time durations and contrasting timestamps across various systems and platforms. C++ programs may acquire a generally recognized timestamp value by calculating the time difference between the current date and time & the Unix epoch. This number is then compared and modified in a standardized manner.

Method 1: Current Timestamps in Milliseconds from the Unix Epoch using chrono Library

We can get the elapsed time since the epoch by std::chrono since C++11. The goal is to use chrono::system_clock::now() to retrieve the current system time. Then call the time_since_epoch() method to find the duration indicating the time since the epoch. C++20 defined this time_since_epoch() function for the Unix epoch.

Syntax

#include <chrono>

using namespace std;

using namespace chrono;

millisec var = duration_cast<milliseconds>(

system_clock::now().time_since_epoch()).count();

In the above syntax, we include <chrono> library which has methods the duration_cast<milliseconds> to convert time in milliseconds. The time_since_epoch() gives the time elapsed since 00:00:00 UTC on 1 January 1970.

Example

Following is the simple program of epoch with chrono library to show timestamps in milliseconds:

#include <iostream>

#include <chrono>

using namespace std;

int main()

{

   using namespace chrono;

//code for getting the time elapsed in milliseconds

  uint64_t millisec = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();

  cout << "Time duration in milliseconds since the Epoch: "<< millisec<<"\n";

// code for getting the time elapsed in seconds

  uint64_t second = duration_cast<seconds>(system_clock::now().time_since_epoch()).count();

  cout <<"Time duration in seconds since the Epoch: "<< second;

 return 0;

}

In the above code, we find the time current timestamp since the epoch in milliseconds and seconds. The time in milliseconds and seconds is printed on the screen.

Note: uint64_t is a datatype used for storing large positive integers. When you execute the program the following result will dis[lay on the terminal:

Method 2: Current Timestamps in Milliseconds from the Unix Epoch with ctime Library

The time() function provides the current calendar time within the time_t object. To use this function we have to add a header file <ctime> in the code:

#include <ctime>

Using namespace std;

int main(){

time_t var = time(nullptr);

}

In the above syntax, time_t is a method used to get the current time since the epoch and store it in the variable var (you can give any name to this variable). Usually, we give a null parameter to time() function to get the current system time.

Example

The following code sample shows how to use it to calculate the current time in milliseconds since the epoch using the time() function:

#include <iostream>

#include <ctime>

using namespace std;

int main()

{

  time_t sec =time(nullptr);

  time_t millisec = sec *1000;

  cout << "Time in seconds since epoch: "<<sec<<"\n";

  cout << "Time in milliseconds since epoch: "<<millisec<<"\n";

  return 0;

}

In the above code, we are using the time() function of the <ctime> header file in the program.

With the help of the time(), we get the current time, and then by assigning the null parameter to the time(nullptr), we return the time elapsed in seconds. For getting time elapsed in milliseconds we create a variable millisec that converts the number of seconds stored in the variable sec to milliseconds. The output of this program is shown below:

Note: The time() function has a precision of seconds, this value will be a whole integer reflecting the number of seconds since the epoch.

Conclusion

Finding the current timestamp in milliseconds from the Unix epoch in C++ is critical for measuring time and date, and it is possible using the<chrono> and <ctime> libraries with their respective functions. The Unix epoch offers uniformity across systems and platforms as a universal point of reference for measuring timestamps. The implementation comprises retrieving the current time, turning it into precision, and also calculating the period of time since the Unix epoch, and translating the period to milliseconds and seconds.

About the author

Kaynat Asif

My passion to research new technologies has brought me here to write for the LinuxHint. My major focus is to write in C, C++, and other Computer Science related fields. My aim is to share my knowledge with other people.