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