#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
}
Run Code Online (Sandbox Code Playgroud)
输出为2019年3月31日而不是2019年3月28日。这种行为的理论基础是什么?谈论它以类似的添加月份代码在C#和delphi中输出2019年3月28日。
的文档中提到了此行为boost::date_time::month_functor。
此调整功能提供了基于ymd日历的“基于月”进度的逻辑。它用于处理不存在的月末日期的策略是备份到该月的最后一天。另外,如果开始日期是一个月的最后一天,则此函子将尝试调整到该月末。
因此,在2月28日增加一个月将在3月31日产生结果。
但是,要在2月27日之前增加一个月,则会导致3月27日。
看到 LIVE DEMO.
| 归档时间: |
|
| 查看次数: |
61 次 |
| 最近记录: |