Year is out of range: 1400 ... 10000

I am trying to use boost :: date_time to parse a date string (obtained from the Twitter API) into a ptime object. Date format example:

Thu Mar 24 16:12:42 +0000 2011 

No matter what I do, I get a β€œYear out of range” exception when trying to parse a string. The date format looks correct for me, here is the code:

 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; 

The "created_string" code above contains the date above. Did I make a mistake in the format string?

+6
c ++ boost datetime parsing
source share
2 answers

Both %T and %q are flags of the online-output format.

To demonstrate this, change the format to "%a %b %d %H:%M:%S +0000 %Y" and your program will work as described.

As for entering the time zone, this is a bit more complicated, you may need to pre-process the string to change +0000 to the time zone format first.

EDIT: for example, you can do it like this:

 #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'; } 
+4
source share

According to docs , %T not used for input at this time, since it follows after it ! in the chart. I cannot verify this right now, but I suspect that this is your problem.

Edit:

%q is also an output-only flag, as noted in the comments below.

+3
source share

All Articles