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