]> git.saurik.com Git - wxWidgets.git/blob - include/wx/datetime.inl
57555e63553df0994b2d37e52075709c39e95698
[wxWidgets.git] / include / wx / datetime.inl
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.
8 // Modified by:
9 // Created: 30.11.99
10 // RCS-ID: $Id$
11 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
12 // Licence: wxWindows license
13 /////////////////////////////////////////////////////////////////////////////
14
15 #ifndef INCLUDED_FROM_WX_DATETIME_H
16 #error "This file is only included by wx/datetime.h, don't include it manually!"
17 #endif
18
19 // ----------------------------------------------------------------------------
20 // wxDateTime statics
21 // ----------------------------------------------------------------------------
22
23 /* static */
24 wxDateTime::Country wxDateTime::GetCountry()
25 {
26 return ms_country;
27 }
28
29 // ----------------------------------------------------------------------------
30 // wxDateTime construction
31 // ----------------------------------------------------------------------------
32
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
37
38 bool wxDateTime::IsInStdRange() const
39 {
40 return m_time >= 0l && (m_time / (long)TIME_T_FACTOR) < LONG_MAX;
41 }
42
43 /* static */
44 wxDateTime wxDateTime::Now()
45 {
46 return wxDateTime(GetTimeNow());
47 }
48
49 wxDateTime& wxDateTime::Set(time_t timet)
50 {
51 // assign first to avoid long multiplication overflow!
52 m_time = timet;
53 m_time *= TIME_T_FACTOR;
54
55 return *this;
56 }
57
58 wxDateTime& wxDateTime::SetToCurrent()
59 {
60 return Set(GetTimeNow());
61 }
62
63 wxDateTime::wxDateTime(time_t timet)
64 {
65 Set(timet);
66 }
67
68 wxDateTime::wxDateTime(const struct tm& tm)
69 {
70 Set(tm);
71 }
72
73 wxDateTime::wxDateTime(const Tm& tm)
74 {
75 Set(tm);
76 }
77
78 wxDateTime& wxDateTime::Set(const Tm& tm)
79 {
80 wxASSERT_MSG( tm.IsValid(), _T("invalid broken down date/time") );
81
82 return Set(tm.mday, (Month)tm.mon, tm.year, tm.hour, tm.min, tm.sec);
83 }
84
85 wxDateTime::wxDateTime(wxDateTime_t hour,
86 wxDateTime_t minute,
87 wxDateTime_t second,
88 wxDateTime_t millisec)
89 {
90 Set(hour, minute, second, millisec);
91 }
92
93 wxDateTime::wxDateTime(wxDateTime_t day,
94 Month month,
95 int year,
96 wxDateTime_t hour,
97 wxDateTime_t minute,
98 wxDateTime_t second,
99 wxDateTime_t millisec)
100 {
101 Set(day, month, year, hour, minute, second, millisec);
102 }
103
104 // ----------------------------------------------------------------------------
105 // wxDateTime accessors
106 // ----------------------------------------------------------------------------
107
108 wxLongLong wxDateTime::GetValue() const
109 {
110 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
111
112 return m_time;
113 }
114
115 time_t wxDateTime::GetTicks() const
116 {
117 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
118 if ( !IsInStdRange() )
119 {
120 return (time_t)-1;
121 }
122
123 return (time_t)((m_time / (long)TIME_T_FACTOR).GetLo());
124 }
125
126 bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
127 Month month,
128 int year)
129 {
130 return SetToWeekDay(weekday, -1, month, year);
131 }
132
133 // ----------------------------------------------------------------------------
134 // wxDateTime comparison
135 // ----------------------------------------------------------------------------
136
137 bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
138 {
139 wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime");
140
141 return m_time == datetime.m_time;
142 }
143
144 bool wxDateTime::operator==(const wxDateTime& datetime) const
145 {
146 return IsEqualTo(datetime);
147 }
148
149 bool wxDateTime::operator!=(const wxDateTime& datetime) const
150 {
151 return !IsEqualTo(datetime);
152 }
153
154 bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
155 {
156 wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime");
157
158 return m_time < datetime.m_time;
159 }
160
161 bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
162 {
163 wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime");
164
165 return m_time > datetime.m_time;
166 }
167
168 bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1,
169 const wxDateTime& t2) const
170 {
171 // no need for assert, will be checked by the functions we call
172 return IsLaterThan(t1) && IsEarlierThan(t2);
173 }
174
175 bool wxDateTime::IsBetween(const wxDateTime& t1, const wxDateTime& t2) const
176 {
177 // no need for assert, will be checked by the functions we call
178 return IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2);
179 }
180
181 // ----------------------------------------------------------------------------
182 // wxDateTime arithmetics
183 // ----------------------------------------------------------------------------
184
185 wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
186 {
187 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
188
189 m_time += diff.GetValue();
190
191 return *this;
192 }
193
194 wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
195 {
196 return Add(diff);
197 }
198
199 wxDateTime& wxDateTime::Substract(const wxTimeSpan& diff)
200 {
201 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
202
203 m_time -= diff.GetValue();
204
205 return *this;
206 }
207
208 wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
209 {
210 return Substract(diff);
211 }
212
213 wxTimeSpan wxDateTime::Substract(const wxDateTime& datetime) const
214 {
215 wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime");
216
217 return wxTimeSpan(datetime.GetValue() - GetValue());
218 }
219
220 wxTimeSpan wxDateTime::operator-(const wxDateTime& datetime) const
221 {
222 return Substract(datetime);
223 }
224
225 wxDateTime& wxDateTime::Substract(const wxDateSpan& diff)
226 {
227 return Add(diff.Negate());
228 }
229
230 wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
231 {
232 return Substract(diff);
233 }
234
235 wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
236 {
237 return Add(diff);
238 }
239
240 // ----------------------------------------------------------------------------
241 // wxDateTime and timezones
242 // ----------------------------------------------------------------------------
243
244 wxDateTime wxDateTime::ToUTC() const
245 {
246 return wxDateTime(*this).MakeUTC();
247 }
248
249 wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz) const
250 {
251 return wxDateTime(*this).MakeTimezone(tz);
252 }
253
254 wxDateTime wxDateTime::ToLocalTime(const wxDateTime::TimeZone& tz) const
255 {
256 return wxDateTime(*this).MakeLocalTime(tz);
257 }
258
259 // ----------------------------------------------------------------------------
260 // wxTimeSpan
261 // ----------------------------------------------------------------------------
262
263 wxTimeSpan::wxTimeSpan(int hours, int minutes, int seconds, int milliseconds)
264 {
265 // assign first to avoid precision loss
266 m_diff = hours;
267 m_diff *= 60;
268 m_diff += minutes;
269 m_diff *= 60;
270 m_diff += seconds;
271 m_diff *= 1000;
272 m_diff += milliseconds;
273 }
274
275 wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
276 {
277 m_diff += diff.GetValue();
278
279 return *this;
280 }
281
282 wxTimeSpan& wxTimeSpan::Substract(const wxTimeSpan& diff)
283 {
284 m_diff -= diff.GetValue();
285
286 return *this;
287 }
288
289 wxTimeSpan& wxTimeSpan::Multiply(int n)
290 {
291 m_diff *= n;
292
293 return *this;
294 }
295
296 wxTimeSpan wxTimeSpan::operator*(int n) const
297 {
298 wxTimeSpan result(*this);
299 result.Multiply(n);
300
301 return result;
302 }
303
304 wxTimeSpan wxTimeSpan::Abs() const
305 {
306 return wxTimeSpan(GetValue().Abs());
307 }
308
309 bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
310 {
311 return GetValue() == ts.GetValue();
312 }
313
314 bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
315 {
316 return GetValue().Abs() > ts.GetValue().Abs();
317 }
318
319 // ----------------------------------------------------------------------------
320 // wxDateSpan
321 // ----------------------------------------------------------------------------
322
323 wxDateSpan&
324 wxDateSpan::operator+=(const wxDateSpan& other)
325 {
326 m_years += other.m_years;
327 m_months += other.m_months;
328 m_weeks += other.m_weeks;
329 m_days += other.m_days;
330
331 return *this;
332 }
333
334 wxDateSpan& wxDateSpan::operator*=(int factor)
335 {
336 m_years *= m_years;
337 m_months *= m_months;
338 m_weeks *= m_weeks;
339 m_days *= m_days;
340
341 return *this;
342 }
343
344 wxDateSpan wxDateSpan::Negate() const
345 {
346 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
347 }
348
349 wxDateSpan& wxDateSpan::Neg()
350 {
351 m_years = -m_years;
352 m_months = -m_months;
353 m_weeks = -m_weeks;
354 m_days = -m_days;
355
356 return *this;
357 }
358