X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/12e8247795963d724765d3108aeac5b96dbee83a..67abc78356eca7b033aed920a6852b226fe42438:/include/wx/datetime.h diff --git a/include/wx/datetime.h b/include/wx/datetime.h index fbab3a35c2..7da0c17a28 100644 --- a/include/wx/datetime.h +++ b/include/wx/datetime.h @@ -17,19 +17,22 @@ #if wxUSE_DATETIME -#ifndef __WXWINCE__ -#include -#else -#include "wx/msw/wince/time.h" -#endif +#ifdef __WXWINCE__ + #include "wx/msw/wince/time.h" +#elif !defined(__WXPALMOS5__) + #include +#endif // OS #include // for INT_MIN #include "wx/longlong.h" -class WXDLLIMPEXP_BASE wxDateTime; -class WXDLLIMPEXP_BASE wxTimeSpan; -class WXDLLIMPEXP_BASE wxDateSpan; +class WXDLLIMPEXP_FWD_BASE wxDateTime; +class WXDLLIMPEXP_FWD_BASE wxTimeSpan; +class WXDLLIMPEXP_FWD_BASE wxDateSpan; +#ifdef __WXMSW__ +struct _SYSTEMTIME; +#endif #include "wx/dynarray.h" @@ -37,11 +40,8 @@ class WXDLLIMPEXP_BASE wxDateSpan; // set this to the corresponding value in seconds 1/1/1970 has on your // systems c-runtime -#if defined(__WXMAC__) && !defined(__DARWIN__) && __MSL__ < 0x6000 - #define WX_TIME_BASE_OFFSET ( 2082844800L + 126144000L ) -#else - #define WX_TIME_BASE_OFFSET 0 -#endif +#define WX_TIME_BASE_OFFSET 0 + /* * TODO * @@ -125,10 +125,10 @@ WXDLLIMPEXP_BASE struct tm *wxGmtime_r(const time_t*, struct tm*); // argument for arguments of type wxDateTime; it is also returned by all // functions returning wxDateTime on failure (this is why it is also called // wxInvalidDateTime) -class WXDLLIMPEXP_BASE wxDateTime; +class WXDLLIMPEXP_FWD_BASE wxDateTime; -extern WXDLLIMPEXP_DATA_BASE(const wxChar*) wxDefaultDateTimeFormat; -extern WXDLLIMPEXP_DATA_BASE(const wxChar*) 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 @@ -421,7 +421,16 @@ public: { public: TimeZone(TZ tz); - TimeZone(wxDateTime_t offset = 0) { m_offset = offset; } + + // create time zone object with the given offset + TimeZone(long offset = 0) { m_offset = offset; } + + static TimeZone Make(long offset) + { + TimeZone tz; + tz.m_offset = offset; + return tz; + } long GetOffset() const { return m_offset; } @@ -592,6 +601,12 @@ public: wxDateTime_t minute = 0, wxDateTime_t second = 0, wxDateTime_t millisec = 0); +#ifdef __WXMSW__ + wxDateTime(const struct _SYSTEMTIME& st) + { + SetFromMSWSysTime(st); + } +#endif // default copy ctor ok @@ -906,6 +921,17 @@ public: // pack the date in DOS format unsigned long GetAsDOS() const; + // SYSTEMTIME format + // ------------------------------------------------------------------------ +#ifdef __WXMSW__ + + // convert SYSTEMTIME to wxDateTime + wxDateTime& SetFromMSWSysTime(const struct _SYSTEMTIME&); + + // convert wxDateTime to SYSTEMTIME + void GetAsMSWSysTime(struct _SYSTEMTIME*) const; +#endif // __WXMSW__ + // comparison (see also functions below for operator versions) // ------------------------------------------------------------------------ @@ -1031,27 +1057,186 @@ public: // 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. + // 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) // ------------------------------------------------------------------------ // 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 wxChar *ParseRfc822Date(const wxChar* date); + 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)); + } + // 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 wxChar *ParseFormat(const wxChar *date, - const wxString& format = wxDefaultDateTimeFormat, - const wxDateTime& dateDef = wxDefaultDateTime); + + // notice that we unfortunately need all those overloads because we use + // the type of the date string to select the return value of the + // function: it's wchar_t if a wide string is passed for compatibility + // with the code doing "const wxChar *p = dt.ParseFormat(_T("..."))", + // but char* in all other cases for compatibility with ANSI build which + // allowed code like "const char *p = dt.ParseFormat("...")" + // + // so we need wchar_t overload and now passing s.c_str() as first + // argument is ambiguous because it's convertible to both wxString and + // wchar_t* and now it's passing char* which becomes ambiguous as it is + // convertible to both wxString and wxCStrData hence we need char* + // overload too + // + // and to make our life more miserable we also pay for having the + // optional dateDef parameter: as it's almost never used, we want to + // allow people to omit it when specifying the end iterator output + // parameter but we still have to allow specifying dateDef too, so we + // need another overload for this + // + // FIXME: all this mess could be avoided by using some class similar to + // wxFormatString, i.e. remembering string [pointer] of any type + // and convertible to either char* or wchar_t* as wxCStrData and + // having only 1 (or 2, because of the last paragraph above) + // overload taking it, see #9560 + 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 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) + { + 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); + } + + + // parse a string containing date, time or both in ISO 8601 format + // + // notice that these functions are new in wx 3.0 and so we don't + // provide compatibility overloads for them + bool ParseISODate(const wxString& date) + { + wxString::const_iterator end; + return ParseFormat(date, wxS("%Y-%m-%d"), &end) && end == date.end(); + } + + bool ParseISOTime(const wxString& time) + { + wxString::const_iterator 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"); + 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 - const wxChar *ParseDateTime(const wxChar *datetime); + 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)); + } + // parse a string containing the date only in "free" format (less // flexible than ParseDateTime) - const wxChar *ParseDate(const wxChar *date); + 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)); + } + // parse a string containing the time only in "free" format - const wxChar *ParseTime(const wxChar *time); + 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); + } + + 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 @@ -1060,15 +1245,20 @@ public: wxString Format(const wxString& format = wxDefaultDateTimeFormat, const TimeZone& tz = Local) const; // preferred date representation for the current locale - wxString FormatDate() const { return Format(_T("%x")); } + wxString FormatDate() const { return Format(wxS("%x")); } // preferred time representation for the current locale - wxString FormatTime() const { return Format(_T("%X")); } + wxString FormatTime() const { return Format(wxS("%X")); } // returns the string representing the date in ISO 8601 format // (YYYY-MM-DD) - wxString FormatISODate() const { return Format(_T("%Y-%m-%d")); } + wxString FormatISODate() const { return Format(wxS("%Y-%m-%d")); } // returns the string representing the time in ISO 8601 format // (HH:MM:SS) - wxString FormatISOTime() const { return Format(_T("%H:%M:%S")); } + wxString FormatISOTime() const { return Format(wxS("%H:%M:%S")); } + // return the combined date time representation in ISO 8601 format; the + // separator character should be 'T' according to the standard but it + // can also be useful to set it to ' ' + wxString FormatISOCombined(char sep = 'T') const + { return FormatISODate() + sep + FormatISOTime(); } // implementation // ------------------------------------------------------------------------ @@ -1085,14 +1275,31 @@ public: // another one to get the current time broken down static struct tm *GetTmNow() { - time_t t = GetTimeNow(); - return localtime(&t); + static struct tm l_CurrentTime; + return GetTmNow(&l_CurrentTime); } // get current time using thread-safe function 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() @@ -1237,7 +1444,7 @@ public: // compare two timestamps: works with the absolute values, i.e. 1 // hour is shorter than -2 hours. Also, it will return false if the // timespans are equal in absolute value. - bool IsShorterThan(const wxTimeSpan& t) const { return !IsLongerThan(t); } + bool IsShorterThan(const wxTimeSpan& t) const; inline bool operator<(const wxTimeSpan &ts) const { @@ -1487,7 +1694,7 @@ WX_DECLARE_USER_EXPORTED_OBJARRAY(wxDateTime, wxDateTimeArray, WXDLLIMPEXP_BASE) // virtual methods to work with the holidays they correspond to. // ---------------------------------------------------------------------------- -class WXDLLIMPEXP_BASE wxDateTimeHolidayAuthority; +class WXDLLIMPEXP_FWD_BASE wxDateTimeHolidayAuthority; WX_DEFINE_USER_EXPORTED_ARRAY_PTR(wxDateTimeHolidayAuthority *, wxHolidayAuthoritiesArray, class WXDLLIMPEXP_BASE); @@ -1676,7 +1883,7 @@ inline time_t wxDateTime::GetTicks() const return (time_t)-1; } - return (time_t)((m_time / (long)TIME_T_FACTOR).GetLo())+WX_TIME_BASE_OFFSET ; + return (time_t)((m_time / (long)TIME_T_FACTOR).ToLong()) + WX_TIME_BASE_OFFSET; } inline bool wxDateTime::SetToLastWeekDay(WeekDay weekday, @@ -2007,6 +2214,11 @@ inline bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const return GetValue().Abs() > ts.GetValue().Abs(); } +inline bool wxTimeSpan::IsShorterThan(const wxTimeSpan& ts) const +{ + return GetValue().Abs() < ts.GetValue().Abs(); +} + // ---------------------------------------------------------------------------- // wxDateSpan // ----------------------------------------------------------------------------