我看过C#,Java的例子,但是对于C ++,我找不到解决方案来计算两个日期之间的天数。
例如2012-01-24和2013-01-08之间
谢谢!
答案 0 :(得分:19)
这是一种方式。
#include <iostream>
#include <ctime>
int main()
{
struct std::tm a = {0,0,0,24,5,104}; /* June 24, 2004 */
struct std::tm b = {0,0,0,5,6,104}; /* July 5, 2004 */
std::time_t x = std::mktime(&a);
std::time_t y = std::mktime(&b);
if ( x != (std::time_t)(-1) && y != (std::time_t)(-1) )
{
double difference = std::difftime(y, x) / (60 * 60 * 24);
std::cout << std::ctime(&x);
std::cout << std::ctime(&y);
std::cout << "difference = " << difference << " days" << std::endl;
}
return 0;
}
Thu Jun 24 01:00:00 2004
Mon Jul 05 01:00:00 2004
difference = 11 days
答案 1 :(得分:12)
将日期转换为整数,表示自纪元以来的天数,然后减去。在此示例中,我选择Rata Die,可以在&lt; {{}}&gt;找到对算法的解释。
int
rdn(int y, int m, int d) { /* Rata Die day one is 0001-01-01 */
if (m < 3)
y--, m += 12;
return 365*y + y/4 - y/100 + y/400 + (153*m - 457)/5 + d - 306;
}
int days = rdn(2013, 1, 8) - rdn(2012, 1, 24);
答案 2 :(得分:10)
旧问题的新答案:
使用此C++11/C++14 header-only date library,您现在可以写:
#include "date.h"
#include <iostream>
int
main()
{
using namespace date;
using namespace std;
auto x = 2012_y/1/24;
auto y = 2013_y/1/8;
cout << x << '\n';
cout << y << '\n';
cout << "difference = " << (sys_days{y} - sys_days{x}).count() << " days\n";
}
哪个输出:
2012-01-24
2013-01-08
difference = 350 days
如果您不想依赖此库,则可以使用上述日期库使用的相同日期算法编写自己的库。它们可以在本文中找到:chrono-Compatible Low-Level Date Algorithms。本例中正在运用的算法是这样的:
// 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;
}
有关此算法的工作原理,单元测试及其有效范围的详细信息,请参阅chrono-Compatible Low-Level Date Algorithms。
此算法模拟proleptic Gregorian calendar,它无限地向前和向后扩展公历。要为其他日历(例如Julian日历)建模,您需要其他算法such as the ones shown here。一旦你设置了其他日历,并同步到同一个串行纪元(这些算法使用1970-01-01格里高利,这也是Unix time纪元),你可以轻松地计算任何日期之间的天数两个日期,也可以在您建模的任何两个日历之间。
这使您可以自由地在从Julian到Gregorian的转换日期中进行硬编码。您只需要知道引用输入数据的日历。
有时,历史文档中可能含糊不清的日期会被Old Style / New Style注释,以分别表示朱利安或格里高利历。
如果您还关注日期的时间,same date library会与<chrono>
库无缝集成,以便hours
使用minutes
,{{ 1}},seconds
,milliseconds
和microseconds
以及nanoseconds
来获取当前日期和时间。
如果您担心时区,则会在timezone library之上写一个额外的(单独的)date library来使用IANA timezone database来处理时区。如果需要,timezone library还具有包含leap seconds的计算工具。
答案 3 :(得分:3)
您可以试用boost date_time库
答案 4 :(得分:0)
为避免自己动手,可以使用Boost中的 date_time 。