X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/f7541d48074e6a44fb00050424d03ad922ebacb0..db414879d45f5da2be534a0f523b0506f2ab1183:/include/wx/datetime.h diff --git a/include/wx/datetime.h b/include/wx/datetime.h index 45afb33748..9deab532e1 100644 --- a/include/wx/datetime.h +++ b/include/wx/datetime.h @@ -19,18 +19,19 @@ #ifdef __WXWINCE__ #include "wx/msw/wince/time.h" -#elif !defined(__WXPALMOS5__) +#else #include #endif // OS #include // for INT_MIN #include "wx/longlong.h" +#include "wx/anystr.h" class WXDLLIMPEXP_FWD_BASE wxDateTime; class WXDLLIMPEXP_FWD_BASE wxTimeSpan; class WXDLLIMPEXP_FWD_BASE wxDateSpan; -#ifdef __WXMSW__ +#ifdef __WINDOWS__ struct _SYSTEMTIME; #endif @@ -52,27 +53,6 @@ struct _SYSTEMTIME; * 5. wxDateTimeHolidayAuthority for Easter and other christian feasts */ -/* Two wrapper functions for thread safety */ -#ifdef HAVE_LOCALTIME_R -#define wxLocaltime_r localtime_r -#else -WXDLLIMPEXP_BASE struct tm *wxLocaltime_r(const time_t*, struct tm*); -#if wxUSE_THREADS && !defined(__WINDOWS__) && !defined(__WATCOMC__) - // On Windows, localtime _is_ threadsafe! -#warning using pseudo thread-safe wrapper for localtime to emulate localtime_r -#endif -#endif - -#ifdef HAVE_GMTIME_R -#define wxGmtime_r gmtime_r -#else -WXDLLIMPEXP_BASE struct tm *wxGmtime_r(const time_t*, struct tm*); -#if wxUSE_THREADS && !defined(__WINDOWS__) && !defined(__WATCOMC__) - // On Windows, gmtime _is_ threadsafe! -#warning using pseudo thread-safe wrapper for gmtime to emulate gmtime_r -#endif -#endif - /* The three (main) classes declared in this header represent: @@ -127,12 +107,25 @@ WXDLLIMPEXP_BASE struct tm *wxGmtime_r(const time_t*, struct tm*); // wxInvalidDateTime) class WXDLLIMPEXP_FWD_BASE wxDateTime; -extern WXDLLIMPEXP_DATA_BASE(const char *) wxDefaultDateTimeFormat; -extern WXDLLIMPEXP_DATA_BASE(const char *) wxDefaultTimeSpanFormat; +extern WXDLLIMPEXP_DATA_BASE(const char) wxDefaultDateTimeFormat[]; +extern WXDLLIMPEXP_DATA_BASE(const char) wxDefaultTimeSpanFormat[]; extern WXDLLIMPEXP_DATA_BASE(const wxDateTime) wxDefaultDateTime; #define wxInvalidDateTime wxDefaultDateTime + +// ---------------------------------------------------------------------------- +// conditional compilation +// ---------------------------------------------------------------------------- + +// 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 + // ---------------------------------------------------------------------------- // wxDateTime represents an absolute moment in the time // ---------------------------------------------------------------------------- @@ -159,7 +152,7 @@ public: // the time in the current time zone Local, - // zones from GMT (= Greenwhich Mean Time): they're guaranteed to be + // zones from GMT (= Greenwich Mean Time): they're guaranteed to be // consequent numbers, so writing something like `GMT0 + offset' is // safe if abs(offset) <= 12 @@ -233,7 +226,7 @@ public: // adoption of the Gregorian calendar (see IsGregorian()) // // All data and comments taken verbatim from "The Calendar FAQ (v 2.0)" - // by Claus Tøndering, http://www.pip.dknet.dk/~c-t/calendar.html + // by Claus Tøndering, http://www.pip.dknet.dk/~c-t/calendar.html // except for the comments "we take". // // Symbol "->" should be read as "was followed by" in the comments @@ -448,7 +441,9 @@ public: // instead of modifying the member fields directly! struct WXDLLIMPEXP_BASE Tm { - wxDateTime_t msec, sec, min, hour, mday; + wxDateTime_t msec, sec, min, hour, + mday, // Day of the month in 1..31 range. + yday; // Day of the year in 0..365 range. Month mon; int year; @@ -483,9 +478,10 @@ public: // the timezone we correspond to TimeZone m_tz; - // these values can't be accessed directly because they're not always - // computed and we calculate them on demand - wxDateTime_t wday, yday; + // This value can only be accessed via GetWeekDay() and not directly + // because it's not always computed when creating this object and may + // need to be calculated on demand. + wxDateTime_t wday; }; // static methods @@ -528,16 +524,25 @@ public: int year = Inv_Year, Calendar cal = Gregorian); + // get the full (default) or abbreviated month name in the current // locale, returns empty string on error static wxString GetMonthName(Month month, NameFlags flags = Name_Full); + // get the standard English full (default) or abbreviated month name + static wxString GetEnglishMonthName(Month month, + NameFlags flags = Name_Full); + // get the full (default) or abbreviated weekday name in the current // locale, returns empty string on error static wxString GetWeekDayName(WeekDay weekday, NameFlags flags = Name_Full); + // get the standard English full (default) or abbreviated weekday name + static wxString GetEnglishWeekDayName(WeekDay weekday, + NameFlags flags = Name_Full); + // get the AM and PM strings in the current locale (may be empty) static void GetAmPmStrings(wxString *am, wxString *pm); @@ -601,7 +606,7 @@ public: wxDateTime_t minute = 0, wxDateTime_t second = 0, wxDateTime_t millisec = 0); -#ifdef __WXMSW__ +#ifdef __WINDOWS__ wxDateTime(const struct _SYSTEMTIME& st) { SetFromMSWSysTime(st); @@ -706,7 +711,7 @@ public: wxDateTime& SetToPrevWeekDay(WeekDay weekday); inline wxDateTime GetPrevWeekDay(WeekDay weekday) const; - // set to Nth occurence of given weekday in the given month of the + // set to Nth occurrence of given weekday in the given month of the // given year (time is set to 0), return true on success and false on // failure. n may be positive (1..5) or negative to count from the end // of the month (see helper function SetToLastWeekDay()) @@ -923,14 +928,17 @@ public: // SYSTEMTIME format // ------------------------------------------------------------------------ -#ifdef __WXMSW__ - +#ifdef __WINDOWS__ // convert SYSTEMTIME to wxDateTime - wxDateTime& SetFromMSWSysTime(const struct _SYSTEMTIME&); + wxDateTime& SetFromMSWSysTime(const struct _SYSTEMTIME& st); // convert wxDateTime to SYSTEMTIME - void GetAsMSWSysTime(struct _SYSTEMTIME*) const; -#endif // __WXMSW__ + void GetAsMSWSysTime(struct _SYSTEMTIME* st) const; + + // same as above but only take date part into account, time is always zero + wxDateTime& SetFromMSWSysDate(const struct _SYSTEMTIME& st); + void GetAsMSWSysDate(struct _SYSTEMTIME* st) const; +#endif // __WINDOWS__ // comparison (see also functions below for operator versions) // ------------------------------------------------------------------------ @@ -962,37 +970,37 @@ public: inline bool operator<(const wxDateTime& dt) const { - wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") ); + wxASSERT_MSG( IsValid() && dt.IsValid(), wxT("invalid wxDateTime") ); return GetValue() < dt.GetValue(); } inline bool operator<=(const wxDateTime& dt) const { - wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") ); + wxASSERT_MSG( IsValid() && dt.IsValid(), wxT("invalid wxDateTime") ); return GetValue() <= dt.GetValue(); } inline bool operator>(const wxDateTime& dt) const { - wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") ); + wxASSERT_MSG( IsValid() && dt.IsValid(), wxT("invalid wxDateTime") ); return GetValue() > dt.GetValue(); } inline bool operator>=(const wxDateTime& dt) const { - wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") ); + wxASSERT_MSG( IsValid() && dt.IsValid(), wxT("invalid wxDateTime") ); return GetValue() >= dt.GetValue(); } inline bool operator==(const wxDateTime& dt) const { - wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") ); + wxASSERT_MSG( IsValid() && dt.IsValid(), wxT("invalid wxDateTime") ); return GetValue() == dt.GetValue(); } inline bool operator!=(const wxDateTime& dt) const { - wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") ); + wxASSERT_MSG( IsValid() && dt.IsValid(), wxT("invalid wxDateTime") ); return GetValue() != dt.GetValue(); } @@ -1055,91 +1063,40 @@ public: inline wxTimeSpan Subtract(const wxDateTime& dt) const; inline wxTimeSpan operator-(const wxDateTime& dt2) const; - // conversion to/from text: all conversions from text return the pointer to - // the next character following the date specification (i.e. the one where - // the scan had to stop) or NULL on failure; for the versions taking - // wxString or wxCStrData, we don't know if the user code needs char* or - // wchar_t* pointer and so we return char* one for compatibility with the - // existing ANSI code and also return iterator in another output parameter - // (it will be equal to end if the entire string was parsed) + // 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") - const char *ParseRfc822Date(const wxString& date, - wxString::const_iterator *end = NULL); - const char *ParseRfc822Date(const wxCStrData& date, - wxString::const_iterator *end = NULL) - { - return ParseRfc822Date(date.AsString(), end); - } - - const wchar_t *ParseRfc822Date(const wchar_t* date) - { - return ReturnEndAsWidePtr(&wxDateTime::ParseRfc822Date, date); - } - - const char *ParseRfc822Date(const char* date) - { - return ParseRfc822Date(wxString(date)); - } + 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) - const char *ParseFormat(const wxString& date, - const wxString& format = wxDefaultDateTimeFormat, - const wxDateTime& dateDef = wxDefaultDateTime, - wxString::const_iterator *end = NULL); - - const char *ParseFormat(const wxString& date, - const char *format = wxDefaultDateTimeFormat, - const wxDateTime& dateDef = wxDefaultDateTime, - wxString::const_iterator *end = NULL) - { - return ParseFormat(date, wxString(format), dateDef, end); - } - - const char *ParseFormat(const wxString& date, - const wxString& format = wxDefaultDateTimeFormat, - wxString::const_iterator *end = NULL) + 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); } - const char *ParseFormat(const wxCStrData& date, - const wxString& format = wxDefaultDateTimeFormat, - const wxDateTime& dateDef = wxDefaultDateTime, - wxString::const_iterator *end = NULL) + bool ParseFormat(const wxString& date, + wxString::const_iterator *end) { - return ParseFormat(date.AsString(), format, dateDef, end); - } - - const wchar_t *ParseFormat(const wchar_t *date, - const wxString& format = wxDefaultDateTimeFormat, - const wxDateTime& dateDef = wxDefaultDateTime) - { - const wxString datestr(date); - wxString::const_iterator end; - if ( !ParseFormat(datestr, format, dateDef, &end) ) - return NULL; - - return date + (end - datestr.begin()); - } - - const char *ParseFormat(const char *date, - const wxString& format = "%c", - const wxDateTime& dateDef = wxDefaultDateTime) - { - return ParseFormat(wxString(date), format, dateDef); - } - - const char *ParseFormat(const char *date, - const char *format = wxDefaultDateTimeFormat, - const wxDateTime& dateDef = wxDefaultDateTime) - { - return ParseFormat(wxString(date), wxString(format), dateDef); + return ParseFormat(date, wxDefaultDateTimeFormat, wxDefaultDateTime, end); } // parse a string containing date, time or both in ISO 8601 format @@ -1167,65 +1124,18 @@ public: // parse a string containing the date/time in "free" format, this // function will try to make an educated guess at the string contents - const char *ParseDateTime(const wxString& datetime, - wxString::const_iterator *end = NULL); - - const char *ParseDateTime(const wxCStrData& datetime, - wxString::const_iterator *end = NULL) - { - return ParseDateTime(datetime.AsString(), end); - } - - const wchar_t *ParseDateTime(const wchar_t *datetime) - { - return ReturnEndAsWidePtr(&wxDateTime::ParseDateTime, datetime); - } - - const char *ParseDateTime(const char *datetime) - { - return ParseDateTime(wxString(datetime)); - } + bool ParseDateTime(const wxString& datetime, + wxString::const_iterator *end); // parse a string containing the date only in "free" format (less // flexible than ParseDateTime) - const char *ParseDate(const wxString& date, - wxString::const_iterator *end = NULL); - - const char *ParseDate(const wxCStrData& date, - wxString::const_iterator *end = NULL) - { - return ParseDate(date.AsString(), end); - } - - const wchar_t *ParseDate(const wchar_t *date) - { - return ReturnEndAsWidePtr(&wxDateTime::ParseDate, date); - } - - const char *ParseDate(const char *date) - { - return ParseDate(wxString(date)); - } + bool ParseDate(const wxString& date, + wxString::const_iterator *end); // parse a string containing the time only in "free" format - const char *ParseTime(const wxString& time, - wxString::const_iterator *end = NULL); - - const char *ParseTime(const wxCStrData& time, - wxString::const_iterator *end = NULL) - { - return ParseTime(time.AsString(), end); - } - - const wchar_t *ParseTime(const wchar_t *time) - { - return ReturnEndAsWidePtr(&wxDateTime::ParseTime, time); - } + bool ParseTime(const wxString& time, + wxString::const_iterator *end); - const char *ParseTime(const char *time) - { - return ParseTime(wxString(time)); - } // this function accepts strftime()-like format string (default // argument corresponds to the preferred date and time representation @@ -1249,6 +1159,90 @@ 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(); + } + + // In addition to wxAnyStrPtr versions above we also must provide the + // overloads for C strings as we must return a pointer into the original + // string and not inside a temporary wxString which would have been created + // if the overloads above were used. + // + // And then we also have to provide the overloads for wxCStrData, as usual. + wxAnyStrPtr ParseRfc822Date(const wxCStrData& date) + { return ParseRfc822Date(wxString(date)); } + const char* ParseRfc822Date(const char* date); + const wchar_t* ParseRfc822Date(const wchar_t* date); + + wxAnyStrPtr ParseFormat(const wxCStrData& date, + const wxString& format = wxDefaultDateTimeFormat, + const wxDateTime& dateDef = wxDefaultDateTime) + { return ParseFormat(wxString(date), format, dateDef); } + const char* ParseFormat(const char* date, + const wxString& format = wxDefaultDateTimeFormat, + const wxDateTime& dateDef = wxDefaultDateTime); + const wchar_t* ParseFormat(const wchar_t* date, + const wxString& format = wxDefaultDateTimeFormat, + const wxDateTime& dateDef = wxDefaultDateTime); + + wxAnyStrPtr ParseDateTime(const wxCStrData& datetime) + { return ParseDateTime(wxString(datetime)); } + const char* ParseDateTime(const char* datetime); + const wchar_t* ParseDateTime(const wchar_t* datetime); + + wxAnyStrPtr ParseDate(const wxCStrData& date) + { return ParseDate(wxString(date)); } + const char* ParseDate(const char* date); + const wchar_t* ParseDate(const wchar_t* date); + + wxAnyStrPtr ParseTime(const wxCStrData& time) + { return ParseTime(wxString(time)); } + const char* ParseTime(const char* time); + const wchar_t* ParseTime(const wchar_t* time); + + // implementation // ------------------------------------------------------------------------ @@ -1259,7 +1253,7 @@ public: inline wxLongLong GetValue() const; // a helper function to get the current time_t - static time_t GetTimeNow() { return time((time_t *)NULL); } + static time_t GetTimeNow() { return time(NULL); } // another one to get the current time broken down static struct tm *GetTmNow() @@ -1272,23 +1266,6 @@ public: static struct tm *GetTmNow(struct tm *tmstruct); private: - // helper function for defining backward-compatible wrappers for code - // using wchar_t* pointer instead of wxString iterators - typedef - const char *(wxDateTime::*StringMethod)(const wxString& s, - wxString::const_iterator *end); - - const wchar_t *ReturnEndAsWidePtr(StringMethod func, const wchar_t *p) - { - const wxString s(p); - wxString::const_iterator end; - if ( !(this->*func)(s, &end) ) - return NULL; - - return p + (end - s.begin()); - } - - // the current country - as it's the same for all program objects (unless // it runs on a _really_ big cluster system :-), this is a static member: // see SetCountry() and GetCountry() @@ -1768,7 +1745,9 @@ protected: inline bool wxDateTime::IsInStdRange() const { - return m_time >= 0l && (m_time / TIME_T_FACTOR) < LONG_MAX; + // currently we don't know what is the real type of time_t so prefer to err + // on the safe side and limit it to 32 bit values which is safe everywhere + return m_time >= 0l && (m_time / TIME_T_FACTOR) < wxINT32_MAX; } /* static */ @@ -1828,7 +1807,7 @@ inline wxDateTime::wxDateTime(double jdn) inline wxDateTime& wxDateTime::Set(const Tm& tm) { - wxASSERT_MSG( tm.IsValid(), _T("invalid broken down date/time") ); + wxASSERT_MSG( tm.IsValid(), wxT("invalid broken down date/time") ); return Set(tm.mday, (Month)tm.mon, tm.year, tm.hour, tm.min, tm.sec, tm.msec); @@ -1859,14 +1838,14 @@ inline wxDateTime::wxDateTime(wxDateTime_t day, inline wxLongLong wxDateTime::GetValue() const { - wxASSERT_MSG( IsValid(), _T("invalid wxDateTime")); + wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime")); return m_time; } inline time_t wxDateTime::GetTicks() const { - wxASSERT_MSG( IsValid(), _T("invalid wxDateTime")); + wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime")); if ( !IsInStdRange() ) { return (time_t)-1; @@ -1934,21 +1913,21 @@ inline wxDateTime wxDateTime::GetYearDay(wxDateTime_t yday) const inline bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const { - wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime")); + wxASSERT_MSG( IsValid() && datetime.IsValid(), wxT("invalid wxDateTime")); return m_time == datetime.m_time; } inline bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const { - wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime")); + wxASSERT_MSG( IsValid() && datetime.IsValid(), wxT("invalid wxDateTime")); return m_time < datetime.m_time; } inline bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const { - wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime")); + wxASSERT_MSG( IsValid() && datetime.IsValid(), wxT("invalid wxDateTime")); return m_time > datetime.m_time; } @@ -2005,14 +1984,14 @@ inline bool wxDateTime::IsEqualUpTo(const wxDateTime& dt, inline wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const { - wxASSERT_MSG( IsValid(), _T("invalid wxDateTime")); + wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime")); return wxDateTime(m_time + diff.GetValue()); } inline wxDateTime& wxDateTime::Add(const wxTimeSpan& diff) { - wxASSERT_MSG( IsValid(), _T("invalid wxDateTime")); + wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime")); m_time += diff.GetValue(); @@ -2026,14 +2005,14 @@ inline wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff) inline wxDateTime wxDateTime::Subtract(const wxTimeSpan& diff) const { - wxASSERT_MSG( IsValid(), _T("invalid wxDateTime")); + wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime")); return wxDateTime(m_time - diff.GetValue()); } inline wxDateTime& wxDateTime::Subtract(const wxTimeSpan& diff) { - wxASSERT_MSG( IsValid(), _T("invalid wxDateTime")); + wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime")); m_time -= diff.GetValue(); @@ -2047,7 +2026,7 @@ inline wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff) inline wxTimeSpan wxDateTime::Subtract(const wxDateTime& datetime) const { - wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime")); + wxASSERT_MSG( IsValid() && datetime.IsValid(), wxT("invalid wxDateTime")); return wxTimeSpan(GetValue() - datetime.GetValue()); }