]> git.saurik.com Git - wxWidgets.git/blob - include/wx/datetime.inl
Many bug fixes for display code:
[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 // ----------------------------------------------------------------------------
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
43 bool wxDateTime::IsInStdRange() const
44 {
45 return m_time >= 0l && (m_time / TIME_T_FACTOR) < LONG_MAX;
46 }
47
48 /* static */
49 wxDateTime wxDateTime::Now()
50 {
51 return wxDateTime(*GetTmNow());
52 }
53
54 /* static */
55 wxDateTime 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))
66 wxDateTime& 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
76 wxDateTime& wxDateTime::SetToCurrent()
77 {
78 *this = Now();
79 return *this;
80 }
81
82 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
83 wxDateTime::wxDateTime(time_t timet)
84 {
85 Set(timet);
86 }
87 #endif
88
89 wxDateTime::wxDateTime(const struct tm& tm)
90 {
91 Set(tm);
92 }
93
94 wxDateTime::wxDateTime(const Tm& tm)
95 {
96 Set(tm);
97 }
98
99 wxDateTime::wxDateTime(double jdn)
100 {
101 Set(jdn);
102 }
103
104 wxDateTime& 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
111 wxDateTime::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
119 wxDateTime::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
134 wxLongLong wxDateTime::GetValue() const
135 {
136 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
137
138 return m_time;
139 }
140
141 time_t wxDateTime::GetTicks() const
142 {
143 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
144 if ( !IsInStdRange() )
145 {
146 return (time_t)-1;
147 }
148
149 return (time_t)((m_time / (long)TIME_T_FACTOR).GetLo());
150 }
151
152 bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
153 Month month,
154 int year)
155 {
156 return SetToWeekDay(weekday, -1, month, year);
157 }
158
159 wxDateTime wxDateTime::GetWeekDayInSameWeek(WeekDay weekday) const
160 {
161 MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday) );
162 }
163
164 wxDateTime wxDateTime::GetNextWeekDay(WeekDay weekday) const
165 {
166 MODIFY_AND_RETURN( SetToNextWeekDay(weekday) );
167 }
168
169 wxDateTime wxDateTime::GetPrevWeekDay(WeekDay weekday) const
170 {
171 MODIFY_AND_RETURN( SetToPrevWeekDay(weekday) );
172 }
173
174 wxDateTime 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
184 wxDateTime 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
193 wxDateTime wxDateTime::GetWeek(wxDateTime_t numWeek, WeekDay weekday) const
194 {
195 wxDateTime dt(*this);
196
197 return dt.SetToTheWeek(numWeek, weekday) ? dt : wxInvalidDateTime;
198 }
199
200 wxDateTime wxDateTime::GetLastMonthDay(Month month, int year) const
201 {
202 MODIFY_AND_RETURN( SetToLastMonthDay(month, year) );
203 }
204
205 wxDateTime wxDateTime::GetYearDay(wxDateTime_t yday) const
206 {
207 MODIFY_AND_RETURN( SetToYearDay(yday) );
208 }
209
210 // ----------------------------------------------------------------------------
211 // wxDateTime comparison
212 // ----------------------------------------------------------------------------
213
214 bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
215 {
216 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
217
218 return m_time == datetime.m_time;
219 }
220
221 bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
222 {
223 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
224
225 return m_time < datetime.m_time;
226 }
227
228 bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
229 {
230 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
231
232 return m_time > datetime.m_time;
233 }
234
235 bool 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
242 bool 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
248 bool wxDateTime::IsSameDate(const wxDateTime& dt) const
249 {
250 return (m_time - dt.m_time).Abs() < MILLISECONDS_PER_DAY;
251 }
252
253 bool 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
269 bool wxDateTime::IsEqualUpTo(const wxDateTime& dt, const wxTimeSpan& ts) const
270 {
271 return IsBetween(dt.Substract(ts), dt.Add(ts));
272 }
273
274 // ----------------------------------------------------------------------------
275 // wxDateTime arithmetics
276 // ----------------------------------------------------------------------------
277
278 wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const
279 {
280 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
281
282 return wxDateTime(m_time + diff.GetValue());
283 }
284
285 wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
286 {
287 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
288
289 m_time += diff.GetValue();
290
291 return *this;
292 }
293
294 wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
295 {
296 return Add(diff);
297 }
298
299 wxDateTime wxDateTime::Substract(const wxTimeSpan& diff) const
300 {
301 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
302
303 return wxDateTime(m_time - diff.GetValue());
304 }
305
306 wxDateTime& wxDateTime::Substract(const wxTimeSpan& diff)
307 {
308 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
309
310 m_time -= diff.GetValue();
311
312 return *this;
313 }
314
315 wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
316 {
317 return Substract(diff);
318 }
319
320 wxTimeSpan wxDateTime::Substract(const wxDateTime& datetime) const
321 {
322 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
323
324 return wxTimeSpan(datetime.GetValue() - GetValue());
325 }
326
327 wxDateTime wxDateTime::Add(const wxDateSpan& diff) const
328 {
329 return wxDateTime(*this).Add(diff);
330 }
331
332 wxDateTime& wxDateTime::Substract(const wxDateSpan& diff)
333 {
334 return Add(diff.Negate());
335 }
336
337 wxDateTime wxDateTime::Substract(const wxDateSpan& diff) const
338 {
339 return wxDateTime(*this).Substract(diff);
340 }
341
342 wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
343 {
344 return Substract(diff);
345 }
346
347 wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
348 {
349 return Add(diff);
350 }
351
352 // ----------------------------------------------------------------------------
353 // wxDateTime and timezones
354 // ----------------------------------------------------------------------------
355
356 wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz,
357 bool noDST) const
358 {
359 MODIFY_AND_RETURN( MakeTimezone(tz, noDST) );
360 }
361
362 // ----------------------------------------------------------------------------
363 // wxTimeSpan construction
364 // ----------------------------------------------------------------------------
365
366 wxTimeSpan::wxTimeSpan(long hours, long minutes, long seconds, long milliseconds)
367 {
368 // assign first to avoid precision loss
369 m_diff = hours;
370 m_diff *= 60l;
371 m_diff += minutes;
372 m_diff *= 60l;
373 m_diff += seconds;
374 m_diff *= 1000l;
375 m_diff += milliseconds;
376 }
377
378 // ----------------------------------------------------------------------------
379 // wxTimeSpan accessors
380 // ----------------------------------------------------------------------------
381
382 wxLongLong wxTimeSpan::GetSeconds() const
383 {
384 return m_diff / 1000l;
385 }
386
387 int wxTimeSpan::GetMinutes() const
388 {
389 return (GetSeconds() / 60l).GetLo();
390 }
391
392 int wxTimeSpan::GetHours() const
393 {
394 return GetMinutes() / 60;
395 }
396
397 int wxTimeSpan::GetDays() const
398 {
399 return GetHours() / 24;
400 }
401
402 int wxTimeSpan::GetWeeks() const
403 {
404 return GetDays() / 7;
405 }
406
407 // ----------------------------------------------------------------------------
408 // wxTimeSpan arithmetics
409 // ----------------------------------------------------------------------------
410
411 wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const
412 {
413 return wxTimeSpan(m_diff + diff.GetValue());
414 }
415
416 wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
417 {
418 m_diff += diff.GetValue();
419
420 return *this;
421 }
422
423 wxTimeSpan wxTimeSpan::Substract(const wxTimeSpan& diff) const
424 {
425 return wxTimeSpan(m_diff - diff.GetValue());
426 }
427
428 wxTimeSpan& wxTimeSpan::Substract(const wxTimeSpan& diff)
429 {
430 m_diff -= diff.GetValue();
431
432 return *this;
433 }
434
435 wxTimeSpan& wxTimeSpan::Multiply(int n)
436 {
437 m_diff *= (long)n;
438
439 return *this;
440 }
441
442 wxTimeSpan wxTimeSpan::Multiply(int n) const
443 {
444 return wxTimeSpan(m_diff * (long)n);
445 }
446
447 wxTimeSpan wxTimeSpan::Abs() const
448 {
449 return wxTimeSpan(GetValue().Abs());
450 }
451
452 bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
453 {
454 return GetValue() == ts.GetValue();
455 }
456
457 bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
458 {
459 return GetValue().Abs() > ts.GetValue().Abs();
460 }
461
462 // ----------------------------------------------------------------------------
463 // wxDateSpan
464 // ----------------------------------------------------------------------------
465
466 wxDateSpan&
467 wxDateSpan::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
477 wxDateSpan& wxDateSpan::Multiply(int factor)
478 {
479 m_years *= factor;
480 m_months *= factor;
481 m_weeks *= factor;
482 m_days *= factor;
483
484 return *this;
485 }
486
487 wxDateSpan wxDateSpan::Multiply(int factor) const
488 {
489 return wxDateSpan(*this).Multiply(factor);
490 }
491
492 wxDateSpan wxDateSpan::Negate() const
493 {
494 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
495 }
496
497 wxDateSpan& 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
507 #undef MILLISECONDS_PER_DAY
508
509 #undef MODIFY_AND_RETURN