C ++库(unix)解析日期/时间字符串包括时区 [英] C++ library (unix) to parse date/time string Including timezones

查看:162
本文介绍了C ++库(unix)解析日期/时间字符串包括时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些格式的日期。
现在我想有一个函数(从一些库)在c ++中,可以解析这些日期/时间字符串,并给我一些结构,像一个tm或转换成一些确定性的表示,使我可以玩日期/时间。

I have dates in a bunch of formats. Now i would like to have a function (from some library) in c++ that can parse these date/time string and give me some structure like a tm or convert them to some deterministic representation so that i can play around with the date/time.

我看到的一些格式如下:
Tue,19 Feb 2008 20:47:53 +0530
Tue, 28 Apr 2009 18:22:39 -0700(PDT)

Some formats that I see are as follows : Tue, 19 Feb 2008 20:47:53 +0530 Tue, 28 Apr 2009 18:22:39 -0700 (PDT)

我可以做那些没有时区,但对于那些与时区,我基本上需要库在tm结构中将其转换为UTC。

I am able to do the ones without the timezones but for the ones with the timezone, i basically need the library to convert it to UTC in the tm structure.

我试过boost和strptime,但据我所知,两者都不支持时区的输入。有没有什么我错过了?

I have tried boost and strptime, but as far as i know, both do no support timezones on inputs. is there anything that i have missed?

任何对此的帮助将非常感激。

ANy help on this one will be really appreciated.

/ p>

Regards

推荐答案

您可以通过boost来做到这一点,但它对输入字符串中的时区格式有些特别。它必须在 POSIX时间区域格式

You can do that with boost, but it's a little particular about the format of time zones in the input string. It has to be in POSIX time zone format.

例如:

#include <iostream>
#include <boost/date_time/local_time/local_time.hpp>
#include <boost/date_time/time_facet.hpp>
int main()
{
       std::string msg = "28 Apr 2009 18:22:39 PST-8PDT,M4.1.0,M10.1.0"; // or just "PDT-7" but that will be a new, fake time zone called 'PDT'

       std::istringstream ss(msg);
       ss.imbue( std::locale(ss.getloc(),
                 new boost::local_time::local_time_input_facet("%d %b %Y %H:%M:%S%F %ZP")));
       boost::local_time::local_date_time t(boost::date_time::pos_infin);
       ss >> t;
       std::cout << t << '\n';
       // and now you can call t.to_tm() to get your tm structure
}

我会添加一些预处理来将您的时区格式转换为posix格式,然后提供字符串boost。

I would add some sort of pre-processing to convert your time zone formats to posix format, and then feed the strings to boost.

这篇关于C ++库(unix)解析日期/时间字符串包括时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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