将UTC时差转换为字符串提升

时间:2012-12-07 13:44:41

标签: c++ boost

我有两个UTC时间戳(自1970年1月1日起的时间)

我想将它们之间的差异显示为字符串%H:%M:%S例如。十三点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?

1 个答案:

答案 0 :(得分:3)

这样的事情可以完成这项工作,你不需要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;
}