C++ 给定日期和今天日期之间的天数

C++ Days between given date and today´s date

本文关键字:日期 之间 C++ 今天      更新时间:2023-10-16

我有一个名为int differenceDatesInDays(string& date)的函数。此函数应该获取一个字符串值作为日期(YYYY-MM-DD),并将其与今天的日期进行比较。

我不知道STL里面是否已经有东西了,我找不到匹配的算法。我刚刚发现boost有一个解决方案,但我不想使用boost。

到目前为止,这是我的代码:

int differenceDatesInDays(string& date) {
    string year = date.substr(0, 4);
    string month = date.substr(5,2);
    string day = date.substr(8, string::npos);
    int y = stoi(year);
    int m = stoi(month);
    int d = stoi(day);
    time_t time_now = time(0);
    tm* now = localtime(&time_now);
    int diffY = y - (now->tm_year + 1900);
    int diffM = m - (now->tm_mon + 1);
    int diffD = d - (now->tm_mday);
    int difference = (diffY * 365) + (diffM * 30) + diffD;
    return difference;
}

我不知道如何知道这个月是30天、31天还是28天。

一旦有了两个积分三元组:{y1, m1, d1}{y0, m0, d0},计算它们之间差异的最有效方法是使用公共域函数days_from_civil来计算每个三元组的连续天数,并减去它们:

diff = days_from_civil(y1, m1, d1) - days_from_civil(y0, m0, d0);

这里是重复的days_from_civil

// Returns number of days since civil 1970-01-01.  Negative values indicate
//    days prior to 1970-01-01.
// Preconditions:  y-m-d represents a date in the civil (Gregorian) calendar
//                 m is in [1, 12]
//                 d is in [1, last_day_of_month(y, m)]
//                 y is "approximately" in
//                   [numeric_limits<Int>::min()/366, numeric_limits<Int>::max()/366]
//                 Exact range of validity is:
//                 [civil_from_days(numeric_limits<Int>::min()),
//                  civil_from_days(numeric_limits<Int>::max()-719468)]
template <class Int>
constexpr
Int
days_from_civil(Int y, unsigned m, unsigned d) noexcept
{
    static_assert(std::numeric_limits<unsigned>::digits >= 18,
             "This algorithm has not been ported to a 16 bit unsigned integer");
    static_assert(std::numeric_limits<Int>::digits >= 20,
             "This algorithm has not been ported to a 16 bit signed integer");
    y -= m <= 2;
    const Int era = (y >= 0 ? y : y-399) / 400;
    const unsigned yoe = static_cast<unsigned>(y - era * 400);      // [0, 399]
    const unsigned doy = (153*(m + (m > 2 ? -3 : 9)) + 2)/5 + d-1;  // [0, 365]
    const unsigned doe = yoe * 365 + yoe/4 - yoe/100 + doy;         // [0, 146096]
    return era * 146097 + static_cast<Int>(doe) - 719468;
}

这将比典型的基于tm的代码具有更大的有效性范围。而且速度会更快。而且你不会被迫处理一天中的时间。如果你所有的信息都是编译时常数,并且你在C++14中,你可以在编译时得到答案。

既然你用Boost标记了它,为什么不使用并免费获得所有东西:

在Coliru上直播

#include <boost/date_time/gregorian/gregorian.hpp>
int differenceDatesInDays(std::string const& s) {
    using namespace boost::gregorian;
    return (day_clock::local_day() - from_string(s)).days();
}
int main() {
    return differenceDatesInDays("2015-01-01");
}

打印

317

也许是这样的东西:

int differenceDatesInDays(string& date) {
    // Parse `date` as in your code
    int y = ...;
    int m = ...;
    int d = ...;
    tm then = {0};
    then.tm_year = y - 1900;
    then.tm_mon = m - 1;
    then.tm_day = d;
    time_t then_secs = mktime(&then);
    time_t time_now = time(0);
    tm* now = localtime(&time_now);
    tm today = {0};
    today.tm_year = now->tm_year;
    today.tm_mon = now->tm_mon;
    today.tm_day = now->tm_day;
    time_t today_secs = mktime(&today);
    return (today_secs - then_secs) / (24*60*60);
}