Boost logo

Boost Users :

From: Gregory J. Sharp (gregor_at_[hidden])
Date: 2007-02-27 15:53:55


On 27Feb2007, at 15:15 , Jeff Garland wrote

>
> I thought this case threw and exception and "2006,12,06" didn't? And,
> honestly I find it strange -- what was the constructed value? NADT?

On MacOS 10.4.7 the following program prints:

2006-12-30 == 2007-03-23T14:12:30

It should crash, but something magical happens, except when the
separator is /. Scary stuff.

  -- snip --
#include "boost/date_time/posix_time/posix_time.hpp"
#include <iostream>

using namespace boost::posix_time;

main(int argc, char * argv[])
{
     std::string d( "2006-12-30" );
     ptime x( time_from_string( d ) );
     std::cout << d << " == " << to_iso_extended_string( x ) <<
std::endl;
}
  -- snip --

I think you could fix this to be more forgiving (and accept a date
without a time) by changing the split function in boost/date_time/
time_parser.hpp as follows. It would also not crash if there on
inputs like "2007-10-02 " and "2006-11-12 4" (double space).

     int sep_pos = static_cast<int>(s.find(sep));
     first = s.substr(0,sep_pos);
     // Skip leading space on time field (catches double space
between fields)
     sep_pos = s.find_first_not_of(sep, sep_pos+1);
     if ( pos == std::string::npos ) {
        second = "";
     } else {
        // There might be a time field. Strip leading space.
        second = s.substr(sep_pos); // NOTE: removed the +1
     }
     return true;

and then in parse_delimited_time() change it to

     //split date/time on a unique delimiter char such as ' ' or 'T'
     std::string date_string, tod_string;
     split(s, sep, date_string, tod_string);
     //call parse_date with first string
     date_type d = parse_date<date_type>(date_string);
     if ( tod_string.size() == 0 )
     {
         return time_type( d );
     }
     //call parse_time_duration with remaining string
     time_duration td = parse_delimited_time_duration<time_duration>
(tod_string);
     //construct a time
     return time_type(d, td);

--
Gregory J. Sharp                   email: gregor_at_[hidden]
Wilson Synchrotron Laboratory      url: http://www.lepp.cornell.edu/ 
~gregor
Cornell University                 ph:  +1 607 255 4882
Ithaca, NY 14853                   fax: +1 607 255 8062

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