From 2423ef22eeb56d05635bbf9af1ee626af7876ac5 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 1 May 2003 00:48:58 +0000 Subject: [PATCH] compilation fix for Unicode git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@20401 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/datetime.cpp | 43 +++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/src/common/datetime.cpp b/src/common/datetime.cpp index 7ff53bacf5..adc7531568 100644 --- a/src/common/datetime.cpp +++ b/src/common/datetime.cpp @@ -88,7 +88,8 @@ const long wxDateTime::TIME_T_FACTOR = 1000l; // conditional compilation // ---------------------------------------------------------------------------- -#if defined(HAVE_STRPTIME) && defined(__LINUX__) +#if defined(HAVE_STRPTIME) && defined(__GLIBC__) && \ + ((__GLIBC__ == 2) && (__GLIBC_MINOR__ == 0)) // glibc 2.0.7 strptime() is broken - the following snippet causes it to // crash (instead of just failing): // @@ -342,7 +343,7 @@ static long GetTruncatedJDN(wxDateTime::wxDateTime_t day, - JDN_OFFSET; } -// this function is a wrapper around strftime(3) +// this function is a wrapper around strftime(3) adding error checking static wxString CallStrftime(const wxChar *format, const tm* tm) { wxChar buf[4096]; @@ -355,6 +356,35 @@ static wxString CallStrftime(const wxChar *format, const tm* tm) return wxString(buf); } +#ifdef HAVE_STRPTIME + +// Unicode-friendly strptime() wrapper +static const wxChar * +CallStrptime(const wxChar *input, const char *fmt, tm *tm) +{ + // the problem here is that strptime() returns pointer into the string we + // passed to it while we're really interested in the pointer into the + // original, Unicode, string so we try to transform the pointer back +#if wxUSE_UNICODE + wxCharBuffer inputMB(wxConvertWX2MB(input)); +#else // ASCII + const char * const inputMB = input; +#endif // Unicode/Ascii + + const char *result = strptime(inputMB, fmt, tm); + if ( !result ) + return NULL; + +#if wxUSE_UNICODE + // FIXME: this is wrong in presence of surrogates &c + return input + (result - inputMB.data()); +#else // ASCII + return result; +#endif // Unicode/Ascii +} + +#endif // HAVE_STRPTIME + // if year and/or month have invalid values, replace them with the current ones static void ReplaceDefaultYearMonthWithCurrent(int *year, wxDateTime::Month *month) @@ -2921,12 +2951,13 @@ const wxChar *wxDateTime::ParseFormat(const wxChar *date, case _T('x'): // locale default date representation #ifdef HAVE_STRPTIME - // try using strptime() - it may fail even if the input is + // try using strptime() -- it may fail even if the input is // correct but the date is out of range, so we will fall back - // to our generic code anyhow (FIXME !Unicode friendly) + // to our generic code anyhow { struct tm tm; - const wxChar *result = strptime(input, "%x", &tm); + + const wxChar *result = CallStrptime(input, "%x", &tm); if ( result ) { input = result; @@ -2991,7 +3022,7 @@ const wxChar *wxDateTime::ParseFormat(const wxChar *date, { // use strptime() to do it for us (FIXME !Unicode friendly) struct tm tm; - input = strptime(input, "%X", &tm); + input = CallStrptime(input, "%X", &tm); if ( !input ) { return (wxChar *)NULL; -- 2.47.2