与上一节中介绍的与位置无关的时间不同,与位置相关的时间考虑了时区。 Boost.DateTime 提供类 boost::local_time::local_date_time,定义在 boost/date_time/local_time/local_time.hpp 中。此类使用 boost::local_time::posix_time_zone 存储时区相关数据。
示例 36.17。使用 boost::local_time::local_date_time
- #include <boost/date_time/local_time/local_time.hpp>
- #include <iostream>
-
- using namespace boost::local_time;
- using namespace boost::posix_time;
- using namespace boost::gregorian;
-
- int main()
- {
- time_zone_ptr tz{new posix_time_zone{"CET+1"}};
- ptime pt{date{2014, 5, 12}, time_duration{12, 0, 0}};
- local_date_time dt{pt, tz};
- std::cout << dt.utc_time() << '\n';
- std::cout << dt << '\n';
- std::cout << dt.local_time() << '\n';
- std::cout << dt.zone_name() << '\n';
- }
boost::local_time::local_date_time 的构造函数期望它的第一个参数是 boost::posix_time