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