[英]Boost boost::posix_time::time_duration to string
您好我正在使用boost Posix Time System。 我上課了
class event{
private:
boost::posix_time::ptime time;
//some other stuufff
public:
string gettime(void);
}
//functions
string event::gettime(void){
return to_iso_extended_string(time.time_of_day());
}
但是to_iso_extended_string不接受類型
boost::posix_time::time_duration
只有類型
boost::posix_time
這可以在這里看到
我想返回一個字符串以供以后輸出等
我怎么解決這個問題? 我無法在boost中看到轉換方法
boost::posix_time::time_duration
串起來。 我是C ++的新手,如果這是一個非常簡單的話,我會道歉。
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
int
main()
{
using namespace boost::posix_time;
const ptime start = microsec_clock::local_time();
const ptime stop = microsec_clock::local_time();
const time_duration elapsed = stop - start;
std::cout << elapsed << std::endl;
}
mac:stackoverflow samm$ g++ posix_time.cc -I /opt/local/include
mac:stackoverflow samm$ ./a.out
00:00:00.000485
mac:stackoverflow samm$
請注意,您需要使用posix_time.hpp
標頭而不是posix_time_types.hpp
來包含I / O運算符。
我認為使用time_duration進行字符串格式化很煩人。 我想將以秒為單位指定的持續時間格式化為d HH:mm:ss(例如1 10:17:36,持續123456秒)。 由於我找不到合適的格式化功能,我做了一些“手工”的工作:
const int HOURS_PER_DAY = 24;
std::string formattedDuration(const int seconds)
{
boost::posix_time::time_duration a_duration = boost::posix_time::seconds(seconds);
int a_days = a_duration.hours() / HOURS_PER_DAY;
int a_hours = a_duration.hours() - (a_days * HOURS_PER_DAY);
return str(boost::format("%d %d:%d:%d") % a_days % a_hours % a_duration.minutes() % a_duration.seconds());
}
不是很優雅,但我想出的最好。
你試過簡單地使用<<運算符:
std::stringstream ssDuration;
ssDuration << duration;
std::string str = ssDuration.str();
您可以使用提升日期/時間庫將時間轉換為字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.