C++ Can';t验证从时间点到tm和tm回到时间点的转换
我创建了一个当前时间点,并将其转换为structure tm并打印其值。 现在将这个tm结构转换为时间点。 通过比较第一次和第二次的时间点,可以看出它们是不同的。但结构的价值是完全相同的 有人能发现我哪里做错了吗C++ Can';t验证从时间点到tm和tm回到时间点的转换,c++,std,chrono,ctime,C++,Std,Chrono,Ctime,我创建了一个当前时间点,并将其转换为structure tm并打印其值。 现在将这个tm结构转换为时间点。 通过比较第一次和第二次的时间点,可以看出它们是不同的。但结构的价值是完全相同的 有人能发现我哪里做错了吗 #include <iostream> #include <ctime> #include <chrono> using namespace std; using namespace std::chrono; system_clock::time
#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);
}
#包括
#包括
#包括
使用名称空间std;
使用名称空间std::chrono;
系统时钟:时间点到时间点(结构tm tim)
{
返回std::chrono::system_clock::from_time(mktime(&tim));
}
tm toTm(系统时钟:时间点tp)
{
时间tmt=系统时钟::至时间(tp);
struct tm*tim=localtime(&tmt);
struct tm newTim(*tim);
cout是四舍五入。时间点
的分辨率比时间点
高得多时间点
仅为秒,而时间点
由系统定义。例如,在linux libstdc++上,它是纳秒
例如,您所做的与下面的类似
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;
float f=4.25;
int i=(int)f;//i是4
标准::cout