• boost之date_time库


    最近开了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;

    }

  • 相关阅读:
    调用网易有道词典api
    函数设计
    参数2
    新浪微博API使用初步介绍——解决回调地址的问题
    参数关键点和return返回多个(伪多个)值问题
    函数基本理论
    一个值得思考的例子
    Beego基础学习(五)Golang原生sql操作Mysql数据库增删改查(基于Beego下测试)
    Golang利用select实现超时机制
    Golang利用select和普通函数分别实现斐波那契数列
  • 原文地址:https://www.cnblogs.com/yu-chao/p/3889556.html
Copyright © 2020-2023  润新知