boost 时间与日期处理

时间:2022-09-15 09:26:41

博客转载自:

特点
缺点
说明
timer
计时基类
不适合大跨度时间
适用大部分的普通计时
progress_timer
继承自timer 可以自动写入流中
只精确到0.01s
如果需要更精确,可派生个类,调用stream的precision设置
progress_display 图形化显示进度 只能输出到cout 如果还有其他输出则会干扰进度显示。
折中的办法是重新显示 pd.restart(size); pd+= pNum;
date 日期结构,时间点 —— date是date_time库的核心类 boost::gregorian
date_duration days、months、years 时间段 —— 表示一段时间,可以把它看成一个int
date_period 标量,左开右闭,时间区间 —— 可以认为是一个有起点的date_duration。能做交集、并集
date_iterator 迭代器,以某个单位增减 —— 天、周、月、年四种迭代器,以某种增量移动。
time_duration 时间段 同date_duration —— hours、minutes、seconds、millisec、boost::posix_time
ptime 时间点 date+time_duration —— 分date()和time_of_day()操作。
time_period 时间区间 同date_period —— ——
time_iterator 迭代器,以某个单位增减 —— 可直接与ptime比较
date_facet 流格式化日期 —— %Y年%m月%d日
time_facet 流格式化时间 —— %Y年%m月%d日 %H点%M分%S%F秒
#include <boost/timer.hpp>
#include <boost/progress.hpp>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <Windows.h> #include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time.hpp> using namespace std; int main()
{
boost::timer t;
std::cout<<"Max "<<t.elapsed_max()<<endl;
std::cout<<"Min "<<t.elapsed_min()<<endl;
std::cout<<"elapsed: "<<t.elapsed()<<endl;
t.restart();
Sleep(100);
std::cout<<"elapsed: "<<t.elapsed()<<endl;
cout<<"---------------------------"<<endl;
stringstream ss;
{
boost::progress_timer t(ss);
Sleep(300);
}
cout<<ss.str();
cout<<"---------------------------"<<endl; vector<string> v(100);
//Do Data Fill......
ofstream fs("c:\test.txt"); boost::progress_display pd(v.size());
vector<string>::iterator pos;
for (pos = v.begin();pos != v.end();++pos)
{
fs<<*pos<<endl;
Sleep(10);
++pd;
//pd.restart(v.size());
//pd+=(pos-v.begin() +1);
}
cout<<"---------------------------"<<endl; {
using namespace boost::gregorian;
cout<<"----------------- date ------------------"<<endl;
date d1;
date d2(2013,4,7);
date d3(2013,Apr,7);
date d4(d2); assert(d1 == date(not_a_date_time)); //默认初始化为无效日期
assert(d2 == d4);
assert(d3 == d2); d1 = from_string("1999,9,9");
date d5 (from_string("2008/8/8"));
d3 = from_undelimited_string("20110111"); cout<<day_clock::local_day()<<endl;
cout<<day_clock::universal_day()<<endl; date d6 (neg_infin);
date d7(pos_infin);
cout<<d6<<endl;
cout<<d7<<endl; cout<<"---------------------------"<<endl;
date today (2013,4,17);
assert(today.year() == 2013);
assert(today.month() == 4);
assert(today.day() == 17); date::ymd_type ymd = today.year_month_day();
assert(ymd.year == 2013);
assert(ymd.month == 4);
assert(ymd.day == 17); assert(today.day_of_week() == 3); //星期几 周日为0
cout<<today.day_of_year()<<endl; //在一年中是第几天
assert(today.end_of_month() == date(2013,4,30)); //当月的最后一天
cout<<today.week_number()<<endl; //当年的第几周 范围0~53 年初的半周归为上一年,即53
assert(d6.is_infinity()); //日期为无限日期
assert(d6.is_neg_infinity());
cout<<"---------------------------"<<endl; cout<<to_simple_string(today)<<endl;
cout<<to_iso_string(today)<<endl;
cout<<to_iso_extended_string(today)<<endl; //常用日期格式YYYY-MM-DD
cout<<today<<endl; cout<<"---------------------------"<<endl;
tm t = to_tm(today);
assert(t.tm_hour == 0 && t.tm_min == 0); date new_today = date_from_tm(t); //从tm转为date
assert(new_today == today); cout<<"-------------- days(date_duration) --------------"<<endl;
days dd1(10),dd2(-20),dd3(365);
assert(dd1>dd2 &&dd1<dd3);
assert(dd1+dd2 == days(-10));
assert((dd2+dd3).days() == 345);
assert(dd3/5 == days(73)); weeks w(3); //3个星期
assert(w.days() == 21); months m(5);
years y(2); months m2 = y+m;
assert(m2.number_of_months() == 29);
assert((y*2).number_of_years() == 4); cout<<"-------------- Calc --------------"<<endl;
date dA(2000,1,1),dB(2008,8,8);
cout<<dB-dA<<endl; //3142天 dA+=days(10);
assert(dA.day() == 11);
dA+=months(2);
assert(dA.month() ==3 && dA.day()== 11); dA-=weeks(1);
assert(dA.day() == 4); dB-=years(7);
assert(dA.year() == dB.year()-1); //如果日期是月末的最后一天,加减月或年会得到月末的时间,而不是简单的月、年加1
date sp(2013,3,30);
sp-=months(1);
assert(sp.month() == 2 && sp.day() == 28);
sp -=months(1);
assert(sp.month()== 1 && sp.day()== 31);
sp+=months(2);
assert(sp.day() == 31); //与原来的日期已经不相等! cout<<"-------------- date_period --------------"<<endl;
date_period dp(date(2013,4,17),days(14)); //左开右闭与STL的容器相似
assert(!dp.is_null());
assert(dp.begin().day() == 17);
assert(dp.last().day() == 30);
assert(dp.end().day() == 1); cout<<dp<<endl; date_period new_dp = dp;
new_dp.shift(days(3)); //将时间区间向后移动
assert(new_dp.begin().day() == 20);
assert(new_dp.length().days() == 14); new_dp.expand(days(3)); //区间两段延长n天,即延长2n天。
assert(new_dp.begin().day() == 17);
assert(new_dp.length().days() == 20); assert(dp.is_after(date(2013,1,1)));
assert(dp.contains(date(2013,4,20))); date_period dp2 (date(2013,4,17),days(5));
assert(dp.contains(dp2)); assert(dp.intersects(dp2)); //交集
assert(dp.intersection(dp2) == dp2); date_period dp3 (date(2013,5,1),days(5));
assert(!dp3.intersects(dp));
assert(dp3.intersection(dp2).is_null()); assert(dp.is_adjacent(dp3)); date_period dp4(date(2013,4,17),days(19)); //并集
assert(dp.merge(dp3).is_null()); //无交集返回空
assert(dp.span(dp3) == dp4); //填充中间区域 cout<<"-------------- date_iterator --------------"<<endl;
date last(2013,4,17); day_iterator d_iter(last); //日期迭代器 assert(d_iter == last);
++d_iter;
assert(d_iter == date(2013,4,18)); year_iterator y_iter(*d_iter,3); //增减步长为3
assert(y_iter == last + days(1)); ++y_iter;
assert(y_iter->year() == 2016); cout<<"-------------- func --------------"<<endl;
cout<<(gregorian_calendar::is_leap_year(2000)? "Yes":"no")<<endl; //闰年
assert(gregorian_calendar::end_of_month_day(2013,2) == 28); //月末天 } {
using namespace boost::posix_time;
cout<<"-------------- time_duration --------------"<<endl;
time_duration td(1,1,1); //时、分、秒 会自动借、进位
hours h0(1);
minutes m(1);
seconds s(1);
millisec ms(1); time_duration td2 = h0+m+s+ms;
time_duration td3 = hours(2) + minutes(10);
time_duration td4 = duration_from_string("1:10:10:300"); assert(td4.hours() == 1 && td4.minutes() == 10 && td4.seconds() == 10);
assert(td.total_seconds() == 1*3600 + 1*60 +1); //转为sec hours h(-10);
assert(h.is_negative()); time_duration h2 = h.invert_sign(); //取反
assert(!h2.is_negative() && h2.hours() == 10); cout<<td3-td2<<endl;
cout<<to_simple_string(td4)<<endl;
cout<<to_iso_string(td4)<<endl; cout<<"-------------- ptime --------------"<<endl;
{
using namespace boost::gregorian;
ptime p(date(2013,4,17),hours(1)); //ptime相当于date+time_duration
ptime p1 = time_from_string("2013-4-17 16:25:00");
cout<<p<<endl;
cout<<p1<<endl;
ptime p2 = second_clock::local_time(); //常用时间输出
ptime p3 = microsec_clock::universal_time(); //微秒精度
cout<<p2<<endl<<p3<<endl; ptime op(date(2013,4,17),hours(1)+minutes(30)); date d = op.date();
time_duration optd = op.time_of_day();
assert(d.day() == 17 && d.month() == 4);
assert(optd.hours() == 1 && optd.minutes() == 30);
cout<<to_iso_extended_string(op)<<endl; tm t = to_tm(op); //不可逆,此处与date不同
//只能用date_from_tm先得到日期,再填充时间。 cout<<"-------------- time_period --------------"<<endl;
time_period tp1 (op,hours(8));
time_period tp2(op+hours(8),hours(1));
assert(tp1.end() == tp2.begin() && tp1.is_adjacent(tp2));
assert(!tp1.intersects(tp2)); tp1.shift(hours(1));
assert(tp1.is_after(op));
assert(tp1.intersects(tp2)); tp2.expand(hours(10));
assert(tp2.contains(op) && tp2.contains(tp1)); cout<<"-------------- time_iterator --------------"<<endl;
for (time_iterator t_iter(op,minutes(10));t_iter<op+hours(1);++t_iter)
{
cout<<*t_iter<<endl;
}
cout<<"-------------- formate --------------"<<endl;
date_facet* dfacet = new date_facet("%Y 年%m 月%d 日");
cout.imbue(locale(cout.getloc(),dfacet));
cout<<date(2013,4,17)<<endl; time_facet* tfacet = new time_facet("%Y 年%m 月%d 日 %H点%M分%S%F秒");
cout.imbue(locale(cout.getloc(),tfacet));
cout<<op<<endl;
}
} getchar();
return 0;
}

  

