+ wxString fmtWX;
+ fmtWX.reserve(fmt.length());
+
+ char chLast = '\0';
+ size_t lastCount = 0;
+
+ const char* formatchars =
+ "dghHmMsSy"
+#ifdef __WXMSW__
+ "t"
+#else
+ "EawD"
+#endif
+ ;
+ for ( wxString::const_iterator p = fmt.begin(); /* end handled inside */; ++p )
+ {
+ if ( p != fmt.end() )
+ {
+ if ( *p == chLast )
+ {
+ lastCount++;
+ continue;
+ }
+
+ const wxUniChar ch = (*p).GetValue();
+ if ( ch.IsAscii() && strchr(formatchars, ch) )
+ {
+ // these characters come in groups, start counting them
+ chLast = ch;
+ lastCount = 1;
+ continue;
+ }
+ }
+
+ // interpret any special characters we collected so far
+ if ( lastCount )
+ {
+ switch ( chLast )
+ {
+ case 'd':
+ switch ( lastCount )
+ {
+ case 1: // d
+ case 2: // dd
+ // these two are the same as we don't distinguish
+ // between 1 and 2 digits for days
+ fmtWX += "%d";
+ break;
+#ifdef __WXMSW__
+ case 3: // ddd
+ fmtWX += "%a";
+ break;
+
+ case 4: // dddd
+ fmtWX += "%A";
+ break;
+#endif
+ default:
+ wxFAIL_MSG( "too many 'd's" );
+ }
+ break;
+#ifndef __WXMSW__
+ case 'D':
+ switch ( lastCount )
+ {
+ case 1: // D
+ case 2: // DD
+ case 3: // DDD
+ fmtWX += "%j";
+ break;
+
+ default:
+ wxFAIL_MSG( "wrong number of 'D's" );
+ }
+ break;
+ case 'w':
+ switch ( lastCount )
+ {
+ case 1: // w
+ case 2: // ww
+ fmtWX += "%W";
+ break;
+
+ default:
+ wxFAIL_MSG( "wrong number of 'w's" );
+ }
+ break;
+ case 'E':
+ switch ( lastCount )
+ {
+ case 1: // E
+ case 2: // EE
+ case 3: // EEE
+ fmtWX += "%a";
+ break;
+ case 4: // EEEE
+ fmtWX += "%A";
+ break;
+ case 5: // EEEEE
+ fmtWX += "%a";
+ break;
+
+ default:
+ wxFAIL_MSG( "wrong number of 'E's" );
+ }
+ break;
+#endif
+ case 'M':
+ switch ( lastCount )
+ {
+ case 1: // M
+ case 2: // MM
+ // as for 'd' and 'dd' above
+ fmtWX += "%m";
+ break;
+
+ case 3:
+ fmtWX += "%b";
+ break;
+
+ case 4:
+ fmtWX += "%B";
+ break;
+
+ default:
+ wxFAIL_MSG( "too many 'M's" );
+ }
+ break;
+
+ case 'y':
+ switch ( lastCount )
+ {
+ case 1: // y
+ case 2: // yy
+ fmtWX += "%y";
+ break;
+
+ case 4: // yyyy
+ fmtWX += "%Y";
+ break;
+
+ default:
+ wxFAIL_MSG( "wrong number of 'y's" );
+ }
+ break;
+
+ case 'H':
+ switch ( lastCount )
+ {
+ case 1: // H
+ case 2: // HH
+ fmtWX += "%H";
+ break;
+
+ default:
+ wxFAIL_MSG( "wrong number of 'H's" );
+ }
+ break;
+
+ case 'h':
+ switch ( lastCount )
+ {
+ case 1: // h
+ case 2: // hh
+ fmtWX += "%I";
+ break;
+
+ default:
+ wxFAIL_MSG( "wrong number of 'h's" );
+ }
+ break;
+
+ case 'm':
+ switch ( lastCount )
+ {
+ case 1: // m
+ case 2: // mm
+ fmtWX += "%M";
+ break;
+
+ default:
+ wxFAIL_MSG( "wrong number of 'm's" );
+ }
+ break;
+
+ case 's':
+ switch ( lastCount )
+ {
+ case 1: // s
+ case 2: // ss
+ fmtWX += "%S";
+ break;
+
+ default:
+ wxFAIL_MSG( "wrong number of 's's" );
+ }
+ break;
+
+ case 'g':
+ // strftime() doesn't have era string,
+ // ignore this format
+ wxASSERT_MSG( lastCount <= 2, "too many 'g's" );
+
+ break;
+#ifndef __WXMSW__
+ case 'a':
+ fmtWX += "%p";
+ break;
+#endif
+#ifdef __WXMSW__
+ case 't':
+ switch ( lastCount )
+ {
+ case 1: // t
+ case 2: // tt
+ fmtWX += "%p";
+ break;
+
+ default:
+ wxFAIL_MSG( "too many 't's" );
+ }
+ break;
+#endif
+ default:
+ wxFAIL_MSG( "unreachable" );
+ }
+
+ chLast = '\0';
+ lastCount = 0;
+ }
+
+ if ( p == fmt.end() )
+ break;
+
+ // not a special character so must be just a separator, treat as is
+ if ( *p == wxT('%') )
+ {
+ // this one needs to be escaped
+ fmtWX += wxT('%');
+ }
+
+ fmtWX += *p;
+ }
+
+ return fmtWX;
+}
+
+} // anonymous namespace
+
+#endif // __WXMSW__ || __WXOSX__
+
+#if defined(__WXMSW__)
+
+namespace
+{
+
+LCTYPE GetLCTYPEFormatFromLocalInfo(wxLocaleInfo index)
+{
+ switch ( index )
+ {
+ case wxLOCALE_SHORT_DATE_FMT:
+ return LOCALE_SSHORTDATE;
+
+ case wxLOCALE_LONG_DATE_FMT:
+ return LOCALE_SLONGDATE;
+
+ case wxLOCALE_TIME_FMT:
+ return LOCALE_STIMEFORMAT;
+
+ default:
+ wxFAIL_MSG( "no matching LCTYPE" );
+ }
+
+ return 0;
+}
+
+} // anonymous namespace
+
+/* static */
+wxString wxLocale::GetInfo(wxLocaleInfo index, wxLocaleCategory cat)
+{
+ wxUint32 lcid = LOCALE_USER_DEFAULT;
+ if ( wxGetLocale() )
+ {
+ const wxLanguageInfo * const
+ info = GetLanguageInfo(wxGetLocale()->GetLanguage());
+ if ( info )
+ lcid = info->GetLCID();
+ }
+
+ wxString str;
+
+ wxChar buf[256];
+ buf[0] = wxT('\0');
+
+ switch ( index )
+ {
+ case wxLOCALE_THOUSANDS_SEP:
+ if ( ::GetLocaleInfo(lcid, LOCALE_STHOUSAND, buf, WXSIZEOF(buf)) )
+ str = buf;
+ break;
+
+ case wxLOCALE_DECIMAL_POINT:
+ if ( ::GetLocaleInfo(lcid,
+ cat == wxLOCALE_CAT_MONEY
+ ? LOCALE_SMONDECIMALSEP
+ : LOCALE_SDECIMAL,
+ buf,
+ WXSIZEOF(buf)) )
+ {
+ str = buf;
+
+ // As we get our decimal point separator from Win32 and not the
+ // CRT there is a possibility of mismatch between them and this
+ // can easily happen if the user code called setlocale()
+ // instead of using wxLocale to change the locale. And this can
+ // result in very strange bugs elsewhere in the code as the
+ // assumptions that formatted strings do use the decimal
+ // separator actually fail, so check for it here.
+ wxASSERT_MSG
+ (
+ wxString::Format("%.3f", 1.23).find(str) != wxString::npos,
+ "Decimal separator mismatch -- did you use setlocale()?"
+ "If so, use wxLocale to change the locale instead."
+ );
+ }
+ break;
+
+ case wxLOCALE_SHORT_DATE_FMT:
+ case wxLOCALE_LONG_DATE_FMT:
+ case wxLOCALE_TIME_FMT:
+ if ( ::GetLocaleInfo(lcid, GetLCTYPEFormatFromLocalInfo(index),
+ buf, WXSIZEOF(buf)) )
+ {
+ return TranslateFromUnicodeFormat(buf);
+ }
+ break;
+
+ case wxLOCALE_DATE_TIME_FMT:
+ // there doesn't seem to be any specific setting for this, so just
+ // combine date and time ones
+ //
+ // we use the short date because this is what "%c" uses by default
+ // ("%#c" uses long date but we have no way to specify the
+ // alternate representation here)
+ {
+ const wxString datefmt = GetInfo(wxLOCALE_SHORT_DATE_FMT);
+ if ( datefmt.empty() )
+ break;
+
+ const wxString timefmt = GetInfo(wxLOCALE_TIME_FMT);
+ if ( timefmt.empty() )
+ break;
+
+ str << datefmt << ' ' << timefmt;
+ }
+ break;
+
+ default:
+ wxFAIL_MSG( "unknown wxLocaleInfo" );
+ }
+
+ return str;
+}
+
+#elif defined(__WXOSX__)
+
+/* static */
+wxString wxLocale::GetInfo(wxLocaleInfo index, wxLocaleCategory WXUNUSED(cat))
+{
+ CFLocaleRef userLocaleRefRaw;
+ if ( wxGetLocale() )
+ {
+ userLocaleRefRaw = CFLocaleCreate
+ (
+ kCFAllocatorDefault,
+ wxCFStringRef(wxGetLocale()->GetCanonicalName())
+ );
+ }
+ else // no current locale, use the default one
+ {
+ userLocaleRefRaw = CFLocaleCopyCurrent();
+ }
+
+ wxCFRef<CFLocaleRef> userLocaleRef(userLocaleRefRaw);
+
+ CFStringRef cfstr = 0;
+ switch ( index )
+ {
+ case wxLOCALE_THOUSANDS_SEP:
+ cfstr = (CFStringRef) CFLocaleGetValue(userLocaleRef, kCFLocaleGroupingSeparator);
+ break;
+
+ case wxLOCALE_DECIMAL_POINT:
+ cfstr = (CFStringRef) CFLocaleGetValue(userLocaleRef, kCFLocaleDecimalSeparator);
+ break;
+
+ case wxLOCALE_SHORT_DATE_FMT:
+ case wxLOCALE_LONG_DATE_FMT:
+ case wxLOCALE_DATE_TIME_FMT:
+ case wxLOCALE_TIME_FMT:
+ {
+ CFDateFormatterStyle dateStyle = kCFDateFormatterNoStyle;
+ CFDateFormatterStyle timeStyle = kCFDateFormatterNoStyle;
+ switch (index )
+ {
+ case wxLOCALE_SHORT_DATE_FMT:
+ dateStyle = kCFDateFormatterShortStyle;
+ break;
+ case wxLOCALE_LONG_DATE_FMT:
+ dateStyle = kCFDateFormatterFullStyle;
+ break;
+ case wxLOCALE_DATE_TIME_FMT:
+ dateStyle = kCFDateFormatterFullStyle;
+ timeStyle = kCFDateFormatterMediumStyle;
+ break;
+ case wxLOCALE_TIME_FMT:
+ timeStyle = kCFDateFormatterMediumStyle;
+ break;
+ default:
+ wxFAIL_MSG( "unexpected time locale" );
+ return wxString();
+ }
+ wxCFRef<CFDateFormatterRef> dateFormatter( CFDateFormatterCreate
+ (NULL, userLocaleRef, dateStyle, timeStyle));
+ wxCFStringRef cfs = wxCFRetain( CFDateFormatterGetFormat(dateFormatter ));
+ wxString format = TranslateFromUnicodeFormat(cfs.AsString());
+ // we always want full years
+ format.Replace("%y","%Y");
+ return format;
+ }
+ break;
+
+ default:
+ wxFAIL_MSG( "Unknown locale info" );
+ return wxString();
+ }
+
+ wxCFStringRef str(wxCFRetain(cfstr));
+ return str.AsString();