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 #define MILLISECONDS_PER_DAY 86400000l
21 // ----------------------------------------------------------------------------
22 // wxDateTime construction
23 // ----------------------------------------------------------------------------
25 // only define this once, when included from datetime.cpp
26 #ifdef wxDEFINE_TIME_CONSTANTS
27 const long wxDateTime::TIME_T_FACTOR = 1000l;
28 #endif // wxDEFINE_TIME_CONSTANTS
30 bool wxDateTime::IsInStdRange() const
32 return m_time >= 0l && (m_time / TIME_T_FACTOR) < LONG_MAX;
36 wxDateTime wxDateTime::Now()
38 return wxDateTime(GetTimeNow());
42 wxDateTime wxDateTime::Today()
44 return wxDateTime((time_t)(86400*(GetTimeNow() / 86400)));
47 wxDateTime& wxDateTime::Set(time_t timet)
49 // assign first to avoid long multiplication overflow!
51 m_time *= TIME_T_FACTOR;
56 wxDateTime& wxDateTime::SetToCurrent()
58 return Set(GetTimeNow());
61 wxDateTime::wxDateTime(time_t timet)
66 wxDateTime::wxDateTime(const struct tm& tm)
71 wxDateTime::wxDateTime(const Tm& tm)
76 wxDateTime::wxDateTime(double jdn)
81 wxDateTime& wxDateTime::Set(const Tm& tm)
83 wxASSERT_MSG( tm.IsValid(), _T("invalid broken down date/time") );
85 return Set(tm.mday, (Month)tm.mon, tm.year, tm.hour, tm.min, tm.sec);
88 wxDateTime::wxDateTime(wxDateTime_t hour,
91 wxDateTime_t millisec)
93 Set(hour, minute, second, millisec);
96 wxDateTime::wxDateTime(wxDateTime_t day,
102 wxDateTime_t millisec)
104 Set(day, month, year, hour, minute, second, millisec);
107 // ----------------------------------------------------------------------------
108 // wxDateTime accessors
109 // ----------------------------------------------------------------------------
111 wxLongLong wxDateTime::GetValue() const
113 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
118 time_t wxDateTime::GetTicks() const
120 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
121 if ( !IsInStdRange() )
126 return (time_t)((m_time / (long)TIME_T_FACTOR).GetLo());
129 bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
133 return SetToWeekDay(weekday, -1, month, year);
136 // ----------------------------------------------------------------------------
137 // wxDateTime comparison
138 // ----------------------------------------------------------------------------
140 bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
142 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
144 return m_time == datetime.m_time;
147 bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
149 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
151 return m_time < datetime.m_time;
154 bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
156 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
158 return m_time > datetime.m_time;
161 bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1,
162 const wxDateTime& t2) const
164 // no need for assert, will be checked by the functions we call
165 return IsLaterThan(t1) && IsEarlierThan(t2);
168 bool wxDateTime::IsBetween(const wxDateTime& t1, const wxDateTime& t2) const
170 // no need for assert, will be checked by the functions we call
171 return IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2);
174 bool wxDateTime::IsSameDate(const wxDateTime& dt) const
176 return (m_time - dt.m_time).Abs() < MILLISECONDS_PER_DAY;
179 bool wxDateTime::IsSameTime(const wxDateTime& dt) const
181 // notice that we can't do something like this:
183 // m_time % MILLISECONDS_PER_DAY == dt.m_time % MILLISECONDS_PER_DAY
185 // because we have also to deal with (possibly) different DST settings!
189 return tm1.hour == tm2.hour &&
190 tm1.min == tm2.min &&
191 tm1.sec == tm2.sec &&
192 tm1.msec == tm2.msec;
195 bool wxDateTime::IsEqualUpTo(const wxDateTime& dt, const wxTimeSpan& ts) const
197 return IsBetween(dt.Substract(ts), dt.Add(ts));
200 // ----------------------------------------------------------------------------
201 // wxDateTime arithmetics
202 // ----------------------------------------------------------------------------
204 wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const
206 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
208 return wxDateTime(m_time + diff.GetValue());
211 wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
213 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
215 m_time += diff.GetValue();
220 wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
225 wxDateTime wxDateTime::Substract(const wxTimeSpan& diff) const
227 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
229 return wxDateTime(m_time - diff.GetValue());
232 wxDateTime& wxDateTime::Substract(const wxTimeSpan& diff)
234 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
236 m_time -= diff.GetValue();
241 wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
243 return Substract(diff);
246 wxTimeSpan wxDateTime::Substract(const wxDateTime& datetime) const
248 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
250 return wxTimeSpan(datetime.GetValue() - GetValue());
253 wxDateTime wxDateTime::Add(const wxDateSpan& diff) const
255 return wxDateTime(*this).Add(diff);
258 wxDateTime& wxDateTime::Substract(const wxDateSpan& diff)
260 return Add(diff.Negate());
263 wxDateTime wxDateTime::Substract(const wxDateSpan& diff) const
265 return wxDateTime(*this).Substract(diff);
268 wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
270 return Substract(diff);
273 wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
278 // ----------------------------------------------------------------------------
279 // wxDateTime and timezones
280 // ----------------------------------------------------------------------------
282 wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz) const
284 return wxDateTime(*this).MakeTimezone(tz);
287 // ----------------------------------------------------------------------------
288 // wxTimeSpan construction
289 // ----------------------------------------------------------------------------
291 wxTimeSpan::wxTimeSpan(int hours, int minutes, int seconds, int milliseconds)
293 // assign first to avoid precision loss
294 m_diff = (long)hours;
300 m_diff += milliseconds;
303 // ----------------------------------------------------------------------------
304 // wxTimeSpan accessors
305 // ----------------------------------------------------------------------------
307 wxLongLong wxTimeSpan::GetSeconds() const
309 return m_diff / 1000l;
312 int wxTimeSpan::GetMinutes() const
314 return (GetSeconds() / 60l).GetLo();
317 int wxTimeSpan::GetHours() const
319 return GetMinutes() / 60;
322 int wxTimeSpan::GetDays() const
324 return GetHours() / 24;
327 int wxTimeSpan::GetWeeks() const
329 return GetDays() / 7;
332 // ----------------------------------------------------------------------------
333 // wxTimeSpan arithmetics
334 // ----------------------------------------------------------------------------
336 wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const
338 return wxTimeSpan(m_diff + diff.GetValue());
341 wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
343 m_diff += diff.GetValue();
348 wxTimeSpan wxTimeSpan::Substract(const wxTimeSpan& diff) const
350 return wxTimeSpan(m_diff - diff.GetValue());
353 wxTimeSpan& wxTimeSpan::Substract(const wxTimeSpan& diff)
355 m_diff -= diff.GetValue();
360 wxTimeSpan& wxTimeSpan::Multiply(int n)
367 wxTimeSpan wxTimeSpan::Multiply(int n) const
369 return wxTimeSpan(m_diff * (long)n);
372 wxTimeSpan wxTimeSpan::Abs() const
374 return wxTimeSpan(GetValue().Abs());
377 bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
379 return GetValue() == ts.GetValue();
382 bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
384 return GetValue().Abs() > ts.GetValue().Abs();
387 // ----------------------------------------------------------------------------
389 // ----------------------------------------------------------------------------
392 wxDateSpan::operator+=(const wxDateSpan& other)
394 m_years += other.m_years;
395 m_months += other.m_months;
396 m_weeks += other.m_weeks;
397 m_days += other.m_days;
402 wxDateSpan& wxDateSpan::Multiply(int factor)
412 wxDateSpan wxDateSpan::Multiply(int factor) const
414 return wxDateSpan(*this).Multiply(factor);
417 wxDateSpan wxDateSpan::Negate() const
419 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
422 wxDateSpan& wxDateSpan::Neg()
425 m_months = -m_months;
432 #undef MILLISECONDS_PER_DAY