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