boost.date_time

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/static_assert.hpp>
using  namespace  std;
using  namespace  boost::gregorian;
 
 
int  _tmain( int  argc, _TCHAR* argv[])
{  
     date d1;
     date d2(2012,12,12);
     date d3(2012, Jan, 1);
     date d4(d2);
 
     assert (d1==date(not_a_date_time));
     assert (d2==d4);
 
     d1=from_string( "2012-12-10" );
     d2=(from_string( "2012/12/10" ));
     d3=from_undelimited_string( "20121210" );
     cout<<d1<<endl<<d2<<endl<<d3<<endl<<d4<<endl;
 
     cout<<day_clock::local_day()<<endl;
     cout<<day_clock::universal_day()<<endl;
     return  0;
}
image
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/static_assert.hpp>
using  namespace  std;
using  namespace  boost::gregorian;
 
int  _tmain( int  argc, _TCHAR* argv[])
{  
     date d(2012,3,26);
     assert (d.year()==2012);
     assert (d.month()==3);
     assert (d.day()==26);
 
     //一次性的获得年月日数据
     date::ymd_type ymd=d.year_month_day();
     assert (ymd.year==2012);
     assert (ymd.month==3);
     assert (ymd.day==26);
     
     cout<< "今天是" <<d.day_of_week()<<endl;  //输出date的星期数,0表示星期日
     cout<< "今天是今年的第" <<d.day_of_year()<< "天" <<endl;
     cout<< "本周是今年的第" <<d.week_number()<< "周" <<endl;
     cout<< "本月的最后一天是:" <<d.end_of_month()<<endl;
 
     //几种输出格式的比较
     cout<<to_simple_string(d)<<endl;
     cout<<to_iso_string(d)<<endl;
     cout<<to_iso_extended_string(d)<<endl;
     cout<<d<<endl;
     return  0;
}
image

 

日期长度:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/static_assert.hpp>
using  namespace  std;
using  namespace  boost::gregorian;
 
int  _tmain( int  argc, _TCHAR* argv[])
{  
     days d1(100);
     days d2(-100);
 
     assert (d1+d2==days(0));
     assert ((d1+d2).days()==0);
 
     weeks w(3);    //三个星期
     assert (w.days()==21);
 
     months m(5);  //5个月
     years y(2);  //2年
 
     months m2=y+m;   //2年零5个月
     assert (m2.number_of_months()==29);
     assert (y.number_of_years()*2==4);
     return  0;
}

一些日期的运算:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/static_assert.hpp>
using  namespace  std;
using  namespace  boost::gregorian;
 
//一些日期的运算
 
int  _tmain( int  argc, _TCHAR* argv[])
{  
     date d1(2012,3,26);
     date d2(1991,2,10);
     cout<< "我已经出生了" <<d1-d2<< "天了" <<endl;
 
     d1+=days(10);
     cout<<to_iso_extended_string(d1)<<endl;
 
     d1+=months(2);
     cout<<to_iso_extended_string(d1)<<endl;
 
     d1+=weeks(1);
     cout<<to_iso_extended_string(d1)<<endl;
 
     d1-=years(2);
     cout<<to_iso_extended_string(d1)<<endl;
 
     return  0;
}

image

 

日期区间:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/static_assert.hpp>
using  namespace  std;
using  namespace  boost::gregorian;
 
//日期区间
 
int  _tmain( int  argc, _TCHAR* argv[])
{  
     date_period dp(date(2012,1,1),days(10));
     assert (!dp.is_null());
     assert (dp.begin().day()==1);
     assert (dp.last().day()==10);
     assert (dp.end().day()==11);
     assert (dp.length().days()==10);
 
     cout<<dp<<endl;
     return  0;
}
image

日期区间的运算

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/static_assert.hpp>
using  namespace  std;
using  namespace  boost::gregorian;
 
//日期区间的运算
 
int  _tmain( int  argc, _TCHAR* argv[])
{  
     date_period dp(date(2012,1,1),days(10));
     dp.shift(days(3));
     assert (dp.begin().day()==4);
     assert (dp.length().days()==10);
 
     dp.expand(days(3));
     assert (dp.begin().day()==1);
     assert (dp.length().days()==16);
 
     assert (dp.is_after(date(2000,12,12)));
     assert (dp.is_before(date(2013,12,12)));
     assert (dp.contains(date(2012,1,2)));
 
     return  0;
}

1  posix_time 使用的时候

