2010-08-23 58 views
5

我有一堆格式的日期。 现在我想在C++中有一个函数(来自某个库),它可以解析这些日期/时间字符串,并为我提供一些结构,如tm或将它们转换为某种确定性表示形式,以便我可以使用日期/时间。是解析日期/时间字符串的C++库(unix)包括时区

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

我能做没有时区的人,但对于时区的人,我基本上需要库在tm结构中将其转换为UTC。

我已经尝试了提升和strptime,但据我所知,两个都没有输入支持时区。有什么我错过了吗?

任何帮助这一个将非常感激。

问候

+0

使用的语言环境试过吗? http://www.cplusplus.com/reference/std/locale/ – DumbCoder 2010-08-23 12:35:03

回答

2

你可以做到这一点与提升,但它是一个有点讲究的时区输入字符串格式。它必须在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的格式,然后喂字符串提振。