]> git.saurik.com Git - wxWidgets.git/blame - include/wx/datetime.inl
Added simplistic makefile/project files for wxWinCE.
[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
2b5f62a0
VZ
159inline wxDateTime wxDateTime::GetWeekDayInSameWeek(WeekDay weekday,
160 WeekFlags flags) const
4f6aed9c
VZ
161{
162 MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday) );
163}
164
f6bcfd97 165inline wxDateTime wxDateTime::GetNextWeekDay(WeekDay weekday) const
4f6aed9c
VZ
166{
167 MODIFY_AND_RETURN( SetToNextWeekDay(weekday) );
168}
169
f6bcfd97 170inline wxDateTime wxDateTime::GetPrevWeekDay(WeekDay weekday) const
4f6aed9c
VZ
171{
172 MODIFY_AND_RETURN( SetToPrevWeekDay(weekday) );
173}
174
f6bcfd97
BP
175inline wxDateTime wxDateTime::GetWeekDay(WeekDay weekday,
176 int n,
177 Month month,
178 int year) const
4f6aed9c
VZ
179{
180 wxDateTime dt(*this);
181
182 return dt.SetToWeekDay(weekday, n, month, year) ? dt : wxInvalidDateTime;
183}
184
f6bcfd97
BP
185inline wxDateTime wxDateTime::GetLastWeekDay(WeekDay weekday,
186 Month month,
187 int year)
4f6aed9c
VZ
188{
189 wxDateTime dt(*this);
190
191 return dt.SetToLastWeekDay(weekday, month, year) ? dt : wxInvalidDateTime;
192}
193
f6bcfd97 194inline wxDateTime wxDateTime::GetWeek(wxDateTime_t numWeek,
2b5f62a0
VZ
195 WeekDay weekday,
196 WeekFlags flags) const
4f6aed9c
VZ
197{
198 wxDateTime dt(*this);
199
2b5f62a0 200 return dt.SetToTheWeek(numWeek, weekday, flags) ? dt : wxInvalidDateTime;
4f6aed9c
VZ
201}
202
f6bcfd97 203inline wxDateTime wxDateTime::GetLastMonthDay(Month month, int year) const
4f6aed9c
VZ
204{
205 MODIFY_AND_RETURN( SetToLastMonthDay(month, year) );
206}
207
f6bcfd97 208inline wxDateTime wxDateTime::GetYearDay(wxDateTime_t yday) const
4f6aed9c
VZ
209{
210 MODIFY_AND_RETURN( SetToYearDay(yday) );
211}
212
2f02cb89
VZ
213// ----------------------------------------------------------------------------
214// wxDateTime comparison
215// ----------------------------------------------------------------------------
216
f6bcfd97 217inline bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
2f02cb89 218{
cd0b1709 219 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
2f02cb89
VZ
220
221 return m_time == datetime.m_time;
222}
223
f6bcfd97 224inline bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
2f02cb89 225{
cd0b1709 226 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
2f02cb89
VZ
227
228 return m_time < datetime.m_time;
229}
230
f6bcfd97 231inline bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
2f02cb89 232{
cd0b1709 233 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
2f02cb89
VZ
234
235 return m_time > datetime.m_time;
236}
237
f6bcfd97
BP
238inline bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1,
239 const wxDateTime& t2) const
2f02cb89
VZ
240{
241 // no need for assert, will be checked by the functions we call
242 return IsLaterThan(t1) && IsEarlierThan(t2);
243}
244
f6bcfd97
BP
245inline bool wxDateTime::IsBetween(const wxDateTime& t1,
246 const wxDateTime& t2) const
2f02cb89
VZ
247{
248 // no need for assert, will be checked by the functions we call
249 return IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2);
250}
251
f6bcfd97 252inline bool wxDateTime::IsSameDate(const wxDateTime& dt) const
be4017f8 253{
f41cb81e
VZ
254 Tm tm1 = GetTm(),
255 tm2 = dt.GetTm();
256
257 return tm1.year == tm2.year &&
258 tm1.mon == tm2.mon &&
259 tm1.mday == tm2.mday;
be4017f8
VZ
260}
261
f6bcfd97 262inline bool wxDateTime::IsSameTime(const wxDateTime& dt) const
be4017f8
VZ
263{
264 // notice that we can't do something like this:
265 //
266 // m_time % MILLISECONDS_PER_DAY == dt.m_time % MILLISECONDS_PER_DAY
267 //
268 // because we have also to deal with (possibly) different DST settings!
269 Tm tm1 = GetTm(),
270 tm2 = dt.GetTm();
271
272 return tm1.hour == tm2.hour &&
273 tm1.min == tm2.min &&
274 tm1.sec == tm2.sec &&
275 tm1.msec == tm2.msec;
276}
277
f6bcfd97
BP
278inline bool wxDateTime::IsEqualUpTo(const wxDateTime& dt,
279 const wxTimeSpan& ts) const
be4017f8 280{
f6bcfd97 281 return IsBetween(dt.Subtract(ts), dt.Add(ts));
be4017f8
VZ
282}
283
2f02cb89
VZ
284// ----------------------------------------------------------------------------
285// wxDateTime arithmetics
286// ----------------------------------------------------------------------------
287
f6bcfd97 288inline wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const
cd0b1709
VZ
289{
290 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
291
292 return wxDateTime(m_time + diff.GetValue());
293}
294
f6bcfd97 295inline wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
2f02cb89 296{
cd0b1709 297 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
2f02cb89
VZ
298
299 m_time += diff.GetValue();
300
301 return *this;
302}
303
f6bcfd97 304inline wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
2f02cb89
VZ
305{
306 return Add(diff);
307}
308
f6bcfd97 309inline wxDateTime wxDateTime::Subtract(const wxTimeSpan& diff) const
cd0b1709
VZ
310{
311 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
312
313 return wxDateTime(m_time - diff.GetValue());
314}
315
f6bcfd97 316inline wxDateTime& wxDateTime::Subtract(const wxTimeSpan& diff)
2f02cb89 317{
cd0b1709 318 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
2f02cb89
VZ
319
320 m_time -= diff.GetValue();
321
322 return *this;
323}
324
f6bcfd97 325inline wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
2f02cb89 326{
f6bcfd97 327 return Subtract(diff);
2f02cb89
VZ
328}
329
f6bcfd97 330inline wxTimeSpan wxDateTime::Subtract(const wxDateTime& datetime) const
2f02cb89 331{
cd0b1709 332 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
2f02cb89 333
f6bcfd97 334 return wxTimeSpan(GetValue() - datetime.GetValue());
2f02cb89
VZ
335}
336
f6bcfd97 337inline wxDateTime wxDateTime::Add(const wxDateSpan& diff) const
68ee7c47
VZ
338{
339 return wxDateTime(*this).Add(diff);
340}
341
f6bcfd97 342inline wxDateTime& wxDateTime::Subtract(const wxDateSpan& diff)
2f02cb89
VZ
343{
344 return Add(diff.Negate());
345}
346
f6bcfd97 347inline wxDateTime wxDateTime::Subtract(const wxDateSpan& diff) const
68ee7c47 348{
f6bcfd97 349 return wxDateTime(*this).Subtract(diff);
68ee7c47
VZ
350}
351
f6bcfd97 352inline wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
2f02cb89 353{
f6bcfd97 354 return Subtract(diff);
2f02cb89
VZ
355}
356
f6bcfd97 357inline wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
2f02cb89
VZ
358{
359 return Add(diff);
360}
361
fcc3d7cb
VZ
362// ----------------------------------------------------------------------------
363// wxDateTime and timezones
364// ----------------------------------------------------------------------------
365
f6bcfd97
BP
366inline wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz,
367 bool noDST) const
fcc3d7cb 368{
4f6aed9c 369 MODIFY_AND_RETURN( MakeTimezone(tz, noDST) );
fcc3d7cb
VZ
370}
371
2f02cb89 372// ----------------------------------------------------------------------------
e6ec579c 373// wxTimeSpan construction
2f02cb89
VZ
374// ----------------------------------------------------------------------------
375
f6bcfd97
BP
376inline wxTimeSpan::wxTimeSpan(long hours,
377 long minutes,
378 long seconds,
379 long milliseconds)
fcc3d7cb
VZ
380{
381 // assign first to avoid precision loss
6f2a55e3 382 m_diff = hours;
cd0b1709 383 m_diff *= 60l;
fcc3d7cb 384 m_diff += minutes;
cd0b1709 385 m_diff *= 60l;
fcc3d7cb 386 m_diff += seconds;
cd0b1709 387 m_diff *= 1000l;
fcc3d7cb
VZ
388 m_diff += milliseconds;
389}
390
e6ec579c
VZ
391// ----------------------------------------------------------------------------
392// wxTimeSpan accessors
393// ----------------------------------------------------------------------------
394
f6bcfd97 395inline wxLongLong wxTimeSpan::GetSeconds() const
e6ec579c
VZ
396{
397 return m_diff / 1000l;
398}
399
f6bcfd97 400inline int wxTimeSpan::GetMinutes() const
e6ec579c
VZ
401{
402 return (GetSeconds() / 60l).GetLo();
403}
404
f6bcfd97 405inline int wxTimeSpan::GetHours() const
e6ec579c
VZ
406{
407 return GetMinutes() / 60;
408}
409
f6bcfd97 410inline int wxTimeSpan::GetDays() const
e6ec579c
VZ
411{
412 return GetHours() / 24;
413}
414
f6bcfd97 415inline int wxTimeSpan::GetWeeks() const
e6ec579c
VZ
416{
417 return GetDays() / 7;
418}
419
420// ----------------------------------------------------------------------------
421// wxTimeSpan arithmetics
422// ----------------------------------------------------------------------------
423
f6bcfd97 424inline wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const
cd0b1709
VZ
425{
426 return wxTimeSpan(m_diff + diff.GetValue());
427}
428
f6bcfd97 429inline wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
2f02cb89
VZ
430{
431 m_diff += diff.GetValue();
432
433 return *this;
434}
435
f6bcfd97 436inline wxTimeSpan wxTimeSpan::Subtract(const wxTimeSpan& diff) const
cd0b1709
VZ
437{
438 return wxTimeSpan(m_diff - diff.GetValue());
439}
440
f6bcfd97 441inline wxTimeSpan& wxTimeSpan::Subtract(const wxTimeSpan& diff)
2f02cb89
VZ
442{
443 m_diff -= diff.GetValue();
444
445 return *this;
446}
447
f6bcfd97 448inline wxTimeSpan& wxTimeSpan::Multiply(int n)
2f02cb89 449{
cd0b1709 450 m_diff *= (long)n;
2f02cb89
VZ
451
452 return *this;
453}
454
f6bcfd97 455inline wxTimeSpan wxTimeSpan::Multiply(int n) const
2f02cb89 456{
cd0b1709 457 return wxTimeSpan(m_diff * (long)n);
2f02cb89
VZ
458}
459
f6bcfd97 460inline wxTimeSpan wxTimeSpan::Abs() const
2f02cb89
VZ
461{
462 return wxTimeSpan(GetValue().Abs());
463}
464
f6bcfd97 465inline bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
2f02cb89
VZ
466{
467 return GetValue() == ts.GetValue();
468}
469
f6bcfd97 470inline bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
2f02cb89
VZ
471{
472 return GetValue().Abs() > ts.GetValue().Abs();
473}
474
475// ----------------------------------------------------------------------------
476// wxDateSpan
477// ----------------------------------------------------------------------------
478
f6bcfd97 479inline wxDateSpan& wxDateSpan::operator+=(const wxDateSpan& other)
2f02cb89
VZ
480{
481 m_years += other.m_years;
482 m_months += other.m_months;
483 m_weeks += other.m_weeks;
484 m_days += other.m_days;
485
486 return *this;
487}
488
f6bcfd97
BP
489inline wxDateSpan& wxDateSpan::Add(const wxDateSpan& other)
490{
491 return *this += other;
492}
493
494inline wxDateSpan wxDateSpan::Add(const wxDateSpan& other) const
495{
496 wxDateSpan ds(*this);
497 ds.Add(other);
498 return ds;
499}
500
501inline wxDateSpan& wxDateSpan::Multiply(int factor)
2f02cb89 502{
9c2882d9
VZ
503 m_years *= factor;
504 m_months *= factor;
505 m_weeks *= factor;
506 m_days *= factor;
2f02cb89
VZ
507
508 return *this;
509}
510
f6bcfd97 511inline wxDateSpan wxDateSpan::Multiply(int factor) const
cd0b1709 512{
f6bcfd97
BP
513 wxDateSpan ds(*this);
514 ds.Multiply(factor);
515 return ds;
cd0b1709
VZ
516}
517
f6bcfd97 518inline wxDateSpan wxDateSpan::Negate() const
2f02cb89
VZ
519{
520 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
521}
522
f6bcfd97 523inline wxDateSpan& wxDateSpan::Neg()
2f02cb89
VZ
524{
525 m_years = -m_years;
526 m_months = -m_months;
527 m_weeks = -m_weeks;
528 m_days = -m_days;
529
530 return *this;
531}
532
f6bcfd97
BP
533inline wxDateSpan& wxDateSpan::operator-=(const wxDateSpan& other)
534{
535 return *this += other.Negate();
536}
537
538inline wxDateSpan& wxDateSpan::Subtract(const wxDateSpan& other)
539{
540 return *this -= other;
541}
542
543inline wxDateSpan wxDateSpan::Subtract(const wxDateSpan& other) const
544{
545 wxDateSpan ds(*this);
546 ds.Subtract(other);
547 return ds;
548}
549
be4017f8 550#undef MILLISECONDS_PER_DAY
4f6aed9c
VZ
551
552#undef MODIFY_AND_RETURN