boost::posix_time::ptime ptime1 = boost::posix_time::time_from_string(std::string(str1)));

boost::posix_time::ptime ptime2 = boost::posix_time::time_from_string(std::string(str2)));

判断 equal函数

(ptime1 == ptime2)?true:false

greaterthan函数

(ptime1 > ptime2)?true:false

lessthan 函数

(ptime1 < ptime2)?true:false

当time1=2011-01-11 00:00:00

time2 = 2011-01-11 

测试结果:

time1跟time2是不相等

time1 也不大于 time2

time1 小于time2

2 计算时间相差的天数

date day1 = from_simple_string("2001-10-9");
date today = day_clock::local_day();
days days_limint = today-day1;
struct stat stInfo;
time_t tt;
stat("F:\\yuj\\test_env\\11.txt",&stInfo);
tt = stInfo.st_ctime;
ptime  pt = boost::posix_time::from_time_t(tt);
date fileDate = pt.date();
days days_limint2 = fileDate-today;
printf("相差%d天 \n",days_limint.days());
printf("相差%d天 \n",days_limint2.days());

BOOST:使用local_date_time 计算当前时间戳

在工作中,我们经常使用时间戳,来标示些特性,昨天也正好有个同事问我,所以想起用这个办法。

原理:取得现在时间的utc - 纪元时间。

 

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include <boost/date_time/local_time/local_time.hpp>  
  3. using namespace std;  
  4. using namespace boost::gregorian;  
  5. using namespace boost::local_time;  
  6. using namespace boost::posix_time;  
  7. int main()  
  8. {  
  9. tz_database tz_db;  
  10. try  
  11. {  
  12. /* 
  13. *加载时区数据库 
  14. */  
  15. tz_db.load_from_file("/usr/local/boost_1_42_0/libs/date_time/data/date_time_zonespec.csv");  
  16. }  
  17. catch(data_not_accessible dna)  
  18. {  
  19. std::cerr << "Error with time zone data file: " << dna.what() << std::endl;  
  20. exit(EXIT_FAILURE);  
  21. }  
  22. catch(bad_field_count bfc)  
  23. {  
  24. std::cerr << "Error with time zone data file: " << bfc.what() << std::endl;  
  25. exit(EXIT_FAILURE);  
  26. }  
  27. /* 
  28. *由于没有找到北京,我们暂时指向重庆 
  29. */  
  30. time_zone_ptr time_zone = tz_db.time_zone_from_region("Asia/Chongqing");  
  31. const time_t tt = time(NULL);  
  32. tm* pLocalTime = localtime(&tt);  
  33. date record_date(pLocalTime->tm_year+1900,pLocalTime->tm_mon+1,pLocalTime->tm_mday);  
  34. time_duration record_time(pLocalTime->tm_hour,pLocalTime->tm_min,pLocalTime->tm_sec);  
  35. /* 
  36. *本地当前时间 
  37. */  
  38. local_date_time time_t_now(record_date,  
  39. record_time,  
  40. time_zone,  
  41. local_date_time::NOT_DATE_TIME_ON_ERROR);  
  42. std::cout << "localtime:[" << time_t_now << "]" << std::endl;  
  43. /* 
  44. *纪元时间 
  45. */  
  46. ptime time_t_begin(date(1970,1,1));  
  47. time_duration timestamp = time_t_now.utc_time() - time_t_begin;  
  48. cout << "timestamp:[" << timestamp.total_seconds() << "]" << endl ;  

 

 

这是构造函数的声明,所在<boost/date_time/local_time/local_date_time.hpp>中

[cpp]  view plain copy
  1. /*! This constructs a local time -- the passed time information 
  2.      * understood to be in the passed tz.  The DST flag is calculated 
  3.      * according to the specified rule. 
  4.      */  
  5.     local_date_time_base(date_type d,  
  6.                          time_duration_type td,  
  7.                          boost::shared_ptr<tz_type> tz,  
  8.                          DST_CALC_OPTIONS calc_option) :  
  9.       // dummy value - time_ is set in constructor code  
  10.       date_time::base_time<utc_time_type,time_system_type>(utc_time_type(d,td)),  
  11.       zone_(tz)  
 

 

 

运行结果:

[cpp]  view plain copy
  1. [root@localhost bin]# ./date   
  2. localtime:[2010-Jul-08 09:32:18 CST]  
  3. timestamp:[1278552738]  
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值