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 // ----------------------------------------------------------------------------
20 // wxDateTime construction
21 // ----------------------------------------------------------------------------
23 // only define this once, when included from datetime.cpp
24 #ifdef wxDEFINE_TIME_CONSTANTS
25 const long wxDateTime::TIME_T_FACTOR = 1000l;
26 #endif // wxDEFINE_TIME_CONSTANTS
28 bool wxDateTime::IsInStdRange() const
30 return m_time >= 0l && (m_time / TIME_T_FACTOR) < LONG_MAX;
34 wxDateTime wxDateTime::Now()
36 return wxDateTime(GetTimeNow());
40 wxDateTime wxDateTime::Today()
42 return wxDateTime((time_t)(86400*(GetTimeNow() / 86400)));
45 wxDateTime& wxDateTime::Set(time_t timet)
47 // assign first to avoid long multiplication overflow!
49 m_time *= TIME_T_FACTOR;
54 wxDateTime& wxDateTime::SetToCurrent()
56 return Set(GetTimeNow());
59 wxDateTime::wxDateTime(time_t timet)
64 wxDateTime::wxDateTime(const struct tm& tm)
69 wxDateTime::wxDateTime(const Tm& tm)
74 wxDateTime::wxDateTime(double jdn)
79 wxDateTime& wxDateTime::Set(const Tm& tm)
81 wxASSERT_MSG( tm.IsValid(), _T("invalid broken down date/time") );
83 return Set(tm.mday, (Month)tm.mon, tm.year, tm.hour, tm.min, tm.sec);
86 wxDateTime::wxDateTime(wxDateTime_t hour,
89 wxDateTime_t millisec)
91 Set(hour, minute, second, millisec);
94 wxDateTime::wxDateTime(wxDateTime_t day,
100 wxDateTime_t millisec)
102 Set(day, month, year, hour, minute, second, millisec);
105 // ----------------------------------------------------------------------------
106 // wxDateTime accessors
107 // ----------------------------------------------------------------------------
109 wxLongLong wxDateTime::GetValue() const
111 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
116 time_t wxDateTime::GetTicks() const
118 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
119 if ( !IsInStdRange() )
124 return (time_t)((m_time / (long)TIME_T_FACTOR).GetLo());
127 bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
131 return SetToWeekDay(weekday, -1, month, year);
134 // ----------------------------------------------------------------------------
135 // wxDateTime comparison
136 // ----------------------------------------------------------------------------
138 bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
140 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
142 return m_time == datetime.m_time;
145 bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
147 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
149 return m_time < datetime.m_time;
152 bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
154 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
156 return m_time > datetime.m_time;
159 bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1,
160 const wxDateTime& t2) const
162 // no need for assert, will be checked by the functions we call
163 return IsLaterThan(t1) && IsEarlierThan(t2);
166 bool wxDateTime::IsBetween(const wxDateTime& t1, const wxDateTime& t2) const
168 // no need for assert, will be checked by the functions we call
169 return IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2);
172 // ----------------------------------------------------------------------------
173 // wxDateTime arithmetics
174 // ----------------------------------------------------------------------------
176 wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const
178 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
180 return wxDateTime(m_time + diff.GetValue());
183 wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
185 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
187 m_time += diff.GetValue();
192 wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
197 wxDateTime wxDateTime::Substract(const wxTimeSpan& diff) const
199 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
201 return wxDateTime(m_time - diff.GetValue());
204 wxDateTime& wxDateTime::Substract(const wxTimeSpan& diff)
206 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
208 m_time -= diff.GetValue();
213 wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
215 return Substract(diff);
218 wxTimeSpan wxDateTime::Substract(const wxDateTime& datetime) const
220 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
222 return wxTimeSpan(datetime.GetValue() - GetValue());
225 wxDateTime& wxDateTime::Substract(const wxDateSpan& diff)
227 return Add(diff.Negate());
230 wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
232 return Substract(diff);
235 wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
240 // ----------------------------------------------------------------------------
241 // wxDateTime and timezones
242 // ----------------------------------------------------------------------------
244 wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz) const
246 return wxDateTime(*this).MakeTimezone(tz);
249 // ----------------------------------------------------------------------------
250 // wxTimeSpan construction
251 // ----------------------------------------------------------------------------
253 wxTimeSpan::wxTimeSpan(int hours, int minutes, int seconds, int milliseconds)
255 // assign first to avoid precision loss
256 m_diff = (long)hours;
262 m_diff += milliseconds;
265 // ----------------------------------------------------------------------------
266 // wxTimeSpan accessors
267 // ----------------------------------------------------------------------------
269 wxLongLong wxTimeSpan::GetSeconds() const
271 return m_diff / 1000l;
274 int wxTimeSpan::GetMinutes() const
276 return (GetSeconds() / 60l).GetLo();
279 int wxTimeSpan::GetHours() const
281 return GetMinutes() / 60;
284 int wxTimeSpan::GetDays() const
286 return GetHours() / 24;
289 int wxTimeSpan::GetWeeks() const
291 return GetDays() / 7;
294 // ----------------------------------------------------------------------------
295 // wxTimeSpan arithmetics
296 // ----------------------------------------------------------------------------
298 wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const
300 return wxTimeSpan(m_diff + diff.GetValue());
303 wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
305 m_diff += diff.GetValue();
310 wxTimeSpan wxTimeSpan::Substract(const wxTimeSpan& diff) const
312 return wxTimeSpan(m_diff - diff.GetValue());
315 wxTimeSpan& wxTimeSpan::Substract(const wxTimeSpan& diff)
317 m_diff -= diff.GetValue();
322 wxTimeSpan& wxTimeSpan::Multiply(int n)
329 wxTimeSpan wxTimeSpan::Multiply(int n) const
331 return wxTimeSpan(m_diff * (long)n);
334 wxTimeSpan wxTimeSpan::Abs() const
336 return wxTimeSpan(GetValue().Abs());
339 bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
341 return GetValue() == ts.GetValue();
344 bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
346 return GetValue().Abs() > ts.GetValue().Abs();
349 // ----------------------------------------------------------------------------
351 // ----------------------------------------------------------------------------
354 wxDateSpan::operator+=(const wxDateSpan& other)
356 m_years += other.m_years;
357 m_months += other.m_months;
358 m_weeks += other.m_weeks;
359 m_days += other.m_days;
364 wxDateSpan& wxDateSpan::Multiply(int factor)
367 m_months *= m_months;
374 wxDateSpan wxDateSpan::Multiply(int factor) const
376 return wxDateSpan(*this).Multiply(factor);
379 wxDateSpan wxDateSpan::Negate() const
381 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
384 wxDateSpan& wxDateSpan::Neg()
387 m_months = -m_months;