c++ - 在 universal_time 上使用时间方面

标签 c++ boost datetime

在 boost 上,要创建一个时间方面来格式化指定的时间,我们使用以下方法:

boost::local_time::local_time_facet* facet = new boost::local_time::local_time_facet("%Y%m%d %H:%M:%S.%f");
std::stringstream date_stream;
date_stream.imbue(std::locale(date_stream.getloc(), facet));
date_stream << boost::local_time::local_microsec_clock::local_time(boost::local_time::time_zone_ptr());

我如何做同样的事情,但使用通用时钟:

boost::posix_time::microsec_clock::universal_time()

谢谢

最佳答案

我知道现在 scooterman 要么找到答案要么不再关心 (:D),但如果有人在搜索时发现了这个问题(就像我一样),这里就是答案:

boost::posix_time::ptime time(microsec_clock::universal_time());
std::stringstream ss;
boost::date_time::time_facet *facetPtr 
              = new boost::date_time::time_facet("%a, %d %b %Y %H:%M:%S.%f GMT");
ss.imbue(std::locale(ss.getloc(), facetPtr));
ss << time;
//ss.str() contains the formatted time string

关于c++ - 在 universal_time 上使用时间方面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2516165/

相关文章:

c++ - 如何访问位图中的像素颜色?

c++ - int 有效,但 string::size_type 无效

c++ - 在 boost::format 中使用 * 宽度和精度说明符

hibernate - 在 HQL 中执行日期/时间数学?

datetime - SSRS 表达式删除日期中的前导 0

javascript - 经常更新值时闪烁

c++ - 有没有一种方法可以在不使用 new 的情况下从函数返回抽象(出于性能原因)

c++ - 指示 Qi 转换属性失败的正确方法是什么?

c++ - 我可以从 constexpr 函数返回一个可选值吗?

c++ - 使用 boost asio 原始套接字创建第 2 层/以太网套接字(在 C++ 中)