another bug in ParseDate fixed
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_2_BRANCH@7924 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -3044,6 +3044,11 @@ const wxChar *wxDateTime::ParseDate(const wxChar *date)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We try to guess what we have here: for each new (numeric) token, we
|
||||||
|
// determine if it can be a month, day or a year. Of course, there is an
|
||||||
|
// ambiguity as some numbers may be days as well as months, so we also
|
||||||
|
// have the ability to back track.
|
||||||
|
|
||||||
// what do we have?
|
// what do we have?
|
||||||
bool haveDay = FALSE, // the months day?
|
bool haveDay = FALSE, // the months day?
|
||||||
haveWDay = FALSE, // the day of week?
|
haveWDay = FALSE, // the day of week?
|
||||||
@@ -3063,6 +3068,8 @@ const wxChar *wxDateTime::ParseDate(const wxChar *date)
|
|||||||
while ( tok.HasMoreTokens() )
|
while ( tok.HasMoreTokens() )
|
||||||
{
|
{
|
||||||
wxString token = tok.GetNextToken();
|
wxString token = tok.GetNextToken();
|
||||||
|
if ( !token )
|
||||||
|
continue;
|
||||||
|
|
||||||
// is it a number?
|
// is it a number?
|
||||||
unsigned long val;
|
unsigned long val;
|
||||||
@@ -3072,111 +3079,54 @@ const wxChar *wxDateTime::ParseDate(const wxChar *date)
|
|||||||
|
|
||||||
bool isDay = FALSE,
|
bool isDay = FALSE,
|
||||||
isMonth = FALSE,
|
isMonth = FALSE,
|
||||||
// only years are counted from 0
|
isYear = FALSE;
|
||||||
isYear = (val == 0) || (val > 31);
|
|
||||||
if ( !isYear )
|
if ( !haveMon && val > 0 && val <= 12 )
|
||||||
{
|
{
|
||||||
// may be the month or month day or the year, assume the
|
// assume it is month
|
||||||
// month day by default and fallback to month if the range
|
isMonth = TRUE;
|
||||||
// allow it or to the year if our assumption doesn't work
|
}
|
||||||
if ( haveDay )
|
else // not the month
|
||||||
|
{
|
||||||
|
wxDateTime_t maxDays = haveMon
|
||||||
|
? GetNumOfDaysInMonth(haveYear ? year : Inv_Year, mon)
|
||||||
|
: 31;
|
||||||
|
|
||||||
|
// can it be day?
|
||||||
|
if ( (val == 0) || (val > maxDays) )
|
||||||
{
|
{
|
||||||
// we already have the day, so may only be a month or year
|
isYear = TRUE;
|
||||||
if ( !haveYear && (val > 12) )
|
|
||||||
{
|
|
||||||
isYear = TRUE;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
isMonth = TRUE;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else // it may be day
|
else
|
||||||
{
|
{
|
||||||
isDay = TRUE;
|
isDay = TRUE;
|
||||||
|
|
||||||
// check the range
|
|
||||||
if ( haveMon )
|
|
||||||
{
|
|
||||||
if ( val > GetNumOfDaysInMonth(haveYear ? year
|
|
||||||
: Inv_Year,
|
|
||||||
mon) )
|
|
||||||
{
|
|
||||||
// oops, it can't be a day finally
|
|
||||||
isDay = FALSE;
|
|
||||||
|
|
||||||
if ( val > 12 )
|
|
||||||
{
|
|
||||||
isYear = TRUE;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
isMonth = TRUE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// remember that we have this and stop the scan if it's the second
|
|
||||||
// time we find this, except for the day logic (see there)
|
|
||||||
if ( isYear )
|
if ( isYear )
|
||||||
{
|
{
|
||||||
if ( haveYear )
|
if ( haveYear )
|
||||||
{
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
haveYear = TRUE;
|
haveYear = TRUE;
|
||||||
|
|
||||||
// no roll over - 99 means 99, not 1999 for us
|
|
||||||
year = (wxDateTime_t)val;
|
year = (wxDateTime_t)val;
|
||||||
}
|
}
|
||||||
else if ( isMonth )
|
else if ( isDay )
|
||||||
{
|
{
|
||||||
if ( haveMon )
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
haveMon = TRUE;
|
|
||||||
|
|
||||||
// month 1 is Jan
|
|
||||||
mon = (wxDateTime::Month)(val - 1);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
wxASSERT_MSG( isDay, _T("logic error") );
|
|
||||||
|
|
||||||
if ( haveDay )
|
if ( haveDay )
|
||||||
{
|
break;
|
||||||
// may be were mistaken when we found it for the first
|
|
||||||
// time? may be it was a month or year instead?
|
|
||||||
//
|
|
||||||
// this ability to "backtrack" allows us to correctly parse
|
|
||||||
// both things like 01/13 and 13/01 - but, of course, we
|
|
||||||
// still can't resolve the ambiguity in 01/02 (it will be
|
|
||||||
// Feb 1 for us, not Jan 2 as americans might expect!)
|
|
||||||
if ( (day <= 12) && !haveMon )
|
|
||||||
{
|
|
||||||
// exchange day and month
|
|
||||||
mon = (wxDateTime::Month)(day - 1);
|
|
||||||
|
|
||||||
haveMon = TRUE;
|
|
||||||
}
|
|
||||||
else if ( !haveYear )
|
|
||||||
{
|
|
||||||
// exchange day and year
|
|
||||||
year = day;
|
|
||||||
|
|
||||||
haveYear = TRUE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
haveDay = TRUE;
|
haveDay = TRUE;
|
||||||
|
|
||||||
day = (wxDateTime_t)val;
|
day = (wxDateTime_t)val;
|
||||||
}
|
}
|
||||||
|
else if ( isMonth )
|
||||||
|
{
|
||||||
|
haveMon = TRUE;
|
||||||
|
|
||||||
|
mon = (Month)(val - 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else // not a number
|
else // not a number
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user