X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/c5e8b33639ce37dffc19505fd78ce74e1b465a4d..86ac84b8ce086e6bbda58f422d41f84268606e35:/include/wx/datetime.h diff --git a/include/wx/datetime.h b/include/wx/datetime.h index 5b26a830a0..351ced98ea 100644 --- a/include/wx/datetime.h +++ b/include/wx/datetime.h @@ -139,33 +139,12 @@ extern WXDLLIMPEXP_DATA_BASE(const wxDateTime) wxDefaultDateTime; // conditional compilation // ---------------------------------------------------------------------------- -#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): - // - // strncpy(buf, "Tue Dec 21 20:25:40 1999", 128); - // strptime(buf, "%x", &tm); - // - // so don't use it - #undef HAVE_STRPTIME -#endif // broken strptime() - -#if defined(HAVE_STRPTIME) && defined(__DARWIN__) && defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS - // configure detects strptime as linkable because it's in the OS X - // System library but MSL headers don't declare it. - -// char *strptime(const char *, const char *, struct tm *); - // However, we DON'T want to just provide it here because we would - // crash and/or overwrite data when strptime from OS X tries - // to fill in MW's struct tm which is two fields shorter (no TZ stuff) - // So for now let's just say we don't have strptime - #undef HAVE_STRPTIME -#endif - -// everyone has strftime except Win CE unless VC8 is used -#if !defined(__WXWINCE__) || defined(__VISUALC8__) - #define HAVE_STRFTIME +// if configure detected strftime(), we have it too +#ifdef HAVE_STRFTIME + #define wxHAS_STRFTIME +// suppose everyone else has strftime except Win CE unless VC8 is used +#elif !defined(__WXWINCE__) || defined(__VISUALC8__) + #define wxHAS_STRFTIME #endif // ---------------------------------------------------------------------------- @@ -1099,36 +1078,41 @@ public: inline wxTimeSpan Subtract(const wxDateTime& dt) const; inline wxTimeSpan operator-(const wxDateTime& dt2) const; - // conversion to/from text: all conversions from text return an object - // representing the next character following the date specification (i.e. - // the one where the scan had to stop) or a special NULL-like object - // on failure -- this object is necessary to preserve compatibility with - // the existing code assigning the return value of these functions to - // either char* or wxChar* (new code should treat the return value as bool - // and use end parameter to retrieve the end of the scan) + // conversion to/from text // ------------------------------------------------------------------------ + // all conversions functions return true to indicate whether parsing + // succeeded or failed and fill in the provided end iterator, which must + // not be NULL, with the location of the character where the parsing + // stopped (this will be end() of the passed string if everything was + // parsed) + // parse a string in RFC 822 format (found e.g. in mail headers and // having the form "Wed, 10 Feb 1999 19:07:07 +0100") - wxAnyStrPtr ParseRfc822Date(const wxString& date, - wxString::const_iterator *end = NULL); + bool ParseRfc822Date(const wxString& date, + wxString::const_iterator *end); // parse a date/time in the given format (see strptime(3)), fill in // the missing (in the string) fields with the values of dateDef (by // default, they will not change if they had valid values or will // default to Today() otherwise) - wxAnyStrPtr ParseFormat(const wxString& date, - const wxString& format = wxDefaultDateTimeFormat, - const wxDateTime& dateDef = wxDefaultDateTime, - wxString::const_iterator *end = NULL); - - wxAnyStrPtr ParseFormat(const wxString& date, - const wxString& format, - wxString::const_iterator *end) + bool ParseFormat(const wxString& date, + const wxString& format, + const wxDateTime& dateDef, + wxString::const_iterator *end); + + bool ParseFormat(const wxString& date, + const wxString& format, + wxString::const_iterator *end) { return ParseFormat(date, format, wxDefaultDateTime, end); } + bool ParseFormat(const wxString& date, + wxString::const_iterator *end) + { + return ParseFormat(date, wxDefaultDateTimeFormat, wxDefaultDateTime, end); + } // parse a string containing date, time or both in ISO 8601 format // @@ -1136,49 +1120,36 @@ public: // provide compatibility overloads for them bool ParseISODate(const wxString& date) { - // FIXME-VC6: notice that writing "return ParseFormat() && ..." crashes - // VC6 with internal compiler error so don't attempt to - // simplify this code like this - wxString::const_iterator end; - if ( !ParseFormat(date, wxS("%Y-%m-%d"), &end) ) - return false; - - return end == date.end(); + return ParseFormat(date, wxS("%Y-%m-%d"), &end) && end == date.end(); } bool ParseISOTime(const wxString& time) { wxString::const_iterator end; - if ( !ParseFormat(time, wxS("%H:%M:%S"), &end) ) - return false; - - return end == time.end(); + return ParseFormat(time, wxS("%H:%M:%S"), &end) && end == time.end(); } bool ParseISOCombined(const wxString& datetime, char sep = 'T') { wxString::const_iterator end; const wxString fmt = wxS("%Y-%m-%d") + wxString(sep) + wxS("%H:%M:%S"); - if ( !ParseFormat(datetime, fmt, &end) ) - return false; - - return end == datetime.end(); + return ParseFormat(datetime, fmt, &end) && end == datetime.end(); } // parse a string containing the date/time in "free" format, this // function will try to make an educated guess at the string contents - wxAnyStrPtr ParseDateTime(const wxString& datetime, - wxString::const_iterator *end = NULL); + bool ParseDateTime(const wxString& datetime, + wxString::const_iterator *end); // parse a string containing the date only in "free" format (less // flexible than ParseDateTime) - wxAnyStrPtr ParseDate(const wxString& date, - wxString::const_iterator *end = NULL); + bool ParseDate(const wxString& date, + wxString::const_iterator *end); // parse a string containing the time only in "free" format - wxAnyStrPtr ParseTime(const wxString& time, - wxString::const_iterator *end = NULL); + bool ParseTime(const wxString& time, + wxString::const_iterator *end); // this function accepts strftime()-like format string (default @@ -1203,6 +1174,52 @@ public: wxString FormatISOCombined(char sep = 'T') const { return FormatISODate() + sep + FormatISOTime(); } + + // backwards compatible versions of the parsing functions: they return an + // object representing the next character following the date specification + // (i.e. the one where the scan had to stop) or a special NULL-like object + // on failure + // + // they're not deprecated because a lot of existing code uses them and + // there is no particular harm in keeping them but you should still prefer + // the versions above in the new code + wxAnyStrPtr ParseRfc822Date(const wxString& date) + { + wxString::const_iterator end; + return ParseRfc822Date(date, &end) ? wxAnyStrPtr(date, end) + : wxAnyStrPtr(); + } + + wxAnyStrPtr ParseFormat(const wxString& date, + const wxString& format = wxDefaultDateTimeFormat, + const wxDateTime& dateDef = wxDefaultDateTime) + { + wxString::const_iterator end; + return ParseFormat(date, format, dateDef, &end) ? wxAnyStrPtr(date, end) + : wxAnyStrPtr(); + } + + wxAnyStrPtr ParseDateTime(const wxString& datetime) + { + wxString::const_iterator end; + return ParseDateTime(datetime, &end) ? wxAnyStrPtr(datetime, end) + : wxAnyStrPtr(); + } + + wxAnyStrPtr ParseDate(const wxString& date) + { + wxString::const_iterator end; + return ParseDate(date, &end) ? wxAnyStrPtr(date, end) + : wxAnyStrPtr(); + } + + wxAnyStrPtr ParseTime(const wxString& time) + { + wxString::const_iterator end; + return ParseTime(time, &end) ? wxAnyStrPtr(time, end) + : wxAnyStrPtr(); + } + // implementation // ------------------------------------------------------------------------