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小时运行的话每天还要进行一下时间区间的复位,不然不管怎么判断都会发现当前时间晚于规定的时间区间。 
  • 相关阅读:
    SPOJ 149 FSHEEP Fencing in the Sheep ( 计算几何 + 二分 )
    UVa 11806
    UVa 1445
    HDU 4725 The Shortest Path in Nya Graph( 建图 + 最短路 )
    HDU 4661 Message Passing ( 树DP + 推公式 )
    从远程库克隆库
    添加远程库
    远程仓库
    删除文件
    撤销修改
  • 原文地址:https://www.cnblogs.com/rooney/p/2707540.html
Copyright © 2011-2022 走看看