stefanB
stefanB

Reputation: 79790

How to get GMT time in milliseconds using boost Date_Time?

Is there a simple way to get from boost Date_Time library a current GMT time in milliseconds?

Here is one example which uses time_of_day, I don't want time_of_day but total time in GMT as long long int:

boost::posix_time::ptime time =
          boost::posix_time::microsec_clock::universal_time();
boost::posix_time::time_duration duration( time.time_of_day() );  // ???
long long int timeInMilliseconds = duration.total_milliseconds();

Upvotes: 6

Views: 6535

Answers (2)

Mohan Kumar
Mohan Kumar

Reputation: 631

With C++11, you can

//get current millisecond in epoch
auto currentMillis = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();

//get crrent nanosecond in epoch
auto currentNanos = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count();

Upvotes: 0

ildjarn
ildjarn

Reputation: 62975

There's nothing built-in that I can see, but as usual, it's trivial to implement:

boost::posix_time::time_duration::tick_type milliseconds_since_epoch()
{
    using boost::gregorian::date;
    using boost::posix_time::ptime;
    using boost::posix_time::microsec_clock;

    static ptime const epoch(date(1970, 1, 1));
    return (microsec_clock::universal_time() - epoch).total_milliseconds();
}

Upvotes: 8

Related Questions