C++ C++;日期比较并不总是计算为相同的逻辑值
我正在做一个项目,必须跟踪与书籍相关的日期。我将日期存储为字符串。我需要打印出指定日期后出版的所有书籍 下面是一个类似于我在代码中所做的循环,它复制了一个比较两个日期的值不一致的问题C++ C++;日期比较并不总是计算为相同的逻辑值,c++,time,strptime,time-t,C++,Time,Strptime,Time T,我正在做一个项目,必须跟踪与书籍相关的日期。我将日期存储为字符串。我需要打印出指定日期后出版的所有书籍 下面是一个类似于我在代码中所做的循环,它复制了一个比较两个日期的值不一致的问题 #include <time.h> #include <stdio.h> #include <string> #include <ctime> #include <vector> int main() { std::string comp_aga
#include <time.h>
#include <stdio.h>
#include <string>
#include <ctime>
#include <vector>
int main()
{
std::string comp_against = "11/1995";
std::vector<std::string> dates = {"11/1995", "10/1990", "03/2004", "2/1992", "11/1995"};
for(auto it = dates.begin(); it != dates.end(); ++it)
{
std::string date = *it;
struct tm t1;
struct tm t2;
// parse the dates with mm/YYYY format
strptime(comp_against.c_str(), "%m/%Y", &t1);
strptime(date.c_str(), "%m/%Y", &t2);
std::time_t s1 = mktime(&t1);
std::time_t s2 = mktime(&t2);
printf("%s > %s: %s\n", date.c_str(), comp_against.c_str(), (s2 > s1 ? "true" : "false"));
}
return 0;
}
发件人:
原则上,此函数不初始化tm,而是存储指定的值。这意味着tm应该在调用之前初始化
解决方案:
struct tm t1 = {}; // same for t2 ...
您的
strtime
提取没有填充您提供给它们的struct tm
构造中的所有内容;他们只填充制造您请求的格式数据所需的项
将您的struct tm
声明更改为:
struct tm t1{}; // value initialize members
struct tm t2{}; // here too.
假设您使用的是与C++11兼容的工具链。这样做的结果是:
11/1995 > 11/1995: false
10/1990 > 11/1995: false
03/2004 > 11/1995: true
2/1992 > 11/1995: false
11/1995 > 11/1995: false
如果你想知道:
printf("%ld %ld : %s > %s: %s\n", s1, s2, date.c_str(),
comp_against.c_str(), (s2 > s1 ? "true" : "false"));
在没有初始化tm
结构的情况下生成此值:
815126400 815126400 : 11/1995 > 11/1995: false
817804800 657360000 : 10/1990 > 11/1995: false
815212800 1080720000 : 03/2004 > 11/1995: true
815212800 699523200 : 2/1992 > 11/1995: false
815212800 815299200 : 11/1995 > 11/1995: true
并在初始化时生成:
815126400 815126400 : 11/1995 > 11/1995: false
815126400 654681600 : 10/1990 > 11/1995: false
815126400 1078041600 : 03/2004 > 11/1995: true
815126400 696844800 : 2/1992 > 11/1995: false
815126400 815126400 : 11/1995 > 11/1995: false
使用此功能,以下是一种使代码更简单、更不容易出错的方法:
#include "date.h"
#include <iostream>
#include <vector>
int main()
{
using namespace date::literals;
auto comp_against = 1995_y/11;
std::vector<date::year_month> dates = {1995_y/11, 1990_y/10, 2004_y/03,
1992_y/2, 1995_y/11};
std::cout << std::boolalpha;
for(auto it = dates.begin(); it != dates.end(); ++it)
std::cout << *it << " > " << comp_against << ": "
<< (*it > comp_against) << '\n';
}
啊,对。我没有理由不自己意识到这一点。非常感谢。
815126400 815126400 : 11/1995 > 11/1995: false
815126400 654681600 : 10/1990 > 11/1995: false
815126400 1078041600 : 03/2004 > 11/1995: true
815126400 696844800 : 2/1992 > 11/1995: false
815126400 815126400 : 11/1995 > 11/1995: false
#include "date.h"
#include <iostream>
#include <vector>
int main()
{
using namespace date::literals;
auto comp_against = 1995_y/11;
std::vector<date::year_month> dates = {1995_y/11, 1990_y/10, 2004_y/03,
1992_y/2, 1995_y/11};
std::cout << std::boolalpha;
for(auto it = dates.begin(); it != dates.end(); ++it)
std::cout << *it << " > " << comp_against << ": "
<< (*it > comp_against) << '\n';
}
1995/Nov > 1995/Nov: false
1990/Oct > 1995/Nov: false
2004/Mar > 1995/Nov: true
1992/Feb > 1995/Nov: false
1995/Nov > 1995/Nov: false