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