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