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.

boost datetime的更多相关文章

  1. Linux上安装使用boost入门指导

    Data Mining Linux上安装使用boost入门指导 获得boost boost分布 只需要头文件的库 使用boost建立一个简单的程序 准备使用boost二进制文件库 把你的程序链接到bo ...

  2. Win7下Boost库的安装

    Boost库是C++领域公认的经过千锤百炼的知名C++类库,涉及编程中的方方面面,简单记录一下使用时的安装过程 1.boost库的下载 boost库官网主页:www.boost.org 2.安装 将下 ...

  3. VS2008下直接安装使用Boost库1.46.1版本号

    Boost库是一个可移植.提供源码的C++库,作为标准库的后备,是C++标准化进程的发动机之中的一个. Boost库由C++标准委员会库工作组成员发起,当中有些内容有望成为下一代C++标准库内容.在C ...

  4. [Boost]boost的时间和日期处理-(1)日期的操作

    <开篇> Boost.DateTime库提供了时间日期相关的计算.格式化.转换.输入输出等等功能,为C++的编程提供了便利.不过它有如下特点: 1. Boost.DateTime 只支持1 ...

  5. [Boost]boost的时间和日期处理-(2)时间的操作

    <开篇> 本篇紧接着boost上篇叙述Boost::DateTime的时间处理.在C++中,常见的时间有time_t, FILETIME和tm,而boost中用ptime. 构造ptime ...

  6. VS2008下直接安装使用Boost库1.46.1版本

    Boost库是一个可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一. Boost库由C++标准委员会库工作组成员发起,其中有些内容有望成为下一代C++标准库内容.在C++ ...

  7. VS2008下直接安装Boost库1.46.1版本号

    Boost图书馆是一个移植.提供源代码C++库.作为一个备份标准库,这是C++发动机之间的一种标准化的过程. Boost图书馆由C++图书馆标准委员会工作组成员发起,一些内容有望成为下一代C++标准库 ...

  8. Boost中的网络库ASIO,nginx

    boost C++ 本身就是跨平台的,在Linux.Unix.Windos上都可以使用. Boost.Asio  针对网络编程,很多服务端C++开发使用此库. 这个库在以下的平台和编译器上测试通过: ...

  9. 在IDE中集成boost

    1. 获得Boost 进入Boost的网站(http://www.boost.org/) 下载boost_1_62_0.zip 2. 解压Boost 解压 boost_1_62_0.zip ,比如解压 ...

随机推荐

  1. JS replace方法

    var str = '1abc2defg3hijk'; str.replace(/\d/g,function(a,b,c,d){ console.log("a:",a);// 匹配 ...

  2. sqlalchemy.exc.IntegrityError: (mysql.connector.errors.IntegrityError) 1062 (23000): Duplicate entry '1' for key 'PRIMARY'

    排错: 看到 Duplicate entry '1' for key 'PRIMARY'是主键错误 看一看自己添加的数据id是1 再查询一下数据库中的表,发现id=1的记录已经存在了 所以在代码中让i ...

  3. quick BI 修改列名备注

    有些列名太长了,所以造成些影响.注意修改即可.

  4. 【ABAP系列】SAP ABAP中关于commit的一点解释

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP中关于commi ...

  5. Eclipse如何汉化[完美版]

    当前版本:Eclipse 4.5.1 1.如何查看eclipse的版本呢 找到关于Eclipse,点击 . 2.打开浏览器连接http://www.eclipse.org/babel/download ...

  6. 解决ubuntu下eth0不显示

    今天电脑重启之后,用ifconfig查看网络地址,就发现eth0神奇的消失了,顿时感觉吓尿了. 按照网上看到的资料,发现输入ifconfig -a 发现可以显示eth0,但是当输入ifconfig就没 ...

  7. [Codeforces 280D]k-Maximum Subsequence Sum(线段树)

    [Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...

  8. go 学习之fmt包

    基本模式fmt 方法可以大致分为 print, scan两类, 根据基础方法可以构建特定方法. Print 将参数写入字符串或io.writer scan 从字符串或 io.Reader 读取指定数据 ...

  9. TensorFlow 安装报错的解决办法

    最近关注了几个python相关的公众号,没事随便翻翻,几天前发现了一个人工智能公开课,闲着没事,点击了报名. 几天都没有音信,我本以为像我这种大龄转行的不会被审核通过,没想到昨天来了审核通过的电话,通 ...

  10. js/jquery中什么时候用return,什么时候用return false

    根本的说 return 是函数的返回结果用, 如果你一个函数需要执行结果那就return 你需要的结果, 不需要结果就不用return而在jq中有些特殊的用法,比如$().each(function( ...