X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/56560c387fc77f80f7a6846f01c1f8402fcd50fd..0c73d133517098480565f3dbb529a5f2a72ef68f:/include/wx/datetime.h diff --git a/include/wx/datetime.h b/include/wx/datetime.h index 7c75150dba..5aac2b16d4 100644 --- a/include/wx/datetime.h +++ b/include/wx/datetime.h @@ -27,9 +27,9 @@ #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; #include "wx/dynarray.h" @@ -37,11 +37,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 +122,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 +418,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; } @@ -564,7 +570,7 @@ public: // ------------------------------------------------------------------------ // default ctor does not initialize the object, use Set()! - wxDateTime() { m_time = wxLongLong((long)ULONG_MAX, ULONG_MAX); } + wxDateTime() { m_time = wxLongLong(wxINT32_MIN, 0); } // from time_t: seconds since the Epoch 00:00:00 UTC, Jan 1, 1970) #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400)) @@ -639,6 +645,10 @@ public: // resets time to 00:00:00, doesn't change the date wxDateTime& ResetTime(); + // get the date part of this object only, i.e. the object which has the + // same date as this one but time of 00:00:00 + wxDateTime GetDateOnly() const; + // the following functions don't change the values of the other // fields, i.e. SetMinute() won't change either hour or seconds value @@ -1027,33 +1037,158 @@ 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 wxChar *format = wxDefaultDateTimeFormat, - const wxDateTime& dateDef = wxDefaultDateTime); + 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) + { + 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 = L"%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); + } + // 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 // for the current locale) and returns the string containing the // resulting text representation - wxString Format(const wxChar *format = wxDefaultDateTimeFormat, + 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")); } @@ -1081,14 +1216,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() @@ -1290,7 +1442,7 @@ public: // resulting text representation. Notice that only some of format // specifiers valid for wxDateTime are valid for wxTimeSpan: hours, // minutes and seconds make sense, but not "PM/AM" string for example. - wxString Format(const wxChar *format = wxDefaultTimeSpanFormat) const; + wxString Format(const wxString& format = wxDefaultTimeSpanFormat) const; // implementation // ------------------------------------------------------------------------ @@ -1483,7 +1635,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); @@ -1672,7 +1824,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, @@ -1928,7 +2080,9 @@ inline wxLongLong wxTimeSpan::GetSeconds() const inline int wxTimeSpan::GetMinutes() const { - return (GetSeconds() / 60l).GetLo(); + // explicit cast to int suppresses a warning with CodeWarrior and possibly + // others (changing the return type to long from int is impossible in 2.8) + return (int)((GetSeconds() / 60l).GetLo()); } inline int wxTimeSpan::GetHours() const