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