X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/3422227afc9ef4ace183dc57bc1e146efc90ba2f..785f5eaa05195f5e44a974861864324667a3326e:/src/common/datetime.cpp diff --git a/src/common/datetime.cpp b/src/common/datetime.cpp index 19236b62d8..9c14cc43db 100644 --- a/src/common/datetime.cpp +++ b/src/common/datetime.cpp @@ -82,6 +82,23 @@ const long wxDateTime::TIME_T_FACTOR = 1000l; +#if wxUSE_EXTENDED_RTTI + +template<> void wxStringReadValue(const wxString &s , wxDateTime &data ) +{ + data.ParseFormat(s,wxT("%Y-%m-%d %H:%M:%S")) ; +} + +template<> void wxStringWriteValue(wxString &s , const wxDateTime &data ) +{ + s = data.Format(wxT("%Y-%m-%d %H:%M:%S")) ; +} + +wxCUSTOM_TYPE_INFO(wxDateTime, wxToStringConverter , wxFromStringConverter) + +#endif + +// // ---------------------------------------------------------------------------- // conditional compilation // ---------------------------------------------------------------------------- @@ -822,14 +839,29 @@ void wxDateTime::GetAmPmStrings(wxString *am, wxString *pm) { tm tm; InitTm(tm); + wxChar buffer[64]; + // @Note: Do not call 'CallStrftime' here! CallStrftime checks the return code + // and causes an assertion failed if the buffer is to small (which is good) - OR - + // if strftime does not return anything because the format string is invalid - OR - + // if there are no 'am' / 'pm' tokens defined for the current locale (which is not good). + // wxDateTime::ParseTime will try several different formats to parse the time. + // As a result, GetAmPmStrings might get called, even if the current locale + // does not define any 'am' / 'pm' tokens. In this case, wxStrftime would + // assert, even though it is a perfectly legal use. if ( am ) { - *am = CallStrftime(_T("%p"), &tm); + if (wxStrftime(buffer, sizeof buffer, _T("%p"), &tm) > 0) + *am = wxString(buffer); + else + *am = wxString(); } if ( pm ) { tm.tm_hour = 13; - *pm = CallStrftime(_T("%p"), &tm); + if (wxStrftime(buffer, sizeof buffer, _T("%p"), &tm) > 0) + *pm = wxString(buffer); + else + *pm = wxString(); } } @@ -1294,6 +1326,7 @@ wxDateTime& wxDateTime::ResetTime() wxDateTime& wxDateTime::SetFromDOS(unsigned long ddt) { struct tm tm; + InitTm(tm); long year = ddt & 0xFE000000; year >>= 25; @@ -1854,7 +1887,7 @@ wxDateTime& wxDateTime::SetToYearDay(wxDateTime::wxDateTime_t yday) // for Dec, we can't compare with gs_cumulatedDays[mon + 1], but we // don't need it neither - because of the CHECK above we know that // yday lies in December then - if ( (mon == Dec) || (yday < gs_cumulatedDays[isLeap][mon + 1]) ) + if ( (mon == Dec) || (yday <= gs_cumulatedDays[isLeap][mon + 1]) ) { Set(yday - gs_cumulatedDays[isLeap][mon], mon, year); @@ -2848,6 +2881,8 @@ const wxChar *wxDateTime::ParseFormat(const wxChar *date, wxString am, pm, token = GetAlphaToken(input); GetAmPmStrings(&am, &pm); + if (am.IsEmpty() && pm.IsEmpty()) + return (wxChar *)NULL; // no am/pm strings defined if ( token.CmpNoCase(pm) == 0 ) { isPM = TRUE; @@ -3200,11 +3235,48 @@ const wxChar *wxDateTime::ParseDateTime(const wxChar *date) { wxCHECK_MSG( date, (wxChar *)NULL, _T("NULL pointer in wxDateTime::Parse") ); - // there is a public domain version of getdate.y, but it only works for - // English... - wxFAIL_MSG(_T("TODO")); + // Set to current day and hour, so strings like '14:00' becomes today at + // 14, not some other random date + wxDateTime dtDate = wxDateTime::Today(); + wxDateTime dtTime = wxDateTime::Today(); + + const wxChar* pchTime; + + // Try to parse the beginning of the string as a date + const wxChar* pchDate = dtDate.ParseDate(date); + + // We got a date in the beginning, see if there is a time specified after the date + if ( pchDate ) + { + // Skip spaces, as the ParseTime() function fails on spaces + while ( wxIsspace(*pchDate) ) + pchDate++; - return (wxChar *)NULL; + pchTime = dtTime.ParseTime(pchDate); + } + else // no date in the beginning + { + // check and see if we have a time followed by a date + pchTime = dtTime.ParseTime(date); + if ( pchTime ) + { + while ( wxIsspace(*pchTime) ) + pchTime++; + + pchDate = dtDate.ParseDate(pchTime); + } + } + + // If we have a date specified, set our own data to the same date + if ( !pchDate || !pchTime ) + return NULL; + + Set(dtDate.GetDay(), dtDate.GetMonth(), dtDate.GetYear(), + dtTime.GetHour(), dtTime.GetMinute(), dtTime.GetSecond(), + dtTime.GetMillisecond()); + + // Return endpoint of scan + return pchDate > pchTime ? pchDate : pchTime; } const wxChar *wxDateTime::ParseDate(const wxChar *date) @@ -3295,18 +3367,27 @@ const wxChar *wxDateTime::ParseDate(const wxChar *date) } else // not the month { - wxDateTime_t maxDays = haveMon - ? GetNumOfDaysInMonth(haveYear ? year : Inv_Year, mon) - : 31; - - // can it be day? - if ( (val == 0) || (val > (unsigned long)maxDays) ) // cast to shut up compiler warning in BCC + if ( haveDay ) { + // this can only be the year isYear = TRUE; } - else + else // may be either day or year { - isDay = TRUE; + wxDateTime_t maxDays = haveMon + ? GetNumOfDaysInMonth(haveYear ? year : Inv_Year, mon) + : 31; + + // can it be day? + if ( (val == 0) || (val > (unsigned long)maxDays) ) + { + // no + isYear = TRUE; + } + else // yes, suppose it's the day + { + isDay = TRUE; + } } } @@ -3469,8 +3550,7 @@ const wxChar *wxDateTime::ParseDate(const wxChar *date) mon = (wxDateTime::Month)(day - 1); // we're in the current year then - if ( (year > 0) && - (unsigned)year <= GetNumOfDaysInMonth(Inv_Year, mon) ) + if ( (year > 0) && (year <= (int)GetNumOfDaysInMonth(Inv_Year, mon)) ) { day = year;