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. mysql 之 frm+ibd文件还原data

      此方法只适合innodb_file_per_table          = 1 当误删除ibdata 该怎么办? 如下步骤即可恢复: 1.准备工作 1)准备一台纯洁的mysql环境[从启动到现在 ...

  2. 1204C Anna, Svyatoslav and Maps

    题目大意 给你一个有向图和一个路径 让你在给定路径中选出尽量少的点使得新路径的最短路长度和原路径相等 给定路径相邻两点间距离为1 分析 先floyd求出两点间最短路 之后每次对于点i找到所有跟它的最短 ...

  3. Vue过渡:CSS过渡

    一 项目结构 二 App.vue <template> <div id="app"> <transition name="fade" ...

  4. Java中的类修饰符

    资料主要来源于网络(http://60.28.60.3/zy/java-new/zhishidian/chap3/3.htm) 之前每次写小测试程序的时候,总是把一个类放在一个Java文件中,按理说这 ...

  5. php编程怎么和mysql连接

    php连接mysql的方法: MySQLi - 面向对象 MySQLi - 面向过程 关闭连接 连接在脚本执行完后会自动关闭.你也可以使用以下代码来关闭连接: (MySQLi - 面向对象 MySQL ...

  6. python网络编程中互斥锁与进程之间的通信

    一.互斥锁 进程之间数据隔离,但是共享一套文件系统,因而可以通过文件来实现进程直接的通信,但问题是必须自己加锁处理. 注意:加锁的目的是为了保证多个进程修改同一块数据时,同一时间只能有一个修改,即串行 ...

  7. Throwable -抛出异常类与自定义异常类

    /* 自定义异常类 java提供的异常类,不够我们使用,需要自己定义一些异常类 格式: public class XXXException extends Exception/runtimeExcep ...

  8. PHP学习:set_time_limit,max_execution_time,sleep

    set_time_limit 设置脚本最大允许执行时间,可以在php脚本中使用, 参数为秒,如果为0,表示无时间限制: set_time_limit(seconds); max_execution_t ...

  9. Redis在windows下的环境搭建

    Redis在windows下的环境搭建 下载windows版本redis,,官方下载地址:http://redis.io/download, 不过官方没有Windows版本,官网只提供linux版本的 ...

  10. JS 页面繁简字转换

    // 本js用于客户在网站页面选择繁体中文或简体中文显示,默认是正常显示,即简繁体同时显示// 在用户第一次访问网页时,会自动检测客户端语言进行操作并提示.此功能可关闭// 本程序只在UTF8编码下测 ...