以下是算法你库的包含文件,一些操作函数在这些文件中定义声明
#include<boost/date_time.hpp>
#include <boost/date_time/posix_time/ptime.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
图片摘自网络
///测试时间
typedef boost::posix_time::ptime ptime;
using namespace boost::gregorian;
//取当前时间
ptime t(boost::posix_time::second_clock::local_time());
//将时间格式转换成标准格式
//YYYYMMDD
std::string day_str1 = to_iso_string(t.date());
//YYYY-MM-DD
std::string day_str2 = to_iso_extended_string(t.date());
//YYYYMMDDThhmmss
std::string day_str3 = to_iso_string(t);
//YYYY-MM-DDThhmmss
std::string day_str4 = to_iso_extended_string(t);
//hhmmss
std::string day_str5 = to_iso_string(t.time_of_day());
//hh::mm:ss
std::string day_str12 = to_simple_string(t.time_of_day());
//将ptime类型转换成tm类型
tm tm_t = to_tm(t);
//将tm类型转换成ptime类型的子类
date new_today = date_from_tm(tm_t);
assert(new_today == t.date());
//日期加减
date sp(2013, 3, 31);
std::string day_str6 = to_iso_extended_string(sp);
sp += days(1);
std::string day_str7 = to_iso_extended_string(sp);
using namespace boost::posix_time;
//时间加减
time_duration spp(23, 59, 31);
spp += seconds(65);
auto kkk=spp.hours();
std::string day_str11 = to_simple_string(spp);
//将标准格式化字符串转成ptime
ptime day_str8 = time_from_string("2016-06-15 17:00:00");
ptime day_str10 = time_from_string("2016-06-18 17:00:00");
date day_str9 = from_string("2016-06-15");
date_duration dd = t.date() - day_str9;
int numm = dd.days();
//time_duration tt = day_str8- day_str10;
//int numm2 = tt.seconds();
//判断大小
bool result = day_str10.date() > t.date();
bool result2 = day_str10.date() < t.date();