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