最近开了boost库的学习,就先从日期时间库开始吧,boost的date_time库是一个很强大的时间库,用起来还是挺方便的。以下算是我学习的笔记,我把它记录下来,以后便于我复习和查阅。

#include<iostream>
#include<boost/date_time/gregorian/greg_month.hpp>
#include<boost/date_time/gregorian/gregorian.hpp>

using namespace std;
using namespace boost;
using namespace boost::gregorian;

void timeDuration()

{

date d1;
        date d2(2010,1,1);
        date d3(2000,Jan,1);
        date d4(d2);
        if(d1==date(not_a_date_time))
        {
            cout<<"d1 is not a date time"<<endl;
        }
        if(d2==d4)
        {
            cout<<"d2 is eq to d4"<<endl;
        }
        if(d3<d4)
        {
            cout<<"d3 is before of d4"<<endl;
        }

date t1(neg_infin);
        date t2(pos_infin);
        date t3(not_a_date_time);
        date t4(max_date_time);
        date t5(min_date_time);
        cout<<t1<<endl;
        cout<<t2<<endl;
        cout<<t3<<endl;
        cout<<t4<<endl;
        cout<<t5<<endl;

date s1 = day_clock::local_day();
        date s2 = day_clock::universal_day();
        cout<<s1<<endl;
        cout<<s2<<endl;

//date e1(1399,12,1);

date::ymd_type ymd = s1.year_month_day();
        cout<<"year:"<<s1.year()<<endl;
        cout<<"month:"<<s1.month()<<endl;
        cout<<"day:"<<s1.day()<<endl;
    //
        cout<<"year:"<<ymd.year<<",month:"<<ymd.month<<",day:"<<ymd.day<<endl;
        cout<<"day_of_weeks:"<<s1.day_of_week()<<endl;
        cout<<"day_of_years:"<<s1.day_of_year()<<endl;
        cout<<"day_of_month:"<<s1.end_of_month()<<endl;

boost::gregorian::greg_month gm(1);
        std::cout<<gm.as_short_string()<<endl;

std::cout << to_simple_string(s1) << std::endl;
        std::cout << to_iso_string(s1) << std::endl;
        std::cout<<to_iso_extended_string(s1)<<endl;

tm tm1 = to_tm(s1);
        cout<<"year:"<<tm1.tm_year<<",month:"<<tm1.tm_mon<<",day:"<<tm1.tm_mday <<endl;

d1 = date_from_tm(tm1);
        cout<<d1<<endl;

days dd1(10),dd2(-100),dd3(255);

cout<<dd1<<" "<<dd2<<" "<<dd3<<endl;

/*日期的运算*/
        date ddd1(2000,1,1),ddd2(2008,8,8);
        cout<<ddd2-ddd1<<endl;   //结果是天数
        cout<<(ddd1+=days(100))<<endl;
        cout<<(ddd1+=years(8))<<endl;
    /*日期区间*/
        date_period dp1(date(2010,1,1),days(200));
        date_period dp2(date(2010,1,1),date(2009,1,1));
        date_period dp3(date(2010,1,1),date(2014,1,1));
        cout<<dp1<<endl;
        cout<<dp2<<endl;
        cout<<dp3<<endl;
        cout<<dp1.begin()<<"--"<<dp1.end()<<endl;

dp1.shift(days(100));
        cout<<dp1<<endl;
        dp1.expand(days(3));
        cout<<dp1<<endl;
        if(dp1.is_after(d1))
        {
            cout<<"dp1 is after d1"<<endl;
        }
        else cout<<"dp1 is before d1"<<endl;

d1 = date(2014,7,27);
        d2 = d1 + days(10);

day_iterator d_iter(d1);
        for(; d_iter<=d2 ; ++d_iter)
        {
            cout<<*d_iter<<endl;
        }
        date d_start(s1.year(),s1.month(),1);
        date d_end = s1.end_of_month();

for(day_iterator d_iter(d_start);d_iter!=d_end;++d_iter)
        {
            cout<<*d_iter<<" "<<d_iter->day_of_week()<<endl;
        }

date birth(2008,11,20);
        date d18years = birth + years(18);
        cout<<d18years << " is " << d18years.day_of_week()<<endl;

int count = 0;
        for(day_iterator d_iter(date(d18years.year(),11,1));d_iter!=d18years.end_of_month();++d_iter)
        {
            if(d_iter->day_of_week()==Sunday) ++count;
        }
        cout<<"total "<<count<<" Sundays."<<endl;
        count = 0;
        for(month_iterator m_iter(date(d18years.year(),1,1));m_iter<date(d18years.year()+1,1,1);++m_iter)
        {
            count += m_iter->end_of_month().day();
        }
        cout<<"total "<<count<<"days of year."<<endl;

typedef gregorian_calendar gre_cal;

cout<<(gre_cal::is_leap_year(d18years.year()) ? 365 : 366)<<endl;

/*时间长度操作*/

time_duration td(1,20,30,1000);//1小时20分30秒1毫秒

hours h(1);   //1小时

minutes m(10);  //10分钟

seconds s(30);  //30秒

millisec ms(1); //1毫秒

time_duration td1 = h + m + s +  ms;  //可以直接赋值

time_duration td2 = duration_from_string("1:10:30:001");//从字符串创建

int nHour = td1.hours();  //小时

int nMin = td1.minutes(); //分钟

int nSec = td1.seconds(); //秒数

long nMic = td1.fractional_seconds();  //微妙数

int total_seconds = td1.total_seconds(); //总秒数

time_duration::tick_type total_milliseconds = td1.total_milliseconds(); //总毫秒数

time_duration::tick_type total_microseconds = td1.total_microseconds(); //总微妙数

hours h1(-10);

if(h1.is_negative()) cout<<"h1 is negative"<<endl;  //可以是负值

time_duration td3 = h1.invert_sign();  //改变时间长度符号

cout<<td3<<endl;

/*时间长度的特殊值*/

time_duration td4(not_a_date_time);

if(td4.is_special() && td4.is_not_a_date_time())

cout<<"td4 is a special and not a date time"<<endl;

time_duration td5(neg_infin); //负无限

if(td5.is_special() && td5.is_neg_infinity())

cout<<"td5 is a special and is a negative infinity time"<<endl;

/*时间长度的比较*/

time_duration td6 = hours(1);

time_duration td7 = hours(1) + minutes(30);

if(td6 < td7) cout<<"dt6 < td7"<<endl;

if((td6+td7).hours()==3) cout<<"(td6+td7).hours() is 3"<<endl;

if((td1-td7).is_negative()) cout<<"(td6-td7) is negative"<<endl;

if((td6/2).minutes()==td7.minutes())cout<<"(td6/2).minutes == td7.minutes"<<endl;

time_duration td8(1,20,30,1000);

cout<<to_simple_string(td8)<<endl;

cout<<to_iso_string(td8)<<endl;

tm tm1 = to_tm(td8);  //转换到tm结构

/*date_time 库的默认的时间精度是微妙,当定义了BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG

时间的精度就发生了变化*/

//#define BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG  //定义纳秒精度宏

time_duration td9(1,10,30,1000); //1000纳秒

cout<<td9<<endl;

cout<<td9.fractional_seconds()<<endl;  //返回秒的小数部分,此处返回的时纳秒

if(time_duration::unit()*1000*1000*1000== seconds(1))//时间计量的最小单位

{

cout<<"精度为1纳秒"<<endl;

}

else

{

cout<<"精度为1微妙"<<endl;

}

if(td9.resolution() == boost::date_time::nano) cout<<"精度为1纳秒"<<endl;

cout<<"秒的小数部分的位数:"<<td9.num_fractional_digits()<<endl;

time_duration::tick_type my_millisec = time_duration::ticks_per_second()/1000;

time_duration td10(1,10,20,10*my_millisec);  //10毫秒

/*时间点*/

ptime p(boost::gregorian::date(2010,3,5), time_duration(15,55,30,0));   //日期+时间段组成一个时间点

ptime p1 = time_from_string("2014-7-29 16:00:30");  //从字符串创建时间点

ptime p2 = second_clock::local_time();    //当前本地时间

ptime p3 = microsec_clock::universal_time(); //获取UTC时间

cout<<p2<<" "<<p3<<endl;

/*时间点与tm、time_t结构转换*/

tm t = to_tm(p1);

cout<<"tm.year:"<<t.tm_year<<",tm.month"<<t.tm_mon<<",tm.day"<<t.tm_mday<<endl;

date dt = date_from_tm(t);

time_duration td11(t.tm_hour,t.tm_min,t.tm_sec);

ptime p4 = ptime(dt,td11);

p4 = from_time_t(std::time(0));

if(p4.date()==day_clock::local_day()) cout<<"p4.day == day_clock::day"<<endl;

/*时间区间*/

ptime p5(date(2010,1,1),hours(12));

time_period tp1(p5,hours(8)); //时间点+区间长度

time_period tp2(p5+hours(8),hours(1));

if(tp1.end() == tp2.begin() && tp1.is_adjacent(tp2)) cout<<"tp1 and tp2 is adjacent"<<endl;

if(tp1.intersects(tp2)) cout<<"tp1和tp2相交"<<endl;

tp1.shift(hours(10)); /*tp1向后平移了一小时*/;

if(tp1.is_after(p5)) cout<<"tp1 在 tp5 之后"<<endl;

tp2.expand(hours(2)); /*tp2向两端扩展2小时*/

if(tp1.contains(tp2)) cout<<"tp1 包含 tp2"<<endl;

/*时间迭代器*/

ptime p6(date(2010,2,27),hours(10));

for(time_iterator iter(p,minutes(10));iter < p+hours(1); ++iter) /*时间点+步长*/

{

cout<<*iter<<endl;

}

/*时间的格式化*/

date d(2010,3,6);

date_facet * dfacet = new date_facet("%Y年%m月%d日");

cout.imbue(locale(cout.getloc(),dfacet));

cout<<d<<endl;

time_facet * tfacet = new time_facet("%Y年%m月%d日%H点%M分%S%F秒");

cout.imbue(locale(cout.getloc(),tfacet));

cout<<ptime(d,hours(21)+minutes(50)+millisec(100))<<endl;

/*本地时间*/

tz_database tz_db;   //时区数据库对象

{

boost::timer t;

tz_db.load_from_file("./date_time_zonespec.csv");

}

cout<<endl;

time_zone_ptr shz = tz_db.time_zone_from_region("Asia/Shanghai");  //获取上海时区,即北京时间

time_zone_ptr nyz = tz_db.time_zone_from_region("America/New_York"); //获取纽约时区

cout<<shz->has_dst()<<endl;    //是否有夏令时

cout<<shz->std_zone_name()<<endl;  //上海市区的名称

local_date_time dt_bj(date(2008,1,7),   //北京时间 2008 1 7

hours(12),     //中午12点

shz,  //上海时区

false //无夏令时

);

time_duration flight_time = hours(15);  //飞行15小时

dt_bj += flight_time;  //到达的北京时间

cout<<dt_bj<<endl;

local_date_time dt_ny = dt_bj.local_time_in(nyz); //转化为纽约时间

cout<<dt_ny<<endl;

}

