]> git.saurik.com Git - wxWidgets.git/blob - include/wx/datetime.inl
wxDateTime starting to work, more tests for it and for threads in console sample
[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 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
38 wxDateTime::IsInStdRange() const
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
78 wxDateTime& wxDateTime::Set(const Tm& tm)
79 {
80 wxASSERT_MSG( tm.IsValid(), _T("invalid broken down date/time") );
81
82 return Set(tm.mday, (Month)tm.mon, tm.year, tm.hour, tm.min, tm.sec);
83 }
84
85 wxDateTime::wxDateTime(wxDateTime_t hour,
86 wxDateTime_t minute,
87 wxDateTime_t second,
88 wxDateTime_t millisec)
89 {
90 Set(hour, minute, second, millisec);
91 }
92
93 wxDateTime::wxDateTime(wxDateTime_t day,
94 Month month,
95 int year,
96 wxDateTime_t hour,
97 wxDateTime_t minute,
98 wxDateTime_t second,
99 wxDateTime_t millisec)
100 {
101 Set(day, month, year, hour, minute, second, millisec);
102 }
103
104 // ----------------------------------------------------------------------------
105 // wxDateTime accessors
106 // ----------------------------------------------------------------------------
107
108 wxLongLong wxDateTime::GetValue() const
109 {
110 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
111
112 return m_time;
113 }
114
115 time_t wxDateTime::GetTicks() const
116 {
117 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
118 if ( !IsInStdRange() )
119 {
120 return (time_t)-1;
121 }
122
123 return (time_t)((m_time / (long)TIME_T_FACTOR).GetLo());
124 }
125
126 bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
127 Month month,
128 int year)
129 {
130 return SetToWeekDay(weekday, -1, month, year);
131 }
132
133 // ----------------------------------------------------------------------------
134 // wxDateTime comparison
135 // ----------------------------------------------------------------------------
136
137 bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
138 {
139 wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime");
140
141 return m_time == datetime.m_time;
142 }
143
144 bool wxDateTime::operator==(const wxDateTime& datetime) const
145 {
146 return IsEqualTo(datetime);
147 }
148
149 bool wxDateTime::operator!=(const wxDateTime& datetime) const
150 {
151 return !IsEqualTo(datetime);
152 }
153
154 bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
155 {
156 wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime");
157
158 return m_time < datetime.m_time;
159 }
160
161 bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
162 {
163 wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime");
164
165 return m_time > datetime.m_time;
166 }
167
168 bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1,
169 const wxDateTime& t2) const
170 {
171 // no need for assert, will be checked by the functions we call
172 return IsLaterThan(t1) && IsEarlierThan(t2);
173 }
174
175 bool wxDateTime::IsBetween(const wxDateTime& t1, const wxDateTime& t2) const
176 {
177 // no need for assert, will be checked by the functions we call
178 return IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2);
179 }
180
181 // ----------------------------------------------------------------------------
182 // wxDateTime arithmetics
183 // ----------------------------------------------------------------------------
184
185 wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
186 {
187 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
188
189 m_time += diff.GetValue();
190
191 return *this;
192 }
193
194 wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
195 {
196 return Add(diff);
197 }
198
199 wxDateTime& wxDateTime::Substract(const wxTimeSpan& diff)
200 {
201 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
202
203 m_time -= diff.GetValue();
204
205 return *this;
206 }
207
208 wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
209 {
210 return Substract(diff);
211 }
212
213 wxTimeSpan wxDateTime::Substract(const wxDateTime& datetime) const
214 {
215 wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime");
216
217 return wxTimeSpan(datetime.GetValue() - GetValue());
218 }
219
220 wxTimeSpan wxDateTime::operator-(const wxDateTime& datetime) const
221 {
222 return Substract(datetime);
223 }
224
225 wxDateTime& wxDateTime::Substract(const wxDateSpan& diff)
226 {
227 return Add(diff.Negate());
228 }
229
230 wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
231 {
232 return Substract(diff);
233 }
234
235 wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
236 {
237 return Add(diff);
238 }
239
240 // ----------------------------------------------------------------------------
241 // wxTimeSpan
242 // ----------------------------------------------------------------------------
243
244 wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
245 {
246 m_diff += diff.GetValue();
247
248 return *this;
249 }
250
251 wxTimeSpan& wxTimeSpan::Substract(const wxTimeSpan& diff)
252 {
253 m_diff -= diff.GetValue();
254
255 return *this;
256 }
257
258 wxTimeSpan& wxTimeSpan::Multiply(int n)
259 {
260 m_diff *= n;
261
262 return *this;
263 }
264
265 wxTimeSpan wxTimeSpan::operator*(int n) const
266 {
267 wxTimeSpan result(*this);
268 result.Multiply(n);
269
270 return result;
271 }
272
273 wxTimeSpan wxTimeSpan::Abs() const
274 {
275 return wxTimeSpan(GetValue().Abs());
276 }
277
278 bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
279 {
280 return GetValue() == ts.GetValue();
281 }
282
283 bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
284 {
285 return GetValue().Abs() > ts.GetValue().Abs();
286 }
287
288 // ----------------------------------------------------------------------------
289 // wxDateSpan
290 // ----------------------------------------------------------------------------
291
292 wxDateSpan&
293 wxDateSpan::operator+=(const wxDateSpan& other)
294 {
295 m_years += other.m_years;
296 m_months += other.m_months;
297 m_weeks += other.m_weeks;
298 m_days += other.m_days;
299
300 return *this;
301 }
302
303 wxDateSpan& wxDateSpan::operator*=(int factor)
304 {
305 m_years *= m_years;
306 m_months *= m_months;
307 m_weeks *= m_weeks;
308 m_days *= m_days;
309
310 return *this;
311 }
312
313 wxDateSpan wxDateSpan::Negate() const
314 {
315 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
316 }
317
318 wxDateSpan& wxDateSpan::Neg()
319 {
320 m_years = -m_years;
321 m_months = -m_months;
322 m_weeks = -m_weeks;
323 m_days = -m_days;
324
325 return *this;
326 }
327
328