X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/694cb6c5819c59bc104619d3dc3b6c0f18e80ea9..dbe0872fc80f7fc9ac37235ab1f8ba8224b0bfa5:/src/common/datetimefmt.cpp diff --git a/src/common/datetimefmt.cpp b/src/common/datetimefmt.cpp index 91d6452981..e6544edd23 100644 --- a/src/common/datetimefmt.cpp +++ b/src/common/datetimefmt.cpp @@ -46,7 +46,6 @@ #endif // WX_PRECOMP #include "wx/thread.h" -#include "wx/tokenzr.h" #include @@ -92,6 +91,42 @@ static const int MIN_PER_HOUR = 60; namespace { +// all the functions below taking non-const wxString::const_iterator p advance +// it until the end of the match + +// scans all digits (but no more than len) and returns the resulting number +bool GetNumericToken(size_t len, + wxString::const_iterator& p, + const wxString::const_iterator& end, + unsigned long *number) +{ + size_t n = 1; + wxString s; + while ( p != end && wxIsdigit(*p) ) + { + s += *p++; + + if ( len && ++n > len ) + break; + } + + return !s.empty() && s.ToULong(number); +} + +// scans all alphabetic characters and returns the resulting string +wxString +GetAlphaToken(wxString::const_iterator& p, + const wxString::const_iterator& end) +{ + wxString s; + while ( p != end && wxIsalpha(*p) ) + { + s += *p++; + } + + return s; +} + enum { DateLang_English = 1, @@ -105,8 +140,16 @@ enum // month name or DateLang_English to parse it as a standard English name or // their combination to interpret it in any way wxDateTime::Month -GetMonthFromName(const wxString& name, int flags, int lang) +GetMonthFromName(wxString::const_iterator& p, + const wxString::const_iterator& end, + int flags, + int lang) { + const wxString::const_iterator pOrig = p; + const wxString name = GetAlphaToken(p, end); + if ( name.empty() ) + return wxDateTime::Inv_Month; + wxDateTime::Month mon; for ( mon = wxDateTime::Jan; mon < wxDateTime::Inv_Month; wxNextMonth(mon) ) { @@ -140,13 +183,35 @@ GetMonthFromName(const wxString& name, int flags, int lang) if ( lang & DateLang_Local ) { - if ( name.CmpNoCase(wxDateTime::GetMonthName(mon, - wxDateTime::Name_Abbr)) == 0 ) + // some locales (e.g. French one) use periods for the + // abbreviated month names but it's never part of name so + // compare it specially + wxString nameAbbr = wxDateTime::GetMonthName(mon, + wxDateTime::Name_Abbr); + const bool hasPeriod = *nameAbbr.rbegin() == '.'; + if ( hasPeriod ) + nameAbbr.erase(nameAbbr.end() - 1); + + if ( name.CmpNoCase(nameAbbr) == 0 ) + { + if ( hasPeriod ) + { + // skip trailing period if it was part of the match + if ( *p == '.' ) + ++p; + else // no match as no matching period + continue; + } + break; + } } } } + if ( mon == wxDateTime::Inv_Month ) + p = pOrig; + return mon; } @@ -155,8 +220,15 @@ GetMonthFromName(const wxString& name, int flags, int lang) // flags and lang parameters have the same meaning as for GetMonthFromName() // above wxDateTime::WeekDay -GetWeekDayFromName(const wxString& name, int flags, int lang) +GetWeekDayFromName(wxString::const_iterator& p, + const wxString::const_iterator& end, + int flags, int lang) { + const wxString::const_iterator pOrig = p; + const wxString name = GetAlphaToken(p, end); + if ( name.empty() ) + return wxDateTime::Inv_WeekDay; + wxDateTime::WeekDay wd; for ( wd = wxDateTime::Sun; wd < wxDateTime::Inv_WeekDay; wxNextWDay(wd) ) { @@ -195,40 +267,10 @@ GetWeekDayFromName(const wxString& name, int flags, int lang) } } - return wd; -} - -// scans all digits (but no more than len) and returns the resulting number -bool GetNumericToken(size_t len, - wxString::const_iterator& p, - const wxString::const_iterator& end, - unsigned long *number) -{ - size_t n = 1; - wxString s; - while ( p != end && wxIsdigit(*p) ) - { - s += *p++; - - if ( len && ++n > len ) - break; - } - - return !s.empty() && s.ToULong(number); -} + if ( wd == wxDateTime::Inv_WeekDay ) + p = pOrig; -// scans all alphabetic characters and returns the resulting string -wxString -GetAlphaToken(wxString::const_iterator& p, - const wxString::const_iterator& end) -{ - wxString s; - while ( p != end && wxIsalpha(*p) ) - { - s += *p++; - } - - return s; + return wd; } // parses string starting at given iterator using the specified format and, @@ -266,14 +308,20 @@ ParseFormatAt(wxString::const_iterator& p, // wxDateTime to/from text representations // ---------------------------------------------------------------------------- -wxString wxDateTime::Format(const wxString& format, const TimeZone& tz) const +wxString wxDateTime::Format(const wxString& formatp, const TimeZone& tz) const { - wxCHECK_MSG( !format.empty(), wxEmptyString, + wxCHECK_MSG( !formatp.empty(), wxEmptyString, _T("NULL format in wxDateTime::Format") ); + wxString format = formatp; +#ifdef __WXOSX__ + format.Replace("%c",wxLocale::GetInfo(wxLOCALE_DATE_TIME_FMT)); + format.Replace("%x",wxLocale::GetInfo(wxLOCALE_SHORT_DATE_FMT)); + format.Replace("%X",wxLocale::GetInfo(wxLOCALE_TIME_FMT)); +#endif // we have to use our own implementation if the date is out of range of // strftime() or if we use non standard specificators -#ifdef HAVE_STRFTIME +#ifdef wxHAS_STRFTIME time_t time = GetTicks(); if ( (time != (time_t)-1) && !wxStrstr(format, _T("%l")) ) @@ -317,7 +365,7 @@ wxString wxDateTime::Format(const wxString& format, const TimeZone& tz) const } } //else: use generic code below -#endif // HAVE_STRFTIME +#endif // wxHAS_STRFTIME // we only parse ANSI C format specifications here, no POSIX 2 // complications, no GNU extensions but we do add support for a "%l" format @@ -394,7 +442,7 @@ wxString wxDateTime::Format(const wxString& format, const TimeZone& tz) const case _T('c'): // locale default date and time representation case _T('x'): // locale default date representation -#ifdef HAVE_STRFTIME +#ifdef wxHAS_STRFTIME // // the problem: there is no way to know what do these format // specifications correspond to for the current locale. @@ -510,11 +558,11 @@ wxString wxDateTime::Format(const wxString& format, const TimeZone& tz) const res += str; } -#else // !HAVE_STRFTIME +#else // !wxHAS_STRFTIME // Use "%m/%d/%y %H:%M:%S" format instead res += wxString::Format(wxT("%02d/%02d/%04d %02d:%02d:%02d"), tm.mon+1,tm.mday, tm.year, tm.hour, tm.min, tm.sec); -#endif // HAVE_STRFTIME/!HAVE_STRFTIME +#endif // wxHAS_STRFTIME/!wxHAS_STRFTIME break; case _T('d'): // day of a month (01-31) @@ -551,11 +599,11 @@ wxString wxDateTime::Format(const wxString& format, const TimeZone& tz) const break; case _T('p'): // AM or PM string -#ifdef HAVE_STRFTIME +#ifdef wxHAS_STRFTIME res += CallStrftime(_T("%p"), &tmTimeOnly); -#else // !HAVE_STRFTIME +#else // !wxHAS_STRFTIME res += (tmTimeOnly.tm_hour > 12) ? wxT("pm") : wxT("am"); -#endif // HAVE_STRFTIME/!HAVE_STRFTIME +#endif // wxHAS_STRFTIME/!wxHAS_STRFTIME break; case _T('S'): // second as a decimal number (00-61) @@ -578,11 +626,11 @@ wxString wxDateTime::Format(const wxString& format, const TimeZone& tz) const case _T('X'): // locale default time representation // just use strftime() to format the time for us -#ifdef HAVE_STRFTIME +#ifdef wxHAS_STRFTIME res += CallStrftime(_T("%X"), &tmTimeOnly); -#else // !HAVE_STRFTIME +#else // !wxHAS_STRFTIME res += wxString::Format(wxT("%02d:%02d:%02d"),tm.hour, tm.min, tm.sec); -#endif // HAVE_STRFTIME/!HAVE_STRFTIME +#endif // wxHAS_STRFTIME/!wxHAS_STRFTIME break; case _T('y'): // year without century (00-99) @@ -594,7 +642,7 @@ wxString wxDateTime::Format(const wxString& format, const TimeZone& tz) const break; case _T('Z'): // timezone name -#ifdef HAVE_STRFTIME +#ifdef wxHAS_STRFTIME res += CallStrftime(_T("%Z"), &tmTimeOnly); #endif break; @@ -650,19 +698,17 @@ wxString wxDateTime::Format(const wxString& format, const TimeZone& tz) const bool wxDateTime::ParseRfc822Date(const wxString& date, wxString::const_iterator *end) { + const wxString::const_iterator pEnd = date.end(); wxString::const_iterator p = date.begin(); // 1. week day - static const int WDAY_LEN = 3; - const wxString::const_iterator endWday = p + WDAY_LEN; - const wxString wday(p, endWday); - if ( GetWeekDayFromName(wday, Name_Abbr, DateLang_English) == Inv_WeekDay ) + const wxDateTime::WeekDay + wd = GetWeekDayFromName(p, pEnd, Name_Abbr, DateLang_English); + if ( wd == Inv_WeekDay ) return false; //else: ignore week day for now, we could also check that it really // corresponds to the specified date - p = endWday; - // 2. separating comma if ( *p++ != ',' || *p++ != ' ' ) return false; @@ -682,15 +728,10 @@ wxDateTime::ParseRfc822Date(const wxString& date, wxString::const_iterator *end) return false; // 4. month name - static const int MONTH_LEN = 3; - const wxString::const_iterator endMonth = p + MONTH_LEN; - const wxString monName(p, endMonth); - Month mon = GetMonthFromName(monName, Name_Abbr, DateLang_English); + const Month mon = GetMonthFromName(p, pEnd, Name_Abbr, DateLang_English); if ( mon == Inv_Month ) return false; - p = endMonth; - if ( *p++ != ' ' ) return false; @@ -770,7 +811,7 @@ wxDateTime::ParseRfc822Date(const wxString& date, wxString::const_iterator *end) return false; // 7. now the interesting part: the timezone - int offset wxDUMMY_INITIALIZE(0); + int offset = 0; // just to suppress warnings if ( *p == '-' || *p == '+' ) { // the explicit offset given: it has the form of hhmm @@ -972,7 +1013,7 @@ wxDateTime::ParseFormat(const wxString& date, { wday = GetWeekDayFromName ( - GetAlphaToken(input, end), + input, end, *fmt == 'a' ? Name_Abbr : Name_Full, DateLang_Local ); @@ -990,7 +1031,7 @@ wxDateTime::ParseFormat(const wxString& date, { mon = GetMonthFromName ( - GetAlphaToken(input, end), + input, end, *fmt == 'b' ? Name_Abbr : Name_Full, DateLang_Local ); @@ -1127,25 +1168,25 @@ wxDateTime::ParseFormat(const wxString& date, case _T('p'): // AM or PM string { - wxString am, pm, token = GetAlphaToken(input, end); + wxString am, pm; + GetAmPmStrings(&am, &pm); - // some locales have empty AM/PM tokens and thus when formatting - // dates with the %p specifier nothing is generated; when trying to - // parse them back, we get an empty token here... but that's not - // an error. - if (token.empty()) - break; + // we can never match %p in locales which don't use AM/PM + if ( am.empty() || pm.empty() ) + return false; - GetAmPmStrings(&am, &pm); - if (am.empty() && pm.empty()) - return false; // no am/pm strings defined - if ( token.CmpNoCase(pm) == 0 ) + const size_t pos = input - date.begin(); + if ( date.compare(pos, pm.length(), pm) == 0 ) { isPM = true; + input += pm.length(); } - else if ( token.CmpNoCase(am) != 0 ) + else if ( date.compare(pos, am.length(), am) == 0 ) + { + input += am.length(); + } + else // no match { - // no match return false; } } @@ -1507,6 +1548,7 @@ wxDateTime::ParseDate(const wxString& date, wxString::const_iterator *end) // all esoteric constructions ParseDateTime() knows about) const wxString::const_iterator pBegin = date.begin(); + const wxString::const_iterator pEnd = date.end(); wxString::const_iterator p = pBegin; while ( wxIsspace(*p) ) @@ -1524,7 +1566,7 @@ wxDateTime::ParseDate(const wxString& date, wxString::const_iterator *end) { wxTRANSLATE("tomorrow"), 1 }, }; - const size_t lenRest = date.end() - p; + const size_t lenRest = pEnd - p; for ( size_t n = 0; n < WXSIZEOF(literalDates); n++ ) { const wxString dateStr = wxGetTranslation(literalDates[n].str); @@ -1564,6 +1606,8 @@ wxDateTime::ParseDate(const wxString& date, wxString::const_iterator *end) haveMon = false, // the month? haveYear = false; // the year? + bool monWasNumeric = false; // was month specified as a number? + // and the value of the items we have (init them to get rid of warnings) WeekDay wday = Inv_WeekDay; wxDateTime_t day = 0; @@ -1571,18 +1615,22 @@ wxDateTime::ParseDate(const wxString& date, wxString::const_iterator *end) int year = 0; // tokenize the string - size_t nPosCur = 0; - static const wxStringCharType *dateDelimiters = wxS(".,/-\t\r\n "); - wxStringTokenizer tok(wxString(p, date.end()), dateDelimiters); - while ( tok.HasMoreTokens() ) + while ( p != pEnd ) { - wxString token = tok.GetNextToken(); - if ( !token ) - continue; + // skip white space and date delimiters + while ( wxStrchr(".,/-\t\r\n ", *p) ) + { + ++p; + } - // is it a number? + // modify copy of the iterator as we're not sure if the next token is + // still part of the date at all + wxString::const_iterator pCopy = p; + + // we can have either alphabetic or numeric token, start by testing if + // it's the latter unsigned long val; - if ( token.ToULong(&val) ) + if ( GetNumericToken(10 /* max length */, pCopy, pEnd, &val) ) { // guess what this number is @@ -1646,6 +1694,7 @@ wxDateTime::ParseDate(const wxString& date, wxString::const_iterator *end) else if ( isMonth ) { haveMon = true; + monWasNumeric = true; mon = (Month)(val - 1); } @@ -1655,7 +1704,7 @@ wxDateTime::ParseDate(const wxString& date, wxString::const_iterator *end) // be careful not to overwrite the current mon value Month mon2 = GetMonthFromName ( - token, + pCopy, pEnd, Name_Full | Name_Abbr, DateLang_Local | DateLang_English ); @@ -1664,20 +1713,18 @@ wxDateTime::ParseDate(const wxString& date, wxString::const_iterator *end) // it's a month if ( haveMon ) { - // but we already have a month - maybe we guessed wrong? - if ( !haveDay ) - { - // no need to check in month range as always < 12, but - // the days are counted from 1 unlike the months - day = (wxDateTime_t)(mon + 1); - haveDay = true; - } - else - { - // could possible be the year (doesn't the year come - // before the month in the japanese format?) (FIXME) + // but we already have a month - maybe we guessed wrong + // when we had interpreted that numeric value as a month + // and it was the day number instead? + if ( haveDay || !monWasNumeric ) break; - } + + // assume we did and change our mind: reinterpret the month + // value as a day (notice that there is no need to check + // that it is valid as month values are always < 12, but + // the days are counted from 1 unlike the months) + day = (wxDateTime_t)(mon + 1); + haveDay = true; } mon = mon2; @@ -1688,7 +1735,7 @@ wxDateTime::ParseDate(const wxString& date, wxString::const_iterator *end) { WeekDay wday2 = GetWeekDayFromName ( - token, + pCopy, pEnd, Name_Full | Name_Abbr, DateLang_Local | DateLang_English ); @@ -1696,9 +1743,7 @@ wxDateTime::ParseDate(const wxString& date, wxString::const_iterator *end) { // a week day if ( haveWDay ) - { break; - } wday = wday2; @@ -1736,8 +1781,11 @@ wxDateTime::ParseDate(const wxString& date, wxString::const_iterator *end) size_t n; for ( n = 0; n < WXSIZEOF(ordinals); n++ ) { - if ( token.CmpNoCase(ordinals[n]) == 0 ) + const wxString ord = wxGetTranslation(ordinals[n]); + const size_t len = ord.length(); + if ( date.compare(p - pBegin, len, ord) == 0 ) { + p += len; break; } } @@ -1764,7 +1812,8 @@ wxDateTime::ParseDate(const wxString& date, wxString::const_iterator *end) } } - nPosCur = tok.GetPosition(); + // advance iterator past a successfully parsed token + p = pCopy; } // either no more tokens or the scan was stopped by something we couldn't @@ -1840,15 +1889,6 @@ wxDateTime::ParseDate(const wxString& date, wxString::const_iterator *end) SetToWeekDayInSameWeek(wday); } - // return the pointer to the first unparsed char - p += nPosCur; - if ( nPosCur && wxStrchr(dateDelimiters, *(p - 1)) ) - { - // if we couldn't parse the token after the delimiter, put back the - // delimiter as well - p--; - } - *end = p; return true;