]> git.saurik.com Git - wxWidgets.git/blob - include/wx/datetime.inl
added wxEncodingConverter
[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 wxDateTime dt(*this);
291 return dt.MakeTimezone(tz, noDST);
292 }
293
294 // ----------------------------------------------------------------------------
295 // wxTimeSpan construction
296 // ----------------------------------------------------------------------------
297
298 wxTimeSpan::wxTimeSpan(int hours, int minutes, int seconds, int milliseconds)
299 {
300 // assign first to avoid precision loss
301 m_diff = (long)hours;
302 m_diff *= 60l;
303 m_diff += minutes;
304 m_diff *= 60l;
305 m_diff += seconds;
306 m_diff *= 1000l;
307 m_diff += milliseconds;
308 }
309
310 // ----------------------------------------------------------------------------
311 // wxTimeSpan accessors
312 // ----------------------------------------------------------------------------
313
314 wxLongLong wxTimeSpan::GetSeconds() const
315 {
316 return m_diff / 1000l;
317 }
318
319 int wxTimeSpan::GetMinutes() const
320 {
321 return (GetSeconds() / 60l).GetLo();
322 }
323
324 int wxTimeSpan::GetHours() const
325 {
326 return GetMinutes() / 60;
327 }
328
329 int wxTimeSpan::GetDays() const
330 {
331 return GetHours() / 24;
332 }
333
334 int wxTimeSpan::GetWeeks() const
335 {
336 return GetDays() / 7;
337 }
338
339 // ----------------------------------------------------------------------------
340 // wxTimeSpan arithmetics
341 // ----------------------------------------------------------------------------
342
343 wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const
344 {
345 return wxTimeSpan(m_diff + diff.GetValue());
346 }
347
348 wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
349 {
350 m_diff += diff.GetValue();
351
352 return *this;
353 }
354
355 wxTimeSpan wxTimeSpan::Substract(const wxTimeSpan& diff) const
356 {
357 return wxTimeSpan(m_diff - diff.GetValue());
358 }
359
360 wxTimeSpan& wxTimeSpan::Substract(const wxTimeSpan& diff)
361 {
362 m_diff -= diff.GetValue();
363
364 return *this;
365 }
366
367 wxTimeSpan& wxTimeSpan::Multiply(int n)
368 {
369 m_diff *= (long)n;
370
371 return *this;
372 }
373
374 wxTimeSpan wxTimeSpan::Multiply(int n) const
375 {
376 return wxTimeSpan(m_diff * (long)n);
377 }
378
379 wxTimeSpan wxTimeSpan::Abs() const
380 {
381 return wxTimeSpan(GetValue().Abs());
382 }
383
384 bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
385 {
386 return GetValue() == ts.GetValue();
387 }
388
389 bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
390 {
391 return GetValue().Abs() > ts.GetValue().Abs();
392 }
393
394 // ----------------------------------------------------------------------------
395 // wxDateSpan
396 // ----------------------------------------------------------------------------
397
398 wxDateSpan&
399 wxDateSpan::operator+=(const wxDateSpan& other)
400 {
401 m_years += other.m_years;
402 m_months += other.m_months;
403 m_weeks += other.m_weeks;
404 m_days += other.m_days;
405
406 return *this;
407 }
408
409 wxDateSpan& wxDateSpan::Multiply(int factor)
410 {
411 m_years *= factor;
412 m_months *= factor;
413 m_weeks *= factor;
414 m_days *= factor;
415
416 return *this;
417 }
418
419 wxDateSpan wxDateSpan::Multiply(int factor) const
420 {
421 return wxDateSpan(*this).Multiply(factor);
422 }
423
424 wxDateSpan wxDateSpan::Negate() const
425 {
426 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
427 }
428
429 wxDateSpan& wxDateSpan::Neg()
430 {
431 m_years = -m_years;
432 m_months = -m_months;
433 m_weeks = -m_weeks;
434 m_days = -m_days;
435
436 return *this;
437 }
438
439 #undef MILLISECONDS_PER_DAY