]> git.saurik.com Git - wxWidgets.git/commitdiff
Fix wxLocale::GetInfo() in C locale under Windows.
authorVadim Zeitlin <vadim@wxwidgets.org>
Sat, 29 Dec 2012 21:53:13 +0000 (21:53 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sat, 29 Dec 2012 21:53:13 +0000 (21:53 +0000)
Don't use LOCALE_USER_DEFAULT when the locale hadn't been changed because the
user default locale often (and maybe even always) is different from "C" locale
used by the CRT resulting in mismatch between the conventions used by
wxDateTime::Format(), which uses the CRT locale, and wxDateTime::Parse(),
which uses Windows format. Instead use the hard-coded values corresponding to
the "C" locale to ensure we use the same values as the CRT in this case.

This also reverts r73244 which was applies to make the unit tests pass before
this fix as it's not necessary any longer.

Closes #14918.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@73320 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

src/common/intl.cpp
tests/test.cpp

index a7aaf30597fa935963989ec64c64dc18323a187e..29132e9ad66fc84156f3a3c4b6d88812acb650a2 100644 (file)
@@ -1422,15 +1422,54 @@ LCTYPE GetLCTYPEFormatFromLocalInfo(wxLocaleInfo index)
 /* static */
 wxString wxLocale::GetInfo(wxLocaleInfo index, wxLocaleCategory cat)
 {
-    wxUint32 lcid = LOCALE_USER_DEFAULT;
-    if ( wxGetLocale() )
+    const wxLanguageInfo * const
+        info = wxGetLocale() ? GetLanguageInfo(wxGetLocale()->GetLanguage())
+                             : NULL;
+    if ( !info )
     {
-        const wxLanguageInfo * const
-            info = GetLanguageInfo(wxGetLocale()->GetLanguage());
-        if ( info )
-            lcid = info->GetLCID();
+        // wxSetLocale() hadn't been called yet of failed, hence CRT must be
+        // using "C" locale -- but check it to detect bugs that would happen if
+        // this were not the case.
+        wxASSERT_MSG( strcmp(setlocale(LC_ALL, NULL), "C") == 0,
+                      wxS("You probably called setlocale() directly instead ")
+                      wxS("of calling wxSetLocale() and now there is a ")
+                      wxS("mismatch between C/C++ and Windows locale.\n")
+                      wxS("Things are going to break, use wxSetLocale() to ")
+                      wxS("avoid this!") );
+
+
+        // Return the hard coded values for C locale. This is really the right
+        // thing to do as there is no LCID we can use in the code below in this
+        // case, even LOCALE_INVARIANT is not quite the same as C locale (the
+        // only difference is that it uses %Y instead of %y in the date format
+        // but this difference is significant enough).
+        switch ( index )
+        {
+            case wxLOCALE_THOUSANDS_SEP:
+                return wxString();
+
+            case wxLOCALE_DECIMAL_POINT:
+                return ".";
+
+            case wxLOCALE_SHORT_DATE_FMT:
+                return "%m/%d/%y";
+
+            case wxLOCALE_LONG_DATE_FMT:
+                return "%A, %B %d, %Y";
+
+            case wxLOCALE_TIME_FMT:
+                return "%H:%M:%S";
+
+            case wxLOCALE_DATE_TIME_FMT:
+                return "%m/%d/%y %H:%M:%S";
+
+            default:
+                wxFAIL_MSG( "unknown wxLocaleInfo" );
+        }
     }
 
+    const wxUint32 lcid = info->GetLCID();
+
     wxString str;
 
     wxChar buf[256];
index 5225ce76b1d39963669a4721dd0895b5e703d9ff..6619dd21c529f03c6022798c4d663e9e135e9344 100644 (file)
@@ -472,8 +472,6 @@ bool TestApp::OnInit()
     if ( !TestAppBase::OnInit() )
         return false;
 
-    SetCLocale();
-
 #if wxUSE_GUI
     cout << "Test program for wxWidgets GUI features\n"
 #else