2
我试图将诸如“2011年8月12日”之类的字符串转换为time_t或经过的秒数,或者我可以用它来比较日期列表。C++将字符串转换为time_t问题
此刻,我尝试了以下,但输出似乎等于假!另外,过去的秒数似乎在不断变化?
这是正确的吗?
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>
#include <time.h>
#include <stdio.h>
using namespace std;
int main()
{
struct tm tmlol, tmloltwo;
time_t t, u;
t = mktime(&tmlol);
u = mktime(&tmloltwo);
//char test[] = "01/01/2008";string test = "01/01/2008";
strptime("10 February 2010", "%d %b %Y", &tmlol);
strptime("10 February 2010", "%d %b %Y", &tmloltwo);
t = mktime(&tmlol);
u = mktime(&tmloltwo);
cout << t << endl;
cout << u << endl;
if (u>t)
{
cout << "true" << endl;
}
else if (u==t)
{
cout << "same" << endl;
}
else
{
cout << "false" << endl;
}
cout << (u-t);
}
简单:'struct tm tmlol = {0}; struct tm tmloltwo = {0};'。 (海湾合作委员会可能会警告缺少初始值设定项;随意忽略它。) –
@Keith:Simpler:'tm tmlol = {},tmloltwo = {};'; - ] – ildjarn
@ildjarn:有趣的。这在C中是非法的;我不知道C++允许它。 –