]> git.saurik.com Git - wxWidgets.git/blob - include/wx/datetime.inl
Forgot to update this enhancement from last weekend
[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 // private macros
21 // ----------------------------------------------------------------------------
22
23 #define MILLISECONDS_PER_DAY 86400000l
24
25 // some broken compilers (HP-UX CC) refuse to compile the "normal" version, but
26 // using a temp variable always might prevent other compilers from optimising
27 // it away - hence use of this ugly macro
28 #ifndef __HPUX__
29 #define MODIFY_AND_RETURN(op) return wxDateTime(*this).op
30 #else
31 #define MODIFY_AND_RETURN(op) wxDateTime dt(*this); dt.op; return dt
32 #endif
33
34 // ----------------------------------------------------------------------------
35 // wxDateTime construction
36 // ----------------------------------------------------------------------------
37
38 // only define this once, when included from datetime.cpp
39 #ifdef wxDEFINE_TIME_CONSTANTS
40 const long wxDateTime::TIME_T_FACTOR = 1000l;
41 #endif // wxDEFINE_TIME_CONSTANTS
42
43 inline bool wxDateTime::IsInStdRange() const
44 {
45 return m_time >= 0l && (m_time / TIME_T_FACTOR) < LONG_MAX;
46 }
47
48 /* static */
49 inline wxDateTime wxDateTime::Now()
50 {
51 return wxDateTime(*GetTmNow());
52 }
53
54 /* static */
55 inline wxDateTime wxDateTime::Today()
56 {
57 struct tm *tm = GetTmNow();
58 tm->tm_hour =
59 tm->tm_min =
60 tm->tm_sec = 0;
61
62 return wxDateTime(*tm);
63 }
64
65 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
66 inline wxDateTime& wxDateTime::Set(time_t timet)
67 {
68 // assign first to avoid long multiplication overflow!
69 m_time = timet;
70 m_time *= TIME_T_FACTOR;
71
72 return *this;
73 }
74 #endif
75
76 inline wxDateTime& wxDateTime::SetToCurrent()
77 {
78 *this = Now();
79 return *this;
80 }
81
82 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
83 inline wxDateTime::wxDateTime(time_t timet)
84 {
85 Set(timet);
86 }
87 #endif
88
89 inline wxDateTime::wxDateTime(const struct tm& tm)
90 {
91 Set(tm);
92 }
93
94 inline wxDateTime::wxDateTime(const Tm& tm)
95 {
96 Set(tm);
97 }
98
99 inline wxDateTime::wxDateTime(double jdn)
100 {
101 Set(jdn);
102 }
103
104 inline wxDateTime& wxDateTime::Set(const Tm& tm)
105 {
106 wxASSERT_MSG( tm.IsValid(), _T("invalid broken down date/time") );
107
108 return Set(tm.mday, (Month)tm.mon, tm.year, tm.hour, tm.min, tm.sec);
109 }
110
111 inline wxDateTime::wxDateTime(wxDateTime_t hour,
112 wxDateTime_t minute,
113 wxDateTime_t second,
114 wxDateTime_t millisec)
115 {
116 Set(hour, minute, second, millisec);
117 }
118
119 inline wxDateTime::wxDateTime(wxDateTime_t day,
120 Month month,
121 int year,
122 wxDateTime_t hour,
123 wxDateTime_t minute,
124 wxDateTime_t second,
125 wxDateTime_t millisec)
126 {
127 Set(day, month, year, hour, minute, second, millisec);
128 }
129
130 // ----------------------------------------------------------------------------
131 // wxDateTime accessors
132 // ----------------------------------------------------------------------------
133
134 inline wxLongLong wxDateTime::GetValue() const
135 {
136 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
137
138 return m_time;
139 }
140
141 inline time_t wxDateTime::GetTicks() const
142 {
143 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
144 if ( !IsInStdRange() )
145 {
146 return (time_t)-1;
147 }
148
149 return (time_t)((m_time / (long)TIME_T_FACTOR).GetLo());
150 }
151
152 inline bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
153 Month month,
154 int year)
155 {
156 return SetToWeekDay(weekday, -1, month, year);
157 }
158
159 inline wxDateTime wxDateTime::GetWeekDayInSameWeek(WeekDay weekday) const
160 {
161 MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday) );
162 }
163
164 inline wxDateTime wxDateTime::GetNextWeekDay(WeekDay weekday) const
165 {
166 MODIFY_AND_RETURN( SetToNextWeekDay(weekday) );
167 }
168
169 inline wxDateTime wxDateTime::GetPrevWeekDay(WeekDay weekday) const
170 {
171 MODIFY_AND_RETURN( SetToPrevWeekDay(weekday) );
172 }
173
174 inline wxDateTime wxDateTime::GetWeekDay(WeekDay weekday,
175 int n,
176 Month month,
177 int year) const
178 {
179 wxDateTime dt(*this);
180
181 return dt.SetToWeekDay(weekday, n, month, year) ? dt : wxInvalidDateTime;
182 }
183
184 inline wxDateTime wxDateTime::GetLastWeekDay(WeekDay weekday,
185 Month month,
186 int year)
187 {
188 wxDateTime dt(*this);
189
190 return dt.SetToLastWeekDay(weekday, month, year) ? dt : wxInvalidDateTime;
191 }
192
193 inline wxDateTime wxDateTime::GetWeek(wxDateTime_t numWeek,
194 WeekDay weekday) const
195 {
196 wxDateTime dt(*this);
197
198 return dt.SetToTheWeek(numWeek, weekday) ? dt : wxInvalidDateTime;
199 }
200
201 inline wxDateTime wxDateTime::GetLastMonthDay(Month month, int year) const
202 {
203 MODIFY_AND_RETURN( SetToLastMonthDay(month, year) );
204 }
205
206 inline wxDateTime wxDateTime::GetYearDay(wxDateTime_t yday) const
207 {
208 MODIFY_AND_RETURN( SetToYearDay(yday) );
209 }
210
211 // ----------------------------------------------------------------------------
212 // wxDateTime comparison
213 // ----------------------------------------------------------------------------
214
215 inline bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
216 {
217 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
218
219 return m_time == datetime.m_time;
220 }
221
222 inline bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
223 {
224 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
225
226 return m_time < datetime.m_time;
227 }
228
229 inline bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
230 {
231 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
232
233 return m_time > datetime.m_time;
234 }
235
236 inline bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1,
237 const wxDateTime& t2) const
238 {
239 // no need for assert, will be checked by the functions we call
240 return IsLaterThan(t1) && IsEarlierThan(t2);
241 }
242
243 inline bool wxDateTime::IsBetween(const wxDateTime& t1,
244 const wxDateTime& t2) const
245 {
246 // no need for assert, will be checked by the functions we call
247 return IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2);
248 }
249
250 inline bool wxDateTime::IsSameDate(const wxDateTime& dt) const
251 {
252 return (m_time - dt.m_time).Abs() < MILLISECONDS_PER_DAY;
253 }
254
255 inline bool wxDateTime::IsSameTime(const wxDateTime& dt) const
256 {
257 // notice that we can't do something like this:
258 //
259 // m_time % MILLISECONDS_PER_DAY == dt.m_time % MILLISECONDS_PER_DAY
260 //
261 // because we have also to deal with (possibly) different DST settings!
262 Tm tm1 = GetTm(),
263 tm2 = dt.GetTm();
264
265 return tm1.hour == tm2.hour &&
266 tm1.min == tm2.min &&
267 tm1.sec == tm2.sec &&
268 tm1.msec == tm2.msec;
269 }
270
271 inline bool wxDateTime::IsEqualUpTo(const wxDateTime& dt,
272 const wxTimeSpan& ts) const
273 {
274 return IsBetween(dt.Subtract(ts), dt.Add(ts));
275 }
276
277 // ----------------------------------------------------------------------------
278 // wxDateTime arithmetics
279 // ----------------------------------------------------------------------------
280
281 inline wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const
282 {
283 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
284
285 return wxDateTime(m_time + diff.GetValue());
286 }
287
288 inline wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
289 {
290 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
291
292 m_time += diff.GetValue();
293
294 return *this;
295 }
296
297 inline wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
298 {
299 return Add(diff);
300 }
301
302 inline wxDateTime wxDateTime::Subtract(const wxTimeSpan& diff) const
303 {
304 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
305
306 return wxDateTime(m_time - diff.GetValue());
307 }
308
309 inline wxDateTime& wxDateTime::Subtract(const wxTimeSpan& diff)
310 {
311 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
312
313 m_time -= diff.GetValue();
314
315 return *this;
316 }
317
318 inline wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
319 {
320 return Subtract(diff);
321 }
322
323 inline wxTimeSpan wxDateTime::Subtract(const wxDateTime& datetime) const
324 {
325 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
326
327 return wxTimeSpan(GetValue() - datetime.GetValue());
328 }
329
330 inline wxDateTime wxDateTime::Add(const wxDateSpan& diff) const
331 {
332 return wxDateTime(*this).Add(diff);
333 }
334
335 inline wxDateTime& wxDateTime::Subtract(const wxDateSpan& diff)
336 {
337 return Add(diff.Negate());
338 }
339
340 inline wxDateTime wxDateTime::Subtract(const wxDateSpan& diff) const
341 {
342 return wxDateTime(*this).Subtract(diff);
343 }
344
345 inline wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
346 {
347 return Subtract(diff);
348 }
349
350 inline wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
351 {
352 return Add(diff);
353 }
354
355 // ----------------------------------------------------------------------------
356 // wxDateTime and timezones
357 // ----------------------------------------------------------------------------
358
359 inline wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz,
360 bool noDST) const
361 {
362 MODIFY_AND_RETURN( MakeTimezone(tz, noDST) );
363 }
364
365 // ----------------------------------------------------------------------------
366 // wxTimeSpan construction
367 // ----------------------------------------------------------------------------
368
369 inline wxTimeSpan::wxTimeSpan(long hours,
370 long minutes,
371 long seconds,
372 long milliseconds)
373 {
374 // assign first to avoid precision loss
375 m_diff = hours;
376 m_diff *= 60l;
377 m_diff += minutes;
378 m_diff *= 60l;
379 m_diff += seconds;
380 m_diff *= 1000l;
381 m_diff += milliseconds;
382 }
383
384 // ----------------------------------------------------------------------------
385 // wxTimeSpan accessors
386 // ----------------------------------------------------------------------------
387
388 inline wxLongLong wxTimeSpan::GetSeconds() const
389 {
390 return m_diff / 1000l;
391 }
392
393 inline int wxTimeSpan::GetMinutes() const
394 {
395 return (GetSeconds() / 60l).GetLo();
396 }
397
398 inline int wxTimeSpan::GetHours() const
399 {
400 return GetMinutes() / 60;
401 }
402
403 inline int wxTimeSpan::GetDays() const
404 {
405 return GetHours() / 24;
406 }
407
408 inline int wxTimeSpan::GetWeeks() const
409 {
410 return GetDays() / 7;
411 }
412
413 // ----------------------------------------------------------------------------
414 // wxTimeSpan arithmetics
415 // ----------------------------------------------------------------------------
416
417 inline wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const
418 {
419 return wxTimeSpan(m_diff + diff.GetValue());
420 }
421
422 inline wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
423 {
424 m_diff += diff.GetValue();
425
426 return *this;
427 }
428
429 inline wxTimeSpan wxTimeSpan::Subtract(const wxTimeSpan& diff) const
430 {
431 return wxTimeSpan(m_diff - diff.GetValue());
432 }
433
434 inline wxTimeSpan& wxTimeSpan::Subtract(const wxTimeSpan& diff)
435 {
436 m_diff -= diff.GetValue();
437
438 return *this;
439 }
440
441 inline wxTimeSpan& wxTimeSpan::Multiply(int n)
442 {
443 m_diff *= (long)n;
444
445 return *this;
446 }
447
448 inline wxTimeSpan wxTimeSpan::Multiply(int n) const
449 {
450 return wxTimeSpan(m_diff * (long)n);
451 }
452
453 inline wxTimeSpan wxTimeSpan::Abs() const
454 {
455 return wxTimeSpan(GetValue().Abs());
456 }
457
458 inline bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
459 {
460 return GetValue() == ts.GetValue();
461 }
462
463 inline bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
464 {
465 return GetValue().Abs() > ts.GetValue().Abs();
466 }
467
468 // ----------------------------------------------------------------------------
469 // wxDateSpan
470 // ----------------------------------------------------------------------------
471
472 inline wxDateSpan& wxDateSpan::operator+=(const wxDateSpan& other)
473 {
474 m_years += other.m_years;
475 m_months += other.m_months;
476 m_weeks += other.m_weeks;
477 m_days += other.m_days;
478
479 return *this;
480 }
481
482 inline wxDateSpan& wxDateSpan::Add(const wxDateSpan& other)
483 {
484 return *this += other;
485 }
486
487 inline wxDateSpan wxDateSpan::Add(const wxDateSpan& other) const
488 {
489 wxDateSpan ds(*this);
490 ds.Add(other);
491 return ds;
492 }
493
494 inline wxDateSpan& wxDateSpan::Multiply(int factor)
495 {
496 m_years *= factor;
497 m_months *= factor;
498 m_weeks *= factor;
499 m_days *= factor;
500
501 return *this;
502 }
503
504 inline wxDateSpan wxDateSpan::Multiply(int factor) const
505 {
506 wxDateSpan ds(*this);
507 ds.Multiply(factor);
508 return ds;
509 }
510
511 inline wxDateSpan wxDateSpan::Negate() const
512 {
513 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
514 }
515
516 inline wxDateSpan& wxDateSpan::Neg()
517 {
518 m_years = -m_years;
519 m_months = -m_months;
520 m_weeks = -m_weeks;
521 m_days = -m_days;
522
523 return *this;
524 }
525
526 inline wxDateSpan& wxDateSpan::operator-=(const wxDateSpan& other)
527 {
528 return *this += other.Negate();
529 }
530
531 inline wxDateSpan& wxDateSpan::Subtract(const wxDateSpan& other)
532 {
533 return *this -= other;
534 }
535
536 inline wxDateSpan wxDateSpan::Subtract(const wxDateSpan& other) const
537 {
538 wxDateSpan ds(*this);
539 ds.Subtract(other);
540 return ds;
541 }
542
543 #undef MILLISECONDS_PER_DAY
544
545 #undef MODIFY_AND_RETURN