]> git.saurik.com Git - wxWidgets.git/blob - include/wx/datetime.inl
GUI thread update and event corrections.
[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 #define MILLISECONDS_PER_DAY 86400000l
20
21 // ----------------------------------------------------------------------------
22 // wxDateTime construction
23 // ----------------------------------------------------------------------------
24
25 // only define this once, when included from datetime.cpp
26 #ifdef wxDEFINE_TIME_CONSTANTS
27 const long wxDateTime::TIME_T_FACTOR = 1000l;
28 #endif // wxDEFINE_TIME_CONSTANTS
29
30 bool wxDateTime::IsInStdRange() const
31 {
32 return m_time >= 0l && (m_time / TIME_T_FACTOR) < LONG_MAX;
33 }
34
35 /* static */
36 wxDateTime wxDateTime::Now()
37 {
38 return wxDateTime(GetTimeNow());
39 }
40
41 /* static */
42 wxDateTime wxDateTime::Today()
43 {
44 return wxDateTime((time_t)(86400*(GetTimeNow() / 86400)));
45 }
46
47 wxDateTime& wxDateTime::Set(time_t timet)
48 {
49 // assign first to avoid long multiplication overflow!
50 m_time = timet;
51 m_time *= TIME_T_FACTOR;
52
53 return *this;
54 }
55
56 wxDateTime& wxDateTime::SetToCurrent()
57 {
58 return Set(GetTimeNow());
59 }
60
61 wxDateTime::wxDateTime(time_t timet)
62 {
63 Set(timet);
64 }
65
66 wxDateTime::wxDateTime(const struct tm& tm)
67 {
68 Set(tm);
69 }
70
71 wxDateTime::wxDateTime(const Tm& tm)
72 {
73 Set(tm);
74 }
75
76 wxDateTime::wxDateTime(double jdn)
77 {
78 Set(jdn);
79 }
80
81 wxDateTime& wxDateTime::Set(const Tm& tm)
82 {
83 wxASSERT_MSG( tm.IsValid(), _T("invalid broken down date/time") );
84
85 return Set(tm.mday, (Month)tm.mon, tm.year, tm.hour, tm.min, tm.sec);
86 }
87
88 wxDateTime::wxDateTime(wxDateTime_t hour,
89 wxDateTime_t minute,
90 wxDateTime_t second,
91 wxDateTime_t millisec)
92 {
93 Set(hour, minute, second, millisec);
94 }
95
96 wxDateTime::wxDateTime(wxDateTime_t day,
97 Month month,
98 int year,
99 wxDateTime_t hour,
100 wxDateTime_t minute,
101 wxDateTime_t second,
102 wxDateTime_t millisec)
103 {
104 Set(day, month, year, hour, minute, second, millisec);
105 }
106
107 // ----------------------------------------------------------------------------
108 // wxDateTime accessors
109 // ----------------------------------------------------------------------------
110
111 wxLongLong wxDateTime::GetValue() const
112 {
113 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
114
115 return m_time;
116 }
117
118 time_t wxDateTime::GetTicks() const
119 {
120 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
121 if ( !IsInStdRange() )
122 {
123 return (time_t)-1;
124 }
125
126 return (time_t)((m_time / (long)TIME_T_FACTOR).GetLo());
127 }
128
129 bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
130 Month month,
131 int year)
132 {
133 return SetToWeekDay(weekday, -1, month, year);
134 }
135
136 // ----------------------------------------------------------------------------
137 // wxDateTime comparison
138 // ----------------------------------------------------------------------------
139
140 bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
141 {
142 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
143
144 return m_time == datetime.m_time;
145 }
146
147 bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
148 {
149 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
150
151 return m_time < datetime.m_time;
152 }
153
154 bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
155 {
156 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
157
158 return m_time > datetime.m_time;
159 }
160
161 bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1,
162 const wxDateTime& t2) const
163 {
164 // no need for assert, will be checked by the functions we call
165 return IsLaterThan(t1) && IsEarlierThan(t2);
166 }
167
168 bool wxDateTime::IsBetween(const wxDateTime& t1, const wxDateTime& t2) const
169 {
170 // no need for assert, will be checked by the functions we call
171 return IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2);
172 }
173
174 bool wxDateTime::IsSameDate(const wxDateTime& dt) const
175 {
176 return (m_time - dt.m_time).Abs() < MILLISECONDS_PER_DAY;
177 }
178
179 bool wxDateTime::IsSameTime(const wxDateTime& dt) const
180 {
181 // notice that we can't do something like this:
182 //
183 // m_time % MILLISECONDS_PER_DAY == dt.m_time % MILLISECONDS_PER_DAY
184 //
185 // because we have also to deal with (possibly) different DST settings!
186 Tm tm1 = GetTm(),
187 tm2 = dt.GetTm();
188
189 return tm1.hour == tm2.hour &&
190 tm1.min == tm2.min &&
191 tm1.sec == tm2.sec &&
192 tm1.msec == tm2.msec;
193 }
194
195 bool wxDateTime::IsEqualUpTo(const wxDateTime& dt, const wxTimeSpan& ts) const
196 {
197 return IsBetween(dt.Substract(ts), dt.Add(ts));
198 }
199
200 // ----------------------------------------------------------------------------
201 // wxDateTime arithmetics
202 // ----------------------------------------------------------------------------
203
204 wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const
205 {
206 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
207
208 return wxDateTime(m_time + diff.GetValue());
209 }
210
211 wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
212 {
213 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
214
215 m_time += diff.GetValue();
216
217 return *this;
218 }
219
220 wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
221 {
222 return Add(diff);
223 }
224
225 wxDateTime wxDateTime::Substract(const wxTimeSpan& diff) const
226 {
227 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
228
229 return wxDateTime(m_time - diff.GetValue());
230 }
231
232 wxDateTime& wxDateTime::Substract(const wxTimeSpan& diff)
233 {
234 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
235
236 m_time -= diff.GetValue();
237
238 return *this;
239 }
240
241 wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
242 {
243 return Substract(diff);
244 }
245
246 wxTimeSpan wxDateTime::Substract(const wxDateTime& datetime) const
247 {
248 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
249
250 return wxTimeSpan(datetime.GetValue() - GetValue());
251 }
252
253 wxDateTime wxDateTime::Add(const wxDateSpan& diff) const
254 {
255 return wxDateTime(*this).Add(diff);
256 }
257
258 wxDateTime& wxDateTime::Substract(const wxDateSpan& diff)
259 {
260 return Add(diff.Negate());
261 }
262
263 wxDateTime wxDateTime::Substract(const wxDateSpan& diff) const
264 {
265 return wxDateTime(*this).Substract(diff);
266 }
267
268 wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
269 {
270 return Substract(diff);
271 }
272
273 wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
274 {
275 return Add(diff);
276 }
277
278 // ----------------------------------------------------------------------------
279 // wxDateTime and timezones
280 // ----------------------------------------------------------------------------
281
282 wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz,
283 bool noDST) const
284 {
285 return wxDateTime(*this).MakeTimezone(tz, noDST);
286 }
287
288 // ----------------------------------------------------------------------------
289 // wxTimeSpan construction
290 // ----------------------------------------------------------------------------
291
292 wxTimeSpan::wxTimeSpan(int hours, int minutes, int seconds, int milliseconds)
293 {
294 // assign first to avoid precision loss
295 m_diff = (long)hours;
296 m_diff *= 60l;
297 m_diff += minutes;
298 m_diff *= 60l;
299 m_diff += seconds;
300 m_diff *= 1000l;
301 m_diff += milliseconds;
302 }
303
304 // ----------------------------------------------------------------------------
305 // wxTimeSpan accessors
306 // ----------------------------------------------------------------------------
307
308 wxLongLong wxTimeSpan::GetSeconds() const
309 {
310 return m_diff / 1000l;
311 }
312
313 int wxTimeSpan::GetMinutes() const
314 {
315 return (GetSeconds() / 60l).GetLo();
316 }
317
318 int wxTimeSpan::GetHours() const
319 {
320 return GetMinutes() / 60;
321 }
322
323 int wxTimeSpan::GetDays() const
324 {
325 return GetHours() / 24;
326 }
327
328 int wxTimeSpan::GetWeeks() const
329 {
330 return GetDays() / 7;
331 }
332
333 // ----------------------------------------------------------------------------
334 // wxTimeSpan arithmetics
335 // ----------------------------------------------------------------------------
336
337 wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const
338 {
339 return wxTimeSpan(m_diff + diff.GetValue());
340 }
341
342 wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
343 {
344 m_diff += diff.GetValue();
345
346 return *this;
347 }
348
349 wxTimeSpan wxTimeSpan::Substract(const wxTimeSpan& diff) const
350 {
351 return wxTimeSpan(m_diff - diff.GetValue());
352 }
353
354 wxTimeSpan& wxTimeSpan::Substract(const wxTimeSpan& diff)
355 {
356 m_diff -= diff.GetValue();
357
358 return *this;
359 }
360
361 wxTimeSpan& wxTimeSpan::Multiply(int n)
362 {
363 m_diff *= (long)n;
364
365 return *this;
366 }
367
368 wxTimeSpan wxTimeSpan::Multiply(int n) const
369 {
370 return wxTimeSpan(m_diff * (long)n);
371 }
372
373 wxTimeSpan wxTimeSpan::Abs() const
374 {
375 return wxTimeSpan(GetValue().Abs());
376 }
377
378 bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
379 {
380 return GetValue() == ts.GetValue();
381 }
382
383 bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
384 {
385 return GetValue().Abs() > ts.GetValue().Abs();
386 }
387
388 // ----------------------------------------------------------------------------
389 // wxDateSpan
390 // ----------------------------------------------------------------------------
391
392 wxDateSpan&
393 wxDateSpan::operator+=(const wxDateSpan& other)
394 {
395 m_years += other.m_years;
396 m_months += other.m_months;
397 m_weeks += other.m_weeks;
398 m_days += other.m_days;
399
400 return *this;
401 }
402
403 wxDateSpan& wxDateSpan::Multiply(int factor)
404 {
405 m_years *= factor;
406 m_months *= factor;
407 m_weeks *= factor;
408 m_days *= factor;
409
410 return *this;
411 }
412
413 wxDateSpan wxDateSpan::Multiply(int factor) const
414 {
415 return wxDateSpan(*this).Multiply(factor);
416 }
417
418 wxDateSpan wxDateSpan::Negate() const
419 {
420 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
421 }
422
423 wxDateSpan& wxDateSpan::Neg()
424 {
425 m_years = -m_years;
426 m_months = -m_months;
427 m_weeks = -m_weeks;
428 m_days = -m_days;
429
430 return *this;
431 }
432
433 #undef MILLISECONDS_PER_DAY