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