X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/9c2882d9e1b17c600a196e1b69fbe66b98204151..a315cec9685964e73d833f4c8cf6b38828780899:/include/wx/datetime.inl diff --git a/include/wx/datetime.inl b/include/wx/datetime.inl index 9c0dec16a3..8ec379dd9d 100644 --- a/include/wx/datetime.inl +++ b/include/wx/datetime.inl @@ -16,8 +16,21 @@ #error "This file is only included by wx/datetime.h, don't include it manually!" #endif +// ---------------------------------------------------------------------------- +// private macros +// ---------------------------------------------------------------------------- + #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 + // ---------------------------------------------------------------------------- // wxDateTime construction // ---------------------------------------------------------------------------- @@ -27,79 +40,89 @@ const long wxDateTime::TIME_T_FACTOR = 1000l; #endif // wxDEFINE_TIME_CONSTANTS -bool wxDateTime::IsInStdRange() const +inline bool wxDateTime::IsInStdRange() const { return m_time >= 0l && (m_time / TIME_T_FACTOR) < LONG_MAX; } /* static */ -wxDateTime wxDateTime::Now() +inline wxDateTime wxDateTime::Now() { - return wxDateTime(GetTimeNow()); + return wxDateTime(*GetTmNow()); } /* static */ -wxDateTime wxDateTime::Today() +inline wxDateTime wxDateTime::Today() { - return wxDateTime((time_t)(86400*(GetTimeNow() / 86400))); + struct tm *time = GetTmNow(); + time->tm_hour = 0; + time->tm_min = 0; + time->tm_sec = 0; + + return wxDateTime(*time); } -wxDateTime& wxDateTime::Set(time_t timet) +#if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400)) +inline wxDateTime& wxDateTime::Set(time_t timet) { // assign first to avoid long multiplication overflow! - m_time = timet; + m_time = timet - WX_TIME_BASE_OFFSET ; m_time *= TIME_T_FACTOR; return *this; } +#endif -wxDateTime& wxDateTime::SetToCurrent() +inline wxDateTime& wxDateTime::SetToCurrent() { - return Set(GetTimeNow()); + *this = Now(); + return *this; } -wxDateTime::wxDateTime(time_t timet) +#if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400)) +inline wxDateTime::wxDateTime(time_t timet) { Set(timet); } +#endif -wxDateTime::wxDateTime(const struct tm& tm) +inline wxDateTime::wxDateTime(const struct tm& tm) { Set(tm); } -wxDateTime::wxDateTime(const Tm& tm) +inline wxDateTime::wxDateTime(const Tm& tm) { Set(tm); } -wxDateTime::wxDateTime(double jdn) +inline wxDateTime::wxDateTime(double jdn) { Set(jdn); } -wxDateTime& wxDateTime::Set(const Tm& tm) +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); } -wxDateTime::wxDateTime(wxDateTime_t hour, - wxDateTime_t minute, - wxDateTime_t second, - wxDateTime_t millisec) +inline wxDateTime::wxDateTime(wxDateTime_t hour, + wxDateTime_t minute, + wxDateTime_t second, + wxDateTime_t millisec) { Set(hour, minute, second, millisec); } -wxDateTime::wxDateTime(wxDateTime_t day, - Month month, - int year, - wxDateTime_t hour, - wxDateTime_t minute, - wxDateTime_t second, - wxDateTime_t 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); } @@ -108,14 +131,14 @@ wxDateTime::wxDateTime(wxDateTime_t day, // wxDateTime accessors // ---------------------------------------------------------------------------- -wxLongLong wxDateTime::GetValue() const +inline wxLongLong wxDateTime::GetValue() const { wxASSERT_MSG( IsValid(), _T("invalid wxDateTime")); return m_time; } -time_t wxDateTime::GetTicks() const +inline time_t wxDateTime::GetTicks() const { wxASSERT_MSG( IsValid(), _T("invalid wxDateTime")); if ( !IsInStdRange() ) @@ -123,60 +146,118 @@ time_t wxDateTime::GetTicks() const return (time_t)-1; } - return (time_t)((m_time / (long)TIME_T_FACTOR).GetLo()); + return (time_t)((m_time / (long)TIME_T_FACTOR).GetLo())+WX_TIME_BASE_OFFSET ; } -bool wxDateTime::SetToLastWeekDay(WeekDay weekday, - Month month, - int year) +inline bool wxDateTime::SetToLastWeekDay(WeekDay weekday, + Month month, + int year) { return SetToWeekDay(weekday, -1, month, year); } +inline wxDateTime wxDateTime::GetWeekDayInSameWeek(WeekDay weekday) const +{ + MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday) ); +} + +inline wxDateTime wxDateTime::GetNextWeekDay(WeekDay weekday) const +{ + MODIFY_AND_RETURN( SetToNextWeekDay(weekday) ); +} + +inline wxDateTime wxDateTime::GetPrevWeekDay(WeekDay weekday) const +{ + MODIFY_AND_RETURN( SetToPrevWeekDay(weekday) ); +} + +inline wxDateTime wxDateTime::GetWeekDay(WeekDay weekday, + int n, + Month month, + int year) const +{ + wxDateTime dt(*this); + + return dt.SetToWeekDay(weekday, n, month, year) ? dt : wxInvalidDateTime; +} + +inline wxDateTime wxDateTime::GetLastWeekDay(WeekDay weekday, + Month month, + int year) +{ + wxDateTime dt(*this); + + return dt.SetToLastWeekDay(weekday, month, year) ? dt : wxInvalidDateTime; +} + +inline wxDateTime wxDateTime::GetWeek(wxDateTime_t numWeek, + WeekDay weekday) const +{ + wxDateTime dt(*this); + + return dt.SetToTheWeek(numWeek, weekday) ? dt : wxInvalidDateTime; +} + +inline wxDateTime wxDateTime::GetLastMonthDay(Month month, int year) const +{ + MODIFY_AND_RETURN( SetToLastMonthDay(month, year) ); +} + +inline wxDateTime wxDateTime::GetYearDay(wxDateTime_t yday) const +{ + MODIFY_AND_RETURN( SetToYearDay(yday) ); +} + // ---------------------------------------------------------------------------- // wxDateTime comparison // ---------------------------------------------------------------------------- -bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const +inline bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const { wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime")); return m_time == datetime.m_time; } -bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const +inline bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const { wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime")); return m_time < datetime.m_time; } -bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const +inline bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const { wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime")); return m_time > datetime.m_time; } -bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1, - const wxDateTime& t2) const +inline bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1, + const wxDateTime& t2) const { // no need for assert, will be checked by the functions we call return IsLaterThan(t1) && IsEarlierThan(t2); } -bool wxDateTime::IsBetween(const wxDateTime& t1, const wxDateTime& t2) const +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); } -bool wxDateTime::IsSameDate(const wxDateTime& dt) const +inline bool wxDateTime::IsSameDate(const wxDateTime& dt) const { - return (m_time - dt.m_time).Abs() < MILLISECONDS_PER_DAY; + Tm tm1 = GetTm(), + tm2 = dt.GetTm(); + + return tm1.year == tm2.year && + tm1.mon == tm2.mon && + tm1.mday == tm2.mday; } -bool wxDateTime::IsSameTime(const wxDateTime& dt) const +inline bool wxDateTime::IsSameTime(const wxDateTime& dt) const { // notice that we can't do something like this: // @@ -192,23 +273,24 @@ bool wxDateTime::IsSameTime(const wxDateTime& dt) const tm1.msec == tm2.msec; } -bool wxDateTime::IsEqualUpTo(const wxDateTime& dt, const wxTimeSpan& ts) const +inline bool wxDateTime::IsEqualUpTo(const wxDateTime& dt, + const wxTimeSpan& ts) const { - return IsBetween(dt.Substract(ts), dt.Add(ts)); + return IsBetween(dt.Subtract(ts), dt.Add(ts)); } // ---------------------------------------------------------------------------- // wxDateTime arithmetics // ---------------------------------------------------------------------------- -wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const +inline wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const { wxASSERT_MSG( IsValid(), _T("invalid wxDateTime")); return wxDateTime(m_time + diff.GetValue()); } -wxDateTime& wxDateTime::Add(const wxTimeSpan& diff) +inline wxDateTime& wxDateTime::Add(const wxTimeSpan& diff) { wxASSERT_MSG( IsValid(), _T("invalid wxDateTime")); @@ -217,19 +299,19 @@ wxDateTime& wxDateTime::Add(const wxTimeSpan& diff) return *this; } -wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff) +inline wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff) { return Add(diff); } -wxDateTime wxDateTime::Substract(const wxTimeSpan& diff) const +inline wxDateTime wxDateTime::Subtract(const wxTimeSpan& diff) const { wxASSERT_MSG( IsValid(), _T("invalid wxDateTime")); return wxDateTime(m_time - diff.GetValue()); } -wxDateTime& wxDateTime::Substract(const wxTimeSpan& diff) +inline wxDateTime& wxDateTime::Subtract(const wxTimeSpan& diff) { wxASSERT_MSG( IsValid(), _T("invalid wxDateTime")); @@ -238,39 +320,39 @@ wxDateTime& wxDateTime::Substract(const wxTimeSpan& diff) return *this; } -wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff) +inline wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff) { - return Substract(diff); + return Subtract(diff); } -wxTimeSpan wxDateTime::Substract(const wxDateTime& datetime) const +inline wxTimeSpan wxDateTime::Subtract(const wxDateTime& datetime) const { wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime")); - return wxTimeSpan(datetime.GetValue() - GetValue()); + return wxTimeSpan(GetValue() - datetime.GetValue()); } -wxDateTime wxDateTime::Add(const wxDateSpan& diff) const +inline wxDateTime wxDateTime::Add(const wxDateSpan& diff) const { return wxDateTime(*this).Add(diff); } -wxDateTime& wxDateTime::Substract(const wxDateSpan& diff) +inline wxDateTime& wxDateTime::Subtract(const wxDateSpan& diff) { return Add(diff.Negate()); } -wxDateTime wxDateTime::Substract(const wxDateSpan& diff) const +inline wxDateTime wxDateTime::Subtract(const wxDateSpan& diff) const { - return wxDateTime(*this).Substract(diff); + return wxDateTime(*this).Subtract(diff); } -wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff) +inline wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff) { - return Substract(diff); + return Subtract(diff); } -wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff) +inline wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff) { return Add(diff); } @@ -279,19 +361,23 @@ wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff) // wxDateTime and timezones // ---------------------------------------------------------------------------- -wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz) const +inline wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz, + bool noDST) const { - return wxDateTime(*this).MakeTimezone(tz); + MODIFY_AND_RETURN( MakeTimezone(tz, noDST) ); } // ---------------------------------------------------------------------------- // wxTimeSpan construction // ---------------------------------------------------------------------------- -wxTimeSpan::wxTimeSpan(int hours, int minutes, int seconds, int milliseconds) +inline wxTimeSpan::wxTimeSpan(long hours, + long minutes, + long seconds, + long milliseconds) { // assign first to avoid precision loss - m_diff = (long)hours; + m_diff = hours; m_diff *= 60l; m_diff += minutes; m_diff *= 60l; @@ -304,27 +390,27 @@ wxTimeSpan::wxTimeSpan(int hours, int minutes, int seconds, int milliseconds) // wxTimeSpan accessors // ---------------------------------------------------------------------------- -wxLongLong wxTimeSpan::GetSeconds() const +inline wxLongLong wxTimeSpan::GetSeconds() const { return m_diff / 1000l; } -int wxTimeSpan::GetMinutes() const +inline int wxTimeSpan::GetMinutes() const { return (GetSeconds() / 60l).GetLo(); } -int wxTimeSpan::GetHours() const +inline int wxTimeSpan::GetHours() const { return GetMinutes() / 60; } -int wxTimeSpan::GetDays() const +inline int wxTimeSpan::GetDays() const { return GetHours() / 24; } -int wxTimeSpan::GetWeeks() const +inline int wxTimeSpan::GetWeeks() const { return GetDays() / 7; } @@ -333,53 +419,53 @@ int wxTimeSpan::GetWeeks() const // wxTimeSpan arithmetics // ---------------------------------------------------------------------------- -wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const +inline wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const { return wxTimeSpan(m_diff + diff.GetValue()); } -wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff) +inline wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff) { m_diff += diff.GetValue(); return *this; } -wxTimeSpan wxTimeSpan::Substract(const wxTimeSpan& diff) const +inline wxTimeSpan wxTimeSpan::Subtract(const wxTimeSpan& diff) const { return wxTimeSpan(m_diff - diff.GetValue()); } -wxTimeSpan& wxTimeSpan::Substract(const wxTimeSpan& diff) +inline wxTimeSpan& wxTimeSpan::Subtract(const wxTimeSpan& diff) { m_diff -= diff.GetValue(); return *this; } -wxTimeSpan& wxTimeSpan::Multiply(int n) +inline wxTimeSpan& wxTimeSpan::Multiply(int n) { m_diff *= (long)n; return *this; } -wxTimeSpan wxTimeSpan::Multiply(int n) const +inline wxTimeSpan wxTimeSpan::Multiply(int n) const { return wxTimeSpan(m_diff * (long)n); } -wxTimeSpan wxTimeSpan::Abs() const +inline wxTimeSpan wxTimeSpan::Abs() const { return wxTimeSpan(GetValue().Abs()); } -bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const +inline bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const { return GetValue() == ts.GetValue(); } -bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const +inline bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const { return GetValue().Abs() > ts.GetValue().Abs(); } @@ -388,8 +474,7 @@ bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const // wxDateSpan // ---------------------------------------------------------------------------- -wxDateSpan& -wxDateSpan::operator+=(const wxDateSpan& other) +inline wxDateSpan& wxDateSpan::operator+=(const wxDateSpan& other) { m_years += other.m_years; m_months += other.m_months; @@ -399,7 +484,19 @@ wxDateSpan::operator+=(const wxDateSpan& other) return *this; } -wxDateSpan& wxDateSpan::Multiply(int factor) +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; @@ -409,17 +506,19 @@ wxDateSpan& wxDateSpan::Multiply(int factor) return *this; } -wxDateSpan wxDateSpan::Multiply(int factor) const +inline wxDateSpan wxDateSpan::Multiply(int factor) const { - return wxDateSpan(*this).Multiply(factor); + wxDateSpan ds(*this); + ds.Multiply(factor); + return ds; } -wxDateSpan wxDateSpan::Negate() const +inline wxDateSpan wxDateSpan::Negate() const { return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days); } -wxDateSpan& wxDateSpan::Neg() +inline wxDateSpan& wxDateSpan::Neg() { m_years = -m_years; m_months = -m_months; @@ -429,4 +528,23 @@ wxDateSpan& wxDateSpan::Neg() 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