Convert std::chrono::system_clock::time_point to struct timeval and back
20:02 09 Sep 2016

I'm writing a C++ code that needs to access an old C library that uses timeval as a representation of the current time.

In the old package to get the current date/time we used:

struct timeval dateTime;
gettimeofday(&dateTime, NULL);

function(dateTime); // The function will do its task

Now I need to use C++ chrono, something as:

system_clock::time_point now = system_clock::now();
struct timeval dateTime;

dateTime.tv_sec = ???? // Help appreciated here
dateTime.tv_usec = ???? // Help appreciated here

function(dateTime);

Later in code I need the way back, building a time_point variable from the returned struct timeval:

struct timeval dateTime;
function(&dateTime);

system_clock::time_point returnedDateTime = ?? // Help appreciated

I'm using C++11.

c++ c c++11