Boost logo

Boost Users :

From: Jeff Garland (jeff_at_[hidden])
Date: 2005-12-08 09:12:26


Michael Lin wrote:
> Hi all,
>
> I come across a bug in boost::posix_time::time_from_string();
>
> try
> {
> ptime t1(time_from_string("2005-01-01"));
> cout << to_simple_string(t1) << endl;
> }
> catch (...)
> {
> cout << "exception thrown" << endl;
> }
>
> The above code will strangely output: "2005-Mar-25 13:01:01"
>
> time_from_string() will parse "2005-01-01 12", "2005-01-01 12:23" and
> "2005-01-01 12:23:33" correctly.
>
> It will even parse "2005-01-01 34" to the next day and output
> "2005-Jan-02 10:00:00"
>
> Does anyone have a fix for this bug? It should either parse correctly or it
> should throw an exception.
>
> Thanks
> Michael
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
>
>
You could parse the elements independently and check it yourself pretty
trivially.

time_duration td;
date d;
std::stringstream ss("2005-Jan-01 13:20:20");
ss >> d;
ss >> td;
if (td.hours() > 23 || td == not_a_date_time) {....
  //parse failed..

Didn't compile the above, but should be close :-)

from_string treats the second part as a time duration and isn't strict
about limiting the input, so that's why it behaves as it does...

Jeff


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net