【发布时间】:2014-09-24 21:14:22
【问题描述】:
我正在尝试一种奇怪的 mktime() 函数行为。当我分配函数返回的值时,输入参数的值为 1,而当我不分配时,输入参数的值不同。
我已经知道 mktime() 调整了 struct tm 输入参数的值,但是发生了什么不同,让我们看看带有相应输出的代码:
第一个代码
#include <iostream>
#include <time.h>
using namespace std;
int main(int argc, char** argv) {
struct tm cT;
strptime("31/07/2014 16:54:00", "%d/%m/%Y%n%T", &cT);
mktime(&cT);
cout << "Current Time: " << cT.tm_mday << "/" << cT.tm_mon + 1 << "/" << cT.tm_year + 1900 << " " << cT.tm_hour << ":" << cT.tm_min << ":" << cT.tm_sec << endl;
}
输出:
当前时间:31/7/2014 16:54:0
第二个代码
#include <iostream>
#include <time.h>
using namespace std;
int main(int argc, char** argv) {
struct tm cT;
strptime("31/07/2014 16:54:00", "%d/%m/%Y%n%T", &cT);
time_t t = mktime(&cT);
cout << "Current Time: " << cT.tm_mday << "/" << cT.tm_mon + 1 << "/" << cT.tm_year + 1900 << " " << cT.tm_hour << ":" << cT.tm_min << ":" << cT.tm_sec << endl;
}
输出:
当前时间:31/7/2014 15:54:0
欢迎任何帮助。 :)
【问题讨论】:
-
time.h已弃用。使用ctime。或chrono. -
@chris,感谢您的信息!