#ifdef __WXWINCE__
#include "wx/msw/wince/time.h"
-#elif !defined(__WXPALMOS5__)
+#else
#include <time.h>
#endif // OS
class WXDLLIMPEXP_FWD_BASE wxDateTime;
class WXDLLIMPEXP_FWD_BASE wxTimeSpan;
class WXDLLIMPEXP_FWD_BASE wxDateSpan;
-#ifdef __WXMSW__
+#ifdef __WINDOWS__
struct _SYSTEMTIME;
#endif
* 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:
// 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 defined(HAVE_STRPTIME) && defined(__GLIBC__) && \
- ((__GLIBC__ == 2) && (__GLIBC_MINOR__ == 0))
- // glibc 2.0.7 strptime() is broken - the following snippet causes it to
- // crash (instead of just failing):
- //
- // strncpy(buf, "Tue Dec 21 20:25:40 1999", 128);
- // strptime(buf, "%x", &tm);
- //
- // so don't use it
- #undef HAVE_STRPTIME
-#endif // broken strptime()
-
-#if defined(HAVE_STRPTIME) && defined(__DARWIN__) && defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS
- // configure detects strptime as linkable because it's in the OS X
- // System library but MSL headers don't declare it.
-
-// char *strptime(const char *, const char *, struct tm *);
- // However, we DON'T want to just provide it here because we would
- // crash and/or overwrite data when strptime from OS X tries
- // to fill in MW's struct tm which is two fields shorter (no TZ stuff)
- // So for now let's just say we don't have strptime
- #undef HAVE_STRPTIME
-#endif
-
-// everyone has strftime except Win CE unless VC8 is used
-#if !defined(__WXWINCE__) || defined(__VISUALC8__)
- #define HAVE_STRFTIME
+// 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
// ----------------------------------------------------------------------------
// 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
// 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
// helper classes
// ------------------------------------------------------------------------
- // a class representing a time zone: basicly, this is just an offset
+ // a class representing a time zone: basically, this is just an offset
// (in seconds) from GMT
class WXDLLIMPEXP_BASE TimeZone
{
// 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;
// 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
wxDateTime_t minute = 0,
wxDateTime_t second = 0,
wxDateTime_t millisec = 0);
-#ifdef __WXMSW__
+#ifdef __WINDOWS__
wxDateTime(const struct _SYSTEMTIME& st)
{
SetFromMSWSysTime(st);
// default assignment operator is ok
// calendar calculations (functions which set the date only leave the time
- // unchanged, e.g. don't explictly zero it): SetXXX() functions modify the
+ // unchanged, e.g. don't explicitly zero it): SetXXX() functions modify the
// object itself, GetXXX() ones return a new object.
// ------------------------------------------------------------------------
// 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)
// ------------------------------------------------------------------------
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();
}
inline wxTimeSpan Subtract(const wxDateTime& dt) const;
inline wxTimeSpan operator-(const wxDateTime& dt2) const;
- // conversion to/from text: all conversions from text 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 -- this object is necessary to preserve compatibility with
- // the existing code assigning the return value of these functions to
- // either char* or wxChar* (new code should treat the return value as bool
- // and use end parameter to retrieve the end of the scan)
+ wxDateSpan DiffAsDateSpan(const wxDateTime& dt) const;
+
+ // 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")
- wxAnyStrPtr ParseRfc822Date(const wxString& date,
- wxString::const_iterator *end = NULL);
+ 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)
- wxAnyStrPtr ParseFormat(const wxString& date,
- const wxString& format = wxDefaultDateTimeFormat,
- const wxDateTime& dateDef = wxDefaultDateTime,
- wxString::const_iterator *end = NULL);
-
- wxAnyStrPtr ParseFormat(const wxString& date,
- const wxString& format,
- wxString::const_iterator *end)
+ 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);
}
+ bool ParseFormat(const wxString& date,
+ wxString::const_iterator *end)
+ {
+ return ParseFormat(date, wxDefaultDateTimeFormat, wxDefaultDateTime, end);
+ }
// parse a string containing date, time or both in ISO 8601 format
//
// provide compatibility overloads for them
bool ParseISODate(const wxString& date)
{
- // FIXME-VC6: notice that writing "return ParseFormat() && ..." crashes
- // VC6 with internal compiler error so don't attempt to
- // simplify this code like this
-
wxString::const_iterator end;
- if ( !ParseFormat(date, wxS("%Y-%m-%d"), &end) )
- return false;
-
- return end == date.end();
+ return ParseFormat(date, wxS("%Y-%m-%d"), &end) && end == date.end();
}
bool ParseISOTime(const wxString& time)
{
wxString::const_iterator end;
- if ( !ParseFormat(time, wxS("%H:%M:%S"), &end) )
- return false;
-
- return end == time.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");
- if ( !ParseFormat(datetime, fmt, &end) )
- return false;
-
- return end == datetime.end();
+ 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
- wxAnyStrPtr ParseDateTime(const wxString& datetime,
- wxString::const_iterator *end = NULL);
+ bool ParseDateTime(const wxString& datetime,
+ wxString::const_iterator *end);
// parse a string containing the date only in "free" format (less
// flexible than ParseDateTime)
- wxAnyStrPtr ParseDate(const wxString& date,
- wxString::const_iterator *end = NULL);
+ bool ParseDate(const wxString& date,
+ wxString::const_iterator *end);
// parse a string containing the time only in "free" format
- wxAnyStrPtr ParseTime(const wxString& time,
- wxString::const_iterator *end = NULL);
+ bool ParseTime(const wxString& time,
+ wxString::const_iterator *end);
// this function accepts strftime()-like format string (default
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.
+ // Unfortunately those ones can't return anything as we don't have any
+ // sufficiently long-lived wxAnyStrPtr to return from them: any temporary
+ // strings it would point to would be destroyed when this function returns
+ // making it impossible to dereference the return value. So we just don't
+ // return anything from here which at least allows to keep compatibility
+ // with the code not testing the return value. Other uses of this method
+ // need to be converted to use one of the new bool-returning overloads
+ // above.
+ void ParseRfc822Date(const wxCStrData& date)
+ { ParseRfc822Date(wxString(date)); }
+ const char* ParseRfc822Date(const char* date);
+ const wchar_t* ParseRfc822Date(const wchar_t* date);
+
+ void ParseFormat(const wxCStrData& date,
+ const wxString& format = wxDefaultDateTimeFormat,
+ const wxDateTime& dateDef = wxDefaultDateTime)
+ { 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);
+
+ void ParseDateTime(const wxCStrData& datetime)
+ { ParseDateTime(wxString(datetime)); }
+ const char* ParseDateTime(const char* datetime);
+ const wchar_t* ParseDateTime(const wchar_t* datetime);
+
+ void ParseDate(const wxCStrData& date)
+ { ParseDate(wxString(date)); }
+ const char* ParseDate(const char* date);
+ const wchar_t* ParseDate(const wchar_t* date);
+
+ void ParseTime(const wxCStrData& time)
+ { ParseTime(wxString(time)); }
+ const char* ParseTime(const char* time);
+ const wchar_t* ParseTime(const wchar_t* time);
+
+
// implementation
// ------------------------------------------------------------------------
int GetYears() const { return m_years; }
// get number of months
int GetMonths() const { return m_months; }
+ // returns 12*GetYears() + GetMonths()
+ int GetTotalMonths() const { return 12*m_years + m_months; }
// get number of weeks
int GetWeeks() const { return m_weeks; }
// get number of days
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 */
#if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
inline wxDateTime& wxDateTime::Set(time_t timet)
{
- // assign first to avoid long multiplication overflow!
- m_time = timet - WX_TIME_BASE_OFFSET ;
- m_time *= TIME_T_FACTOR;
+ if ( timet == (time_t)-1 )
+ {
+ m_time = wxInvalidDateTime.m_time;
+ }
+ else
+ {
+ // assign first to avoid long multiplication overflow!
+ m_time = timet - WX_TIME_BASE_OFFSET;
+ m_time *= TIME_T_FACTOR;
+ }
return *this;
}
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);
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;
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;
}
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();
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();
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());
}
inline int wxTimeSpan::GetMinutes() const
{
- // 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());
+ // For compatibility, this method (and the other accessors) return int,
+ // even though GetLo() actually returns unsigned long with greater range.
+ return static_cast<int>((GetSeconds() / 60l).GetLo());
}
inline int wxTimeSpan::GetHours() const