c++ - Can't verify the conversion from time_point to tm and tm back to time_point -


i created current time_point , converted structure tm , printed it's values. converted tm structure time_point. on comparing first , second time_points, telling they're different. values of structure same.

can spot, i'm doing wrong?

#include <iostream> #include <ctime> #include <chrono>  using namespace std; using namespace std::chrono;  system_clock::time_point totimepoint(struct tm tim) {     return std::chrono::system_clock::from_time_t(mktime(&tim)); }  tm totm(system_clock::time_point tp) {     time_t tmt = system_clock::to_time_t(tp);     struct tm * tim = localtime(&tmt);     struct tm newtim(*tim);     cout << "info: " << tim->tm_mday << "/" << tim->tm_mon << "/" << tim->tm_year << " " << tim->tm_hour << ":" << tim->tm_min << ":" << tim->tm_sec << endl;     cout << "is daylight saving: " << tim->tm_isdst << " wday: " << tim->tm_wday << " yday: " << tim->tm_yday << endl;     return newtim; }  int _tmain(int argc, _tchar* argv[]) {     system_clock::time_point tp = system_clock::now();      struct tm tmstruct = totm(tp);     system_clock::time_point newtp = totimepoint(tmstruct);     cout << "time comparison: " << (tp == newtp) << endl;      totm(newtp); } 

output:

info: 8/4/115 16:26:20 daylight saving: 0 wday: 5 yday: 127

time comparison: 0

info: 8/4/115 16:26:20 daylight saving: 0 wday: 5 yday: 127

it's rounding. time_point can have higher resolutions time_t. time_t seconds, while time_point defined system. on linux libstdc++, example, it's nanoseconds.

as example, you're doing similar below

  float f = 4.25;   int = (int)f; // 4   std::cout << << std::endl;   float f2 = i; // f2 4.0    std::cout << (f == f2) << std::endl; // false   int i2 = (int)f2; // i2 4   std::cout << i2 << std::endl; 

Comments