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