[PATCH] lenient datetime

From: Date: Wed, 03 Nov 2010 17:07:27 +0000
Subject: [PATCH] lenient datetime
Groups: php.internals 
Request: Send a blank email to internals+get-50092@lists.php.net to get a copy of this message
Hi! As I previously noted, in DateTime module there's no way to make DateTime format parser to parse the string if the string contains more data that the format supplies - i.e. if you need 'Y-m-d' and you have string '2010-11-02 12:30' - you can't make DateTime accept it without pre-parsing. The attached patch adds format character '+' that implements that - i.e., if you use format 'Y-m-d+' then the string '2010-11-02 12:30' will be parsed successfully, as well as '2010-11-02' and string with anything following the date. The character would make the format 'lenient' when used in any position in the string, doesn't have to be at the end. Any objections? If not, I'll add some unit tests and commit it. -- Stanislav Malyshev, Software Architect SugarCRM: http://www.sugarcrm.com/ (408)454-6900 ext. 227

Index: lib/parse_date.c =================================================================== --- lib/parse_date.c (revision 305040) +++ lib/parse_date.c (working copy) @@ -24824,6 +24824,7 @@ timelib_sll tmp; Scanner in; Scanner *s = ∈ + int allow_extra = 0; memset(&in, 0, sizeof(in)); in.errors = malloc(sizeof(struct timelib_error_container)); @@ -25046,7 +25047,9 @@ case '*': /* random chars until a separator or number ([ \t.,:;/-0123456789]) */ timelib_eat_until_separator((char **) &ptr); break; - + case '+': + allow_extra = 1; + break; default: if (*fptr != *ptr) { add_pbf_error(s, "The format separator does not match", string, begin); @@ -25055,9 +25058,10 @@ } fptr++; } - if (*ptr) { + if (*ptr && !allow_extra) { add_pbf_error(s, "Trailing data", string, ptr); } + while(*fptr == '+') fptr++; /* ignore trailing +'s */ if (*fptr) { add_pbf_error(s, "Data missing", string, ptr); }
« previous php.internals (#50092) next »