To create a date, use the class boost::gregorian::date

1. date

#include <boost/date_time/gregorian/gregorian.hpp>
#include <iostream> int main()
{
boost::gregorian::date d(, , 31);
std::cout << d.year() << std::endl;
std::cout << d.month() << std::endl;
std::cout << d.day() << std::endl;
std::cout << d.day_of_week() << std::endl;
std::cout << d.end_of_month() << std::endl;
  date d = day_clock::universal_day();
std::cout << d.year() << std::endl;
std::cout << d.month() << std::endl;
std::cout << d.day() << std::endl'; d = date_from_iso_string("20140131");
std::cout << d.year() << std::endl;
std::cout << d.month() << std::endl;
std::cout << d.day() << std::endl;
  return ;
}

boost::gregorian::date provides several constructors to create dates. The most basic constructor takes a year, a month, and a day as parameters.

boost::gregorian::date returns the current date. The member function universal_day() returns a UTC date, which is independent of time zones and daylight savings. boost::gregorian::day_clock also provides a member function called local_day(), which takes local settings into account. To retrieve the current date within the local time zone, use local_day().

date_from_iso_string converts a date in the ISO 8601 format. Other functions include: boost::gregorian::from_simple_string() and boost::gregorian::from_us_string().

2. date_duration

#include <boost/date_time/gregorian/gregorian.hpp>
#include <iostream> using namespace boost::gregorian; int main()
{
date d1(, , 31);
date d2(, , 28);
date_duration dd = d2 - d1;
std::cout << dd.days() << std::endl;

date_duration dd{4};
std::cout << dd.days() << std::endl;
weeks ws{4};
std::cout << ws.days() << std::endl;
months ms{4};
std::cout << ms.number_of_months() << std::endl;
years ys{4};
std::cout << ys.number_of_years() << std::endl;

return ;
}

boost::gregorian::date overloads operator-, two points in time can be subtracted. The return value is of type boost::gregorian::date_duration and marks the duration between the two dates. days() returns the number of days in the duration specified.

Objects of type boost::gregorian::date_duration can also be created by passing the number of days as a single parameter to the constructor. To create a duration that involves weeks, months, or years, use boost::gregorian::weeks, boost::gregorian::months, or boost::gregorian::years

3. date_period

#include <boost/date_time/gregorian/gregorian.hpp>
#include <iostream> using namespace boost::gregorian; int main()
{
date d1(, , 1);
date d2(, , 28);
date_period dp{d1, d2};
date_duration dd = dp.length();
std::cout << dd.days() << std::endl; std::cout.setf(std::ios::boolalpha);
std::cout << dp.contains(d1) << std::endl;
std::cout << dp.contains(d2) << std::endl;
return ;
}

The constructor of boost::gregorian::date_period can accept two kinds of input. You can pass two parameters of type boost::gergorian::date, one for the beginning date and one for the end date. Please note that the day before the end date is actually that last day of the period.

dp contains d1 while dose not contain d2.

4. iterator

#include <boost/date_time/gregorian/gregorian.hpp>
#include <iostream> using namespace boost; int main()
{
gregorian::date d(, , 12);
gregorian::day_iterator it{d};
std::cout << *++it << std::endl;
std::cout << date_time::next_weekday(*it, gregorian::greg_weekday(date_time::Friday)) << std::endl;
return 0;
}

Use the iterator boost::gregorian::day_iterator to jump forward or backward by a day from a specific date. Use boost::gregorian::week_iterator, boost::gregorian::month_iterator, and boost::gregorian::year_iterator to jump by weeks, months, or years, respectively.

最新文章

  1. Linux:去除认证,加速 SSH登录
  2. window xp Apache与Tomcat集群配置--转载
  3. scala中的call-by-name和call-by-value
  4. C# inherit
  5. Python内置函数(37)——sorted
  6. prop与attr的区别
  7. android – 无法解析AppCompatActivity
  8. Android远程桌面助手之系统兼容篇
  9. Spring Boot中Web应用的统一异常处理 转载来自翟永超
  10. python3+selenium框架设计04-封装测试基类
  11. spring中@Value(&quot;${key}&quot;)值原样输出${key}分析与解决
  12. 关于spfa
  13. 11: Nginx安装lua支持
  14. 合并CSV文件.bat
  15. python 判断变量是否存在 防止报错
  16. DataStructure.BloomFilter
  17. JavaScript -- Select
  18. 喵哈哈村的魔法考试 Round #3 (Div.2) 题解
  19. jmert jdbc request支持执行多条sql语句并设置jdbc字符集
  20. 【10.6校内测试】【小模拟】【hash+线段树维护覆盖序列】

热门文章

  1. (转)jupyter常用快捷键
  2. UVA 1045 最长公共子序列
  3. day12—jQuery ui引入及初体验
  4. hibernate搭建及其增删改查
  5. groub by 与 over partition by 的区别
  6. Github建站笔记
  7. python学习笔记:__init__.py的作用
  8. idea 快捷键汇总
  9. Netty基础-BIO/NIO/AIO
  10. Java的socket编程中关于bufferedWriter的发送问题