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 wxDateTime& wxDateTime::Set(time_t timet)
67 // assign first to avoid long multiplication overflow!
69 m_time *= TIME_T_FACTOR;
74 wxDateTime& wxDateTime::SetToCurrent()
79 wxDateTime::wxDateTime(time_t timet)
84 wxDateTime::wxDateTime(const struct tm& tm)
89 wxDateTime::wxDateTime(const Tm& tm)
94 wxDateTime::wxDateTime(double jdn)
99 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 wxDateTime::wxDateTime(wxDateTime_t hour,
109 wxDateTime_t millisec)
111 Set(hour, minute, second, millisec);
114 wxDateTime::wxDateTime(wxDateTime_t day,
120 wxDateTime_t millisec)
122 Set(day, month, year, hour, minute, second, millisec);
125 // ----------------------------------------------------------------------------
126 // wxDateTime accessors
127 // ----------------------------------------------------------------------------
129 wxLongLong wxDateTime::GetValue() const
131 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
136 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());
147 bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
151 return SetToWeekDay(weekday, -1, month, year);
154 wxDateTime wxDateTime::GetWeekDayInSameWeek(WeekDay weekday) const
156 MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday) );
159 wxDateTime wxDateTime::GetNextWeekDay(WeekDay weekday) const
161 MODIFY_AND_RETURN( SetToNextWeekDay(weekday) );
164 wxDateTime wxDateTime::GetPrevWeekDay(WeekDay weekday) const
166 MODIFY_AND_RETURN( SetToPrevWeekDay(weekday) );
169 wxDateTime wxDateTime::GetWeekDay(WeekDay weekday,
174 wxDateTime dt(*this);
176 return dt.SetToWeekDay(weekday, n, month, year) ? dt : wxInvalidDateTime;
179 wxDateTime wxDateTime::GetLastWeekDay(WeekDay weekday,
183 wxDateTime dt(*this);
185 return dt.SetToLastWeekDay(weekday, month, year) ? dt : wxInvalidDateTime;
188 wxDateTime wxDateTime::GetWeek(wxDateTime_t numWeek, WeekDay weekday) const
190 wxDateTime dt(*this);
192 return dt.SetToTheWeek(numWeek, weekday) ? dt : wxInvalidDateTime;
195 wxDateTime wxDateTime::GetLastMonthDay(Month month, int year) const
197 MODIFY_AND_RETURN( SetToLastMonthDay(month, year) );
200 wxDateTime wxDateTime::GetYearDay(wxDateTime_t yday) const
202 MODIFY_AND_RETURN( SetToYearDay(yday) );
205 // ----------------------------------------------------------------------------
206 // wxDateTime comparison
207 // ----------------------------------------------------------------------------
209 bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
211 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
213 return m_time == datetime.m_time;
216 bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
218 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
220 return m_time < datetime.m_time;
223 bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
225 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
227 return m_time > datetime.m_time;
230 bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1,
231 const wxDateTime& t2) const
233 // no need for assert, will be checked by the functions we call
234 return IsLaterThan(t1) && IsEarlierThan(t2);
237 bool wxDateTime::IsBetween(const wxDateTime& t1, const wxDateTime& t2) const
239 // no need for assert, will be checked by the functions we call
240 return IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2);
243 bool wxDateTime::IsSameDate(const wxDateTime& dt) const
245 return (m_time - dt.m_time).Abs() < MILLISECONDS_PER_DAY;
248 bool wxDateTime::IsSameTime(const wxDateTime& dt) const
250 // notice that we can't do something like this:
252 // m_time % MILLISECONDS_PER_DAY == dt.m_time % MILLISECONDS_PER_DAY
254 // because we have also to deal with (possibly) different DST settings!
258 return tm1.hour == tm2.hour &&
259 tm1.min == tm2.min &&
260 tm1.sec == tm2.sec &&
261 tm1.msec == tm2.msec;
264 bool wxDateTime::IsEqualUpTo(const wxDateTime& dt, const wxTimeSpan& ts) const
266 return IsBetween(dt.Substract(ts), dt.Add(ts));
269 // ----------------------------------------------------------------------------
270 // wxDateTime arithmetics
271 // ----------------------------------------------------------------------------
273 wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const
275 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
277 return wxDateTime(m_time + diff.GetValue());
280 wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
282 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
284 m_time += diff.GetValue();
289 wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
294 wxDateTime wxDateTime::Substract(const wxTimeSpan& diff) const
296 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
298 return wxDateTime(m_time - diff.GetValue());
301 wxDateTime& wxDateTime::Substract(const wxTimeSpan& diff)
303 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
305 m_time -= diff.GetValue();
310 wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
312 return Substract(diff);
315 wxTimeSpan wxDateTime::Substract(const wxDateTime& datetime) const
317 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
319 return wxTimeSpan(datetime.GetValue() - GetValue());
322 wxDateTime wxDateTime::Add(const wxDateSpan& diff) const
324 return wxDateTime(*this).Add(diff);
327 wxDateTime& wxDateTime::Substract(const wxDateSpan& diff)
329 return Add(diff.Negate());
332 wxDateTime wxDateTime::Substract(const wxDateSpan& diff) const
334 return wxDateTime(*this).Substract(diff);
337 wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
339 return Substract(diff);
342 wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
347 // ----------------------------------------------------------------------------
348 // wxDateTime and timezones
349 // ----------------------------------------------------------------------------
351 wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz,
354 MODIFY_AND_RETURN( MakeTimezone(tz, noDST) );
357 // ----------------------------------------------------------------------------
358 // wxTimeSpan construction
359 // ----------------------------------------------------------------------------
361 wxTimeSpan::wxTimeSpan(int hours, int minutes, int seconds, int milliseconds)
363 // assign first to avoid precision loss
364 m_diff = (long)hours;
370 m_diff += milliseconds;
373 // ----------------------------------------------------------------------------
374 // wxTimeSpan accessors
375 // ----------------------------------------------------------------------------
377 wxLongLong wxTimeSpan::GetSeconds() const
379 return m_diff / 1000l;
382 int wxTimeSpan::GetMinutes() const
384 return (GetSeconds() / 60l).GetLo();
387 int wxTimeSpan::GetHours() const
389 return GetMinutes() / 60;
392 int wxTimeSpan::GetDays() const
394 return GetHours() / 24;
397 int wxTimeSpan::GetWeeks() const
399 return GetDays() / 7;
402 // ----------------------------------------------------------------------------
403 // wxTimeSpan arithmetics
404 // ----------------------------------------------------------------------------
406 wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const
408 return wxTimeSpan(m_diff + diff.GetValue());
411 wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
413 m_diff += diff.GetValue();
418 wxTimeSpan wxTimeSpan::Substract(const wxTimeSpan& diff) const
420 return wxTimeSpan(m_diff - diff.GetValue());
423 wxTimeSpan& wxTimeSpan::Substract(const wxTimeSpan& diff)
425 m_diff -= diff.GetValue();
430 wxTimeSpan& wxTimeSpan::Multiply(int n)
437 wxTimeSpan wxTimeSpan::Multiply(int n) const
439 return wxTimeSpan(m_diff * (long)n);
442 wxTimeSpan wxTimeSpan::Abs() const
444 return wxTimeSpan(GetValue().Abs());
447 bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
449 return GetValue() == ts.GetValue();
452 bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
454 return GetValue().Abs() > ts.GetValue().Abs();
457 // ----------------------------------------------------------------------------
459 // ----------------------------------------------------------------------------
462 wxDateSpan::operator+=(const wxDateSpan& other)
464 m_years += other.m_years;
465 m_months += other.m_months;
466 m_weeks += other.m_weeks;
467 m_days += other.m_days;
472 wxDateSpan& wxDateSpan::Multiply(int factor)
482 wxDateSpan wxDateSpan::Multiply(int factor) const
484 return wxDateSpan(*this).Multiply(factor);
487 wxDateSpan wxDateSpan::Negate() const
489 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
492 wxDateSpan& wxDateSpan::Neg()
495 m_months = -m_months;
502 #undef MILLISECONDS_PER_DAY
504 #undef MODIFY_AND_RETURN