我看到了 C #,Java 的例子,但对于 C ++ 我找不到解决方案来计算两个日期之间有多少天。
例如在 2012-01-24 和 2013-01-08 之间
谢谢
这是一种方式。
#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
Here is a ref to Original author post
将您的日期转换为表示一个时代以来的天数的整数,然后减去。在这个例子中,我选择了Rata Die,算法的解释可以在 & lt;http://mysite.verizon.net/aesir_research/date/rata.htm& 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);
C ++ 20 更新:
#include <chrono>
#include <iostream>
int
main()
{
using namespace std::chrono;
using namespace std;
auto x = 2012y/1/24;
auto y = 2013y/1/8;
cout << x << '\n';
cout << y << '\n';
cout << "difference = " << sys_days{y} - sys_days{x} << 'n';
}
输出:
2012-01-24
2013-01-08
difference = 350d
如果{year, month, day}
数据存在于int
中,那么它看起来就像:
int xy = 2012;
int xm = 1;
int xd = 24;
int yy = 2013;
int ym = 1;
int yd = 8;
auto x = year{xy}/xm/xd;
auto y = year{yy}/ym/yd;
// ...
sys_days{y} - sys_days{x}
的类型是std::chrono::days
,它是std::chrono::duration<signed integral type, std::ratio<86'400>>
的类型别名。
一个老问题的新答案:
使用此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进行建模,从而无限期地向前和向后扩展公历。要对其他日历(例如朱利安日历)进行建模,您将需要其他算法such as the ones shown here。一旦设置了其他日历,并将其同步到相同的串行(这些算法使用 1970-01-01 公历,这也是您可以轻松计算的两个日期之间的任何Unix time
这使您不必在从 Julian 切换到 Gregorian 的日期中进行硬编码。您只需要知道您的输入数据是针对哪个日历引用的。
有时,历史文档中可能含糊不清的日期会分别用Old Style / New Style注释,以表示朱利安或格里高利历法。
如果您还关心日期的时间,则此same date library与<chrono>
库无缝集成,以使用hours
,minutes
,seconds
,milliseconds
,microseconds
和nanoseconds
时间。
如果您关注时区,则会在date library的顶部写入一个额外的(单独的)timezone library,以使用IANA timezone database处理时区。如果需要,timezone library还具有用于计算leap seconds的功能。
您可以尝试boost date_time库
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(3条)