zoukankan      html  css  js  c++  java
  • boost--日期处理

    1、timer

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

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

    2、date日期类

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

        boost::gregorian::date d; //创建了一个无效的日期对象
        boost::gregorian::date d1(2010, 1, 1);
        boost::gregorian::date d2(2000, boost::date_time::Jan, 1);
        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("19991231");
    View Code

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

        boost::gregorian::date d; //创建了一个无效的日期对象
        boost::gregorian::date d1(2010, 1, 1);
        boost::gregorian::date d2(2000, 1, 1);
    
        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() == 2010); //获取年
        assert(d1.month() == 1); //获取月
        assert(d1.day() == 1); //获取日
    
        boost::gregorian::date::ymd_type ymd = d1.year_month_day(); //获取年月日
        assert(ymd.year == 2010);
        assert(ymd.month == 1);
        assert(ymd.day == 1);
    
        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
    View Code

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

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

      date与C中tm结构转换:

        boost::gregorian::date d1(2010, 1, 1);
    
        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的年、月、日三个成员
    View Code

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

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

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

        boost::gregorian::date d1(2010, 1, 1);
    
        boost::gregorian::days dd1(10), dd2(-100), dd3(255);
        assert(dd1 > dd2);
        assert(dd1 + dd2 == boost::gregorian::days(-90));
        boost::gregorian::weeks w(3);
        assert(w.days() == 21);
        boost::gregorian::months m(5);
        assert(m.number_of_months() == 5);
        boost::gregorian::years y(10);
        d1 += y;
        assert(d1.year() == 2020);
    View Code

    4、 日期运算

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

        boost::gregorian::date d1(2000, 1, 30), d2(2008, 8, 8);
        int s = (d2 - d1).days(); //3113天
        d1 += boost::gregorian::months(1);
        assert(d1.day() != 30);
        d1 += boost::gregorian::days(10);
    View Code

    5、日期迭代器

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

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

      迭代器的应用示例:

        //一个人出生于2000年2月10日,输出它18岁生日的那个月每天的日期、星期以及计算该月一共有几个星期天
        boost::gregorian::date dBir(2000, 2, 10);
        boost::gregorian::date d2 = dBir + boost::gregorian::years(18) - boost::gregorian::days(9);
    
        boost::gregorian::day_iterator d_iter2(d2);
        int iCount = 0;
        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;
    View Code

    6、其它

      判断闰年:

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

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

  • 相关阅读:
    debian 降级
    linux 常用查看设备命令
    viewstate
    linux图形界面编程基本知识
    Java 不适合编写桌面应用
    temp
    ASP.NET中26个常用性能优化方法
    三层架构与MVC的关系
    分页查询前台HTML+后台asp.net代码
    windows身份验证登入数据库 iis 无法访问数据库
  • 原文地址:https://www.cnblogs.com/milanleon/p/8780538.html
Copyright © 2011-2022 走看看