1.boost处理日期的类的介绍。
类所在位置:
#include <boost/date_time/gregorian/gregorian.hpp>
using namespace boost::gregorian;
date类构造函数:
date(year_type, month_type, day_type);
date(const ymd_type&);
date d1;
date d2(2010, 1, 1);
date d3(2010, Jan, 1);
date d4(d2);
date d1 = from_string("1999-9-3");
date d2(from_string("2005-1-1"));
静态成员函数:
day_clock::local_day();//返回目前的时间。2013-Nov-30
day_clock::universal_day();
date d1=day_clock::local_day();//使用
date d2=day_clock::universal_day();
date类的成员函数:
year_type year() const;
month_type month() const;
day_type day() const;
day_of_week_type day_of_week() const;
ymd_type year_month_day() const;
输出日期:
date d2(2010, 1, 1);
cout<<to_simple_string(d2)<<endl;//2010-Jan-01
cout<<to_iso_string(d)<<endl;//20100101
cout<<to_iso_extended_string(d)<<endl;//2010-01-01
cout<<d<<endl;//2010-Jan-01
日期的运算:
cout<<d2 - d1<<endl;
d2 += days(10);
d2 += months(3);
d2 += years(3);
日期迭代器:
date_iterator,week_iterator,month_iterator, year_iterator.
day_iterator d_iter(d);
++d_iter;(为了效率考虑,没有后缀式,即iter++);
week_iterator w_iter(d);
++w_iter;
month_iterator m_iter(d);
++m_iter;
year_iterator y_iter(d);
++y_iter;
部分代码:
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
#include <iterator>
#include <algorithm>
#include <hash_map>
//#include <boost/timer/timer.hpp>
//#include <windows.h>
#include <boost/date_time/gregorian/gregorian.hpp>
using namespace boost::gregorian;
using std::cout;
using std::endl;
int main()
{
std::cout<<day_clock::local_day()<<std::endl;
std::cout<<day_clock::universal_day()<<std::endl;
date d1 = day_clock::local_day();
std::cout<<d1.day_of_week()<<"\n";
date d2(2010, 1, 1);
cout<<to_simple_string(d2)<<endl;
cout<<to_iso_string(d2)<<endl;
cout<<to_iso_extended_string(d2)<<endl;
cout<<d2<<endl;
cout<<d1 - d2<<endl;
d2 += days(10);
d2 += months(3);
d2 += years(3);
date d(2010, 1, 1);
day_iterator d_iter(d);
++d_iter;//指向2010-Jan-02
week_iterator w_iter(d);
++w_iter;//指向2010-Jan-08
month_iterator m_iter(d);
++m_iter;//指向2010-Feb-02
year_iterator y_iter(d);
++y_iter;//指向2011-Jan-02
cout<<d<<endl;
d2.month();
d2.year();
d2.day();
d2.day_of_week();
return 0;
}