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