X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/2f02cb8966964ea7a6f5d875ed9f3434ea05ab59..94799627ee0ad217b2fc41df5bd5bccf59494e00:/include/wx/datetime.inl diff --git a/include/wx/datetime.inl b/include/wx/datetime.inl index 1ae112d5ad..8d3112b6a4 100644 --- a/include/wx/datetime.inl +++ b/include/wx/datetime.inl @@ -17,14 +17,19 @@ #endif // ---------------------------------------------------------------------------- -// wxDateTime statics +// private macros // ---------------------------------------------------------------------------- -/* static */ -wxDateTime::Country wxDateTime::GetCountry() -{ - return ms_country; -} +#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 @@ -32,20 +37,32 @@ wxDateTime::Country wxDateTime::GetCountry() // only define this once, when included from datetime.cpp #ifdef wxDEFINE_TIME_CONSTANTS - const unsigned int wxDateTime::TIME_T_FACTOR = 1000; + const long wxDateTime::TIME_T_FACTOR = 1000l; #endif // wxDEFINE_TIME_CONSTANTS -wxDateTime::IsInStdRange() const +bool wxDateTime::IsInStdRange() const { - return m_time >= 0l && (m_time / (long)TIME_T_FACTOR) < LONG_MAX; + return m_time >= 0l && (m_time / TIME_T_FACTOR) < LONG_MAX; } /* static */ wxDateTime wxDateTime::Now() { - return wxDateTime(GetTimeNow()); + return wxDateTime(*GetTmNow()); +} + +/* static */ +wxDateTime wxDateTime::Today() +{ + 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! @@ -54,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) { @@ -75,6 +96,11 @@ wxDateTime::wxDateTime(const Tm& tm) Set(tm); } +wxDateTime::wxDateTime(double jdn) +{ + Set(jdn); +} + wxDateTime& wxDateTime::Set(const Tm& tm) { wxASSERT_MSG( tm.IsValid(), _T("invalid broken down date/time") ); @@ -107,14 +133,14 @@ wxDateTime::wxDateTime(wxDateTime_t day, wxLongLong wxDateTime::GetValue() const { - wxASSERT_MSG( IsValid(), "invalid wxDateTime"); + wxASSERT_MSG( IsValid(), _T("invalid wxDateTime")); return m_time; } time_t wxDateTime::GetTicks() const { - wxASSERT_MSG( IsValid(), "invalid wxDateTime"); + wxASSERT_MSG( IsValid(), _T("invalid wxDateTime")); if ( !IsInStdRange() ) { return (time_t)-1; @@ -130,37 +156,78 @@ bool wxDateTime::SetToLastWeekDay(WeekDay weekday, return SetToWeekDay(weekday, -1, month, year); } -// ---------------------------------------------------------------------------- -// wxDateTime comparison -// ---------------------------------------------------------------------------- +wxDateTime wxDateTime::GetWeekDayInSameWeek(WeekDay weekday) const +{ + MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday) ); +} -bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const +wxDateTime wxDateTime::GetNextWeekDay(WeekDay weekday) const { - wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime"); + MODIFY_AND_RETURN( SetToNextWeekDay(weekday) ); +} - return m_time == datetime.m_time; +wxDateTime wxDateTime::GetPrevWeekDay(WeekDay weekday) const +{ + MODIFY_AND_RETURN( SetToPrevWeekDay(weekday) ); } -bool wxDateTime::operator==(const wxDateTime& datetime) const +wxDateTime wxDateTime::GetWeekDay(WeekDay weekday, + int n, + Month month, + int year) const { - return IsEqualTo(datetime); + wxDateTime dt(*this); + + return dt.SetToWeekDay(weekday, n, month, year) ? dt : wxInvalidDateTime; } -bool wxDateTime::operator!=(const wxDateTime& datetime) const +wxDateTime wxDateTime::GetLastWeekDay(WeekDay weekday, + Month month, + int year) { - return !IsEqualTo(datetime); + 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 +// ---------------------------------------------------------------------------- + +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 { - wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime"); + wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime")); return m_time < datetime.m_time; } bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const { - wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime"); + wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime")); return m_time > datetime.m_time; } @@ -178,13 +245,46 @@ 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 // ---------------------------------------------------------------------------- +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) { - wxASSERT_MSG( IsValid(), "invalid wxDateTime"); + wxASSERT_MSG( IsValid(), _T("invalid wxDateTime")); m_time += diff.GetValue(); @@ -196,9 +296,16 @@ wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff) return Add(diff); } +wxDateTime wxDateTime::Substract(const wxTimeSpan& diff) const +{ + wxASSERT_MSG( IsValid(), _T("invalid wxDateTime")); + + return wxDateTime(m_time - diff.GetValue()); +} + wxDateTime& wxDateTime::Substract(const wxTimeSpan& diff) { - wxASSERT_MSG( IsValid(), "invalid wxDateTime"); + wxASSERT_MSG( IsValid(), _T("invalid wxDateTime")); m_time -= diff.GetValue(); @@ -212,14 +319,14 @@ wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff) wxTimeSpan wxDateTime::Substract(const wxDateTime& datetime) const { - wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime"); + wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime")); return wxTimeSpan(datetime.GetValue() - GetValue()); } -wxTimeSpan wxDateTime::operator-(const wxDateTime& datetime) const +wxDateTime wxDateTime::Add(const wxDateSpan& diff) const { - return Substract(datetime); + return wxDateTime(*this).Add(diff); } wxDateTime& wxDateTime::Substract(const wxDateSpan& diff) @@ -227,6 +334,11 @@ 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); @@ -238,9 +350,69 @@ wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff) } // ---------------------------------------------------------------------------- -// wxTimeSpan +// wxDateTime and timezones +// ---------------------------------------------------------------------------- + +wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz, + bool noDST) const +{ + MODIFY_AND_RETURN( MakeTimezone(tz, noDST) ); +} + +// ---------------------------------------------------------------------------- +// wxTimeSpan construction +// ---------------------------------------------------------------------------- + +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; +} + +// ---------------------------------------------------------------------------- +// wxTimeSpan accessors +// ---------------------------------------------------------------------------- + +wxLongLong wxTimeSpan::GetSeconds() const +{ + return m_diff / 1000l; +} + +int wxTimeSpan::GetMinutes() const +{ + return (GetSeconds() / 60l).GetLo(); +} + +int wxTimeSpan::GetHours() const +{ + return GetMinutes() / 60; +} + +int wxTimeSpan::GetDays() const +{ + return GetHours() / 24; +} + +int wxTimeSpan::GetWeeks() const +{ + return GetDays() / 7; +} + +// ---------------------------------------------------------------------------- +// wxTimeSpan arithmetics // ---------------------------------------------------------------------------- +wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const +{ + return wxTimeSpan(m_diff + diff.GetValue()); +} + wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff) { m_diff += diff.GetValue(); @@ -248,6 +420,11 @@ wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff) return *this; } +wxTimeSpan wxTimeSpan::Substract(const wxTimeSpan& diff) const +{ + return wxTimeSpan(m_diff - diff.GetValue()); +} + wxTimeSpan& wxTimeSpan::Substract(const wxTimeSpan& diff) { m_diff -= diff.GetValue(); @@ -257,17 +434,14 @@ wxTimeSpan& wxTimeSpan::Substract(const wxTimeSpan& diff) wxTimeSpan& wxTimeSpan::Multiply(int n) { - m_diff *= n; + m_diff *= (long)n; return *this; } -wxTimeSpan wxTimeSpan::operator*(int n) const +wxTimeSpan wxTimeSpan::Multiply(int n) const { - wxTimeSpan result(*this); - result.Multiply(n); - - return result; + return wxTimeSpan(m_diff * (long)n); } wxTimeSpan wxTimeSpan::Abs() const @@ -300,16 +474,21 @@ wxDateSpan::operator+=(const wxDateSpan& other) return *this; } -wxDateSpan& wxDateSpan::operator*=(int factor) +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; } +wxDateSpan wxDateSpan::Multiply(int factor) const +{ + return wxDateSpan(*this).Multiply(factor); +} + wxDateSpan wxDateSpan::Negate() const { return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days); @@ -325,4 +504,6 @@ wxDateSpan& wxDateSpan::Neg() return *this; } +#undef MILLISECONDS_PER_DAY +#undef MODIFY_AND_RETURN