提升月份的行为的原则是什么?

What is the principle for behavior of boost adding month?

本文关键字:原则 是什么      更新时间:2023-10-16

boost添加月的示例

28/02/2009 1个月= 31/03/2009

#include <boost/date_time/gregorian/gregorian_types.hpp>
#include <boost/date_time/gregorian/formatters.hpp>
#include <iostream>
using namespace boost::gregorian;
using namespace std;
int main()
{
typedef boost::date_time::month_functor<date> add_month;
        date d(2019, Feb, 28);
        add_month mf(1);
        date d2 = d + mf.get_offset(d);
        cout << to_simple_string(d2) << endl;//output:2019-Mar-31
}

输出是2019-MAR-31,而不是2019-MAR-28。这种行为的理论基础是什么?谈论它在C#和Delphi中输出2019-MAR-28,以获取类似的添加月份代码。

boost::date_time::month_functor 的文档中提到了此行为。

此调整功能为基于YMD的日历上的"基于月"的进步提供了逻辑。它用于处理不存在的一个月末日的政策是备份到本月的最后一天。也是,如果起始日期是一个月的最后一天,则该函数将尝试调整到月底

因此,将一个月添加到2月28日将在3月31日导致。
但是将一个月添加到2月27日将在3月27日导致。

参见 LIVE DEMO.