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::Set(const Tm& tm)
80 wxASSERT_MSG( tm.IsValid(), _T("invalid broken down date/time") );
82 return Set(tm.mday, (Month)tm.mon, tm.year, tm.hour, tm.min, tm.sec);
85 wxDateTime::wxDateTime(wxDateTime_t hour,
88 wxDateTime_t millisec)
90 Set(hour, minute, second, millisec);
93 wxDateTime::wxDateTime(wxDateTime_t day,
99 wxDateTime_t millisec)
101 Set(day, month, year, hour, minute, second, millisec);
104 // ----------------------------------------------------------------------------
105 // wxDateTime accessors
106 // ----------------------------------------------------------------------------
108 wxLongLong wxDateTime::GetValue() const
110 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
115 time_t wxDateTime::GetTicks() const
117 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
118 if ( !IsInStdRange() )
123 return (time_t)((m_time / (long)TIME_T_FACTOR).GetLo());
126 bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
130 return SetToWeekDay(weekday, -1, month, year);
133 // ----------------------------------------------------------------------------
134 // wxDateTime comparison
135 // ----------------------------------------------------------------------------
137 bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
139 wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime");
141 return m_time == datetime.m_time;
144 bool wxDateTime::operator==(const wxDateTime& datetime) const
146 return IsEqualTo(datetime);
149 bool wxDateTime::operator!=(const wxDateTime& datetime) const
151 return !IsEqualTo(datetime);
154 bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
156 wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime");
158 return m_time < datetime.m_time;
161 bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
163 wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime");
165 return m_time > datetime.m_time;
168 bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1,
169 const wxDateTime& t2) const
171 // no need for assert, will be checked by the functions we call
172 return IsLaterThan(t1) && IsEarlierThan(t2);
175 bool wxDateTime::IsBetween(const wxDateTime& t1, const wxDateTime& t2) const
177 // no need for assert, will be checked by the functions we call
178 return IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2);
181 // ----------------------------------------------------------------------------
182 // wxDateTime arithmetics
183 // ----------------------------------------------------------------------------
185 wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
187 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
189 m_time += diff.GetValue();
194 wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
199 wxDateTime& wxDateTime::Substract(const wxTimeSpan& diff)
201 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
203 m_time -= diff.GetValue();
208 wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
210 return Substract(diff);
213 wxTimeSpan wxDateTime::Substract(const wxDateTime& datetime) const
215 wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime");
217 return wxTimeSpan(datetime.GetValue() - GetValue());
220 wxTimeSpan wxDateTime::operator-(const wxDateTime& datetime) const
222 return Substract(datetime);
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::ToUTC() const
246 return wxDateTime(*this).MakeUTC();
249 wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz) const
251 return wxDateTime(*this).MakeTimezone(tz);
254 wxDateTime wxDateTime::ToLocalTime(const wxDateTime::TimeZone& tz) const
256 return wxDateTime(*this).MakeLocalTime(tz);
259 // ----------------------------------------------------------------------------
261 // ----------------------------------------------------------------------------
263 wxTimeSpan::wxTimeSpan(int hours, int minutes, int seconds, int milliseconds)
265 // assign first to avoid precision loss
272 m_diff += milliseconds;
275 wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
277 m_diff += diff.GetValue();
282 wxTimeSpan& wxTimeSpan::Substract(const wxTimeSpan& diff)
284 m_diff -= diff.GetValue();
289 wxTimeSpan& wxTimeSpan::Multiply(int n)
296 wxTimeSpan wxTimeSpan::operator*(int n) const
298 wxTimeSpan result(*this);
304 wxTimeSpan wxTimeSpan::Abs() const
306 return wxTimeSpan(GetValue().Abs());
309 bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
311 return GetValue() == ts.GetValue();
314 bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
316 return GetValue().Abs() > ts.GetValue().Abs();
319 // ----------------------------------------------------------------------------
321 // ----------------------------------------------------------------------------
324 wxDateSpan::operator+=(const wxDateSpan& other)
326 m_years += other.m_years;
327 m_months += other.m_months;
328 m_weeks += other.m_weeks;
329 m_days += other.m_days;
334 wxDateSpan& wxDateSpan::operator*=(int factor)
337 m_months *= m_months;
344 wxDateSpan wxDateSpan::Negate() const
346 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
349 wxDateSpan& wxDateSpan::Neg()
352 m_months = -m_months;