用于解析日期/时间字符串的C ++库(unix)包括时区

时间:2010-08-23 12:30:23

标签: c++ datetime

我有多种格式的约会。 现在我想在c ++中有一个函数(来自某个库)可以解析这些日期/时间字符串并给我一些像tm这样的结构或将它们转换为某种确定性表示,这样我就可以使用日期/时间。

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

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

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

非常感谢你对这一个的帮助。

此致

2 个答案:

答案 0 :(得分:2)

你可以使用boost来做到这一点,但是输入字符串中的时区格式有点特别。它必须在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格式,然后输入字符串以进行提升。

答案 1 :(得分:1)

如果您有很多格式可以处理,我建议您查看ICU:http://site.icu-project.org/

相关问题