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. HttpClient请求服务器图片

    我们先引入一个IO流相关的Jar包, 从apache下载 下载后,jar包和源码如图: 我们只需要将jar包引入项目: 之后我们使用FileUtils这个类,其中有一个文件复制方法. 我们将请求的图片 ...

  2. base64编码操作图片

    package com.trsmedia.service; import java.io.FileInputStream; import java.io.FileOutputStream; impor ...

  3. 下载工具 qBittorrent 使用

    官网地址,软件可以在官网上下载. GitHub 源码 知乎的参考链接 qBittorrent 是开源软件,支持用 BT 种子或种子的链接下载,也可以用磁力链接进行下载. 搜索功能 qBittorren ...

  4. php远程抓取(下载)文件到本项目指定目录中

    function httpcopy($url, $file="", $timeout=60) { $file = empty($file) ? pathinfo($url,PATH ...

  5. 【ABAP系列】SAP ABAP基础-abap数据类型的解析整理

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP基础-abap数 ...

  6. POJ - 3176 Cow Bowling 动态规划

    动态规划:多阶段决策问题,每步求解的问题是后面阶段问题求解的子问题,每步决策将依赖于以前步骤的决策结果.(可以用于组合优化问题) 优化原则:一个最优决策序列的任何子序列本身一定是相当于子序列初始和结束 ...

  7. 类型(type)判断

    windows下源文件编码问题 在windows下不要直接右击桌面创建.txt再改成.c,这种方式容易引起编码问题 windows下gvim的设置: 先打开gvim再用:w newfile.c这种方式 ...

  8. C#获取本地路径

    /// <summary> /// 本地路径 /// </summary> /// <param name="path"></param& ...

  9. SQL Server 分页语句查询

    --查询第10页的数据(15条) SELECT TEMP1.* FROM( SELECT TOP 15 ROW_NUMBER() OVER(ORDER BY ID ASC) AS ROWID,* FR ...

  10. SQL server 连接 查询

    在sql server中,我们经常能用到连接,今天总结一下连接的基础知识.连接的分类: 交叉连接CROSS JOIN 内连接INNER JOIN 外连接{左外连接LEFT [OUTER] JOIN : ...