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