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::ToUTC() const
251 return wxDateTime(*this).MakeUTC();
254 wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz) const
256 return wxDateTime(*this).MakeTimezone(tz);
259 wxDateTime wxDateTime::ToLocalTime(const wxDateTime::TimeZone& tz) const
261 return wxDateTime(*this).MakeLocalTime(tz);
264 // ----------------------------------------------------------------------------
265 // wxTimeSpan construction
266 // ----------------------------------------------------------------------------
268 wxTimeSpan::wxTimeSpan(int hours, int minutes, int seconds, int milliseconds)
270 // assign first to avoid precision loss
277 m_diff += milliseconds;
280 // ----------------------------------------------------------------------------
281 // wxTimeSpan accessors
282 // ----------------------------------------------------------------------------
284 wxLongLong wxTimeSpan::GetSeconds() const
286 return m_diff / 1000l;
289 int wxTimeSpan::GetMinutes() const
291 return (GetSeconds() / 60l).GetLo();
294 int wxTimeSpan::GetHours() const
296 return GetMinutes() / 60;
299 int wxTimeSpan::GetDays() const
301 return GetHours() / 24;
304 int wxTimeSpan::GetWeeks() const
306 return GetDays() / 7;
309 // ----------------------------------------------------------------------------
310 // wxTimeSpan arithmetics
311 // ----------------------------------------------------------------------------
313 wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
315 m_diff += diff.GetValue();
320 wxTimeSpan& wxTimeSpan::Substract(const wxTimeSpan& diff)
322 m_diff -= diff.GetValue();
327 wxTimeSpan& wxTimeSpan::Multiply(int n)
334 wxTimeSpan wxTimeSpan::operator*(int n) const
336 wxTimeSpan result(*this);
342 wxTimeSpan wxTimeSpan::Abs() const
344 return wxTimeSpan(GetValue().Abs());
347 bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
349 return GetValue() == ts.GetValue();
352 bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
354 return GetValue().Abs() > ts.GetValue().Abs();
357 // ----------------------------------------------------------------------------
359 // ----------------------------------------------------------------------------
362 wxDateSpan::operator+=(const wxDateSpan& other)
364 m_years += other.m_years;
365 m_months += other.m_months;
366 m_weeks += other.m_weeks;
367 m_days += other.m_days;
372 wxDateSpan& wxDateSpan::operator*=(int factor)
375 m_months *= m_months;
382 wxDateSpan wxDateSpan::Negate() const
384 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
387 wxDateSpan& wxDateSpan::Neg()
390 m_months = -m_months;