1、timer

不同于系统函数的timer()一般生成一个定时器,boost中的timer是一个计时器,以秒为单位,最小精度为毫秒,使用需要包含头文件"boost\timer.hpp",下面是它的使用方法:

    boost::timer t; //对象定义后就开始计时
cout << "max timespan: " << t.elapsed_max() / << "h" << endl; //输出最大计时范围,596.523h
cout << "min timespan: " << t.elapsed_min() << "s" << endl; //输出最小计时范围,0.001s
cout << "now time elapsed: " << t.elapsed() << "s" << endl; //输出当前计时

2、date日期类

data_time库需要编译它来使用,它主要包含date日期类、日期长度类days、weeks、months、years,日期迭代器等。data_time只支持1400-9999年之间的日期,如果创建的日期对象使用了其它的值则会抛出异常,使用需要包含头文件"boost\date_time\gregorian\gregorian.hpp"
  下面是定义date日期类对象的方法:

    boost::gregorian::date d; //创建了一个无效的日期对象
boost::gregorian::date d1(, , );
boost::gregorian::date d2(, boost::date_time::Jan, );
boost::gregorian::date d3 = boost::gregorian::from_string("1999-12-31");
boost::gregorian::date d4 = boost::gregorian::from_string("1999/12/31");
boost::gregorian::date d5 = boost::gregorian::from_undelimited_string("");

date类支持流输入输出、比较操作,也包含许多有用的成员函数:

    boost::gregorian::date d; //创建了一个无效的日期对象
boost::gregorian::date d1(, , );
boost::gregorian::date d2(, , ); assert(d == boost::gregorian::date(boost::date_time::not_a_date_time)); //date对象可以进行比较,not_a_date_time表示无效时间
assert(d1 > d2); cout << d1 << endl; //date类对象支持流输入输出,输出d1的日期, 2010-Jan-01
boost::gregorian::date d3 = boost::gregorian::day_clock::local_day(); //获取当前日期date对象(本地时间)
cout << boost::gregorian::day_clock::local_day() << endl; //2018-Apr-10
cout << boost::gregorian::day_clock::universal_day() << endl; //获取当前日期date对象(UTC时间), 2018-Apr-10 assert(d1.year() == ); //获取年
assert(d1.month() == ); //获取月
assert(d1.day() == ); //获取日 boost::gregorian::date::ymd_type ymd = d1.year_month_day(); //获取年月日
assert(ymd.year == );
assert(ymd.month == );
assert(ymd.day == ); cout << d1.day_of_week() << endl; //获取是星期几,0为周日
cout << d1.end_of_month() << endl; //获取当月最后一天的date对象
cout << d1.day_of_year() << endl; //获取是当年的第几天
cout << d1.week_number() << endl; //获取是当年的第几周,范围是0-53,如果年初的几天位于去年的周则为53

将date类对象转换为string字符串:

    boost::gregorian::date d1(, , );
cout << boost::gregorian::to_iso_extended_string(d1) << endl; //2010-01-01
cout << boost::gregorian::to_iso_string(d1) << endl; //
cout << boost::gregorian::to_simple_string(d1) << endl; //2010-Jan-01

date与C中tm结构转换:

    boost::gregorian::date d1(, , );

    boost::gregorian::to_tm(d1); //将date转换为tm,tm结构的时分秒均置为0,夏令时标志tm_isdst设为-1表示未知
tm datetm;
boost::gregorian::date dat = boost::gregorian::date_from_tm(datetm);//将tm转换为date,只使用tm的年、月、日三个成员

3、日期长度类days、weeks、months、years

日期长度类date_duration是以天为单位的时长,可正可负,支持==、<、+、-、++、*等运算。使用它的构造函数创建一个日期长度,成员函数days()返回时长的天数,成员函数is_negative()用来判断是否为负。

date_duration有一个常用的typedef:days,另外类似的还有weeks、months、years三个时长类:

    boost::gregorian::date d1(, , );

    boost::gregorian::days dd1(), dd2(-), dd3();
assert(dd1 > dd2);
assert(dd1 + dd2 == boost::gregorian::days(-));
boost::gregorian::weeks w();
assert(w.days() == );
boost::gregorian::months m();
assert(m.number_of_months() == );
boost::gregorian::years y();
d1 += y;
assert(d1.year() == );

4、 日期运算

date类对象支持流输入输出、比较操作、减法运算,也可以与days、weeks、months、years进行加减运算,date与months、years进行加减运算时,当date的天数是28、29、30时,如果加减运算的结果是2月份,那么date中原来的天数信息会丢失,总是月末最后一天:

    boost::gregorian::date d1(, , ), d2(, , );
int s = (d2 - d1).days(); //3113天
d1 += boost::gregorian::months();
assert(d1.day() != );
d1 += boost::gregorian::days();

5、日期迭代器

日期迭代器有date_iterator、weak_iterator、month_iterator、year_iterator,他们分别以天、周、月、年为单位增减,日期迭代器在构造的时候需要传入一个date对象以指明起始日期和增减步长:

    boost::gregorian::date d(, , );
boost::gregorian::day_iterator d_iter(d); //增减步长默认为1
++d_iter;
assert(d_iter->day() == );

