boost::date_time时间类的使用

#include<boost/date_time/gregorian/gregorian.hpp>
using namespace boost::gregorian;
date类
    1、构造:
     from_string();使用斜杠和连字符
     from_undelmited_string();无分隔符;
     day_clock::local_day();返回当天日期
     day_clock::universal_day();返回当天日期
     2、字符串输出:
     to_simple_string();---->YYYY-mm-DD mm是英文
     to_iso_string();---->YYYYMMDD
     to_iso_extended_string();---->YYYY-MM-DD

to_tm(date)  date-->tm;
date_from_rm tm-->date;

date_period类日期区间
    begin();
    end();
    
    last();
    
    shift(); 平移
    expand();两边扩展
    contains();包含
    intersects();是否存在交集
    is_adjacent();
    is_before(); 之前
    is_after();之后
    intersection(); 交集
    merge();并集
    span();合并两国各日期区间及两者之间的间隔


#include<boost/date_time/posix_time/posix_time.hpp>
using namespace boost::posic_time;

时间长度类
time_duration
duration_from_string();
    hour();
    minutes()
    senconds()
    total_senconds();时间长度的总秒数
    total_milliseconds();时间长度的总毫秒数
    total_microseconds();时间长度的总微秒数
    long fractional_seconds();时间长度的总微秒数
    
    to_simple_string();----->HH:MM:SS.ffffff
    to_iso_string();----->HHMMSS,ffffff
    to_iso_extended_string();----->YYYY-MM-DDTHH:MM:SS,ffffffffff
ptime类:视为 date + duration_time
    
    //格式化字符串
    time_from_string("2018-08-12 01:22:03");
    from_ios_string("20180812T012203")
    
    //获取本地时间
    ptime time= second_clock::local_time();
    ptime time =microsec::universal_time();
    
    date d = time.date();
    time_duration t=time.time_of_day();

ptime--->tm;
    to_tm();


time_period类 :时间区间

#include<boost/date_time/gregorian/gregorian.hpp>
using namespace boost::gregorian;

#include<boost/date_time/posix_time/posix_time.hpp>
using namespace boost::posix_time;
#include<boost/format.hpp>
void date_time_() {

	date d = day_clock::local_day();
	cout << "date1:" << to_iso_string(d) << endl;
	d += months(1);
	d += days(2);
	cout << "date2:" << to_iso_string(d) << endl;


	cout << "time to_simple_string :" << to_simple_string(d) << endl;
	cout << "time to_iso_string :" << to_iso_string(d) << endl;
	cout << "time to_iso_extended_string :" << to_iso_extended_string(d) << endl;
	boost::format fm("%04d-%02d-%02d");
	fm%d.year() % d.month().as_number() % d.day();
	cout << "time custom_string 1 :" << fm.str() << endl;

	boost::format fm1("%1%-%2%-%3%");
	fm.parse("%1%,%2%,%3%");
	fm%d.year() % d.month().as_number() % d.day();
	cout << "time custom_string 2 :" << fm.str() << endl;

	date_period dl(d, date(2019, 3, 6));
	cout << "begin:" << dl.begin() << endl;
	cout << "end:" << dl.end() << endl;
	dl.shift(days(1));
	cout << "shitf end:" << dl.end() << endl;
	cout << "contains:"<<dl.contains(date(2019, 2, 2)) << endl;;

	//----------------------
	ptime t = second_clock::local_time();
	d = t.date();
	time_duration td = t.time_of_day();
	cout << "time to_simple_string :" << to_simple_string(t) << endl;
	cout << "time to_iso_string :" << to_iso_string(t) << endl;
	cout << "time to_iso_extended_string :" << to_iso_extended_string(t) << endl;
	fm.parse("%04d:%02d:%02d");
	fm % td.hours()%td.minutes() % td.seconds();
	cout << "time custom_string 1 :" << fm.str() << endl;


	td += hours(5);
	td += minutes(30);
	cout << "time to_iso_string 2:" << to_iso_string(t) << endl;

	time_period tp(t, ptime(date(2019, 2, 21), time_duration(2, 33, 3, 333)));
	cout << tp.length() << endl;;
	cout << tp.contains(second_clock::local_time())<<endl;
}
发布了136 篇原创文章 · 获赞 22 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/u010261063/article/details/86555905