将boost :: posix_time :: time_duration提升为字符串

Tom*_*mmy 3 c++ boost

您好我正在使用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());
}
Run Code Online (Sandbox Code Playgroud)

但是to_iso_extended_string不接受类型

boost::posix_time::time_duration
Run Code Online (Sandbox Code Playgroud)

只有类型

boost::posix_time
Run Code Online (Sandbox Code Playgroud)

这可以在这里看到

我想返回一个字符串以供以后输出等

我怎么解决这个问题?我无法在boost中看到转换方法

boost::posix_time::time_duration
Run Code Online (Sandbox Code Playgroud)

串起来.我是C++的新手,如果这是一个非常简单的话,我会道歉.

Sam*_*ler 6

使用operator <<

#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$ 
Run Code Online (Sandbox Code Playgroud)

请注意,您需要使用posix_time.hpp标头而不是posix_time_types.hpp包含I/O运算符.


yas*_*ser -3

您可以使用boost 日期/时间库将时间转换为字符串。