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 image](https://i-blog.csdnimg.cn/blog_migrate/b3d655990957f8011677daff012455e4.png)
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 image](https://i-blog.csdnimg.cn/blog_migrate/391deafe30a8a8e013a11ae45b1d2ed1.png)
日期长度:
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;
}
|
日期区间:
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 image](https://i-blog.csdnimg.cn/blog_migrate/49d037c682677ffef30b543ed647d35c.png)
日期区间的运算
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 - 纪元时间。
- #include <iostream>
- #include <boost/date_time/local_time/local_time.hpp>
- using namespace std;
- using namespace boost::gregorian;
- using namespace boost::local_time;
- using namespace boost::posix_time;
- int main()
- {
- tz_database tz_db;
- try
- {
- /*
- *加载时区数据库
- */
- tz_db.load_from_file("/usr/local/boost_1_42_0/libs/date_time/data/date_time_zonespec.csv");
- }
- catch(data_not_accessible dna)
- {
- std::cerr << "Error with time zone data file: " << dna.what() << std::endl;
- exit(EXIT_FAILURE);
- }
- catch(bad_field_count bfc)
- {
- std::cerr << "Error with time zone data file: " << bfc.what() << std::endl;
- exit(EXIT_FAILURE);
- }
- /*
- *由于没有找到北京,我们暂时指向重庆
- */
- time_zone_ptr time_zone = tz_db.time_zone_from_region("Asia/Chongqing");
- const time_t tt = time(NULL);
- tm* pLocalTime = localtime(&tt);
- date record_date(pLocalTime->tm_year+1900,pLocalTime->tm_mon+1,pLocalTime->tm_mday);
- time_duration record_time(pLocalTime->tm_hour,pLocalTime->tm_min,pLocalTime->tm_sec);
- /*
- *本地当前时间
- */
- local_date_time time_t_now(record_date,
- record_time,
- time_zone,
- local_date_time::NOT_DATE_TIME_ON_ERROR);
- std::cout << "localtime:[" << time_t_now << "]" << std::endl;
- /*
- *纪元时间
- */
- ptime time_t_begin(date(1970,1,1));
- time_duration timestamp = time_t_now.utc_time() - time_t_begin;
- cout << "timestamp:[" << timestamp.total_seconds() << "]" << endl ;
这是构造函数的声明,所在<boost/date_time/local_time/local_date_time.hpp>中
- /*! This constructs a local time -- the passed time information
- * understood to be in the passed tz. The DST flag is calculated
- * according to the specified rule.
- */
- local_date_time_base(date_type d,
- time_duration_type td,
- boost::shared_ptr<tz_type> tz,
- DST_CALC_OPTIONS calc_option) :
- // dummy value - time_ is set in constructor code
- date_time::base_time<utc_time_type,time_system_type>(utc_time_type(d,td)),
- zone_(tz)
运行结果:
- [root@localhost bin]# ./date
- localtime:[2010-Jul-08 09:32:18 CST]
- timestamp:[1278552738]