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