C++ Boost::格里高利和闰年

C++ Boost::格里高利和闰年,c++,datetime,boost,C++,Datetime,Boost,我正在编写一段代码,其中我只使用boost库作为先决条件 我需要一个类来处理日期时间值和操作(加减年、月、小时等),所以我选择了公历日期作为选项 但是,当我处理闰年的日子时,一些惊喜出现了。下面是一段示例代码: int main() { boost::gregorian::date d1(2000,1,1); boost::gregorian::days ds(118); boost::gregorian::date d2 = d1 + ds; std::cout <&

我正在编写一段代码,其中我只使用boost库作为先决条件

我需要一个类来处理日期时间值和操作(加减年、月、小时等),所以我选择了公历日期作为选项

但是,当我处理闰年的日子时,一些惊喜出现了。下面是一段示例代码:

int main()
{
  boost::gregorian::date d1(2000,1,1);
  boost::gregorian::days ds(118);

  boost::gregorian::date d2 = d1 + ds;

  std::cout << boost::gregorian::to_iso_extended_string(d1) << std::endl;
  std::cout << boost::gregorian::to_iso_extended_string(d2) << std::endl;

  return 0;
}

Output:
2000-01-01
2000-04-28 (should be 2000-04-27)
intmain()
{
日期d1(2000年1月1日);
boost::gregorian::days ds(118);
boost::gregorian::date d2=d1+ds;

我认为这是正确的:

for a in {1..118}; do echo -n "+$a days: "; date --rfc-2822 -d"2000-01-01 +$a days"; done
印刷品

显示我在闰日前后未发现异常:

+1 days: Sun, 02 Jan 2000 00:00:00 +0100
+2 days: Mon, 03 Jan 2000 00:00:00 +0100
...
+57 days: Sun, 27 Feb 2000 00:00:00 +0100
+58 days: Mon, 28 Feb 2000 00:00:00 +0100
+59 days: Tue, 29 Feb 2000 00:00:00 +0100
+60 days: Wed, 01 Mar 2000 00:00:00 +0100
...
+116 days: Wed, 26 Apr 2000 00:00:00 +0200
+117 days: Thu, 27 Apr 2000 00:00:00 +0200
+118 days: Fri, 28 Apr 2000 00:00:00 +0200

为什么结果应该是4月27日?2000年1月1日后的118天是2000年4月28日。事实上,您的答案是正确的。我们测试的代码用于在datetime中执行额外的操作,并在一天内进行减法,以达到舍入目的,谢谢。事实上,您的答案是正确的。我们测试的代码用于在datetime中执行额外的操作,并在一天内进行减法,f还是四舍五入,谢谢。