头文件和命名空间
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost::posix_time;
using namespace boost::gregorian;
构造函数
1.
std::string ts("2002-01-20 23:59:59.000");
ptime t(time_from_string(ts))
boost::posix_time::ptime time_now ( boost::posix_time::second_clock::local_time() ); //机器的本地时间,比如北京时间,与电脑设置的时区有关
boost::posix_time::ptime time_now ( boost::posix_time::second_clock::universal_time() );//机器的GMT时间,本地时间加减时区偏移量
2.
std::string ts("20020131T235959");
ptime t(from_iso_string(ts))
3.
ptime p1(date(2012, 11, 30), time_duration(1,2,3,4));
转为字符串
to_simple_string(const boost::posix_time &pt) //"2002-01-20 23:59:59.000"
to_iso_string(const boost::posix_time &pt) //"20020131T235959"
两个ptime相减得到一个boost::posix_time::time_duration对象
ptime的time_of_day()函数得到一个boost::posix_time::time_duration对象
ptime的date()返回一个boost::gregorian::date对象
time_duration的成员函数
total_microseconds() 将时分秒累计转为微秒
totao_nanoseconds() 将时分秒累计转为纳秒
total_milliseconds() 将时分秒累计转为毫秒
total_seconds() 将时分秒累计转为秒
hours() 返回小时部分
minutes() 返回分钟部分
seconds() 返回秒部分
is_negative() 是否为负值,比如两个ptime对象相减,第一个对象时间早,第二个对象时间晚
is_neg_infinity() 是否达到所能表示的最大负值
is_pos_infinity() 是否达到所能表示的最大正值
is_not_a_date_time() 是否是无效的类型time_duration类型
date的成员函数
date_of_week()返回一个boost::gregorian::grep_weekday对象,即得到该日期是星期几,
可以调用as_short_string()返回星期几的英文前三个字母,as_long_string()返回星期几的完成单词
也可以转为一个数字as_number()返回0-6表示Sun-Mon
date_of_year()返回改日期在一年中是第几天1-365(366)
year_month_day()返回一个类型可以得到年月日格式的日期
boost::posix_time::ptime tw(boost::posix_time::second_clock::universal_time());
//vaen_TolCheckm_Www_dd_Mon_yyyy_HH24_MM_SS.qvt
//vaen_TolPQRm_Www_dd_Mon_yyyy_HH24_MM_SS.qvt
string name;
name = (type == CLogger::T_LOG)?"vae0_TolCheck":"vae0_TolPRQ";
stringstream ss;
ss<<name<<partitionID<<"_";
ss<<tw.date().day_of_week().as_short_string()<<"_";
auto dt = tw.date().year_month_day();
ss<<dt.day<<"_"<<dt.month<<"_"<<dt.year<<"_";
auto td = tw.time_of_day();
ss<<td.hours()<<"24_"<<td.minutes()<<"_"<<td.seconds()<<".qvt";