X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/cd0b170911637899ac3c126367ee5821f357c185..cd6ce4a90c11b54cc00799d0e4611d858739a749:/include/wx/datetime.inl diff --git a/include/wx/datetime.inl b/include/wx/datetime.inl index f96f06e806..8d3112b6a4 100644 --- a/include/wx/datetime.inl +++ b/include/wx/datetime.inl @@ -16,6 +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 // ---------------------------------------------------------------------------- @@ -33,15 +48,21 @@ bool wxDateTime::IsInStdRange() const /* static */ wxDateTime wxDateTime::Now() { - return wxDateTime(GetTimeNow()); + return wxDateTime(*GetTmNow()); } /* static */ wxDateTime wxDateTime::Today() { - return wxDateTime((time_t)(86400*(GetTimeNow() / 86400))); + struct tm *tm = GetTmNow(); + tm->tm_hour = + tm->tm_min = + tm->tm_sec = 0; + + return wxDateTime(*tm); } +#if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400)) wxDateTime& wxDateTime::Set(time_t timet) { // assign first to avoid long multiplication overflow! @@ -50,16 +71,20 @@ wxDateTime& wxDateTime::Set(time_t timet) return *this; } +#endif wxDateTime& wxDateTime::SetToCurrent() { - return Set(GetTimeNow()); + *this = Now(); + return *this; } +#if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400)) wxDateTime::wxDateTime(time_t timet) { Set(timet); } +#endif wxDateTime::wxDateTime(const struct tm& tm) { @@ -131,6 +156,57 @@ bool wxDateTime::SetToLastWeekDay(WeekDay weekday, return SetToWeekDay(weekday, -1, month, year); } +wxDateTime wxDateTime::GetWeekDayInSameWeek(WeekDay weekday) const +{ + MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday) ); +} + +wxDateTime wxDateTime::GetNextWeekDay(WeekDay weekday) const +{ + MODIFY_AND_RETURN( SetToNextWeekDay(weekday) ); +} + +wxDateTime wxDateTime::GetPrevWeekDay(WeekDay weekday) const +{ + MODIFY_AND_RETURN( SetToPrevWeekDay(weekday) ); +} + +wxDateTime wxDateTime::GetWeekDay(WeekDay weekday, + int n, + Month month, + int year) const +{ + wxDateTime dt(*this); + + return dt.SetToWeekDay(weekday, n, month, year) ? dt : wxInvalidDateTime; +} + +wxDateTime wxDateTime::GetLastWeekDay(WeekDay weekday, + Month month, + int year) +{ + wxDateTime dt(*this); + + return dt.SetToLastWeekDay(weekday, month, year) ? dt : wxInvalidDateTime; +} + +wxDateTime wxDateTime::GetWeek(wxDateTime_t numWeek, WeekDay weekday) const +{ + wxDateTime dt(*this); + + return dt.SetToTheWeek(numWeek, weekday) ? dt : wxInvalidDateTime; +} + +wxDateTime wxDateTime::GetLastMonthDay(Month month, int year) const +{ + MODIFY_AND_RETURN( SetToLastMonthDay(month, year) ); +} + +wxDateTime wxDateTime::GetYearDay(wxDateTime_t yday) const +{ + MODIFY_AND_RETURN( SetToYearDay(yday) ); +} + // ---------------------------------------------------------------------------- // wxDateTime comparison // ---------------------------------------------------------------------------- @@ -169,6 +245,32 @@ bool wxDateTime::IsBetween(const wxDateTime& t1, const wxDateTime& t2) const return IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2); } +bool wxDateTime::IsSameDate(const wxDateTime& dt) const +{ + return (m_time - dt.m_time).Abs() < MILLISECONDS_PER_DAY; +} + +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; +} + +bool wxDateTime::IsEqualUpTo(const wxDateTime& dt, const wxTimeSpan& ts) const +{ + return IsBetween(dt.Substract(ts), dt.Add(ts)); +} + // ---------------------------------------------------------------------------- // wxDateTime arithmetics // ---------------------------------------------------------------------------- @@ -222,11 +324,21 @@ wxTimeSpan wxDateTime::Substract(const wxDateTime& datetime) const return wxTimeSpan(datetime.GetValue() - GetValue()); } +wxDateTime wxDateTime::Add(const wxDateSpan& diff) const +{ + return wxDateTime(*this).Add(diff); +} + wxDateTime& wxDateTime::Substract(const wxDateSpan& diff) { return Add(diff.Negate()); } +wxDateTime wxDateTime::Substract(const wxDateSpan& diff) const +{ + return wxDateTime(*this).Substract(diff); +} + wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff) { return Substract(diff); @@ -241,19 +353,20 @@ wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff) // wxDateTime and timezones // ---------------------------------------------------------------------------- -wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz) const +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) +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; @@ -363,10 +476,10 @@ wxDateSpan::operator+=(const wxDateSpan& other) wxDateSpan& wxDateSpan::Multiply(int factor) { - m_years *= m_years; - m_months *= m_months; - m_weeks *= m_weeks; - m_days *= m_days; + m_years *= factor; + m_months *= factor; + m_weeks *= factor; + m_days *= factor; return *this; } @@ -391,3 +504,6 @@ wxDateSpan& wxDateSpan::Neg() return *this; } +#undef MILLISECONDS_PER_DAY + +#undef MODIFY_AND_RETURN