转换到的int64_t TIME_DURATION [英] Convert int64_t to time_duration

查看:1284
本文介绍了转换到的int64_t TIME_DURATION的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过网络为的boost ::的int64_t 转移的boost ::了posix_time ::分组时间>。据打开的boost ::了posix_time一种方法::分组时间到一个__int64 ,我可以很容易地定义自己的的划时代的,只有从参考时间为64位整数传送 TIME_DURATION 。但如何转换回一个分组时间

I would like to transfer a boost::posix_time::ptime over the network as a boost::int64_t. According to A way to turn boost::posix_time::ptime into an __int64, I can easily define my own epoch and only transfer the time_duration from that reference epoch as a 64 bits integer. But how to convert back to a ptime?

#include <iostream>
#include <cassert>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/gregorian/greg_month.hpp>

using namespace std;

using boost::posix_time::ptime;
using boost::posix_time::time_duration;
using boost::gregorian::date;

int main(int argc, char ** argv){
    ptime t = boost::posix_time::microsec_clock::local_time();

    // convert to int64_t
    ptime myEpoch(date(1970,boost::gregorian::Jan,1));
    time_duration myTimeFromEpoch = t - myEpoch;
    boost::int64_t myTimeAsInt = myTimeFromEpoch.ticks();

    // convert back to ptime
    ptime test = myEpoch + time_duration(myTimeAsInt);

    assert(test == t);
    return 0;
}

这是不是因为 TIME_DURATION 构造采取滴答计数的说法是私人的工作。我也有兴趣在任何其他方式传送简单的分组时间过简单的数据类型。

This is not working since the time_duration constructor taking a tick count as argument is private. I am also interested in any other way to simply transfer that ptime over simple data types.

推荐答案

是毫秒级工作液:

int main(int argc, char ** argv){
    ptime t = boost::posix_time::microsec_clock::local_time();

    // convert to int64_t
    ptime myEpoch(date(1970,boost::gregorian::Jan,1));
    time_duration myTimeFromEpoch = t - myEpoch;
    boost::int64_t myTimeAsInt = myTimeFromEpoch.total_milliseconds();

    // convert back to ptime
    ptime test = myEpoch + boost::posix_time::milliseconds(myTimeAsInt);

    cout << test << endl;
    cout << t << endl;

    time_duration diff = test - t;

    assert(diff.total_milliseconds()==0);
    return 0;
}

由于12a6上。

Thanks 12a6.

这篇关于转换到的int64_t TIME_DURATION的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