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 inline bool wxDateTime::IsInStdRange() const
40 return m_time >= 0l && (m_time / TIME_T_FACTOR) < LONG_MAX;
44 inline wxDateTime wxDateTime::Now()
46 return wxDateTime(*GetTmNow());
50 inline wxDateTime wxDateTime::Today()
52 struct tm *time = GetTmNow();
57 return wxDateTime(*time);
60 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
61 inline wxDateTime& wxDateTime::Set(time_t timet)
63 // assign first to avoid long multiplication overflow!
64 m_time = timet - WX_TIME_BASE_OFFSET ;
65 m_time *= TIME_T_FACTOR;
71 inline wxDateTime& wxDateTime::SetToCurrent()
77 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
78 inline wxDateTime::wxDateTime(time_t timet)
84 inline wxDateTime::wxDateTime(const struct tm& tm)
89 inline wxDateTime::wxDateTime(const Tm& tm)
94 inline wxDateTime::wxDateTime(double jdn)
99 inline wxDateTime& wxDateTime::Set(const Tm& tm)
101 wxASSERT_MSG( tm.IsValid(), _T("invalid broken down date/time") );
103 return Set(tm.mday, (Month)tm.mon, tm.year,
104 tm.hour, tm.min, tm.sec, tm.msec);
107 inline wxDateTime::wxDateTime(wxDateTime_t hour,
110 wxDateTime_t millisec)
112 Set(hour, minute, second, millisec);
115 inline wxDateTime::wxDateTime(wxDateTime_t day,
121 wxDateTime_t millisec)
123 Set(day, month, year, hour, minute, second, millisec);
126 // ----------------------------------------------------------------------------
127 // wxDateTime accessors
128 // ----------------------------------------------------------------------------
130 inline wxLongLong wxDateTime::GetValue() const
132 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
137 inline time_t wxDateTime::GetTicks() const
139 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
140 if ( !IsInStdRange() )
145 return (time_t)((m_time / (long)TIME_T_FACTOR).GetLo())+WX_TIME_BASE_OFFSET ;
148 inline bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
152 return SetToWeekDay(weekday, -1, month, year);
155 inline wxDateTime wxDateTime::GetWeekDayInSameWeek(WeekDay weekday,
156 WeekFlags flags) const
158 MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday) );
161 inline wxDateTime wxDateTime::GetNextWeekDay(WeekDay weekday) const
163 MODIFY_AND_RETURN( SetToNextWeekDay(weekday) );
166 inline wxDateTime wxDateTime::GetPrevWeekDay(WeekDay weekday) const
168 MODIFY_AND_RETURN( SetToPrevWeekDay(weekday) );
171 inline wxDateTime wxDateTime::GetWeekDay(WeekDay weekday,
176 wxDateTime dt(*this);
178 return dt.SetToWeekDay(weekday, n, month, year) ? dt : wxInvalidDateTime;
181 inline wxDateTime wxDateTime::GetLastWeekDay(WeekDay weekday,
185 wxDateTime dt(*this);
187 return dt.SetToLastWeekDay(weekday, month, year) ? dt : wxInvalidDateTime;
190 inline wxDateTime wxDateTime::GetWeek(wxDateTime_t numWeek,
192 WeekFlags flags) const
194 wxDateTime dt(*this);
196 return dt.SetToTheWeek(numWeek, weekday, flags) ? dt : wxInvalidDateTime;
199 inline wxDateTime wxDateTime::GetLastMonthDay(Month month, int year) const
201 MODIFY_AND_RETURN( SetToLastMonthDay(month, year) );
204 inline wxDateTime wxDateTime::GetYearDay(wxDateTime_t yday) const
206 MODIFY_AND_RETURN( SetToYearDay(yday) );
209 // ----------------------------------------------------------------------------
210 // wxDateTime comparison
211 // ----------------------------------------------------------------------------
213 inline bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
215 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
217 return m_time == datetime.m_time;
220 inline bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
222 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
224 return m_time < datetime.m_time;
227 inline bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
229 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
231 return m_time > datetime.m_time;
234 inline bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1,
235 const wxDateTime& t2) const
237 // no need for assert, will be checked by the functions we call
238 return IsLaterThan(t1) && IsEarlierThan(t2);
241 inline bool wxDateTime::IsBetween(const wxDateTime& t1,
242 const wxDateTime& t2) const
244 // no need for assert, will be checked by the functions we call
245 return IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2);
248 inline bool wxDateTime::IsSameDate(const wxDateTime& dt) const
253 return tm1.year == tm2.year &&
254 tm1.mon == tm2.mon &&
255 tm1.mday == tm2.mday;
258 inline bool wxDateTime::IsSameTime(const wxDateTime& dt) const
260 // notice that we can't do something like this:
262 // m_time % MILLISECONDS_PER_DAY == dt.m_time % MILLISECONDS_PER_DAY
264 // because we have also to deal with (possibly) different DST settings!
268 return tm1.hour == tm2.hour &&
269 tm1.min == tm2.min &&
270 tm1.sec == tm2.sec &&
271 tm1.msec == tm2.msec;
274 inline bool wxDateTime::IsEqualUpTo(const wxDateTime& dt,
275 const wxTimeSpan& ts) const
277 return IsBetween(dt.Subtract(ts), dt.Add(ts));
280 // ----------------------------------------------------------------------------
281 // wxDateTime arithmetics
282 // ----------------------------------------------------------------------------
284 inline wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const
286 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
288 return wxDateTime(m_time + diff.GetValue());
291 inline wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
293 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
295 m_time += diff.GetValue();
300 inline wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
305 inline wxDateTime wxDateTime::Subtract(const wxTimeSpan& diff) const
307 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
309 return wxDateTime(m_time - diff.GetValue());
312 inline wxDateTime& wxDateTime::Subtract(const wxTimeSpan& diff)
314 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
316 m_time -= diff.GetValue();
321 inline wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
323 return Subtract(diff);
326 inline wxTimeSpan wxDateTime::Subtract(const wxDateTime& datetime) const
328 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
330 return wxTimeSpan(GetValue() - datetime.GetValue());
333 inline wxDateTime wxDateTime::Add(const wxDateSpan& diff) const
335 return wxDateTime(*this).Add(diff);
338 inline wxDateTime& wxDateTime::Subtract(const wxDateSpan& diff)
340 return Add(diff.Negate());
343 inline wxDateTime wxDateTime::Subtract(const wxDateSpan& diff) const
345 return wxDateTime(*this).Subtract(diff);
348 inline wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
350 return Subtract(diff);
353 inline wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
358 // ----------------------------------------------------------------------------
359 // wxDateTime and timezones
360 // ----------------------------------------------------------------------------
362 inline wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz,
365 MODIFY_AND_RETURN( MakeTimezone(tz, noDST) );
368 // ----------------------------------------------------------------------------
369 // wxTimeSpan construction
370 // ----------------------------------------------------------------------------
372 inline wxTimeSpan::wxTimeSpan(long hours,
377 // assign first to avoid precision loss
384 m_diff += milliseconds;
387 // ----------------------------------------------------------------------------
388 // wxTimeSpan accessors
389 // ----------------------------------------------------------------------------
391 inline wxLongLong wxTimeSpan::GetSeconds() const
393 return m_diff / 1000l;
396 inline int wxTimeSpan::GetMinutes() const
398 return (GetSeconds() / 60l).GetLo();
401 inline int wxTimeSpan::GetHours() const
403 return GetMinutes() / 60;
406 inline int wxTimeSpan::GetDays() const
408 return GetHours() / 24;
411 inline int wxTimeSpan::GetWeeks() const
413 return GetDays() / 7;
416 // ----------------------------------------------------------------------------
417 // wxTimeSpan arithmetics
418 // ----------------------------------------------------------------------------
420 inline wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const
422 return wxTimeSpan(m_diff + diff.GetValue());
425 inline wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
427 m_diff += diff.GetValue();
432 inline wxTimeSpan wxTimeSpan::Subtract(const wxTimeSpan& diff) const
434 return wxTimeSpan(m_diff - diff.GetValue());
437 inline wxTimeSpan& wxTimeSpan::Subtract(const wxTimeSpan& diff)
439 m_diff -= diff.GetValue();
444 inline wxTimeSpan& wxTimeSpan::Multiply(int n)
451 inline wxTimeSpan wxTimeSpan::Multiply(int n) const
453 return wxTimeSpan(m_diff * (long)n);
456 inline wxTimeSpan wxTimeSpan::Abs() const
458 return wxTimeSpan(GetValue().Abs());
461 inline bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
463 return GetValue() == ts.GetValue();
466 inline bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
468 return GetValue().Abs() > ts.GetValue().Abs();
471 // ----------------------------------------------------------------------------
473 // ----------------------------------------------------------------------------
475 inline wxDateSpan& wxDateSpan::operator+=(const wxDateSpan& other)
477 m_years += other.m_years;
478 m_months += other.m_months;
479 m_weeks += other.m_weeks;
480 m_days += other.m_days;
485 inline wxDateSpan& wxDateSpan::Add(const wxDateSpan& other)
487 return *this += other;
490 inline wxDateSpan wxDateSpan::Add(const wxDateSpan& other) const
492 wxDateSpan ds(*this);
497 inline wxDateSpan& wxDateSpan::Multiply(int factor)
507 inline wxDateSpan wxDateSpan::Multiply(int factor) const
509 wxDateSpan ds(*this);
514 inline wxDateSpan wxDateSpan::Negate() const
516 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
519 inline wxDateSpan& wxDateSpan::Neg()
522 m_months = -m_months;
529 inline wxDateSpan& wxDateSpan::operator-=(const wxDateSpan& other)
531 return *this += other.Negate();
534 inline wxDateSpan& wxDateSpan::Subtract(const wxDateSpan& other)
536 return *this -= other;
539 inline wxDateSpan wxDateSpan::Subtract(const wxDateSpan& other) const
541 wxDateSpan ds(*this);
546 #undef MILLISECONDS_PER_DAY
548 #undef MODIFY_AND_RETURN