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 // ----------------------------------------------------------------------------
21 // ----------------------------------------------------------------------------
24 wxDateTime::Country wxDateTime::GetCountry()
29 // ----------------------------------------------------------------------------
30 // wxDateTime construction
31 // ----------------------------------------------------------------------------
33 // only define this once, when included from datetime.cpp
34 #ifdef wxDEFINE_TIME_CONSTANTS
35 const unsigned int wxDateTime::TIME_T_FACTOR = 1000;
36 #endif // wxDEFINE_TIME_CONSTANTS
38 bool wxDateTime::IsInStdRange() const
40 return m_time >= 0l && (m_time / (long)TIME_T_FACTOR) < LONG_MAX;
44 wxDateTime wxDateTime::Now()
46 return wxDateTime(GetTimeNow());
49 wxDateTime& wxDateTime::Set(time_t timet)
51 // assign first to avoid long multiplication overflow!
53 m_time *= TIME_T_FACTOR;
58 wxDateTime& wxDateTime::SetToCurrent()
60 return Set(GetTimeNow());
63 wxDateTime::wxDateTime(time_t timet)
68 wxDateTime::wxDateTime(const struct tm& tm)
73 wxDateTime::wxDateTime(const Tm& tm)
78 wxDateTime::wxDateTime(double jdn)
83 wxDateTime& wxDateTime::Set(const Tm& tm)
85 wxASSERT_MSG( tm.IsValid(), _T("invalid broken down date/time") );
87 return Set(tm.mday, (Month)tm.mon, tm.year, tm.hour, tm.min, tm.sec);
90 wxDateTime::wxDateTime(wxDateTime_t hour,
93 wxDateTime_t millisec)
95 Set(hour, minute, second, millisec);
98 wxDateTime::wxDateTime(wxDateTime_t day,
104 wxDateTime_t millisec)
106 Set(day, month, year, hour, minute, second, millisec);
109 // ----------------------------------------------------------------------------
110 // wxDateTime accessors
111 // ----------------------------------------------------------------------------
113 wxLongLong wxDateTime::GetValue() const
115 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
120 time_t wxDateTime::GetTicks() const
122 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
123 if ( !IsInStdRange() )
128 return (time_t)((m_time / (long)TIME_T_FACTOR).GetLo());
131 bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
135 return SetToWeekDay(weekday, -1, month, year);
138 // ----------------------------------------------------------------------------
139 // wxDateTime comparison
140 // ----------------------------------------------------------------------------
142 bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
144 wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime");
146 return m_time == datetime.m_time;
149 bool wxDateTime::operator==(const wxDateTime& datetime) const
151 return IsEqualTo(datetime);
154 bool wxDateTime::operator!=(const wxDateTime& datetime) const
156 return !IsEqualTo(datetime);
159 bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
161 wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime");
163 return m_time < datetime.m_time;
166 bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
168 wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime");
170 return m_time > datetime.m_time;
173 bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1,
174 const wxDateTime& t2) const
176 // no need for assert, will be checked by the functions we call
177 return IsLaterThan(t1) && IsEarlierThan(t2);
180 bool wxDateTime::IsBetween(const wxDateTime& t1, const wxDateTime& t2) const
182 // no need for assert, will be checked by the functions we call
183 return IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2);
186 // ----------------------------------------------------------------------------
187 // wxDateTime arithmetics
188 // ----------------------------------------------------------------------------
190 wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
192 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
194 m_time += diff.GetValue();
199 wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
204 wxDateTime& wxDateTime::Substract(const wxTimeSpan& diff)
206 wxASSERT_MSG( IsValid(), "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(), "invalid wxDateTime");
222 return wxTimeSpan(datetime.GetValue() - GetValue());
225 wxTimeSpan wxDateTime::operator-(const wxDateTime& datetime) const
227 return Substract(datetime);
230 wxDateTime& wxDateTime::Substract(const wxDateSpan& diff)
232 return Add(diff.Negate());
235 wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
237 return Substract(diff);
240 wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
245 // ----------------------------------------------------------------------------
246 // wxDateTime and timezones
247 // ----------------------------------------------------------------------------
249 wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz) const
251 return wxDateTime(*this).MakeTimezone(tz);
254 // ----------------------------------------------------------------------------
255 // wxTimeSpan construction
256 // ----------------------------------------------------------------------------
258 wxTimeSpan::wxTimeSpan(int hours, int minutes, int seconds, int milliseconds)
260 // assign first to avoid precision loss
267 m_diff += milliseconds;
270 // ----------------------------------------------------------------------------
271 // wxTimeSpan accessors
272 // ----------------------------------------------------------------------------
274 wxLongLong wxTimeSpan::GetSeconds() const
276 return m_diff / 1000l;
279 int wxTimeSpan::GetMinutes() const
281 return (GetSeconds() / 60l).GetLo();
284 int wxTimeSpan::GetHours() const
286 return GetMinutes() / 60;
289 int wxTimeSpan::GetDays() const
291 return GetHours() / 24;
294 int wxTimeSpan::GetWeeks() const
296 return GetDays() / 7;
299 // ----------------------------------------------------------------------------
300 // wxTimeSpan arithmetics
301 // ----------------------------------------------------------------------------
303 wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
305 m_diff += diff.GetValue();
310 wxTimeSpan& wxTimeSpan::Substract(const wxTimeSpan& diff)
312 m_diff -= diff.GetValue();
317 wxTimeSpan& wxTimeSpan::Multiply(int n)
324 wxTimeSpan wxTimeSpan::operator*(int n) const
326 wxTimeSpan result(*this);
332 wxTimeSpan wxTimeSpan::Abs() const
334 return wxTimeSpan(GetValue().Abs());
337 bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
339 return GetValue() == ts.GetValue();
342 bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
344 return GetValue().Abs() > ts.GetValue().Abs();
347 // ----------------------------------------------------------------------------
349 // ----------------------------------------------------------------------------
352 wxDateSpan::operator+=(const wxDateSpan& other)
354 m_years += other.m_years;
355 m_months += other.m_months;
356 m_weeks += other.m_weeks;
357 m_days += other.m_days;
362 wxDateSpan& wxDateSpan::operator*=(int factor)
365 m_months *= m_months;
372 wxDateSpan wxDateSpan::Negate() const
374 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
377 wxDateSpan& wxDateSpan::Neg()
380 m_months = -m_months;