基本概念这里不再浪费时间介绍了,这里给出时间日期库的常见使用方法:
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
//时间库:
#include //timeer计时器
#include //progress_timer计时器
#include //日期库
#include
int main() {
/**********************************测试代码*************************************************
std::cout << BOOST_VERSION << std::endl;
std::cout << BOOST_LIB_VERSION << std::endl;
std::cout << BOOST_PLATFORM << std::endl;
std::cout << BOOST_COMPILER << std::endl;
std::cout << BOOST_STDLIB << std::endl;*/
//**********************************timer计时器*************************************************
boost::timer TimeObj; //声明计时器,声明对象的同时开始计时
std::cout << TimeObj.elapsed_max() << std::endl; //计时器的最大时间长度,单位是小时
std::cout << TimeObj.elapsed_min() << std::endl; //计时器的最小时间长度,单位是秒
std::cout << TimeObj.elapsed() << std::endl; //计时器从声明开始,到当前的运行时间(这里可以使用单步调试的方式查看时间)
TimeObj.restart();
std::cout << TimeObj.elapsed() << std::endl;
//**********************************progress_timer计时器*************************************************
//boost::progress_timer TimeObj2;
//这里其他方法和timer计时器一样,不同点:析构的时候,会自动打印出计时
//**********************************日期*************************************************
boost::gregorian::date DateObj1(2023, 8, 15);
boost::gregorian::date DateObj2(2023, boost::gregorian::Jan, 6);
boost::gregorian::date DateObj3(DateObj1);
//输出方法:
std::cout << DateObj1 << std::endl; //可以直接输出:2023-Aug-15
printf("%d-%d-%d\n", DateObj1.year(), DateObj1.month(), DateObj1.day()); //2023-8-15
char buffer[_MAX_PATH];
memset(buffer, 0, _MAX_PATH);
sprintf(buffer, "%d-%d-%d", DateObj1.year(), DateObj1.month(), DateObj1.day());
std::cout << buffer << std::endl;
//YYYY-MMM-DD 形式:2023-Aug-15
std::string a = boost::gregorian::to_simple_string(DateObj1);
std::cout << a << std::endl;
//YYYYMMDD 形式:20230815
std::string b = boost::gregorian::to_iso_string(DateObj1);
std::cout << b << std::endl;
//YYYY-MM-DD 形式:2023-08-15
std::string c = boost::gregorian::to_iso_extended_string(DateObj1);
std::cout << c << std::endl;
//tm结构体:
/*struct tm
{
int tm_sec; // seconds after the minute - [0, 60] including leap second
int tm_min; // minutes after the hour - [0, 59]
int tm_hour; // hours since midnight - [0, 23]
int tm_mday; // day of the month - [1, 31]
int tm_mon; // months since January - [0, 11]
int tm_year; // years since 1900
int tm_wday; // days since Sunday - [0, 6]
int tm_yday; // days since January 1 - [0, 365]
int tm_isdst; // daylight savings time flag
};*/
tm ct = boost::gregorian::to_tm(DateObj1);
std::cout << ct.tm_year << "-" << ct.tm_mon << "-" << ct.tm_mday << std::endl;//这里输出的年是从1900年开始算,经过了多长时间
boost::gregorian::date d = boost::gregorian::date_from_tm(ct);
std::cout << d << std::endl;
std::string e = boost::gregorian::to_iso_extended_string(d);
std::cout << e << std::endl; //2023-08-15
//日期分开定义:
boost::gregorian::days ds(11); //定义天数:11
std::cout << ds << std::endl; //输出天数:11
boost::gregorian::weeks ws(3); //定义周数:3
std::cout << ws << std::endl; //输出周数,输出形式为以天数形式输出:21
boost::gregorian::months ms(2); //定义月数:2
std::cout << ms.number_of_months() << std::endl; //输出月数:2
boost::gregorian::years ys(2); //定义年数
std::cout << ys.number_of_years() << std::endl; //输出年数:2
boost::gregorian::months mo(ms + ys); //定义月数为:2年加2月
std::cout << mo.number_of_months() << std::endl; //输出月数:26
//日期的基本算法(加减)
boost::gregorian::date Obj(2023, 3, 25); //定义一个时间:2023-03-25
Obj -= boost::gregorian::months(2); //日期减去月份(这里的月份不能直接写2,要以boost::gregorian::months(2)的形式来写
std::cout << boost::gregorian::to_iso_extended_string(Obj) << std::endl; // boost::gregorian::to_iso_extended_string的形式输出日期: 2023-1-25
//日期的加法
boost::gregorian::date_period dp(boost::gregorian::date(2022, 1, 1), boost::gregorian::days(15)); //定义一个时间区间:2023-1-01/2023-01-15
std::cout << dp << std::endl;
std::cout << dp.begin() << "\t" << dp.last() << "\t" << dp.end() << std::endl; //输出:2022-Jan-01 2022-Jan-15 2022-Jan-16 迭代器的end指向之后一个的下一个
dp.shift(boost::gregorian::days(5)); //将日期向后挪5天
std::cout << dp.begin() << "\t" << dp.last() << "\t" << dp.end() << std::endl; //输出:2022-Jan-06 2022-Jan-20 2022-Jan-21
dp.expand(boost::gregorian::days(5)); //将日期前后加5天
std::cout << dp.begin() << "\t" << dp.last() << "\t" << dp.end() << std::endl; //输出:2022-Jan-01 2022-Jan-25 2022-Jan-21
//时间的判断(判断的是时间区间的头)
//判断是否在某个时间之前:boost::gregorian::date_period().is_before方法
if (dp.is_before(boost::gregorian::date(2022, 2, 2))) {
std::cout << "before 2022-02-02" << std::endl;
}
else {
std::cout << "no before 2022-02-02" << std::endl;
}
//判断是否在某个时间之后:boost::gregorian::date_period().is_before方法
if (dp.is_after(boost::gregorian::date(2019, 12, 5))) {
std::cout << "after 2019-12-05" << std::endl;
}
else {
std::cout << "no after 2019-12-05" << std::endl;
}
//判断日期区间是否包含某个日期:boost::gregorian::date_period().contains方法
if (dp.contains(boost::gregorian::date(2022, 2, 6))) {
std::cout << "contains 2022-01-06" << std::endl;
}
else {
std::cout << "no contains 2022-01-06" << std::endl;
}
//**********************************posix_time*************************************************
//posix_time
//1:10:30:001 1小时,10分钟,30秒,1毫秒
boost::posix_time::time_duration timeObj = boost::posix_time::duration_from_string("1:30:10:0023"); //声明时间:01:30:10.002300
std::cout << timeObj << std::endl;
//时间的分开声明:
boost::posix_time::hours ho(2); //boost::posix_time::hours声明小时:2
boost::posix_time::minutes mi(30); //boost::posix_time::minutes声明分钟:30
boost::posix_time::seconds se(25); //boost::posix_time::seconds声明秒数:25
boost::posix_time::millisec mill(56);//boost::posix_time::millisec声明毫秒:56
//我们可以将分开的声明组合起来:
boost::posix_time::ptime timeObj1(boost::gregorian::date(2022,2,15),ho + mi + se + mill);
std::cout << timeObj1 << std::endl; //输出:02:30:25.056000
//日期时间合并:
boost::posix_time::ptime pTimeObj(boost::gregorian::date(2023, 5, 26), boost::posix_time::hours(2) + boost::posix_time::minutes(32) + boost::posix_time::seconds(25) + boost::posix_time::millisec(56));
std::cout << typeid(pTimeObj.date()).name() << std::endl;
boost::gregorian::date aaa = pTimeObj.date(); //pTempOnj.date()返回值类型:class boost::gregorian::date
std::cout << pTimeObj << std::endl; //输出:2023-May-26 02:32:25.056000
//输出格式:
//1. 可以直接输出:
std::cout << pTimeObj << std::endl; //2023-May-26 02:32:25.056000
//2. YYYY-MMM-DD hh:mm:ss.mi形式:
std::cout << boost::posix_time::to_simple_string(pTimeObj)<<std::endl; //2023-May-26 02:32:25.056000
//3. YYYYMMDD hhmmss.mi形式:
std::cout << boost::posix_time::to_iso_string(pTimeObj) << std::endl; //20230526T023225.056000
//4. YYYY-MM-DDThh-mm-ss-mi形式:
std::cout << boost::posix_time::to_iso_extended_string(pTimeObj) << std::endl; //2023-05-26T02:32:25.056000
//时间是区间声明:
boost::posix_time::time_period tp(pTimeObj, boost::posix_time::hours(5)); //声明时间区间:[2023-May-26 02:32:25.056000/2023-May-26 07:32:25.055999]
std::cout << tp << std::endl;
//将日期向后挪:
tp.shift(boost::posix_time::hours(1));
std::cout << tp.begin() << "\t" << tp.last() << "\t" << tp.end() << std::endl; //将日期向后挪1个小时,输出:2023-May-26 03:32:25.056000 2023-May-26 08:32:25.055999 2023-May-26 08:32:25.056000
//将时间区间前后延长:
tp.expand(boost::posix_time::hours(2));
std::cout << tp.begin() << "\t" << tp.last() << "\t" << tp.end() << std::endl; //将时间前后都演唱2个小时,输出:2023-May-26 01:32:25.056000 2023-May-26 10:32:25.055999 2023-May-26 10:32:25.056000
//判断是否在某个时间之前:
if (tp.is_before(timeObj1)) {
std::cout << tp << "is before " <<" " << timeObj1 << std::endl;
}
else {
std::cout << tp << "no before" <<" " << timeObj1 << std::endl;
}
//判断是否在某个时间之后:
if (tp.is_after(timeObj1)) {
std::cout << tp << "is after " << " " << timeObj1 << std::endl;
}
else {
std::cout << tp << "no after" << " " << timeObj1 << std::endl;
}
//判断是否包含某个日期:
if (tp.contains(timeObj1)) {
std::cout << tp << "is contain " << " " << timeObj1 << std::endl;
}
else {
std::cout << tp << "is contain " << " " << timeObj1 << std::endl;
}
return 0;
}