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()
81 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
82 wxDateTime::wxDateTime(time_t timet)
88 wxDateTime::wxDateTime(const struct tm& tm)
93 wxDateTime::wxDateTime(const Tm& tm)
98 wxDateTime::wxDateTime(double jdn)
103 wxDateTime& wxDateTime::Set(const Tm& tm)
105 wxASSERT_MSG( tm.IsValid(), _T("invalid broken down date/time") );
107 return Set(tm.mday, (Month)tm.mon, tm.year, tm.hour, tm.min, tm.sec);
110 wxDateTime::wxDateTime(wxDateTime_t hour,
113 wxDateTime_t millisec)
115 Set(hour, minute, second, millisec);
118 wxDateTime::wxDateTime(wxDateTime_t day,
124 wxDateTime_t millisec)
126 Set(day, month, year, hour, minute, second, millisec);
129 // ----------------------------------------------------------------------------
130 // wxDateTime accessors
131 // ----------------------------------------------------------------------------
133 wxLongLong wxDateTime::GetValue() const
135 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
140 time_t wxDateTime::GetTicks() const
142 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
143 if ( !IsInStdRange() )
148 return (time_t)((m_time / (long)TIME_T_FACTOR).GetLo());
151 bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
155 return SetToWeekDay(weekday, -1, month, year);
158 wxDateTime wxDateTime::GetWeekDayInSameWeek(WeekDay weekday) const
160 MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday) );
163 wxDateTime wxDateTime::GetNextWeekDay(WeekDay weekday) const
165 MODIFY_AND_RETURN( SetToNextWeekDay(weekday) );
168 wxDateTime wxDateTime::GetPrevWeekDay(WeekDay weekday) const
170 MODIFY_AND_RETURN( SetToPrevWeekDay(weekday) );
173 wxDateTime wxDateTime::GetWeekDay(WeekDay weekday,
178 wxDateTime dt(*this);
180 return dt.SetToWeekDay(weekday, n, month, year) ? dt : wxInvalidDateTime;
183 wxDateTime wxDateTime::GetLastWeekDay(WeekDay weekday,
187 wxDateTime dt(*this);
189 return dt.SetToLastWeekDay(weekday, month, year) ? dt : wxInvalidDateTime;
192 wxDateTime wxDateTime::GetWeek(wxDateTime_t numWeek, WeekDay weekday) const
194 wxDateTime dt(*this);
196 return dt.SetToTheWeek(numWeek, weekday) ? dt : wxInvalidDateTime;
199 wxDateTime wxDateTime::GetLastMonthDay(Month month, int year) const
201 MODIFY_AND_RETURN( SetToLastMonthDay(month, year) );
204 wxDateTime wxDateTime::GetYearDay(wxDateTime_t yday) const
206 MODIFY_AND_RETURN( SetToYearDay(yday) );
209 // ----------------------------------------------------------------------------
210 // wxDateTime comparison
211 // ----------------------------------------------------------------------------
213 bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
215 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
217 return m_time == datetime.m_time;
220 bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
222 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
224 return m_time < datetime.m_time;
227 bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
229 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
231 return m_time > datetime.m_time;
234 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 bool wxDateTime::IsBetween(const wxDateTime& t1, 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 bool wxDateTime::IsSameDate(const wxDateTime& dt) const
249 return (m_time - dt.m_time).Abs() < MILLISECONDS_PER_DAY;
252 bool wxDateTime::IsSameTime(const wxDateTime& dt) const
254 // notice that we can't do something like this:
256 // m_time % MILLISECONDS_PER_DAY == dt.m_time % MILLISECONDS_PER_DAY
258 // because we have also to deal with (possibly) different DST settings!
262 return tm1.hour == tm2.hour &&
263 tm1.min == tm2.min &&
264 tm1.sec == tm2.sec &&
265 tm1.msec == tm2.msec;
268 bool wxDateTime::IsEqualUpTo(const wxDateTime& dt, const wxTimeSpan& ts) const
270 return IsBetween(dt.Substract(ts), dt.Add(ts));
273 // ----------------------------------------------------------------------------
274 // wxDateTime arithmetics
275 // ----------------------------------------------------------------------------
277 wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const
279 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
281 return wxDateTime(m_time + diff.GetValue());
284 wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
286 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
288 m_time += diff.GetValue();
293 wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
298 wxDateTime wxDateTime::Substract(const wxTimeSpan& diff) const
300 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
302 return wxDateTime(m_time - diff.GetValue());
305 wxDateTime& wxDateTime::Substract(const wxTimeSpan& diff)
307 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
309 m_time -= diff.GetValue();
314 wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
316 return Substract(diff);
319 wxTimeSpan wxDateTime::Substract(const wxDateTime& datetime) const
321 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
323 return wxTimeSpan(datetime.GetValue() - GetValue());
326 wxDateTime wxDateTime::Add(const wxDateSpan& diff) const
328 return wxDateTime(*this).Add(diff);
331 wxDateTime& wxDateTime::Substract(const wxDateSpan& diff)
333 return Add(diff.Negate());
336 wxDateTime wxDateTime::Substract(const wxDateSpan& diff) const
338 return wxDateTime(*this).Substract(diff);
341 wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
343 return Substract(diff);
346 wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
351 // ----------------------------------------------------------------------------
352 // wxDateTime and timezones
353 // ----------------------------------------------------------------------------
355 wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz,
358 MODIFY_AND_RETURN( MakeTimezone(tz, noDST) );
361 // ----------------------------------------------------------------------------
362 // wxTimeSpan construction
363 // ----------------------------------------------------------------------------
365 wxTimeSpan::wxTimeSpan(long hours, long minutes, long seconds, long milliseconds)
367 // assign first to avoid precision loss
374 m_diff += milliseconds;
377 // ----------------------------------------------------------------------------
378 // wxTimeSpan accessors
379 // ----------------------------------------------------------------------------
381 wxLongLong wxTimeSpan::GetSeconds() const
383 return m_diff / 1000l;
386 int wxTimeSpan::GetMinutes() const
388 return (GetSeconds() / 60l).GetLo();
391 int wxTimeSpan::GetHours() const
393 return GetMinutes() / 60;
396 int wxTimeSpan::GetDays() const
398 return GetHours() / 24;
401 int wxTimeSpan::GetWeeks() const
403 return GetDays() / 7;
406 // ----------------------------------------------------------------------------
407 // wxTimeSpan arithmetics
408 // ----------------------------------------------------------------------------
410 wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const
412 return wxTimeSpan(m_diff + diff.GetValue());
415 wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
417 m_diff += diff.GetValue();
422 wxTimeSpan wxTimeSpan::Substract(const wxTimeSpan& diff) const
424 return wxTimeSpan(m_diff - diff.GetValue());
427 wxTimeSpan& wxTimeSpan::Substract(const wxTimeSpan& diff)
429 m_diff -= diff.GetValue();
434 wxTimeSpan& wxTimeSpan::Multiply(int n)
441 wxTimeSpan wxTimeSpan::Multiply(int n) const
443 return wxTimeSpan(m_diff * (long)n);
446 wxTimeSpan wxTimeSpan::Abs() const
448 return wxTimeSpan(GetValue().Abs());
451 bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
453 return GetValue() == ts.GetValue();
456 bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
458 return GetValue().Abs() > ts.GetValue().Abs();
461 // ----------------------------------------------------------------------------
463 // ----------------------------------------------------------------------------
466 wxDateSpan::operator+=(const wxDateSpan& other)
468 m_years += other.m_years;
469 m_months += other.m_months;
470 m_weeks += other.m_weeks;
471 m_days += other.m_days;
476 wxDateSpan& wxDateSpan::Multiply(int factor)
486 wxDateSpan wxDateSpan::Multiply(int factor) const
488 return wxDateSpan(*this).Multiply(factor);
491 wxDateSpan wxDateSpan::Negate() const
493 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
496 wxDateSpan& wxDateSpan::Neg()
499 m_months = -m_months;
506 #undef MILLISECONDS_PER_DAY
508 #undef MODIFY_AND_RETURN