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 #if 1 // def wxDEFINE_TIME_CONSTANTS
40 const long wxDateTime::TIME_T_FACTOR = 1000l;
41 #endif // wxDEFINE_TIME_CONSTANTS
43 inline bool wxDateTime::IsInStdRange() const
45 return m_time >= 0l && (m_time / TIME_T_FACTOR) < LONG_MAX;
49 inline wxDateTime wxDateTime::Now()
51 return wxDateTime(*GetTmNow());
55 inline wxDateTime wxDateTime::Today()
57 struct tm *time = GetTmNow();
62 return wxDateTime(*time);
65 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
66 inline wxDateTime& wxDateTime::Set(time_t timet)
68 // assign first to avoid long multiplication overflow!
69 m_time = timet - WX_TIME_BASE_OFFSET ;
70 m_time *= TIME_T_FACTOR;
76 inline wxDateTime& wxDateTime::SetToCurrent()
82 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
83 inline wxDateTime::wxDateTime(time_t timet)
89 inline wxDateTime::wxDateTime(const struct tm& tm)
94 inline wxDateTime::wxDateTime(const Tm& tm)
99 inline wxDateTime::wxDateTime(double jdn)
104 inline wxDateTime& wxDateTime::Set(const Tm& tm)
106 wxASSERT_MSG( tm.IsValid(), _T("invalid broken down date/time") );
108 return Set(tm.mday, (Month)tm.mon, tm.year, tm.hour, tm.min, tm.sec);
111 inline wxDateTime::wxDateTime(wxDateTime_t hour,
114 wxDateTime_t millisec)
116 Set(hour, minute, second, millisec);
119 inline wxDateTime::wxDateTime(wxDateTime_t day,
125 wxDateTime_t millisec)
127 Set(day, month, year, hour, minute, second, millisec);
130 // ----------------------------------------------------------------------------
131 // wxDateTime accessors
132 // ----------------------------------------------------------------------------
134 inline wxLongLong wxDateTime::GetValue() const
136 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
141 inline time_t wxDateTime::GetTicks() const
143 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
144 if ( !IsInStdRange() )
149 return (time_t)((m_time / (long)TIME_T_FACTOR).GetLo())+WX_TIME_BASE_OFFSET ;
152 inline bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
156 return SetToWeekDay(weekday, -1, month, year);
159 inline wxDateTime wxDateTime::GetWeekDayInSameWeek(WeekDay weekday,
160 WeekFlags flags) const
162 MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday) );
165 inline wxDateTime wxDateTime::GetNextWeekDay(WeekDay weekday) const
167 MODIFY_AND_RETURN( SetToNextWeekDay(weekday) );
170 inline wxDateTime wxDateTime::GetPrevWeekDay(WeekDay weekday) const
172 MODIFY_AND_RETURN( SetToPrevWeekDay(weekday) );
175 inline wxDateTime wxDateTime::GetWeekDay(WeekDay weekday,
180 wxDateTime dt(*this);
182 return dt.SetToWeekDay(weekday, n, month, year) ? dt : wxInvalidDateTime;
185 inline wxDateTime wxDateTime::GetLastWeekDay(WeekDay weekday,
189 wxDateTime dt(*this);
191 return dt.SetToLastWeekDay(weekday, month, year) ? dt : wxInvalidDateTime;
194 inline wxDateTime wxDateTime::GetWeek(wxDateTime_t numWeek,
196 WeekFlags flags) const
198 wxDateTime dt(*this);
200 return dt.SetToTheWeek(numWeek, weekday, flags) ? dt : wxInvalidDateTime;
203 inline wxDateTime wxDateTime::GetLastMonthDay(Month month, int year) const
205 MODIFY_AND_RETURN( SetToLastMonthDay(month, year) );
208 inline wxDateTime wxDateTime::GetYearDay(wxDateTime_t yday) const
210 MODIFY_AND_RETURN( SetToYearDay(yday) );
213 // ----------------------------------------------------------------------------
214 // wxDateTime comparison
215 // ----------------------------------------------------------------------------
217 inline bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
219 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
221 return m_time == datetime.m_time;
224 inline bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
226 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
228 return m_time < datetime.m_time;
231 inline bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
233 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
235 return m_time > datetime.m_time;
238 inline bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1,
239 const wxDateTime& t2) const
241 // no need for assert, will be checked by the functions we call
242 return IsLaterThan(t1) && IsEarlierThan(t2);
245 inline bool wxDateTime::IsBetween(const wxDateTime& t1,
246 const wxDateTime& t2) const
248 // no need for assert, will be checked by the functions we call
249 return IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2);
252 inline bool wxDateTime::IsSameDate(const wxDateTime& dt) const
257 return tm1.year == tm2.year &&
258 tm1.mon == tm2.mon &&
259 tm1.mday == tm2.mday;
262 inline bool wxDateTime::IsSameTime(const wxDateTime& dt) const
264 // notice that we can't do something like this:
266 // m_time % MILLISECONDS_PER_DAY == dt.m_time % MILLISECONDS_PER_DAY
268 // because we have also to deal with (possibly) different DST settings!
272 return tm1.hour == tm2.hour &&
273 tm1.min == tm2.min &&
274 tm1.sec == tm2.sec &&
275 tm1.msec == tm2.msec;
278 inline bool wxDateTime::IsEqualUpTo(const wxDateTime& dt,
279 const wxTimeSpan& ts) const
281 return IsBetween(dt.Subtract(ts), dt.Add(ts));
284 // ----------------------------------------------------------------------------
285 // wxDateTime arithmetics
286 // ----------------------------------------------------------------------------
288 inline wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const
290 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
292 return wxDateTime(m_time + diff.GetValue());
295 inline wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
297 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
299 m_time += diff.GetValue();
304 inline wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
309 inline wxDateTime wxDateTime::Subtract(const wxTimeSpan& diff) const
311 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
313 return wxDateTime(m_time - diff.GetValue());
316 inline wxDateTime& wxDateTime::Subtract(const wxTimeSpan& diff)
318 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
320 m_time -= diff.GetValue();
325 inline wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
327 return Subtract(diff);
330 inline wxTimeSpan wxDateTime::Subtract(const wxDateTime& datetime) const
332 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
334 return wxTimeSpan(GetValue() - datetime.GetValue());
337 inline wxDateTime wxDateTime::Add(const wxDateSpan& diff) const
339 return wxDateTime(*this).Add(diff);
342 inline wxDateTime& wxDateTime::Subtract(const wxDateSpan& diff)
344 return Add(diff.Negate());
347 inline wxDateTime wxDateTime::Subtract(const wxDateSpan& diff) const
349 return wxDateTime(*this).Subtract(diff);
352 inline wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
354 return Subtract(diff);
357 inline wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
362 // ----------------------------------------------------------------------------
363 // wxDateTime and timezones
364 // ----------------------------------------------------------------------------
366 inline wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz,
369 MODIFY_AND_RETURN( MakeTimezone(tz, noDST) );
372 // ----------------------------------------------------------------------------
373 // wxTimeSpan construction
374 // ----------------------------------------------------------------------------
376 inline wxTimeSpan::wxTimeSpan(long hours,
381 // assign first to avoid precision loss
388 m_diff += milliseconds;
391 // ----------------------------------------------------------------------------
392 // wxTimeSpan accessors
393 // ----------------------------------------------------------------------------
395 inline wxLongLong wxTimeSpan::GetSeconds() const
397 return m_diff / 1000l;
400 inline int wxTimeSpan::GetMinutes() const
402 return (GetSeconds() / 60l).GetLo();
405 inline int wxTimeSpan::GetHours() const
407 return GetMinutes() / 60;
410 inline int wxTimeSpan::GetDays() const
412 return GetHours() / 24;
415 inline int wxTimeSpan::GetWeeks() const
417 return GetDays() / 7;
420 // ----------------------------------------------------------------------------
421 // wxTimeSpan arithmetics
422 // ----------------------------------------------------------------------------
424 inline wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const
426 return wxTimeSpan(m_diff + diff.GetValue());
429 inline wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
431 m_diff += diff.GetValue();
436 inline wxTimeSpan wxTimeSpan::Subtract(const wxTimeSpan& diff) const
438 return wxTimeSpan(m_diff - diff.GetValue());
441 inline wxTimeSpan& wxTimeSpan::Subtract(const wxTimeSpan& diff)
443 m_diff -= diff.GetValue();
448 inline wxTimeSpan& wxTimeSpan::Multiply(int n)
455 inline wxTimeSpan wxTimeSpan::Multiply(int n) const
457 return wxTimeSpan(m_diff * (long)n);
460 inline wxTimeSpan wxTimeSpan::Abs() const
462 return wxTimeSpan(GetValue().Abs());
465 inline bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
467 return GetValue() == ts.GetValue();
470 inline bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
472 return GetValue().Abs() > ts.GetValue().Abs();
475 // ----------------------------------------------------------------------------
477 // ----------------------------------------------------------------------------
479 inline wxDateSpan& wxDateSpan::operator+=(const wxDateSpan& other)
481 m_years += other.m_years;
482 m_months += other.m_months;
483 m_weeks += other.m_weeks;
484 m_days += other.m_days;
489 inline wxDateSpan& wxDateSpan::Add(const wxDateSpan& other)
491 return *this += other;
494 inline wxDateSpan wxDateSpan::Add(const wxDateSpan& other) const
496 wxDateSpan ds(*this);
501 inline wxDateSpan& wxDateSpan::Multiply(int factor)
511 inline wxDateSpan wxDateSpan::Multiply(int factor) const
513 wxDateSpan ds(*this);
518 inline wxDateSpan wxDateSpan::Negate() const
520 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
523 inline wxDateSpan& wxDateSpan::Neg()
526 m_months = -m_months;
533 inline wxDateSpan& wxDateSpan::operator-=(const wxDateSpan& other)
535 return *this += other.Negate();
538 inline wxDateSpan& wxDateSpan::Subtract(const wxDateSpan& other)
540 return *this -= other;
543 inline wxDateSpan wxDateSpan::Subtract(const wxDateSpan& other) const
545 wxDateSpan ds(*this);
550 #undef MILLISECONDS_PER_DAY
552 #undef MODIFY_AND_RETURN