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::Add(const wxDateSpan& diff) const
227 return wxDateTime(*this).Add(diff);
230 wxDateTime& wxDateTime::Substract(const wxDateSpan& diff)
232 return Add(diff.Negate());
235 wxDateTime wxDateTime::Substract(const wxDateSpan& diff) const
237 return wxDateTime(*this).Substract(diff);
240 wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
242 return Substract(diff);
245 wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
250 // ----------------------------------------------------------------------------
251 // wxDateTime and timezones
252 // ----------------------------------------------------------------------------
254 wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz) const
256 return wxDateTime(*this).MakeTimezone(tz);
259 // ----------------------------------------------------------------------------
260 // wxTimeSpan construction
261 // ----------------------------------------------------------------------------
263 wxTimeSpan::wxTimeSpan(int hours, int minutes, int seconds, int milliseconds)
265 // assign first to avoid precision loss
266 m_diff = (long)hours;
272 m_diff += milliseconds;
275 // ----------------------------------------------------------------------------
276 // wxTimeSpan accessors
277 // ----------------------------------------------------------------------------
279 wxLongLong wxTimeSpan::GetSeconds() const
281 return m_diff / 1000l;
284 int wxTimeSpan::GetMinutes() const
286 return (GetSeconds() / 60l).GetLo();
289 int wxTimeSpan::GetHours() const
291 return GetMinutes() / 60;
294 int wxTimeSpan::GetDays() const
296 return GetHours() / 24;
299 int wxTimeSpan::GetWeeks() const
301 return GetDays() / 7;
304 // ----------------------------------------------------------------------------
305 // wxTimeSpan arithmetics
306 // ----------------------------------------------------------------------------
308 wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const
310 return wxTimeSpan(m_diff + diff.GetValue());
313 wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
315 m_diff += diff.GetValue();
320 wxTimeSpan wxTimeSpan::Substract(const wxTimeSpan& diff) const
322 return wxTimeSpan(m_diff - diff.GetValue());
325 wxTimeSpan& wxTimeSpan::Substract(const wxTimeSpan& diff)
327 m_diff -= diff.GetValue();
332 wxTimeSpan& wxTimeSpan::Multiply(int n)
339 wxTimeSpan wxTimeSpan::Multiply(int n) const
341 return wxTimeSpan(m_diff * (long)n);
344 wxTimeSpan wxTimeSpan::Abs() const
346 return wxTimeSpan(GetValue().Abs());
349 bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
351 return GetValue() == ts.GetValue();
354 bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
356 return GetValue().Abs() > ts.GetValue().Abs();
359 // ----------------------------------------------------------------------------
361 // ----------------------------------------------------------------------------
364 wxDateSpan::operator+=(const wxDateSpan& other)
366 m_years += other.m_years;
367 m_months += other.m_months;
368 m_weeks += other.m_weeks;
369 m_days += other.m_days;
374 wxDateSpan& wxDateSpan::Multiply(int factor)
377 m_months *= m_months;
384 wxDateSpan wxDateSpan::Multiply(int factor) const
386 return wxDateSpan(*this).Multiply(factor);
389 wxDateSpan wxDateSpan::Negate() const
391 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
394 wxDateSpan& wxDateSpan::Neg()
397 m_months = -m_months;