]> git.saurik.com Git - wxWidgets.git/blame - include/wx/datetime.inl
fixed wxHtmlWindow flicker
[wxWidgets.git] / include / wx / datetime.inl
CommitLineData
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 */
24wxDateTime::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 38bool wxDateTime::IsInStdRange() const
2f02cb89
VZ
39{
40 return m_time >= 0l && (m_time / (long)TIME_T_FACTOR) < LONG_MAX;
41}
42
43/* static */
44wxDateTime wxDateTime::Now()
45{
46 return wxDateTime(GetTimeNow());
47}
48
49wxDateTime& 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
58wxDateTime& wxDateTime::SetToCurrent()
59{
60 return Set(GetTimeNow());
61}
62
63wxDateTime::wxDateTime(time_t timet)
64{
65 Set(timet);
66}
67
68wxDateTime::wxDateTime(const struct tm& tm)
69{
70 Set(tm);
71}
72
73wxDateTime::wxDateTime(const Tm& tm)
74{
75 Set(tm);
76}
77
e6ec579c
VZ
78wxDateTime::wxDateTime(double jdn)
79{
80 Set(jdn);
81}
82
2f02cb89
VZ
83wxDateTime& 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
90wxDateTime::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
98wxDateTime::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
113wxLongLong wxDateTime::GetValue() const
114{
115 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
116
117 return m_time;
118}
119
120time_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
131bool 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
142bool 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
149bool wxDateTime::operator==(const wxDateTime& datetime) const
150{
151 return IsEqualTo(datetime);
152}
153
154bool wxDateTime::operator!=(const wxDateTime& datetime) const
155{
156 return !IsEqualTo(datetime);
157}
158
159bool 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
166bool 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
173bool 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
180bool 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
190wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
191{
192 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
193
194 m_time += diff.GetValue();
195
196 return *this;
197}
198
199wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
200{
201 return Add(diff);
202}
203
204wxDateTime& wxDateTime::Substract(const wxTimeSpan& diff)
205{
206 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
207
208 m_time -= diff.GetValue();
209
210 return *this;
211}
212
213wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
214{
215 return Substract(diff);
216}
217
218wxTimeSpan wxDateTime::Substract(const wxDateTime& datetime) const
219{
220 wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime");
221
222 return wxTimeSpan(datetime.GetValue() - GetValue());
223}
224
225wxTimeSpan wxDateTime::operator-(const wxDateTime& datetime) const
226{
227 return Substract(datetime);
228}
229
230wxDateTime& wxDateTime::Substract(const wxDateSpan& diff)
231{
232 return Add(diff.Negate());
233}
234
235wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
236{
237 return Substract(diff);
238}
239
240wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
241{
242 return Add(diff);
243}
244
fcc3d7cb
VZ
245// ----------------------------------------------------------------------------
246// wxDateTime and timezones
247// ----------------------------------------------------------------------------
248
fcc3d7cb
VZ
249wxDateTime 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
258wxTimeSpan::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
274wxLongLong wxTimeSpan::GetSeconds() const
275{
276 return m_diff / 1000l;
277}
278
279int wxTimeSpan::GetMinutes() const
280{
281 return (GetSeconds() / 60l).GetLo();
282}
283
284int wxTimeSpan::GetHours() const
285{
286 return GetMinutes() / 60;
287}
288
289int wxTimeSpan::GetDays() const
290{
291 return GetHours() / 24;
292}
293
294int wxTimeSpan::GetWeeks() const
295{
296 return GetDays() / 7;
297}
298
299// ----------------------------------------------------------------------------
300// wxTimeSpan arithmetics
301// ----------------------------------------------------------------------------
302
2f02cb89
VZ
303wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
304{
305 m_diff += diff.GetValue();
306
307 return *this;
308}
309
310wxTimeSpan& wxTimeSpan::Substract(const wxTimeSpan& diff)
311{
312 m_diff -= diff.GetValue();
313
314 return *this;
315}
316
317wxTimeSpan& wxTimeSpan::Multiply(int n)
318{
319 m_diff *= n;
320
321 return *this;
322}
323
324wxTimeSpan wxTimeSpan::operator*(int n) const
325{
326 wxTimeSpan result(*this);
327 result.Multiply(n);
328
329 return result;
330}
331
332wxTimeSpan wxTimeSpan::Abs() const
333{
334 return wxTimeSpan(GetValue().Abs());
335}
336
337bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
338{
339 return GetValue() == ts.GetValue();
340}
341
342bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
343{
344 return GetValue().Abs() > ts.GetValue().Abs();
345}
346
347// ----------------------------------------------------------------------------
348// wxDateSpan
349// ----------------------------------------------------------------------------
350
351wxDateSpan&
352wxDateSpan::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
362wxDateSpan& 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
372wxDateSpan wxDateSpan::Negate() const
373{
374 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
375}
376
377wxDateSpan& 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