zoukankan      html  css  js  c++  java
  • Boost Posix Time

    有时候经常要实现程序在某个时间段进行某些操作的功能,如果利用boost库的话可以很方便就能实现。

    [cpp]
    1. #include <boost/date_time/posix_time/posix_time.hpp>  
    2. #include <boost/date_time/gregorian/gregorian.hpp>  
    3.   
    4. // 首先要定义一个时间区间段,设程序需在每日的8:50~9:15之间实现某些操作;  
    5. boost::posix_time::time_period *pTimePeriod NULL;  
    6. boost::posix_time::ptime pt(boost::gregorian::day_clock::local_day());  
    7. pt += boost::posix_time::hours(8);  
    8. pt += boost::posix_time::minutes(50);  
    9.   
    10. // 以8点50分为左边界,区间长度为25分钟,即从8点50分到9点15分之间;  
    11. pTimePeriod new boost::posix_time::time_period(pt,boost::posix_time::minutes(25));  
    12.   
    13. // 判断当前时间是否在规定的时间区间内;  
    14. if (pTimePeriod->contains(boost::posix_time::second_clock::local_time()))  
    15.  
    16.     // 当前时间在规定的时间区间内;  
    17.     // do something;  
    18.  
    19. else if (pTimePeriod->is_after(boost::posix_time::second_clock::local_time()))  
    20.  
    21.     // 当前时间早于8点50分;  
    22.     // do something;  
    23.  
    24. else if (pTimePeriod->is_before(boost::posix_time::second_clock::local_time()))  
    25.  
    26.     // 当前时间晚于9点15分;  
    27.     // do something;  
    28.  
    29.   
    30. 因为这个区间是和日期相关的,所以如果程序是24小时运行的话每天还要进行一下时间区间的复位,不然不管怎么判断都会发现当前时间晚于规定的时间区间。 
  • 相关阅读:
    opensuse使用zypper安装软件
    补习系列(1)-springboot项目基础搭建课
    补习系列-springboot-使用assembly进行项目打包
    log4j2 使用纪要
    mongos-sharding连接池配置
    maven-代码风格检查工具
    mtools-你可能没用过的mongodb神器
    mongodb分布式集群搭建手记
    mongodb分片扩展架构
    mongodb副本集高可用架构
  • 原文地址:https://www.cnblogs.com/rooney/p/2707540.html
Copyright © 2011-2022 走看看