]> git.saurik.com Git - wxWidgets.git/blame_incremental - include/wx/datetime.inl
avoid a bug in Carbon headers
[wxWidgets.git] / include / wx / datetime.inl
... / ...
CommitLineData
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
38inline bool wxDateTime::IsInStdRange() const
39{
40 return m_time >= 0l && (m_time / TIME_T_FACTOR) < LONG_MAX;
41}
42
43/* static */
44inline wxDateTime wxDateTime::Now()
45{
46 return wxDateTime(*GetTmNow());
47}
48
49/* static */
50inline 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))
61inline 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
71inline wxDateTime& wxDateTime::SetToCurrent()
72{
73 *this = Now();
74 return *this;
75}
76
77#if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
78inline wxDateTime::wxDateTime(time_t timet)
79{
80 Set(timet);
81}
82#endif
83
84inline wxDateTime::wxDateTime(const struct tm& tm)
85{
86 Set(tm);
87}
88
89inline wxDateTime::wxDateTime(const Tm& tm)
90{
91 Set(tm);
92}
93
94inline wxDateTime::wxDateTime(double jdn)
95{
96 Set(jdn);
97}
98
99inline 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
106inline 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
114inline 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
129inline wxLongLong wxDateTime::GetValue() const
130{
131 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
132
133 return m_time;
134}
135
136inline 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
147inline bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
148 Month month,
149 int year)
150{
151 return SetToWeekDay(weekday, -1, month, year);
152}
153
154inline wxDateTime wxDateTime::GetWeekDayInSameWeek(WeekDay weekday,
155 WeekFlags flags) const
156{
157 MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday) );
158}
159
160inline wxDateTime wxDateTime::GetNextWeekDay(WeekDay weekday) const
161{
162 MODIFY_AND_RETURN( SetToNextWeekDay(weekday) );
163}
164
165inline wxDateTime wxDateTime::GetPrevWeekDay(WeekDay weekday) const
166{
167 MODIFY_AND_RETURN( SetToPrevWeekDay(weekday) );
168}
169
170inline 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
180inline 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
189inline 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
198inline wxDateTime wxDateTime::GetLastMonthDay(Month month, int year) const
199{
200 MODIFY_AND_RETURN( SetToLastMonthDay(month, year) );
201}
202
203inline wxDateTime wxDateTime::GetYearDay(wxDateTime_t yday) const
204{
205 MODIFY_AND_RETURN( SetToYearDay(yday) );
206}
207
208// ----------------------------------------------------------------------------
209// wxDateTime comparison
210// ----------------------------------------------------------------------------
211
212inline 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
219inline 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
226inline 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
233inline 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
240inline 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
247inline 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
257inline 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
273inline 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
283inline 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
290inline 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
299inline wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
300{
301 return Add(diff);
302}
303
304inline 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
311inline 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
320inline wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
321{
322 return Subtract(diff);
323}
324
325inline 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
332inline wxDateTime wxDateTime::Add(const wxDateSpan& diff) const
333{
334 return wxDateTime(*this).Add(diff);
335}
336
337inline wxDateTime& wxDateTime::Subtract(const wxDateSpan& diff)
338{
339 return Add(diff.Negate());
340}
341
342inline wxDateTime wxDateTime::Subtract(const wxDateSpan& diff) const
343{
344 return wxDateTime(*this).Subtract(diff);
345}
346
347inline wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
348{
349 return Subtract(diff);
350}
351
352inline wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
353{
354 return Add(diff);
355}
356
357// ----------------------------------------------------------------------------
358// wxDateTime and timezones
359// ----------------------------------------------------------------------------
360
361inline 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
371inline 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
390inline wxLongLong wxTimeSpan::GetSeconds() const
391{
392 return m_diff / 1000l;
393}
394
395inline int wxTimeSpan::GetMinutes() const
396{
397 return (GetSeconds() / 60l).GetLo();
398}
399
400inline int wxTimeSpan::GetHours() const
401{
402 return GetMinutes() / 60;
403}
404
405inline int wxTimeSpan::GetDays() const
406{
407 return GetHours() / 24;
408}
409
410inline int wxTimeSpan::GetWeeks() const
411{
412 return GetDays() / 7;
413}
414
415// ----------------------------------------------------------------------------
416// wxTimeSpan arithmetics
417// ----------------------------------------------------------------------------
418
419inline wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const
420{
421 return wxTimeSpan(m_diff + diff.GetValue());
422}
423
424inline wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
425{
426 m_diff += diff.GetValue();
427
428 return *this;
429}
430
431inline wxTimeSpan wxTimeSpan::Subtract(const wxTimeSpan& diff) const
432{
433 return wxTimeSpan(m_diff - diff.GetValue());
434}
435
436inline wxTimeSpan& wxTimeSpan::Subtract(const wxTimeSpan& diff)
437{
438 m_diff -= diff.GetValue();
439
440 return *this;
441}
442
443inline wxTimeSpan& wxTimeSpan::Multiply(int n)
444{
445 m_diff *= (long)n;
446
447 return *this;
448}
449
450inline wxTimeSpan wxTimeSpan::Multiply(int n) const
451{
452 return wxTimeSpan(m_diff * (long)n);
453}
454
455inline wxTimeSpan wxTimeSpan::Abs() const
456{
457 return wxTimeSpan(GetValue().Abs());
458}
459
460inline bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
461{
462 return GetValue() == ts.GetValue();
463}
464
465inline bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
466{
467 return GetValue().Abs() > ts.GetValue().Abs();
468}
469
470// ----------------------------------------------------------------------------
471// wxDateSpan
472// ----------------------------------------------------------------------------
473
474inline 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
484inline wxDateSpan& wxDateSpan::Add(const wxDateSpan& other)
485{
486 return *this += other;
487}
488
489inline wxDateSpan wxDateSpan::Add(const wxDateSpan& other) const
490{
491 wxDateSpan ds(*this);
492 ds.Add(other);
493 return ds;
494}
495
496inline 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
506inline wxDateSpan wxDateSpan::Multiply(int factor) const
507{
508 wxDateSpan ds(*this);
509 ds.Multiply(factor);
510 return ds;
511}
512
513inline wxDateSpan wxDateSpan::Negate() const
514{
515 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
516}
517
518inline 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
528inline wxDateSpan& wxDateSpan::operator-=(const wxDateSpan& other)
529{
530 return *this += other.Negate();
531}
532
533inline wxDateSpan& wxDateSpan::Subtract(const wxDateSpan& other)
534{
535 return *this -= other;
536}
537
538inline 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