迭代器的应用示例:

    //一个人出生于2000年2月10日,输出它18岁生日的那个月每天的日期、星期以及计算该月一共有几个星期天
boost::gregorian::date dBir(, , );
boost::gregorian::date d2 = dBir + boost::gregorian::years() - boost::gregorian::days(); boost::gregorian::day_iterator d_iter2(d2);
int iCount = ;
for (; d_iter2 <= d2.end_of_month(); ++d_iter2)
{
cout << *d_iter2 << " " << d_iter2->day_of_week() << endl;
if (d_iter2->day_of_week() == boost::gregorian::Sunday)
iCount++;
}
cout << iCount << "Sundays" << endl;

6、其它

判断闰年:

bool bRet = boost::gregorian::gregorian_calendar::is_leap_year();

date_time库提供了很多date日期生成器,如某个月的最后一个星期天、第一个星期一等。

boost--日期处理的更多相关文章

  1. boost 日期时间计算

    示例代码如下: #include <boost/date_time/gregorian/gregorian.hpp> #include <boost/date_time/posix_ ...

  2. boost库thread.hpp编译警告honored已修复

    请浏览:https://svn.boost.org/trac/boost/ticket/7874 #7874: compile warning: thread.hpp:342: warning: ty ...

  3. 《Boost程序库完全开发指南》读书笔记-日期时间

    ●timer库 #include <boost\timer.hpp> #include <boost\progress.hpp> 1.timer类 // timer类的示例. ...

  4. (一)boost库之日期、时间

    (一)boost库之日期.时间 一.计时器  计时器,通常在一个项目中统计一个函数的执行时间是非常实用的.   #include <boost/timer.hpp> void PrintU ...

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

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

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

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

  7. boost 时间与日期处理

    博客转载自: 类 特点 缺点 说明 timer 计时基类 不适合大跨度时间 适用大部分的普通计时 progress_timer 继承自timer 可以自动写入流中 只精确到0.01s 如果需要更精确, ...

  8. boost之日期date_time

    date_time库使用的日期基于格里高利历,支持从1400-01-01到9999-12-31的日期. 空的构造函数会创建一个值为not_a_date_time的无效日期:顺序传入年月日值则创建一个对 ...

  9. c++ boost库学习一:时间和日期

    timer类 #include <boost\timer.hpp> #include "iostream" using namespace std; int _tmai ...

  10. Boost学习笔记(二) 时间与日期

    timer库概述 timer库包含三个组件:分别是计时器类timer.progress_timer和进度指示类progress_display timer 主要作用是计时,精确度是毫秒级.下面是一个简 ...

随机推荐

  1. ps-如何去水印

    现在,版权意识越来越明显了,所以加水印的图片越来越多了,但我们在一些特定的情况又不得不去使用那些图片,去水印又是问题.今天,我来说下如何去水印. 一.ps-仿制图章工具去水印 1.打开ps,打开待去水 ...

  2. C#将List<T>转化为DataTable

    using System; using System.Collections.Generic; using System.Data; using System.Reflection; using Sy ...

  3. linux tail 命令详解

    linux ---tail命令 linux中tail命令---用于查看文件内容 最基本的是cat.more和less. 1. 如果你只想看文件的前5行,可以使用head命令,如: head -5 /e ...

  4. android 开发 View _16 自定义计步器View、自定义柱状图View

    /** *@content:实现计步的环形View *@time:2018-7-30 *@build: */ public class CountStepsAnnularView extends Vi ...

  5. 代码: jquery 插件开发(自用插件)

    http://www.imooc.com/learn/99 阿当大话西游之WEB组件 2016-4-19 jquery插件开发: 2016-3-1 http://www.cnblogs.com/Way ...

  6. oracle 11g密码过期问题解决方法

    ORACLE 11G密码过期问题: 1.使用oracle用户进入sql编辑器中执行修改密码(原始密码,保持不变)的命令 sql>alter user 用户名 identified by &quo ...

  7. 开启safe_mode之后对php系统函数的影响

    safe_mode即为PHP的安全模式,在php.ini中设置safe_mode = On重启PHP便可开启安全模式. 当安全模式开启后,PHP相应的一些系统函数,文件操作函数等将会受限.例如: ck ...

  8. 用java语言构建一个网络服务器,实现客户端和服务器之间通信,实现客户端拥有独立线程,互不干扰

    服务器: 1.与客户端的交流手段多是I/O流的方式 2.对接的方式是Socket套接字,套接字通过IP地址和端口号来建立连接 3.(曾经十分影响理解的点)服务器发出的输出流的所有信息都会成为客户端的输 ...

  9. Html转成Image

    html转图片,原来用了html2image.jar,但是做不到复杂的css js渲染效果. 在网友推荐下,可以用wkhtmltoimage插件,这是用谷歌浏览器的内核webkit,网上搜一下可以下载 ...

  10. mysql win10x64 免安装版 安装配置

    安装包下载或者 gaobo百度云/工具/开发工具/mysql-5.7.23-winx64.zip 第一步, 解压MySQL压缩包    将以下载的MySQL压缩包解压到自定义目录下,我的解压目录是:  ...