运行结果:

Max 2.14748e+006
Min 0.001
elapsed: 0.001
elapsed: 0.1
---------------------------
0.30 s
---------------------------
% %
|----|----|----|----|----|----|----|----|----|----|
***************************************************
---------------------------
----------------- date ------------------
-Apr-
-Apr-
-infinity
+infinity
--------------------------- ---------------------------
-Apr- --
-Apr-
---------------------------
-------------- days(date_duration) --------------
-------------- Calc -------------- -------------- date_period --------------
[-Apr-/-Apr-]
-------------- date_iterator --------------
-------------- func --------------
Yes
-------------- time_duration --------------
::58.999000
::10.300000
011010.300000
-------------- ptime --------------
-Apr- ::
-Apr- ::
-Apr- ::
-Apr- ::21.870604
--17T01::
-------------- time_period --------------
-------------- time_iterator --------------
-Apr- ::
-Apr- ::
-Apr- ::
-Apr- ::
-Apr- ::
-Apr- ::
-------------- formate --------------
年04 月17 日
年04 月17 日 01点30分00秒

boost 时间与日期处理的更多相关文章

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

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

  2. Android随笔之——Android时间、日期相关类和方法

    今天要讲的是Android里关于时间.日期相关类和方法.在Android中,跟时间.日期有关的类主要有Time.Calendar.Date三个类.而与日期格式化输出有关的DateFormat和Simp ...

  3. Lua库之时间和日期操作

    Lua库之时间和日期操作 (2010-02-07 18:41:20) 转载▼ os.time() <== 返回当前系统的日历时间os.date() <== 返回本地化的时间字符串,这里是& ...

  4. date 显示或设置系统时间和日期

    显示或设置系统时间和日期 date [options] [+format] date [options] [new date] date用来显示系统的时间和日期,超级用户可以使用date来更改系统时钟 ...

  5. java时间和日期类型

    在java中,代表时间和日期的类型包括:java.util.Date和java.util.Calendar,此外,在JDBC API中还提供了3个扩展类,java.UtilDate类的子类:java. ...

  6. iOS中的时间和日期

    怎么说?时间和日期不是了不起的属性.了不起的功能,但是,我们决不能够因此就“冷落”它. 一:怎么“搞到货”--如何获取时间.日期 //-=-==当前时间------默认显示“0时区”时间 NSDate ...

  7. Python 时间和日期模块的常用例子

    获取当前时间的两种方法 import datetime,time now = time.strftime("%Y-%m-%d %H:%M:%S") print now now = ...

  8. Java 对时间和日期的相关处理

    1. 获取当前系统时间和日期并格式化输出 import java.util.Date; import java.text.SimpleDateFormat; public class NowStrin ...

  9. PHP date 格式化一个本地时间/日期

    PHP date 格式化一个本地时间/日期 date (PHP 4, PHP 5) date — 格式化一个本地时间/日期 说明 string date ( string $format [, int ...

随机推荐

  1. 编译安装zabbix3&period;2

    1.1 环境准备 系统环境准备:redhat 6.6 64位mysql-5.6.34php-5.6.28zabbix-3.2.1配置前先关闭iptables和SELINUX,避免安装过程中报错. # ...

  2. ubuntu &&num;39&semi;Unable to correct problems&comma; you have held broken packages&&num;39&semi; 错误

    在用apt 安装软件时,有时会用国内的源以加快下载速度. 但是在使用ubuntu 14.04的过程中,这一过程可能会导致错误“Unable to correct problems, you have ...

  3. Hive安装

    一.下载:http://hive.apache.org/,选择合适的版本,Hive 1.X版本要求Hadoop2.x以上版本,Jdk1.7以上 这里选择1.2.1版本 二.安装jdk 略 三.安装Ha ...

  4. 最长递增子序列LIS再谈

    DP模型: d(i) 以第 i 个元素结尾的最长递增子序列的长度. 那么就有 d(i) = max(d(j)) + 1;(j<i&&a[j]<a[i]),答案 max(d( ...

  5. TX Textcontrol 使用总结二——常见异常

    在使用Tx text control中间,我们经过会遇到在开发人员自己的电脑上我们的程序是可以正常允许的,但当部署到客户端却往往会发现一些意想不到的问题 如下所示: 未能加载文件或程序集“txtool ...

  6. Filter过滤器实现登录检查

    主要利用filter过滤掉未经登录而直接跳转到非登录访问页面.代码而言的话并不难,只是有几点问题需要注意一下. 1.使用filter需要配置web.xml,如果是/*那么在拦截后的页面会连带jsp页面 ...

  7. 关于Axis 1&period;4 环境的搭建问题

    本来很简单的一个环境搭建问题足足困扰了我一周的时间,所以思来想去还是写一篇博文记录下来,以后就不用那么四处去找资料找例子了,实在是浪费时间 废话不多说 1  首先在MyEclipse下创建WEB PR ...

  8. Loadrunner之文件的下载(八)

    老猪提供: https://mp.weixin.qq.com/s?__biz=MzIwOTMzNDEwNw==&mid=100000013&idx=1&sn=624f5bc74 ...

  9. js-注释代码习惯

    功能块代码 /** * xxxx */ 定义的函数或方法 /* xxxx */ 调用了某个函数或方法 // <--xxx

  10. MFC中小笔记&lpar;二&rpar;

    6.有三个API函数可以运行可执行文件WinExec.ShellExecute和CreateProcess.  关于这三者的概述总结,有好几篇,自己选择. 1.CreateProcess因为使用复杂, ...