无法验证从 time_point 到 tm 和 tm 返回 time_point 的转换
Can't verify the conversion from time_point to tm and tm back to time_point
我创建了一个当前的time_point,然后将其转换为结构TM并打印其值。现在将此TM结构转换为time_point。在比较第一个和第二个time_points时,它表明它们与众不同。但是结构的值是完全相同的。
有人可以发现,我做错了什么?
#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);
}
输出:
信息:8/4/115 16:26:20是日光节省:0 wday:5 yday:127
时间比较:0
信息:8/4/115 16:26:20是夏令时节省:0 wday:5 yday:127
它正在舍入。time_point
的分辨率比time_t
更高。time_t
只是秒,而time_point
由系统定义。例如,在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;
相关文章:
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")
- 为什么我会收到"Run-Time Check Failure #2 - Stack around the variable 'pr' was corrupted"错误?
- 解析某些行的文本和输出"time of appearance"
- 如何在不使用 std::tm 和 mktime() 的情况下为给定日历日期创建 chrono::time_point 对
- 如何比较两个tm(来自ctime)变量
- 返回time()库函数的数据类型
- 如何将 time-uuid(存储在 boost uuid 中)转换为时间戳/自纪元以来的时间?
- 如何从 boost::p osix_time::p time 获取秒数
- 调用 dll 函数时"Run-Time Check Failure #0 - The value of ESP"
- C++ SRAND(time(0)) 在猜谜游戏中不起作用
- 即使包含 <time.h 后仍"‘CLOCK_PER_SEC’ was not declared in this scope"错误>
- struct TM time;vs TM time = {}.输出相同但不相同?
- C++ std::tm 从 std::chrono::time_point 转换后返回错误的值
- 将 OLE 自动化日期 (OADate) 双精度转换为结构 tm,而不使用 VariantTime到SystemTime
- 使用 Windows 寄存器查找下一个"Daylight Saving Time"
- c++ get millsecond time
- "Feature X requires run-time support"的含义是什么?
- 如何传递以 "size determined at run time" 作为引用的动态分配数组?
- 在c++中调用time会改变我的结构体tm