Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 年份超出有效范围:1400…10000_C++_Parsing_Boost_Datetime - Fatal编程技术网

C++ 年份超出有效范围:1400…10000

C++ 年份超出有效范围:1400…10000,c++,parsing,boost,datetime,C++,Parsing,Boost,Datetime,我试图使用boost::date\u time将日期字符串(从twitterapi获得)解析为ptime对象。日期格式的一个示例是: Thu Mar 24 16:12:42 +0000 2011 不管我做什么,在试图解析字符串时都会出现“Year is out of valid range”(年份超出有效范围)异常。日期格式在我看来是正确的,下面是代码: boost::posix_time::ptime created_time; std::stringstream ss(created_st

我试图使用boost::date\u time将日期字符串(从twitterapi获得)解析为ptime对象。日期格式的一个示例是:

Thu Mar 24 16:12:42 +0000 2011
不管我做什么,在试图解析字符串时都会出现“Year is out of valid range”(年份超出有效范围)异常。日期格式在我看来是正确的,下面是代码:

boost::posix_time::ptime created_time;
std::stringstream ss(created_string);
ss.exceptions(std::ios_base::failbit); //Turn on exceptions
ss.imbue(std::locale(ss.getloc(), new boost::posix_time::time_input_facet("%a %b %d %T %q %Y")));
ss >> created_time;
在上述代码中,“created_string”包含上述日期。我是否在格式字符串中犯了错误?

根据,
%T
现在不可用于输入,因为它后面跟一个
在图表中。我现在无法测试,但我怀疑这是你的问题

编辑:


%q
也是一个仅输出的标志,正如下面的评论所指出的。

两个
%T
%q
都是在线输出格式标志

为了演示这一点,请将格式更改为
“%a%b%d%H:%M:%S+0000%Y”
,然后程序将按说明运行

至于时区输入,它有点复杂,您可能需要对字符串进行预处理以将+0000更改为第一个

编辑:例如,您可以这样做:

#include <iostream>
#include <sstream>
#include <boost/date_time.hpp>
int main()
{
        //std::string created_string = "Thu Mar 24 16:12:42 +0000 2011";
        // write your own function to search and replace +0000 with GMT+00:00
        std::string created_string = "Thu Mar 24 16:12:42 GMT+00:00 2011";

        boost::local_time::local_date_time created_time(boost::local_time::not_a_date_time);
        std::stringstream ss(created_string);
        ss.exceptions(std::ios_base::failbit);
        ss.imbue(std::locale(ss.getloc(),
                 new boost::local_time::local_time_input_facet("%a %b %d %H:%M:%S %ZP %Y")));
        ss >> created_time;
        std::cout << created_time << '\n';
}
#包括
#包括
#包括
int main()
{
//std::string created_string=“Thu Mar 24 16:12:42+0000 2011”;
//编写自己的函数来搜索+0000并将其替换为GMT+00:00
std::string created_string=“Thu Mar 24 16:12:42 GMT+00:00 2011”;
boost::local_time::local_date_time created_time(boost::local_time::not_date_time);
std::stringstream ss(创建的字符串);
ss.异常(std::ios_base::failbit);
ss.imbue(std::locale(ss.getloc(),
新的boost::local_time::local_time_input_facet(“%a%b%d%H:%M:%S%ZP%Y”);
ss>>创建时间;

std::有一件事可以肯定,你在函数调用中使用
new
造成了内存泄漏。你对Java或C有更丰富的经验吗?是的,别担心,我知道这一点,我只是在重新安排代码以使其正常工作,我会在得到日期解析时修复:)编辑:事实上,我不认为这会导致错误ak…除非将1作为第二个可选构造函数arg传递,否则time\u input\u facet是refcounted。是的,我刚刚检查过,在堆栈上传递指向time\u input\u facet的指针会在区域设置被破坏时导致崩溃(除非将1传递给禁用refcounting的构造函数)。因此上述代码不会泄漏(尽管看起来应该是:)@Kazade,
%q
也被声明为“仅输出”在描述中。我怀疑这才是真正的问题。是的,我也只是想对
%q
发表评论。它没有很好的
来让它立即变得明显:)谢谢,通常我会分别尝试%H:%M:%s和+0000,但不是同时尝试!谢谢!