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 unsigned int wxDateTime::TIME_T_FACTOR = 1000;
26 #endif // wxDEFINE_TIME_CONSTANTS
28 bool wxDateTime::IsInStdRange() const
30 return m_time >= 0l && (m_time / (long)TIME_T_FACTOR) < LONG_MAX;
34 wxDateTime wxDateTime::Now()
36 return wxDateTime(GetTimeNow());
39 wxDateTime& wxDateTime::Set(time_t timet)
41 // assign first to avoid long multiplication overflow!
43 m_time *= TIME_T_FACTOR;
48 wxDateTime& wxDateTime::SetToCurrent()
50 return Set(GetTimeNow());
53 wxDateTime::wxDateTime(time_t timet)
58 wxDateTime::wxDateTime(const struct tm& tm)
63 wxDateTime::wxDateTime(const Tm& tm)
68 wxDateTime::wxDateTime(double jdn)
73 wxDateTime& wxDateTime::Set(const Tm& tm)
75 wxASSERT_MSG( tm.IsValid(), _T("invalid broken down date/time") );
77 return Set(tm.mday, (Month)tm.mon, tm.year, tm.hour, tm.min, tm.sec);
80 wxDateTime::wxDateTime(wxDateTime_t hour,
83 wxDateTime_t millisec)
85 Set(hour, minute, second, millisec);
88 wxDateTime::wxDateTime(wxDateTime_t day,
94 wxDateTime_t millisec)
96 Set(day, month, year, hour, minute, second, millisec);
99 // ----------------------------------------------------------------------------
100 // wxDateTime accessors
101 // ----------------------------------------------------------------------------
103 wxLongLong wxDateTime::GetValue() const
105 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
110 time_t wxDateTime::GetTicks() const
112 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
113 if ( !IsInStdRange() )
118 return (time_t)((m_time / (long)TIME_T_FACTOR).GetLo());
121 bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
125 return SetToWeekDay(weekday, -1, month, year);
128 // ----------------------------------------------------------------------------
129 // wxDateTime comparison
130 // ----------------------------------------------------------------------------
132 bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
134 wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime");
136 return m_time == datetime.m_time;
139 bool wxDateTime::operator==(const wxDateTime& datetime) const
141 return IsEqualTo(datetime);
144 bool wxDateTime::operator!=(const wxDateTime& datetime) const
146 return !IsEqualTo(datetime);
149 bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
151 wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime");
153 return m_time < datetime.m_time;
156 bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
158 wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime");
160 return m_time > datetime.m_time;
163 bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1,
164 const wxDateTime& t2) const
166 // no need for assert, will be checked by the functions we call
167 return IsLaterThan(t1) && IsEarlierThan(t2);
170 bool wxDateTime::IsBetween(const wxDateTime& t1, const wxDateTime& t2) const
172 // no need for assert, will be checked by the functions we call
173 return IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2);
176 // ----------------------------------------------------------------------------
177 // wxDateTime arithmetics
178 // ----------------------------------------------------------------------------
180 wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
182 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
184 m_time += diff.GetValue();
189 wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
194 wxDateTime& wxDateTime::Substract(const wxTimeSpan& diff)
196 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
198 m_time -= diff.GetValue();
203 wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
205 return Substract(diff);
208 wxTimeSpan wxDateTime::Substract(const wxDateTime& datetime) const
210 wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime");
212 return wxTimeSpan(datetime.GetValue() - GetValue());
215 wxTimeSpan wxDateTime::operator-(const wxDateTime& datetime) const
217 return Substract(datetime);
220 wxDateTime& wxDateTime::Substract(const wxDateSpan& diff)
222 return Add(diff.Negate());
225 wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
227 return Substract(diff);
230 wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
235 // ----------------------------------------------------------------------------
236 // wxDateTime and timezones
237 // ----------------------------------------------------------------------------
239 wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz) const
241 return wxDateTime(*this).MakeTimezone(tz);
244 // ----------------------------------------------------------------------------
245 // wxTimeSpan construction
246 // ----------------------------------------------------------------------------
248 wxTimeSpan::wxTimeSpan(int hours, int minutes, int seconds, int milliseconds)
250 // assign first to avoid precision loss
257 m_diff += milliseconds;
260 // ----------------------------------------------------------------------------
261 // wxTimeSpan accessors
262 // ----------------------------------------------------------------------------
264 wxLongLong wxTimeSpan::GetSeconds() const
266 return m_diff / 1000l;
269 int wxTimeSpan::GetMinutes() const
271 return (GetSeconds() / 60l).GetLo();
274 int wxTimeSpan::GetHours() const
276 return GetMinutes() / 60;
279 int wxTimeSpan::GetDays() const
281 return GetHours() / 24;
284 int wxTimeSpan::GetWeeks() const
286 return GetDays() / 7;
289 // ----------------------------------------------------------------------------
290 // wxTimeSpan arithmetics
291 // ----------------------------------------------------------------------------
293 wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
295 m_diff += diff.GetValue();
300 wxTimeSpan& wxTimeSpan::Substract(const wxTimeSpan& diff)
302 m_diff -= diff.GetValue();
307 wxTimeSpan& wxTimeSpan::Multiply(int n)
314 wxTimeSpan wxTimeSpan::operator*(int n) const
316 wxTimeSpan result(*this);
322 wxTimeSpan wxTimeSpan::Abs() const
324 return wxTimeSpan(GetValue().Abs());
327 bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
329 return GetValue() == ts.GetValue();
332 bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
334 return GetValue().Abs() > ts.GetValue().Abs();
337 // ----------------------------------------------------------------------------
339 // ----------------------------------------------------------------------------
342 wxDateSpan::operator+=(const wxDateSpan& other)
344 m_years += other.m_years;
345 m_months += other.m_months;
346 m_weeks += other.m_weeks;
347 m_days += other.m_days;
352 wxDateSpan& wxDateSpan::operator*=(int factor)
355 m_months *= m_months;
362 wxDateSpan wxDateSpan::Negate() const
364 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
367 wxDateSpan& wxDateSpan::Neg()
370 m_months = -m_months;