]> git.saurik.com Git - wxWidgets.git/blame - include/wx/datetime.inl
Mostly Mingw32/Cygwin corrections
[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// ----------------------------------------------------------------------------
2f02cb89
VZ
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
fcc3d7cb 28bool wxDateTime::IsInStdRange() const
2f02cb89
VZ
29{
30 return m_time >= 0l && (m_time / (long)TIME_T_FACTOR) < LONG_MAX;
31}
32
33/* static */
34wxDateTime wxDateTime::Now()
35{
36 return wxDateTime(GetTimeNow());
37}
38
39wxDateTime& 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
48wxDateTime& wxDateTime::SetToCurrent()
49{
50 return Set(GetTimeNow());
51}
52
53wxDateTime::wxDateTime(time_t timet)
54{
55 Set(timet);
56}
57
58wxDateTime::wxDateTime(const struct tm& tm)
59{
60 Set(tm);
61}
62
63wxDateTime::wxDateTime(const Tm& tm)
64{
65 Set(tm);
66}
67
e6ec579c
VZ
68wxDateTime::wxDateTime(double jdn)
69{
70 Set(jdn);
71}
72
2f02cb89
VZ
73wxDateTime& 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
80wxDateTime::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
88wxDateTime::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
103wxLongLong wxDateTime::GetValue() const
104{
105 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
106
107 return m_time;
108}
109
110time_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
121bool 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
132bool 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
139bool wxDateTime::operator==(const wxDateTime& datetime) const
140{
141 return IsEqualTo(datetime);
142}
143
144bool wxDateTime::operator!=(const wxDateTime& datetime) const
145{
146 return !IsEqualTo(datetime);
147}
148
149bool 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
156bool 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
163bool 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
170bool 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
180wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
181{
182 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
183
184 m_time += diff.GetValue();
185
186 return *this;
187}
188
189wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
190{
191 return Add(diff);
192}
193
194wxDateTime& wxDateTime::Substract(const wxTimeSpan& diff)
195{
196 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
197
198 m_time -= diff.GetValue();
199
200 return *this;
201}
202
203wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
204{
205 return Substract(diff);
206}
207
208wxTimeSpan wxDateTime::Substract(const wxDateTime& datetime) const
209{
210 wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime");
211
212 return wxTimeSpan(datetime.GetValue() - GetValue());
213}
214
215wxTimeSpan wxDateTime::operator-(const wxDateTime& datetime) const
216{
217 return Substract(datetime);
218}
219
220wxDateTime& wxDateTime::Substract(const wxDateSpan& diff)
221{
222 return Add(diff.Negate());
223}
224
225wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
226{
227 return Substract(diff);
228}
229
230wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
231{
232 return Add(diff);
233}
234
fcc3d7cb
VZ
235// ----------------------------------------------------------------------------
236// wxDateTime and timezones
237// ----------------------------------------------------------------------------
238
fcc3d7cb
VZ
239wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz) const
240{
241 return wxDateTime(*this).MakeTimezone(tz);
242}
243
2f02cb89 244// ----------------------------------------------------------------------------
e6ec579c 245// wxTimeSpan construction
2f02cb89
VZ
246// ----------------------------------------------------------------------------
247
fcc3d7cb
VZ
248wxTimeSpan::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
e6ec579c
VZ
260// ----------------------------------------------------------------------------
261// wxTimeSpan accessors
262// ----------------------------------------------------------------------------
263
264wxLongLong wxTimeSpan::GetSeconds() const
265{
266 return m_diff / 1000l;
267}
268
269int wxTimeSpan::GetMinutes() const
270{
271 return (GetSeconds() / 60l).GetLo();
272}
273
274int wxTimeSpan::GetHours() const
275{
276 return GetMinutes() / 60;
277}
278
279int wxTimeSpan::GetDays() const
280{
281 return GetHours() / 24;
282}
283
284int wxTimeSpan::GetWeeks() const
285{
286 return GetDays() / 7;
287}
288
289// ----------------------------------------------------------------------------
290// wxTimeSpan arithmetics
291// ----------------------------------------------------------------------------
292
2f02cb89
VZ
293wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
294{
295 m_diff += diff.GetValue();
296
297 return *this;
298}
299
300wxTimeSpan& wxTimeSpan::Substract(const wxTimeSpan& diff)
301{
302 m_diff -= diff.GetValue();
303
304 return *this;
305}
306
307wxTimeSpan& wxTimeSpan::Multiply(int n)
308{
309 m_diff *= n;
310
311 return *this;
312}
313
314wxTimeSpan wxTimeSpan::operator*(int n) const
315{
316 wxTimeSpan result(*this);
317 result.Multiply(n);
318
319 return result;
320}
321
322wxTimeSpan wxTimeSpan::Abs() const
323{
324 return wxTimeSpan(GetValue().Abs());
325}
326
327bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
328{
329 return GetValue() == ts.GetValue();
330}
331
332bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
333{
334 return GetValue().Abs() > ts.GetValue().Abs();
335}
336
337// ----------------------------------------------------------------------------
338// wxDateSpan
339// ----------------------------------------------------------------------------
340
341wxDateSpan&
342wxDateSpan::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
352wxDateSpan& 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
362wxDateSpan wxDateSpan::Negate() const
363{
364 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
365}
366
367wxDateSpan& 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