boost之date_time库的更多相关文章

  1. boost学习笔记(七)---date_time库

    date_time库的日期基于格里高利历,支持从1400-01-01到9999-12-31之间的日期计算 #define BOOST_DATE_TIME_SOURCE #include <boo ...

  2. Boost的某些库还是需要生成二进制的库的,必须安装才行,以及使用库的方法

    头文件就是库使用者最常问的问题就是“我该怎么安装Boost”,这个也是我一开始最关心的问题,Boost这点做的很好,将大部分实现都封装在头文件里,所以对于一些基本的Boost库,其实是不需要安装的,只 ...

  3. Boost 常用的库

    boost是一系列C++模板库组成的免费,可移植,开源的程序库.网络上关于boost的文章已经很多.     这里摘记一些库的信息,供自己日后参考. 0.foreach - BOOST_FOREACH ...

  4. Linux:编译安装boost 1.69库

    Boost库是为C++语言标准库提供扩展的一些C++程序库的总称,由Boost社区组织开发.维护.在C++的地位感觉可以和Spring在Java中相比. boost向来有准标准库之称,很多新特性例如智 ...

  5. 初探boost之timer库学习笔记

    timer   使用方法     #include <boost/timer.hpp> #include <iostream> using namespace std; usi ...

  6. 一起学习Boost标准库--Boost.texical_cast&format库

    今天接续介绍有关字符串表示相关的两个boost库: lexical_cast 将数值转换成字符串 format 字符串输出格式化 首先,介绍下lexical_cast ,闻其名,知其意.类似C中的at ...

  7. 初探boost之smart_ptr库学习笔记

    概述 Boost.smart_ptr库提供了六种智能指针,除了shared_ptr 和 weak_ptr 以外还包含 scoped_ptr .scoped_array . shared_array . ...

  8. C++之Boost准标准库配置

    下载安装 进入官网下载地址:https://www.boost.org/users/download/ 本教程直接下载官方已编译库,不涉及源代码手动编译 点击官方编号好的链接,然后进入一个下载地址:h ...

  9. boost的并发库

    thread: http://www.boost.org/doc/libs/1_61_0/libs/thread/ asio: http://www.boost.org/doc/libs/1_61_0 ...

