从boost :: gregorian中删除add_month?

时间:2017-03-23 13:54:06

标签: c++ datetime boost

我正在使用boost::gregorian执行日期计算。我想根据示例使用add_month(当前截至1.63 http://www.boost.org/doc/libs/1_63_0/doc/html/date_time/examples.html

/* Simple program that uses the gregorian calendar to progress by exactly
* one month, irregardless of how many days are in that month.
*
* This method can be used as an alternative to iterators
*/
#include "boost/date_time/gregorian/gregorian.hpp"
#include <iostream>

int main()
{

  using namespace boost::gregorian;

  date d = day_clock::local_day();
  add_month mf(1);
  date d2 = d + mf.get_offset(d);
  std::cout << "Today is: " << to_simple_string(d) << ".\n"
  << "One month from today will be: " << to_simple_string(d2) 
  << std::endl;

  return 0;
}

但是,这会给出错误消息

month.cpp: In function `int main()':                                   
month.cpp:33:5: error: `add_month' was not declared in this scope      
 add_month mf(1);                                                  
 ^                                                                 
month.cpp:35:19: error: `mf' was not declared in this scope            
 date d2 = d + mf.get_offset(d);                                   
               ^                                                   

1 个答案:

答案 0 :(得分:1)

事实上。这个例子已经过时了。事实上,我不记得看到这个功能,所以它可能已经过时了。

我建议采用以下方法:

/* Simple program that uses the gregorian calendar to progress by exactly
 * one month, irregardless of how many days are in that month.
 *
 * This method can be used as an alternative to iterators
 */

#include "boost/date_time/gregorian/gregorian.hpp"
#include "boost/date_time/gregorian/gregorian.hpp"
#include <iostream>

int main()
{

    using namespace boost::gregorian;

    date d = day_clock::local_day(),
         prev = d - months(1),
         next = d + months(1);

    std::cout << "Today is: "                        << to_simple_string(d)  << ".\n"
              << "One month before today was: " << to_simple_string(prev) << "\n"
              << "One month from today will be: "    << to_simple_string(next) << "\n";
}

打印(对我来说):

Today is: 2017-Mar-23.
One month before today was: 2017-Feb-23
One month from today will be: 2017-Apr-23