boost学习笔记(时间长度)

时间是日期的细化,在年月日的基础上增加了时分秒。需要头文件:posix_time.hpp

#include <iostream>
#include <boost\date_time\gregorian\gregorian.hpp>
#include <boost\date_time\posix_time\posix_time.hpp>

using namespace std;
using namespace boost;

int main() {

    //构造一个1小时10分钟30秒1毫秒的时间长度
    posix_time::time_duration td(1,10,30,1000);
    cout << td << endl;
    //也可以从字符串创建
    td = posix_time::duration_from_string("1:10:30.001");
    cout << td << endl;

    posix_time::hours h(1);
    posix_time::minutes m(10);
    posix_time::seconds s(30);
    posix_time::milliseconds ms(1);

    //时间长度可以进行运算
    posix_time::time_duration td1 = h + m + s + ms;
    cout << td1 << endl;

    //常用函数
    cout << "hours:" << td.hours() << endl;
    cout << "minutes:" << td.minutes() << endl;
    cout << "seconds:" << td.seconds() << endl;
    cout << "total_seconds:" << td.total_seconds() << endl;
    cout << "total_mls:" << td.total_milliseconds() << endl;
    cout << "total_mcs:" << td.total_microseconds() << endl;
    cout << "to_simple_string:" << posix_time::to_simple_string(td) << endl;
    return 0;
}
01:10:30.001000
01:10:30.001000
01:10:30.001000
hours:1
minutes:10
seconds:30
total_seconds:4230
total_mls:4230001
total_mcs:4230001000
to_simple_string:01:10:30.001000

猜你喜欢

转载自blog.csdn.net/maosijunzi/article/details/80761499
今日推荐