]> git.saurik.com Git - wxWidgets.git/blob - include/wx/datetime.inl
wxMotif::wxControl() ctor added, now wxCalendarCtrl compiles ok
[wxWidgets.git] / include / wx / datetime.inl
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 #define MILLISECONDS_PER_DAY 86400000l
20
21 // ----------------------------------------------------------------------------
22 // wxDateTime construction
23 // ----------------------------------------------------------------------------
24
25 // only define this once, when included from datetime.cpp
26 #ifdef wxDEFINE_TIME_CONSTANTS
27 const long wxDateTime::TIME_T_FACTOR = 1000l;
28 #endif // wxDEFINE_TIME_CONSTANTS
29
30 bool wxDateTime::IsInStdRange() const
31 {
32 return m_time >= 0l && (m_time / TIME_T_FACTOR) < LONG_MAX;
33 }
34
35 /* static */
36 wxDateTime wxDateTime::Now()
37 {
38 return wxDateTime(*GetTmNow());
39 }
40
41 /* static */
42 wxDateTime wxDateTime::Today()
43 {
44 struct tm *tm = GetTmNow();
45 tm->tm_hour =
46 tm->tm_min =
47 tm->tm_sec = 0;
48
49 return wxDateTime(*tm);
50 }
51
52 wxDateTime& 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
61 wxDateTime& wxDateTime::SetToCurrent()
62 {
63 return *this = Now();
64 }
65
66 wxDateTime::wxDateTime(time_t timet)
67 {
68 Set(timet);
69 }
70
71 wxDateTime::wxDateTime(const struct tm& tm)
72 {
73 Set(tm);
74 }
75
76 wxDateTime::wxDateTime(const Tm& tm)
77 {
78 Set(tm);
79 }
80
81 wxDateTime::wxDateTime(double jdn)
82 {
83 Set(jdn);
84 }
85
86 wxDateTime& 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
93 wxDateTime::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
101 wxDateTime::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
116 wxLongLong wxDateTime::GetValue() const
117 {
118 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
119
120 return m_time;
121 }
122
123 time_t wxDateTime::GetTicks() const
124 {
125 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
126 if ( !IsInStdRange() )
127 {
128 return (time_t)-1;
129 }
130
131 return (time_t)((m_time / (long)TIME_T_FACTOR).GetLo());
132 }
133
134 bool 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
145 bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
146 {
147 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
148
149 return m_time == datetime.m_time;
150 }
151
152 bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
153 {
154 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
155
156 return m_time < datetime.m_time;
157 }
158
159 bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
160 {
161 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
162
163 return m_time > datetime.m_time;
164 }
165
166 bool 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
173 bool 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
179 bool wxDateTime::IsSameDate(const wxDateTime& dt) const
180 {
181 return (m_time - dt.m_time).Abs() < MILLISECONDS_PER_DAY;
182 }
183
184 bool 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
200 bool wxDateTime::IsEqualUpTo(const wxDateTime& dt, const wxTimeSpan& ts) const
201 {
202 return IsBetween(dt.Substract(ts), dt.Add(ts));
203 }
204
205 // ----------------------------------------------------------------------------
206 // wxDateTime arithmetics
207 // ----------------------------------------------------------------------------
208
209 wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const
210 {
211 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
212
213 return wxDateTime(m_time + diff.GetValue());
214 }
215
216 wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
217 {
218 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
219
220 m_time += diff.GetValue();
221
222 return *this;
223 }
224
225 wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
226 {
227 return Add(diff);
228 }
229
230 wxDateTime wxDateTime::Substract(const wxTimeSpan& diff) const
231 {
232 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
233
234 return wxDateTime(m_time - diff.GetValue());
235 }
236
237 wxDateTime& wxDateTime::Substract(const wxTimeSpan& diff)
238 {
239 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
240
241 m_time -= diff.GetValue();
242
243 return *this;
244 }
245
246 wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
247 {
248 return Substract(diff);
249 }
250
251 wxTimeSpan wxDateTime::Substract(const wxDateTime& datetime) const
252 {
253 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
254
255 return wxTimeSpan(datetime.GetValue() - GetValue());
256 }
257
258 wxDateTime wxDateTime::Add(const wxDateSpan& diff) const
259 {
260 return wxDateTime(*this).Add(diff);
261 }
262
263 wxDateTime& wxDateTime::Substract(const wxDateSpan& diff)
264 {
265 return Add(diff.Negate());
266 }
267
268 wxDateTime wxDateTime::Substract(const wxDateSpan& diff) const
269 {
270 return wxDateTime(*this).Substract(diff);
271 }
272
273 wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
274 {
275 return Substract(diff);
276 }
277
278 wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
279 {
280 return Add(diff);
281 }
282
283 // ----------------------------------------------------------------------------
284 // wxDateTime and timezones
285 // ----------------------------------------------------------------------------
286
287 wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz,
288 bool noDST) const
289 {
290 return wxDateTime(*this).MakeTimezone(tz, noDST);
291 }
292
293 // ----------------------------------------------------------------------------
294 // wxTimeSpan construction
295 // ----------------------------------------------------------------------------
296
297 wxTimeSpan::wxTimeSpan(int hours, int minutes, int seconds, int milliseconds)
298 {
299 // assign first to avoid precision loss
300 m_diff = (long)hours;
301 m_diff *= 60l;
302 m_diff += minutes;
303 m_diff *= 60l;
304 m_diff += seconds;
305 m_diff *= 1000l;
306 m_diff += milliseconds;
307 }
308
309 // ----------------------------------------------------------------------------
310 // wxTimeSpan accessors
311 // ----------------------------------------------------------------------------
312
313 wxLongLong wxTimeSpan::GetSeconds() const
314 {
315 return m_diff / 1000l;
316 }
317
318 int wxTimeSpan::GetMinutes() const
319 {
320 return (GetSeconds() / 60l).GetLo();
321 }
322
323 int wxTimeSpan::GetHours() const
324 {
325 return GetMinutes() / 60;
326 }
327
328 int wxTimeSpan::GetDays() const
329 {
330 return GetHours() / 24;
331 }
332
333 int wxTimeSpan::GetWeeks() const
334 {
335 return GetDays() / 7;
336 }
337
338 // ----------------------------------------------------------------------------
339 // wxTimeSpan arithmetics
340 // ----------------------------------------------------------------------------
341
342 wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const
343 {
344 return wxTimeSpan(m_diff + diff.GetValue());
345 }
346
347 wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
348 {
349 m_diff += diff.GetValue();
350
351 return *this;
352 }
353
354 wxTimeSpan wxTimeSpan::Substract(const wxTimeSpan& diff) const
355 {
356 return wxTimeSpan(m_diff - diff.GetValue());
357 }
358
359 wxTimeSpan& wxTimeSpan::Substract(const wxTimeSpan& diff)
360 {
361 m_diff -= diff.GetValue();
362
363 return *this;
364 }
365
366 wxTimeSpan& wxTimeSpan::Multiply(int n)
367 {
368 m_diff *= (long)n;
369
370 return *this;
371 }
372
373 wxTimeSpan wxTimeSpan::Multiply(int n) const
374 {
375 return wxTimeSpan(m_diff * (long)n);
376 }
377
378 wxTimeSpan wxTimeSpan::Abs() const
379 {
380 return wxTimeSpan(GetValue().Abs());
381 }
382
383 bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
384 {
385 return GetValue() == ts.GetValue();
386 }
387
388 bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
389 {
390 return GetValue().Abs() > ts.GetValue().Abs();
391 }
392
393 // ----------------------------------------------------------------------------
394 // wxDateSpan
395 // ----------------------------------------------------------------------------
396
397 wxDateSpan&
398 wxDateSpan::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
408 wxDateSpan& wxDateSpan::Multiply(int factor)
409 {
410 m_years *= factor;
411 m_months *= factor;
412 m_weeks *= factor;
413 m_days *= factor;
414
415 return *this;
416 }
417
418 wxDateSpan wxDateSpan::Multiply(int factor) const
419 {
420 return wxDateSpan(*this).Multiply(factor);
421 }
422
423 wxDateSpan wxDateSpan::Negate() const
424 {
425 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
426 }
427
428 wxDateSpan& 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
438 #undef MILLISECONDS_PER_DAY