我有多种格式的日期。
现在我想在 C++ 中有一个函数(来自某个库),它可以解析这些日期/时间字符串,并给我一些像 tm 这样的结构,或者将它们转换为某种确定性表示,以便我可以处理日期/时间。
我看到的一些格式如下:
2008 年 2 月 19 日星期二 20:47:53 +0530
2009 年 4 月 28 日星期二 18:22:39 -0700(太平洋夏令时间)
我可以做那些没有时区的事情,但对于那些有时区的事情,我基本上需要库将其转换为 tm 结构中的 UTC。
我尝试过 boost 和 strptime,但据我所知,两者都不支持输入时区。我有什么遗漏的吗?
对此的任何帮助将不胜感激。
Regards
您可以使用 boost 来做到这一点,但它对输入字符串中时区的格式有一点特殊。它必须在POSIX 时区格式 http://beta.boost.org/doc/libs/1_44_0/doc/html/date_time/local_time.html#date_time.local_time.posix_time_zone.
例如:
#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。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)