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