升压 - 格式化第二子precision时间和时间戳 [英] Boost - Formatting sub second precision time with a time stamp

查看:133
本文介绍了升压 - 格式化第二子precision时间和时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要得到一个很好的格式化的时间戳(略作修改ISO 8601)用毫秒precision。

I need to get a nicely formatted timestamp (slightly modified ISO 8601) with millisecond precision.

和例子日期应该是这样的: 2011-09-28 13:11:15.237-08:00 结果
格式应当能够被overwridden以及

And example date would look like this: 2011-09-28 13:11:15.237-08:00
The formatting should be able to be overwridden as well.

我一直使用 <$ C玩弄$ C>的boost ::了posix_time :: microsec_clock :: LOCAL_TIME() 和<一个href=\"http://www.boost.org/doc/libs/1_47_0/doc/html/date_time/date_time_io.html#date_time.time_facet\"相对=nofollow> 的boost ::了posix_time :: time_facet 当谈到时间戳除了这工作就好了。结果
由于POSIX时间不包含时区信息这仅仅是不可能的(我猜)

I've been playing around with boost::posix_time::microsec_clock::local_time() and boost::posix_time::time_facet which works just fine except when it comes to timestamps.
Since posix time doesn't contain time zone information this is just not possible (I'm guessing)

那么,还有什么我可以使用具有包含时区信息,或者我可以做毫秒precision的 time_facet 与时区工作?

So, is there anything else I could use that has millisecond precision that contains timezone information or can I make the time_facet work with timezones?

也许我应该总是使用UTC?

Maybe I should always use UTC?

感谢

推荐答案

有一个看的boost :: LOCAL_TIME :: LOCAL_DATE_TIME类。你可以指定一个时区,使用本地微秒的时钟初始化它像

Have a look at the boost::local_time::local_date_time class. You can specify a time zone and use the local microsecond clock to initialize it like

boost::local_time::local_date_time  my_ldt(boost::local_time::local_microsec_clock::local_time(new boost::local_time::posix_time_zone("EST-5EDT,M4.1.0,M10.5.0")));

您应该然后就可以使用方面来格式化为一个字符串。

You should then be able to use the facets to format to a string.

编辑:完整的示例:

#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>

#include <boost/date_time/local_time/local_time.hpp>

int main(void)
{
    boost::posix_time::ptime  now = boost::posix_time::second_clock::local_time();
    boost::posix_time::ptime  utc = boost::posix_time::second_clock::universal_time();

    boost::posix_time::time_duration tz_offset = (now - utc);

    std::stringstream   ss;
    boost::local_time::local_time_facet* output_facet = new boost::local_time::local_time_facet();
    ss.imbue(std::locale(std::locale::classic(), output_facet));

    output_facet->format("%H:%M:%S");
    ss.str("");
    ss << tz_offset;

    boost::local_time::time_zone_ptr    zone(new boost::local_time::posix_time_zone(ss.str().c_str()));
    boost::local_time::local_date_time  ldt = boost::local_time::local_microsec_clock::local_time(zone);
    boost::local_time::local_time_facet* output_facet = new boost::local_time::local_time_facet();
    ss.imbue(std::locale(std::locale::classic(), output_facet));
    output_facet->format("%Y-%m-%d %H:%M:%S%f%Q");
    ss.str("");
    ss << ldt;
    std::cout << ss.str() << std::endl; // "2004-02-29 12:34:56.000789-05:00"

    std::cout << "Press return to exit" << std::endl;
    std::string wait_for_line;
    std::getline(std::cin, wait_for_line);

    return (0);
}

这篇关于升压 - 格式化第二子precision时间和时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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