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