开发者

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 w开发者_如何转开发hich 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();


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