zoukankan      html  css  js  c++  java
  • Boost 时间库使用 笔记

    最近学习了一下boost日期时间相关的库,在此做一下学习笔记,来源于《Boost程序库完全开发指南 第4版》

    思维导图

    date_time库

    使用方式注意点

    date_time库 是需要编译才能使用

    头文件

    • 处理日期的头文件 gregorian
    // 处理日期的组件
    #include <boost/date_time/gregorian/gregorian.hpp>
    using namesapce boost::gregorian;
    
    • 处理时间的头文件 posix_time
    // 处理时间的组件
    #include <boost/date_time/posix_time/posix_time.hpp>
    using namespace boost::posix_time
    

    基本概念

    • 时间是个无限延伸的实数轴,时间点是数轴上的点,两个时间点确定一个区间,时长(时间长度)是一个有正负号的标量,是两个时间点之差,不属于数轴

    • 时间点、时间段和时长可以进行一定的运算,可以用常识想到 “时间点 + 时长 = 时间点” ,“时长 + 时长 = 时长”,“时间段 ∩ 时间段 = 时间段”,“时间点 ∈ 时间段” ; 还有一些无意义的运算如 “时间点+时间点”、“时长+时间段”

    • date_time 支持 无限时间和无效时间,这样特殊的时间,类似于数学上的极限。这样的 special_values 有如下几个

    • pos_infin : 表示正无限

    • neg_infin : 表示负无限

    • not_a_date_time : 无效时间

    • min_date_time : 可表示的最小日期或时间

    • max_date_time : 可表示的最大日期或时间

    日期的处理

    • date_time 是日期的处理部分 ,日期只涉及 年月日。
    • date_time 仅支持1400-01-01到9999-12-31之间的日期计算

    date类

    • date 是date_time处理 日期的核心类,使用一个32为整数作为内部存储,以天为单位表示时间点的概念

    • 类 思维导图

    • 类摘要

    templat<typename T, typename calendar, typename duration_type_>
    class date{
    public:
          date(year_type,month_type,date_type); // 构造函数 如 date(2021,1,1)
          date(const ymd_type &);// 其他类型的构造函数
          // 基本操作函数
          year_type year() const; // 获取 年
          month_type month() const; // 获取 月
          day_type day() const; // 获取 日
          day_of_week_type day_of_week() const;// 获取 当天的星期数
          ymd_type year_month_day() const; 
    
          bool operator<(const date_type &) const;
          bool operator==(const date_type &) const; 
    
          // 校验函数
          bool is_special() const;
          bool is_not_a_date() const;
          bool is_infinity() const;
          bool is_pos_infinity() const;
          bool is_neg_infinity() const;
          
          // 日期运算函数
          duration_type operator-(const date_tye &) const;
          ... 
    };
    
    • 创建日期对象

    • 普通构造

    • 字符串构造 使用from_string() 或 from_undelimited_string()

    • day_clock

    • day_clock 是一个天级别的时钟,调用其静态成员函数local_day() 或universal_day()返回当天的日期兑现。

    • day_clock 内部使用 C标准库的localtime() 和 gmtime() 函数,所以local_day()结果依赖于操作系统的时区设置

    • 非法日期的处理-采用抛出异常的方式

    • 1.日期超出1400-01-01到9999-12-31的范围

    • 2.使用不存在的月份或日期 如 date(2020,2,30) date(2021,13,1)

    • date_time库会抛出异常(不会转换为一个无效日期),使用what()可以获得具体错误信息

    • 日期访问函数

    • date.year() 返回 年

    • date.month() 返回 月

    • date.day() 返回 日

    • year_month_day() 返回 date::ymd_type结构

    • day_of_week() 返回 date 的星期数,0表示星期天 0~6

    • day_of_year() 返回 date 是当年的第几天 最多366

    • end_of_month() 返回 当月最后一天的date

    • week_number() 返回date所在周是当年的第几周 范围0~53

    • 检验特殊日期的函数

    • is_infinity() 是否是一个 无限日期

    • is_neg_infinity() 是否是一个 负无限日期

    • is_pos_infinity() 是否是一个 正无限日期

    • is_not_a_date() 是否是一个无效日期 注意:非法日期没办法用这个检测出来。非法日期在构造时就会抛出异常

    • is_special() 是否是任意一个特殊日期

    • 例子:

    • 日期的输出

    • to_simple_string() :YYYY-mmm-DD 其中mmm是3字符的英文月份名

    • to_iso_string() : YYYYMMDD 都为数字

    • to_iso_extended_string() : YYYY-MM-DD 都为数字

    • date支持 流输入输出,默认使用YYYY-mmm-DD格式

    • 例子:

    • 日期长度 date_duration

    • 类摘要

    class date_duration
    {
    public:
      date_duration(long); //构造函数
      date_duration(special_values);
     
      long days() const; // 成员访问函数
      bool is_special() const;
      bool is_negative() const;
    
      bool operator==(const date_duration &) const;
      ...
    
      static date_duration unit(); // 时长单位
    };
    
    • days 天单位时长类

    • weeks 周单位时长类

    • months 月单位时长类

    • years 年单位时长类

    • 以上四个 支持加减 乘除运算

    • 用法例子:

    • 日期运算

    • days = date1 - date2

    • date1 = date + days(x) : x天间隔

    • date1 = date + weeks(x) : x个周间隔

    • date1 = date + months(x) : x个月间隔 注意:如果是月末日期,可能加减之后会丢失 日期。如1月31号加一个月会得到 2月28号 或 29号,再 加一个月 只能得到 3月 28号 或 29号

    • date1 = date + years(x) : x个年间隔 年也可能有月类似问题。当出现 闰年的 2月 29号
      以上 x 值可以为负数

    处理时间

    • 时间 是 日期的进一步细化,相当于在日期“天”的量级下增加时分秒。

    时间长度 time_duration

    • time_duration 和 date_duration 类似。可以说是对时长单位的细化
    • hours 小时
    • minutes
    • seconds
    • milliseconds 毫秒
    • microseconds 微秒
    • nanoseconds 纳秒
    • 运算上 和 date_duration 运算类似

    时间点 ptime

    • ptime 类摘要
    class ptime
    {
    public:
      ptime(const date_type &,const time_duration_type &, dast_flags = not_dst); // 构造函数
      ptime(special_values)
      
      date_type date() const; //获取日期
      time_duration_type time_of_day() const; // 获取时间
      // 其他函数 与 date类 类似
    };
    
    • 创建时间点对象
    • ptime(date(2021,1,1),hours(1)) // 2021年1月1号 凌晨 1时
    • 实际上 时间点对象 就是 date+time_duration
    • 时间运算
    • ptime 除了days、weeks、months、years 增加了 hours、minutes、seconds、milliseconds...等待时长单位
    • 运算上 和 date相同
  • 相关阅读:
    vue.js中created方法作用
    UasyUi的各种方法整理
    echarts 3.8.4: tree设置节点与节点之间连线的颜色,可以独立每条线分开设置
    echarts中的树形结构图(参数分析)
    echarts y轴数据如果太大就会造成坐标轴显示不完全的问题
    echarts 网络拓扑告警闪烁及提示信息自定义
    echarts 树图问题
    echarts grid多格显示问题
    echarts中自定义tooltip的换行问题
    yarn install 安装报错问题
  • 原文地址:https://www.cnblogs.com/q1076452761/p/14304724.html
Copyright © 2011-2022 走看看