C++ boost 时间与日期处理详细介绍_C 语言

boost 时间与日期处理

导视:



特点

缺点
说明

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
---------------------------
0% 10 20 30 40 50 60 70 80 90 100%
|----|----|----|----|----|----|----|----|----|----|
***************************************************
---------------------------
----------------- date ------------------
2013-Apr-17
2013-Apr-17
-infinity
+infinity
---------------------------
107
16
---------------------------
2013-Apr-17
20130417
2013-04-17
2013-Apr-17
---------------------------
-------------- days(date_duration) --------------
-------------- Calc --------------
3142
-------------- date_period --------------
[2013-Apr-17/2013-Apr-30]
-------------- date_iterator --------------
-------------- func --------------
Yes
-------------- time_duration --------------
01:08:58.999000
01:10:10.300000
011010.300000
-------------- ptime --------------
2013-Apr-17 01:00:00
2013-Apr-17 16:25:00
2013-Apr-17 17:19:21
2013-Apr-17 09:19:21.870604
2013-04-17T01:30:00
-------------- time_period --------------
-------------- time_iterator --------------
2013-Apr-17 01:30:00
2013-Apr-17 01:40:00
2013-Apr-17 01:50:00
2013-Apr-17 02:00:00
2013-Apr-17 02:10:00
2013-Apr-17 02:20:00
-------------- formate --------------
2013 年04 月17 日
2013 年04 月17 日 01点30分00秒 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索c++
, boost
时间与日期
r语言xgboost、r语言xgboost实例、boost 日期、xgboost r语言使用、xgboost 安装 r语言,以便于您获取更多的相关知识。

时间: 2024-08-02 20:16:57

C++ boost 时间与日期处理详细介绍_C 语言的相关文章

C++开发:为什么多线程读写shared_ptr要加锁的详细介绍_C 语言

我在<Linux 多线程服务端编程:使用 muduo C++ 网络库>第 1.9 节"再论 shared_ptr 的线程安全"中写道: (shared_ptr)的引用计数本身是安全且无锁的,但对象的读写则不是,因为 shared_ptr 有两个数据成员,读写操作不能原子化.根据文档(http://www.boost.org/doc/libs/release/libs/smart_ptr/shared_ptr.htm#ThreadSafety), shared_ptr 的线程

C语言 数据类型详细介绍_C 语言

C 数据类型 在 C 语言中,数据类型指的是用于声明不同类型的变量或函数的一个广泛的系统.变量的类型决定了变量存储占用的空间,以及如何解释存储的位模式. C 中的类型可分为以下几种: 序号 类型与描述 1 基本类型: 它们是算术类型,包括两种类型:整数类型和浮点类型. 2 枚举类型: 它们也是算术类型,被用来定义在程序中只能赋予其一定的离散整数值的变量. 3 void 类型: 类型说明符 void 表明没有可用的值. 4 派生类型: 它们包括:指针类型.数组类型.结构类型.共用体类型和函数类型.

C语言 栈的表示和实现详细介绍_C 语言

C语言 栈的表示和实现详细介绍 定义:栈是限定仅在表尾进行插入和删除操作的线性表. 栈作为一种数据结构,是一种只能在一端进行插入和删除操作的特殊线性表.它按照先进后出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据(最后一个数据被第一个读出来).栈具有记忆作用,对栈的插入与删除操作中,不需要改变栈底指针. 栈是允许在同一端进行插入和删除操作的特殊线性表.允许进行插入和删除操作的一端称为栈顶(top),另一端为栈底(bottom):栈底固定,而栈顶浮动:

C语言 位段的详细介绍_C 语言

C语言中的位段       位段(bit-field)是以位为单位来定义结构体(或联合体)中的成员变量所占的空间.含有位段的结构体(联合体)称为位段结构.采用位段结构既能够节省空间,又方便于操作.      位段的定义格式为:      type  [var]: digits     其中type只能为int,unsigned int,signed int三种类型(int型能不能表示负数视编译器而定,比如VC中int就默认是signed int,能够表示负数).位段名称var是可选参数,即可以省

C++ Qt属性系统详细介绍_C 语言

C++ Qt属性系统详细介绍 Qt提供了一个绝妙的属性系统.跟那些由编译器提供的属性差不多.然而,作为一个独立于编译器和平台的库,Qt不依赖于非标准的编译特性,比如__property 或[property].Qt可以在任何平台上的标准编译器下编译.Qt属性系统基于元数据对象系统--就是那个提供了对象内置信号和槽通讯机制的家伙. 声明属性需要什么 要声明一个属性,需在继承自QObject的类中使用Q_PROPERTY()宏. Q_PROPERTY(type name READ getFuncti

使用boost读取XML文件详细介绍_C 语言

boost读取XML文件 boost中提供了对配置文件读取的支持,它就是:property_tree.     basic_ptree 是property_tree的核心基础.其接口像std::list.可以执行很多基本的元素操作,比如使用begin().end()等.     此外还加入了操作属性树的get().get_child().get_value().data()等额外的操作.     basic_ptree有两个重要的内部定义self_type和value_type.self_typ

原码, 反码与补码基础知识详细介绍_C 语言

原码, 反码,补码详解 本篇文章讲解了计算机的原码, 反码和补码. 并且进行了深入探求了为何要使用反码和补码, 以及更进一步的论证了为何可以用反码, 补码的加法计算原码的减法. 论证部分如有不对的地方请各位牛人帮忙指正! 希望本文对大家学习计算机基础有所帮助! 一. 机器数和真值 在学习原码, 反码和补码之前, 需要先了解机器数和真值的概念. 1.机器数 一个数在计算机中的二进制表示形式,  叫做这个数的机器数.机器数是带符号的,在计算机用一个数的最高位存放符号, 正数为0, 负数为1. 比如,

链接库动态链接库详细介绍_C 语言

windows中,链接库分为两种类型:静态链接库.lib和动态链接库.dll.其中动态链接库在被使用的时候,通常还提供一个.lib,称为引入库,它主要提供被Dll导出的函数和符号名称,使得链接的时候能够找到dll中对应的函数映射. 静态链接库和动态链接库的作用相似,都是提供给其他程序进行调用的资源.其中,动态链接库的调用方法分隐式调用(静态导入调用)和显示调用(动态导入调用).  编译环境: Microsoft Visual Stdio 2010 ------------------------

C++ 关键字 inline详细介绍_C 语言

1.  内联函数 在C++中我们通常定义以下函数来求两个整数的最大值: 复制代码 代码如下: int max(int a, int b){ return a > b ? a : b;} 为这么一个小的操作定义一个函数的好处有: ① 阅读和理解函数 max 的调用,要比读一条等价的条件表达式并解释它的含义要容易得多 ② 如果需要做任何修改,修改函数要比找出并修改每一处等价表达式容易得多 ③ 使用函数可以确保统一的行为,每个测试都保证以相同的方式实现 ④ 函数可以重用,不必为其他应用程序重写代码 虽