最近開了boost庫的學習,就先從日期時間庫開始吧,boost的date_time庫是一個很強大的時間庫,用起來仍是挺方便的。如下算是我學習的筆記,我把它記錄下來,之後便於我複習和查閱。ios
#include<iostream>
#include<boost/date_time/gregorian/greg_month.hpp>
#include<boost/date_time/gregorian/gregorian.hpp>
using namespace std;
using namespace boost;
using namespace boost::gregorian;
git
void timeDuration()數據庫
{學習
date d1;
date d2(2010,1,1);
date d3(2000,Jan,1);
date d4(d2);
if(d1==date(not_a_date_time))
{
cout<<"d1 is not a date time"<<endl;
}
if(d2==d4)
{
cout<<"d2 is eq to d4"<<endl;
}
if(d3<d4)
{
cout<<"d3 is before of d4"<<endl;
}
date t1(neg_infin);
date t2(pos_infin);
date t3(not_a_date_time);
date t4(max_date_time);
date t5(min_date_time);
cout<<t1<<endl;
cout<<t2<<endl;
cout<<t3<<endl;
cout<<t4<<endl;
cout<<t5<<endl;
date s1 = day_clock::local_day();
date s2 = day_clock::universal_day();
cout<<s1<<endl;
cout<<s2<<endl;
//date e1(1399,12,1);
date::ymd_type ymd = s1.year_month_day();
cout<<"year:"<<s1.year()<<endl;
cout<<"month:"<<s1.month()<<endl;
cout<<"day:"<<s1.day()<<endl;
//
cout<<"year:"<<ymd.year<<",month:"<<ymd.month<<",day:"<<ymd.day<<endl;
cout<<"day_of_weeks:"<<s1.day_of_week()<<endl;
cout<<"day_of_years:"<<s1.day_of_year()<<endl;
cout<<"day_of_month:"<<s1.end_of_month()<<endl;
boost::gregorian::greg_month gm(1);
std::cout<<gm.as_short_string()<<endl;
std::cout << to_simple_string(s1) << std::endl;
std::cout << to_iso_string(s1) << std::endl;
std::cout<<to_iso_extended_string(s1)<<endl;
tm tm1 = to_tm(s1);
cout<<"year:"<<tm1.tm_year<<",month:"<<tm1.tm_mon<<",day:"<<tm1.tm_mday <<endl;
d1 = date_from_tm(tm1);
cout<<d1<<endl;
days dd1(10),dd2(-100),dd3(255);
cout<<dd1<<" "<<dd2<<" "<<dd3<<endl;
/*日期的運算*/
date ddd1(2000,1,1),ddd2(2008,8,8);
cout<<ddd2-ddd1<<endl; //結果是天數
cout<<(ddd1+=days(100))<<endl;
cout<<(ddd1+=years(8))<<endl;
/*日期區間*/
date_period dp1(date(2010,1,1),days(200));
date_period dp2(date(2010,1,1),date(2009,1,1));
date_period dp3(date(2010,1,1),date(2014,1,1));
cout<<dp1<<endl;
cout<<dp2<<endl;
cout<<dp3<<endl;
cout<<dp1.begin()<<"--"<<dp1.end()<<endl;
dp1.shift(days(100));
cout<<dp1<<endl;
dp1.expand(days(3));
cout<<dp1<<endl;
if(dp1.is_after(d1))
{
cout<<"dp1 is after d1"<<endl;
}
else cout<<"dp1 is before d1"<<endl;
d1 = date(2014,7,27);
d2 = d1 + days(10);
day_iterator d_iter(d1);
for(; d_iter<=d2 ; ++d_iter)
{
cout<<*d_iter<<endl;
}
date d_start(s1.year(),s1.month(),1);
date d_end = s1.end_of_month();
for(day_iterator d_iter(d_start);d_iter!=d_end;++d_iter)
{
cout<<*d_iter<<" "<<d_iter->day_of_week()<<endl;
}
date birth(2008,11,20);
date d18years = birth + years(18);
cout<<d18years << " is " << d18years.day_of_week()<<endl;
int count = 0;
for(day_iterator d_iter(date(d18years.year(),11,1));d_iter!=d18years.end_of_month();++d_iter)
{
if(d_iter->day_of_week()==Sunday) ++count;
}
cout<<"total "<<count<<" Sundays."<<endl;
count = 0;
for(month_iterator m_iter(date(d18years.year(),1,1));m_iter<date(d18years.year()+1,1,1);++m_iter)
{
count += m_iter->end_of_month().day();
}
cout<<"total "<<count<<"days of year."<<endl;
typedef gregorian_calendar gre_cal;
cout<<(gre_cal::is_leap_year(d18years.year()) ? 365 : 366)<<endl;spa
/*時間長度操做*/對象
time_duration td(1,20,30,1000);//1小時20分30秒1毫秒ci
hours h(1); //1小時字符串
minutes m(10); //10分鐘get
seconds s(30); //30秒string
millisec ms(1); //1毫秒
time_duration td1 = h + m + s + ms; //能夠直接賦值
time_duration td2 = duration_from_string("1:10:30:001");//從字符串建立
int nHour = td1.hours(); //小時
int nMin = td1.minutes(); //分鐘
int nSec = td1.seconds(); //秒數
long nMic = td1.fractional_seconds(); //微妙數
int total_seconds = td1.total_seconds(); //總秒數
time_duration::tick_type total_milliseconds = td1.total_milliseconds(); //總毫秒數
time_duration::tick_type total_microseconds = td1.total_microseconds(); //總微妙數
hours h1(-10);
if(h1.is_negative()) cout<<"h1 is negative"<<endl; //能夠是負值
time_duration td3 = h1.invert_sign(); //改變時間長度符號
cout<<td3<<endl;
/*時間長度的特殊值*/
time_duration td4(not_a_date_time);
if(td4.is_special() && td4.is_not_a_date_time())
cout<<"td4 is a special and not a date time"<<endl;
time_duration td5(neg_infin); //負無限
if(td5.is_special() && td5.is_neg_infinity())
cout<<"td5 is a special and is a negative infinity time"<<endl;
/*時間長度的比較*/
time_duration td6 = hours(1);
time_duration td7 = hours(1) + minutes(30);
if(td6 < td7) cout<<"dt6 < td7"<<endl;
if((td6+td7).hours()==3) cout<<"(td6+td7).hours() is 3"<<endl;
if((td1-td7).is_negative()) cout<<"(td6-td7) is negative"<<endl;
if((td6/2).minutes()==td7.minutes())cout<<"(td6/2).minutes == td7.minutes"<<endl;
time_duration td8(1,20,30,1000);
cout<<to_simple_string(td8)<<endl;
cout<<to_iso_string(td8)<<endl;
tm tm1 = to_tm(td8); //轉換到tm結構
/*date_time 庫的默認的時間精度是微妙,當定義了BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
時間的精度就發生了變化*/
//#define BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG //定義納秒精度宏
time_duration td9(1,10,30,1000); //1000納秒
cout<<td9<<endl;
cout<<td9.fractional_seconds()<<endl; //返回秒的小數部分,此處返回的時納秒
if(time_duration::unit()*1000*1000*1000== seconds(1))//時間計量的最小單位
{
cout<<"精度爲1納秒"<<endl;
}
else
{
cout<<"精度爲1微妙"<<endl;
}
if(td9.resolution() == boost::date_time::nano) cout<<"精度爲1納秒"<<endl;
cout<<"秒的小數部分的位數:"<<td9.num_fractional_digits()<<endl;
time_duration::tick_type my_millisec = time_duration::ticks_per_second()/1000;
time_duration td10(1,10,20,10*my_millisec); //10毫秒
/*時間點*/
ptime p(boost::gregorian::date(2010,3,5), time_duration(15,55,30,0)); //日期+時間段組成一個時間點
ptime p1 = time_from_string("2014-7-29 16:00:30"); //從字符串建立時間點
ptime p2 = second_clock::local_time(); //當前本地時間
ptime p3 = microsec_clock::universal_time(); //獲取UTC時間
cout<<p2<<" "<<p3<<endl;
/*時間點與tm、time_t結構轉換*/
tm t = to_tm(p1);
cout<<"tm.year:"<<t.tm_year<<",tm.month"<<t.tm_mon<<",tm.day"<<t.tm_mday<<endl;
date dt = date_from_tm(t);
time_duration td11(t.tm_hour,t.tm_min,t.tm_sec);
ptime p4 = ptime(dt,td11);
p4 = from_time_t(std::time(0));
if(p4.date()==day_clock::local_day()) cout<<"p4.day == day_clock::day"<<endl;
/*時間區間*/
ptime p5(date(2010,1,1),hours(12));
time_period tp1(p5,hours(8)); //時間點+區間長度
time_period tp2(p5+hours(8),hours(1));
if(tp1.end() == tp2.begin() && tp1.is_adjacent(tp2)) cout<<"tp1 and tp2 is adjacent"<<endl;
if(tp1.intersects(tp2)) cout<<"tp1和tp2相交"<<endl;
tp1.shift(hours(10)); /*tp1向後平移了一小時*/;
if(tp1.is_after(p5)) cout<<"tp1 在 tp5 以後"<<endl;
tp2.expand(hours(2)); /*tp2向兩端擴展2小時*/
if(tp1.contains(tp2)) cout<<"tp1 包含 tp2"<<endl;
/*時間迭代器*/
ptime p6(date(2010,2,27),hours(10));
for(time_iterator iter(p,minutes(10));iter < p+hours(1); ++iter) /*時間點+步長*/
{
cout<<*iter<<endl;
}
/*時間的格式化*/
date d(2010,3,6);
date_facet * dfacet = new date_facet("%Y年%m月%d日");
cout.imbue(locale(cout.getloc(),dfacet));
cout<<d<<endl;
time_facet * tfacet = new time_facet("%Y年%m月%d日%H點%M分%S%F秒");
cout.imbue(locale(cout.getloc(),tfacet));
cout<<ptime(d,hours(21)+minutes(50)+millisec(100))<<endl;
/*本地時間*/
tz_database tz_db; //時區數據庫對象
{
boost::timer t;
tz_db.load_from_file("./date_time_zonespec.csv");
}
cout<<endl;
time_zone_ptr shz = tz_db.time_zone_from_region("Asia/Shanghai"); //獲取上海時區,即北京時間
time_zone_ptr nyz = tz_db.time_zone_from_region("America/New_York"); //獲取紐約時區
cout<<shz->has_dst()<<endl; //是否有夏令時
cout<<shz->std_zone_name()<<endl; //上海市區的名稱
local_date_time dt_bj(date(2008,1,7), //北京時間 2008 1 7
hours(12), //中午12點
shz, //上海時區
false //無夏令時
);
time_duration flight_time = hours(15); //飛行15小時
dt_bj += flight_time; //到達的北京時間
cout<<dt_bj<<endl;
local_date_time dt_ny = dt_bj.local_time_in(nyz); //轉化爲紐約時間
cout<<dt_ny<<endl;
}