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

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

问题描述

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



我看到的一些格式如下:
星期二,2008年2月19日20:47:53 +0530
星期二, 28 Apr 2009 18:22:39 -0700(PDT)



我能够做到没有时区的人,但对于那些与时区,我基本上需要图书馆将其转换为tm结构中的UTC。



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



对这一点的帮助将非常感谢。



/ p>

解决方案

你可以用boost来做到这一点,但是对输入字符串中时区的格式有一点特别的意义。必须在 POSIX时区格式



例如:

  #include< iostream> 
#include< boost / date_time / local_time / local_time.hpp>
#include int main()
{
std :: string msg =28 Apr 2009 18:22:39 PST-8PDT,M4.1.0,M10.1.0; //或只是PDT-7,但这将是一个新的假时区称为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>> ;
std :: cout<<< t < '\\\
';
//现在你可以调用t.to_tm()来获取你的tm结构
}

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


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.

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)

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.

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.

Regards

解决方案

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.

For example:

#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
}

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天全站免登陆