X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/fb96cf856e152671099f464f5d1defa429bfc4ef..69659fd770f615210efac4b4fa741b3ad6223616:/src/common/datetime.cpp?ds=sidebyside diff --git a/src/common/datetime.cpp b/src/common/datetime.cpp index c40dedee2b..9067100085 100644 --- a/src/common/datetime.cpp +++ b/src/common/datetime.cpp @@ -70,6 +70,7 @@ #include "wx/intl.h" #include "wx/stopwatch.h" // for wxGetLocalTimeMillis() #include "wx/module.h" + #include "wx/crt.h" #endif // WX_PRECOMP #include "wx/thread.h" @@ -210,7 +211,18 @@ struct tm *wxLocaltime_r(const time_t* ticks, struct tm* temp) // thread local storage for localtime anyway. wxMutexLocker locker(timeLock); #endif - memcpy(temp, localtime(ticks), sizeof(struct tm)); + + // Borland CRT crashes when passed 0 ticks for some reason, see SF bug 1704438 +#ifdef __BORLANDC__ + if ( !*ticks ) + return NULL; +#endif + + const tm * const t = localtime(ticks); + if ( !t ) + return NULL; + + memcpy(temp, t, sizeof(struct tm)); return temp; } #endif // !HAVE_LOCALTIME_R @@ -223,6 +235,16 @@ struct tm *wxGmtime_r(const time_t* ticks, struct tm* temp) // using thread local storage for gmtime anyway. wxMutexLocker locker(timeLock); #endif + +#ifdef __BORLANDC__ + if ( !*ticks ) + return NULL; +#endif + + const tm * const t = gmtime(ticks); + if ( !t ) + return NULL; + memcpy(temp, gmtime(ticks), sizeof(struct tm)); return temp; } @@ -429,7 +451,7 @@ static long GetTruncatedJDN(wxDateTime::wxDateTime_t day, #ifdef HAVE_STRFTIME // this function is a wrapper around strftime(3) adding error checking -static wxString CallStrftime(const wxChar *format, const tm* tm) +static wxString CallStrftime(const wxString& format, const tm* tm) { wxChar buf[4096]; // Create temp wxString here to work around mingw/cygwin bug 1046059 @@ -660,7 +682,7 @@ void wxDateTime::Tm::ComputeWeekDay() // compute the week day from day/month/year: we use the dumbest algorithm // possible: just compute our JDN and then use the (simple to derive) // formula: weekday = (JDN + 1.5) % 7 - wday = (wxDateTime::wxDateTime_t)((wxDateTime::WeekDay)(GetTruncatedJDN(mday, mon, year) + 2) % 7); + wday = (wxDateTime::wxDateTime_t)((GetTruncatedJDN(mday, mon, year) + 2) % 7); } void wxDateTime::Tm::AddMonths(int monDiff) @@ -2257,15 +2279,16 @@ wxDateTime& wxDateTime::MakeFromTimezone(const TimeZone& tz, bool noDST) // wxDateTime to/from text representations // ---------------------------------------------------------------------------- -wxString wxDateTime::Format(const wxChar *format, const TimeZone& tz) const +wxString wxDateTime::Format(const wxString& format, const TimeZone& tz) const { - wxCHECK_MSG( format, wxEmptyString, _T("NULL format in wxDateTime::Format") ); - - time_t time = GetTicks(); + wxCHECK_MSG( !format.empty(), wxEmptyString, + _T("NULL format in wxDateTime::Format") ); // we have to use our own implementation if the date is out of range of // strftime() or if we use non standard specificators #ifdef HAVE_STRFTIME + time_t time = GetTicks(); + if ( (time != (time_t)-1) && !wxStrstr(format, _T("%l")) ) { // use strftime() @@ -2327,7 +2350,7 @@ wxString wxDateTime::Format(const wxChar *format, const TimeZone& tz) const tmTimeOnly.tm_isdst = 0; // no DST, we adjust for tz ourselves wxString tmp, res, fmt; - for ( const wxChar *p = format; *p; p++ ) + for ( wxString::const_iterator p = format.begin(); p != format.end(); ++p ) { if ( *p != _T('%') ) { @@ -2338,7 +2361,7 @@ wxString wxDateTime::Format(const wxChar *format, const TimeZone& tz) const } // set the default format - switch ( *++p ) + switch ( (*++p).GetValue() ) { case _T('Y'): // year has 4 digits fmt = _T("%04d"); @@ -2367,7 +2390,7 @@ wxString wxDateTime::Format(const wxChar *format, const TimeZone& tz) const restart = false; // start of the format specification - switch ( *p ) + switch ( (*p).GetValue() ) { case _T('a'): // a weekday name case _T('A'): @@ -3115,10 +3138,10 @@ static wxString GetLocaleDateFormat() #endif // __WINDOWS__ const wxChar *wxDateTime::ParseFormat(const wxChar *date, - const wxChar *format, + const wxString& format, const wxDateTime& dateDef) { - wxCHECK_MSG( date && format, (wxChar *)NULL, + wxCHECK_MSG( date && !format.empty(), (wxChar *)NULL, _T("NULL pointer in wxDateTime::ParseFormat()") ); wxString str; @@ -3148,7 +3171,7 @@ const wxChar *wxDateTime::ParseFormat(const wxChar *date, int year = 0; const wxChar *input = date; - for ( const wxChar *fmt = format; *fmt; fmt++ ) + for ( wxString::const_iterator fmt = format.begin(); fmt != format.end(); ++fmt ) { if ( *fmt != _T('%') ) { @@ -3189,7 +3212,7 @@ const wxChar *wxDateTime::ParseFormat(const wxChar *date, // the default widths for the various fields if ( !width ) { - switch ( *fmt ) + switch ( (*fmt).GetValue() ) { case _T('Y'): // year has 4 digits width = 4; @@ -3211,7 +3234,7 @@ const wxChar *wxDateTime::ParseFormat(const wxChar *date, } // then the format itself - switch ( *fmt ) + switch ( (*fmt).GetValue() ) { case _T('a'): // a weekday name case _T('A'): @@ -4248,12 +4271,13 @@ enum TimeSpanPart // And, to be better than MFC :-), we also have // %E number of wEeks // %l milliseconds (000 - 999) -wxString wxTimeSpan::Format(const wxChar *format) const +wxString wxTimeSpan::Format(const wxString& format) const { - wxCHECK_MSG( format, wxEmptyString, _T("NULL format in wxTimeSpan::Format") ); + wxCHECK_MSG( !format.empty(), wxEmptyString, + _T("NULL format in wxTimeSpan::Format") ); wxString str; - str.Alloc(wxStrlen(format)); + str.Alloc(format.length()); // Suppose we have wxTimeSpan ts(1 /* hour */, 2 /* min */, 3 /* sec */) // @@ -4271,7 +4295,7 @@ wxString wxTimeSpan::Format(const wxChar *format) const // we remember the most important unit found so far TimeSpanPart partBiggest = Part_MSec; - for ( const wxChar *pch = format; *pch; pch++ ) + for ( wxString::const_iterator pch = format.begin(); pch != format.end(); ++pch ) { wxChar ch = *pch;