From 64f9c1009476cd0a347d6c3dfc93993652cbd199 Mon Sep 17 00:00:00 2001 From: =?utf8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Tue, 22 Feb 2005 16:48:34 +0000 Subject: [PATCH] fixed wxDateTime::ParseFormat to correctly handle '%x' on Windows git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@32310 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/datetime.cpp | 68 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/src/common/datetime.cpp b/src/common/datetime.cpp index 17410c7443..ec0c9c5ccf 100644 --- a/src/common/datetime.cpp +++ b/src/common/datetime.cpp @@ -77,6 +77,12 @@ #include +#ifdef __WINDOWS__ + #include "wx/msw/wrapwin.h" + #include + #include +#endif + #include "wx/datetime.h" #include "wx/stopwatch.h" // for wxGetLocalTimeMillis() @@ -2824,6 +2830,60 @@ const wxChar *wxDateTime::ParseRfc822Date(const wxChar* date) return p; } +#ifdef __WINDOWS__ +// Get's current locale's date formatting string and stores it in fmt if +// the locale is set; otherwise or in case of failure, leaves fmt unchanged +void GetLocaleDateFormat(wxString *fmt) +{ + if ( strcmp(setlocale(LC_ALL, NULL), "C") != 0 ) + { + // The locale was programatically set to non-C. We assume that this was + // done using wxLocale, in which case thread's current locale is also + // set to correct LCID value and we can use GetLocaleInfo to determine + // the correct formatting string: + LCID lcid = GetThreadLocale(); + wxChar delim[5]; // fields deliminer, 4 chars max + if ( GetLocaleInfo(lcid, LOCALE_SDATE, delim, 5) ) + { + wxChar centurybuf[2]; // use %y or %Y, 1 char max + wxChar century = 'y'; + if ( GetLocaleInfo(lcid, LOCALE_ICENTURY, centurybuf, 2) ) + { + if ( centurybuf[0] == _T('1') ) + century = 'Y'; + // else 'y' as above + } + + wxChar order[2]; // order code, 1 char max + if ( GetLocaleInfo(lcid, LOCALE_IDATE, order, 2) ) + { + if ( order[0] == _T('0') ) // M-D-Y + { + *fmt = wxString::Format(_T("%%m%s%%d%s%%%c"), + delim, delim, century); + } + else if ( order[0] == _T('1') ) // D-M-Y + { + *fmt = wxString::Format(_T("%%d%s%%m%s%%%c"), + delim, delim, century); + } + else if ( order[0] == _T('2') ) // Y-M-D + { + *fmt = wxString::Format(_T("%%%c%s%%m%s%%d"), + century, delim, delim); + } + else + { + wxFAIL_MSG(_T("unexpected GetLocaleInfo return value")); + } + } + } + // if we failed, leave fmtDate value unchanged and + // try our luck with the default set above + } +} +#endif // __WINDOWS__ + const wxChar *wxDateTime::ParseFormat(const wxChar *date, const wxChar *format, const wxDateTime& dateDef) @@ -3183,11 +3243,11 @@ const wxChar *wxDateTime::ParseFormat(const wxChar *date, } #endif // HAVE_STRPTIME - // TODO query the LOCALE_IDATE setting under Win32 { wxDateTime dt; wxString fmtDate, fmtDateAlt; + if ( IsWestEuropeanCountry(GetCountry()) || GetCountry() == Russia ) { @@ -3200,6 +3260,12 @@ const wxChar *wxDateTime::ParseFormat(const wxChar *date, fmtDateAlt = _T("%d/%m/%y"); } +#ifdef __WINDOWS__ + // The above doesn't work for all locales, try to query + // Windows for the right way of formatting the date: + GetLocaleDateFormat(&fmtDate); +#endif + const wxChar *result = dt.ParseFormat(input, fmtDate); if ( !result ) -- 2.47.2