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

use*_*164 3 c++ time boost date c++11

我有一个叫做的函数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;
}
Run Code Online (Sandbox Code Playgroud)

我不知道如何确定这个月是30天,31天还是28天.

How*_*ant 5

一旦你有两个完整的三元组:{y1, m1, d1}并且{y0, m0, d0},计算它们之间差异的最有效方法是使用公共域函数days_from_civil来计算每个三元组的序列日计数,并减去它们:

diff = days_from_civil(y1, m1, d1) - days_from_civil(y0, m0, d0);
Run Code Online (Sandbox Code Playgroud)

这里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;
}
Run Code Online (Sandbox Code Playgroud)

这将比tm基于典型的代码具有更大的有效性范围.它会更快.而且你不会被迫处理时间.如果您的所有信息都是编译时常量,并且您使用的是C++ 14,那么您可以在编译时获得答案.