tpow
tpow

Reputation: 7894

Get time_t from microseconds in the past

Working on a c++ 11 function that returns a string from an epoch timestamp with millisecond resolution. Doing this with the current date seems straight forward:

auto currentTime = std::chrono::system_clock::now( );
const time_t time = std::chrono::system_clock::to_time_t( currentTime );

However, I'm having a hard time finding out to initialize without now() and instead using a timestamp from the past. Trying to do this using std library, but can't quite see how to initialize the time_point using a past timestamp.

Upvotes: 0

Views: 212

Answers (1)

Sisir
Sisir

Reputation: 5418

How about using the std::chrono::duration class. Below is an example.

unsigned long noOfClockTicks = 10111111111; // Mar 16 10:31:59 2018
std::chrono::duration<unsigned long> duration(noOfClockTicks);
system_clock::time_point pastTime(duration);

Adjust noOfClockTicks to get the correct value you want or you can even calculate it from std::chrono::system_clock::now().

Upvotes: 1

Related Questions