]>
Commit | Line | Data |
---|---|---|
2f02cb89 VZ |
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 | ||
fcc3d7cb | 38 | bool wxDateTime::IsInStdRange() const |
2f02cb89 VZ |
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 | ||
e6ec579c VZ |
78 | wxDateTime::wxDateTime(double jdn) |
79 | { | |
80 | Set(jdn); | |
81 | } | |
82 | ||
2f02cb89 VZ |
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 | ||
fcc3d7cb VZ |
245 | // ---------------------------------------------------------------------------- |
246 | // wxDateTime and timezones | |
247 | // ---------------------------------------------------------------------------- | |
248 | ||
fcc3d7cb VZ |
249 | wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz) const |
250 | { | |
251 | return wxDateTime(*this).MakeTimezone(tz); | |
252 | } | |
253 | ||
2f02cb89 | 254 | // ---------------------------------------------------------------------------- |
e6ec579c | 255 | // wxTimeSpan construction |
2f02cb89 VZ |
256 | // ---------------------------------------------------------------------------- |
257 | ||
fcc3d7cb VZ |
258 | wxTimeSpan::wxTimeSpan(int hours, int minutes, int seconds, int milliseconds) |
259 | { | |
260 | // assign first to avoid precision loss | |
261 | m_diff = hours; | |
262 | m_diff *= 60; | |
263 | m_diff += minutes; | |
264 | m_diff *= 60; | |
265 | m_diff += seconds; | |
266 | m_diff *= 1000; | |
267 | m_diff += milliseconds; | |
268 | } | |
269 | ||
e6ec579c VZ |
270 | // ---------------------------------------------------------------------------- |
271 | // wxTimeSpan accessors | |
272 | // ---------------------------------------------------------------------------- | |
273 | ||
274 | wxLongLong wxTimeSpan::GetSeconds() const | |
275 | { | |
276 | return m_diff / 1000l; | |
277 | } | |
278 | ||
279 | int wxTimeSpan::GetMinutes() const | |
280 | { | |
281 | return (GetSeconds() / 60l).GetLo(); | |
282 | } | |
283 | ||
284 | int wxTimeSpan::GetHours() const | |
285 | { | |
286 | return GetMinutes() / 60; | |
287 | } | |
288 | ||
289 | int wxTimeSpan::GetDays() const | |
290 | { | |
291 | return GetHours() / 24; | |
292 | } | |
293 | ||
294 | int wxTimeSpan::GetWeeks() const | |
295 | { | |
296 | return GetDays() / 7; | |
297 | } | |
298 | ||
299 | // ---------------------------------------------------------------------------- | |
300 | // wxTimeSpan arithmetics | |
301 | // ---------------------------------------------------------------------------- | |
302 | ||
2f02cb89 VZ |
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) | |
311 | { | |
312 | m_diff -= diff.GetValue(); | |
313 | ||
314 | return *this; | |
315 | } | |
316 | ||
317 | wxTimeSpan& wxTimeSpan::Multiply(int n) | |
318 | { | |
319 | m_diff *= n; | |
320 | ||
321 | return *this; | |
322 | } | |
323 | ||
324 | wxTimeSpan wxTimeSpan::operator*(int n) const | |
325 | { | |
326 | wxTimeSpan result(*this); | |
327 | result.Multiply(n); | |
328 | ||
329 | return result; | |
330 | } | |
331 | ||
332 | wxTimeSpan wxTimeSpan::Abs() const | |
333 | { | |
334 | return wxTimeSpan(GetValue().Abs()); | |
335 | } | |
336 | ||
337 | bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const | |
338 | { | |
339 | return GetValue() == ts.GetValue(); | |
340 | } | |
341 | ||
342 | bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const | |
343 | { | |
344 | return GetValue().Abs() > ts.GetValue().Abs(); | |
345 | } | |
346 | ||
347 | // ---------------------------------------------------------------------------- | |
348 | // wxDateSpan | |
349 | // ---------------------------------------------------------------------------- | |
350 | ||
351 | wxDateSpan& | |
352 | wxDateSpan::operator+=(const wxDateSpan& other) | |
353 | { | |
354 | m_years += other.m_years; | |
355 | m_months += other.m_months; | |
356 | m_weeks += other.m_weeks; | |
357 | m_days += other.m_days; | |
358 | ||
359 | return *this; | |
360 | } | |
361 | ||
362 | wxDateSpan& wxDateSpan::operator*=(int factor) | |
363 | { | |
364 | m_years *= m_years; | |
365 | m_months *= m_months; | |
366 | m_weeks *= m_weeks; | |
367 | m_days *= m_days; | |
368 | ||
369 | return *this; | |
370 | } | |
371 | ||
372 | wxDateSpan wxDateSpan::Negate() const | |
373 | { | |
374 | return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days); | |
375 | } | |
376 | ||
377 | wxDateSpan& wxDateSpan::Neg() | |
378 | { | |
379 | m_years = -m_years; | |
380 | m_months = -m_months; | |
381 | m_weeks = -m_weeks; | |
382 | m_days = -m_days; | |
383 | ||
384 | return *this; | |
385 | } | |
386 |