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