C++ - 比较两个日期 [英] C++ - compare two dates

查看:54
本文介绍了C++ - 比较两个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个需要比较两个日期的应用程序.这是我目前所拥有的:

I am writing a application which needs the possibility to compare two dates. This is what I have so far:

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
}

代码获得一个包含日期的条目"结构.比代码将日期与实际时间进行比较.问题是,它不起作用!我用一些示例内容运行了一些测试,但结果 (date2 > date1) 返回 false.

the code gets an "entry" struct which contains a date. Than the code compares the date with the actual time. The problem is, it does not work! I run some tests with some example content, but the result (date2 > date1) returns false.

为什么?

我读过这个:C++ 与字符串日期的比较

推荐答案

我实际上并不是在回答你的问题.但是,我为您提供了一个解决方案.您是否考虑过日期/时间库?Boost datetime 非常流行.

I'm not actually answering your question. However I am offering you a solution. Have you considered a date/time library? Boost datetime is very popular.

如果您使用 C++11 或更高版本进行编译,我推荐这个 日期时间库,因为它仅包含标题(无需链接到诸如 boost 之类的库),而且在我看来,它具有更清晰的语法(这是一个非常主观和有偏见的观点).

If you are compiling in C++11 or later, I recommend this date time library, as it is header-only (eliminating the need to link to a library such as boost), and in my opinion, it has cleaner syntax (that is a very subjective and biased viewpoint).

后一个库建立在 C++11 库之上.这是您使用此库的示例代码:

This latter library builds on the C++11 <chrono> library. Here is your example code using this library:

#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 文字使得文字时间的指定非常紧凑:

In C++14, the chrono literals make specifying literal times very compact:

using namespace std::literals;
auto date1 = date::sys_days{e.date} + 0h + 0min + 0s;

同样在文字方面,如果你加入一个 using namespace date;:

Also on the subject of literals, you can make the construction of entry slightly more compact if you drop in a using namespace date;:

entry e = {"here is some text", 21_d/3/2019, false};

重用日期或日期时间类,甚至创建自己的类,比尝试使用字符串来保存日期更容易.此外,当您打算向时间点添加持续时间时,您不会意外地将字符串添加到日期中,从而获得类型安全性.

Reusing a date or datetime class, or even creating your own, is easier than trying to use a string to hold a date. Additionally you get the type-safety of not accidentally adding a string to a date, when you meant to add a time duration to a time point.

这篇关于C++ - 比较两个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