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