引言
使用boost::posix_time::microsec_clock::local_time()获取本地时间
boost::posix_time::microsec_clock::universal_time()获取格林威治时间
boost::aiso::timer的expries_at()返回的格林威治时间
问题
有时候获取到格林威治时间时间,想输出为本地时间,
解决办法
using namespace std;
// Get current time, as an example
boost::posix_time::ptime dt = boost::posix_time::microsec_clock::universal_time();
// Create a time_zone_ptr for the desired time zone and use it to create a local_date_time
boost::local_time::time_zone_ptr zone(new boost::local_time::posix_time_zone("EST+08:00:00"));
boost::local_time::local_date_time dt_with_zone(dt, zone);
std::stringstream strm;
// Set the formatting facet on the stringstream and print the local_date_time to it.
// Ownership of the boost::local_time::local_time_facet object goes to the created std::locale object.
strm.imbue(std::locale(std::cout.getloc(), new boost::local_time::local_time_facet("%Y-%m-%d %H:%M:%S UTC%Q")));
strm << dt_with_zone;
// Print the stream's content to the console
cout << strm.str() << endl;

本文介绍了如何使用Boost库中的posix_time模块将格林威治时间转换为本地时间。通过创建特定时区的时间区对象并应用时间格式化,可以将获取的全局时间显示为本地时间。

被折叠的 条评论
为什么被折叠?



