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(*GetTmNow());
42 wxDateTime wxDateTime::Today()
44 struct tm *tm = GetTmNow();
49 return wxDateTime(*tm);
52 wxDateTime& wxDateTime::Set(time_t timet)
54 // assign first to avoid long multiplication overflow!
56 m_time *= TIME_T_FACTOR;
61 wxDateTime& wxDateTime::SetToCurrent()
66 wxDateTime::wxDateTime(time_t timet)
71 wxDateTime::wxDateTime(const struct tm& tm)
76 wxDateTime::wxDateTime(const Tm& tm)
81 wxDateTime::wxDateTime(double jdn)
86 wxDateTime& wxDateTime::Set(const Tm& tm)
88 wxASSERT_MSG( tm.IsValid(), _T("invalid broken down date/time") );
90 return Set(tm.mday, (Month)tm.mon, tm.year, tm.hour, tm.min, tm.sec);
93 wxDateTime::wxDateTime(wxDateTime_t hour,
96 wxDateTime_t millisec)
98 Set(hour, minute, second, millisec);
101 wxDateTime::wxDateTime(wxDateTime_t day,
107 wxDateTime_t millisec)
109 Set(day, month, year, hour, minute, second, millisec);
112 // ----------------------------------------------------------------------------
113 // wxDateTime accessors
114 // ----------------------------------------------------------------------------
116 wxLongLong wxDateTime::GetValue() const
118 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
123 time_t wxDateTime::GetTicks() const
125 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
126 if ( !IsInStdRange() )
131 return (time_t)((m_time / (long)TIME_T_FACTOR).GetLo());
134 bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
138 return SetToWeekDay(weekday, -1, month, year);
141 // ----------------------------------------------------------------------------
142 // wxDateTime comparison
143 // ----------------------------------------------------------------------------
145 bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
147 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
149 return m_time == datetime.m_time;
152 bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
154 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
156 return m_time < datetime.m_time;
159 bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
161 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
163 return m_time > datetime.m_time;
166 bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1,
167 const wxDateTime& t2) const
169 // no need for assert, will be checked by the functions we call
170 return IsLaterThan(t1) && IsEarlierThan(t2);
173 bool wxDateTime::IsBetween(const wxDateTime& t1, const wxDateTime& t2) const
175 // no need for assert, will be checked by the functions we call
176 return IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2);
179 bool wxDateTime::IsSameDate(const wxDateTime& dt) const
181 return (m_time - dt.m_time).Abs() < MILLISECONDS_PER_DAY;
184 bool wxDateTime::IsSameTime(const wxDateTime& dt) const
186 // notice that we can't do something like this:
188 // m_time % MILLISECONDS_PER_DAY == dt.m_time % MILLISECONDS_PER_DAY
190 // because we have also to deal with (possibly) different DST settings!
194 return tm1.hour == tm2.hour &&
195 tm1.min == tm2.min &&
196 tm1.sec == tm2.sec &&
197 tm1.msec == tm2.msec;
200 bool wxDateTime::IsEqualUpTo(const wxDateTime& dt, const wxTimeSpan& ts) const
202 return IsBetween(dt.Substract(ts), dt.Add(ts));
205 // ----------------------------------------------------------------------------
206 // wxDateTime arithmetics
207 // ----------------------------------------------------------------------------
209 wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const
211 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
213 return wxDateTime(m_time + diff.GetValue());
216 wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
218 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
220 m_time += diff.GetValue();
225 wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
230 wxDateTime wxDateTime::Substract(const wxTimeSpan& diff) const
232 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
234 return wxDateTime(m_time - diff.GetValue());
237 wxDateTime& wxDateTime::Substract(const wxTimeSpan& diff)
239 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
241 m_time -= diff.GetValue();
246 wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
248 return Substract(diff);
251 wxTimeSpan wxDateTime::Substract(const wxDateTime& datetime) const
253 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
255 return wxTimeSpan(datetime.GetValue() - GetValue());
258 wxDateTime wxDateTime::Add(const wxDateSpan& diff) const
260 return wxDateTime(*this).Add(diff);
263 wxDateTime& wxDateTime::Substract(const wxDateSpan& diff)
265 return Add(diff.Negate());
268 wxDateTime wxDateTime::Substract(const wxDateSpan& diff) const
270 return wxDateTime(*this).Substract(diff);
273 wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
275 return Substract(diff);
278 wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
283 // ----------------------------------------------------------------------------
284 // wxDateTime and timezones
285 // ----------------------------------------------------------------------------
287 wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz,
290 wxDateTime dt(*this);
291 return dt.MakeTimezone(tz, noDST);
294 // ----------------------------------------------------------------------------
295 // wxTimeSpan construction
296 // ----------------------------------------------------------------------------
298 wxTimeSpan::wxTimeSpan(int hours, int minutes, int seconds, int milliseconds)
300 // assign first to avoid precision loss
301 m_diff = (long)hours;
307 m_diff += milliseconds;
310 // ----------------------------------------------------------------------------
311 // wxTimeSpan accessors
312 // ----------------------------------------------------------------------------
314 wxLongLong wxTimeSpan::GetSeconds() const
316 return m_diff / 1000l;
319 int wxTimeSpan::GetMinutes() const
321 return (GetSeconds() / 60l).GetLo();
324 int wxTimeSpan::GetHours() const
326 return GetMinutes() / 60;
329 int wxTimeSpan::GetDays() const
331 return GetHours() / 24;
334 int wxTimeSpan::GetWeeks() const
336 return GetDays() / 7;
339 // ----------------------------------------------------------------------------
340 // wxTimeSpan arithmetics
341 // ----------------------------------------------------------------------------
343 wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const
345 return wxTimeSpan(m_diff + diff.GetValue());
348 wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
350 m_diff += diff.GetValue();
355 wxTimeSpan wxTimeSpan::Substract(const wxTimeSpan& diff) const
357 return wxTimeSpan(m_diff - diff.GetValue());
360 wxTimeSpan& wxTimeSpan::Substract(const wxTimeSpan& diff)
362 m_diff -= diff.GetValue();
367 wxTimeSpan& wxTimeSpan::Multiply(int n)
374 wxTimeSpan wxTimeSpan::Multiply(int n) const
376 return wxTimeSpan(m_diff * (long)n);
379 wxTimeSpan wxTimeSpan::Abs() const
381 return wxTimeSpan(GetValue().Abs());
384 bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
386 return GetValue() == ts.GetValue();
389 bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
391 return GetValue().Abs() > ts.GetValue().Abs();
394 // ----------------------------------------------------------------------------
396 // ----------------------------------------------------------------------------
399 wxDateSpan::operator+=(const wxDateSpan& other)
401 m_years += other.m_years;
402 m_months += other.m_months;
403 m_weeks += other.m_weeks;
404 m_days += other.m_days;
409 wxDateSpan& wxDateSpan::Multiply(int factor)
419 wxDateSpan wxDateSpan::Multiply(int factor) const
421 return wxDateSpan(*this).Multiply(factor);
424 wxDateSpan wxDateSpan::Negate() const
426 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
429 wxDateSpan& wxDateSpan::Neg()
432 m_months = -m_months;
439 #undef MILLISECONDS_PER_DAY