随机推荐

  1. celery docker 基本使用

    项目参考官网资料,比较简单的add task 具体代码参考https://github.com/rongfengliang/celery-docker-demo 项目结构 ├── README.md ...

  2. hadoop之 YARN配置参数剖析—RM与NM相关参数

    参数均需要在yarn-site.xml中配置: 1. ResourceManager相关配置参数 (1) yarn.resourcemanager.address 参数解释:ResourceManag ...

  3. postgresql PL/pgSQL—存储过程结构和变量声明

    ref: https://www.postgresql.org/docs/9.6/static/plpgsql-structure.html 一. 函数结构 CREATE FUNCTION somef ...

  4. Accessing data in Hadoop using dplyr and SQL

    If your primary objective is to query your data in Hadoop to browse, manipulate, and extract it into ...

  5. 【转】java内存分配和String类型的深度解析

    一.引题 在java语言的所有数据类型中,String类型是比较特殊的一种类型,同时也是面试的时候经常被问到的一个知识点,本文结合java内存分配深度分析关于String的许多令人迷惑的问题.下面是本 ...

  6. 浅谈Laravel框架的CSRF

    前文 CSRF攻击和漏洞的参考文章: http://www.cnblogs.com/hyddd/archive/2009/04/09/1432744.html Laravel默认是开启了CSRF功能, ...

  7. Python 中的属性访问与描述符

    在Python中,对于一个对象的属性访问,我们一般采用的是点(.)属性运算符进行操作.例如,有一个类实例对象foo,它有一个name属性,那便可以使用foo.name对此属性进行访问.一般而言,点(. ...

  8. js实现的快速排序

    快速排序是一种平均性能非常优秀的排序算法,在很多场合都会应用到他. 了解快速排序于对开放高效率的软件有很重要的作用. 但是有不少的书本讲得并不是很清楚,而且不同的教材的实现方式也不尽相同, 我这里将最 ...

  9. OpenCL Hello World

    ▶ OpenCL 的环境配置与第一个程序 ● CUDA 中自带 OpenCL 需要的头文件和库,直接拉近项目里边去就行:AMD 需要下载 AMD APP SDK(https://community.a ...

  10. Matlab信号处理工具箱函数

    波形产生和绘图chirp 产生扫描频率余弦diric 产生Dirichlet函数或周期Sinc函数gauspuls 产生高斯调制正弦脉冲pulstran 产生脉冲串rectpuls 产生非周期矩形信号 ...