//#define DATE_TIME_NO_DEFAULT_CONSTRUCTOR //不希望出现无效时间 #include #include #include #include #include using namespace std; using namespace boost; using namespace boost::gregorian; using namespace boost::posix_time; //自定义字面值 c11 新增了重载operator""的特性 days operator"" _D(unsigned long long n) { return days(n); } weeks operator"" _W(unsigned long long n) { return weeks(n); } date operator"" _YMD(const char* s, std::size_t) { return from_string(s); } int main() { timer t; //计时器 cout << "max timespan:" << t.elapsed_max() / 3600 << "h" << endl; // 可度量的最大时间 cout << "min timespan:" << t.elapsed_min() << "s" << endl; //最小时间 cout << "now time elapsed:" << t.elapsed() << "s" << endl; //流逝的时间 progress_timer t2; //析构时会打印流逝时间elapsed() // progress_timer t3 = t2; //继承自noncopyable 不行 // progress_timer t3(t2); //继承自noncopyable 不行 /******************************DATE*******************************/ gregorian::date d(2021,7,11); d = day_clock::local_day(); //当前时间 gregorian::date d2 = from_string("2021-7-11"); //d = from_undelimited_string("20210711"); cout << to_iso_extended_string(d) << endl; // 日期以指定格式打印 cout << to_iso_string(d) << endl; cout << to_simple_string(d) << endl; if (d > d2) { //支持比较操作符 cout << "1111" << endl; } int inweek = d.day_of_week(); cout << inweek << endl; //星期几 int numInYear = d.day_of_year(); cout << numInYear << endl; //一年中的第几天 // 与C结构 tm 相互转换 tm _tm = to_tm(d); date_from_tm(_tm); /******************************DATE END*******************************/ //日期计算 days day(1); //1天 weeks week(1); //1个星期 months mon(1); //1个月 years year(1); //1年 d += day ; d += week; d += mon; d += year; cout << to_iso_extended_string(d) << endl; // date ddd(2021, 3, 30); ddd -= months(1); //20210228 //注意 这里得到的是月末 //日期区间 左闭右开区间 date_period dp(d, days(20)); if (dp.contains(d + days(2))) //是否包含 is_before is_after 在前在后、 cout << 111 << endl; //还有交集、并集等 不常用就不一一讲了 //日期迭代器 好像没啥用。。。 //day_iterator iter(d); /******************************TIME*******************************/ posix_time::time_duration time(1,10,30,1000);//时、分、秒、微秒 cout << time << endl; hours houre(1); minutes min(10); seconds sec(59); millisec ms(1000); time_duration time2 = houre + min + sec + ms; cout << to_simple_string(time2) << endl; //以指定格式打印 //与 date类似也可以用于比较 //转换到tm _tm = to_tm(time2); //但是不能反向转换 //默认精确度到微秒,纳秒相关的类函数默认都不可用!! 纳秒用得也不多,就不讨论了。。 /******************************TIME END*******************************/ /******************************DATE_TIME*******************************/ ptime _ptime(d, time); //同时包含 日期和时间的结构 ptime p1 = time_from_string("2021-7-12 01:00:00"); ptime p2 = from_iso_string("20210712T020000"); ptime p3 = second_clock::local_time(); //精确到秒 ptime p4 = second_clock::universal_time(); //精确到微秒 //操作ptime 可以分解为 date time 来处理 //也支持加减处理 p3 += hours(3); cout << to_simple_string(p3) << endl; cout << to_iso_string(p3) << endl; cout << to_iso_extended_string(p3) << endl; //源码是以T为分隔,可以自己改为空格 //转为c结构: _tm = to_tm(p3); ptime_from_tm(_tm); ptime p5 = from_time_t(std::time(0)); std::time_t time_t = to_time_t(p5); //时间区间 time_period //类似于 date_period 不在细说了 /******************************DATE_TIME_END*******************************/ days d11 = 11_D; //这个用来装逼还挺有一套,基础撇点的都不知道,哈哈 weeks w3 = 3_W; date d20110712 = "2021-7-12"_YMD; //格式化时间 //这个玩意儿 继承自std::locale::facet 是个智能指针,当引用计数为0 就会自动delete掉 。 所以当创在栈上,或创在堆上手动delete都会飞掉!注意了! date_facet *facet = new date_facet("%Y年%m月%d日"); cout.imbue(locale(cout.getloc(),facet)); cout << d20110712 << endl; time_facet* tfacet = new time_facet("%Y年%m月%d日 %H点%M分%S%F秒"); cout.imbue(locale(cout.getloc(), tfacet)); cout << p4 << endl; return 0; }