您的位置:首页 > 其它

(一)boost库之日期、时间

2016-03-15 14:32 441 查看
一、计时器 计时器,通常在一个项目中统计一个函数的执行时间是非常实用的。
#include <boost/timer.hpp>
void PrintUserTime()
{
boost::timer t;  //定义一个计时类,开始计时
std::cout << "可度量的最大时间:" << t.elapsed_max()/3600 << "h" << std::endl;
std::cout << "可度量的最大时间:" << t.elapsed_min() << "s" << std::endl;
std::cout << "使用时间为:" <<  t.elapsed() << std::endl;
}
 二、获取当前日期
date d(day_clock::local_day());
 三、将字符串转换成日期
date fmtdt1 = from_string("2013-04-21");
date fmtdt2 = date_from_iso_string("20140320");
date fmtdt3 = from_us_string("1-25-2003")
 四、日期转换成字符串
to_iso_string
to_simple_string
 五、日期长度,以天为单位 
date_duration dur = end - beg;
cout << dur.days() << endl;
 六、日期区间接收两个时间点,用于判断时间区间的交集、并集、间隔、区间平移等关于区间的操作
date_period p(beg, end);
 七、日期运算提供days、months、years三个时间类,可与日期类进行混合运算
months m(1);
date end = d + m;
 八、日期、日期与字符串转换、日期加减运算
#include <boost/date_time/gregorian/gregorian.hpp>
void PrintDate()
{
using namespace boost::gregorian;
using namespace std;
//获取当前时间
date d(day_clock::local_day());
//日期类支持IO流输出
cout << d << endl;
//将日期转换成字符串  20140320
cout << to_iso_string(d) << endl;
//将字符串中转换成日期
    date fmtdt1 = from_string("2013-04-21");
date fmtdt2 = from_string("2013/04/21");
cout << fmtdt1 << " " << fmtdt2 << endl;
//日期的运算,
days day(1);
    months m(1);
years y(1);
date beg = d + day - y;
date end = d + m;
cout << (beg < end ? "Yes" : "No") << endl;
//特别地, date - date = date_duration , 同时也支持date +/- date_duration
//日期长度
    date_duration dur = end - beg;
cout << dur.days() << endl;
//日期区间
date_period p(beg, dur);
cout << p << endl;
}
 九、时间、时间与字符串转换、时间加减运算和日期大同小异,也提供时间长度:time_duration,时间区间:time_period,及时间操作类hours、minutes、seconds、milliseconds、microseconds
#include <boost/date_time/posix_time/posix_time.hpp>
void PrintTime()
{
using namespace boost::posix_time;
using namespace std;
using namespace boost::gregorian;
//获取本地时间
ptime t1(second_clock::local_time());
//获取UTC时间
ptime t2(second_clock::universal_time());
cout << t1 << t2 << endl;
//时间8:30
time_duration dur(8,30,0);
hours h(1);
minutes m(1);
seconds s(1);
milliseconds mi(1);   //毫秒
microseconds mic(1);  //微秒
//nanoseconds na(1);    //纳秒   需要系统的支持
ptime t3(date(2013,4,20));
ptime t4(date(2013,4,20), dur);
ptime t5(date(2013,4,20), mic);
cout << to_simple_string(t3) <<" " <<  to_simple_string(t5) << endl;
    //时间的运算,与日期差不多,非常简单
    cout << t2 - t1 << endl;
}
 十、格式化时间主要用来两个类,输入:date_input_facet, 输出:date_facet 时间格式化类为:time_input_facet   time_facet  boost::posix_time::time_facet *timeFmt = new boost::posix_time::time_facet("%Y-%m-%d %H:%M:%S");
#include <boost/date_time/gregorian/gregorian.hpp>
void FormatDateTime()
{
using namespace boost::gregorian;
using namespace std;
    //输入
    date d;
    std::string teststring = "140320";
    boost::gregorian::date_input_facet *fmt = new boost::gregorian::date_input_facet("%y%m%d");
    std::stringstream ss1(teststring);
    ss1.imbue(locale(ss1.getloc(), fmt));
    ss1 >> d;
cout << d << endl;
    //输出
    boost::gregorian::date d2(2009, 1, 7);
    boost::gregorian::date_facet *fmt2 = new boost::gregorian::date_facet("%A, %d %B %Y");
    std::cout.imbue(std::locale(std::cout.getloc(), fmt2));
    std::cout << d2 << std::endl;
    //定制
    std::locale::global(std::locale("German"));
    std::string months[12] = { "Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember" };
    std::string weekdays[7] = { "Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag" };
    boost::gregorian::date d3(2009, 1, 7);
    boost::gregorian::date_facet *df = new boost::gregorian::date_facet("%A, %d. %B %Y");
    df->long_month_names(std::vector<std::string>(months, months + 12));
    df->long_weekday_names(std::vector<std::string>(weekdays, weekdays + 7));
    std::cout.imbue(std::locale(std::cout.getloc(), df));
    std::cout << d3 << std::endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: