Possible bugs in date_time package

I'm trying to use the date_time/ptime module to parse strings. Platform: MacOS X 10.4.7 on Intel Compiler: gcc 4.0.1 I have three potential bugs, so far. Perhaps I'm unreasonable in hoping that the following strings should be acceptable to time_from_string. 1. time_from_string( "2006-12-06" ) != time_from_string( "2006-12-06 00:00:00" ) 2. time_from_string( "2006/12/06" ) throws an exception (probably related to 1) even though time_from_string( "2006,12,06" ) doesn't throw. 3. time_from_string( "2006-12-06 " ) throws an exception because it passes a null string to str_from_delimited_time_duration(), which tries to read the first character of the string, even though there isn't one. I think 1 & 2 are probably caused by parse_delimited_time() doing a split on ' ' and getting nothing to pass to parse_delimited_time_duration(). -- Gregory J. Sharp email: gregor@mail.lepp.cornell.edu 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

Gregory J. Sharp wrote:
I'm trying to use the date_time/ptime module to parse strings.
Platform: MacOS X 10.4.7 on Intel Compiler: gcc 4.0.1
I have three potential bugs, so far. Perhaps I'm unreasonable in hoping that the following strings should be acceptable to time_from_string.
1. time_from_string( "2006-12-06" ) != time_from_string( "2006-12-06 00:00:00" ) 2. time_from_string( "2006/12/06" ) throws an exception (probably related to 1) even though time_from_string( "2006,12,06" ) doesn't throw. 3. time_from_string( "2006-12-06 " ) throws an exception because it passes a null string to str_from_delimited_time_duration(), which tries to read the first character of the string, even though there isn't one.
I think 1 & 2 are probably caused by parse_delimited_time() doing a split on ' ' and getting nothing to pass to parse_delimited_time_duration().
The main problem is that the time_from_string function is expecting a complete time (eg: 2006-12-26 00:00:00), not just a date. I believe all of these will parse correctly if you do this: using namespace boost::gregorian; date d = from_string("2006-12-06"); which can trivially be turned into a ptime if that's what you need. Jeff

On 27Feb2007, at 10:58 , Jeff Garland wrote:
The main problem is that the time_from_string function is expecting a complete time (eg: 2006-12-26 00:00:00), not just a date. I believe all of these will parse correctly if you do this:
using namespace boost::gregorian; date d = from_string("2006-12-06");
which can trivially be turned into a ptime if that's what you need.
Thanks. I didn't see the requirement that it have a time component in the manual. Perhaps it is worth adding a note about that? The fact that it silently accepts "2006-12-06" but does the wrong thing(TM) with it, is also a worry. I will try to make a time_from_Gstring() function that parses more aggressively, since I also prefer to avoid things like "2006-05-06 123" being a valid time, in my application. -- Gregory J. Sharp email: gregor@mail.lepp.cornell.edu 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

Gregory J. Sharp wrote:
On 27Feb2007, at 10:58 , Jeff Garland wrote:
The main problem is that the time_from_string function is expecting a complete time (eg: 2006-12-26 00:00:00), not just a date. I believe all of these will parse correctly if you do this:
using namespace boost::gregorian; date d = from_string("2006-12-06");
which can trivially be turned into a ptime if that's what you need.
Thanks. I didn't see the requirement that it have a time component in the manual. Perhaps it is worth adding a note about that?
The fact that it silently accepts "2006-12-06" but does the wrong thing(TM) with it, is also a worry.
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?
I will try to make a time_from_Gstring() function that parses more aggressively, since I also prefer to avoid things like "2006-05-06 123" being a valid time, in my application.
You might have a look at the iostream, format facet-based parsing approaches. This is the more flexible and complete way to do date-time i/o http://www.boost.org/doc/html/date_time/date_time_io.html I pretty much never use the from_string stuff anymore. Jeff

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@mail.lepp.cornell.edu 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
participants (2)
-
Gregory J. Sharp
-
Jeff Garland