c++ - add_month 从 boost::gregorian 中删除?

标签 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);                                   
               ^                                                   

最佳答案

的确如此。该示例已过时。事实上,我不记得看到过这个功能,所以它可能早就过时了。

我推荐以下方法:

/* 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

关于c++ - add_month 从 boost::gregorian 中删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42978102/

相关文章:

c++ - 有没有办法在 VS 调试器中自定义自定义对象的工具提示?

c++ - 编译的 C 可执行文件被 Windows Defender 检测为病毒

sql-server - SSIS包如何在每次包运行时向文件名添加日期/时间戳

java - DateTimeFormatter 可以格式化日期,但无法读取其自身的格式

c++ - boost 正则表达式。命名组分为两部分

c++ - 与 Boost::geometry 的多边形相交严重性能下降

matlab - 为什么矢量化对 Matlab 程序有益? NumPy 和 Boost(uBLAS) 是否相同?

c++ - 为什么将 const (...)& 放在 C++ 中

C++ 等价于代数数据类型?

javascript - 在 AngularJs 中解析日期