]> git.saurik.com Git - wxWidgets.git/blame - include/wx/datetime.inl
Update to Scintilla 1.53
[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
103 return Set(tm.mday, (Month)tm.mon, tm.year, tm.hour, tm.min, tm.sec);
104}
105
f6bcfd97
BP
106inline wxDateTime::wxDateTime(wxDateTime_t hour,
107 wxDateTime_t minute,
108 wxDateTime_t second,
109 wxDateTime_t millisec)
2f02cb89
VZ
110{
111 Set(hour, minute, second, millisec);
112}
113
f6bcfd97
BP
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)
2f02cb89
VZ
121{
122 Set(day, month, year, hour, minute, second, millisec);
123}
124
125// ----------------------------------------------------------------------------
126// wxDateTime accessors
127// ----------------------------------------------------------------------------
128
f6bcfd97 129inline wxLongLong wxDateTime::GetValue() const
2f02cb89 130{
cd0b1709 131 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
2f02cb89
VZ
132
133 return m_time;
134}
135
f6bcfd97 136inline time_t wxDateTime::GetTicks() const
2f02cb89 137{
cd0b1709 138 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
2f02cb89
VZ
139 if ( !IsInStdRange() )
140 {
141 return (time_t)-1;
142 }
143
5b781a67 144 return (time_t)((m_time / (long)TIME_T_FACTOR).GetLo())+WX_TIME_BASE_OFFSET ;
2f02cb89
VZ
145}
146
e5aea721 147inline bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
f6bcfd97
BP
148 Month month,
149 int year)
2f02cb89
VZ
150{
151 return SetToWeekDay(weekday, -1, month, year);
152}
153
2b5f62a0
VZ
154inline wxDateTime wxDateTime::GetWeekDayInSameWeek(WeekDay weekday,
155 WeekFlags flags) const
4f6aed9c
VZ
156{
157 MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday) );
158}
159
f6bcfd97 160inline wxDateTime wxDateTime::GetNextWeekDay(WeekDay weekday) const
4f6aed9c
VZ
161{
162 MODIFY_AND_RETURN( SetToNextWeekDay(weekday) );
163}
164
f6bcfd97 165inline wxDateTime wxDateTime::GetPrevWeekDay(WeekDay weekday) const
4f6aed9c
VZ
166{
167 MODIFY_AND_RETURN( SetToPrevWeekDay(weekday) );
168}
169
f6bcfd97
BP
170inline wxDateTime wxDateTime::GetWeekDay(WeekDay weekday,
171 int n,
172 Month month,
173 int year) const
4f6aed9c
VZ
174{
175 wxDateTime dt(*this);
176
177 return dt.SetToWeekDay(weekday, n, month, year) ? dt : wxInvalidDateTime;
178}
179
f6bcfd97
BP
180inline wxDateTime wxDateTime::GetLastWeekDay(WeekDay weekday,
181 Month month,
182 int year)
4f6aed9c
VZ
183{
184 wxDateTime dt(*this);
185
186 return dt.SetToLastWeekDay(weekday, month, year) ? dt : wxInvalidDateTime;
187}
188
f6bcfd97 189inline wxDateTime wxDateTime::GetWeek(wxDateTime_t numWeek,
2b5f62a0
VZ
190 WeekDay weekday,
191 WeekFlags flags) const
4f6aed9c
VZ
192{
193 wxDateTime dt(*this);
194
2b5f62a0 195 return dt.SetToTheWeek(numWeek, weekday, flags) ? dt : wxInvalidDateTime;
4f6aed9c
VZ
196}
197
f6bcfd97 198inline wxDateTime wxDateTime::GetLastMonthDay(Month month, int year) const
4f6aed9c
VZ
199{
200 MODIFY_AND_RETURN( SetToLastMonthDay(month, year) );
201}
202
f6bcfd97 203inline wxDateTime wxDateTime::GetYearDay(wxDateTime_t yday) const
4f6aed9c
VZ
204{
205 MODIFY_AND_RETURN( SetToYearDay(yday) );
206}
207
2f02cb89
VZ
208// ----------------------------------------------------------------------------
209// wxDateTime comparison
210// ----------------------------------------------------------------------------
211
f6bcfd97 212inline bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
2f02cb89 213{
cd0b1709 214 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
2f02cb89
VZ
215
216 return m_time == datetime.m_time;
217}
218
f6bcfd97 219inline bool wxDateTime::IsEarlierThan(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::IsLaterThan(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
BP
233inline bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1,
234 const wxDateTime& t2) const
2f02cb89
VZ
235{
236 // no need for assert, will be checked by the functions we call
237 return IsLaterThan(t1) && IsEarlierThan(t2);
238}
239
f6bcfd97
BP
240inline bool wxDateTime::IsBetween(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 IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2);
245}
246
f6bcfd97 247inline bool wxDateTime::IsSameDate(const wxDateTime& dt) const
be4017f8 248{
f41cb81e
VZ
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;
be4017f8
VZ
255}
256
f6bcfd97 257inline bool wxDateTime::IsSameTime(const wxDateTime& dt) const
be4017f8
VZ
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
f6bcfd97
BP
273inline bool wxDateTime::IsEqualUpTo(const wxDateTime& dt,
274 const wxTimeSpan& ts) const
be4017f8 275{
f6bcfd97 276 return IsBetween(dt.Subtract(ts), dt.Add(ts));
be4017f8
VZ
277}
278
2f02cb89
VZ
279// ----------------------------------------------------------------------------
280// wxDateTime arithmetics
281// ----------------------------------------------------------------------------
282
f6bcfd97 283inline wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const
cd0b1709
VZ
284{
285 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
286
287 return wxDateTime(m_time + diff.GetValue());
288}
289
f6bcfd97 290inline wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
2f02cb89 291{
cd0b1709 292 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
2f02cb89
VZ
293
294 m_time += diff.GetValue();
295
296 return *this;
297}
298
f6bcfd97 299inline wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
2f02cb89
VZ
300{
301 return Add(diff);
302}
303
f6bcfd97 304inline wxDateTime wxDateTime::Subtract(const wxTimeSpan& diff) const
cd0b1709
VZ
305{
306 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
307
308 return wxDateTime(m_time - diff.GetValue());
309}
310
f6bcfd97 311inline wxDateTime& wxDateTime::Subtract(const wxTimeSpan& diff)
2f02cb89 312{
cd0b1709 313 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
2f02cb89
VZ
314
315 m_time -= diff.GetValue();
316
317 return *this;
318}
319
f6bcfd97 320inline wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
2f02cb89 321{
f6bcfd97 322 return Subtract(diff);
2f02cb89
VZ
323}
324
f6bcfd97 325inline wxTimeSpan wxDateTime::Subtract(const wxDateTime& datetime) const
2f02cb89 326{
cd0b1709 327 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
2f02cb89 328
f6bcfd97 329 return wxTimeSpan(GetValue() - datetime.GetValue());
2f02cb89
VZ
330}
331
f6bcfd97 332inline wxDateTime wxDateTime::Add(const wxDateSpan& diff) const
68ee7c47
VZ
333{
334 return wxDateTime(*this).Add(diff);
335}
336
f6bcfd97 337inline wxDateTime& wxDateTime::Subtract(const wxDateSpan& diff)
2f02cb89
VZ
338{
339 return Add(diff.Negate());
340}
341
f6bcfd97 342inline wxDateTime wxDateTime::Subtract(const wxDateSpan& diff) const
68ee7c47 343{
f6bcfd97 344 return wxDateTime(*this).Subtract(diff);
68ee7c47
VZ
345}
346
f6bcfd97 347inline wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
2f02cb89 348{
f6bcfd97 349 return Subtract(diff);
2f02cb89
VZ
350}
351
f6bcfd97 352inline wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
2f02cb89
VZ
353{
354 return Add(diff);
355}
356
fcc3d7cb
VZ
357// ----------------------------------------------------------------------------
358// wxDateTime and timezones
359// ----------------------------------------------------------------------------
360
f6bcfd97
BP
361inline wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz,
362 bool noDST) const
fcc3d7cb 363{
4f6aed9c 364 MODIFY_AND_RETURN( MakeTimezone(tz, noDST) );
fcc3d7cb
VZ
365}
366
2f02cb89 367// ----------------------------------------------------------------------------
e6ec579c 368// wxTimeSpan construction
2f02cb89
VZ
369// ----------------------------------------------------------------------------
370
f6bcfd97
BP
371inline wxTimeSpan::wxTimeSpan(long hours,
372 long minutes,
373 long seconds,
374 long milliseconds)
fcc3d7cb
VZ
375{
376 // assign first to avoid precision loss
6f2a55e3 377 m_diff = hours;
cd0b1709 378 m_diff *= 60l;
fcc3d7cb 379 m_diff += minutes;
cd0b1709 380 m_diff *= 60l;
fcc3d7cb 381 m_diff += seconds;
cd0b1709 382 m_diff *= 1000l;
fcc3d7cb
VZ
383 m_diff += milliseconds;
384}
385
e6ec579c
VZ
386// ----------------------------------------------------------------------------
387// wxTimeSpan accessors
388// ----------------------------------------------------------------------------
389
f6bcfd97 390inline wxLongLong wxTimeSpan::GetSeconds() const
e6ec579c
VZ
391{
392 return m_diff / 1000l;
393}
394
f6bcfd97 395inline int wxTimeSpan::GetMinutes() const
e6ec579c
VZ
396{
397 return (GetSeconds() / 60l).GetLo();
398}
399
f6bcfd97 400inline int wxTimeSpan::GetHours() const
e6ec579c
VZ
401{
402 return GetMinutes() / 60;
403}
404
f6bcfd97 405inline int wxTimeSpan::GetDays() const
e6ec579c
VZ
406{
407 return GetHours() / 24;
408}
409
f6bcfd97 410inline int wxTimeSpan::GetWeeks() const
e6ec579c
VZ
411{
412 return GetDays() / 7;
413}
414
415// ----------------------------------------------------------------------------
416// wxTimeSpan arithmetics
417// ----------------------------------------------------------------------------
418
f6bcfd97 419inline wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const
cd0b1709
VZ
420{
421 return wxTimeSpan(m_diff + diff.GetValue());
422}
423
f6bcfd97 424inline wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
2f02cb89
VZ
425{
426 m_diff += diff.GetValue();
427
428 return *this;
429}
430
f6bcfd97 431inline wxTimeSpan wxTimeSpan::Subtract(const wxTimeSpan& diff) const
cd0b1709
VZ
432{
433 return wxTimeSpan(m_diff - diff.GetValue());
434}
435
f6bcfd97 436inline wxTimeSpan& wxTimeSpan::Subtract(const wxTimeSpan& diff)
2f02cb89
VZ
437{
438 m_diff -= diff.GetValue();
439
440 return *this;
441}
442
f6bcfd97 443inline wxTimeSpan& wxTimeSpan::Multiply(int n)
2f02cb89 444{
cd0b1709 445 m_diff *= (long)n;
2f02cb89
VZ
446
447 return *this;
448}
449
f6bcfd97 450inline wxTimeSpan wxTimeSpan::Multiply(int n) const
2f02cb89 451{
cd0b1709 452 return wxTimeSpan(m_diff * (long)n);
2f02cb89
VZ
453}
454
f6bcfd97 455inline wxTimeSpan wxTimeSpan::Abs() const
2f02cb89
VZ
456{
457 return wxTimeSpan(GetValue().Abs());
458}
459
f6bcfd97 460inline bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
2f02cb89
VZ
461{
462 return GetValue() == ts.GetValue();
463}
464
f6bcfd97 465inline bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
2f02cb89
VZ
466{
467 return GetValue().Abs() > ts.GetValue().Abs();
468}
469
470// ----------------------------------------------------------------------------
471// wxDateSpan
472// ----------------------------------------------------------------------------
473
f6bcfd97 474inline wxDateSpan& wxDateSpan::operator+=(const wxDateSpan& other)
2f02cb89
VZ
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
f6bcfd97
BP
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)
2f02cb89 497{
9c2882d9
VZ
498 m_years *= factor;
499 m_months *= factor;
500 m_weeks *= factor;
501 m_days *= factor;
2f02cb89
VZ
502
503 return *this;
504}
505
f6bcfd97 506inline wxDateSpan wxDateSpan::Multiply(int factor) const
cd0b1709 507{
f6bcfd97
BP
508 wxDateSpan ds(*this);
509 ds.Multiply(factor);
510 return ds;
cd0b1709
VZ
511}
512
f6bcfd97 513inline wxDateSpan wxDateSpan::Negate() const
2f02cb89
VZ
514{
515 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
516}
517
f6bcfd97 518inline wxDateSpan& wxDateSpan::Neg()
2f02cb89
VZ
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
f6bcfd97
BP
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
be4017f8 545#undef MILLISECONDS_PER_DAY
4f6aed9c
VZ
546
547#undef MODIFY_AND_RETURN