c++ - 将UTC时差转换成字符串boost

标签 c++ boost

我有两个 UTC 时间戳(自 1.1.1970 以来的时间)

我想将它们之间的区别显示为字符串 %H:%M:%S 例如。 13:34:12

目前我已经走到这一步

time_facet *facet = new time_facet("%H:%M:%S");
cout.imbue(locale(cout.getloc(), facet));

ptime now = boost::date_time::not_a_date_time;
now = boost::posix_time::microsec_clock::universal_time();

ptime timerEnd = from_time_t(timestamp);
boost::posix_time::time_period tp(now, timerEnd);

//what now?

最佳答案

像这样的东西就可以完成工作,你不需要 time_period

#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>

namespace pt = boost::posix_time;

int main() {
    //format for ptime
    pt::time_facet *facet = new pt::time_facet("%H:%M:%S");
    //format for time_duration
    facet->time_duration_format("%H:%M");

    std::cout.imbue(std::locale(std::cout.getloc(), facet));

    time_t timestamp1 = 79387320;
    time_t timestamp2 = 79377320;
    pt::time_duration td = pt::from_time_t(timestamp1) - pt::from_time_t(timestamp2);

    std::cout << td << std::endl;       
    return 0;
}

关于c++ - 将UTC时差转换成字符串boost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13764301/

相关文章:

c++ - 将 double 转换为 int 时抛出浮点异常

c++ - std::string、std::wstring 和 UTF8

c++ - boost::bimap 不采用 vector_of

c++ - boost phoenix 的 map new_?

c++ - 使用 ROS 在 C++11 中 boost 文件系统段错误

c++ - 如何设置 "this"线程的自定义名称?

c++ - 在构造函数中使用 constexpr 成员

c++ - 通用线程池类无法正常工作

c++ - 推荐跨平台 C++ GUI 和网络库

c++ - MFC中如何在非矩形窗口中绘制边框