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

AMM*_*AMM 5 c++ datetime

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

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

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

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

对此的任何帮助将非常感谢.

问候

Cub*_*bbi 2

您可以使用 boost 来做到这一点,但它对输入字符串中时区的格式有一点特殊。它必须采用POSIX 时区格式

例如:

#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
}
Run Code Online (Sandbox Code Playgroud)

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