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