纪元以来字符串使用升压时间 [英] time since epoch to string using boost

查看:126
本文介绍了纪元以来字符串使用升压时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

auto timeSinceEpoch = boost::chrono::duration_cast<boost::chrono::microseconds>(boost::chrono::steady_clock::now().time_since_epoch()).count();

boost::posix_time::ptime now(boost::gregorian::date(1970, 1, 1), boost::posix_time::microsec(static_cast<std::int64_t>(timeSinceEpoch)));

std::string str = boost::posix_time::to_iso_string(now);

输出: 19700114T232422.133653 这是不正确,我在做什么错

Output : 19700114T232422.133653 which it is incorrect, what am I doing wrong ?

推荐答案

在某些系统中,steady_clock的时代是自启动纳秒。

On some systems, the epoch of steady_clock is nanoseconds since boot.

您将获得更多有用的预期结果与其他设备:

You will get a more useful expected result with other clocks:

<大骨节病> 住在Coliru

#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/chrono.hpp>
#include <string>
#include <iostream>

int main()
{
    boost::posix_time::ptime const time_epoch(boost::gregorian::date(1970, 1, 1));

    auto ms = (boost::posix_time::microsec_clock::local_time() - time_epoch).total_microseconds();
    std::cout << "microseconds: " << ms << "\n";

    boost::posix_time::ptime now = time_epoch + boost::posix_time::microseconds(ms);
    std::cout << boost::posix_time::to_iso_string(now);
}

打印

microseconds: 1415115992000000
20141104T154632

这篇关于纪元以来字符串使用升压时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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