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 #ifdef wxDEFINE_TIME_CONSTANTS
40 const long wxDateTime::TIME_T_FACTOR = 1000l;
41 #endif // wxDEFINE_TIME_CONSTANTS
43 bool wxDateTime::IsInStdRange() const
45 return m_time >= 0l && (m_time / TIME_T_FACTOR) < LONG_MAX;
49 wxDateTime wxDateTime::Now()
51 return wxDateTime(*GetTmNow());
55 wxDateTime wxDateTime::Today()
57 struct tm *tm = GetTmNow();
62 return wxDateTime(*tm);
65 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
66 wxDateTime& wxDateTime::Set(time_t timet)
68 // assign first to avoid long multiplication overflow!
70 m_time *= TIME_T_FACTOR;
76 wxDateTime& wxDateTime::SetToCurrent()
82 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
83 wxDateTime::wxDateTime(time_t timet)
89 wxDateTime::wxDateTime(const struct tm& tm)
94 wxDateTime::wxDateTime(const Tm& tm)
99 wxDateTime::wxDateTime(double jdn)
104 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 wxDateTime::wxDateTime(wxDateTime_t hour,
114 wxDateTime_t millisec)
116 Set(hour, minute, second, millisec);
119 wxDateTime::wxDateTime(wxDateTime_t day,
125 wxDateTime_t millisec)
127 Set(day, month, year, hour, minute, second, millisec);
130 // ----------------------------------------------------------------------------
131 // wxDateTime accessors
132 // ----------------------------------------------------------------------------
134 wxLongLong wxDateTime::GetValue() const
136 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
141 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());
152 bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
156 return SetToWeekDay(weekday, -1, month, year);
159 wxDateTime wxDateTime::GetWeekDayInSameWeek(WeekDay weekday) const
161 MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday) );
164 wxDateTime wxDateTime::GetNextWeekDay(WeekDay weekday) const
166 MODIFY_AND_RETURN( SetToNextWeekDay(weekday) );
169 wxDateTime wxDateTime::GetPrevWeekDay(WeekDay weekday) const
171 MODIFY_AND_RETURN( SetToPrevWeekDay(weekday) );
174 wxDateTime wxDateTime::GetWeekDay(WeekDay weekday,
179 wxDateTime dt(*this);
181 return dt.SetToWeekDay(weekday, n, month, year) ? dt : wxInvalidDateTime;
184 wxDateTime wxDateTime::GetLastWeekDay(WeekDay weekday,
188 wxDateTime dt(*this);
190 return dt.SetToLastWeekDay(weekday, month, year) ? dt : wxInvalidDateTime;
193 wxDateTime wxDateTime::GetWeek(wxDateTime_t numWeek, WeekDay weekday) const
195 wxDateTime dt(*this);
197 return dt.SetToTheWeek(numWeek, weekday) ? dt : wxInvalidDateTime;
200 wxDateTime wxDateTime::GetLastMonthDay(Month month, int year) const
202 MODIFY_AND_RETURN( SetToLastMonthDay(month, year) );
205 wxDateTime wxDateTime::GetYearDay(wxDateTime_t yday) const
207 MODIFY_AND_RETURN( SetToYearDay(yday) );
210 // ----------------------------------------------------------------------------
211 // wxDateTime comparison
212 // ----------------------------------------------------------------------------
214 bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
216 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
218 return m_time == datetime.m_time;
221 bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
223 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
225 return m_time < datetime.m_time;
228 bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
230 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
232 return m_time > datetime.m_time;
235 bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1,
236 const wxDateTime& t2) const
238 // no need for assert, will be checked by the functions we call
239 return IsLaterThan(t1) && IsEarlierThan(t2);
242 bool wxDateTime::IsBetween(const wxDateTime& t1, 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 bool wxDateTime::IsSameDate(const wxDateTime& dt) const
250 return (m_time - dt.m_time).Abs() < MILLISECONDS_PER_DAY;
253 bool wxDateTime::IsSameTime(const wxDateTime& dt) const
255 // notice that we can't do something like this:
257 // m_time % MILLISECONDS_PER_DAY == dt.m_time % MILLISECONDS_PER_DAY
259 // because we have also to deal with (possibly) different DST settings!
263 return tm1.hour == tm2.hour &&
264 tm1.min == tm2.min &&
265 tm1.sec == tm2.sec &&
266 tm1.msec == tm2.msec;
269 bool wxDateTime::IsEqualUpTo(const wxDateTime& dt, const wxTimeSpan& ts) const
271 return IsBetween(dt.Substract(ts), dt.Add(ts));
274 // ----------------------------------------------------------------------------
275 // wxDateTime arithmetics
276 // ----------------------------------------------------------------------------
278 wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const
280 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
282 return wxDateTime(m_time + diff.GetValue());
285 wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
287 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
289 m_time += diff.GetValue();
294 wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
299 wxDateTime wxDateTime::Substract(const wxTimeSpan& diff) const
301 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
303 return wxDateTime(m_time - diff.GetValue());
306 wxDateTime& wxDateTime::Substract(const wxTimeSpan& diff)
308 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
310 m_time -= diff.GetValue();
315 wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
317 return Substract(diff);
320 wxTimeSpan wxDateTime::Substract(const wxDateTime& datetime) const
322 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
324 return wxTimeSpan(datetime.GetValue() - GetValue());
327 wxDateTime wxDateTime::Add(const wxDateSpan& diff) const
329 return wxDateTime(*this).Add(diff);
332 wxDateTime& wxDateTime::Substract(const wxDateSpan& diff)
334 return Add(diff.Negate());
337 wxDateTime wxDateTime::Substract(const wxDateSpan& diff) const
339 return wxDateTime(*this).Substract(diff);
342 wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
344 return Substract(diff);
347 wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
352 // ----------------------------------------------------------------------------
353 // wxDateTime and timezones
354 // ----------------------------------------------------------------------------
356 wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz,
359 MODIFY_AND_RETURN( MakeTimezone(tz, noDST) );
362 // ----------------------------------------------------------------------------
363 // wxTimeSpan construction
364 // ----------------------------------------------------------------------------
366 wxTimeSpan::wxTimeSpan(long hours, long minutes, long seconds, long milliseconds)
368 // assign first to avoid precision loss
375 m_diff += milliseconds;
378 // ----------------------------------------------------------------------------
379 // wxTimeSpan accessors
380 // ----------------------------------------------------------------------------
382 wxLongLong wxTimeSpan::GetSeconds() const
384 return m_diff / 1000l;
387 int wxTimeSpan::GetMinutes() const
389 return (GetSeconds() / 60l).GetLo();
392 int wxTimeSpan::GetHours() const
394 return GetMinutes() / 60;
397 int wxTimeSpan::GetDays() const
399 return GetHours() / 24;
402 int wxTimeSpan::GetWeeks() const
404 return GetDays() / 7;
407 // ----------------------------------------------------------------------------
408 // wxTimeSpan arithmetics
409 // ----------------------------------------------------------------------------
411 wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const
413 return wxTimeSpan(m_diff + diff.GetValue());
416 wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
418 m_diff += diff.GetValue();
423 wxTimeSpan wxTimeSpan::Substract(const wxTimeSpan& diff) const
425 return wxTimeSpan(m_diff - diff.GetValue());
428 wxTimeSpan& wxTimeSpan::Substract(const wxTimeSpan& diff)
430 m_diff -= diff.GetValue();
435 wxTimeSpan& wxTimeSpan::Multiply(int n)
442 wxTimeSpan wxTimeSpan::Multiply(int n) const
444 return wxTimeSpan(m_diff * (long)n);
447 wxTimeSpan wxTimeSpan::Abs() const
449 return wxTimeSpan(GetValue().Abs());
452 bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
454 return GetValue() == ts.GetValue();
457 bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
459 return GetValue().Abs() > ts.GetValue().Abs();
462 // ----------------------------------------------------------------------------
464 // ----------------------------------------------------------------------------
467 wxDateSpan::operator+=(const wxDateSpan& other)
469 m_years += other.m_years;
470 m_months += other.m_months;
471 m_weeks += other.m_weeks;
472 m_days += other.m_days;
477 wxDateSpan& wxDateSpan::Multiply(int factor)
487 wxDateSpan wxDateSpan::Multiply(int factor) const
489 return wxDateSpan(*this).Multiply(factor);
492 wxDateSpan wxDateSpan::Negate() const
494 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
497 wxDateSpan& wxDateSpan::Neg()
500 m_months = -m_months;
507 #undef MILLISECONDS_PER_DAY
509 #undef MODIFY_AND_RETURN