BOOST 日期时间库 之 时间点 posix_time 3/3

一、时间 boost::posix_time
//--------------------------
1.包含头文件
#include
using namespace boost::posix_time;


//--------------------------
2.时间长度 boost::posix_time::time_duration
//--------------------------
1>、构造函数 秒的单位"秒->毫秒->微妙->纳秒"
time_duration( hour_type, min_type, sec_type = 0, fractional_seconds_type = 0 );

boost::posix_time::time_duration td1(1,10,30,1000); //fractional_seconds->微妙


其构造函数中的时分秒等值可以是任意的数量,不一定必须在它们的限度里,超出的时间会自动进位或错位
boost::posix_time::time_duration td2(1, 60, 60, 1000*1000*6+1000 ); //2小时01分06.001秒


boost::posix_time::hours hourT(1); //1小时
boost::posix_time::minutes minuteT(10); //10分钟
boost::posix_time::seconds secoudT(30); //30秒
boost::posix_time::millisec ms(1); //1毫秒
boost::posix_time::time_duration td3 = hourT + minuteT + secoudT + ms;


boost::posix_time::time_duration td4 = boost::posix_time::millisec(360010);


boost::posix_time::time_duration td5 = boost::posix_time::duration_from_string("1:10:30:001");


2>、成员函数
hours()->返回时间长度的小时
minutes()->返回时间长度的分钟
seconds()->返回时间长度的秒
fractional_seconds()->返回时间长度的微妙


total_seconds()->返回时间长度的总秒数
total_milliseconds()->返回时间长度的总毫秒数
total_microseconds()->返回时间长度的总微妙数


应用:XXX毫秒 转换成 x小时x分钟x秒x微妙
boost::posix_time::time_duration td6 = boost::posix_time::millisec(360010);
int hI = td6.hours();
int mI = td6.minutes();
int sI = td6.seconds();
long fsI = td6.fractional_seconds(); //微妙


3>、获取时间长度表示的字符串格式
to_simple_string(td6); //返回 HH:MM:SS.fffffffff格式的字符串 如"01:10:30.001000"
to_iso_string(td6); //返回 HHMMSS.fffffffff格式的字符串 如"011030.001000"


//--------------------------
2.时间点 boost::posix_time::ptime
//--------------------------
1>、概念
时间点 = 日期 + 时间长度(小于一天的)
boost::posix_time::ptime = boost::gregorian::date + boost::posix_time::time_duration;


2>、构造
boost::posix_time::ptime pt1( boost::gregorian::date(2010,2,5), boost::posix_time::hours(2) );
boost::posix_time::ptime pt2 = boost::posix_time::time_from_string( "2010-2-5 02:00:10" );
boost::posix_time::ptime pt3 = boost::posix_time::from_iso_string( "20100205T020010" );
boost::posix_time::ptime pt4 = boost::posix_time::second_clock::local_time(); //秒精度的 本地时间
boost::posix_time::ptime pt5 = boost::posix_time::microsec_clock::universal_time(); //微妙精度的 UTC时间


3>、获取日期和时间长度--date()、time_of_date()
boost::gregorian::date date23 = pt1.date();
boost::posix_time::time_duration time_d = pt1.time_of_day();


4>、支持比较操作与加减法操作
pt2 = pt3 + boost::posix_time::hours(2);


5>、提供三个自由函数转换为字符串
to_simple_string(pt3); //2010-Feb-5 02:00:10
to_iso_string(pt3); //20100205T020010
to_iso_extended_string(pt3); //2010-02-5T02:00:10


//--------------------------
3.格式化时间-定制日期时间格式
//--------------------------
摘要
使用专门的格式化对象date_facet()、time_facet()等来搭配IO流,
定制日期时间的表现形式
boost::gregorian::date date33( 2010, 3, 6 );
boost::gregorian::date_facet * dface = new boost::gregorian::date_facet("%Y年%m月%d日");
std::cout.imbue( std::locale( std::cout.getloc(), dface ) );
std::cout << date33 << std::endl; //2010年3月6日


boost::posix_time::time_facet *tdface = new boost::posix_time::time_facet("%Y年%m月%d日 %H时%M分%S%F秒");
std::cout.imbue( std::locale( std::cout.getloc(), tdface ) );
std::cout << pt3 << std::endl; //2010年02月5日 02时00分10秒

    推荐阅读