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, tm.hour, tm.min, tm.sec);
106 inline wxDateTime::wxDateTime(wxDateTime_t hour,
109 wxDateTime_t millisec)
111 Set(hour, minute, second, millisec);
114 inline wxDateTime::wxDateTime(wxDateTime_t day,
120 wxDateTime_t millisec)
122 Set(day, month, year, hour, minute, second, millisec);
125 // ----------------------------------------------------------------------------
126 // wxDateTime accessors
127 // ----------------------------------------------------------------------------
129 inline wxLongLong wxDateTime::GetValue() const
131 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
136 inline time_t wxDateTime::GetTicks() const
138 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
139 if ( !IsInStdRange() )
144 return (time_t)((m_time / (long)TIME_T_FACTOR).GetLo())+WX_TIME_BASE_OFFSET ;
147 inline bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
151 return SetToWeekDay(weekday, -1, month, year);
154 inline wxDateTime wxDateTime::GetWeekDayInSameWeek(WeekDay weekday,
155 WeekFlags flags) const
157 MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday) );
160 inline wxDateTime wxDateTime::GetNextWeekDay(WeekDay weekday) const
162 MODIFY_AND_RETURN( SetToNextWeekDay(weekday) );
165 inline wxDateTime wxDateTime::GetPrevWeekDay(WeekDay weekday) const
167 MODIFY_AND_RETURN( SetToPrevWeekDay(weekday) );
170 inline wxDateTime wxDateTime::GetWeekDay(WeekDay weekday,
175 wxDateTime dt(*this);
177 return dt.SetToWeekDay(weekday, n, month, year) ? dt : wxInvalidDateTime;
180 inline wxDateTime wxDateTime::GetLastWeekDay(WeekDay weekday,
184 wxDateTime dt(*this);
186 return dt.SetToLastWeekDay(weekday, month, year) ? dt : wxInvalidDateTime;
189 inline wxDateTime wxDateTime::GetWeek(wxDateTime_t numWeek,
191 WeekFlags flags) const
193 wxDateTime dt(*this);
195 return dt.SetToTheWeek(numWeek, weekday, flags) ? dt : wxInvalidDateTime;
198 inline wxDateTime wxDateTime::GetLastMonthDay(Month month, int year) const
200 MODIFY_AND_RETURN( SetToLastMonthDay(month, year) );
203 inline wxDateTime wxDateTime::GetYearDay(wxDateTime_t yday) const
205 MODIFY_AND_RETURN( SetToYearDay(yday) );
208 // ----------------------------------------------------------------------------
209 // wxDateTime comparison
210 // ----------------------------------------------------------------------------
212 inline bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
214 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
216 return m_time == datetime.m_time;
219 inline bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
221 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
223 return m_time < datetime.m_time;
226 inline bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
228 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
230 return m_time > datetime.m_time;
233 inline bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1,
234 const wxDateTime& t2) const
236 // no need for assert, will be checked by the functions we call
237 return IsLaterThan(t1) && IsEarlierThan(t2);
240 inline bool wxDateTime::IsBetween(const wxDateTime& t1,
241 const wxDateTime& t2) const
243 // no need for assert, will be checked by the functions we call
244 return IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2);
247 inline bool wxDateTime::IsSameDate(const wxDateTime& dt) const
252 return tm1.year == tm2.year &&
253 tm1.mon == tm2.mon &&
254 tm1.mday == tm2.mday;
257 inline bool wxDateTime::IsSameTime(const wxDateTime& dt) const
259 // notice that we can't do something like this:
261 // m_time % MILLISECONDS_PER_DAY == dt.m_time % MILLISECONDS_PER_DAY
263 // because we have also to deal with (possibly) different DST settings!
267 return tm1.hour == tm2.hour &&
268 tm1.min == tm2.min &&
269 tm1.sec == tm2.sec &&
270 tm1.msec == tm2.msec;
273 inline bool wxDateTime::IsEqualUpTo(const wxDateTime& dt,
274 const wxTimeSpan& ts) const
276 return IsBetween(dt.Subtract(ts), dt.Add(ts));
279 // ----------------------------------------------------------------------------
280 // wxDateTime arithmetics
281 // ----------------------------------------------------------------------------
283 inline wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const
285 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
287 return wxDateTime(m_time + diff.GetValue());
290 inline wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
292 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
294 m_time += diff.GetValue();
299 inline wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
304 inline wxDateTime wxDateTime::Subtract(const wxTimeSpan& diff) const
306 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
308 return wxDateTime(m_time - diff.GetValue());
311 inline wxDateTime& wxDateTime::Subtract(const wxTimeSpan& diff)
313 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
315 m_time -= diff.GetValue();
320 inline wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
322 return Subtract(diff);
325 inline wxTimeSpan wxDateTime::Subtract(const wxDateTime& datetime) const
327 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
329 return wxTimeSpan(GetValue() - datetime.GetValue());
332 inline wxDateTime wxDateTime::Add(const wxDateSpan& diff) const
334 return wxDateTime(*this).Add(diff);
337 inline wxDateTime& wxDateTime::Subtract(const wxDateSpan& diff)
339 return Add(diff.Negate());
342 inline wxDateTime wxDateTime::Subtract(const wxDateSpan& diff) const
344 return wxDateTime(*this).Subtract(diff);
347 inline wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
349 return Subtract(diff);
352 inline wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
357 // ----------------------------------------------------------------------------
358 // wxDateTime and timezones
359 // ----------------------------------------------------------------------------
361 inline wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz,
364 MODIFY_AND_RETURN( MakeTimezone(tz, noDST) );
367 // ----------------------------------------------------------------------------
368 // wxTimeSpan construction
369 // ----------------------------------------------------------------------------
371 inline wxTimeSpan::wxTimeSpan(long hours,
376 // assign first to avoid precision loss
383 m_diff += milliseconds;
386 // ----------------------------------------------------------------------------
387 // wxTimeSpan accessors
388 // ----------------------------------------------------------------------------
390 inline wxLongLong wxTimeSpan::GetSeconds() const
392 return m_diff / 1000l;
395 inline int wxTimeSpan::GetMinutes() const
397 return (GetSeconds() / 60l).GetLo();
400 inline int wxTimeSpan::GetHours() const
402 return GetMinutes() / 60;
405 inline int wxTimeSpan::GetDays() const
407 return GetHours() / 24;
410 inline int wxTimeSpan::GetWeeks() const
412 return GetDays() / 7;
415 // ----------------------------------------------------------------------------
416 // wxTimeSpan arithmetics
417 // ----------------------------------------------------------------------------
419 inline wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const
421 return wxTimeSpan(m_diff + diff.GetValue());
424 inline wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
426 m_diff += diff.GetValue();
431 inline wxTimeSpan wxTimeSpan::Subtract(const wxTimeSpan& diff) const
433 return wxTimeSpan(m_diff - diff.GetValue());
436 inline wxTimeSpan& wxTimeSpan::Subtract(const wxTimeSpan& diff)
438 m_diff -= diff.GetValue();
443 inline wxTimeSpan& wxTimeSpan::Multiply(int n)
450 inline wxTimeSpan wxTimeSpan::Multiply(int n) const
452 return wxTimeSpan(m_diff * (long)n);
455 inline wxTimeSpan wxTimeSpan::Abs() const
457 return wxTimeSpan(GetValue().Abs());
460 inline bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
462 return GetValue() == ts.GetValue();
465 inline bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
467 return GetValue().Abs() > ts.GetValue().Abs();
470 // ----------------------------------------------------------------------------
472 // ----------------------------------------------------------------------------
474 inline wxDateSpan& wxDateSpan::operator+=(const wxDateSpan& other)
476 m_years += other.m_years;
477 m_months += other.m_months;
478 m_weeks += other.m_weeks;
479 m_days += other.m_days;
484 inline wxDateSpan& wxDateSpan::Add(const wxDateSpan& other)
486 return *this += other;
489 inline wxDateSpan wxDateSpan::Add(const wxDateSpan& other) const
491 wxDateSpan ds(*this);
496 inline wxDateSpan& wxDateSpan::Multiply(int factor)
506 inline wxDateSpan wxDateSpan::Multiply(int factor) const
508 wxDateSpan ds(*this);
513 inline wxDateSpan wxDateSpan::Negate() const
515 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
518 inline wxDateSpan& wxDateSpan::Neg()
521 m_months = -m_months;
528 inline wxDateSpan& wxDateSpan::operator-=(const wxDateSpan& other)
530 return *this += other.Negate();
533 inline wxDateSpan& wxDateSpan::Subtract(const wxDateSpan& other)
535 return *this -= other;
538 inline wxDateSpan wxDateSpan::Subtract(const wxDateSpan& other) const
540 wxDateSpan ds(*this);
545 #undef MILLISECONDS_PER_DAY
547 #undef MODIFY_AND_RETURN