// 忽略警告
// 调试日期:2019-03-14 星期4
#define BOOST_DATE_TIME_POSIX_TIME_SET_CONFIG// 让time_duration精确到纳秒(Linux下)
//#define BOOST_DATE_TIME_SOURCE
#define _SCL_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)
#include <assert.h>
#include <iostream>
#include <boost/integer.hpp>
#include <time.h>
#include <boost/timer.hpp> // timer
#include <boost/progress.hpp> // progress_timer、progress_display
#include <sstream> // stringstream
#include <boost/date_time/gregorian/gregorian.hpp>// date_time
using namespace boost::gregorian;
#include <boost/date_time/posix_time/posix_time.hpp>// time_duration(可提供微秒、纳秒级别)
using namespace boost::posix_time;
using namespace boost;
using namespace std;
#include <Windows.h>// Sleep
int main()
{
// 构造
time_duration td1(1, 10, 30, 1000);// 构造一个1小时10分30秒1毫秒(1000微秒)的时长
// 自动进位
time_duration td2(1, 60, 60, 1000 * 1000 * 6 + 1000);// 相当于2小时1分6秒1毫秒的时长
// 子类
hours h(1); // 1小时
minutes m(10); // 10分
seconds s(30); // 30秒
millisec ms(1); // 1毫秒
// nanosec n(10); // 10纳秒
time_duration td3 = h + m + s + ms;// 或者 d3(h + m + s + ms)
// 从字符串构造
time_duration td4 = duration_from_string("1:10:30:001");
assert(td1 == td3); assert(td1 != td2); assert(td4 == td1);
// 成员函数
cout << td1.hours() << endl; // 1
cout << td1.minutes() << endl; // 10
cout << td1.seconds() << endl; // 30
cout << td1.fractional_seconds() << endl; // 微秒数 1000
cout << td1.total_seconds() << endl; // 总秒数 4230
cout << td1.total_milliseconds() << endl; // 总毫秒数 4230001
cout << td1.total_microseconds() << endl; // 总微秒数 4230001000
cout << td1.total_nanoseconds() << endl; // 总纳秒数 4230001000010
cout << td1 << endl; // 01:10:30.001000
cout << td1.resolution() << endl; assert(td1.resolution() == date_time::micro);
cout << td1.num_fractional_digits() << endl;// 6 分辨率
cout << time_duration::ticks_per_second() << endl;// tick数,相当于1秒的微秒数
assert(!td1.is_negative());// 判断符号
hours h1(10);
td3 = h1.invert_sign(); // 符号位取反
cout << td3 << endl; // -10:00:00
// 运算
assert(td1 < td2); assert((td1 + td2) == time_duration(3, 11, 36, 2000));
assert(td1 * 2 == time_duration(2, 21, 0, 2000));
// 得到time_duration的字符串表示
cout << to_simple_string(td1) << endl; // HH:MM:SS.fffffffff
cout << to_iso_string(td1) << endl; // HHMMSS.fffffffff
// 转换(time_duration只有时分秒所以不能转换到time_t)
// time_duration to tm
tm t1 = to_tm(td1); // 年月日默认忽略
// tm to time_duration
time_duration td5(t1.tm_hour, t1.tm_min, t1.tm_sec);
/
// ptime
// 构造
ptime p1(date(2019, 3, 14));// 默认00:00:00
ptime p2(date(2019, 3, 14), td1);// 从date 和 time_duration构造时间点
cout << p1 << endl; // 2019-Mar-14 00:00:00
cout << p2 << endl; // 2019-Mar-14 01:10:30.001000
p1 += hours(1);
cout << p1 << endl; // 2019-Mar-14 01:00:00
ptime p3 = time_from_string("2019-3-14 13:23:00");
ptime p4 = from_iso_string("20190314T132300");
cout << p3 << endl; // 2019-Mar-14 13:23:00
cout << p4 << endl; // 2019-Mar-14 13:23:00
p4 += seconds(23);
cout << p4 << endl; // 2019-Mar-14 13:23:23
cout << "当前时间" << endl;
cout << second_clock::local_time() << endl; // 秒精确
cout << microsec_clock::local_time() << endl; // 微秒精确
// 分解
cout << p3.date() << endl; // 返回日期date类型
cout << p3.time_of_day() << endl; // 返回时间time_duration类型
// 运算
assert(p3 < p4);
assert(p3 + seconds(23) == p4);
// 得到time_duration的字符串表示
cout << to_simple_string(p3) << endl; // YYYY-mmm-DD HH:MM:SS.fffffffff(f..秒的部分为0则不显示) 2019-Mar-14 13:23:00
cout << to_iso_string(p3) << endl; // YYYYMMDDTHHMMSS.fffffffff(T是日期与时间的分隔) 20190314T132300
cout << to_iso_extended_string(p3) << endl; // YYYY-MM-DDTHH:MM:SS.fffffffff 2019-03-14T13:23:00
// ptime to tm
tm t2 = to_tm(p3);
// tm to ptime
t2.tm_hour += 11;
ptime p5 = ptime_from_tm(t2);
cout << p5 << endl; // 2019-Mar-15 00:23:00(跳到第二天)
// ptime to time_t
time_t tt = to_time_t(second_clock::local_time());
cout << tt << endl; // (东八区快UTC时间8小时)
cout << time(NULL) << endl; // UTC时间戳
// time_t to ptime
ptime p6 = from_time_t(tt); //
ptime p7 = from_time_t(time(NULL));
cout << p6 << endl; // second_clock::local_time()
cout << p7 << endl; // UTC时间
// FILETIME to ptime(怎么转换的暂时没搞清楚)
FILETIME ft;
ft.dwHighDateTime = 29715317;
ft.dwLowDateTime = 3865122988UL;
ptime p8 = from_ftime<ptime>(ft);
cout << p8 << endl; // UTC时间
// 时间区间time_period[左闭,右开),用法语date_period基本一致
time_period tp1(p5, hours(8));// [2019,Mar,15 00:23:00,2019,Mar,15 08:23:00)
cout << tp1.begin() << endl;// 2019-Mar-15 00:23:00
cout << tp1.last() << endl;// 2019-Mar-15 08:22:59.999999
cout << tp1.end() << endl;//2019-Mar-15 08:23:00
cout << tp1.last() - tp1.begin() << endl;// 07:59:59.999999
// 时间迭代器
time_iterator t_iter(p5, hours(1));// 1小时步长
t_iter++;
assert(p5 + hours(1) == *t_iter);
// 格式化日期、时间
date d(2019, 3, 14);
date_facet* dfacet = new date_facet("%Y年%m月%d日");
stringstream sss;
sss.imbue(locale(cout.getloc(), dfacet));
sss << d << endl;// 格式化定向到stringstream中
cout << sss.str() << endl;// 2019年03月14日
time_facet* tfacet = new time_facet("%Y年%m月%d日%H时%M分%S%F秒");
cout.imbue(locale(cout.getloc(), tfacet));
cout << tp1.last() << endl;// 2019年03月15日08时22分59.999999秒
return 0;
}