I created a current time_point and converted it to structure tm and printed it's values. Now converted this tm structure to time_point. On comparing the first and second time_points, it is telling that they're different. But the values of structure are exactly same.
Can someone spot, where 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 Is Daylight saving: 0 wday: 5 yday: 127
Time comparison: 0
Info: 8/4/115 16:26:20 Is Daylight saving: 0 wday: 5 yday: 127
It's rounding. time_point
can have much higher resolutions than time_t
. time_t
is simply seconds, while time_point
is defined by the system. On linux libstdc++, for example, it's nanoseconds.
As an example, what you're doing is similar to the below
float f = 4.25;
int i = (int)f; // i is 4
std::cout << i << std::endl;
float f2 = i; // f2 is 4.0
std::cout << (f == f2) << std::endl; // false
int i2 = (int)f2; // i2 is also 4
std::cout << i2 << std::endl;