X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/3371873669823e49ac8b01e67711e453252eaa23..d0f063020e6fb1b1a660952d0844071e0df24324:/include/wx/datetime.h?ds=sidebyside diff --git a/include/wx/datetime.h b/include/wx/datetime.h index b990cfb508..323540f813 100644 --- a/include/wx/datetime.h +++ b/include/wx/datetime.h @@ -17,19 +17,23 @@ #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" +#include "wx/anystr.h" class WXDLLIMPEXP_FWD_BASE wxDateTime; class WXDLLIMPEXP_FWD_BASE wxTimeSpan; class WXDLLIMPEXP_FWD_BASE wxDateSpan; +#ifdef __WXMSW__ +struct _SYSTEMTIME; +#endif #include "wx/dynarray.h" @@ -130,6 +134,16 @@ extern WXDLLIMPEXP_DATA_BASE(const wxDateTime) wxDefaultDateTime; #define wxInvalidDateTime wxDefaultDateTime + +// ---------------------------------------------------------------------------- +// conditional compilation +// ---------------------------------------------------------------------------- + +// everyone has strftime except Win CE unless VC8 is used +#if !defined(__WXWINCE__) || defined(__VISUALC8__) + #define HAVE_STRFTIME +#endif + // ---------------------------------------------------------------------------- // wxDateTime represents an absolute moment in the time // ---------------------------------------------------------------------------- @@ -230,7 +244,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 @@ -525,16 +539,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); @@ -598,6 +621,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 @@ -697,7 +726,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()) @@ -912,6 +941,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) // ------------------------------------------------------------------------ @@ -1035,154 +1075,79 @@ 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); + return ParseFormat(date, wxDefaultDateTimeFormat, wxDefaultDateTime, end); } - const wchar_t *ParseFormat(const wchar_t *date, - const wxString& format = wxDefaultDateTimeFormat, - const wxDateTime& dateDef = wxDefaultDateTime) + // 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) { - const wxString datestr(date); wxString::const_iterator end; - if ( !ParseFormat(datestr, format, dateDef, &end) ) - return NULL; - - return date + (end - datestr.begin()); + return ParseFormat(date, wxS("%Y-%m-%d"), &end) && end == date.end(); } - const wchar_t *ParseFormat(const wchar_t *date, - const wchar_t *format = wxDefaultDateTimeFormat, - const wxDateTime& dateDef = wxDefaultDateTime) + bool ParseISOTime(const wxString& time) { - return ParseFormat(date, wxString(format), dateDef); + wxString::const_iterator end; + return ParseFormat(time, wxS("%H:%M:%S"), &end) && end == time.end(); } - const char *ParseFormat(const char *date, - const char *format = wxDefaultDateTimeFormat, - const wxDateTime& dateDef = wxDefaultDateTime) + bool ParseISOCombined(const wxString& datetime, char sep = 'T') { - return ParseFormat(wxString(date), wxString(format), dateDef); + 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 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); + bool ParseTime(const wxString& time, + wxString::const_iterator *end); - 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 @@ -1191,15 +1156,66 @@ 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(); } + + + // 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 // ------------------------------------------------------------------------ @@ -1211,7 +1227,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() @@ -1224,23 +1240,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() @@ -1385,7 +1384,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 { @@ -2155,6 +2154,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 // ----------------------------------------------------------------------------