]>
Commit | Line | Data |
---|---|---|
0979c962 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/datetime.h | |
3 | // Purpose: declarations of time/date related classes (wxDateTime, | |
4 | // wxTimeSpan) | |
5 | // Author: Vadim Zeitlin | |
6 | // Modified by: | |
7 | // Created: 10.02.99 | |
0979c962 | 8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> |
65571936 | 9 | // Licence: wxWindows licence |
0979c962 VZ |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
2f02cb89 VZ |
12 | #ifndef _WX_DATETIME_H |
13 | #define _WX_DATETIME_H | |
0979c962 | 14 | |
2b5f62a0 VZ |
15 | #include "wx/defs.h" |
16 | ||
e421922f VZ |
17 | #if wxUSE_DATETIME |
18 | ||
9b4da627 VZ |
19 | #ifdef __WXWINCE__ |
20 | #include "wx/msw/wince/time.h" | |
bd362275 | 21 | #else |
9b4da627 VZ |
22 | #include <time.h> |
23 | #endif // OS | |
5e967044 | 24 | |
b76b015e | 25 | #include <limits.h> // for INT_MIN |
0979c962 VZ |
26 | |
27 | #include "wx/longlong.h" | |
254696bb | 28 | #include "wx/anystr.h" |
0979c962 | 29 | |
b5dbe15d VS |
30 | class WXDLLIMPEXP_FWD_BASE wxDateTime; |
31 | class WXDLLIMPEXP_FWD_BASE wxTimeSpan; | |
32 | class WXDLLIMPEXP_FWD_BASE wxDateSpan; | |
d98a58c5 | 33 | #ifdef __WINDOWS__ |
154014d6 VZ |
34 | struct _SYSTEMTIME; |
35 | #endif | |
0979c962 | 36 | |
df5168c4 | 37 | #include "wx/dynarray.h" |
2f02cb89 | 38 | |
5b781a67 SC |
39 | // not all c-runtimes are based on 1/1/1970 being (time_t) 0 |
40 | // set this to the corresponding value in seconds 1/1/1970 has on your | |
41 | // systems c-runtime | |
42 | ||
92f89387 SC |
43 | #define WX_TIME_BASE_OFFSET 0 |
44 | ||
0979c962 | 45 | /* |
f6bcfd97 | 46 | * TODO |
0979c962 | 47 | * |
299fcbfe | 48 | * + 1. Time zones with minutes (make TimeZone a class) |
4f6aed9c | 49 | * ? 2. getdate() function like under Solaris |
299fcbfe | 50 | * + 3. text conversion for wxDateSpan |
4f6aed9c VZ |
51 | * + 4. pluggable modules for the workdays calculations |
52 | * 5. wxDateTimeHolidayAuthority for Easter and other christian feasts | |
0979c962 VZ |
53 | */ |
54 | ||
55 | /* | |
299fcbfe | 56 | The three (main) classes declared in this header represent: |
0979c962 VZ |
57 | |
58 | 1. An absolute moment in the time (wxDateTime) | |
59 | 2. A difference between two moments in the time, positive or negative | |
60 | (wxTimeSpan) | |
61 | 3. A logical difference between two dates expressed in | |
62 | years/months/weeks/days (wxDateSpan) | |
63 | ||
64 | The following arithmetic operations are permitted (all others are not): | |
65 | ||
66 | addition | |
67 | -------- | |
68 | ||
69 | wxDateTime + wxTimeSpan = wxDateTime | |
70 | wxDateTime + wxDateSpan = wxDateTime | |
71 | wxTimeSpan + wxTimeSpan = wxTimeSpan | |
72 | wxDateSpan + wxDateSpan = wxDateSpan | |
73 | ||
f6bcfd97 | 74 | subtraction |
0979c962 VZ |
75 | ------------ |
76 | wxDateTime - wxDateTime = wxTimeSpan | |
cd0b1709 VZ |
77 | wxDateTime - wxTimeSpan = wxDateTime |
78 | wxDateTime - wxDateSpan = wxDateTime | |
0979c962 VZ |
79 | wxTimeSpan - wxTimeSpan = wxTimeSpan |
80 | wxDateSpan - wxDateSpan = wxDateSpan | |
81 | ||
82 | multiplication | |
83 | -------------- | |
84 | wxTimeSpan * number = wxTimeSpan | |
cd0b1709 | 85 | number * wxTimeSpan = wxTimeSpan |
0979c962 | 86 | wxDateSpan * number = wxDateSpan |
cd0b1709 | 87 | number * wxDateSpan = wxDateSpan |
0979c962 VZ |
88 | |
89 | unitary minus | |
90 | ------------- | |
91 | -wxTimeSpan = wxTimeSpan | |
92 | -wxDateSpan = wxDateSpan | |
cd0b1709 VZ |
93 | |
94 | For each binary operation OP (+, -, *) we have the following operatorOP=() as | |
f6bcfd97 | 95 | a method and the method with a symbolic name OPER (Add, Subtract, Multiply) |
cd0b1709 VZ |
96 | as a synonym for it and another const method with the same name which returns |
97 | the changed copy of the object and operatorOP() as a global function which is | |
98 | implemented in terms of the const version of OPEN. For the unary - we have | |
99 | operator-() as a method, Neg() as synonym for it and Negate() which returns | |
100 | the copy of the object with the changed sign. | |
0979c962 VZ |
101 | */ |
102 | ||
2ef31e80 VZ |
103 | // an invalid/default date time object which may be used as the default |
104 | // argument for arguments of type wxDateTime; it is also returned by all | |
105 | // functions returning wxDateTime on failure (this is why it is also called | |
106 | // wxInvalidDateTime) | |
b5dbe15d | 107 | class WXDLLIMPEXP_FWD_BASE wxDateTime; |
2ef31e80 | 108 | |
a243da29 PC |
109 | extern WXDLLIMPEXP_DATA_BASE(const char) wxDefaultDateTimeFormat[]; |
110 | extern WXDLLIMPEXP_DATA_BASE(const char) wxDefaultTimeSpanFormat[]; | |
bddd7a8d | 111 | extern WXDLLIMPEXP_DATA_BASE(const wxDateTime) wxDefaultDateTime; |
1aaf88d2 | 112 | |
2ef31e80 VZ |
113 | #define wxInvalidDateTime wxDefaultDateTime |
114 | ||
48fd6e9d FM |
115 | |
116 | // ---------------------------------------------------------------------------- | |
117 | // conditional compilation | |
118 | // ---------------------------------------------------------------------------- | |
119 | ||
dd36b5a3 VZ |
120 | // if configure detected strftime(), we have it too |
121 | #ifdef HAVE_STRFTIME | |
122 | #define wxHAS_STRFTIME | |
123 | // suppose everyone else has strftime except Win CE unless VC8 is used | |
124 | #elif !defined(__WXWINCE__) || defined(__VISUALC8__) | |
125 | #define wxHAS_STRFTIME | |
48fd6e9d FM |
126 | #endif |
127 | ||
0979c962 | 128 | // ---------------------------------------------------------------------------- |
cd0b1709 | 129 | // wxDateTime represents an absolute moment in the time |
0979c962 | 130 | // ---------------------------------------------------------------------------- |
b76b015e | 131 | |
bddd7a8d | 132 | class WXDLLIMPEXP_BASE wxDateTime |
0979c962 VZ |
133 | { |
134 | public: | |
135 | // types | |
136 | // ------------------------------------------------------------------------ | |
137 | ||
cd0b1709 VZ |
138 | // a small unsigned integer type for storing things like minutes, |
139 | // seconds &c. It should be at least short (i.e. not char) to contain | |
140 | // the number of milliseconds - it may also be 'int' because there is | |
141 | // no size penalty associated with it in our code, we don't store any | |
142 | // data in this format | |
0979c962 VZ |
143 | typedef unsigned short wxDateTime_t; |
144 | ||
f0f951fa VZ |
145 | // constants |
146 | // ------------------------------------------------------------------------ | |
147 | ||
0979c962 | 148 | // the timezones |
b76b015e | 149 | enum TZ |
0979c962 VZ |
150 | { |
151 | // the time in the current time zone | |
152 | Local, | |
153 | ||
4c51a665 | 154 | // zones from GMT (= Greenwich Mean Time): they're guaranteed to be |
0979c962 VZ |
155 | // consequent numbers, so writing something like `GMT0 + offset' is |
156 | // safe if abs(offset) <= 12 | |
157 | ||
158 | // underscore stands for minus | |
159 | GMT_12, GMT_11, GMT_10, GMT_9, GMT_8, GMT_7, | |
160 | GMT_6, GMT_5, GMT_4, GMT_3, GMT_2, GMT_1, | |
161 | GMT0, | |
162 | GMT1, GMT2, GMT3, GMT4, GMT5, GMT6, | |
34f90a1c | 163 | GMT7, GMT8, GMT9, GMT10, GMT11, GMT12, GMT13, |
0979c962 VZ |
164 | // Note that GMT12 and GMT_12 are not the same: there is a difference |
165 | // of exactly one day between them | |
166 | ||
fcc3d7cb VZ |
167 | // some symbolic names for TZ |
168 | ||
169 | // Europe | |
170 | WET = GMT0, // Western Europe Time | |
171 | WEST = GMT1, // Western Europe Summer Time | |
172 | CET = GMT1, // Central Europe Time | |
173 | CEST = GMT2, // Central Europe Summer Time | |
174 | EET = GMT2, // Eastern Europe Time | |
175 | EEST = GMT3, // Eastern Europe Summer Time | |
176 | MSK = GMT3, // Moscow Time | |
177 | MSD = GMT4, // Moscow Summer Time | |
178 | ||
179 | // US and Canada | |
180 | AST = GMT_4, // Atlantic Standard Time | |
181 | ADT = GMT_3, // Atlantic Daylight Time | |
182 | EST = GMT_5, // Eastern Standard Time | |
183 | EDT = GMT_4, // Eastern Daylight Saving Time | |
184 | CST = GMT_6, // Central Standard Time | |
185 | CDT = GMT_5, // Central Daylight Saving Time | |
186 | MST = GMT_7, // Mountain Standard Time | |
187 | MDT = GMT_6, // Mountain Daylight Saving Time | |
188 | PST = GMT_8, // Pacific Standard Time | |
189 | PDT = GMT_7, // Pacific Daylight Saving Time | |
190 | HST = GMT_10, // Hawaiian Standard Time | |
191 | AKST = GMT_9, // Alaska Standard Time | |
192 | AKDT = GMT_8, // Alaska Daylight Saving Time | |
193 | ||
194 | // Australia | |
195 | ||
196 | A_WST = GMT8, // Western Standard Time | |
34f90a1c | 197 | A_CST = GMT13 + 1, // Central Standard Time (+9.5) |
fcc3d7cb VZ |
198 | A_EST = GMT10, // Eastern Standard Time |
199 | A_ESST = GMT11, // Eastern Summer Time | |
200 | ||
34f90a1c VZ |
201 | // New Zealand |
202 | NZST = GMT12, // Standard Time | |
203 | NZDT = GMT13, // Daylight Saving Time | |
204 | ||
fcc3d7cb VZ |
205 | // TODO add more symbolic timezone names here |
206 | ||
207 | // Universal Coordinated Time = the new and politically correct name | |
208 | // for GMT | |
0979c962 | 209 | UTC = GMT0 |
0979c962 VZ |
210 | }; |
211 | ||
212 | // the calendar systems we know about: notice that it's valid (for | |
213 | // this classes purpose anyhow) to work with any of these calendars | |
214 | // even with the dates before the historical appearance of the | |
215 | // calendar | |
216 | enum Calendar | |
217 | { | |
218 | Gregorian, // current calendar | |
219 | Julian // calendar in use since -45 until the 1582 (or later) | |
220 | ||
221 | // TODO Hebrew, Chinese, Maya, ... (just kidding) (or then may be not?) | |
222 | }; | |
223 | ||
0979c962 VZ |
224 | // the country parameter is used so far for calculating the start and |
225 | // the end of DST period and for deciding whether the date is a work | |
226 | // day or not | |
227 | // | |
228 | // TODO move this to intl.h | |
5e967044 JS |
229 | |
230 | // Required for WinCE | |
231 | #ifdef USA | |
232 | #undef USA | |
233 | #endif | |
234 | ||
0979c962 VZ |
235 | enum Country |
236 | { | |
237 | Country_Unknown, // no special information for this country | |
238 | Country_Default, // set the default country with SetCountry() method | |
239446b4 | 239 | // or use the default country with any other |
0979c962 VZ |
240 | |
241 | // TODO add more countries (for this we must know about DST and/or | |
242 | // holidays for this country) | |
239446b4 VZ |
243 | |
244 | // Western European countries: we assume that they all follow the same | |
245 | // DST rules (true or false?) | |
246 | Country_WesternEurope_Start, | |
247 | Country_EEC = Country_WesternEurope_Start, | |
0979c962 | 248 | France, |
239446b4 VZ |
249 | Germany, |
250 | UK, | |
251 | Country_WesternEurope_End = UK, | |
252 | ||
253 | Russia, | |
0979c962 VZ |
254 | USA |
255 | }; | |
0979c962 VZ |
256 | // symbolic names for the months |
257 | enum Month | |
258 | { | |
259 | Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec, Inv_Month | |
260 | }; | |
261 | ||
262 | // symbolic names for the weekdays | |
263 | enum WeekDay | |
264 | { | |
265 | Sun, Mon, Tue, Wed, Thu, Fri, Sat, Inv_WeekDay | |
266 | }; | |
267 | ||
268 | // invalid value for the year | |
269 | enum Year | |
270 | { | |
b76b015e VZ |
271 | Inv_Year = SHRT_MIN // should hold in wxDateTime_t |
272 | }; | |
273 | ||
f0f951fa VZ |
274 | // flags for GetWeekDayName and GetMonthName |
275 | enum NameFlags | |
276 | { | |
277 | Name_Full = 0x01, // return full name | |
278 | Name_Abbr = 0x02 // return abbreviated name | |
279 | }; | |
280 | ||
9d9b7755 VZ |
281 | // flags for GetWeekOfYear and GetWeekOfMonth |
282 | enum WeekFlags | |
283 | { | |
284 | Default_First, // Sunday_First for US, Monday_First for the rest | |
285 | Monday_First, // week starts with a Monday | |
286 | Sunday_First // week starts with a Sunday | |
287 | }; | |
288 | ||
b76b015e VZ |
289 | // helper classes |
290 | // ------------------------------------------------------------------------ | |
291 | ||
1582a1db | 292 | // a class representing a time zone: basically, this is just an offset |
299fcbfe | 293 | // (in seconds) from GMT |
bddd7a8d | 294 | class WXDLLIMPEXP_BASE TimeZone |
299fcbfe VZ |
295 | { |
296 | public: | |
297 | TimeZone(TZ tz); | |
1cf57808 VZ |
298 | |
299 | // create time zone object with the given offset | |
300 | TimeZone(long offset = 0) { m_offset = offset; } | |
301 | ||
302 | static TimeZone Make(long offset) | |
303 | { | |
304 | TimeZone tz; | |
305 | tz.m_offset = offset; | |
306 | return tz; | |
307 | } | |
299fcbfe | 308 | |
cd0b1709 | 309 | long GetOffset() const { return m_offset; } |
299fcbfe VZ |
310 | |
311 | private: | |
312 | // offset for this timezone from GMT in seconds | |
cd0b1709 | 313 | long m_offset; |
299fcbfe VZ |
314 | }; |
315 | ||
b76b015e VZ |
316 | // standard struct tm is limited to the years from 1900 (because |
317 | // tm_year field is the offset from 1900), so we use our own struct | |
318 | // instead to represent broken down time | |
fcc3d7cb VZ |
319 | // |
320 | // NB: this struct should always be kept normalized (i.e. mon should | |
321 | // be < 12, 1 <= day <= 31 &c), so use AddMonths(), AddDays() | |
322 | // instead of modifying the member fields directly! | |
bddd7a8d | 323 | struct WXDLLIMPEXP_BASE Tm |
b76b015e | 324 | { |
5ed8879e VZ |
325 | wxDateTime_t msec, sec, min, hour, |
326 | mday, // Day of the month in 1..31 range. | |
327 | yday; // Day of the year in 0..365 range. | |
fcc3d7cb VZ |
328 | Month mon; |
329 | int year; | |
b76b015e VZ |
330 | |
331 | // default ctor inits the object to an invalid value | |
332 | Tm(); | |
333 | ||
299fcbfe VZ |
334 | // ctor from struct tm and the timezone |
335 | Tm(const struct tm& tm, const TimeZone& tz); | |
b76b015e VZ |
336 | |
337 | // check that the given date/time is valid (in Gregorian calendar) | |
338 | bool IsValid() const; | |
339 | ||
340 | // get the week day | |
341 | WeekDay GetWeekDay() // not const because wday may be changed | |
342 | { | |
343 | if ( wday == Inv_WeekDay ) | |
344 | ComputeWeekDay(); | |
345 | ||
346 | return (WeekDay)wday; | |
347 | } | |
348 | ||
fcc3d7cb | 349 | // add the given number of months to the date keeping it normalized |
e6ec579c | 350 | void AddMonths(int monDiff); |
fcc3d7cb VZ |
351 | |
352 | // add the given number of months to the date keeping it normalized | |
e6ec579c | 353 | void AddDays(int dayDiff); |
fcc3d7cb | 354 | |
b76b015e VZ |
355 | private: |
356 | // compute the weekday from other fields | |
357 | void ComputeWeekDay(); | |
358 | ||
299fcbfe VZ |
359 | // the timezone we correspond to |
360 | TimeZone m_tz; | |
361 | ||
5ed8879e VZ |
362 | // This value can only be accessed via GetWeekDay() and not directly |
363 | // because it's not always computed when creating this object and may | |
364 | // need to be calculated on demand. | |
365 | wxDateTime_t wday; | |
b76b015e VZ |
366 | }; |
367 | ||
0979c962 VZ |
368 | // static methods |
369 | // ------------------------------------------------------------------------ | |
370 | ||
371 | // set the current country | |
372 | static void SetCountry(Country country); | |
373 | // get the current country | |
239446b4 VZ |
374 | static Country GetCountry(); |
375 | ||
68379eaf | 376 | // return true if the country is a West European one (in practice, |
239446b4 VZ |
377 | // this means that the same DST rules as for EEC apply) |
378 | static bool IsWestEuropeanCountry(Country country = Country_Default); | |
0979c962 VZ |
379 | |
380 | // return the current year | |
381 | static int GetCurrentYear(Calendar cal = Gregorian); | |
382 | ||
383 | // convert the year as returned by wxDateTime::GetYear() to a year | |
384 | // suitable for BC/AD notation. The difference is that BC year 1 | |
385 | // corresponds to the year 0 (while BC year 0 didn't exist) and AD | |
386 | // year N is just year N. | |
387 | static int ConvertYearToBC(int year); | |
388 | ||
389 | // return the current month | |
390 | static Month GetCurrentMonth(Calendar cal = Gregorian); | |
391 | ||
68379eaf | 392 | // returns true if the given year is a leap year in the given calendar |
2f02cb89 VZ |
393 | static bool IsLeapYear(int year = Inv_Year, Calendar cal = Gregorian); |
394 | ||
395 | // get the century (19 for 1999, 20 for 2000 and -5 for 492 BC) | |
a333edbd | 396 | static int GetCentury(int year); |
0979c962 | 397 | |
b76b015e VZ |
398 | // returns the number of days in this year (356 or 355 for Gregorian |
399 | // calendar usually :-) | |
0979c962 VZ |
400 | static wxDateTime_t GetNumberOfDays(int year, Calendar cal = Gregorian); |
401 | ||
402 | // get the number of the days in the given month (default value for | |
403 | // the year means the current one) | |
404 | static wxDateTime_t GetNumberOfDays(Month month, | |
405 | int year = Inv_Year, | |
406 | Calendar cal = Gregorian); | |
407 | ||
e538985e | 408 | |
b76b015e VZ |
409 | // get the full (default) or abbreviated month name in the current |
410 | // locale, returns empty string on error | |
f0f951fa VZ |
411 | static wxString GetMonthName(Month month, |
412 | NameFlags flags = Name_Full); | |
0979c962 | 413 | |
e538985e VZ |
414 | // get the standard English full (default) or abbreviated month name |
415 | static wxString GetEnglishMonthName(Month month, | |
416 | NameFlags flags = Name_Full); | |
417 | ||
b76b015e VZ |
418 | // get the full (default) or abbreviated weekday name in the current |
419 | // locale, returns empty string on error | |
f0f951fa VZ |
420 | static wxString GetWeekDayName(WeekDay weekday, |
421 | NameFlags flags = Name_Full); | |
422 | ||
e538985e VZ |
423 | // get the standard English full (default) or abbreviated weekday name |
424 | static wxString GetEnglishWeekDayName(WeekDay weekday, | |
425 | NameFlags flags = Name_Full); | |
426 | ||
f0f951fa VZ |
427 | // get the AM and PM strings in the current locale (may be empty) |
428 | static void GetAmPmStrings(wxString *am, wxString *pm); | |
0979c962 | 429 | |
68379eaf | 430 | // return true if the given country uses DST for this year |
239446b4 VZ |
431 | static bool IsDSTApplicable(int year = Inv_Year, |
432 | Country country = Country_Default); | |
433 | ||
0979c962 VZ |
434 | // get the beginning of DST for this year, will return invalid object |
435 | // if no DST applicable in this year. The default value of the | |
436 | // parameter means to take the current year. | |
239446b4 VZ |
437 | static wxDateTime GetBeginDST(int year = Inv_Year, |
438 | Country country = Country_Default); | |
0979c962 VZ |
439 | // get the end of DST for this year, will return invalid object |
440 | // if no DST applicable in this year. The default value of the | |
441 | // parameter means to take the current year. | |
239446b4 VZ |
442 | static wxDateTime GetEndDST(int year = Inv_Year, |
443 | Country country = Country_Default); | |
0979c962 | 444 | |
b76b015e VZ |
445 | // return the wxDateTime object for the current time |
446 | static inline wxDateTime Now(); | |
447 | ||
f6bcfd97 BP |
448 | // return the wxDateTime object for the current time with millisecond |
449 | // precision (if available on this platform) | |
450 | static wxDateTime UNow(); | |
451 | ||
cd0b1709 VZ |
452 | // return the wxDateTime object for today midnight: i.e. as Now() but |
453 | // with time set to 0 | |
454 | static inline wxDateTime Today(); | |
455 | ||
0979c962 VZ |
456 | // constructors: you should test whether the constructor succeeded with |
457 | // IsValid() function. The values Inv_Month and Inv_Year for the | |
458 | // parameters mean take current month and/or year values. | |
459 | // ------------------------------------------------------------------------ | |
460 | ||
461 | // default ctor does not initialize the object, use Set()! | |
4bd53224 | 462 | wxDateTime() { m_time = wxLongLong(wxINT32_MIN, 0); } |
0979c962 VZ |
463 | |
464 | // from time_t: seconds since the Epoch 00:00:00 UTC, Jan 1, 1970) | |
16ee521a DW |
465 | #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400)) |
466 | // VA C++ confuses this with wxDateTime(double jdn) thinking it is a duplicate declaration | |
0979c962 | 467 | inline wxDateTime(time_t timet); |
16ee521a | 468 | #endif |
b76b015e | 469 | // from broken down time/date (only for standard Unix range) |
0979c962 | 470 | inline wxDateTime(const struct tm& tm); |
b76b015e VZ |
471 | // from broken down time/date (any range) |
472 | inline wxDateTime(const Tm& tm); | |
0979c962 | 473 | |
e6ec579c VZ |
474 | // from JDN (beware of rounding errors) |
475 | inline wxDateTime(double jdn); | |
476 | ||
0979c962 VZ |
477 | // from separate values for each component, date set to today |
478 | inline wxDateTime(wxDateTime_t hour, | |
479 | wxDateTime_t minute = 0, | |
480 | wxDateTime_t second = 0, | |
481 | wxDateTime_t millisec = 0); | |
482 | // from separate values for each component with explicit date | |
b76b015e | 483 | inline wxDateTime(wxDateTime_t day, // day of the month |
33ac7e6f | 484 | Month month, |
0979c962 VZ |
485 | int year = Inv_Year, // 1999, not 99 please! |
486 | wxDateTime_t hour = 0, | |
487 | wxDateTime_t minute = 0, | |
488 | wxDateTime_t second = 0, | |
489 | wxDateTime_t millisec = 0); | |
d98a58c5 | 490 | #ifdef __WINDOWS__ |
154014d6 VZ |
491 | wxDateTime(const struct _SYSTEMTIME& st) |
492 | { | |
493 | SetFromMSWSysTime(st); | |
494 | } | |
495 | #endif | |
0979c962 VZ |
496 | |
497 | // default copy ctor ok | |
498 | ||
499 | // no dtor | |
500 | ||
501 | // assignment operators and Set() functions: all non const methods return | |
502 | // the reference to this object. IsValid() should be used to test whether | |
503 | // the function succeeded. | |
504 | // ------------------------------------------------------------------------ | |
505 | ||
506 | // set to the current time | |
507 | inline wxDateTime& SetToCurrent(); | |
508 | ||
16ee521a DW |
509 | #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400)) |
510 | // VA C++ confuses this with wxDateTime(double jdn) thinking it is a duplicate declaration | |
0979c962 VZ |
511 | // set to given time_t value |
512 | inline wxDateTime& Set(time_t timet); | |
16ee521a | 513 | #endif |
0979c962 VZ |
514 | |
515 | // set to given broken down time/date | |
516 | wxDateTime& Set(const struct tm& tm); | |
517 | ||
b76b015e VZ |
518 | // set to given broken down time/date |
519 | inline wxDateTime& Set(const Tm& tm); | |
520 | ||
e6ec579c VZ |
521 | // set to given JDN (beware of rounding errors) |
522 | wxDateTime& Set(double jdn); | |
523 | ||
0979c962 VZ |
524 | // set to given time, date = today |
525 | wxDateTime& Set(wxDateTime_t hour, | |
526 | wxDateTime_t minute = 0, | |
527 | wxDateTime_t second = 0, | |
528 | wxDateTime_t millisec = 0); | |
529 | ||
530 | // from separate values for each component with explicit date | |
531 | // (defaults for month and year are the current values) | |
532 | wxDateTime& Set(wxDateTime_t day, | |
33ac7e6f | 533 | Month month, |
0979c962 VZ |
534 | int year = Inv_Year, // 1999, not 99 please! |
535 | wxDateTime_t hour = 0, | |
536 | wxDateTime_t minute = 0, | |
537 | wxDateTime_t second = 0, | |
538 | wxDateTime_t millisec = 0); | |
539 | ||
540 | // resets time to 00:00:00, doesn't change the date | |
541 | wxDateTime& ResetTime(); | |
542 | ||
fb96cf85 VZ |
543 | // get the date part of this object only, i.e. the object which has the |
544 | // same date as this one but time of 00:00:00 | |
545 | wxDateTime GetDateOnly() const; | |
fb96cf85 | 546 | |
0979c962 VZ |
547 | // the following functions don't change the values of the other |
548 | // fields, i.e. SetMinute() won't change either hour or seconds value | |
549 | ||
550 | // set the year | |
551 | wxDateTime& SetYear(int year); | |
552 | // set the month | |
553 | wxDateTime& SetMonth(Month month); | |
554 | // set the day of the month | |
555 | wxDateTime& SetDay(wxDateTime_t day); | |
556 | // set hour | |
557 | wxDateTime& SetHour(wxDateTime_t hour); | |
558 | // set minute | |
559 | wxDateTime& SetMinute(wxDateTime_t minute); | |
560 | // set second | |
561 | wxDateTime& SetSecond(wxDateTime_t second); | |
562 | // set millisecond | |
563 | wxDateTime& SetMillisecond(wxDateTime_t millisecond); | |
564 | ||
565 | // assignment operator from time_t | |
566 | wxDateTime& operator=(time_t timet) { return Set(timet); } | |
567 | ||
568 | // assignment operator from broken down time/date | |
569 | wxDateTime& operator=(const struct tm& tm) { return Set(tm); } | |
570 | ||
b76b015e VZ |
571 | // assignment operator from broken down time/date |
572 | wxDateTime& operator=(const Tm& tm) { return Set(tm); } | |
573 | ||
0979c962 VZ |
574 | // default assignment operator is ok |
575 | ||
576 | // calendar calculations (functions which set the date only leave the time | |
1582a1db | 577 | // unchanged, e.g. don't explicitly zero it): SetXXX() functions modify the |
4f6aed9c | 578 | // object itself, GetXXX() ones return a new object. |
0979c962 VZ |
579 | // ------------------------------------------------------------------------ |
580 | ||
cd0b1709 | 581 | // set to the given week day in the same week as this one |
2b5f62a0 VZ |
582 | wxDateTime& SetToWeekDayInSameWeek(WeekDay weekday, |
583 | WeekFlags flags = Monday_First); | |
584 | inline wxDateTime GetWeekDayInSameWeek(WeekDay weekday, | |
585 | WeekFlags flags = Monday_First) const; | |
cd0b1709 VZ |
586 | |
587 | // set to the next week day following this one | |
588 | wxDateTime& SetToNextWeekDay(WeekDay weekday); | |
4f6aed9c | 589 | inline wxDateTime GetNextWeekDay(WeekDay weekday) const; |
cd0b1709 | 590 | |
2ef31e80 | 591 | // set to the previous week day before this one |
cd0b1709 | 592 | wxDateTime& SetToPrevWeekDay(WeekDay weekday); |
4f6aed9c | 593 | inline wxDateTime GetPrevWeekDay(WeekDay weekday) const; |
cd0b1709 | 594 | |
7929902d | 595 | // set to Nth occurrence of given weekday in the given month of the |
68379eaf | 596 | // given year (time is set to 0), return true on success and false on |
0979c962 VZ |
597 | // failure. n may be positive (1..5) or negative to count from the end |
598 | // of the month (see helper function SetToLastWeekDay()) | |
599 | bool SetToWeekDay(WeekDay weekday, | |
600 | int n = 1, | |
2f02cb89 | 601 | Month month = Inv_Month, |
0979c962 | 602 | int year = Inv_Year); |
4f6aed9c VZ |
603 | inline wxDateTime GetWeekDay(WeekDay weekday, |
604 | int n = 1, | |
605 | Month month = Inv_Month, | |
606 | int year = Inv_Year) const; | |
0979c962 VZ |
607 | |
608 | // sets to the last weekday in the given month, year | |
609 | inline bool SetToLastWeekDay(WeekDay weekday, | |
2f02cb89 | 610 | Month month = Inv_Month, |
0979c962 | 611 | int year = Inv_Year); |
4f6aed9c VZ |
612 | inline wxDateTime GetLastWeekDay(WeekDay weekday, |
613 | Month month = Inv_Month, | |
614 | int year = Inv_Year); | |
0979c962 | 615 | |
ca3e85cf | 616 | #if WXWIN_COMPATIBILITY_2_6 |
0979c962 | 617 | // sets the date to the given day of the given week in the year, |
68379eaf | 618 | // returns true on success and false if given date doesn't exist (e.g. |
0979c962 | 619 | // numWeek is > 53) |
4c27e2fa VZ |
620 | // |
621 | // these functions are badly defined as they're not the reverse of | |
622 | // GetWeekOfYear(), use SetToTheWeekOfYear() instead | |
623 | wxDEPRECATED( bool SetToTheWeek(wxDateTime_t numWeek, | |
624 | WeekDay weekday = Mon, | |
625 | WeekFlags flags = Monday_First) ); | |
626 | wxDEPRECATED( wxDateTime GetWeek(wxDateTime_t numWeek, | |
627 | WeekDay weekday = Mon, | |
628 | WeekFlags flags = Monday_First) const ); | |
ca3e85cf | 629 | #endif // WXWIN_COMPATIBILITY_2_6 |
4c27e2fa VZ |
630 | |
631 | // returns the date corresponding to the given week day of the given | |
632 | // week (in ISO notation) of the specified year | |
633 | static wxDateTime SetToWeekOfYear(int year, | |
634 | wxDateTime_t numWeek, | |
635 | WeekDay weekday = Mon); | |
0979c962 | 636 | |
2f02cb89 VZ |
637 | // sets the date to the last day of the given (or current) month or the |
638 | // given (or current) year | |
639 | wxDateTime& SetToLastMonthDay(Month month = Inv_Month, | |
640 | int year = Inv_Year); | |
4f6aed9c VZ |
641 | inline wxDateTime GetLastMonthDay(Month month = Inv_Month, |
642 | int year = Inv_Year) const; | |
0979c962 | 643 | |
f0f951fa VZ |
644 | // sets to the given year day (1..365 or 366) |
645 | wxDateTime& SetToYearDay(wxDateTime_t yday); | |
4f6aed9c | 646 | inline wxDateTime GetYearDay(wxDateTime_t yday) const; |
f0f951fa | 647 | |
0979c962 VZ |
648 | // The definitions below were taken verbatim from |
649 | // | |
650 | // http://www.capecod.net/~pbaum/date/date0.htm | |
651 | // | |
652 | // (Peter Baum's home page) | |
653 | // | |
654 | // definition: The Julian Day Number, Julian Day, or JD of a | |
655 | // particular instant of time is the number of days and fractions of a | |
656 | // day since 12 hours Universal Time (Greenwich mean noon) on January | |
657 | // 1 of the year -4712, where the year is given in the Julian | |
658 | // proleptic calendar. The idea of using this reference date was | |
659 | // originally proposed by Joseph Scalizer in 1582 to count years but | |
660 | // it was modified by 19th century astronomers to count days. One | |
661 | // could have equivalently defined the reference time to be noon of | |
662 | // November 24, -4713 if were understood that Gregorian calendar rules | |
663 | // were applied. Julian days are Julian Day Numbers and are not to be | |
664 | // confused with Julian dates. | |
665 | // | |
666 | // definition: The Rata Die number is a date specified as the number | |
667 | // of days relative to a base date of December 31 of the year 0. Thus | |
668 | // January 1 of the year 1 is Rata Die day 1. | |
669 | ||
e6ec579c VZ |
670 | // get the Julian Day number (the fractional part specifies the time of |
671 | // the day, related to noon - beware of rounding errors!) | |
672 | double GetJulianDayNumber() const; | |
239446b4 VZ |
673 | double GetJDN() const { return GetJulianDayNumber(); } |
674 | ||
675 | // get the Modified Julian Day number: it is equal to JDN - 2400000.5 | |
676 | // and so integral MJDs correspond to the midnights (and not noons). | |
677 | // MJD 0 is Nov 17, 1858 | |
678 | double GetModifiedJulianDayNumber() const { return GetJDN() - 2400000.5; } | |
679 | double GetMJD() const { return GetModifiedJulianDayNumber(); } | |
0979c962 VZ |
680 | |
681 | // get the Rata Die number | |
e6ec579c | 682 | double GetRataDie() const; |
0979c962 VZ |
683 | |
684 | // TODO algorithms for calculating some important dates, such as | |
685 | // religious holidays (Easter...) or moon/solar eclipses? Some | |
686 | // algorithms can be found in the calendar FAQ | |
687 | ||
d26adb9d VZ |
688 | |
689 | // Timezone stuff: a wxDateTime object constructed using given | |
690 | // day/month/year/hour/min/sec values is interpreted as this moment in | |
691 | // local time. Using the functions below, it may be converted to another | |
692 | // time zone (e.g., the Unix epoch is wxDateTime(1, Jan, 1970).ToGMT()). | |
299fcbfe | 693 | // |
d26adb9d | 694 | // These functions try to handle DST internally, but there is no magical |
41acf5c0 VZ |
695 | // way to know all rules for it in all countries in the world, so if the |
696 | // program can handle it itself (or doesn't want to handle it at all for | |
697 | // whatever reason), the DST handling can be disabled with noDST. | |
b76b015e VZ |
698 | // ------------------------------------------------------------------------ |
699 | ||
299fcbfe | 700 | // transform to any given timezone |
68379eaf WS |
701 | inline wxDateTime ToTimezone(const TimeZone& tz, bool noDST = false) const; |
702 | wxDateTime& MakeTimezone(const TimeZone& tz, bool noDST = false); | |
b76b015e | 703 | |
d26adb9d VZ |
704 | // interpret current value as being in another timezone and transform |
705 | // it to local one | |
706 | inline wxDateTime FromTimezone(const TimeZone& tz, bool noDST = false) const; | |
707 | wxDateTime& MakeFromTimezone(const TimeZone& tz, bool noDST = false); | |
d26adb9d VZ |
708 | |
709 | // transform to/from GMT/UTC | |
710 | wxDateTime ToUTC(bool noDST = false) const { return ToTimezone(UTC, noDST); } | |
711 | wxDateTime& MakeUTC(bool noDST = false) { return MakeTimezone(UTC, noDST); } | |
712 | ||
713 | wxDateTime ToGMT(bool noDST = false) const { return ToUTC(noDST); } | |
714 | wxDateTime& MakeGMT(bool noDST = false) { return MakeUTC(noDST); } | |
715 | ||
d26adb9d VZ |
716 | wxDateTime FromUTC(bool noDST = false) const |
717 | { return FromTimezone(UTC, noDST); } | |
718 | wxDateTime& MakeFromUTC(bool noDST = false) | |
719 | { return MakeFromTimezone(UTC, noDST); } | |
fcc3d7cb | 720 | |
239446b4 VZ |
721 | // is daylight savings time in effect at this moment according to the |
722 | // rules of the specified country? | |
723 | // | |
724 | // Return value is > 0 if DST is in effect, 0 if it is not and -1 if | |
725 | // the information is not available (this is compatible with ANSI C) | |
726 | int IsDST(Country country = Country_Default) const; | |
727 | ||
d26adb9d | 728 | |
0979c962 VZ |
729 | // accessors: many of them take the timezone parameter which indicates the |
730 | // timezone for which to make the calculations and the default value means | |
731 | // to do it for the current timezone of this machine (even if the function | |
732 | // only operates with the date it's necessary because a date may wrap as | |
733 | // result of timezone shift) | |
734 | // ------------------------------------------------------------------------ | |
735 | ||
c3302e7e | 736 | // is the date valid? |
384223b3 | 737 | inline bool IsValid() const { return m_time != wxInvalidDateTime.m_time; } |
0979c962 | 738 | |
299fcbfe VZ |
739 | // get the broken down date/time representation in the given timezone |
740 | // | |
741 | // If you wish to get several time components (day, month and year), | |
742 | // consider getting the whole Tm strcuture first and retrieving the | |
743 | // value from it - this is much more efficient | |
744 | Tm GetTm(const TimeZone& tz = Local) const; | |
b76b015e VZ |
745 | |
746 | // get the number of seconds since the Unix epoch - returns (time_t)-1 | |
747 | // if the value is out of range | |
748 | inline time_t GetTicks() const; | |
749 | ||
a333edbd VZ |
750 | // get the century, same as GetCentury(GetYear()) |
751 | int GetCentury(const TimeZone& tz = Local) const | |
752 | { return GetCentury(GetYear(tz)); } | |
0979c962 | 753 | // get the year (returns Inv_Year if date is invalid) |
16ee521a | 754 | int GetYear(const TimeZone& tz = Local) const |
299fcbfe | 755 | { return GetTm(tz).year; } |
0979c962 | 756 | // get the month (Inv_Month if date is invalid) |
16ee521a | 757 | Month GetMonth(const TimeZone& tz = Local) const |
299fcbfe | 758 | { return (Month)GetTm(tz).mon; } |
0979c962 | 759 | // get the month day (in 1..31 range, 0 if date is invalid) |
16ee521a | 760 | wxDateTime_t GetDay(const TimeZone& tz = Local) const |
299fcbfe | 761 | { return GetTm(tz).mday; } |
0979c962 | 762 | // get the day of the week (Inv_WeekDay if date is invalid) |
16ee521a | 763 | WeekDay GetWeekDay(const TimeZone& tz = Local) const |
299fcbfe | 764 | { return GetTm(tz).GetWeekDay(); } |
0979c962 | 765 | // get the hour of the day |
16ee521a | 766 | wxDateTime_t GetHour(const TimeZone& tz = Local) const |
299fcbfe | 767 | { return GetTm(tz).hour; } |
0979c962 | 768 | // get the minute |
16ee521a | 769 | wxDateTime_t GetMinute(const TimeZone& tz = Local) const |
299fcbfe | 770 | { return GetTm(tz).min; } |
0979c962 | 771 | // get the second |
16ee521a | 772 | wxDateTime_t GetSecond(const TimeZone& tz = Local) const |
299fcbfe | 773 | { return GetTm(tz).sec; } |
0979c962 | 774 | // get milliseconds |
16ee521a | 775 | wxDateTime_t GetMillisecond(const TimeZone& tz = Local) const |
299fcbfe | 776 | { return GetTm(tz).msec; } |
0979c962 VZ |
777 | |
778 | // get the day since the year start (1..366, 0 if date is invalid) | |
299fcbfe | 779 | wxDateTime_t GetDayOfYear(const TimeZone& tz = Local) const; |
cd0b1709 | 780 | // get the week number since the year start (1..52 or 53, 0 if date is |
0979c962 | 781 | // invalid) |
9d9b7755 VZ |
782 | wxDateTime_t GetWeekOfYear(WeekFlags flags = Monday_First, |
783 | const TimeZone& tz = Local) const; | |
cd0b1709 VZ |
784 | // get the week number since the month start (1..5, 0 if date is |
785 | // invalid) | |
9d9b7755 VZ |
786 | wxDateTime_t GetWeekOfMonth(WeekFlags flags = Monday_First, |
787 | const TimeZone& tz = Local) const; | |
0979c962 VZ |
788 | |
789 | // is this date a work day? This depends on a country, of course, | |
790 | // because the holidays are different in different countries | |
4f6aed9c | 791 | bool IsWorkDay(Country country = Country_Default) const; |
0979c962 | 792 | |
2b5f62a0 VZ |
793 | // dos date and time format |
794 | // ------------------------------------------------------------------------ | |
795 | ||
796 | // set from the DOS packed format | |
797 | wxDateTime& SetFromDOS(unsigned long ddt); | |
798 | ||
799 | // pack the date in DOS format | |
800 | unsigned long GetAsDOS() const; | |
801 | ||
154014d6 VZ |
802 | // SYSTEMTIME format |
803 | // ------------------------------------------------------------------------ | |
d98a58c5 | 804 | #ifdef __WINDOWS__ |
154014d6 | 805 | // convert SYSTEMTIME to wxDateTime |
10acc3ef | 806 | wxDateTime& SetFromMSWSysTime(const struct _SYSTEMTIME& st); |
154014d6 VZ |
807 | |
808 | // convert wxDateTime to SYSTEMTIME | |
10acc3ef VZ |
809 | void GetAsMSWSysTime(struct _SYSTEMTIME* st) const; |
810 | ||
811 | // same as above but only take date part into account, time is always zero | |
812 | wxDateTime& SetFromMSWSysDate(const struct _SYSTEMTIME& st); | |
813 | void GetAsMSWSysDate(struct _SYSTEMTIME* st) const; | |
d98a58c5 | 814 | #endif // __WINDOWS__ |
154014d6 | 815 | |
0979c962 VZ |
816 | // comparison (see also functions below for operator versions) |
817 | // ------------------------------------------------------------------------ | |
818 | ||
68379eaf | 819 | // returns true if the two moments are strictly identical |
b76b015e VZ |
820 | inline bool IsEqualTo(const wxDateTime& datetime) const; |
821 | ||
68379eaf | 822 | // returns true if the date is strictly earlier than the given one |
b76b015e | 823 | inline bool IsEarlierThan(const wxDateTime& datetime) const; |
0979c962 | 824 | |
68379eaf | 825 | // returns true if the date is strictly later than the given one |
b76b015e | 826 | inline bool IsLaterThan(const wxDateTime& datetime) const; |
0979c962 | 827 | |
68379eaf | 828 | // returns true if the date is strictly in the given range |
b76b015e VZ |
829 | inline bool IsStrictlyBetween(const wxDateTime& t1, |
830 | const wxDateTime& t2) const; | |
831 | ||
68379eaf | 832 | // returns true if the date is in the given range |
b76b015e | 833 | inline bool IsBetween(const wxDateTime& t1, const wxDateTime& t2) const; |
0979c962 | 834 | |
be4017f8 VZ |
835 | // do these two objects refer to the same date? |
836 | inline bool IsSameDate(const wxDateTime& dt) const; | |
837 | ||
838 | // do these two objects have the same time? | |
839 | inline bool IsSameTime(const wxDateTime& dt) const; | |
840 | ||
841 | // are these two objects equal up to given timespan? | |
842 | inline bool IsEqualUpTo(const wxDateTime& dt, const wxTimeSpan& ts) const; | |
843 | ||
26364344 WS |
844 | inline bool operator<(const wxDateTime& dt) const |
845 | { | |
9a83f860 | 846 | wxASSERT_MSG( IsValid() && dt.IsValid(), wxT("invalid wxDateTime") ); |
26364344 WS |
847 | return GetValue() < dt.GetValue(); |
848 | } | |
849 | ||
850 | inline bool operator<=(const wxDateTime& dt) const | |
851 | { | |
9a83f860 | 852 | wxASSERT_MSG( IsValid() && dt.IsValid(), wxT("invalid wxDateTime") ); |
26364344 WS |
853 | return GetValue() <= dt.GetValue(); |
854 | } | |
855 | ||
856 | inline bool operator>(const wxDateTime& dt) const | |
857 | { | |
9a83f860 | 858 | wxASSERT_MSG( IsValid() && dt.IsValid(), wxT("invalid wxDateTime") ); |
26364344 WS |
859 | return GetValue() > dt.GetValue(); |
860 | } | |
861 | ||
862 | inline bool operator>=(const wxDateTime& dt) const | |
863 | { | |
9a83f860 | 864 | wxASSERT_MSG( IsValid() && dt.IsValid(), wxT("invalid wxDateTime") ); |
26364344 WS |
865 | return GetValue() >= dt.GetValue(); |
866 | } | |
867 | ||
868 | inline bool operator==(const wxDateTime& dt) const | |
869 | { | |
9a83f860 | 870 | wxASSERT_MSG( IsValid() && dt.IsValid(), wxT("invalid wxDateTime") ); |
26364344 WS |
871 | return GetValue() == dt.GetValue(); |
872 | } | |
873 | ||
874 | inline bool operator!=(const wxDateTime& dt) const | |
875 | { | |
9a83f860 | 876 | wxASSERT_MSG( IsValid() && dt.IsValid(), wxT("invalid wxDateTime") ); |
26364344 WS |
877 | return GetValue() != dt.GetValue(); |
878 | } | |
879 | ||
cd0b1709 | 880 | // arithmetics with dates (see also below for more operators) |
0979c962 VZ |
881 | // ------------------------------------------------------------------------ |
882 | ||
cd0b1709 VZ |
883 | // return the sum of the date with a time span (positive or negative) |
884 | inline wxDateTime Add(const wxTimeSpan& diff) const; | |
0979c962 | 885 | // add a time span (positive or negative) |
b76b015e | 886 | inline wxDateTime& Add(const wxTimeSpan& diff); |
0979c962 | 887 | // add a time span (positive or negative) |
b76b015e | 888 | inline wxDateTime& operator+=(const wxTimeSpan& diff); |
26364344 WS |
889 | inline wxDateTime operator+(const wxTimeSpan& ts) const |
890 | { | |
891 | wxDateTime dt(*this); | |
892 | dt.Add(ts); | |
893 | return dt; | |
894 | } | |
0979c962 | 895 | |
cd0b1709 | 896 | // return the difference of the date with a time span |
f6bcfd97 BP |
897 | inline wxDateTime Subtract(const wxTimeSpan& diff) const; |
898 | // subtract a time span (positive or negative) | |
899 | inline wxDateTime& Subtract(const wxTimeSpan& diff); | |
900 | // subtract a time span (positive or negative) | |
0979c962 | 901 | inline wxDateTime& operator-=(const wxTimeSpan& diff); |
26364344 WS |
902 | inline wxDateTime operator-(const wxTimeSpan& ts) const |
903 | { | |
904 | wxDateTime dt(*this); | |
905 | dt.Subtract(ts); | |
906 | return dt; | |
907 | } | |
0979c962 | 908 | |
cd0b1709 VZ |
909 | // return the sum of the date with a date span |
910 | inline wxDateTime Add(const wxDateSpan& diff) const; | |
0979c962 VZ |
911 | // add a date span (positive or negative) |
912 | wxDateTime& Add(const wxDateSpan& diff); | |
913 | // add a date span (positive or negative) | |
b76b015e | 914 | inline wxDateTime& operator+=(const wxDateSpan& diff); |
26364344 WS |
915 | inline wxDateTime operator+(const wxDateSpan& ds) const |
916 | { | |
917 | wxDateTime dt(*this); | |
918 | dt.Add(ds); | |
919 | return dt; | |
920 | } | |
0979c962 | 921 | |
cd0b1709 | 922 | // return the difference of the date with a date span |
f6bcfd97 BP |
923 | inline wxDateTime Subtract(const wxDateSpan& diff) const; |
924 | // subtract a date span (positive or negative) | |
925 | inline wxDateTime& Subtract(const wxDateSpan& diff); | |
926 | // subtract a date span (positive or negative) | |
0979c962 | 927 | inline wxDateTime& operator-=(const wxDateSpan& diff); |
26364344 WS |
928 | inline wxDateTime operator-(const wxDateSpan& ds) const |
929 | { | |
930 | wxDateTime dt(*this); | |
931 | dt.Subtract(ds); | |
932 | return dt; | |
933 | } | |
0979c962 | 934 | |
cd0b1709 | 935 | // return the difference between two dates |
f6bcfd97 | 936 | inline wxTimeSpan Subtract(const wxDateTime& dt) const; |
26364344 | 937 | inline wxTimeSpan operator-(const wxDateTime& dt2) const; |
0979c962 | 938 | |
77dd7daa VZ |
939 | wxDateSpan DiffAsDateSpan(const wxDateTime& dt) const; |
940 | ||
c398434d | 941 | // conversion to/from text |
0979c962 VZ |
942 | // ------------------------------------------------------------------------ |
943 | ||
c398434d VZ |
944 | // all conversions functions return true to indicate whether parsing |
945 | // succeeded or failed and fill in the provided end iterator, which must | |
946 | // not be NULL, with the location of the character where the parsing | |
947 | // stopped (this will be end() of the passed string if everything was | |
948 | // parsed) | |
949 | ||
0979c962 VZ |
950 | // parse a string in RFC 822 format (found e.g. in mail headers and |
951 | // having the form "Wed, 10 Feb 1999 19:07:07 +0100") | |
c398434d VZ |
952 | bool ParseRfc822Date(const wxString& date, |
953 | wxString::const_iterator *end); | |
75a29ef1 | 954 | |
f0f951fa VZ |
955 | // parse a date/time in the given format (see strptime(3)), fill in |
956 | // the missing (in the string) fields with the values of dateDef (by | |
957 | // default, they will not change if they had valid values or will | |
958 | // default to Today() otherwise) | |
c398434d VZ |
959 | bool ParseFormat(const wxString& date, |
960 | const wxString& format, | |
961 | const wxDateTime& dateDef, | |
962 | wxString::const_iterator *end); | |
963 | ||
964 | bool ParseFormat(const wxString& date, | |
965 | const wxString& format, | |
966 | wxString::const_iterator *end) | |
71ebd60b VZ |
967 | { |
968 | return ParseFormat(date, format, wxDefaultDateTime, end); | |
969 | } | |
970 | ||
c398434d VZ |
971 | bool ParseFormat(const wxString& date, |
972 | wxString::const_iterator *end) | |
973 | { | |
974 | return ParseFormat(date, wxDefaultDateTimeFormat, wxDefaultDateTime, end); | |
975 | } | |
71ebd60b | 976 | |
f3f2e255 VZ |
977 | // parse a string containing date, time or both in ISO 8601 format |
978 | // | |
979 | // notice that these functions are new in wx 3.0 and so we don't | |
980 | // provide compatibility overloads for them | |
981 | bool ParseISODate(const wxString& date) | |
982 | { | |
a0bcd61b | 983 | wxString::const_iterator end; |
c398434d | 984 | return ParseFormat(date, wxS("%Y-%m-%d"), &end) && end == date.end(); |
f3f2e255 VZ |
985 | } |
986 | ||
a0bcd61b | 987 | bool ParseISOTime(const wxString& time) |
f3f2e255 | 988 | { |
a0bcd61b | 989 | wxString::const_iterator end; |
c398434d | 990 | return ParseFormat(time, wxS("%H:%M:%S"), &end) && end == time.end(); |
f3f2e255 VZ |
991 | } |
992 | ||
a0bcd61b | 993 | bool ParseISOCombined(const wxString& datetime, char sep = 'T') |
f3f2e255 | 994 | { |
a0bcd61b | 995 | wxString::const_iterator end; |
f3f2e255 | 996 | const wxString fmt = wxS("%Y-%m-%d") + wxString(sep) + wxS("%H:%M:%S"); |
c398434d | 997 | return ParseFormat(datetime, fmt, &end) && end == datetime.end(); |
f3f2e255 VZ |
998 | } |
999 | ||
0979c962 VZ |
1000 | // parse a string containing the date/time in "free" format, this |
1001 | // function will try to make an educated guess at the string contents | |
c398434d VZ |
1002 | bool ParseDateTime(const wxString& datetime, |
1003 | wxString::const_iterator *end); | |
71ebd60b | 1004 | |
cd0b1709 VZ |
1005 | // parse a string containing the date only in "free" format (less |
1006 | // flexible than ParseDateTime) | |
c398434d VZ |
1007 | bool ParseDate(const wxString& date, |
1008 | wxString::const_iterator *end); | |
71ebd60b | 1009 | |
cd0b1709 | 1010 | // parse a string containing the time only in "free" format |
c398434d VZ |
1011 | bool ParseTime(const wxString& time, |
1012 | wxString::const_iterator *end); | |
71ebd60b | 1013 | |
0979c962 VZ |
1014 | |
1015 | // this function accepts strftime()-like format string (default | |
1016 | // argument corresponds to the preferred date and time representation | |
1017 | // for the current locale) and returns the string containing the | |
1018 | // resulting text representation | |
12e82477 | 1019 | wxString Format(const wxString& format = wxDefaultDateTimeFormat, |
299fcbfe | 1020 | const TimeZone& tz = Local) const; |
0979c962 | 1021 | // preferred date representation for the current locale |
f3f2e255 | 1022 | wxString FormatDate() const { return Format(wxS("%x")); } |
0979c962 | 1023 | // preferred time representation for the current locale |
f3f2e255 | 1024 | wxString FormatTime() const { return Format(wxS("%X")); } |
4f6aed9c | 1025 | // returns the string representing the date in ISO 8601 format |
9d9b7755 | 1026 | // (YYYY-MM-DD) |
f3f2e255 | 1027 | wxString FormatISODate() const { return Format(wxS("%Y-%m-%d")); } |
4f6aed9c VZ |
1028 | // returns the string representing the time in ISO 8601 format |
1029 | // (HH:MM:SS) | |
f3f2e255 VZ |
1030 | wxString FormatISOTime() const { return Format(wxS("%H:%M:%S")); } |
1031 | // return the combined date time representation in ISO 8601 format; the | |
1032 | // separator character should be 'T' according to the standard but it | |
1033 | // can also be useful to set it to ' ' | |
1034 | wxString FormatISOCombined(char sep = 'T') const | |
1035 | { return FormatISODate() + sep + FormatISOTime(); } | |
0979c962 | 1036 | |
c398434d VZ |
1037 | |
1038 | // backwards compatible versions of the parsing functions: they return an | |
1039 | // object representing the next character following the date specification | |
1040 | // (i.e. the one where the scan had to stop) or a special NULL-like object | |
1041 | // on failure | |
1042 | // | |
1043 | // they're not deprecated because a lot of existing code uses them and | |
1044 | // there is no particular harm in keeping them but you should still prefer | |
1045 | // the versions above in the new code | |
1046 | wxAnyStrPtr ParseRfc822Date(const wxString& date) | |
1047 | { | |
1048 | wxString::const_iterator end; | |
1049 | return ParseRfc822Date(date, &end) ? wxAnyStrPtr(date, end) | |
1050 | : wxAnyStrPtr(); | |
1051 | } | |
1052 | ||
1053 | wxAnyStrPtr ParseFormat(const wxString& date, | |
1054 | const wxString& format = wxDefaultDateTimeFormat, | |
1055 | const wxDateTime& dateDef = wxDefaultDateTime) | |
1056 | { | |
1057 | wxString::const_iterator end; | |
1058 | return ParseFormat(date, format, dateDef, &end) ? wxAnyStrPtr(date, end) | |
1059 | : wxAnyStrPtr(); | |
1060 | } | |
1061 | ||
1062 | wxAnyStrPtr ParseDateTime(const wxString& datetime) | |
1063 | { | |
1064 | wxString::const_iterator end; | |
1065 | return ParseDateTime(datetime, &end) ? wxAnyStrPtr(datetime, end) | |
1066 | : wxAnyStrPtr(); | |
1067 | } | |
1068 | ||
1069 | wxAnyStrPtr ParseDate(const wxString& date) | |
1070 | { | |
1071 | wxString::const_iterator end; | |
1072 | return ParseDate(date, &end) ? wxAnyStrPtr(date, end) | |
1073 | : wxAnyStrPtr(); | |
1074 | } | |
1075 | ||
1076 | wxAnyStrPtr ParseTime(const wxString& time) | |
1077 | { | |
1078 | wxString::const_iterator end; | |
1079 | return ParseTime(time, &end) ? wxAnyStrPtr(time, end) | |
1080 | : wxAnyStrPtr(); | |
1081 | } | |
1082 | ||
f17ac574 VZ |
1083 | // In addition to wxAnyStrPtr versions above we also must provide the |
1084 | // overloads for C strings as we must return a pointer into the original | |
1085 | // string and not inside a temporary wxString which would have been created | |
1086 | // if the overloads above were used. | |
1087 | // | |
1088 | // And then we also have to provide the overloads for wxCStrData, as usual. | |
ae8079a2 VZ |
1089 | // Unfortunately those ones can't return anything as we don't have any |
1090 | // sufficiently long-lived wxAnyStrPtr to return from them: any temporary | |
1091 | // strings it would point to would be destroyed when this function returns | |
1092 | // making it impossible to dereference the return value. So we just don't | |
1093 | // return anything from here which at least allows to keep compatibility | |
1094 | // with the code not testing the return value. Other uses of this method | |
1095 | // need to be converted to use one of the new bool-returning overloads | |
1096 | // above. | |
1097 | void ParseRfc822Date(const wxCStrData& date) | |
1098 | { ParseRfc822Date(wxString(date)); } | |
f17ac574 VZ |
1099 | const char* ParseRfc822Date(const char* date); |
1100 | const wchar_t* ParseRfc822Date(const wchar_t* date); | |
1101 | ||
ae8079a2 VZ |
1102 | void ParseFormat(const wxCStrData& date, |
1103 | const wxString& format = wxDefaultDateTimeFormat, | |
1104 | const wxDateTime& dateDef = wxDefaultDateTime) | |
1105 | { ParseFormat(wxString(date), format, dateDef); } | |
f17ac574 VZ |
1106 | const char* ParseFormat(const char* date, |
1107 | const wxString& format = wxDefaultDateTimeFormat, | |
1108 | const wxDateTime& dateDef = wxDefaultDateTime); | |
1109 | const wchar_t* ParseFormat(const wchar_t* date, | |
1110 | const wxString& format = wxDefaultDateTimeFormat, | |
1111 | const wxDateTime& dateDef = wxDefaultDateTime); | |
1112 | ||
ae8079a2 VZ |
1113 | void ParseDateTime(const wxCStrData& datetime) |
1114 | { ParseDateTime(wxString(datetime)); } | |
f17ac574 VZ |
1115 | const char* ParseDateTime(const char* datetime); |
1116 | const wchar_t* ParseDateTime(const wchar_t* datetime); | |
1117 | ||
ae8079a2 VZ |
1118 | void ParseDate(const wxCStrData& date) |
1119 | { ParseDate(wxString(date)); } | |
f17ac574 VZ |
1120 | const char* ParseDate(const char* date); |
1121 | const wchar_t* ParseDate(const wchar_t* date); | |
1122 | ||
ae8079a2 VZ |
1123 | void ParseTime(const wxCStrData& time) |
1124 | { ParseTime(wxString(time)); } | |
f17ac574 VZ |
1125 | const char* ParseTime(const char* time); |
1126 | const wchar_t* ParseTime(const wchar_t* time); | |
1127 | ||
1128 | ||
0979c962 VZ |
1129 | // implementation |
1130 | // ------------------------------------------------------------------------ | |
1131 | ||
cd0b1709 VZ |
1132 | // construct from internal representation |
1133 | wxDateTime(const wxLongLong& time) { m_time = time; } | |
1134 | ||
0979c962 VZ |
1135 | // get the internal representation |
1136 | inline wxLongLong GetValue() const; | |
1137 | ||
0979c962 | 1138 | // a helper function to get the current time_t |
d3b9f782 | 1139 | static time_t GetTimeNow() { return time(NULL); } |
0979c962 | 1140 | |
2f02cb89 VZ |
1141 | // another one to get the current time broken down |
1142 | static struct tm *GetTmNow() | |
1143 | { | |
135d1ad0 JS |
1144 | static struct tm l_CurrentTime; |
1145 | return GetTmNow(&l_CurrentTime); | |
2f02cb89 VZ |
1146 | } |
1147 | ||
a452689b SN |
1148 | // get current time using thread-safe function |
1149 | static struct tm *GetTmNow(struct tm *tmstruct); | |
1150 | ||
2f02cb89 | 1151 | private: |
0979c962 | 1152 | // the current country - as it's the same for all program objects (unless |
b76b015e VZ |
1153 | // it runs on a _really_ big cluster system :-), this is a static member: |
1154 | // see SetCountry() and GetCountry() | |
0979c962 VZ |
1155 | static Country ms_country; |
1156 | ||
0979c962 VZ |
1157 | // this constant is used to transform a time_t value to the internal |
1158 | // representation, as time_t is in seconds and we use milliseconds it's | |
1159 | // fixed to 1000 | |
cd0b1709 | 1160 | static const long TIME_T_FACTOR; |
0979c962 | 1161 | |
68379eaf | 1162 | // returns true if we fall in range in which we can use standard ANSI C |
b76b015e | 1163 | // functions |
fcc3d7cb | 1164 | inline bool IsInStdRange() const; |
b76b015e VZ |
1165 | |
1166 | // the internal representation of the time is the amount of milliseconds | |
1167 | // elapsed since the origin which is set by convention to the UNIX/C epoch | |
1168 | // value: the midnight of January 1, 1970 (UTC) | |
1169 | wxLongLong m_time; | |
0979c962 VZ |
1170 | }; |
1171 | ||
1172 | // ---------------------------------------------------------------------------- | |
1173 | // This class contains a difference between 2 wxDateTime values, so it makes | |
f6bcfd97 | 1174 | // sense to add it to wxDateTime and it is the result of subtraction of 2 |
0979c962 VZ |
1175 | // objects of that class. See also wxDateSpan. |
1176 | // ---------------------------------------------------------------------------- | |
b76b015e | 1177 | |
bddd7a8d | 1178 | class WXDLLIMPEXP_BASE wxTimeSpan |
0979c962 VZ |
1179 | { |
1180 | public: | |
1181 | // constructors | |
1182 | // ------------------------------------------------------------------------ | |
1183 | ||
bd35ed32 VZ |
1184 | // return the timespan for the given number of milliseconds |
1185 | static wxTimeSpan Milliseconds(wxLongLong ms) { return wxTimeSpan(0, 0, 0, ms); } | |
1186 | static wxTimeSpan Millisecond() { return Milliseconds(1); } | |
1187 | ||
fcc3d7cb | 1188 | // return the timespan for the given number of seconds |
9c21e80b | 1189 | static wxTimeSpan Seconds(wxLongLong sec) { return wxTimeSpan(0, 0, sec); } |
68ee7c47 | 1190 | static wxTimeSpan Second() { return Seconds(1); } |
fcc3d7cb VZ |
1191 | |
1192 | // return the timespan for the given number of minutes | |
6f2a55e3 | 1193 | static wxTimeSpan Minutes(long min) { return wxTimeSpan(0, min, 0 ); } |
68ee7c47 | 1194 | static wxTimeSpan Minute() { return Minutes(1); } |
fcc3d7cb VZ |
1195 | |
1196 | // return the timespan for the given number of hours | |
6f2a55e3 | 1197 | static wxTimeSpan Hours(long hours) { return wxTimeSpan(hours, 0, 0); } |
68ee7c47 | 1198 | static wxTimeSpan Hour() { return Hours(1); } |
fcc3d7cb | 1199 | |
cd0b1709 | 1200 | // return the timespan for the given number of days |
6f2a55e3 | 1201 | static wxTimeSpan Days(long days) { return Hours(24 * days); } |
68ee7c47 | 1202 | static wxTimeSpan Day() { return Days(1); } |
cd0b1709 VZ |
1203 | |
1204 | // return the timespan for the given number of weeks | |
6f2a55e3 | 1205 | static wxTimeSpan Weeks(long days) { return Days(7 * days); } |
68ee7c47 | 1206 | static wxTimeSpan Week() { return Weeks(1); } |
cd0b1709 | 1207 | |
0979c962 VZ |
1208 | // default ctor constructs the 0 time span |
1209 | wxTimeSpan() { } | |
1210 | ||
1211 | // from separate values for each component, date set to 0 (hours are | |
1212 | // not restricted to 0..24 range, neither are minutes, seconds or | |
1213 | // milliseconds) | |
6f2a55e3 VZ |
1214 | inline wxTimeSpan(long hours, |
1215 | long minutes = 0, | |
9c21e80b VZ |
1216 | wxLongLong seconds = 0, |
1217 | wxLongLong milliseconds = 0); | |
0979c962 | 1218 | |
0979c962 VZ |
1219 | // default copy ctor is ok |
1220 | ||
1221 | // no dtor | |
1222 | ||
cd0b1709 | 1223 | // arithmetics with time spans (see also below for more operators) |
0979c962 VZ |
1224 | // ------------------------------------------------------------------------ |
1225 | ||
cd0b1709 VZ |
1226 | // return the sum of two timespans |
1227 | inline wxTimeSpan Add(const wxTimeSpan& diff) const; | |
0979c962 VZ |
1228 | // add two timespans together |
1229 | inline wxTimeSpan& Add(const wxTimeSpan& diff); | |
1230 | // add two timespans together | |
1231 | wxTimeSpan& operator+=(const wxTimeSpan& diff) { return Add(diff); } | |
26364344 WS |
1232 | inline wxTimeSpan operator+(const wxTimeSpan& ts) const |
1233 | { | |
1234 | return wxTimeSpan(GetValue() + ts.GetValue()); | |
1235 | } | |
0979c962 | 1236 | |
cd0b1709 | 1237 | // return the difference of two timespans |
f6bcfd97 BP |
1238 | inline wxTimeSpan Subtract(const wxTimeSpan& diff) const; |
1239 | // subtract another timespan | |
1240 | inline wxTimeSpan& Subtract(const wxTimeSpan& diff); | |
1241 | // subtract another timespan | |
1242 | wxTimeSpan& operator-=(const wxTimeSpan& diff) { return Subtract(diff); } | |
26364344 WS |
1243 | inline wxTimeSpan operator-(const wxTimeSpan& ts) |
1244 | { | |
1245 | return wxTimeSpan(GetValue() - ts.GetValue()); | |
1246 | } | |
0979c962 | 1247 | |
cd0b1709 VZ |
1248 | // multiply timespan by a scalar |
1249 | inline wxTimeSpan Multiply(int n) const; | |
0979c962 VZ |
1250 | // multiply timespan by a scalar |
1251 | inline wxTimeSpan& Multiply(int n); | |
1252 | // multiply timespan by a scalar | |
1253 | wxTimeSpan& operator*=(int n) { return Multiply(n); } | |
26364344 WS |
1254 | inline wxTimeSpan operator*(int n) const |
1255 | { | |
1256 | return wxTimeSpan(*this).Multiply(n); | |
1257 | } | |
0979c962 | 1258 | |
3103e8a9 | 1259 | // return this timespan with opposite sign |
b76b015e | 1260 | wxTimeSpan Negate() const { return wxTimeSpan(-GetValue()); } |
0979c962 VZ |
1261 | // negate the value of the timespan |
1262 | wxTimeSpan& Neg() { m_diff = -GetValue(); return *this; } | |
1263 | // negate the value of the timespan | |
1264 | wxTimeSpan& operator-() { return Neg(); } | |
1265 | ||
1266 | // return the absolute value of the timespan: does _not_ modify the | |
1267 | // object | |
1268 | inline wxTimeSpan Abs() const; | |
1269 | ||
1270 | // there is intentionally no division because we don't want to | |
1271 | // introduce rounding errors in time calculations | |
1272 | ||
1273 | // comparaison (see also operator versions below) | |
1274 | // ------------------------------------------------------------------------ | |
1275 | ||
1276 | // is the timespan null? | |
b76b015e | 1277 | bool IsNull() const { return m_diff == 0l; } |
0979c962 VZ |
1278 | // returns true if the timespan is null |
1279 | bool operator!() const { return !IsNull(); } | |
1280 | ||
1281 | // is the timespan positive? | |
b76b015e | 1282 | bool IsPositive() const { return m_diff > 0l; } |
0979c962 VZ |
1283 | |
1284 | // is the timespan negative? | |
b76b015e | 1285 | bool IsNegative() const { return m_diff < 0l; } |
0979c962 VZ |
1286 | |
1287 | // are two timespans equal? | |
1288 | inline bool IsEqualTo(const wxTimeSpan& ts) const; | |
1289 | // compare two timestamps: works with the absolute values, i.e. -2 | |
68379eaf | 1290 | // hours is longer than 1 hour. Also, it will return false if the |
0979c962 VZ |
1291 | // timespans are equal in absolute value. |
1292 | inline bool IsLongerThan(const wxTimeSpan& ts) const; | |
1293 | // compare two timestamps: works with the absolute values, i.e. 1 | |
68379eaf | 1294 | // hour is shorter than -2 hours. Also, it will return false if the |
0979c962 | 1295 | // timespans are equal in absolute value. |
f7541d48 | 1296 | bool IsShorterThan(const wxTimeSpan& t) const; |
0979c962 | 1297 | |
26364344 WS |
1298 | inline bool operator<(const wxTimeSpan &ts) const |
1299 | { | |
1300 | return GetValue() < ts.GetValue(); | |
1301 | } | |
1302 | ||
1303 | inline bool operator<=(const wxTimeSpan &ts) const | |
1304 | { | |
1305 | return GetValue() <= ts.GetValue(); | |
1306 | } | |
1307 | ||
1308 | inline bool operator>(const wxTimeSpan &ts) const | |
1309 | { | |
1310 | return GetValue() > ts.GetValue(); | |
1311 | } | |
1312 | ||
1313 | inline bool operator>=(const wxTimeSpan &ts) const | |
1314 | { | |
1315 | return GetValue() >= ts.GetValue(); | |
1316 | } | |
1317 | ||
1318 | inline bool operator==(const wxTimeSpan &ts) const | |
1319 | { | |
1320 | return GetValue() == ts.GetValue(); | |
1321 | } | |
1322 | ||
1323 | inline bool operator!=(const wxTimeSpan &ts) const | |
1324 | { | |
1325 | return GetValue() != ts.GetValue(); | |
1326 | } | |
1327 | ||
e6ec579c | 1328 | // breaking into days, hours, minutes and seconds |
0979c962 VZ |
1329 | // ------------------------------------------------------------------------ |
1330 | ||
0979c962 VZ |
1331 | // get the max number of weeks in this timespan |
1332 | inline int GetWeeks() const; | |
1333 | // get the max number of days in this timespan | |
1334 | inline int GetDays() const; | |
1335 | // get the max number of hours in this timespan | |
1336 | inline int GetHours() const; | |
1337 | // get the max number of minutes in this timespan | |
1338 | inline int GetMinutes() const; | |
1339 | // get the max number of seconds in this timespan | |
e6ec579c | 1340 | inline wxLongLong GetSeconds() const; |
0979c962 | 1341 | // get the number of milliseconds in this timespan |
b76b015e | 1342 | wxLongLong GetMilliseconds() const { return m_diff; } |
0979c962 VZ |
1343 | |
1344 | // conversion to text | |
1345 | // ------------------------------------------------------------------------ | |
1346 | ||
1347 | // this function accepts strftime()-like format string (default | |
1348 | // argument corresponds to the preferred date and time representation | |
1349 | // for the current locale) and returns the string containing the | |
1350 | // resulting text representation. Notice that only some of format | |
1351 | // specifiers valid for wxDateTime are valid for wxTimeSpan: hours, | |
1352 | // minutes and seconds make sense, but not "PM/AM" string for example. | |
12e82477 | 1353 | wxString Format(const wxString& format = wxDefaultTimeSpanFormat) const; |
0979c962 VZ |
1354 | |
1355 | // implementation | |
1356 | // ------------------------------------------------------------------------ | |
1357 | ||
cd0b1709 VZ |
1358 | // construct from internal representation |
1359 | wxTimeSpan(const wxLongLong& diff) { m_diff = diff; } | |
1360 | ||
0979c962 VZ |
1361 | // get the internal representation |
1362 | wxLongLong GetValue() const { return m_diff; } | |
1363 | ||
1364 | private: | |
1365 | // the (signed) time span in milliseconds | |
1366 | wxLongLong m_diff; | |
1367 | }; | |
1368 | ||
1369 | // ---------------------------------------------------------------------------- | |
1370 | // This class is a "logical time span" and is useful for implementing program | |
1371 | // logic for such things as "add one month to the date" which, in general, | |
1372 | // doesn't mean to add 60*60*24*31 seconds to it, but to take the same date | |
1373 | // the next month (to understand that this is indeed different consider adding | |
1374 | // one month to Feb, 15 - we want to get Mar, 15, of course). | |
1375 | // | |
1376 | // When adding a month to the date, all lesser components (days, hours, ...) | |
4f6aed9c VZ |
1377 | // won't be changed unless the resulting date would be invalid: for example, |
1378 | // Jan 31 + 1 month will be Feb 28, not (non existing) Feb 31. | |
1379 | // | |
f6bcfd97 | 1380 | // Because of this feature, adding and subtracting back again the same |
4f6aed9c VZ |
1381 | // wxDateSpan will *not*, in general give back the original date: Feb 28 - 1 |
1382 | // month will be Jan 28, not Jan 31! | |
0979c962 VZ |
1383 | // |
1384 | // wxDateSpan can be either positive or negative. They may be | |
1385 | // multiplied by scalars which multiply all deltas by the scalar: i.e. 2*(1 | |
1386 | // month and 1 day) is 2 months and 2 days. They can be added together and | |
1387 | // with wxDateTime or wxTimeSpan, but the type of result is different for each | |
1388 | // case. | |
1389 | // | |
1390 | // Beware about weeks: if you specify both weeks and days, the total number of | |
1391 | // days added will be 7*weeks + days! See also GetTotalDays() function. | |
1392 | // | |
93401be9 VZ |
1393 | // Equality operators are defined for wxDateSpans. Two datespans are equal if |
1394 | // they both give the same target date when added to *every* source date. | |
1395 | // Thus wxDateSpan::Months(1) is not equal to wxDateSpan::Days(30), because | |
1396 | // they not give the same date when added to 1 Feb. But wxDateSpan::Days(14) is | |
1397 | // equal to wxDateSpan::Weeks(2) | |
1398 | // | |
0979c962 VZ |
1399 | // Finally, notice that for adding hours, minutes &c you don't need this |
1400 | // class: wxTimeSpan will do the job because there are no subtleties | |
1401 | // associated with those. | |
1402 | // ---------------------------------------------------------------------------- | |
b76b015e | 1403 | |
bddd7a8d | 1404 | class WXDLLIMPEXP_BASE wxDateSpan |
0979c962 VZ |
1405 | { |
1406 | public: | |
1407 | // constructors | |
1408 | // ------------------------------------------------------------------------ | |
1409 | ||
1410 | // this many years/months/weeks/days | |
2f02cb89 VZ |
1411 | wxDateSpan(int years = 0, int months = 0, int weeks = 0, int days = 0) |
1412 | { | |
1413 | m_years = years; | |
1414 | m_months = months; | |
1415 | m_weeks = weeks; | |
1416 | m_days = days; | |
1417 | } | |
1418 | ||
1419 | // get an object for the given number of days | |
1420 | static wxDateSpan Days(int days) { return wxDateSpan(0, 0, 0, days); } | |
68ee7c47 | 1421 | static wxDateSpan Day() { return Days(1); } |
2f02cb89 VZ |
1422 | |
1423 | // get an object for the given number of weeks | |
1424 | static wxDateSpan Weeks(int weeks) { return wxDateSpan(0, 0, weeks, 0); } | |
68ee7c47 | 1425 | static wxDateSpan Week() { return Weeks(1); } |
2f02cb89 VZ |
1426 | |
1427 | // get an object for the given number of months | |
1428 | static wxDateSpan Months(int mon) { return wxDateSpan(0, mon, 0, 0); } | |
68ee7c47 | 1429 | static wxDateSpan Month() { return Months(1); } |
2f02cb89 VZ |
1430 | |
1431 | // get an object for the given number of years | |
1432 | static wxDateSpan Years(int years) { return wxDateSpan(years, 0, 0, 0); } | |
68ee7c47 | 1433 | static wxDateSpan Year() { return Years(1); } |
0979c962 VZ |
1434 | |
1435 | // default copy ctor is ok | |
1436 | ||
1437 | // no dtor | |
1438 | ||
1439 | // accessors (all SetXXX() return the (modified) wxDateSpan object) | |
1440 | // ------------------------------------------------------------------------ | |
1441 | ||
1442 | // set number of years | |
b76b015e | 1443 | wxDateSpan& SetYears(int n) { m_years = n; return *this; } |
0979c962 | 1444 | // set number of months |
b76b015e | 1445 | wxDateSpan& SetMonths(int n) { m_months = n; return *this; } |
0979c962 | 1446 | // set number of weeks |
b76b015e | 1447 | wxDateSpan& SetWeeks(int n) { m_weeks = n; return *this; } |
0979c962 | 1448 | // set number of days |
b76b015e | 1449 | wxDateSpan& SetDays(int n) { m_days = n; return *this; } |
0979c962 VZ |
1450 | |
1451 | // get number of years | |
1452 | int GetYears() const { return m_years; } | |
1453 | // get number of months | |
1454 | int GetMonths() const { return m_months; } | |
16630eb7 VZ |
1455 | // returns 12*GetYears() + GetMonths() |
1456 | int GetTotalMonths() const { return 12*m_years + m_months; } | |
0979c962 VZ |
1457 | // get number of weeks |
1458 | int GetWeeks() const { return m_weeks; } | |
1459 | // get number of days | |
1460 | int GetDays() const { return m_days; } | |
1461 | // returns 7*GetWeeks() + GetDays() | |
1462 | int GetTotalDays() const { return 7*m_weeks + m_days; } | |
1463 | ||
cd0b1709 | 1464 | // arithmetics with date spans (see also below for more operators) |
0979c962 VZ |
1465 | // ------------------------------------------------------------------------ |
1466 | ||
cd0b1709 VZ |
1467 | // return sum of two date spans |
1468 | inline wxDateSpan Add(const wxDateSpan& other) const; | |
0979c962 VZ |
1469 | // add another wxDateSpan to us |
1470 | inline wxDateSpan& Add(const wxDateSpan& other); | |
1471 | // add another wxDateSpan to us | |
1472 | inline wxDateSpan& operator+=(const wxDateSpan& other); | |
26364344 WS |
1473 | inline wxDateSpan operator+(const wxDateSpan& ds) const |
1474 | { | |
1475 | return wxDateSpan(GetYears() + ds.GetYears(), | |
1476 | GetMonths() + ds.GetMonths(), | |
1477 | GetWeeks() + ds.GetWeeks(), | |
1478 | GetDays() + ds.GetDays()); | |
1479 | } | |
0979c962 | 1480 | |
cd0b1709 | 1481 | // return difference of two date spans |
f6bcfd97 BP |
1482 | inline wxDateSpan Subtract(const wxDateSpan& other) const; |
1483 | // subtract another wxDateSpan from us | |
1484 | inline wxDateSpan& Subtract(const wxDateSpan& other); | |
1485 | // subtract another wxDateSpan from us | |
0979c962 | 1486 | inline wxDateSpan& operator-=(const wxDateSpan& other); |
26364344 WS |
1487 | inline wxDateSpan operator-(const wxDateSpan& ds) const |
1488 | { | |
1489 | return wxDateSpan(GetYears() - ds.GetYears(), | |
1490 | GetMonths() - ds.GetMonths(), | |
1491 | GetWeeks() - ds.GetWeeks(), | |
1492 | GetDays() - ds.GetDays()); | |
1493 | } | |
0979c962 VZ |
1494 | |
1495 | // return a copy of this time span with changed sign | |
1496 | inline wxDateSpan Negate() const; | |
1497 | // inverse the sign of this timespan | |
1498 | inline wxDateSpan& Neg(); | |
1499 | // inverse the sign of this timespan | |
1500 | wxDateSpan& operator-() { return Neg(); } | |
1501 | ||
cd0b1709 VZ |
1502 | // return the date span proportional to this one with given factor |
1503 | inline wxDateSpan Multiply(int factor) const; | |
0979c962 | 1504 | // multiply all components by a (signed) number |
cd0b1709 VZ |
1505 | inline wxDateSpan& Multiply(int factor); |
1506 | // multiply all components by a (signed) number | |
1507 | inline wxDateSpan& operator*=(int factor) { return Multiply(factor); } | |
26364344 WS |
1508 | inline wxDateSpan operator*(int n) const |
1509 | { | |
1510 | return wxDateSpan(*this).Multiply(n); | |
1511 | } | |
1512 | ||
1513 | // ds1 == d2 if and only if for every wxDateTime t t + ds1 == t + ds2 | |
1514 | inline bool operator==(const wxDateSpan& ds) const | |
1515 | { | |
1516 | return GetYears() == ds.GetYears() && | |
1517 | GetMonths() == ds.GetMonths() && | |
1518 | GetTotalDays() == ds.GetTotalDays(); | |
1519 | } | |
1520 | ||
1521 | inline bool operator!=(const wxDateSpan& ds) const | |
1522 | { | |
1523 | return !(*this == ds); | |
1524 | } | |
0979c962 VZ |
1525 | |
1526 | private: | |
1527 | int m_years, | |
1528 | m_months, | |
1529 | m_weeks, | |
1530 | m_days; | |
1531 | }; | |
1532 | ||
4f6aed9c VZ |
1533 | // ---------------------------------------------------------------------------- |
1534 | // wxDateTimeArray: array of dates. | |
1535 | // ---------------------------------------------------------------------------- | |
1536 | ||
bb24c68f | 1537 | WX_DECLARE_USER_EXPORTED_OBJARRAY(wxDateTime, wxDateTimeArray, WXDLLIMPEXP_BASE); |
4f6aed9c VZ |
1538 | |
1539 | // ---------------------------------------------------------------------------- | |
1540 | // wxDateTimeHolidayAuthority: an object of this class will decide whether a | |
1541 | // given date is a holiday and is used by all functions working with "work | |
1542 | // days". | |
1543 | // | |
1544 | // NB: the base class is an ABC, derived classes must implement the pure | |
1545 | // virtual methods to work with the holidays they correspond to. | |
1546 | // ---------------------------------------------------------------------------- | |
1547 | ||
b5dbe15d | 1548 | class WXDLLIMPEXP_FWD_BASE wxDateTimeHolidayAuthority; |
d5d29b8a | 1549 | WX_DEFINE_USER_EXPORTED_ARRAY_PTR(wxDateTimeHolidayAuthority *, |
bb24c68f | 1550 | wxHolidayAuthoritiesArray, |
fd497ede | 1551 | class WXDLLIMPEXP_BASE); |
4f6aed9c | 1552 | |
f6081a04 | 1553 | class wxDateTimeHolidaysModule; |
bddd7a8d | 1554 | class WXDLLIMPEXP_BASE wxDateTimeHolidayAuthority |
4f6aed9c | 1555 | { |
f6bcfd97 | 1556 | friend class wxDateTimeHolidaysModule; |
4f6aed9c | 1557 | public: |
68379eaf | 1558 | // returns true if the given date is a holiday |
4f6aed9c VZ |
1559 | static bool IsHoliday(const wxDateTime& dt); |
1560 | ||
1561 | // fills the provided array with all holidays in the given range, returns | |
1562 | // the number of them | |
1563 | static size_t GetHolidaysInRange(const wxDateTime& dtStart, | |
1564 | const wxDateTime& dtEnd, | |
1565 | wxDateTimeArray& holidays); | |
1566 | ||
1567 | // clear the list of holiday authorities | |
1568 | static void ClearAllAuthorities(); | |
1569 | ||
1570 | // add a new holiday authority (the pointer will be deleted by | |
1571 | // wxDateTimeHolidayAuthority) | |
1572 | static void AddAuthority(wxDateTimeHolidayAuthority *auth); | |
1573 | ||
30e671a5 | 1574 | // the base class must have a virtual dtor |
5e233068 | 1575 | virtual ~wxDateTimeHolidayAuthority(); |
30e671a5 | 1576 | |
4f6aed9c VZ |
1577 | protected: |
1578 | // this function is called to determine whether a given day is a holiday | |
1579 | virtual bool DoIsHoliday(const wxDateTime& dt) const = 0; | |
1580 | ||
1581 | // this function should fill the array with all holidays between the two | |
1582 | // given dates - it is implemented in the base class, but in a very | |
1583 | // inefficient way (it just iterates over all days and uses IsHoliday() for | |
1584 | // each of them), so it must be overridden in the derived class where the | |
1585 | // base class version may be explicitly used if needed | |
1586 | // | |
1587 | // returns the number of holidays in the given range and fills holidays | |
1588 | // array | |
1589 | virtual size_t DoGetHolidaysInRange(const wxDateTime& dtStart, | |
1590 | const wxDateTime& dtEnd, | |
1591 | wxDateTimeArray& holidays) const = 0; | |
1592 | ||
1593 | private: | |
1594 | // all holiday authorities | |
1595 | static wxHolidayAuthoritiesArray ms_authorities; | |
1596 | }; | |
1597 | ||
1598 | // the holidays for this class are all Saturdays and Sundays | |
bddd7a8d | 1599 | class WXDLLIMPEXP_BASE wxDateTimeWorkDays : public wxDateTimeHolidayAuthority |
4f6aed9c VZ |
1600 | { |
1601 | protected: | |
1602 | virtual bool DoIsHoliday(const wxDateTime& dt) const; | |
1603 | virtual size_t DoGetHolidaysInRange(const wxDateTime& dtStart, | |
1604 | const wxDateTime& dtEnd, | |
1605 | wxDateTimeArray& holidays) const; | |
1606 | }; | |
0979c962 | 1607 | |
2f02cb89 VZ |
1608 | // ============================================================================ |
1609 | // inline functions implementation | |
1610 | // ============================================================================ | |
1611 | ||
df5168c4 MB |
1612 | // ---------------------------------------------------------------------------- |
1613 | // private macros | |
1614 | // ---------------------------------------------------------------------------- | |
1615 | ||
1616 | #define MILLISECONDS_PER_DAY 86400000l | |
1617 | ||
1618 | // some broken compilers (HP-UX CC) refuse to compile the "normal" version, but | |
1619 | // using a temp variable always might prevent other compilers from optimising | |
1620 | // it away - hence use of this ugly macro | |
1621 | #ifndef __HPUX__ | |
1622 | #define MODIFY_AND_RETURN(op) return wxDateTime(*this).op | |
1623 | #else | |
1624 | #define MODIFY_AND_RETURN(op) wxDateTime dt(*this); dt.op; return dt | |
1625 | #endif | |
1626 | ||
1627 | // ---------------------------------------------------------------------------- | |
1628 | // wxDateTime construction | |
1629 | // ---------------------------------------------------------------------------- | |
1630 | ||
1631 | inline bool wxDateTime::IsInStdRange() const | |
1632 | { | |
57563c71 VZ |
1633 | // currently we don't know what is the real type of time_t so prefer to err |
1634 | // on the safe side and limit it to 32 bit values which is safe everywhere | |
1635 | return m_time >= 0l && (m_time / TIME_T_FACTOR) < wxINT32_MAX; | |
df5168c4 | 1636 | } |
2f02cb89 | 1637 | |
df5168c4 MB |
1638 | /* static */ |
1639 | inline wxDateTime wxDateTime::Now() | |
1640 | { | |
a452689b SN |
1641 | struct tm tmstruct; |
1642 | return wxDateTime(*GetTmNow(&tmstruct)); | |
df5168c4 MB |
1643 | } |
1644 | ||
1645 | /* static */ | |
1646 | inline wxDateTime wxDateTime::Today() | |
1647 | { | |
cb1ef833 VZ |
1648 | wxDateTime dt(Now()); |
1649 | dt.ResetTime(); | |
df5168c4 | 1650 | |
cb1ef833 | 1651 | return dt; |
df5168c4 MB |
1652 | } |
1653 | ||
1654 | #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400)) | |
1655 | inline wxDateTime& wxDateTime::Set(time_t timet) | |
1656 | { | |
d2a02746 VZ |
1657 | if ( timet == (time_t)-1 ) |
1658 | { | |
1659 | m_time = wxInvalidDateTime.m_time; | |
1660 | } | |
1661 | else | |
1662 | { | |
1663 | // assign first to avoid long multiplication overflow! | |
1664 | m_time = timet - WX_TIME_BASE_OFFSET; | |
1665 | m_time *= TIME_T_FACTOR; | |
1666 | } | |
df5168c4 MB |
1667 | |
1668 | return *this; | |
1669 | } | |
2f02cb89 VZ |
1670 | #endif |
1671 | ||
df5168c4 MB |
1672 | inline wxDateTime& wxDateTime::SetToCurrent() |
1673 | { | |
1674 | *this = Now(); | |
1675 | return *this; | |
1676 | } | |
1677 | ||
1678 | #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400)) | |
1679 | inline wxDateTime::wxDateTime(time_t timet) | |
1680 | { | |
1681 | Set(timet); | |
1682 | } | |
dc84437e | 1683 | #endif |
2f02cb89 | 1684 | |
df5168c4 MB |
1685 | inline wxDateTime::wxDateTime(const struct tm& tm) |
1686 | { | |
1687 | Set(tm); | |
1688 | } | |
1689 | ||
1690 | inline wxDateTime::wxDateTime(const Tm& tm) | |
1691 | { | |
1692 | Set(tm); | |
1693 | } | |
1694 | ||
1695 | inline wxDateTime::wxDateTime(double jdn) | |
1696 | { | |
1697 | Set(jdn); | |
1698 | } | |
1699 | ||
1700 | inline wxDateTime& wxDateTime::Set(const Tm& tm) | |
1701 | { | |
9a83f860 | 1702 | wxASSERT_MSG( tm.IsValid(), wxT("invalid broken down date/time") ); |
df5168c4 | 1703 | |
271f6072 VZ |
1704 | return Set(tm.mday, (Month)tm.mon, tm.year, |
1705 | tm.hour, tm.min, tm.sec, tm.msec); | |
df5168c4 MB |
1706 | } |
1707 | ||
1708 | inline wxDateTime::wxDateTime(wxDateTime_t hour, | |
1709 | wxDateTime_t minute, | |
1710 | wxDateTime_t second, | |
1711 | wxDateTime_t millisec) | |
1712 | { | |
1713 | Set(hour, minute, second, millisec); | |
1714 | } | |
1715 | ||
1716 | inline wxDateTime::wxDateTime(wxDateTime_t day, | |
1717 | Month month, | |
1718 | int year, | |
1719 | wxDateTime_t hour, | |
1720 | wxDateTime_t minute, | |
1721 | wxDateTime_t second, | |
1722 | wxDateTime_t millisec) | |
1723 | { | |
1724 | Set(day, month, year, hour, minute, second, millisec); | |
1725 | } | |
1726 | ||
1727 | // ---------------------------------------------------------------------------- | |
1728 | // wxDateTime accessors | |
1729 | // ---------------------------------------------------------------------------- | |
1730 | ||
1731 | inline wxLongLong wxDateTime::GetValue() const | |
1732 | { | |
9a83f860 | 1733 | wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime")); |
df5168c4 MB |
1734 | |
1735 | return m_time; | |
1736 | } | |
1737 | ||
1738 | inline time_t wxDateTime::GetTicks() const | |
1739 | { | |
9a83f860 | 1740 | wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime")); |
df5168c4 MB |
1741 | if ( !IsInStdRange() ) |
1742 | { | |
1743 | return (time_t)-1; | |
1744 | } | |
1745 | ||
d3c58c7e | 1746 | return (time_t)((m_time / (long)TIME_T_FACTOR).ToLong()) + WX_TIME_BASE_OFFSET; |
df5168c4 MB |
1747 | } |
1748 | ||
1749 | inline bool wxDateTime::SetToLastWeekDay(WeekDay weekday, | |
1750 | Month month, | |
1751 | int year) | |
1752 | { | |
1753 | return SetToWeekDay(weekday, -1, month, year); | |
1754 | } | |
1755 | ||
fc7a2a60 VZ |
1756 | inline wxDateTime |
1757 | wxDateTime::GetWeekDayInSameWeek(WeekDay weekday, | |
1758 | WeekFlags WXUNUSED(flags)) const | |
df5168c4 MB |
1759 | { |
1760 | MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday) ); | |
1761 | } | |
1762 | ||
1763 | inline wxDateTime wxDateTime::GetNextWeekDay(WeekDay weekday) const | |
1764 | { | |
1765 | MODIFY_AND_RETURN( SetToNextWeekDay(weekday) ); | |
1766 | } | |
1767 | ||
1768 | inline wxDateTime wxDateTime::GetPrevWeekDay(WeekDay weekday) const | |
1769 | { | |
1770 | MODIFY_AND_RETURN( SetToPrevWeekDay(weekday) ); | |
1771 | } | |
1772 | ||
1773 | inline wxDateTime wxDateTime::GetWeekDay(WeekDay weekday, | |
1774 | int n, | |
1775 | Month month, | |
1776 | int year) const | |
1777 | { | |
1778 | wxDateTime dt(*this); | |
1779 | ||
1780 | return dt.SetToWeekDay(weekday, n, month, year) ? dt : wxInvalidDateTime; | |
1781 | } | |
1782 | ||
1783 | inline wxDateTime wxDateTime::GetLastWeekDay(WeekDay weekday, | |
1784 | Month month, | |
1785 | int year) | |
1786 | { | |
1787 | wxDateTime dt(*this); | |
1788 | ||
1789 | return dt.SetToLastWeekDay(weekday, month, year) ? dt : wxInvalidDateTime; | |
1790 | } | |
1791 | ||
df5168c4 MB |
1792 | inline wxDateTime wxDateTime::GetLastMonthDay(Month month, int year) const |
1793 | { | |
1794 | MODIFY_AND_RETURN( SetToLastMonthDay(month, year) ); | |
1795 | } | |
1796 | ||
1797 | inline wxDateTime wxDateTime::GetYearDay(wxDateTime_t yday) const | |
1798 | { | |
1799 | MODIFY_AND_RETURN( SetToYearDay(yday) ); | |
1800 | } | |
1801 | ||
1802 | // ---------------------------------------------------------------------------- | |
1803 | // wxDateTime comparison | |
1804 | // ---------------------------------------------------------------------------- | |
1805 | ||
1806 | inline bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const | |
1807 | { | |
9a83f860 | 1808 | wxASSERT_MSG( IsValid() && datetime.IsValid(), wxT("invalid wxDateTime")); |
df5168c4 MB |
1809 | |
1810 | return m_time == datetime.m_time; | |
1811 | } | |
1812 | ||
1813 | inline bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const | |
1814 | { | |
9a83f860 | 1815 | wxASSERT_MSG( IsValid() && datetime.IsValid(), wxT("invalid wxDateTime")); |
df5168c4 MB |
1816 | |
1817 | return m_time < datetime.m_time; | |
1818 | } | |
1819 | ||
1820 | inline bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const | |
1821 | { | |
9a83f860 | 1822 | wxASSERT_MSG( IsValid() && datetime.IsValid(), wxT("invalid wxDateTime")); |
df5168c4 MB |
1823 | |
1824 | return m_time > datetime.m_time; | |
1825 | } | |
1826 | ||
1827 | inline bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1, | |
1828 | const wxDateTime& t2) const | |
1829 | { | |
1830 | // no need for assert, will be checked by the functions we call | |
1831 | return IsLaterThan(t1) && IsEarlierThan(t2); | |
1832 | } | |
1833 | ||
1834 | inline bool wxDateTime::IsBetween(const wxDateTime& t1, | |
1835 | const wxDateTime& t2) const | |
1836 | { | |
1837 | // no need for assert, will be checked by the functions we call | |
1838 | return IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2); | |
1839 | } | |
1840 | ||
1841 | inline bool wxDateTime::IsSameDate(const wxDateTime& dt) const | |
1842 | { | |
1843 | Tm tm1 = GetTm(), | |
1844 | tm2 = dt.GetTm(); | |
1845 | ||
1846 | return tm1.year == tm2.year && | |
1847 | tm1.mon == tm2.mon && | |
1848 | tm1.mday == tm2.mday; | |
1849 | } | |
1850 | ||
1851 | inline bool wxDateTime::IsSameTime(const wxDateTime& dt) const | |
1852 | { | |
1853 | // notice that we can't do something like this: | |
1854 | // | |
1855 | // m_time % MILLISECONDS_PER_DAY == dt.m_time % MILLISECONDS_PER_DAY | |
1856 | // | |
1857 | // because we have also to deal with (possibly) different DST settings! | |
1858 | Tm tm1 = GetTm(), | |
1859 | tm2 = dt.GetTm(); | |
1860 | ||
1861 | return tm1.hour == tm2.hour && | |
1862 | tm1.min == tm2.min && | |
1863 | tm1.sec == tm2.sec && | |
1864 | tm1.msec == tm2.msec; | |
1865 | } | |
1866 | ||
1867 | inline bool wxDateTime::IsEqualUpTo(const wxDateTime& dt, | |
1868 | const wxTimeSpan& ts) const | |
1869 | { | |
1870 | return IsBetween(dt.Subtract(ts), dt.Add(ts)); | |
1871 | } | |
1872 | ||
1873 | // ---------------------------------------------------------------------------- | |
1874 | // wxDateTime arithmetics | |
1875 | // ---------------------------------------------------------------------------- | |
1876 | ||
1877 | inline wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const | |
1878 | { | |
9a83f860 | 1879 | wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime")); |
df5168c4 MB |
1880 | |
1881 | return wxDateTime(m_time + diff.GetValue()); | |
1882 | } | |
1883 | ||
1884 | inline wxDateTime& wxDateTime::Add(const wxTimeSpan& diff) | |
1885 | { | |
9a83f860 | 1886 | wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime")); |
df5168c4 MB |
1887 | |
1888 | m_time += diff.GetValue(); | |
1889 | ||
1890 | return *this; | |
1891 | } | |
1892 | ||
1893 | inline wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff) | |
1894 | { | |
1895 | return Add(diff); | |
1896 | } | |
1897 | ||
1898 | inline wxDateTime wxDateTime::Subtract(const wxTimeSpan& diff) const | |
1899 | { | |
9a83f860 | 1900 | wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime")); |
df5168c4 MB |
1901 | |
1902 | return wxDateTime(m_time - diff.GetValue()); | |
1903 | } | |
1904 | ||
1905 | inline wxDateTime& wxDateTime::Subtract(const wxTimeSpan& diff) | |
1906 | { | |
9a83f860 | 1907 | wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime")); |
df5168c4 MB |
1908 | |
1909 | m_time -= diff.GetValue(); | |
1910 | ||
1911 | return *this; | |
1912 | } | |
1913 | ||
1914 | inline wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff) | |
1915 | { | |
1916 | return Subtract(diff); | |
1917 | } | |
1918 | ||
1919 | inline wxTimeSpan wxDateTime::Subtract(const wxDateTime& datetime) const | |
1920 | { | |
9a83f860 | 1921 | wxASSERT_MSG( IsValid() && datetime.IsValid(), wxT("invalid wxDateTime")); |
df5168c4 MB |
1922 | |
1923 | return wxTimeSpan(GetValue() - datetime.GetValue()); | |
1924 | } | |
1925 | ||
26364344 WS |
1926 | inline wxTimeSpan wxDateTime::operator-(const wxDateTime& dt2) const |
1927 | { | |
1928 | return this->Subtract(dt2); | |
1929 | } | |
1930 | ||
df5168c4 MB |
1931 | inline wxDateTime wxDateTime::Add(const wxDateSpan& diff) const |
1932 | { | |
1933 | return wxDateTime(*this).Add(diff); | |
1934 | } | |
1935 | ||
1936 | inline wxDateTime& wxDateTime::Subtract(const wxDateSpan& diff) | |
1937 | { | |
1938 | return Add(diff.Negate()); | |
1939 | } | |
1940 | ||
1941 | inline wxDateTime wxDateTime::Subtract(const wxDateSpan& diff) const | |
1942 | { | |
1943 | return wxDateTime(*this).Subtract(diff); | |
1944 | } | |
1945 | ||
1946 | inline wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff) | |
1947 | { | |
1948 | return Subtract(diff); | |
1949 | } | |
1950 | ||
1951 | inline wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff) | |
1952 | { | |
1953 | return Add(diff); | |
1954 | } | |
1955 | ||
1956 | // ---------------------------------------------------------------------------- | |
1957 | // wxDateTime and timezones | |
1958 | // ---------------------------------------------------------------------------- | |
1959 | ||
d26adb9d VZ |
1960 | inline wxDateTime |
1961 | wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz, bool noDST) const | |
df5168c4 MB |
1962 | { |
1963 | MODIFY_AND_RETURN( MakeTimezone(tz, noDST) ); | |
1964 | } | |
1965 | ||
d26adb9d VZ |
1966 | inline wxDateTime |
1967 | wxDateTime::FromTimezone(const wxDateTime::TimeZone& tz, bool noDST) const | |
1968 | { | |
1969 | MODIFY_AND_RETURN( MakeFromTimezone(tz, noDST) ); | |
1970 | } | |
1971 | ||
df5168c4 MB |
1972 | // ---------------------------------------------------------------------------- |
1973 | // wxTimeSpan construction | |
1974 | // ---------------------------------------------------------------------------- | |
1975 | ||
1976 | inline wxTimeSpan::wxTimeSpan(long hours, | |
1977 | long minutes, | |
9c21e80b VZ |
1978 | wxLongLong seconds, |
1979 | wxLongLong milliseconds) | |
df5168c4 MB |
1980 | { |
1981 | // assign first to avoid precision loss | |
1982 | m_diff = hours; | |
1983 | m_diff *= 60l; | |
1984 | m_diff += minutes; | |
1985 | m_diff *= 60l; | |
1986 | m_diff += seconds; | |
1987 | m_diff *= 1000l; | |
1988 | m_diff += milliseconds; | |
1989 | } | |
1990 | ||
1991 | // ---------------------------------------------------------------------------- | |
1992 | // wxTimeSpan accessors | |
1993 | // ---------------------------------------------------------------------------- | |
1994 | ||
1995 | inline wxLongLong wxTimeSpan::GetSeconds() const | |
1996 | { | |
1997 | return m_diff / 1000l; | |
1998 | } | |
1999 | ||
2000 | inline int wxTimeSpan::GetMinutes() const | |
2001 | { | |
2415cf67 VZ |
2002 | // For compatibility, this method (and the other accessors) return int, |
2003 | // even though GetLo() actually returns unsigned long with greater range. | |
2004 | return static_cast<int>((GetSeconds() / 60l).GetLo()); | |
df5168c4 MB |
2005 | } |
2006 | ||
2007 | inline int wxTimeSpan::GetHours() const | |
2008 | { | |
2009 | return GetMinutes() / 60; | |
2010 | } | |
2011 | ||
2012 | inline int wxTimeSpan::GetDays() const | |
2013 | { | |
2014 | return GetHours() / 24; | |
2015 | } | |
2016 | ||
2017 | inline int wxTimeSpan::GetWeeks() const | |
2018 | { | |
2019 | return GetDays() / 7; | |
2020 | } | |
2021 | ||
2022 | // ---------------------------------------------------------------------------- | |
2023 | // wxTimeSpan arithmetics | |
2024 | // ---------------------------------------------------------------------------- | |
2025 | ||
2026 | inline wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const | |
2027 | { | |
2028 | return wxTimeSpan(m_diff + diff.GetValue()); | |
2029 | } | |
2030 | ||
2031 | inline wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff) | |
2032 | { | |
2033 | m_diff += diff.GetValue(); | |
2034 | ||
2035 | return *this; | |
2036 | } | |
2037 | ||
2038 | inline wxTimeSpan wxTimeSpan::Subtract(const wxTimeSpan& diff) const | |
2039 | { | |
2040 | return wxTimeSpan(m_diff - diff.GetValue()); | |
2041 | } | |
2042 | ||
2043 | inline wxTimeSpan& wxTimeSpan::Subtract(const wxTimeSpan& diff) | |
2044 | { | |
2045 | m_diff -= diff.GetValue(); | |
2046 | ||
2047 | return *this; | |
2048 | } | |
2049 | ||
2050 | inline wxTimeSpan& wxTimeSpan::Multiply(int n) | |
2051 | { | |
2052 | m_diff *= (long)n; | |
2053 | ||
2054 | return *this; | |
2055 | } | |
2056 | ||
2057 | inline wxTimeSpan wxTimeSpan::Multiply(int n) const | |
2058 | { | |
2059 | return wxTimeSpan(m_diff * (long)n); | |
2060 | } | |
2061 | ||
2062 | inline wxTimeSpan wxTimeSpan::Abs() const | |
2063 | { | |
2064 | return wxTimeSpan(GetValue().Abs()); | |
2065 | } | |
2066 | ||
2067 | inline bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const | |
2068 | { | |
2069 | return GetValue() == ts.GetValue(); | |
2070 | } | |
2071 | ||
2072 | inline bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const | |
2073 | { | |
2074 | return GetValue().Abs() > ts.GetValue().Abs(); | |
f7541d48 VZ |
2075 | } |
2076 | ||
2077 | inline bool wxTimeSpan::IsShorterThan(const wxTimeSpan& ts) const | |
2078 | { | |
2079 | return GetValue().Abs() < ts.GetValue().Abs(); | |
df5168c4 MB |
2080 | } |
2081 | ||
2082 | // ---------------------------------------------------------------------------- | |
2083 | // wxDateSpan | |
2084 | // ---------------------------------------------------------------------------- | |
2085 | ||
2086 | inline wxDateSpan& wxDateSpan::operator+=(const wxDateSpan& other) | |
2087 | { | |
2088 | m_years += other.m_years; | |
2089 | m_months += other.m_months; | |
2090 | m_weeks += other.m_weeks; | |
2091 | m_days += other.m_days; | |
2092 | ||
2093 | return *this; | |
2094 | } | |
2095 | ||
2096 | inline wxDateSpan& wxDateSpan::Add(const wxDateSpan& other) | |
2097 | { | |
2098 | return *this += other; | |
2099 | } | |
2100 | ||
2101 | inline wxDateSpan wxDateSpan::Add(const wxDateSpan& other) const | |
2102 | { | |
2103 | wxDateSpan ds(*this); | |
2104 | ds.Add(other); | |
2105 | return ds; | |
2106 | } | |
2107 | ||
2108 | inline wxDateSpan& wxDateSpan::Multiply(int factor) | |
2109 | { | |
2110 | m_years *= factor; | |
2111 | m_months *= factor; | |
2112 | m_weeks *= factor; | |
2113 | m_days *= factor; | |
2114 | ||
2115 | return *this; | |
2116 | } | |
2117 | ||
2118 | inline wxDateSpan wxDateSpan::Multiply(int factor) const | |
2119 | { | |
2120 | wxDateSpan ds(*this); | |
2121 | ds.Multiply(factor); | |
2122 | return ds; | |
2123 | } | |
2124 | ||
2125 | inline wxDateSpan wxDateSpan::Negate() const | |
2126 | { | |
2127 | return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days); | |
2128 | } | |
2129 | ||
2130 | inline wxDateSpan& wxDateSpan::Neg() | |
2131 | { | |
2132 | m_years = -m_years; | |
2133 | m_months = -m_months; | |
2134 | m_weeks = -m_weeks; | |
2135 | m_days = -m_days; | |
2136 | ||
2137 | return *this; | |
2138 | } | |
2139 | ||
2140 | inline wxDateSpan& wxDateSpan::operator-=(const wxDateSpan& other) | |
2141 | { | |
2142 | return *this += other.Negate(); | |
2143 | } | |
2144 | ||
2145 | inline wxDateSpan& wxDateSpan::Subtract(const wxDateSpan& other) | |
2146 | { | |
2147 | return *this -= other; | |
2148 | } | |
2149 | ||
2150 | inline wxDateSpan wxDateSpan::Subtract(const wxDateSpan& other) const | |
2151 | { | |
2152 | wxDateSpan ds(*this); | |
2153 | ds.Subtract(other); | |
2154 | return ds; | |
2155 | } | |
2156 | ||
2157 | #undef MILLISECONDS_PER_DAY | |
2158 | ||
2159 | #undef MODIFY_AND_RETURN | |
2160 | ||
0979c962 VZ |
2161 | // ============================================================================ |
2162 | // binary operators | |
2163 | // ============================================================================ | |
2164 | ||
0979c962 VZ |
2165 | // ---------------------------------------------------------------------------- |
2166 | // wxTimeSpan operators | |
2167 | // ---------------------------------------------------------------------------- | |
2168 | ||
26364344 | 2169 | wxTimeSpan WXDLLIMPEXP_BASE operator*(int n, const wxTimeSpan& ts); |
0979c962 VZ |
2170 | |
2171 | // ---------------------------------------------------------------------------- | |
2172 | // wxDateSpan | |
2173 | // ---------------------------------------------------------------------------- | |
2174 | ||
26364344 | 2175 | wxDateSpan WXDLLIMPEXP_BASE operator*(int n, const wxDateSpan& ds); |
cd0b1709 VZ |
2176 | |
2177 | // ============================================================================ | |
2178 | // other helper functions | |
2179 | // ============================================================================ | |
2180 | ||
2181 | // ---------------------------------------------------------------------------- | |
2182 | // iteration helpers: can be used to write a for loop over enum variable like | |
2183 | // this: | |
2184 | // for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) ) | |
2185 | // ---------------------------------------------------------------------------- | |
2186 | ||
26364344 WS |
2187 | WXDLLIMPEXP_BASE void wxNextMonth(wxDateTime::Month& m); |
2188 | WXDLLIMPEXP_BASE void wxPrevMonth(wxDateTime::Month& m); | |
2189 | WXDLLIMPEXP_BASE void wxNextWDay(wxDateTime::WeekDay& wd); | |
2190 | WXDLLIMPEXP_BASE void wxPrevWDay(wxDateTime::WeekDay& wd); | |
0979c962 | 2191 | |
e421922f VZ |
2192 | #endif // wxUSE_DATETIME |
2193 | ||
2f02cb89 | 2194 | #endif // _WX_DATETIME_H |