6

Hello I am using boost Posix Time System. I have a class

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());
}

but to_iso_extended_string does not take type

boost::posix_time::time_duration

only type

boost::posix_time

this can be seen here

I want to return a string for later output e.t.c.

How can I solve this problem? I can't see a method in boost to convert

boost::posix_time::time_duration

to string. I am new to both C++ and boost so apologies if this is a real simple one.

4 Answers 4

6

use 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$ 

Note you'll need to use the posix_time.hpp header rather than posix_time_types.hpp to include the I/O operators.

2

Have you tried simply using the << operator:

std::stringstream ssDuration;
ssDuration << duration;

std::string str = ssDuration.str();
1

I think the string formatting with time_duration is kind of annoying. I wanted to format a time duration specified in seconds into d HH:mm:ss (e.g. 1 10:17:36 for 123456 seconds). Since I could not find a suitable formatting function I did some of the work 'by hand':

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());
}

Not very elegant but the best I came up with.

1
  • Don't forget about your very useful friend modulus: int a_hours = a_duration.hours() % HOURS_PER_DAY;
    – moodboom
    Jul 17, 2017 at 19:22
-3

You can use boost date/time library for converting the time to string.

3
  • that's where I've been looking but can't find a function for boost::posix_time::time_duration
    – Tommy
    Jan 19, 2011 at 15:38
  • Please see here (Conversion to String): boost.org/doc/libs/1_45_0/doc/html/date_time/…
    – yasouser
    Jan 19, 2011 at 15:55
  • This is where I am having the problem. That is what I have been using, They don not except type of "time_duration", so o_iso_extended_string(time) is ok as "time" is of type "ptime" but time.time_of_day() is of type "time_duration".
    – Tommy
    Jan 19, 2011 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.