C++ - 比较两个日期
C++ - compare two dates
我正在编写一个需要比较两个日期的应用程序。这是我目前所拥有的:
struct entry {
string text;
string date; // format: dd.mm.yyyy
bool finished;
};
string addNulls(int number, int cols) {
string num = to_string(number);
if (num.size() < cols) {
int count = cols - num.size();
for (int i = 0; i < count; i++) {
num = "0" + num;
}
}
return num;
}
// [...]
entry e = {"here is some text", "21.03.2019", false};
int day2 = atoi(e.date.substr(0, 2).c_str());
int month2 = atoi(e.date.substr(3, 2).c_str());
int year2 = atoi(e.date.substr(6, 4).c_str());
time_t t = time(0);
struct tm * now = localtime(&t);
string date1 = e.date.substr(6, 4) + "-" + e.date.substr(3, 2) + "-" + e.date.substr(0, 2) + " 00:00:00";
string date2 = addNulls(now->tm_year, 4) + "-" + addNulls(now->tm_mon, 2) + "-" + addNulls(now->tm_mday, 2) + " 00:00:00";
if(date2 > date1) {
// do something
}
代码获取一个包含日期的 "entry" 结构。比代码将日期与实际时间进行比较。问题是,它不起作用!我 运行 使用一些示例内容进行了一些测试,但结果 (date2 > date1) returns false.
为什么?
我读了这个:C++ compare to string dates
为什么不使用 strptime
来解析日期字符串,将它们转换为纪元时间然后进行比较?
#include <time.h>
char *
strptime(const char *restrict buf, const char *restrict format,
struct tm *restrict tm);
我实际上并不是在回答你的问题。但是,我正在为您提供解决方案。您考虑过 date/time 图书馆吗? Boost datetime 很受欢迎。
如果您使用 C++11 或更高版本进行编译,我推荐此 date time library,因为它仅包含头文件(无需 link 到 boost 等库),在我看来,它的语法更清晰(这是一个非常主观和有偏见的观点)。
后一个库建立在 C++11 <chrono>
库的基础上。这是您使用此库的示例代码:
#include "date.h"
#include <iostream>
#include <string>
struct entry {
std::string text;
date::year_month_day date;
bool finished;
};
int
main()
{
entry e = {"here is some text", date::day(21)/3/2019, false};
auto day2 = e.date.day();
auto month2 = e.date.month();
auto year2 = e.date.year();
auto t = std::chrono::system_clock::now();
auto date1 = date::sys_days{e.date};
auto date2 = t;
if (date2 > date1)
std::cout << "It is past " << e.date << '\n';
else
std::cout << "It is not past " << e.date << '\n';
}
当前输出:
It is not past 2019-03-21
在 C++14 中,chrono 文字使指定文字时间变得非常紧凑:
using namespace std::literals;
auto date1 = date::sys_days{e.date} + 0h + 0min + 0s;
还有关于文字的主题,如果你放入 using namespace date;
:
,你可以使 entry
的结构稍微紧凑一些
entry e = {"here is some text", 21_d/3/2019, false};
重复使用日期或日期时间 class,甚至创建您自己的日期或日期时间,比尝试使用字符串来保存日期更容易。此外,当您打算将持续时间添加到时间点时,您可以获得类型安全性,不会意外地将字符串添加到日期。
我正在编写一个需要比较两个日期的应用程序。这是我目前所拥有的:
struct entry {
string text;
string date; // format: dd.mm.yyyy
bool finished;
};
string addNulls(int number, int cols) {
string num = to_string(number);
if (num.size() < cols) {
int count = cols - num.size();
for (int i = 0; i < count; i++) {
num = "0" + num;
}
}
return num;
}
// [...]
entry e = {"here is some text", "21.03.2019", false};
int day2 = atoi(e.date.substr(0, 2).c_str());
int month2 = atoi(e.date.substr(3, 2).c_str());
int year2 = atoi(e.date.substr(6, 4).c_str());
time_t t = time(0);
struct tm * now = localtime(&t);
string date1 = e.date.substr(6, 4) + "-" + e.date.substr(3, 2) + "-" + e.date.substr(0, 2) + " 00:00:00";
string date2 = addNulls(now->tm_year, 4) + "-" + addNulls(now->tm_mon, 2) + "-" + addNulls(now->tm_mday, 2) + " 00:00:00";
if(date2 > date1) {
// do something
}
代码获取一个包含日期的 "entry" 结构。比代码将日期与实际时间进行比较。问题是,它不起作用!我 运行 使用一些示例内容进行了一些测试,但结果 (date2 > date1) returns false.
为什么?
我读了这个:C++ compare to string dates
为什么不使用 strptime
来解析日期字符串,将它们转换为纪元时间然后进行比较?
#include <time.h>
char *
strptime(const char *restrict buf, const char *restrict format,
struct tm *restrict tm);
我实际上并不是在回答你的问题。但是,我正在为您提供解决方案。您考虑过 date/time 图书馆吗? Boost datetime 很受欢迎。
如果您使用 C++11 或更高版本进行编译,我推荐此 date time library,因为它仅包含头文件(无需 link 到 boost 等库),在我看来,它的语法更清晰(这是一个非常主观和有偏见的观点)。
后一个库建立在 C++11 <chrono>
库的基础上。这是您使用此库的示例代码:
#include "date.h"
#include <iostream>
#include <string>
struct entry {
std::string text;
date::year_month_day date;
bool finished;
};
int
main()
{
entry e = {"here is some text", date::day(21)/3/2019, false};
auto day2 = e.date.day();
auto month2 = e.date.month();
auto year2 = e.date.year();
auto t = std::chrono::system_clock::now();
auto date1 = date::sys_days{e.date};
auto date2 = t;
if (date2 > date1)
std::cout << "It is past " << e.date << '\n';
else
std::cout << "It is not past " << e.date << '\n';
}
当前输出:
It is not past 2019-03-21
在 C++14 中,chrono 文字使指定文字时间变得非常紧凑:
using namespace std::literals;
auto date1 = date::sys_days{e.date} + 0h + 0min + 0s;
还有关于文字的主题,如果你放入 using namespace date;
:
entry
的结构稍微紧凑一些
entry e = {"here is some text", 21_d/3/2019, false};
重复使用日期或日期时间 class,甚至创建您自己的日期或日期时间,比尝试使用字符串来保存日期更容易。此外,当您打算将持续时间添加到时间点时,您可以获得类型安全性,不会意外地将字符串添加到日期。