08-boost-时间库

#include<boost/date_time/gregorian/gregorian.hpp>

using namespace boost::gregorian;

#pragma comment(lib,"libboost_date_time-vc141-mt-gd-x32-1_67.lib")


1、构造日期

date d1(2022,2,2);

date d2=from_string("2022-02-02");

date d3=from_string("2022/02/02")

date d4 = from_underlimited_string("20220202");

date d5(min_date_time)

date d5(max_date_time)


cout << d1.year()<<(int)d1.month() << d1.day()<<endl;

cout << to_simple_string(d2)

to_iso_string(d3)

to_iso_extended_string(d4)


date today = day_clock::local_day() 获取当天日期

today.year()

today.month()

today.day()

today.day_of_week()

today.week_number() 一年中的第几周

today.day_of_year() 一年中的第几天

today.end_of_month() 这个月的结束日期


days d(1)

months m(1)

date d10(2022,02,02);

cout<< to_iso_extended_string( d10+days(-10) ) << endl;


2、date_period

date d1 = date(2022,1,1)

date d2 =date(2022,9,9)

date_period dp1(d1,d2);

cout << "日期范围" << dp1 <<endl;

cout << "这个范围天数" << dp1.length() << endl;


date_period dp2(d1,date_duration(30))

cout << "日期范围"<< dp2 << endl;

dp1.shift(days(3))

cout << "日期范围后移3天" << dp1 << endl;


dp2.expand(days(1))

cout << "日期范围向两边阔1天" << dp2 << endl;


cout << "日期范围是否包含" << dp1.contains(date(2022,2,2)) <<endl;


day_iterator it1(d2); 默认步长为1

day_iterator it1(d2,10); 步长为10天

++it1;

cout << *it1 <<endl;


week_iterator

month_iterator

year_iterator


3、ptime

#include<boost/date_time/posix_time/posix_time.hpp>

using namespace boost::posix_time

ptime p1 = time_from_string("2022-2-2 02:00:00")

ptime p2 = from_iso_string("20030303T030000")

ptime p3 = second_clock::local_time() 本地当前时间 秒精度

ptime p4 = microsec_clock::universal_time(); UTC当前时间,微秒精度

ptime p5(min_date_time)

ptime p6(max_date_time)


4、获取当前格式化时间

date d1 = day_clock::local_day();

date_facet *pdf = new date_facet("%Y-%m-%d %H:%M:%S ");

cout.imbue( locale(cout.getlocl(),pdf) )

cout << d1 << endl; 输出2022-09-13 12:01:01