// boost_time.cpp : 定义控制台应用程序的入口点。
//made by davidsu33
//2014-5-11
//the usage of posix_time
#include "stdafx.h"
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
#include <cassert>
using namespace std;
#define SHOW_VARIABLE(x)\
{\
cout<<#x<<"="<<x<<endl;\
}
void show_tm(std::tm & atm)
{
cout<<"year="<<atm.tm_year<<" month="<<atm.tm_mon<<" day="<<atm.tm_mday<<endl;
cout<<"hour="<<atm.tm_hour<<" minute="<<atm.tm_min<<" sec="<<atm.tm_sec<<endl;
}
//时间区间
void use_time_duration()
{
//mili-sec 毫秒
//mico-sec 微妙
//nano-sec 纳秒
//1hour 20minutes 30seconds 1000mico-seconds
boost::posix_time::time_duration td(1, 20, 30, 1000);
//cout<<"td="<<td<<endl;
SHOW_VARIABLE(td);
//根据所给的数值自动的进制
boost::posix_time::time_duration td2(1, 60, 60, 1000*1000*6);
SHOW_VARIABLE(td2);
boost::posix_time::time_duration td3(2, 1, 6);
assert(td3 == td2);
SHOW_VARIABLE(td3);
//hours minutes seconds 分别从time_duration派生
boost::posix_time::hours h(2);
boost::posix_time::minutes m(1);
boost::posix_time::seconds s(6);
boost::posix_time::time_duration td4 = h + m + s;
assert(td4 == td3);
SHOW_VARIABLE(td4);
//duration_from_string
std::string str = "2:1:6:0";
boost::posix_time::time_duration td5 = boost::posix_time::duration_from_string(str);
assert(td5 == td4);
SHOW_VARIABLE(td5);
//转换为tm结构
std::tm tm2 = boost::posix_time::to_tm(td5);
show_tm(tm2);
}
void uage_of_ptime()
{
//ptime时间点
//2014年5月11号 凌晨1点
boost::posix_time::ptime
t1(boost::gregorian::date(2014, 5, 11), boost::posix_time::hours(1));
SHOW_VARIABLE(t1);
//time_from_string
std::string s1 = "2014-5-11 01:00:00";
boost::posix_time::ptime t2 =
boost::posix_time::time_from_string(s1);
SHOW_VARIABLE(t2);
//from_iso_string
boost::posix_time::ptime t3 =
boost::posix_time::from_iso_string("20140511T010000");
SHOW_VARIABLE(t3);
}
int _tmain(int argc, _TCHAR* argv[])
{
use_time_duration();
uage_of_ptime();
getchar();
return 0;
}
boost的posix_time用法详解01
最新推荐文章于 2024-05-19 20:13:23 发布