解释函数`mktime()`和`ctime()`

时间:2018-01-11 17:27:48

标签: c++ time

考虑以下代码片段,我希望通过评论解释其功能:

string date = "11/01/2018"; // value of date is only for test
struct tm tm;
strptime(date.c_str(), "%d/%m/%Y", &tm); // convert date to tm struct
time_t ret = mktime(&tm); // now convert tm to time_t

现在,当我打印出dateret变量时,如下所示:

cout << date << endl
       << ctime(&ret) << endl;

我得到以下输出:

11/01/2018
Sun Feb  3 23:44:56 2497

有人可以看到,第二行上打印的日期并不接近变量date中显示的日期。

有人可以给我一些关于这种行为的线索吗?我需要绝对的时间来轻松地比较它们,但当然,我还必须将它们作为格式化为欧洲风格的字符串表示来管理。

0 个答案:

没有答案