X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/c3302e7e295593f89b6e355c921c9e16d8826d0f..9234615181cb1b63d509c2e16afb39b374d44987:/include/wx/datetime.h diff --git a/include/wx/datetime.h b/include/wx/datetime.h index 14c36c2d25..0198f242e7 100644 --- a/include/wx/datetime.h +++ b/include/wx/datetime.h @@ -7,48 +7,45 @@ // Created: 10.02.99 // RCS-ID: $Id$ // Copyright: (c) 1998 Vadim Zeitlin -// Licence: wxWindows license +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// #ifndef _WX_DATETIME_H #define _WX_DATETIME_H -#ifdef __GNUG__ +#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) #pragma interface "datetime.h" #endif +#include "wx/defs.h" + +#if wxUSE_DATETIME + +#ifndef __WXWINCE__ #include +#else +#include "wx/msw/wince/time.h" +#endif + #include // for INT_MIN #include "wx/longlong.h" -class WXDLLEXPORT wxDateTime; -class WXDLLEXPORT wxTimeSpan; -class WXDLLEXPORT wxDateSpan; +class WXDLLIMPEXP_BASE wxDateTime; +class WXDLLIMPEXP_BASE wxTimeSpan; +class WXDLLIMPEXP_BASE wxDateSpan; -// don't use inline functions in debug builds - we don't care about -// performances and this only leads to increased rebuild time (because every -// time an inline method is changed, all files including the header must be -// rebuilt) -// For Mingw32, causes a link error. -#if defined( __WXDEBUG__) && !defined(__MINGW32__) - #undef inline - #define inline -#endif // Debug +#include "wx/dynarray.h" // not all c-runtimes are based on 1/1/1970 being (time_t) 0 // set this to the corresponding value in seconds 1/1/1970 has on your // systems c-runtime -#ifdef __WXMAC__ -#if __MSL__ < 0x6000 +#if defined(__WXMAC__) && !defined(__DARWIN__) && __MSL__ < 0x6000 #define WX_TIME_BASE_OFFSET ( 2082844800L + 126144000L ) #else #define WX_TIME_BASE_OFFSET 0 #endif -#else - #define WX_TIME_BASE_OFFSET 0 -#endif /* * TODO * @@ -111,16 +108,19 @@ class WXDLLEXPORT wxDateSpan; // 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 WXDLLEXPORT wxDateTime; +class WXDLLIMPEXP_BASE wxDateTime; + +extern WXDLLIMPEXP_DATA_BASE(const wxChar*) wxDefaultDateTimeFormat; +extern WXDLLIMPEXP_DATA_BASE(const wxChar*) wxDefaultTimeSpanFormat; +extern WXDLLIMPEXP_DATA_BASE(const wxDateTime) wxDefaultDateTime; -WXDLLEXPORT_DATA(extern const wxDateTime&) wxDefaultDateTime; #define wxInvalidDateTime wxDefaultDateTime // ---------------------------------------------------------------------------- // wxDateTime represents an absolute moment in the time // ---------------------------------------------------------------------------- -class WXDLLEXPORT wxDateTime +class WXDLLIMPEXP_BASE wxDateTime { public: // types @@ -331,6 +331,12 @@ public: // day or not // // TODO move this to intl.h + +// Required for WinCE +#ifdef USA +#undef USA +#endif + enum Country { Country_Unknown, // no special information for this country @@ -350,10 +356,8 @@ public: Country_WesternEurope_End = UK, Russia, - USA }; - // symbolic names for the months enum Month { @@ -392,7 +396,7 @@ public: // a class representing a time zone: basicly, this is just an offset // (in seconds) from GMT - class WXDLLEXPORT TimeZone + class WXDLLIMPEXP_BASE TimeZone { public: TimeZone(TZ tz); @@ -412,7 +416,7 @@ public: // NB: this struct should always be kept normalized (i.e. mon should // be < 12, 1 <= day <= 31 &c), so use AddMonths(), AddDays() // instead of modifying the member fields directly! - struct WXDLLEXPORT Tm + struct WXDLLIMPEXP_BASE Tm { wxDateTime_t msec, sec, min, hour, mday; Month mon; @@ -462,7 +466,7 @@ public: // get the current country static Country GetCountry(); - // return TRUE if the country is a West European one (in practice, + // return true if the country is a West European one (in practice, // this means that the same DST rules as for EEC apply) static bool IsWestEuropeanCountry(Country country = Country_Default); @@ -478,7 +482,7 @@ public: // return the current month static Month GetCurrentMonth(Calendar cal = Gregorian); - // returns TRUE if the given year is a leap year in the given calendar + // returns true if the given year is a leap year in the given calendar static bool IsLeapYear(int year = Inv_Year, Calendar cal = Gregorian); // get the century (19 for 1999, 20 for 2000 and -5 for 492 BC) @@ -507,7 +511,7 @@ public: // get the AM and PM strings in the current locale (may be empty) static void GetAmPmStrings(wxString *am, wxString *pm); - // return TRUE if the given country uses DST for this year + // return true if the given country uses DST for this year static bool IsDSTApplicable(int year = Inv_Year, Country country = Country_Default); @@ -649,8 +653,10 @@ public: // ------------------------------------------------------------------------ // set to the given week day in the same week as this one - wxDateTime& SetToWeekDayInSameWeek(WeekDay weekday); - inline wxDateTime GetWeekDayInSameWeek(WeekDay weekday) const; + wxDateTime& SetToWeekDayInSameWeek(WeekDay weekday, + WeekFlags flags = Monday_First); + inline wxDateTime GetWeekDayInSameWeek(WeekDay weekday, + WeekFlags flags = Monday_First) const; // set to the next week day following this one wxDateTime& SetToNextWeekDay(WeekDay weekday); @@ -661,7 +667,7 @@ public: inline wxDateTime GetPrevWeekDay(WeekDay weekday) const; // set to Nth occurence of given weekday in the given month of the - // given year (time is set to 0), return TRUE on success and FALSE on + // 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()) bool SetToWeekDay(WeekDay weekday, @@ -682,10 +688,23 @@ public: int year = Inv_Year); // sets the date to the given day of the given week in the year, - // returns TRUE on success and FALSE if given date doesn't exist (e.g. + // returns true on success and false if given date doesn't exist (e.g. // numWeek is > 53) - bool SetToTheWeek(wxDateTime_t numWeek, WeekDay weekday = Mon); - inline wxDateTime GetWeek(wxDateTime_t numWeek, WeekDay weekday = Mon) const; + // + // these functions are badly defined as they're not the reverse of + // GetWeekOfYear(), use SetToTheWeekOfYear() instead + wxDEPRECATED( bool SetToTheWeek(wxDateTime_t numWeek, + WeekDay weekday = Mon, + WeekFlags flags = Monday_First) ); + wxDEPRECATED( wxDateTime GetWeek(wxDateTime_t numWeek, + WeekDay weekday = Mon, + WeekFlags flags = Monday_First) const ); + + // returns the date corresponding to the given week day of the given + // week (in ISO notation) of the specified year + static wxDateTime SetToWeekOfYear(int year, + wxDateTime_t numWeek, + WeekDay weekday = Mon); // sets the date to the last day of the given (or current) month or the // given (or current) year @@ -752,12 +771,12 @@ public: // ------------------------------------------------------------------------ // transform to any given timezone - inline wxDateTime ToTimezone(const TimeZone& tz, bool noDST = FALSE) const; - wxDateTime& MakeTimezone(const TimeZone& tz, bool noDST = FALSE); + inline wxDateTime ToTimezone(const TimeZone& tz, bool noDST = false) const; + wxDateTime& MakeTimezone(const TimeZone& tz, bool noDST = false); // transform to GMT/UTC - wxDateTime ToGMT(bool noDST = FALSE) const { return ToTimezone(GMT0, noDST); } - wxDateTime& MakeGMT(bool noDST = FALSE) { return MakeTimezone(GMT0, noDST); } + wxDateTime ToGMT(bool noDST = false) const { return ToTimezone(GMT0, noDST); } + wxDateTime& MakeGMT(bool noDST = false) { return MakeTimezone(GMT0, noDST); } // is daylight savings time in effect at this moment according to the // rules of the specified country? @@ -835,23 +854,32 @@ public: // adoption of the Gregorian calendar is simply unknown. bool IsGregorianDate(GregorianAdoption country = Gr_Standard) const; + // dos date and time format + // ------------------------------------------------------------------------ + + // set from the DOS packed format + wxDateTime& SetFromDOS(unsigned long ddt); + + // pack the date in DOS format + unsigned long GetAsDOS() const; + // comparison (see also functions below for operator versions) // ------------------------------------------------------------------------ - // returns TRUE if the two moments are strictly identical + // returns true if the two moments are strictly identical inline bool IsEqualTo(const wxDateTime& datetime) const; - // returns TRUE if the date is strictly earlier than the given one + // returns true if the date is strictly earlier than the given one inline bool IsEarlierThan(const wxDateTime& datetime) const; - // returns TRUE if the date is strictly later than the given one + // returns true if the date is strictly later than the given one inline bool IsLaterThan(const wxDateTime& datetime) const; - // returns TRUE if the date is strictly in the given range + // returns true if the date is strictly in the given range inline bool IsStrictlyBetween(const wxDateTime& t1, const wxDateTime& t2) const; - // returns TRUE if the date is in the given range + // returns true if the date is in the given range inline bool IsBetween(const wxDateTime& t1, const wxDateTime& t2) const; // do these two objects refer to the same date? @@ -863,6 +891,42 @@ public: // are these two objects equal up to given timespan? inline bool IsEqualUpTo(const wxDateTime& dt, const wxTimeSpan& ts) const; + inline bool operator<(const wxDateTime& dt) const + { + wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") ); + return GetValue() < dt.GetValue(); + } + + inline bool operator<=(const wxDateTime& dt) const + { + wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") ); + return GetValue() <= dt.GetValue(); + } + + inline bool operator>(const wxDateTime& dt) const + { + wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") ); + return GetValue() > dt.GetValue(); + } + + inline bool operator>=(const wxDateTime& dt) const + { + wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") ); + return GetValue() >= dt.GetValue(); + } + + inline bool operator==(const wxDateTime& dt) const + { + wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") ); + return GetValue() == dt.GetValue(); + } + + inline bool operator!=(const wxDateTime& dt) const + { + wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") ); + return GetValue() != dt.GetValue(); + } + // arithmetics with dates (see also below for more operators) // ------------------------------------------------------------------------ @@ -872,6 +936,12 @@ public: inline wxDateTime& Add(const wxTimeSpan& diff); // add a time span (positive or negative) inline wxDateTime& operator+=(const wxTimeSpan& diff); + inline wxDateTime operator+(const wxTimeSpan& ts) const + { + wxDateTime dt(*this); + dt.Add(ts); + return dt; + } // return the difference of the date with a time span inline wxDateTime Subtract(const wxTimeSpan& diff) const; @@ -879,6 +949,12 @@ public: inline wxDateTime& Subtract(const wxTimeSpan& diff); // subtract a time span (positive or negative) inline wxDateTime& operator-=(const wxTimeSpan& diff); + inline wxDateTime operator-(const wxTimeSpan& ts) const + { + wxDateTime dt(*this); + dt.Subtract(ts); + return dt; + } // return the sum of the date with a date span inline wxDateTime Add(const wxDateSpan& diff) const; @@ -886,6 +962,12 @@ public: wxDateTime& Add(const wxDateSpan& diff); // add a date span (positive or negative) inline wxDateTime& operator+=(const wxDateSpan& diff); + inline wxDateTime operator+(const wxDateSpan& ds) const + { + wxDateTime dt(*this); + dt.Add(ds); + return dt; + } // return the difference of the date with a date span inline wxDateTime Subtract(const wxDateSpan& diff) const; @@ -893,9 +975,16 @@ public: inline wxDateTime& Subtract(const wxDateSpan& diff); // subtract a date span (positive or negative) inline wxDateTime& operator-=(const wxDateSpan& diff); + inline wxDateTime operator-(const wxDateSpan& ds) const + { + wxDateTime dt(*this); + dt.Subtract(ds); + return dt; + } // return the difference between two dates 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 @@ -910,7 +999,7 @@ public: // 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 = _T("%c"), + const wxChar *format = wxDefaultDateTimeFormat, const wxDateTime& dateDef = wxDefaultDateTime); // parse a string containing the date/time in "free" format, this // function will try to make an educated guess at the string contents @@ -925,7 +1014,7 @@ public: // 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 = _T("%c"), + wxString Format(const wxChar *format = wxDefaultDateTimeFormat, const TimeZone& tz = Local) const; // preferred date representation for the current locale wxString FormatDate() const { return Format(_T("%x")); } @@ -968,7 +1057,7 @@ private: // fixed to 1000 static const long TIME_T_FACTOR; - // returns TRUE if we fall in range in which we can use standard ANSI C + // returns true if we fall in range in which we can use standard ANSI C // functions inline bool IsInStdRange() const; @@ -984,7 +1073,7 @@ private: // objects of that class. See also wxDateSpan. // ---------------------------------------------------------------------------- -class WXDLLEXPORT wxTimeSpan +class WXDLLIMPEXP_BASE wxTimeSpan { public: // constructors @@ -1034,6 +1123,10 @@ public: inline wxTimeSpan& Add(const wxTimeSpan& diff); // add two timespans together wxTimeSpan& operator+=(const wxTimeSpan& diff) { return Add(diff); } + inline wxTimeSpan operator+(const wxTimeSpan& ts) const + { + return wxTimeSpan(GetValue() + ts.GetValue()); + } // return the difference of two timespans inline wxTimeSpan Subtract(const wxTimeSpan& diff) const; @@ -1041,6 +1134,10 @@ public: inline wxTimeSpan& Subtract(const wxTimeSpan& diff); // subtract another timespan wxTimeSpan& operator-=(const wxTimeSpan& diff) { return Subtract(diff); } + inline wxTimeSpan operator-(const wxTimeSpan& ts) + { + return wxTimeSpan(GetValue() - ts.GetValue()); + } // multiply timespan by a scalar inline wxTimeSpan Multiply(int n) const; @@ -1048,6 +1145,10 @@ public: inline wxTimeSpan& Multiply(int n); // multiply timespan by a scalar wxTimeSpan& operator*=(int n) { return Multiply(n); } + inline wxTimeSpan operator*(int n) const + { + return wxTimeSpan(*this).Multiply(n); + } // return this timespan with inversed sign wxTimeSpan Negate() const { return wxTimeSpan(-GetValue()); } @@ -1080,14 +1181,44 @@ public: // are two timespans equal? inline bool IsEqualTo(const wxTimeSpan& ts) const; // compare two timestamps: works with the absolute values, i.e. -2 - // hours is longer than 1 hour. Also, it will return FALSE if the + // hours is longer than 1 hour. Also, it will return false if the // timespans are equal in absolute value. inline bool IsLongerThan(const wxTimeSpan& ts) const; // compare two timestamps: works with the absolute values, i.e. 1 - // hour is shorter than -2 hours. Also, it will return FALSE if the + // 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); } + inline bool operator<(const wxTimeSpan &ts) const + { + return GetValue() < ts.GetValue(); + } + + inline bool operator<=(const wxTimeSpan &ts) const + { + return GetValue() <= ts.GetValue(); + } + + inline bool operator>(const wxTimeSpan &ts) const + { + return GetValue() > ts.GetValue(); + } + + inline bool operator>=(const wxTimeSpan &ts) const + { + return GetValue() >= ts.GetValue(); + } + + inline bool operator==(const wxTimeSpan &ts) const + { + return GetValue() == ts.GetValue(); + } + + inline bool operator!=(const wxTimeSpan &ts) const + { + return GetValue() != ts.GetValue(); + } + // breaking into days, hours, minutes and seconds // ------------------------------------------------------------------------ @@ -1113,7 +1244,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 = _T("%H:%M:%S")) const; + wxString Format(const wxChar *format = wxDefaultTimeSpanFormat) const; // implementation // ------------------------------------------------------------------------ @@ -1153,12 +1284,18 @@ private: // Beware about weeks: if you specify both weeks and days, the total number of // days added will be 7*weeks + days! See also GetTotalDays() function. // +// Equality operators are defined for wxDateSpans. Two datespans are equal if +// they both give the same target date when added to *every* source date. +// Thus wxDateSpan::Months(1) is not equal to wxDateSpan::Days(30), because +// they not give the same date when added to 1 Feb. But wxDateSpan::Days(14) is +// equal to wxDateSpan::Weeks(2) +// // Finally, notice that for adding hours, minutes &c you don't need this // class: wxTimeSpan will do the job because there are no subtleties // associated with those. // ---------------------------------------------------------------------------- -class WXDLLEXPORT wxDateSpan +class WXDLLIMPEXP_BASE wxDateSpan { public: // constructors @@ -1225,6 +1362,13 @@ public: inline wxDateSpan& Add(const wxDateSpan& other); // add another wxDateSpan to us inline wxDateSpan& operator+=(const wxDateSpan& other); + inline wxDateSpan operator+(const wxDateSpan& ds) const + { + return wxDateSpan(GetYears() + ds.GetYears(), + GetMonths() + ds.GetMonths(), + GetWeeks() + ds.GetWeeks(), + GetDays() + ds.GetDays()); + } // return difference of two date spans inline wxDateSpan Subtract(const wxDateSpan& other) const; @@ -1232,6 +1376,13 @@ public: inline wxDateSpan& Subtract(const wxDateSpan& other); // subtract another wxDateSpan from us inline wxDateSpan& operator-=(const wxDateSpan& other); + inline wxDateSpan operator-(const wxDateSpan& ds) const + { + return wxDateSpan(GetYears() - ds.GetYears(), + GetMonths() - ds.GetMonths(), + GetWeeks() - ds.GetWeeks(), + GetDays() - ds.GetDays()); + } // return a copy of this time span with changed sign inline wxDateSpan Negate() const; @@ -1246,6 +1397,23 @@ public: inline wxDateSpan& Multiply(int factor); // multiply all components by a (signed) number inline wxDateSpan& operator*=(int factor) { return Multiply(factor); } + inline wxDateSpan operator*(int n) const + { + return wxDateSpan(*this).Multiply(n); + } + + // ds1 == d2 if and only if for every wxDateTime t t + ds1 == t + ds2 + inline bool operator==(const wxDateSpan& ds) const + { + return GetYears() == ds.GetYears() && + GetMonths() == ds.GetMonths() && + GetTotalDays() == ds.GetTotalDays(); + } + + inline bool operator!=(const wxDateSpan& ds) const + { + return !(*this == ds); + } private: int m_years, @@ -1258,9 +1426,7 @@ private: // wxDateTimeArray: array of dates. // ---------------------------------------------------------------------------- -#include "wx/dynarray.h" - -WX_DECLARE_EXPORTED_OBJARRAY(wxDateTime, wxDateTimeArray); +WX_DECLARE_USER_EXPORTED_OBJARRAY(wxDateTime, wxDateTimeArray, WXDLLIMPEXP_BASE); // ---------------------------------------------------------------------------- // wxDateTimeHolidayAuthority: an object of this class will decide whether a @@ -1271,15 +1437,17 @@ WX_DECLARE_EXPORTED_OBJARRAY(wxDateTime, wxDateTimeArray); // virtual methods to work with the holidays they correspond to. // ---------------------------------------------------------------------------- -class WXDLLEXPORT wxDateTimeHolidayAuthority; -WX_DEFINE_EXPORTED_ARRAY(wxDateTimeHolidayAuthority *, wxHolidayAuthoritiesArray); +class WXDLLIMPEXP_BASE wxDateTimeHolidayAuthority; +WX_DEFINE_USER_EXPORTED_ARRAY_PTR(wxDateTimeHolidayAuthority *, + wxHolidayAuthoritiesArray, + class WXDLLIMPEXP_BASE); class wxDateTimeHolidaysModule; -class WXDLLEXPORT wxDateTimeHolidayAuthority +class WXDLLIMPEXP_BASE wxDateTimeHolidayAuthority { friend class wxDateTimeHolidaysModule; public: - // returns TRUE if the given date is a holiday + // returns true if the given date is a holiday static bool IsHoliday(const wxDateTime& dt); // fills the provided array with all holidays in the given range, returns @@ -1295,6 +1463,9 @@ public: // wxDateTimeHolidayAuthority) static void AddAuthority(wxDateTimeHolidayAuthority *auth); + // the base class must have a virtual dtor + virtual ~wxDateTimeHolidayAuthority(); + protected: // this function is called to determine whether a given day is a holiday virtual bool DoIsHoliday(const wxDateTime& dt) const = 0; @@ -1317,7 +1488,7 @@ private: }; // the holidays for this class are all Saturdays and Sundays -class WXDLLEXPORT wxDateTimeWorkDays : public wxDateTimeHolidayAuthority +class WXDLLIMPEXP_BASE wxDateTimeWorkDays : public wxDateTimeHolidayAuthority { protected: virtual bool DoIsHoliday(const wxDateTime& dt) const; @@ -1330,244 +1501,562 @@ protected: // inline functions implementation // ============================================================================ -// don't include inline functions definitions when we're included from anything -// else than datetime.cpp in debug builds: this minimizes rebuilds if we change -// some inline function and the performance doesn't matter in the debug builds. +// ---------------------------------------------------------------------------- +// private macros +// ---------------------------------------------------------------------------- -#if !defined(__WXDEBUG__) || defined(wxDEFINE_TIME_CONSTANTS) - #define INCLUDED_FROM_WX_DATETIME_H - #include "wx/datetime.inl" - #undef INCLUDED_FROM_WX_DATETIME_H +#define MILLISECONDS_PER_DAY 86400000l + +// some broken compilers (HP-UX CC) refuse to compile the "normal" version, but +// using a temp variable always might prevent other compilers from optimising +// it away - hence use of this ugly macro +#ifndef __HPUX__ + #define MODIFY_AND_RETURN(op) return wxDateTime(*this).op +#else + #define MODIFY_AND_RETURN(op) wxDateTime dt(*this); dt.op; return dt #endif -// if we defined it to be empty above, restore it now -#undef inline +// ---------------------------------------------------------------------------- +// wxDateTime construction +// ---------------------------------------------------------------------------- -// ============================================================================ -// binary operators -// ============================================================================ +inline bool wxDateTime::IsInStdRange() const +{ + return m_time >= 0l && (m_time / TIME_T_FACTOR) < LONG_MAX; +} + +/* static */ +inline wxDateTime wxDateTime::Now() +{ + return wxDateTime(*GetTmNow()); +} + +/* static */ +inline wxDateTime wxDateTime::Today() +{ + wxDateTime dt(Now()); + dt.ResetTime(); + + return dt; +} + +#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; + + return *this; +} +#endif + +inline wxDateTime& wxDateTime::SetToCurrent() +{ + *this = Now(); + return *this; +} + +#if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400)) +inline wxDateTime::wxDateTime(time_t timet) +{ + Set(timet); +} +#endif + +inline wxDateTime::wxDateTime(const struct tm& tm) +{ + Set(tm); +} + +inline wxDateTime::wxDateTime(const Tm& tm) +{ + Set(tm); +} + +inline wxDateTime::wxDateTime(double jdn) +{ + Set(jdn); +} + +inline wxDateTime& wxDateTime::Set(const Tm& tm) +{ + wxASSERT_MSG( tm.IsValid(), _T("invalid broken down date/time") ); + + return Set(tm.mday, (Month)tm.mon, tm.year, tm.hour, tm.min, tm.sec); +} + +inline wxDateTime::wxDateTime(wxDateTime_t hour, + wxDateTime_t minute, + wxDateTime_t second, + wxDateTime_t millisec) +{ + Set(hour, minute, second, millisec); +} + +inline wxDateTime::wxDateTime(wxDateTime_t day, + Month month, + int year, + wxDateTime_t hour, + wxDateTime_t minute, + wxDateTime_t second, + wxDateTime_t millisec) +{ + Set(day, month, year, hour, minute, second, millisec); +} // ---------------------------------------------------------------------------- -// wxDateTime operators +// wxDateTime accessors // ---------------------------------------------------------------------------- -// arithmetics -// ----------- +inline wxLongLong wxDateTime::GetValue() const +{ + wxASSERT_MSG( IsValid(), _T("invalid wxDateTime")); + + return m_time; +} -// no need to check for validity - the member functions we call will do it +inline time_t wxDateTime::GetTicks() const +{ + wxASSERT_MSG( IsValid(), _T("invalid wxDateTime")); + if ( !IsInStdRange() ) + { + return (time_t)-1; + } -inline wxDateTime WXDLLEXPORT operator+(const wxDateTime& dt, - const wxTimeSpan& ts) + return (time_t)((m_time / (long)TIME_T_FACTOR).GetLo())+WX_TIME_BASE_OFFSET ; +} + +inline bool wxDateTime::SetToLastWeekDay(WeekDay weekday, + Month month, + int year) { - return dt.Add(ts); + return SetToWeekDay(weekday, -1, month, year); } -inline wxDateTime WXDLLEXPORT operator-(const wxDateTime& dt, - const wxTimeSpan& ts) +inline wxDateTime +wxDateTime::GetWeekDayInSameWeek(WeekDay weekday, + WeekFlags WXUNUSED(flags)) const { - return dt.Subtract(ts); + MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday) ); } -inline wxDateTime WXDLLEXPORT operator+(const wxDateTime& dt, - const wxDateSpan& ds) +inline wxDateTime wxDateTime::GetNextWeekDay(WeekDay weekday) const { - return dt.Add(ds); + MODIFY_AND_RETURN( SetToNextWeekDay(weekday) ); } -inline wxDateTime WXDLLEXPORT operator-(const wxDateTime& dt, - const wxDateSpan& ds) +inline wxDateTime wxDateTime::GetPrevWeekDay(WeekDay weekday) const { - return dt.Subtract(ds); + MODIFY_AND_RETURN( SetToPrevWeekDay(weekday) ); } -inline wxTimeSpan WXDLLEXPORT operator-(const wxDateTime& dt1, - const wxDateTime& dt2) +inline wxDateTime wxDateTime::GetWeekDay(WeekDay weekday, + int n, + Month month, + int year) const { - return dt1.Subtract(dt2); + wxDateTime dt(*this); + + return dt.SetToWeekDay(weekday, n, month, year) ? dt : wxInvalidDateTime; } -// comparison -// ---------- +inline wxDateTime wxDateTime::GetLastWeekDay(WeekDay weekday, + Month month, + int year) +{ + wxDateTime dt(*this); + + return dt.SetToLastWeekDay(weekday, month, year) ? dt : wxInvalidDateTime; +} -inline bool WXDLLEXPORT operator<(const wxDateTime& t1, const wxDateTime& t2) +inline wxDateTime wxDateTime::GetLastMonthDay(Month month, int year) const { - wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") ); + MODIFY_AND_RETURN( SetToLastMonthDay(month, year) ); +} - return t1.GetValue() < t2.GetValue(); +inline wxDateTime wxDateTime::GetYearDay(wxDateTime_t yday) const +{ + MODIFY_AND_RETURN( SetToYearDay(yday) ); } -inline bool WXDLLEXPORT operator<=(const wxDateTime& t1, const wxDateTime& t2) +// ---------------------------------------------------------------------------- +// wxDateTime comparison +// ---------------------------------------------------------------------------- + +inline bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const { - wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") ); + wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime")); - return t1.GetValue() <= t2.GetValue(); + return m_time == datetime.m_time; } -inline bool WXDLLEXPORT operator>(const wxDateTime& t1, const wxDateTime& t2) +inline bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const { - wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") ); + wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime")); - return t1.GetValue() > t2.GetValue(); + return m_time < datetime.m_time; } -inline bool WXDLLEXPORT operator>=(const wxDateTime& t1, const wxDateTime& t2) +inline bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const { - wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") ); + wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime")); - return t1.GetValue() >= t2.GetValue(); + return m_time > datetime.m_time; } -inline bool WXDLLEXPORT operator==(const wxDateTime& t1, const wxDateTime& t2) +inline bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1, + const wxDateTime& t2) const { - wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") ); + // no need for assert, will be checked by the functions we call + return IsLaterThan(t1) && IsEarlierThan(t2); +} - return t1.GetValue() == t2.GetValue(); +inline bool wxDateTime::IsBetween(const wxDateTime& t1, + const wxDateTime& t2) const +{ + // no need for assert, will be checked by the functions we call + return IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2); } -inline bool WXDLLEXPORT operator!=(const wxDateTime& t1, const wxDateTime& t2) +inline bool wxDateTime::IsSameDate(const wxDateTime& dt) const { - wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") ); + Tm tm1 = GetTm(), + tm2 = dt.GetTm(); - return t1.GetValue() != t2.GetValue(); + return tm1.year == tm2.year && + tm1.mon == tm2.mon && + tm1.mday == tm2.mday; +} + +inline bool wxDateTime::IsSameTime(const wxDateTime& dt) const +{ + // notice that we can't do something like this: + // + // m_time % MILLISECONDS_PER_DAY == dt.m_time % MILLISECONDS_PER_DAY + // + // because we have also to deal with (possibly) different DST settings! + Tm tm1 = GetTm(), + tm2 = dt.GetTm(); + + return tm1.hour == tm2.hour && + tm1.min == tm2.min && + tm1.sec == tm2.sec && + tm1.msec == tm2.msec; +} + +inline bool wxDateTime::IsEqualUpTo(const wxDateTime& dt, + const wxTimeSpan& ts) const +{ + return IsBetween(dt.Subtract(ts), dt.Add(ts)); } // ---------------------------------------------------------------------------- -// wxTimeSpan operators +// wxDateTime arithmetics // ---------------------------------------------------------------------------- -// arithmetics -// ----------- +inline wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const +{ + wxASSERT_MSG( IsValid(), _T("invalid wxDateTime")); + + return wxDateTime(m_time + diff.GetValue()); +} + +inline wxDateTime& wxDateTime::Add(const wxTimeSpan& diff) +{ + wxASSERT_MSG( IsValid(), _T("invalid wxDateTime")); + + m_time += diff.GetValue(); + + return *this; +} + +inline wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff) +{ + return Add(diff); +} -inline wxTimeSpan WXDLLEXPORT operator+(const wxTimeSpan& ts1, - const wxTimeSpan& ts2) +inline wxDateTime wxDateTime::Subtract(const wxTimeSpan& diff) const { - return wxTimeSpan(ts1.GetValue() + ts2.GetValue()); + wxASSERT_MSG( IsValid(), _T("invalid wxDateTime")); + + return wxDateTime(m_time - diff.GetValue()); } -inline wxTimeSpan WXDLLEXPORT operator-(const wxTimeSpan& ts1, - const wxTimeSpan& ts2) +inline wxDateTime& wxDateTime::Subtract(const wxTimeSpan& diff) { - return wxTimeSpan(ts1.GetValue() - ts2.GetValue()); + wxASSERT_MSG( IsValid(), _T("invalid wxDateTime")); + + m_time -= diff.GetValue(); + + return *this; } -inline wxTimeSpan WXDLLEXPORT operator*(const wxTimeSpan& ts, int n) +inline wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff) { - return wxTimeSpan(ts).Multiply(n); + return Subtract(diff); } -inline wxTimeSpan WXDLLEXPORT operator*(int n, const wxTimeSpan& ts) +inline wxTimeSpan wxDateTime::Subtract(const wxDateTime& datetime) const { - return wxTimeSpan(ts).Multiply(n); + wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime")); + + return wxTimeSpan(GetValue() - datetime.GetValue()); } -// comparison -// ---------- +inline wxTimeSpan wxDateTime::operator-(const wxDateTime& dt2) const +{ + return this->Subtract(dt2); +} -inline bool WXDLLEXPORT operator<(const wxTimeSpan &t1, const wxTimeSpan &t2) +inline wxDateTime wxDateTime::Add(const wxDateSpan& diff) const { - return t1.GetValue() < t2.GetValue(); + return wxDateTime(*this).Add(diff); } -inline bool WXDLLEXPORT operator<=(const wxTimeSpan &t1, const wxTimeSpan &t2) +inline wxDateTime& wxDateTime::Subtract(const wxDateSpan& diff) { - return t1.GetValue() <= t2.GetValue(); + return Add(diff.Negate()); } -inline bool WXDLLEXPORT operator>(const wxTimeSpan &t1, const wxTimeSpan &t2) +inline wxDateTime wxDateTime::Subtract(const wxDateSpan& diff) const { - return t1.GetValue() > t2.GetValue(); + return wxDateTime(*this).Subtract(diff); } -inline bool WXDLLEXPORT operator>=(const wxTimeSpan &t1, const wxTimeSpan &t2) +inline wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff) { - return t1.GetValue() >= t2.GetValue(); + return Subtract(diff); } -inline bool WXDLLEXPORT operator==(const wxTimeSpan &t1, const wxTimeSpan &t2) +inline wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff) { - return t1.GetValue() == t2.GetValue(); + return Add(diff); } -inline bool WXDLLEXPORT operator!=(const wxTimeSpan &t1, const wxTimeSpan &t2) +// ---------------------------------------------------------------------------- +// wxDateTime and timezones +// ---------------------------------------------------------------------------- + +inline wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz, + bool noDST) const { - return t1.GetValue() != t2.GetValue(); + MODIFY_AND_RETURN( MakeTimezone(tz, noDST) ); } // ---------------------------------------------------------------------------- -// wxDateSpan +// wxTimeSpan construction // ---------------------------------------------------------------------------- -// arithmetics -// ----------- +inline wxTimeSpan::wxTimeSpan(long hours, + long minutes, + long seconds, + long milliseconds) +{ + // assign first to avoid precision loss + m_diff = hours; + m_diff *= 60l; + m_diff += minutes; + m_diff *= 60l; + m_diff += seconds; + m_diff *= 1000l; + m_diff += milliseconds; +} -inline WXDLLEXPORT wxDateSpan operator+(const wxDateSpan& ds1, - const wxDateSpan& ds2) +// ---------------------------------------------------------------------------- +// wxTimeSpan accessors +// ---------------------------------------------------------------------------- + +inline wxLongLong wxTimeSpan::GetSeconds() const { - return wxDateSpan(ds1.GetYears() + ds2.GetYears(), - ds1.GetMonths() + ds2.GetMonths(), - ds1.GetWeeks() + ds2.GetWeeks(), - ds1.GetDays() + ds2.GetDays()); + return m_diff / 1000l; } -inline WXDLLEXPORT wxDateSpan operator-(const wxDateSpan& ds1, - const wxDateSpan& ds2) +inline int wxTimeSpan::GetMinutes() const { - return wxDateSpan(ds1.GetYears() - ds2.GetYears(), - ds1.GetMonths() - ds2.GetMonths(), - ds1.GetWeeks() - ds2.GetWeeks(), - ds1.GetDays() - ds2.GetDays()); + return (GetSeconds() / 60l).GetLo(); } -inline WXDLLEXPORT wxDateSpan operator*(const wxDateSpan& ds, int n) +inline int wxTimeSpan::GetHours() const { - return wxDateSpan(ds).Multiply(n); + return GetMinutes() / 60; } -inline WXDLLEXPORT wxDateSpan operator*(int n, const wxDateSpan& ds) +inline int wxTimeSpan::GetDays() const { - return wxDateSpan(ds).Multiply(n); + return GetHours() / 24; } -// ============================================================================ -// other helper functions -// ============================================================================ +inline int wxTimeSpan::GetWeeks() const +{ + return GetDays() / 7; +} // ---------------------------------------------------------------------------- -// iteration helpers: can be used to write a for loop over enum variable like -// this: -// for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) ) +// wxTimeSpan arithmetics // ---------------------------------------------------------------------------- -inline WXDLLEXPORT void wxNextMonth(wxDateTime::Month& m) +inline wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const { - wxASSERT_MSG( m < wxDateTime::Inv_Month, _T("invalid month") ); + return wxTimeSpan(m_diff + diff.GetValue()); +} - // no wrapping or the for loop above would never end! - m = (wxDateTime::Month)(m + 1); +inline wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff) +{ + m_diff += diff.GetValue(); + + return *this; +} + +inline wxTimeSpan wxTimeSpan::Subtract(const wxTimeSpan& diff) const +{ + return wxTimeSpan(m_diff - diff.GetValue()); } -inline WXDLLEXPORT void wxPrevMonth(wxDateTime::Month& m) +inline wxTimeSpan& wxTimeSpan::Subtract(const wxTimeSpan& diff) { - wxASSERT_MSG( m < wxDateTime::Inv_Month, _T("invalid month") ); + m_diff -= diff.GetValue(); - m = m == wxDateTime::Jan ? wxDateTime::Inv_Month - : (wxDateTime::Month)(m - 1); + return *this; } -inline WXDLLEXPORT void wxNextWDay(wxDateTime::WeekDay& wd) +inline wxTimeSpan& wxTimeSpan::Multiply(int n) { - wxASSERT_MSG( wd < wxDateTime::Inv_WeekDay, _T("invalid week day") ); + m_diff *= (long)n; - // no wrapping or the for loop above would never end! - wd = (wxDateTime::WeekDay)(wd + 1); + return *this; } -inline WXDLLEXPORT void wxPrevWDay(wxDateTime::WeekDay& wd) +inline wxTimeSpan wxTimeSpan::Multiply(int n) const { - wxASSERT_MSG( wd < wxDateTime::Inv_WeekDay, _T("invalid week day") ); + return wxTimeSpan(m_diff * (long)n); +} - wd = wd == wxDateTime::Sun ? wxDateTime::Inv_WeekDay - : (wxDateTime::WeekDay)(wd - 1); +inline wxTimeSpan wxTimeSpan::Abs() const +{ + return wxTimeSpan(GetValue().Abs()); } +inline bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const +{ + return GetValue() == ts.GetValue(); +} + +inline bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const +{ + return GetValue().Abs() > ts.GetValue().Abs(); +} + +// ---------------------------------------------------------------------------- +// wxDateSpan +// ---------------------------------------------------------------------------- + +inline wxDateSpan& wxDateSpan::operator+=(const wxDateSpan& other) +{ + m_years += other.m_years; + m_months += other.m_months; + m_weeks += other.m_weeks; + m_days += other.m_days; + + return *this; +} + +inline wxDateSpan& wxDateSpan::Add(const wxDateSpan& other) +{ + return *this += other; +} + +inline wxDateSpan wxDateSpan::Add(const wxDateSpan& other) const +{ + wxDateSpan ds(*this); + ds.Add(other); + return ds; +} + +inline wxDateSpan& wxDateSpan::Multiply(int factor) +{ + m_years *= factor; + m_months *= factor; + m_weeks *= factor; + m_days *= factor; + + return *this; +} + +inline wxDateSpan wxDateSpan::Multiply(int factor) const +{ + wxDateSpan ds(*this); + ds.Multiply(factor); + return ds; +} + +inline wxDateSpan wxDateSpan::Negate() const +{ + return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days); +} + +inline wxDateSpan& wxDateSpan::Neg() +{ + m_years = -m_years; + m_months = -m_months; + m_weeks = -m_weeks; + m_days = -m_days; + + return *this; +} + +inline wxDateSpan& wxDateSpan::operator-=(const wxDateSpan& other) +{ + return *this += other.Negate(); +} + +inline wxDateSpan& wxDateSpan::Subtract(const wxDateSpan& other) +{ + return *this -= other; +} + +inline wxDateSpan wxDateSpan::Subtract(const wxDateSpan& other) const +{ + wxDateSpan ds(*this); + ds.Subtract(other); + return ds; +} + +#undef MILLISECONDS_PER_DAY + +#undef MODIFY_AND_RETURN + +// ============================================================================ +// binary operators +// ============================================================================ + +// ---------------------------------------------------------------------------- +// wxTimeSpan operators +// ---------------------------------------------------------------------------- + +wxTimeSpan WXDLLIMPEXP_BASE operator*(int n, const wxTimeSpan& ts); + +// ---------------------------------------------------------------------------- +// wxDateSpan +// ---------------------------------------------------------------------------- + +wxDateSpan WXDLLIMPEXP_BASE operator*(int n, const wxDateSpan& ds); + +// ============================================================================ +// other helper functions +// ============================================================================ + +// ---------------------------------------------------------------------------- +// iteration helpers: can be used to write a for loop over enum variable like +// this: +// for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) ) +// ---------------------------------------------------------------------------- + +WXDLLIMPEXP_BASE void wxNextMonth(wxDateTime::Month& m); +WXDLLIMPEXP_BASE void wxPrevMonth(wxDateTime::Month& m); +WXDLLIMPEXP_BASE void wxNextWDay(wxDateTime::WeekDay& wd); +WXDLLIMPEXP_BASE void wxPrevWDay(wxDateTime::WeekDay& wd); + +#endif // wxUSE_DATETIME + #endif // _WX_DATETIME_H