c++ - How to find the time difference in milliseconds between an epoch timestamp and std::chrono::system_clock::now -


hi have created c++ app among others uses std::chrono in order calculate time differences.
@ point upload file server , response epoch timestamp receive.php informs timestamp when file uploaded server. i'd calculate time diff between epoch time stamp , starting point of choice since should not change receive.php way of working. far tried achive using following code:

#include <iostream> #include <chrono>  using namespace std;  int main() { auto epoch1 = std::chrono::system_clock::now().time_since_epoch() ; auto epoch2= std::chrono::duration<long long>(epoch_time_stamp); auto diff = epoch1 - epoch2 ; auto s = std::chrono::duration_cast<std::chrono::milliseconds>(diff); cout << s.count() << endl; } 

where epoch_time_stamp 13 digit epoch timestamp e.x 1501190040123.
false results. tried pass epoch_time_stamp both int64_t , time_t no success.since i'm quite new @ using std::chrono assume cast of epoch2 not correct.

any ideas should do?

as know, epoch_time_stamp count of milliseconds since 1970-01-01 00:00:00 utc. when say:

auto epoch2= std::chrono::duration<long long>(epoch_time_stamp); 

you quietly converting count of milliseconds count of seconds. can convert count of milliseconds with:

auto epoch2= std::chrono::duration<long long, std::milli>(epoch_time_stamp); 

and think start getting results right you.

i find helpful create templated using alias this:

template <class d> using sys_time = std::chrono::time_point<std::chrono::system_clock, d>; 

now sys_time<milliseconds> time_point counts milliseconds since epoch. allow simplify code down to:

auto remote_time = sys_time<milliseconds>{milliseconds{epoch_time_stamp}}; cout << duration_cast<milliseconds>(system_clock::now() - remote_time).count() << "ms\n"; 

for more details <chrono>, please see video tutorial.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

.htaccess - ERR_TOO_MANY_REDIRECTS htaccess -