Fix ParseDateTime() to also accept time + date, in that order

The intent of the implementation clearly is to allow parsing time first,
date second. But this failed, because a time such as "14:30:15" would
successfully parse as a date (as 14th of current month, current year).

Consequently an attempt is made to parse the actual date as time, which
fails, and therefore the whole ParseDateTime() fails.

Adding a failing test case for ensuring times cannot be parsed as dates
does not cause a failure, because partially yet successfully parsed inputs
get silently ignored (in both ParseDate and ParseDateTime tests). Fixing
both of these, too.

Closes #22203.
This commit is contained in:
Lauri Nurmi 2022-03-17 21:03:26 +02:00 committed by Vadim Zeitlin
parent 0cb0db015f
commit 504c0b16c3
2 changed files with 17 additions and 3 deletions

View file

@ -1220,6 +1220,7 @@ void DateTimeTestCase::TestDateParse()
{ "31/04/06" },
{ "bloordyblop" },
{ "2 . . " },
{ "14:30:15" },
};
wxGCC_WARNING_RESTORE(missing-field-initializers)
@ -1234,7 +1235,7 @@ void DateTimeTestCase::TestDateParse()
const wxString datestr = TranslateDate(parseTestDates[n].str);
const char * const end = dt.ParseDate(datestr);
if ( end && !*end )
if ( end )
{
WX_ASSERT_MESSAGE(
("Erroneously parsed \"%s\"", datestr),
@ -1358,6 +1359,13 @@ void DateTimeTestCase::TestDateTimeParse()
true
},
{
// date after time
"14:30:00 2020-01-04",
{ 4, wxDateTime::Jan, 2020, 14, 30, 0 },
true,
},
{
"bloordyblop",
{ 1, wxDateTime::Jan, 9999, 0, 0, 0},
@ -1367,7 +1375,8 @@ void DateTimeTestCase::TestDateTimeParse()
{
"2012-01-01 10:12:05 +0100",
{ 1, wxDateTime::Jan, 2012, 10, 12, 5, -1 },
false // ParseDateTime does know yet +0100
true // ParseDateTime does know yet +0100, but
// ignoring that, parsing still succeeds
},
};
@ -1383,7 +1392,7 @@ void DateTimeTestCase::TestDateTimeParse()
const wxString datestr = TranslateDate(parseTestDates[n].str);
const char * const end = dt.ParseDateTime(datestr);
if ( end && !*end )
if ( end )
{
WX_ASSERT_MESSAGE(
("Erroneously parsed \"%s\"", datestr),