Middy
Middy

Reputation: 91

convert UTC time difference into string boost

I have two UTC timestamps (Time since 1.1.1970)

I want to show the difference between them as the string %H:%M:%S eg. 13:34:12

Currently I have gotten this far

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?

Upvotes: 1

Views: 638

Answers (1)

kinnou02
kinnou02

Reputation: 654

something like this will do the job, you don't need a 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;
}

Upvotes: 3

Related Questions