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,
285 return wxDateTime(*this).MakeTimezone(tz, noDST);
288 // ----------------------------------------------------------------------------
289 // wxTimeSpan construction
290 // ----------------------------------------------------------------------------
292 wxTimeSpan::wxTimeSpan(int hours, int minutes, int seconds, int milliseconds)
294 // assign first to avoid precision loss
295 m_diff = (long)hours;
301 m_diff += milliseconds;
304 // ----------------------------------------------------------------------------
305 // wxTimeSpan accessors
306 // ----------------------------------------------------------------------------
308 wxLongLong wxTimeSpan::GetSeconds() const
310 return m_diff / 1000l;
313 int wxTimeSpan::GetMinutes() const
315 return (GetSeconds() / 60l).GetLo();
318 int wxTimeSpan::GetHours() const
320 return GetMinutes() / 60;
323 int wxTimeSpan::GetDays() const
325 return GetHours() / 24;
328 int wxTimeSpan::GetWeeks() const
330 return GetDays() / 7;
333 // ----------------------------------------------------------------------------
334 // wxTimeSpan arithmetics
335 // ----------------------------------------------------------------------------
337 wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const
339 return wxTimeSpan(m_diff + diff.GetValue());
342 wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
344 m_diff += diff.GetValue();
349 wxTimeSpan wxTimeSpan::Substract(const wxTimeSpan& diff) const
351 return wxTimeSpan(m_diff - diff.GetValue());
354 wxTimeSpan& wxTimeSpan::Substract(const wxTimeSpan& diff)
356 m_diff -= diff.GetValue();
361 wxTimeSpan& wxTimeSpan::Multiply(int n)
368 wxTimeSpan wxTimeSpan::Multiply(int n) const
370 return wxTimeSpan(m_diff * (long)n);
373 wxTimeSpan wxTimeSpan::Abs() const
375 return wxTimeSpan(GetValue().Abs());
378 bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
380 return GetValue() == ts.GetValue();
383 bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
385 return GetValue().Abs() > ts.GetValue().Abs();
388 // ----------------------------------------------------------------------------
390 // ----------------------------------------------------------------------------
393 wxDateSpan::operator+=(const wxDateSpan& other)
395 m_years += other.m_years;
396 m_months += other.m_months;
397 m_weeks += other.m_weeks;
398 m_days += other.m_days;
403 wxDateSpan& wxDateSpan::Multiply(int factor)
413 wxDateSpan wxDateSpan::Multiply(int factor) const
415 return wxDateSpan(*this).Multiply(factor);
418 wxDateSpan wxDateSpan::Negate() const
420 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
423 wxDateSpan& wxDateSpan::Neg()
426 m_months = -m_months;
433 #undef MILLISECONDS_PER_DAY