boost datetime
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的更多相关文章
- Linux上安装使用boost入门指导
Data Mining Linux上安装使用boost入门指导 获得boost boost分布 只需要头文件的库 使用boost建立一个简单的程序 准备使用boost二进制文件库 把你的程序链接到bo ...
- Win7下Boost库的安装
Boost库是C++领域公认的经过千锤百炼的知名C++类库,涉及编程中的方方面面,简单记录一下使用时的安装过程 1.boost库的下载 boost库官网主页:www.boost.org 2.安装 将下 ...
- VS2008下直接安装使用Boost库1.46.1版本号
Boost库是一个可移植.提供源码的C++库,作为标准库的后备,是C++标准化进程的发动机之中的一个. Boost库由C++标准委员会库工作组成员发起,当中有些内容有望成为下一代C++标准库内容.在C ...
- [Boost]boost的时间和日期处理-(1)日期的操作
<开篇> Boost.DateTime库提供了时间日期相关的计算.格式化.转换.输入输出等等功能,为C++的编程提供了便利.不过它有如下特点: 1. Boost.DateTime 只支持1 ...
- [Boost]boost的时间和日期处理-(2)时间的操作
<开篇> 本篇紧接着boost上篇叙述Boost::DateTime的时间处理.在C++中,常见的时间有time_t, FILETIME和tm,而boost中用ptime. 构造ptime ...
- VS2008下直接安装使用Boost库1.46.1版本
Boost库是一个可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一. Boost库由C++标准委员会库工作组成员发起,其中有些内容有望成为下一代C++标准库内容.在C++ ...
- VS2008下直接安装Boost库1.46.1版本号
Boost图书馆是一个移植.提供源代码C++库.作为一个备份标准库,这是C++发动机之间的一种标准化的过程. Boost图书馆由C++图书馆标准委员会工作组成员发起,一些内容有望成为下一代C++标准库 ...
- Boost中的网络库ASIO,nginx
boost C++ 本身就是跨平台的,在Linux.Unix.Windos上都可以使用. Boost.Asio 针对网络编程,很多服务端C++开发使用此库. 这个库在以下的平台和编译器上测试通过: ...
- 在IDE中集成boost
1. 获得Boost 进入Boost的网站(http://www.boost.org/) 下载boost_1_62_0.zip 2. 解压Boost 解压 boost_1_62_0.zip ,比如解压 ...
随机推荐
- php str_replace与substr_replace的区别
函数定义: str_replace() :函数替换字符串中的一些字符(区分大小写). substr_replace() :函数把字符串的一部分替换为另一个字符串. 区别: str_replace()和 ...
- python 使用xlsxwriter的方法属性
http://xlsxwriter.readthedocs.io/format.html
- SSL异常javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
jdk 7 http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html jdk 8 http: ...
- Step2 - How to: Implement a Windows Communication Foundation Service Contract
This is the second of six tasks required to create a basic Windows Communication Foundation (WCF) se ...
- EZOJ #374学习
分析 二分天数 暴力判断即可 代码 #include<bits/stdc++.h> using namespace std; #define int long long ],b[],c[] ...
- Node.js - 使用 Express 和 http-proxy 进行反向代理
安装 Express 和 http-proxy npm install --save express http-proxy 反向代理代码 proxy.js var express = require( ...
- Bootstrap 学习笔记6 列表组面板嵌入组件
列表组组件: 面板组件:
- hdu6575Budget
Problem Description Avin’s company has many ongoing projects with different budgets. His company rec ...
- Hibernate异常:IllegalArgumentException
异常信息: java.lang.IllegalArgumentException: attempt to create delete event with null entity at org.hib ...
- PHP日期加减函数
<?phpecho "今天:",date('Y-m-d H:i:s'),"<br>";echo "明天:",date('Y ...