1

我有两个 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?
4

1 回答 1

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;
}
于 2012-12-07T15:36:11.507 回答