1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/datetime.inl
3 // Purpose: definition of inline functions of wxDateTime and related
4 // classes declared in datetime.h
5 // Author: Vadim Zeitlin
6 // Remarks: having the inline functions here allows us to minimize the
7 // dependencies (and hence the rebuild time) in debug builds.
11 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
12 // Licence: wxWindows license
13 /////////////////////////////////////////////////////////////////////////////
15 #ifndef INCLUDED_FROM_WX_DATETIME_H
16 #error "This file is only included by wx/datetime.h, don't include it manually!"
19 // ----------------------------------------------------------------------------
21 // ----------------------------------------------------------------------------
23 #define MILLISECONDS_PER_DAY 86400000l
25 // some broken compilers (HP-UX CC) refuse to compile the "normal" version, but
26 // using a temp variable always might prevent other compilers from optimising
27 // it away - hence use of this ugly macro
29 #define MODIFY_AND_RETURN(op) return wxDateTime(*this).op
31 #define MODIFY_AND_RETURN(op) wxDateTime dt(*this); dt.op; return dt
34 // ----------------------------------------------------------------------------
35 // wxDateTime construction
36 // ----------------------------------------------------------------------------
38 // only define this once, when included from datetime.cpp
39 // TODO: figure out why switching accessibility on causes link errors
40 // if don't use this test.
41 #if defined(wxDEFINE_TIME_CONSTANTS) || (defined(_MSC_VER) && wxUSE_ACCESSIBILITY)
42 const long wxDateTime::TIME_T_FACTOR = 1000l;
43 #endif // wxDEFINE_TIME_CONSTANTS
45 inline bool wxDateTime::IsInStdRange() const
47 return m_time >= 0l && (m_time / TIME_T_FACTOR) < LONG_MAX;
51 inline wxDateTime wxDateTime::Now()
53 return wxDateTime(*GetTmNow());
57 inline wxDateTime wxDateTime::Today()
59 struct tm *time = GetTmNow();
64 return wxDateTime(*time);
67 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
68 inline wxDateTime& wxDateTime::Set(time_t timet)
70 // assign first to avoid long multiplication overflow!
71 m_time = timet - WX_TIME_BASE_OFFSET ;
72 m_time *= TIME_T_FACTOR;
78 inline wxDateTime& wxDateTime::SetToCurrent()
84 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
85 inline wxDateTime::wxDateTime(time_t timet)
91 inline wxDateTime::wxDateTime(const struct tm& tm)
96 inline wxDateTime::wxDateTime(const Tm& tm)
101 inline wxDateTime::wxDateTime(double jdn)
106 inline wxDateTime& wxDateTime::Set(const Tm& tm)
108 wxASSERT_MSG( tm.IsValid(), _T("invalid broken down date/time") );
110 return Set(tm.mday, (Month)tm.mon, tm.year, tm.hour, tm.min, tm.sec);
113 inline wxDateTime::wxDateTime(wxDateTime_t hour,
116 wxDateTime_t millisec)
118 Set(hour, minute, second, millisec);
121 inline wxDateTime::wxDateTime(wxDateTime_t day,
127 wxDateTime_t millisec)
129 Set(day, month, year, hour, minute, second, millisec);
132 // ----------------------------------------------------------------------------
133 // wxDateTime accessors
134 // ----------------------------------------------------------------------------
136 inline wxLongLong wxDateTime::GetValue() const
138 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
143 inline time_t wxDateTime::GetTicks() const
145 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
146 if ( !IsInStdRange() )
151 return (time_t)((m_time / (long)TIME_T_FACTOR).GetLo())+WX_TIME_BASE_OFFSET ;
154 inline bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
158 return SetToWeekDay(weekday, -1, month, year);
161 inline wxDateTime wxDateTime::GetWeekDayInSameWeek(WeekDay weekday,
162 WeekFlags flags) const
164 MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday) );
167 inline wxDateTime wxDateTime::GetNextWeekDay(WeekDay weekday) const
169 MODIFY_AND_RETURN( SetToNextWeekDay(weekday) );
172 inline wxDateTime wxDateTime::GetPrevWeekDay(WeekDay weekday) const
174 MODIFY_AND_RETURN( SetToPrevWeekDay(weekday) );
177 inline wxDateTime wxDateTime::GetWeekDay(WeekDay weekday,
182 wxDateTime dt(*this);
184 return dt.SetToWeekDay(weekday, n, month, year) ? dt : wxInvalidDateTime;
187 inline wxDateTime wxDateTime::GetLastWeekDay(WeekDay weekday,
191 wxDateTime dt(*this);
193 return dt.SetToLastWeekDay(weekday, month, year) ? dt : wxInvalidDateTime;
196 inline wxDateTime wxDateTime::GetWeek(wxDateTime_t numWeek,
198 WeekFlags flags) const
200 wxDateTime dt(*this);
202 return dt.SetToTheWeek(numWeek, weekday, flags) ? dt : wxInvalidDateTime;
205 inline wxDateTime wxDateTime::GetLastMonthDay(Month month, int year) const
207 MODIFY_AND_RETURN( SetToLastMonthDay(month, year) );
210 inline wxDateTime wxDateTime::GetYearDay(wxDateTime_t yday) const
212 MODIFY_AND_RETURN( SetToYearDay(yday) );
215 // ----------------------------------------------------------------------------
216 // wxDateTime comparison
217 // ----------------------------------------------------------------------------
219 inline bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
221 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
223 return m_time == datetime.m_time;
226 inline bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
228 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
230 return m_time < datetime.m_time;
233 inline bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
235 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
237 return m_time > datetime.m_time;
240 inline bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1,
241 const wxDateTime& t2) const
243 // no need for assert, will be checked by the functions we call
244 return IsLaterThan(t1) && IsEarlierThan(t2);
247 inline bool wxDateTime::IsBetween(const wxDateTime& t1,
248 const wxDateTime& t2) const
250 // no need for assert, will be checked by the functions we call
251 return IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2);
254 inline bool wxDateTime::IsSameDate(const wxDateTime& dt) const
259 return tm1.year == tm2.year &&
260 tm1.mon == tm2.mon &&
261 tm1.mday == tm2.mday;
264 inline bool wxDateTime::IsSameTime(const wxDateTime& dt) const
266 // notice that we can't do something like this:
268 // m_time % MILLISECONDS_PER_DAY == dt.m_time % MILLISECONDS_PER_DAY
270 // because we have also to deal with (possibly) different DST settings!
274 return tm1.hour == tm2.hour &&
275 tm1.min == tm2.min &&
276 tm1.sec == tm2.sec &&
277 tm1.msec == tm2.msec;
280 inline bool wxDateTime::IsEqualUpTo(const wxDateTime& dt,
281 const wxTimeSpan& ts) const
283 return IsBetween(dt.Subtract(ts), dt.Add(ts));
286 // ----------------------------------------------------------------------------
287 // wxDateTime arithmetics
288 // ----------------------------------------------------------------------------
290 inline wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const
292 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
294 return wxDateTime(m_time + diff.GetValue());
297 inline wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
299 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
301 m_time += diff.GetValue();
306 inline wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
311 inline wxDateTime wxDateTime::Subtract(const wxTimeSpan& diff) const
313 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
315 return wxDateTime(m_time - diff.GetValue());
318 inline wxDateTime& wxDateTime::Subtract(const wxTimeSpan& diff)
320 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
322 m_time -= diff.GetValue();
327 inline wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
329 return Subtract(diff);
332 inline wxTimeSpan wxDateTime::Subtract(const wxDateTime& datetime) const
334 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
336 return wxTimeSpan(GetValue() - datetime.GetValue());
339 inline wxDateTime wxDateTime::Add(const wxDateSpan& diff) const
341 return wxDateTime(*this).Add(diff);
344 inline wxDateTime& wxDateTime::Subtract(const wxDateSpan& diff)
346 return Add(diff.Negate());
349 inline wxDateTime wxDateTime::Subtract(const wxDateSpan& diff) const
351 return wxDateTime(*this).Subtract(diff);
354 inline wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
356 return Subtract(diff);
359 inline wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
364 // ----------------------------------------------------------------------------
365 // wxDateTime and timezones
366 // ----------------------------------------------------------------------------
368 inline wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz,
371 MODIFY_AND_RETURN( MakeTimezone(tz, noDST) );
374 // ----------------------------------------------------------------------------
375 // wxTimeSpan construction
376 // ----------------------------------------------------------------------------
378 inline wxTimeSpan::wxTimeSpan(long hours,
383 // assign first to avoid precision loss
390 m_diff += milliseconds;
393 // ----------------------------------------------------------------------------
394 // wxTimeSpan accessors
395 // ----------------------------------------------------------------------------
397 inline wxLongLong wxTimeSpan::GetSeconds() const
399 return m_diff / 1000l;
402 inline int wxTimeSpan::GetMinutes() const
404 return (GetSeconds() / 60l).GetLo();
407 inline int wxTimeSpan::GetHours() const
409 return GetMinutes() / 60;
412 inline int wxTimeSpan::GetDays() const
414 return GetHours() / 24;
417 inline int wxTimeSpan::GetWeeks() const
419 return GetDays() / 7;
422 // ----------------------------------------------------------------------------
423 // wxTimeSpan arithmetics
424 // ----------------------------------------------------------------------------
426 inline wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const
428 return wxTimeSpan(m_diff + diff.GetValue());
431 inline wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
433 m_diff += diff.GetValue();
438 inline wxTimeSpan wxTimeSpan::Subtract(const wxTimeSpan& diff) const
440 return wxTimeSpan(m_diff - diff.GetValue());
443 inline wxTimeSpan& wxTimeSpan::Subtract(const wxTimeSpan& diff)
445 m_diff -= diff.GetValue();
450 inline wxTimeSpan& wxTimeSpan::Multiply(int n)
457 inline wxTimeSpan wxTimeSpan::Multiply(int n) const
459 return wxTimeSpan(m_diff * (long)n);
462 inline wxTimeSpan wxTimeSpan::Abs() const
464 return wxTimeSpan(GetValue().Abs());
467 inline bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
469 return GetValue() == ts.GetValue();
472 inline bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
474 return GetValue().Abs() > ts.GetValue().Abs();
477 // ----------------------------------------------------------------------------
479 // ----------------------------------------------------------------------------
481 inline wxDateSpan& wxDateSpan::operator+=(const wxDateSpan& other)
483 m_years += other.m_years;
484 m_months += other.m_months;
485 m_weeks += other.m_weeks;
486 m_days += other.m_days;
491 inline wxDateSpan& wxDateSpan::Add(const wxDateSpan& other)
493 return *this += other;
496 inline wxDateSpan wxDateSpan::Add(const wxDateSpan& other) const
498 wxDateSpan ds(*this);
503 inline wxDateSpan& wxDateSpan::Multiply(int factor)
513 inline wxDateSpan wxDateSpan::Multiply(int factor) const
515 wxDateSpan ds(*this);
520 inline wxDateSpan wxDateSpan::Negate() const
522 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
525 inline wxDateSpan& wxDateSpan::Neg()
528 m_months = -m_months;
535 inline wxDateSpan& wxDateSpan::operator-=(const wxDateSpan& other)
537 return *this += other.Negate();
540 inline wxDateSpan& wxDateSpan::Subtract(const wxDateSpan& other)
542 return *this -= other;
545 inline wxDateSpan wxDateSpan::Subtract(const wxDateSpan& other) const
547 wxDateSpan ds(*this);
552 #undef MILLISECONDS_PER_DAY
554 #undef MODIFY_AND_RETURN