]> git.saurik.com Git - wxWidgets.git/blob - include/wx/datetime.h
added GetEnglish{Month,WeekDay}Name()
[wxWidgets.git] / include / wx / datetime.h
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>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_DATETIME_H
14 #define _WX_DATETIME_H
15
16 #include "wx/defs.h"
17
18 #if wxUSE_DATETIME
19
20 #ifdef __WXWINCE__
21 #include "wx/msw/wince/time.h"
22 #elif !defined(__WXPALMOS5__)
23 #include <time.h>
24 #endif // OS
25
26 #include <limits.h> // for INT_MIN
27
28 #include "wx/longlong.h"
29
30 class WXDLLIMPEXP_FWD_BASE wxDateTime;
31 class WXDLLIMPEXP_FWD_BASE wxTimeSpan;
32 class WXDLLIMPEXP_FWD_BASE wxDateSpan;
33 #ifdef __WXMSW__
34 struct _SYSTEMTIME;
35 #endif
36
37 #include "wx/dynarray.h"
38
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
43 #define WX_TIME_BASE_OFFSET 0
44
45 /*
46 * TODO
47 *
48 * + 1. Time zones with minutes (make TimeZone a class)
49 * ? 2. getdate() function like under Solaris
50 * + 3. text conversion for wxDateSpan
51 * + 4. pluggable modules for the workdays calculations
52 * 5. wxDateTimeHolidayAuthority for Easter and other christian feasts
53 */
54
55 /* Two wrapper functions for thread safety */
56 #ifdef HAVE_LOCALTIME_R
57 #define wxLocaltime_r localtime_r
58 #else
59 WXDLLIMPEXP_BASE struct tm *wxLocaltime_r(const time_t*, struct tm*);
60 #if wxUSE_THREADS && !defined(__WINDOWS__) && !defined(__WATCOMC__)
61 // On Windows, localtime _is_ threadsafe!
62 #warning using pseudo thread-safe wrapper for localtime to emulate localtime_r
63 #endif
64 #endif
65
66 #ifdef HAVE_GMTIME_R
67 #define wxGmtime_r gmtime_r
68 #else
69 WXDLLIMPEXP_BASE struct tm *wxGmtime_r(const time_t*, struct tm*);
70 #if wxUSE_THREADS && !defined(__WINDOWS__) && !defined(__WATCOMC__)
71 // On Windows, gmtime _is_ threadsafe!
72 #warning using pseudo thread-safe wrapper for gmtime to emulate gmtime_r
73 #endif
74 #endif
75
76 /*
77 The three (main) classes declared in this header represent:
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
95 subtraction
96 ------------
97 wxDateTime - wxDateTime = wxTimeSpan
98 wxDateTime - wxTimeSpan = wxDateTime
99 wxDateTime - wxDateSpan = wxDateTime
100 wxTimeSpan - wxTimeSpan = wxTimeSpan
101 wxDateSpan - wxDateSpan = wxDateSpan
102
103 multiplication
104 --------------
105 wxTimeSpan * number = wxTimeSpan
106 number * wxTimeSpan = wxTimeSpan
107 wxDateSpan * number = wxDateSpan
108 number * wxDateSpan = wxDateSpan
109
110 unitary minus
111 -------------
112 -wxTimeSpan = wxTimeSpan
113 -wxDateSpan = wxDateSpan
114
115 For each binary operation OP (+, -, *) we have the following operatorOP=() as
116 a method and the method with a symbolic name OPER (Add, Subtract, Multiply)
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.
122 */
123
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)
128 class WXDLLIMPEXP_FWD_BASE wxDateTime;
129
130 extern WXDLLIMPEXP_DATA_BASE(const char *) wxDefaultDateTimeFormat;
131 extern WXDLLIMPEXP_DATA_BASE(const char *) wxDefaultTimeSpanFormat;
132 extern WXDLLIMPEXP_DATA_BASE(const wxDateTime) wxDefaultDateTime;
133
134 #define wxInvalidDateTime wxDefaultDateTime
135
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
170 // ----------------------------------------------------------------------------
171 // wxDateTime represents an absolute moment in the time
172 // ----------------------------------------------------------------------------
173
174 class WXDLLIMPEXP_BASE wxDateTime
175 {
176 public:
177 // types
178 // ------------------------------------------------------------------------
179
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
185 typedef unsigned short wxDateTime_t;
186
187 // constants
188 // ------------------------------------------------------------------------
189
190 // the timezones
191 enum TZ
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,
205 GMT7, GMT8, GMT9, GMT10, GMT11, GMT12, GMT13,
206 // Note that GMT12 and GMT_12 are not the same: there is a difference
207 // of exactly one day between them
208
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
239 A_CST = GMT13 + 1, // Central Standard Time (+9.5)
240 A_EST = GMT10, // Eastern Standard Time
241 A_ESST = GMT11, // Eastern Summer Time
242
243 // New Zealand
244 NZST = GMT12, // Standard Time
245 NZDT = GMT13, // Daylight Saving Time
246
247 // TODO add more symbolic timezone names here
248
249 // Universal Coordinated Time = the new and politically correct name
250 // for GMT
251 UTC = GMT0
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)"
270 // by Claus T�ndering, http://www.pip.dknet.dk/~c-t/calendar.html
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
389
390 // Required for WinCE
391 #ifdef USA
392 #undef USA
393 #endif
394
395 enum Country
396 {
397 Country_Unknown, // no special information for this country
398 Country_Default, // set the default country with SetCountry() method
399 // or use the default country with any other
400
401 // TODO add more countries (for this we must know about DST and/or
402 // holidays for this country)
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,
408 France,
409 Germany,
410 UK,
411 Country_WesternEurope_End = UK,
412
413 Russia,
414 USA
415 };
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 {
431 Inv_Year = SHRT_MIN // should hold in wxDateTime_t
432 };
433
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
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
449 // helper classes
450 // ------------------------------------------------------------------------
451
452 // a class representing a time zone: basicly, this is just an offset
453 // (in seconds) from GMT
454 class WXDLLIMPEXP_BASE TimeZone
455 {
456 public:
457 TimeZone(TZ tz);
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 }
468
469 long GetOffset() const { return m_offset; }
470
471 private:
472 // offset for this timezone from GMT in seconds
473 long m_offset;
474 };
475
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
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!
483 struct WXDLLIMPEXP_BASE Tm
484 {
485 wxDateTime_t msec, sec, min, hour, mday;
486 Month mon;
487 int year;
488
489 // default ctor inits the object to an invalid value
490 Tm();
491
492 // ctor from struct tm and the timezone
493 Tm(const struct tm& tm, const TimeZone& tz);
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
507 // add the given number of months to the date keeping it normalized
508 void AddMonths(int monDiff);
509
510 // add the given number of months to the date keeping it normalized
511 void AddDays(int dayDiff);
512
513 private:
514 // compute the weekday from other fields
515 void ComputeWeekDay();
516
517 // the timezone we correspond to
518 TimeZone m_tz;
519
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
525 // static methods
526 // ------------------------------------------------------------------------
527
528 // set the current country
529 static void SetCountry(Country country);
530 // get the current country
531 static Country GetCountry();
532
533 // return true if the country is a West European one (in practice,
534 // this means that the same DST rules as for EEC apply)
535 static bool IsWestEuropeanCountry(Country country = Country_Default);
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
549 // returns true if the given year is a leap year in the given calendar
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)
553 static int GetCentury(int year);
554
555 // returns the number of days in this year (356 or 355 for Gregorian
556 // calendar usually :-)
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
565
566 // get the full (default) or abbreviated month name in the current
567 // locale, returns empty string on error
568 static wxString GetMonthName(Month month,
569 NameFlags flags = Name_Full);
570
571 // get the standard English full (default) or abbreviated month name
572 static wxString GetEnglishMonthName(Month month,
573 NameFlags flags = Name_Full);
574
575 // get the full (default) or abbreviated weekday name in the current
576 // locale, returns empty string on error
577 static wxString GetWeekDayName(WeekDay weekday,
578 NameFlags flags = Name_Full);
579
580 // get the standard English full (default) or abbreviated weekday name
581 static wxString GetEnglishWeekDayName(WeekDay weekday,
582 NameFlags flags = Name_Full);
583
584 // get the AM and PM strings in the current locale (may be empty)
585 static void GetAmPmStrings(wxString *am, wxString *pm);
586
587 // return true if the given country uses DST for this year
588 static bool IsDSTApplicable(int year = Inv_Year,
589 Country country = Country_Default);
590
591 // get the beginning of DST for this year, will return invalid object
592 // if no DST applicable in this year. The default value of the
593 // parameter means to take the current year.
594 static wxDateTime GetBeginDST(int year = Inv_Year,
595 Country country = Country_Default);
596 // get the end of DST for this year, will return invalid object
597 // if no DST applicable in this year. The default value of the
598 // parameter means to take the current year.
599 static wxDateTime GetEndDST(int year = Inv_Year,
600 Country country = Country_Default);
601
602 // return the wxDateTime object for the current time
603 static inline wxDateTime Now();
604
605 // return the wxDateTime object for the current time with millisecond
606 // precision (if available on this platform)
607 static wxDateTime UNow();
608
609 // return the wxDateTime object for today midnight: i.e. as Now() but
610 // with time set to 0
611 static inline wxDateTime Today();
612
613 // constructors: you should test whether the constructor succeeded with
614 // IsValid() function. The values Inv_Month and Inv_Year for the
615 // parameters mean take current month and/or year values.
616 // ------------------------------------------------------------------------
617
618 // default ctor does not initialize the object, use Set()!
619 wxDateTime() { m_time = wxLongLong(wxINT32_MIN, 0); }
620
621 // from time_t: seconds since the Epoch 00:00:00 UTC, Jan 1, 1970)
622 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
623 // VA C++ confuses this with wxDateTime(double jdn) thinking it is a duplicate declaration
624 inline wxDateTime(time_t timet);
625 #endif
626 // from broken down time/date (only for standard Unix range)
627 inline wxDateTime(const struct tm& tm);
628 // from broken down time/date (any range)
629 inline wxDateTime(const Tm& tm);
630
631 // from JDN (beware of rounding errors)
632 inline wxDateTime(double jdn);
633
634 // from separate values for each component, date set to today
635 inline wxDateTime(wxDateTime_t hour,
636 wxDateTime_t minute = 0,
637 wxDateTime_t second = 0,
638 wxDateTime_t millisec = 0);
639 // from separate values for each component with explicit date
640 inline wxDateTime(wxDateTime_t day, // day of the month
641 Month month,
642 int year = Inv_Year, // 1999, not 99 please!
643 wxDateTime_t hour = 0,
644 wxDateTime_t minute = 0,
645 wxDateTime_t second = 0,
646 wxDateTime_t millisec = 0);
647 #ifdef __WXMSW__
648 wxDateTime(const struct _SYSTEMTIME& st)
649 {
650 SetFromMSWSysTime(st);
651 }
652 #endif
653
654 // default copy ctor ok
655
656 // no dtor
657
658 // assignment operators and Set() functions: all non const methods return
659 // the reference to this object. IsValid() should be used to test whether
660 // the function succeeded.
661 // ------------------------------------------------------------------------
662
663 // set to the current time
664 inline wxDateTime& SetToCurrent();
665
666 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
667 // VA C++ confuses this with wxDateTime(double jdn) thinking it is a duplicate declaration
668 // set to given time_t value
669 inline wxDateTime& Set(time_t timet);
670 #endif
671
672 // set to given broken down time/date
673 wxDateTime& Set(const struct tm& tm);
674
675 // set to given broken down time/date
676 inline wxDateTime& Set(const Tm& tm);
677
678 // set to given JDN (beware of rounding errors)
679 wxDateTime& Set(double jdn);
680
681 // set to given time, date = today
682 wxDateTime& Set(wxDateTime_t hour,
683 wxDateTime_t minute = 0,
684 wxDateTime_t second = 0,
685 wxDateTime_t millisec = 0);
686
687 // from separate values for each component with explicit date
688 // (defaults for month and year are the current values)
689 wxDateTime& Set(wxDateTime_t day,
690 Month month,
691 int year = Inv_Year, // 1999, not 99 please!
692 wxDateTime_t hour = 0,
693 wxDateTime_t minute = 0,
694 wxDateTime_t second = 0,
695 wxDateTime_t millisec = 0);
696
697 // resets time to 00:00:00, doesn't change the date
698 wxDateTime& ResetTime();
699
700 // get the date part of this object only, i.e. the object which has the
701 // same date as this one but time of 00:00:00
702 wxDateTime GetDateOnly() const;
703
704 // the following functions don't change the values of the other
705 // fields, i.e. SetMinute() won't change either hour or seconds value
706
707 // set the year
708 wxDateTime& SetYear(int year);
709 // set the month
710 wxDateTime& SetMonth(Month month);
711 // set the day of the month
712 wxDateTime& SetDay(wxDateTime_t day);
713 // set hour
714 wxDateTime& SetHour(wxDateTime_t hour);
715 // set minute
716 wxDateTime& SetMinute(wxDateTime_t minute);
717 // set second
718 wxDateTime& SetSecond(wxDateTime_t second);
719 // set millisecond
720 wxDateTime& SetMillisecond(wxDateTime_t millisecond);
721
722 // assignment operator from time_t
723 wxDateTime& operator=(time_t timet) { return Set(timet); }
724
725 // assignment operator from broken down time/date
726 wxDateTime& operator=(const struct tm& tm) { return Set(tm); }
727
728 // assignment operator from broken down time/date
729 wxDateTime& operator=(const Tm& tm) { return Set(tm); }
730
731 // default assignment operator is ok
732
733 // calendar calculations (functions which set the date only leave the time
734 // unchanged, e.g. don't explictly zero it): SetXXX() functions modify the
735 // object itself, GetXXX() ones return a new object.
736 // ------------------------------------------------------------------------
737
738 // set to the given week day in the same week as this one
739 wxDateTime& SetToWeekDayInSameWeek(WeekDay weekday,
740 WeekFlags flags = Monday_First);
741 inline wxDateTime GetWeekDayInSameWeek(WeekDay weekday,
742 WeekFlags flags = Monday_First) const;
743
744 // set to the next week day following this one
745 wxDateTime& SetToNextWeekDay(WeekDay weekday);
746 inline wxDateTime GetNextWeekDay(WeekDay weekday) const;
747
748 // set to the previous week day before this one
749 wxDateTime& SetToPrevWeekDay(WeekDay weekday);
750 inline wxDateTime GetPrevWeekDay(WeekDay weekday) const;
751
752 // set to Nth occurrence of given weekday in the given month of the
753 // given year (time is set to 0), return true on success and false on
754 // failure. n may be positive (1..5) or negative to count from the end
755 // of the month (see helper function SetToLastWeekDay())
756 bool SetToWeekDay(WeekDay weekday,
757 int n = 1,
758 Month month = Inv_Month,
759 int year = Inv_Year);
760 inline wxDateTime GetWeekDay(WeekDay weekday,
761 int n = 1,
762 Month month = Inv_Month,
763 int year = Inv_Year) const;
764
765 // sets to the last weekday in the given month, year
766 inline bool SetToLastWeekDay(WeekDay weekday,
767 Month month = Inv_Month,
768 int year = Inv_Year);
769 inline wxDateTime GetLastWeekDay(WeekDay weekday,
770 Month month = Inv_Month,
771 int year = Inv_Year);
772
773 #if WXWIN_COMPATIBILITY_2_6
774 // sets the date to the given day of the given week in the year,
775 // returns true on success and false if given date doesn't exist (e.g.
776 // numWeek is > 53)
777 //
778 // these functions are badly defined as they're not the reverse of
779 // GetWeekOfYear(), use SetToTheWeekOfYear() instead
780 wxDEPRECATED( bool SetToTheWeek(wxDateTime_t numWeek,
781 WeekDay weekday = Mon,
782 WeekFlags flags = Monday_First) );
783 wxDEPRECATED( wxDateTime GetWeek(wxDateTime_t numWeek,
784 WeekDay weekday = Mon,
785 WeekFlags flags = Monday_First) const );
786 #endif // WXWIN_COMPATIBILITY_2_6
787
788 // returns the date corresponding to the given week day of the given
789 // week (in ISO notation) of the specified year
790 static wxDateTime SetToWeekOfYear(int year,
791 wxDateTime_t numWeek,
792 WeekDay weekday = Mon);
793
794 // sets the date to the last day of the given (or current) month or the
795 // given (or current) year
796 wxDateTime& SetToLastMonthDay(Month month = Inv_Month,
797 int year = Inv_Year);
798 inline wxDateTime GetLastMonthDay(Month month = Inv_Month,
799 int year = Inv_Year) const;
800
801 // sets to the given year day (1..365 or 366)
802 wxDateTime& SetToYearDay(wxDateTime_t yday);
803 inline wxDateTime GetYearDay(wxDateTime_t yday) const;
804
805 // The definitions below were taken verbatim from
806 //
807 // http://www.capecod.net/~pbaum/date/date0.htm
808 //
809 // (Peter Baum's home page)
810 //
811 // definition: The Julian Day Number, Julian Day, or JD of a
812 // particular instant of time is the number of days and fractions of a
813 // day since 12 hours Universal Time (Greenwich mean noon) on January
814 // 1 of the year -4712, where the year is given in the Julian
815 // proleptic calendar. The idea of using this reference date was
816 // originally proposed by Joseph Scalizer in 1582 to count years but
817 // it was modified by 19th century astronomers to count days. One
818 // could have equivalently defined the reference time to be noon of
819 // November 24, -4713 if were understood that Gregorian calendar rules
820 // were applied. Julian days are Julian Day Numbers and are not to be
821 // confused with Julian dates.
822 //
823 // definition: The Rata Die number is a date specified as the number
824 // of days relative to a base date of December 31 of the year 0. Thus
825 // January 1 of the year 1 is Rata Die day 1.
826
827 // get the Julian Day number (the fractional part specifies the time of
828 // the day, related to noon - beware of rounding errors!)
829 double GetJulianDayNumber() const;
830 double GetJDN() const { return GetJulianDayNumber(); }
831
832 // get the Modified Julian Day number: it is equal to JDN - 2400000.5
833 // and so integral MJDs correspond to the midnights (and not noons).
834 // MJD 0 is Nov 17, 1858
835 double GetModifiedJulianDayNumber() const { return GetJDN() - 2400000.5; }
836 double GetMJD() const { return GetModifiedJulianDayNumber(); }
837
838 // get the Rata Die number
839 double GetRataDie() const;
840
841 // TODO algorithms for calculating some important dates, such as
842 // religious holidays (Easter...) or moon/solar eclipses? Some
843 // algorithms can be found in the calendar FAQ
844
845
846 // Timezone stuff: a wxDateTime object constructed using given
847 // day/month/year/hour/min/sec values is interpreted as this moment in
848 // local time. Using the functions below, it may be converted to another
849 // time zone (e.g., the Unix epoch is wxDateTime(1, Jan, 1970).ToGMT()).
850 //
851 // These functions try to handle DST internally, but there is no magical
852 // way to know all rules for it in all countries in the world, so if the
853 // program can handle it itself (or doesn't want to handle it at all for
854 // whatever reason), the DST handling can be disabled with noDST.
855 // ------------------------------------------------------------------------
856
857 // transform to any given timezone
858 inline wxDateTime ToTimezone(const TimeZone& tz, bool noDST = false) const;
859 wxDateTime& MakeTimezone(const TimeZone& tz, bool noDST = false);
860
861 // interpret current value as being in another timezone and transform
862 // it to local one
863 inline wxDateTime FromTimezone(const TimeZone& tz, bool noDST = false) const;
864 wxDateTime& MakeFromTimezone(const TimeZone& tz, bool noDST = false);
865
866 // transform to/from GMT/UTC
867 wxDateTime ToUTC(bool noDST = false) const { return ToTimezone(UTC, noDST); }
868 wxDateTime& MakeUTC(bool noDST = false) { return MakeTimezone(UTC, noDST); }
869
870 wxDateTime ToGMT(bool noDST = false) const { return ToUTC(noDST); }
871 wxDateTime& MakeGMT(bool noDST = false) { return MakeUTC(noDST); }
872
873 wxDateTime FromUTC(bool noDST = false) const
874 { return FromTimezone(UTC, noDST); }
875 wxDateTime& MakeFromUTC(bool noDST = false)
876 { return MakeFromTimezone(UTC, noDST); }
877
878 // is daylight savings time in effect at this moment according to the
879 // rules of the specified country?
880 //
881 // Return value is > 0 if DST is in effect, 0 if it is not and -1 if
882 // the information is not available (this is compatible with ANSI C)
883 int IsDST(Country country = Country_Default) const;
884
885
886 // accessors: many of them take the timezone parameter which indicates the
887 // timezone for which to make the calculations and the default value means
888 // to do it for the current timezone of this machine (even if the function
889 // only operates with the date it's necessary because a date may wrap as
890 // result of timezone shift)
891 // ------------------------------------------------------------------------
892
893 // is the date valid?
894 inline bool IsValid() const { return m_time != wxInvalidDateTime.m_time; }
895
896 // get the broken down date/time representation in the given timezone
897 //
898 // If you wish to get several time components (day, month and year),
899 // consider getting the whole Tm strcuture first and retrieving the
900 // value from it - this is much more efficient
901 Tm GetTm(const TimeZone& tz = Local) const;
902
903 // get the number of seconds since the Unix epoch - returns (time_t)-1
904 // if the value is out of range
905 inline time_t GetTicks() const;
906
907 // get the century, same as GetCentury(GetYear())
908 int GetCentury(const TimeZone& tz = Local) const
909 { return GetCentury(GetYear(tz)); }
910 // get the year (returns Inv_Year if date is invalid)
911 int GetYear(const TimeZone& tz = Local) const
912 { return GetTm(tz).year; }
913 // get the month (Inv_Month if date is invalid)
914 Month GetMonth(const TimeZone& tz = Local) const
915 { return (Month)GetTm(tz).mon; }
916 // get the month day (in 1..31 range, 0 if date is invalid)
917 wxDateTime_t GetDay(const TimeZone& tz = Local) const
918 { return GetTm(tz).mday; }
919 // get the day of the week (Inv_WeekDay if date is invalid)
920 WeekDay GetWeekDay(const TimeZone& tz = Local) const
921 { return GetTm(tz).GetWeekDay(); }
922 // get the hour of the day
923 wxDateTime_t GetHour(const TimeZone& tz = Local) const
924 { return GetTm(tz).hour; }
925 // get the minute
926 wxDateTime_t GetMinute(const TimeZone& tz = Local) const
927 { return GetTm(tz).min; }
928 // get the second
929 wxDateTime_t GetSecond(const TimeZone& tz = Local) const
930 { return GetTm(tz).sec; }
931 // get milliseconds
932 wxDateTime_t GetMillisecond(const TimeZone& tz = Local) const
933 { return GetTm(tz).msec; }
934
935 // get the day since the year start (1..366, 0 if date is invalid)
936 wxDateTime_t GetDayOfYear(const TimeZone& tz = Local) const;
937 // get the week number since the year start (1..52 or 53, 0 if date is
938 // invalid)
939 wxDateTime_t GetWeekOfYear(WeekFlags flags = Monday_First,
940 const TimeZone& tz = Local) const;
941 // get the week number since the month start (1..5, 0 if date is
942 // invalid)
943 wxDateTime_t GetWeekOfMonth(WeekFlags flags = Monday_First,
944 const TimeZone& tz = Local) const;
945
946 // is this date a work day? This depends on a country, of course,
947 // because the holidays are different in different countries
948 bool IsWorkDay(Country country = Country_Default) const;
949
950 // is this date later than Gregorian calendar introduction for the
951 // given country (see enum GregorianAdoption)?
952 //
953 // NB: this function shouldn't be considered as absolute authority in
954 // the matter. Besides, for some countries the exact date of
955 // adoption of the Gregorian calendar is simply unknown.
956 bool IsGregorianDate(GregorianAdoption country = Gr_Standard) const;
957
958 // dos date and time format
959 // ------------------------------------------------------------------------
960
961 // set from the DOS packed format
962 wxDateTime& SetFromDOS(unsigned long ddt);
963
964 // pack the date in DOS format
965 unsigned long GetAsDOS() const;
966
967 // SYSTEMTIME format
968 // ------------------------------------------------------------------------
969 #ifdef __WXMSW__
970
971 // convert SYSTEMTIME to wxDateTime
972 wxDateTime& SetFromMSWSysTime(const struct _SYSTEMTIME&);
973
974 // convert wxDateTime to SYSTEMTIME
975 void GetAsMSWSysTime(struct _SYSTEMTIME*) const;
976 #endif // __WXMSW__
977
978 // comparison (see also functions below for operator versions)
979 // ------------------------------------------------------------------------
980
981 // returns true if the two moments are strictly identical
982 inline bool IsEqualTo(const wxDateTime& datetime) const;
983
984 // returns true if the date is strictly earlier than the given one
985 inline bool IsEarlierThan(const wxDateTime& datetime) const;
986
987 // returns true if the date is strictly later than the given one
988 inline bool IsLaterThan(const wxDateTime& datetime) const;
989
990 // returns true if the date is strictly in the given range
991 inline bool IsStrictlyBetween(const wxDateTime& t1,
992 const wxDateTime& t2) const;
993
994 // returns true if the date is in the given range
995 inline bool IsBetween(const wxDateTime& t1, const wxDateTime& t2) const;
996
997 // do these two objects refer to the same date?
998 inline bool IsSameDate(const wxDateTime& dt) const;
999
1000 // do these two objects have the same time?
1001 inline bool IsSameTime(const wxDateTime& dt) const;
1002
1003 // are these two objects equal up to given timespan?
1004 inline bool IsEqualUpTo(const wxDateTime& dt, const wxTimeSpan& ts) const;
1005
1006 inline bool operator<(const wxDateTime& dt) const
1007 {
1008 wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") );
1009 return GetValue() < dt.GetValue();
1010 }
1011
1012 inline bool operator<=(const wxDateTime& dt) const
1013 {
1014 wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") );
1015 return GetValue() <= dt.GetValue();
1016 }
1017
1018 inline bool operator>(const wxDateTime& dt) const
1019 {
1020 wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") );
1021 return GetValue() > dt.GetValue();
1022 }
1023
1024 inline bool operator>=(const wxDateTime& dt) const
1025 {
1026 wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") );
1027 return GetValue() >= dt.GetValue();
1028 }
1029
1030 inline bool operator==(const wxDateTime& dt) const
1031 {
1032 wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") );
1033 return GetValue() == dt.GetValue();
1034 }
1035
1036 inline bool operator!=(const wxDateTime& dt) const
1037 {
1038 wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") );
1039 return GetValue() != dt.GetValue();
1040 }
1041
1042 // arithmetics with dates (see also below for more operators)
1043 // ------------------------------------------------------------------------
1044
1045 // return the sum of the date with a time span (positive or negative)
1046 inline wxDateTime Add(const wxTimeSpan& diff) const;
1047 // add a time span (positive or negative)
1048 inline wxDateTime& Add(const wxTimeSpan& diff);
1049 // add a time span (positive or negative)
1050 inline wxDateTime& operator+=(const wxTimeSpan& diff);
1051 inline wxDateTime operator+(const wxTimeSpan& ts) const
1052 {
1053 wxDateTime dt(*this);
1054 dt.Add(ts);
1055 return dt;
1056 }
1057
1058 // return the difference of the date with a time span
1059 inline wxDateTime Subtract(const wxTimeSpan& diff) const;
1060 // subtract a time span (positive or negative)
1061 inline wxDateTime& Subtract(const wxTimeSpan& diff);
1062 // subtract a time span (positive or negative)
1063 inline wxDateTime& operator-=(const wxTimeSpan& diff);
1064 inline wxDateTime operator-(const wxTimeSpan& ts) const
1065 {
1066 wxDateTime dt(*this);
1067 dt.Subtract(ts);
1068 return dt;
1069 }
1070
1071 // return the sum of the date with a date span
1072 inline wxDateTime Add(const wxDateSpan& diff) const;
1073 // add a date span (positive or negative)
1074 wxDateTime& Add(const wxDateSpan& diff);
1075 // add a date span (positive or negative)
1076 inline wxDateTime& operator+=(const wxDateSpan& diff);
1077 inline wxDateTime operator+(const wxDateSpan& ds) const
1078 {
1079 wxDateTime dt(*this);
1080 dt.Add(ds);
1081 return dt;
1082 }
1083
1084 // return the difference of the date with a date span
1085 inline wxDateTime Subtract(const wxDateSpan& diff) const;
1086 // subtract a date span (positive or negative)
1087 inline wxDateTime& Subtract(const wxDateSpan& diff);
1088 // subtract a date span (positive or negative)
1089 inline wxDateTime& operator-=(const wxDateSpan& diff);
1090 inline wxDateTime operator-(const wxDateSpan& ds) const
1091 {
1092 wxDateTime dt(*this);
1093 dt.Subtract(ds);
1094 return dt;
1095 }
1096
1097 // return the difference between two dates
1098 inline wxTimeSpan Subtract(const wxDateTime& dt) const;
1099 inline wxTimeSpan operator-(const wxDateTime& dt2) const;
1100
1101 // conversion to/from text: all conversions from text return the pointer to
1102 // the next character following the date specification (i.e. the one where
1103 // the scan had to stop) or NULL on failure; for the versions taking
1104 // wxString or wxCStrData, we don't know if the user code needs char* or
1105 // wchar_t* pointer and so we return char* one for compatibility with the
1106 // existing ANSI code and also return iterator in another output parameter
1107 // (it will be equal to end if the entire string was parsed)
1108 // ------------------------------------------------------------------------
1109
1110 // parse a string in RFC 822 format (found e.g. in mail headers and
1111 // having the form "Wed, 10 Feb 1999 19:07:07 +0100")
1112 const char *ParseRfc822Date(const wxString& date,
1113 wxString::const_iterator *end = NULL);
1114 const char *ParseRfc822Date(const wxCStrData& date,
1115 wxString::const_iterator *end = NULL)
1116 {
1117 return ParseRfc822Date(date.AsString(), end);
1118 }
1119
1120 const wchar_t *ParseRfc822Date(const wchar_t* date)
1121 {
1122 return ReturnEndAsWidePtr(&wxDateTime::ParseRfc822Date, date);
1123 }
1124
1125 const char *ParseRfc822Date(const char* date)
1126 {
1127 return ParseRfc822Date(wxString(date));
1128 }
1129
1130 // parse a date/time in the given format (see strptime(3)), fill in
1131 // the missing (in the string) fields with the values of dateDef (by
1132 // default, they will not change if they had valid values or will
1133 // default to Today() otherwise)
1134
1135 // notice that we unfortunately need all those overloads because we use
1136 // the type of the date string to select the return value of the
1137 // function: it's wchar_t if a wide string is passed for compatibility
1138 // with the code doing "const wxChar *p = dt.ParseFormat(_T("..."))",
1139 // but char* in all other cases for compatibility with ANSI build which
1140 // allowed code like "const char *p = dt.ParseFormat("...")"
1141 //
1142 // so we need wchar_t overload and now passing s.c_str() as first
1143 // argument is ambiguous because it's convertible to both wxString and
1144 // wchar_t* and now it's passing char* which becomes ambiguous as it is
1145 // convertible to both wxString and wxCStrData hence we need char*
1146 // overload too
1147 //
1148 // and to make our life more miserable we also pay for having the
1149 // optional dateDef parameter: as it's almost never used, we want to
1150 // allow people to omit it when specifying the end iterator output
1151 // parameter but we still have to allow specifying dateDef too, so we
1152 // need another overload for this
1153 //
1154 // FIXME: all this mess could be avoided by using some class similar to
1155 // wxFormatString, i.e. remembering string [pointer] of any type
1156 // and convertible to either char* or wchar_t* as wxCStrData and
1157 // having only 1 (or 2, because of the last paragraph above)
1158 // overload taking it, see #9560
1159 const char *ParseFormat(const wxString& date,
1160 const wxString& format = wxDefaultDateTimeFormat,
1161 const wxDateTime& dateDef = wxDefaultDateTime,
1162 wxString::const_iterator *end = NULL);
1163
1164 const char *ParseFormat(const wxString& date,
1165 const wxString& format,
1166 wxString::const_iterator *end)
1167 {
1168 return ParseFormat(date, format, wxDefaultDateTime, end);
1169 }
1170
1171 const char *ParseFormat(const wxCStrData& date,
1172 const wxString& format = wxDefaultDateTimeFormat,
1173 const wxDateTime& dateDef = wxDefaultDateTime,
1174 wxString::const_iterator *end = NULL)
1175 {
1176 return ParseFormat(date.AsString(), format, dateDef, end);
1177 }
1178
1179 const wchar_t *ParseFormat(const wchar_t *date,
1180 const wxString& format = wxDefaultDateTimeFormat,
1181 const wxDateTime& dateDef = wxDefaultDateTime)
1182 {
1183 const wxString datestr(date);
1184 wxString::const_iterator end;
1185 if ( !ParseFormat(datestr, format, dateDef, &end) )
1186 return NULL;
1187
1188 return date + (end - datestr.begin());
1189 }
1190
1191 const char *ParseFormat(const char *date,
1192 const wxString& format = "%c",
1193 const wxDateTime& dateDef = wxDefaultDateTime)
1194 {
1195 return ParseFormat(wxString(date), format, dateDef);
1196 }
1197
1198
1199 // parse a string containing date, time or both in ISO 8601 format
1200 //
1201 // notice that these functions are new in wx 3.0 and so we don't
1202 // provide compatibility overloads for them
1203 bool ParseISODate(const wxString& date)
1204 {
1205 wxString::const_iterator end;
1206 return ParseFormat(date, wxS("%Y-%m-%d"), &end) && end == date.end();
1207 }
1208
1209 bool ParseISOTime(const wxString& time)
1210 {
1211 wxString::const_iterator end;
1212 return ParseFormat(time, wxS("%H:%M:%S"), &end) && end == time.end();
1213 }
1214
1215 bool ParseISOCombined(const wxString& datetime, char sep = 'T')
1216 {
1217 wxString::const_iterator end;
1218 const wxString fmt = wxS("%Y-%m-%d") + wxString(sep) + wxS("%H:%M:%S");
1219 return ParseFormat(datetime, fmt, &end) && end == datetime.end();
1220 }
1221
1222 // parse a string containing the date/time in "free" format, this
1223 // function will try to make an educated guess at the string contents
1224 const char *ParseDateTime(const wxString& datetime,
1225 wxString::const_iterator *end = NULL);
1226
1227 const char *ParseDateTime(const wxCStrData& datetime,
1228 wxString::const_iterator *end = NULL)
1229 {
1230 return ParseDateTime(datetime.AsString(), end);
1231 }
1232
1233 const wchar_t *ParseDateTime(const wchar_t *datetime)
1234 {
1235 return ReturnEndAsWidePtr(&wxDateTime::ParseDateTime, datetime);
1236 }
1237
1238 const char *ParseDateTime(const char *datetime)
1239 {
1240 return ParseDateTime(wxString(datetime));
1241 }
1242
1243 // parse a string containing the date only in "free" format (less
1244 // flexible than ParseDateTime)
1245 const char *ParseDate(const wxString& date,
1246 wxString::const_iterator *end = NULL);
1247
1248 const char *ParseDate(const wxCStrData& date,
1249 wxString::const_iterator *end = NULL)
1250 {
1251 return ParseDate(date.AsString(), end);
1252 }
1253
1254 const wchar_t *ParseDate(const wchar_t *date)
1255 {
1256 return ReturnEndAsWidePtr(&wxDateTime::ParseDate, date);
1257 }
1258
1259 const char *ParseDate(const char *date)
1260 {
1261 return ParseDate(wxString(date));
1262 }
1263
1264 // parse a string containing the time only in "free" format
1265 const char *ParseTime(const wxString& time,
1266 wxString::const_iterator *end = NULL);
1267
1268 const char *ParseTime(const wxCStrData& time,
1269 wxString::const_iterator *end = NULL)
1270 {
1271 return ParseTime(time.AsString(), end);
1272 }
1273
1274 const wchar_t *ParseTime(const wchar_t *time)
1275 {
1276 return ReturnEndAsWidePtr(&wxDateTime::ParseTime, time);
1277 }
1278
1279 const char *ParseTime(const char *time)
1280 {
1281 return ParseTime(wxString(time));
1282 }
1283
1284 // this function accepts strftime()-like format string (default
1285 // argument corresponds to the preferred date and time representation
1286 // for the current locale) and returns the string containing the
1287 // resulting text representation
1288 wxString Format(const wxString& format = wxDefaultDateTimeFormat,
1289 const TimeZone& tz = Local) const;
1290 // preferred date representation for the current locale
1291 wxString FormatDate() const { return Format(wxS("%x")); }
1292 // preferred time representation for the current locale
1293 wxString FormatTime() const { return Format(wxS("%X")); }
1294 // returns the string representing the date in ISO 8601 format
1295 // (YYYY-MM-DD)
1296 wxString FormatISODate() const { return Format(wxS("%Y-%m-%d")); }
1297 // returns the string representing the time in ISO 8601 format
1298 // (HH:MM:SS)
1299 wxString FormatISOTime() const { return Format(wxS("%H:%M:%S")); }
1300 // return the combined date time representation in ISO 8601 format; the
1301 // separator character should be 'T' according to the standard but it
1302 // can also be useful to set it to ' '
1303 wxString FormatISOCombined(char sep = 'T') const
1304 { return FormatISODate() + sep + FormatISOTime(); }
1305
1306 // implementation
1307 // ------------------------------------------------------------------------
1308
1309 // construct from internal representation
1310 wxDateTime(const wxLongLong& time) { m_time = time; }
1311
1312 // get the internal representation
1313 inline wxLongLong GetValue() const;
1314
1315 // a helper function to get the current time_t
1316 static time_t GetTimeNow() { return time(NULL); }
1317
1318 // another one to get the current time broken down
1319 static struct tm *GetTmNow()
1320 {
1321 static struct tm l_CurrentTime;
1322 return GetTmNow(&l_CurrentTime);
1323 }
1324
1325 // get current time using thread-safe function
1326 static struct tm *GetTmNow(struct tm *tmstruct);
1327
1328 private:
1329 // helper function for defining backward-compatible wrappers for code
1330 // using wchar_t* pointer instead of wxString iterators
1331 typedef
1332 const char *(wxDateTime::*StringMethod)(const wxString& s,
1333 wxString::const_iterator *end);
1334
1335 const wchar_t *ReturnEndAsWidePtr(StringMethod func, const wchar_t *p)
1336 {
1337 const wxString s(p);
1338 wxString::const_iterator end;
1339 if ( !(this->*func)(s, &end) )
1340 return NULL;
1341
1342 return p + (end - s.begin());
1343 }
1344
1345
1346 // the current country - as it's the same for all program objects (unless
1347 // it runs on a _really_ big cluster system :-), this is a static member:
1348 // see SetCountry() and GetCountry()
1349 static Country ms_country;
1350
1351 // this constant is used to transform a time_t value to the internal
1352 // representation, as time_t is in seconds and we use milliseconds it's
1353 // fixed to 1000
1354 static const long TIME_T_FACTOR;
1355
1356 // returns true if we fall in range in which we can use standard ANSI C
1357 // functions
1358 inline bool IsInStdRange() const;
1359
1360 // the internal representation of the time is the amount of milliseconds
1361 // elapsed since the origin which is set by convention to the UNIX/C epoch
1362 // value: the midnight of January 1, 1970 (UTC)
1363 wxLongLong m_time;
1364 };
1365
1366 // ----------------------------------------------------------------------------
1367 // This class contains a difference between 2 wxDateTime values, so it makes
1368 // sense to add it to wxDateTime and it is the result of subtraction of 2
1369 // objects of that class. See also wxDateSpan.
1370 // ----------------------------------------------------------------------------
1371
1372 class WXDLLIMPEXP_BASE wxTimeSpan
1373 {
1374 public:
1375 // constructors
1376 // ------------------------------------------------------------------------
1377
1378 // return the timespan for the given number of milliseconds
1379 static wxTimeSpan Milliseconds(wxLongLong ms) { return wxTimeSpan(0, 0, 0, ms); }
1380 static wxTimeSpan Millisecond() { return Milliseconds(1); }
1381
1382 // return the timespan for the given number of seconds
1383 static wxTimeSpan Seconds(wxLongLong sec) { return wxTimeSpan(0, 0, sec); }
1384 static wxTimeSpan Second() { return Seconds(1); }
1385
1386 // return the timespan for the given number of minutes
1387 static wxTimeSpan Minutes(long min) { return wxTimeSpan(0, min, 0 ); }
1388 static wxTimeSpan Minute() { return Minutes(1); }
1389
1390 // return the timespan for the given number of hours
1391 static wxTimeSpan Hours(long hours) { return wxTimeSpan(hours, 0, 0); }
1392 static wxTimeSpan Hour() { return Hours(1); }
1393
1394 // return the timespan for the given number of days
1395 static wxTimeSpan Days(long days) { return Hours(24 * days); }
1396 static wxTimeSpan Day() { return Days(1); }
1397
1398 // return the timespan for the given number of weeks
1399 static wxTimeSpan Weeks(long days) { return Days(7 * days); }
1400 static wxTimeSpan Week() { return Weeks(1); }
1401
1402 // default ctor constructs the 0 time span
1403 wxTimeSpan() { }
1404
1405 // from separate values for each component, date set to 0 (hours are
1406 // not restricted to 0..24 range, neither are minutes, seconds or
1407 // milliseconds)
1408 inline wxTimeSpan(long hours,
1409 long minutes = 0,
1410 wxLongLong seconds = 0,
1411 wxLongLong milliseconds = 0);
1412
1413 // default copy ctor is ok
1414
1415 // no dtor
1416
1417 // arithmetics with time spans (see also below for more operators)
1418 // ------------------------------------------------------------------------
1419
1420 // return the sum of two timespans
1421 inline wxTimeSpan Add(const wxTimeSpan& diff) const;
1422 // add two timespans together
1423 inline wxTimeSpan& Add(const wxTimeSpan& diff);
1424 // add two timespans together
1425 wxTimeSpan& operator+=(const wxTimeSpan& diff) { return Add(diff); }
1426 inline wxTimeSpan operator+(const wxTimeSpan& ts) const
1427 {
1428 return wxTimeSpan(GetValue() + ts.GetValue());
1429 }
1430
1431 // return the difference of two timespans
1432 inline wxTimeSpan Subtract(const wxTimeSpan& diff) const;
1433 // subtract another timespan
1434 inline wxTimeSpan& Subtract(const wxTimeSpan& diff);
1435 // subtract another timespan
1436 wxTimeSpan& operator-=(const wxTimeSpan& diff) { return Subtract(diff); }
1437 inline wxTimeSpan operator-(const wxTimeSpan& ts)
1438 {
1439 return wxTimeSpan(GetValue() - ts.GetValue());
1440 }
1441
1442 // multiply timespan by a scalar
1443 inline wxTimeSpan Multiply(int n) const;
1444 // multiply timespan by a scalar
1445 inline wxTimeSpan& Multiply(int n);
1446 // multiply timespan by a scalar
1447 wxTimeSpan& operator*=(int n) { return Multiply(n); }
1448 inline wxTimeSpan operator*(int n) const
1449 {
1450 return wxTimeSpan(*this).Multiply(n);
1451 }
1452
1453 // return this timespan with opposite sign
1454 wxTimeSpan Negate() const { return wxTimeSpan(-GetValue()); }
1455 // negate the value of the timespan
1456 wxTimeSpan& Neg() { m_diff = -GetValue(); return *this; }
1457 // negate the value of the timespan
1458 wxTimeSpan& operator-() { return Neg(); }
1459
1460 // return the absolute value of the timespan: does _not_ modify the
1461 // object
1462 inline wxTimeSpan Abs() const;
1463
1464 // there is intentionally no division because we don't want to
1465 // introduce rounding errors in time calculations
1466
1467 // comparaison (see also operator versions below)
1468 // ------------------------------------------------------------------------
1469
1470 // is the timespan null?
1471 bool IsNull() const { return m_diff == 0l; }
1472 // returns true if the timespan is null
1473 bool operator!() const { return !IsNull(); }
1474
1475 // is the timespan positive?
1476 bool IsPositive() const { return m_diff > 0l; }
1477
1478 // is the timespan negative?
1479 bool IsNegative() const { return m_diff < 0l; }
1480
1481 // are two timespans equal?
1482 inline bool IsEqualTo(const wxTimeSpan& ts) const;
1483 // compare two timestamps: works with the absolute values, i.e. -2
1484 // hours is longer than 1 hour. Also, it will return false if the
1485 // timespans are equal in absolute value.
1486 inline bool IsLongerThan(const wxTimeSpan& ts) const;
1487 // compare two timestamps: works with the absolute values, i.e. 1
1488 // hour is shorter than -2 hours. Also, it will return false if the
1489 // timespans are equal in absolute value.
1490 bool IsShorterThan(const wxTimeSpan& t) const;
1491
1492 inline bool operator<(const wxTimeSpan &ts) const
1493 {
1494 return GetValue() < ts.GetValue();
1495 }
1496
1497 inline bool operator<=(const wxTimeSpan &ts) const
1498 {
1499 return GetValue() <= ts.GetValue();
1500 }
1501
1502 inline bool operator>(const wxTimeSpan &ts) const
1503 {
1504 return GetValue() > ts.GetValue();
1505 }
1506
1507 inline bool operator>=(const wxTimeSpan &ts) const
1508 {
1509 return GetValue() >= ts.GetValue();
1510 }
1511
1512 inline bool operator==(const wxTimeSpan &ts) const
1513 {
1514 return GetValue() == ts.GetValue();
1515 }
1516
1517 inline bool operator!=(const wxTimeSpan &ts) const
1518 {
1519 return GetValue() != ts.GetValue();
1520 }
1521
1522 // breaking into days, hours, minutes and seconds
1523 // ------------------------------------------------------------------------
1524
1525 // get the max number of weeks in this timespan
1526 inline int GetWeeks() const;
1527 // get the max number of days in this timespan
1528 inline int GetDays() const;
1529 // get the max number of hours in this timespan
1530 inline int GetHours() const;
1531 // get the max number of minutes in this timespan
1532 inline int GetMinutes() const;
1533 // get the max number of seconds in this timespan
1534 inline wxLongLong GetSeconds() const;
1535 // get the number of milliseconds in this timespan
1536 wxLongLong GetMilliseconds() const { return m_diff; }
1537
1538 // conversion to text
1539 // ------------------------------------------------------------------------
1540
1541 // this function accepts strftime()-like format string (default
1542 // argument corresponds to the preferred date and time representation
1543 // for the current locale) and returns the string containing the
1544 // resulting text representation. Notice that only some of format
1545 // specifiers valid for wxDateTime are valid for wxTimeSpan: hours,
1546 // minutes and seconds make sense, but not "PM/AM" string for example.
1547 wxString Format(const wxString& format = wxDefaultTimeSpanFormat) const;
1548
1549 // implementation
1550 // ------------------------------------------------------------------------
1551
1552 // construct from internal representation
1553 wxTimeSpan(const wxLongLong& diff) { m_diff = diff; }
1554
1555 // get the internal representation
1556 wxLongLong GetValue() const { return m_diff; }
1557
1558 private:
1559 // the (signed) time span in milliseconds
1560 wxLongLong m_diff;
1561 };
1562
1563 // ----------------------------------------------------------------------------
1564 // This class is a "logical time span" and is useful for implementing program
1565 // logic for such things as "add one month to the date" which, in general,
1566 // doesn't mean to add 60*60*24*31 seconds to it, but to take the same date
1567 // the next month (to understand that this is indeed different consider adding
1568 // one month to Feb, 15 - we want to get Mar, 15, of course).
1569 //
1570 // When adding a month to the date, all lesser components (days, hours, ...)
1571 // won't be changed unless the resulting date would be invalid: for example,
1572 // Jan 31 + 1 month will be Feb 28, not (non existing) Feb 31.
1573 //
1574 // Because of this feature, adding and subtracting back again the same
1575 // wxDateSpan will *not*, in general give back the original date: Feb 28 - 1
1576 // month will be Jan 28, not Jan 31!
1577 //
1578 // wxDateSpan can be either positive or negative. They may be
1579 // multiplied by scalars which multiply all deltas by the scalar: i.e. 2*(1
1580 // month and 1 day) is 2 months and 2 days. They can be added together and
1581 // with wxDateTime or wxTimeSpan, but the type of result is different for each
1582 // case.
1583 //
1584 // Beware about weeks: if you specify both weeks and days, the total number of
1585 // days added will be 7*weeks + days! See also GetTotalDays() function.
1586 //
1587 // Equality operators are defined for wxDateSpans. Two datespans are equal if
1588 // they both give the same target date when added to *every* source date.
1589 // Thus wxDateSpan::Months(1) is not equal to wxDateSpan::Days(30), because
1590 // they not give the same date when added to 1 Feb. But wxDateSpan::Days(14) is
1591 // equal to wxDateSpan::Weeks(2)
1592 //
1593 // Finally, notice that for adding hours, minutes &c you don't need this
1594 // class: wxTimeSpan will do the job because there are no subtleties
1595 // associated with those.
1596 // ----------------------------------------------------------------------------
1597
1598 class WXDLLIMPEXP_BASE wxDateSpan
1599 {
1600 public:
1601 // constructors
1602 // ------------------------------------------------------------------------
1603
1604 // this many years/months/weeks/days
1605 wxDateSpan(int years = 0, int months = 0, int weeks = 0, int days = 0)
1606 {
1607 m_years = years;
1608 m_months = months;
1609 m_weeks = weeks;
1610 m_days = days;
1611 }
1612
1613 // get an object for the given number of days
1614 static wxDateSpan Days(int days) { return wxDateSpan(0, 0, 0, days); }
1615 static wxDateSpan Day() { return Days(1); }
1616
1617 // get an object for the given number of weeks
1618 static wxDateSpan Weeks(int weeks) { return wxDateSpan(0, 0, weeks, 0); }
1619 static wxDateSpan Week() { return Weeks(1); }
1620
1621 // get an object for the given number of months
1622 static wxDateSpan Months(int mon) { return wxDateSpan(0, mon, 0, 0); }
1623 static wxDateSpan Month() { return Months(1); }
1624
1625 // get an object for the given number of years
1626 static wxDateSpan Years(int years) { return wxDateSpan(years, 0, 0, 0); }
1627 static wxDateSpan Year() { return Years(1); }
1628
1629 // default copy ctor is ok
1630
1631 // no dtor
1632
1633 // accessors (all SetXXX() return the (modified) wxDateSpan object)
1634 // ------------------------------------------------------------------------
1635
1636 // set number of years
1637 wxDateSpan& SetYears(int n) { m_years = n; return *this; }
1638 // set number of months
1639 wxDateSpan& SetMonths(int n) { m_months = n; return *this; }
1640 // set number of weeks
1641 wxDateSpan& SetWeeks(int n) { m_weeks = n; return *this; }
1642 // set number of days
1643 wxDateSpan& SetDays(int n) { m_days = n; return *this; }
1644
1645 // get number of years
1646 int GetYears() const { return m_years; }
1647 // get number of months
1648 int GetMonths() const { return m_months; }
1649 // get number of weeks
1650 int GetWeeks() const { return m_weeks; }
1651 // get number of days
1652 int GetDays() const { return m_days; }
1653 // returns 7*GetWeeks() + GetDays()
1654 int GetTotalDays() const { return 7*m_weeks + m_days; }
1655
1656 // arithmetics with date spans (see also below for more operators)
1657 // ------------------------------------------------------------------------
1658
1659 // return sum of two date spans
1660 inline wxDateSpan Add(const wxDateSpan& other) const;
1661 // add another wxDateSpan to us
1662 inline wxDateSpan& Add(const wxDateSpan& other);
1663 // add another wxDateSpan to us
1664 inline wxDateSpan& operator+=(const wxDateSpan& other);
1665 inline wxDateSpan operator+(const wxDateSpan& ds) const
1666 {
1667 return wxDateSpan(GetYears() + ds.GetYears(),
1668 GetMonths() + ds.GetMonths(),
1669 GetWeeks() + ds.GetWeeks(),
1670 GetDays() + ds.GetDays());
1671 }
1672
1673 // return difference of two date spans
1674 inline wxDateSpan Subtract(const wxDateSpan& other) const;
1675 // subtract another wxDateSpan from us
1676 inline wxDateSpan& Subtract(const wxDateSpan& other);
1677 // subtract another wxDateSpan from us
1678 inline wxDateSpan& operator-=(const wxDateSpan& other);
1679 inline wxDateSpan operator-(const wxDateSpan& ds) const
1680 {
1681 return wxDateSpan(GetYears() - ds.GetYears(),
1682 GetMonths() - ds.GetMonths(),
1683 GetWeeks() - ds.GetWeeks(),
1684 GetDays() - ds.GetDays());
1685 }
1686
1687 // return a copy of this time span with changed sign
1688 inline wxDateSpan Negate() const;
1689 // inverse the sign of this timespan
1690 inline wxDateSpan& Neg();
1691 // inverse the sign of this timespan
1692 wxDateSpan& operator-() { return Neg(); }
1693
1694 // return the date span proportional to this one with given factor
1695 inline wxDateSpan Multiply(int factor) const;
1696 // multiply all components by a (signed) number
1697 inline wxDateSpan& Multiply(int factor);
1698 // multiply all components by a (signed) number
1699 inline wxDateSpan& operator*=(int factor) { return Multiply(factor); }
1700 inline wxDateSpan operator*(int n) const
1701 {
1702 return wxDateSpan(*this).Multiply(n);
1703 }
1704
1705 // ds1 == d2 if and only if for every wxDateTime t t + ds1 == t + ds2
1706 inline bool operator==(const wxDateSpan& ds) const
1707 {
1708 return GetYears() == ds.GetYears() &&
1709 GetMonths() == ds.GetMonths() &&
1710 GetTotalDays() == ds.GetTotalDays();
1711 }
1712
1713 inline bool operator!=(const wxDateSpan& ds) const
1714 {
1715 return !(*this == ds);
1716 }
1717
1718 private:
1719 int m_years,
1720 m_months,
1721 m_weeks,
1722 m_days;
1723 };
1724
1725 // ----------------------------------------------------------------------------
1726 // wxDateTimeArray: array of dates.
1727 // ----------------------------------------------------------------------------
1728
1729 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxDateTime, wxDateTimeArray, WXDLLIMPEXP_BASE);
1730
1731 // ----------------------------------------------------------------------------
1732 // wxDateTimeHolidayAuthority: an object of this class will decide whether a
1733 // given date is a holiday and is used by all functions working with "work
1734 // days".
1735 //
1736 // NB: the base class is an ABC, derived classes must implement the pure
1737 // virtual methods to work with the holidays they correspond to.
1738 // ----------------------------------------------------------------------------
1739
1740 class WXDLLIMPEXP_FWD_BASE wxDateTimeHolidayAuthority;
1741 WX_DEFINE_USER_EXPORTED_ARRAY_PTR(wxDateTimeHolidayAuthority *,
1742 wxHolidayAuthoritiesArray,
1743 class WXDLLIMPEXP_BASE);
1744
1745 class wxDateTimeHolidaysModule;
1746 class WXDLLIMPEXP_BASE wxDateTimeHolidayAuthority
1747 {
1748 friend class wxDateTimeHolidaysModule;
1749 public:
1750 // returns true if the given date is a holiday
1751 static bool IsHoliday(const wxDateTime& dt);
1752
1753 // fills the provided array with all holidays in the given range, returns
1754 // the number of them
1755 static size_t GetHolidaysInRange(const wxDateTime& dtStart,
1756 const wxDateTime& dtEnd,
1757 wxDateTimeArray& holidays);
1758
1759 // clear the list of holiday authorities
1760 static void ClearAllAuthorities();
1761
1762 // add a new holiday authority (the pointer will be deleted by
1763 // wxDateTimeHolidayAuthority)
1764 static void AddAuthority(wxDateTimeHolidayAuthority *auth);
1765
1766 // the base class must have a virtual dtor
1767 virtual ~wxDateTimeHolidayAuthority();
1768
1769 protected:
1770 // this function is called to determine whether a given day is a holiday
1771 virtual bool DoIsHoliday(const wxDateTime& dt) const = 0;
1772
1773 // this function should fill the array with all holidays between the two
1774 // given dates - it is implemented in the base class, but in a very
1775 // inefficient way (it just iterates over all days and uses IsHoliday() for
1776 // each of them), so it must be overridden in the derived class where the
1777 // base class version may be explicitly used if needed
1778 //
1779 // returns the number of holidays in the given range and fills holidays
1780 // array
1781 virtual size_t DoGetHolidaysInRange(const wxDateTime& dtStart,
1782 const wxDateTime& dtEnd,
1783 wxDateTimeArray& holidays) const = 0;
1784
1785 private:
1786 // all holiday authorities
1787 static wxHolidayAuthoritiesArray ms_authorities;
1788 };
1789
1790 // the holidays for this class are all Saturdays and Sundays
1791 class WXDLLIMPEXP_BASE wxDateTimeWorkDays : public wxDateTimeHolidayAuthority
1792 {
1793 protected:
1794 virtual bool DoIsHoliday(const wxDateTime& dt) const;
1795 virtual size_t DoGetHolidaysInRange(const wxDateTime& dtStart,
1796 const wxDateTime& dtEnd,
1797 wxDateTimeArray& holidays) const;
1798 };
1799
1800 // ============================================================================
1801 // inline functions implementation
1802 // ============================================================================
1803
1804 // ----------------------------------------------------------------------------
1805 // private macros
1806 // ----------------------------------------------------------------------------
1807
1808 #define MILLISECONDS_PER_DAY 86400000l
1809
1810 // some broken compilers (HP-UX CC) refuse to compile the "normal" version, but
1811 // using a temp variable always might prevent other compilers from optimising
1812 // it away - hence use of this ugly macro
1813 #ifndef __HPUX__
1814 #define MODIFY_AND_RETURN(op) return wxDateTime(*this).op
1815 #else
1816 #define MODIFY_AND_RETURN(op) wxDateTime dt(*this); dt.op; return dt
1817 #endif
1818
1819 // ----------------------------------------------------------------------------
1820 // wxDateTime construction
1821 // ----------------------------------------------------------------------------
1822
1823 inline bool wxDateTime::IsInStdRange() const
1824 {
1825 return m_time >= 0l && (m_time / TIME_T_FACTOR) < LONG_MAX;
1826 }
1827
1828 /* static */
1829 inline wxDateTime wxDateTime::Now()
1830 {
1831 struct tm tmstruct;
1832 return wxDateTime(*GetTmNow(&tmstruct));
1833 }
1834
1835 /* static */
1836 inline wxDateTime wxDateTime::Today()
1837 {
1838 wxDateTime dt(Now());
1839 dt.ResetTime();
1840
1841 return dt;
1842 }
1843
1844 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
1845 inline wxDateTime& wxDateTime::Set(time_t timet)
1846 {
1847 // assign first to avoid long multiplication overflow!
1848 m_time = timet - WX_TIME_BASE_OFFSET ;
1849 m_time *= TIME_T_FACTOR;
1850
1851 return *this;
1852 }
1853 #endif
1854
1855 inline wxDateTime& wxDateTime::SetToCurrent()
1856 {
1857 *this = Now();
1858 return *this;
1859 }
1860
1861 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
1862 inline wxDateTime::wxDateTime(time_t timet)
1863 {
1864 Set(timet);
1865 }
1866 #endif
1867
1868 inline wxDateTime::wxDateTime(const struct tm& tm)
1869 {
1870 Set(tm);
1871 }
1872
1873 inline wxDateTime::wxDateTime(const Tm& tm)
1874 {
1875 Set(tm);
1876 }
1877
1878 inline wxDateTime::wxDateTime(double jdn)
1879 {
1880 Set(jdn);
1881 }
1882
1883 inline wxDateTime& wxDateTime::Set(const Tm& tm)
1884 {
1885 wxASSERT_MSG( tm.IsValid(), _T("invalid broken down date/time") );
1886
1887 return Set(tm.mday, (Month)tm.mon, tm.year,
1888 tm.hour, tm.min, tm.sec, tm.msec);
1889 }
1890
1891 inline wxDateTime::wxDateTime(wxDateTime_t hour,
1892 wxDateTime_t minute,
1893 wxDateTime_t second,
1894 wxDateTime_t millisec)
1895 {
1896 Set(hour, minute, second, millisec);
1897 }
1898
1899 inline wxDateTime::wxDateTime(wxDateTime_t day,
1900 Month month,
1901 int year,
1902 wxDateTime_t hour,
1903 wxDateTime_t minute,
1904 wxDateTime_t second,
1905 wxDateTime_t millisec)
1906 {
1907 Set(day, month, year, hour, minute, second, millisec);
1908 }
1909
1910 // ----------------------------------------------------------------------------
1911 // wxDateTime accessors
1912 // ----------------------------------------------------------------------------
1913
1914 inline wxLongLong wxDateTime::GetValue() const
1915 {
1916 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1917
1918 return m_time;
1919 }
1920
1921 inline time_t wxDateTime::GetTicks() const
1922 {
1923 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1924 if ( !IsInStdRange() )
1925 {
1926 return (time_t)-1;
1927 }
1928
1929 return (time_t)((m_time / (long)TIME_T_FACTOR).ToLong()) + WX_TIME_BASE_OFFSET;
1930 }
1931
1932 inline bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
1933 Month month,
1934 int year)
1935 {
1936 return SetToWeekDay(weekday, -1, month, year);
1937 }
1938
1939 inline wxDateTime
1940 wxDateTime::GetWeekDayInSameWeek(WeekDay weekday,
1941 WeekFlags WXUNUSED(flags)) const
1942 {
1943 MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday) );
1944 }
1945
1946 inline wxDateTime wxDateTime::GetNextWeekDay(WeekDay weekday) const
1947 {
1948 MODIFY_AND_RETURN( SetToNextWeekDay(weekday) );
1949 }
1950
1951 inline wxDateTime wxDateTime::GetPrevWeekDay(WeekDay weekday) const
1952 {
1953 MODIFY_AND_RETURN( SetToPrevWeekDay(weekday) );
1954 }
1955
1956 inline wxDateTime wxDateTime::GetWeekDay(WeekDay weekday,
1957 int n,
1958 Month month,
1959 int year) const
1960 {
1961 wxDateTime dt(*this);
1962
1963 return dt.SetToWeekDay(weekday, n, month, year) ? dt : wxInvalidDateTime;
1964 }
1965
1966 inline wxDateTime wxDateTime::GetLastWeekDay(WeekDay weekday,
1967 Month month,
1968 int year)
1969 {
1970 wxDateTime dt(*this);
1971
1972 return dt.SetToLastWeekDay(weekday, month, year) ? dt : wxInvalidDateTime;
1973 }
1974
1975 inline wxDateTime wxDateTime::GetLastMonthDay(Month month, int year) const
1976 {
1977 MODIFY_AND_RETURN( SetToLastMonthDay(month, year) );
1978 }
1979
1980 inline wxDateTime wxDateTime::GetYearDay(wxDateTime_t yday) const
1981 {
1982 MODIFY_AND_RETURN( SetToYearDay(yday) );
1983 }
1984
1985 // ----------------------------------------------------------------------------
1986 // wxDateTime comparison
1987 // ----------------------------------------------------------------------------
1988
1989 inline bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
1990 {
1991 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
1992
1993 return m_time == datetime.m_time;
1994 }
1995
1996 inline bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
1997 {
1998 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
1999
2000 return m_time < datetime.m_time;
2001 }
2002
2003 inline bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
2004 {
2005 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
2006
2007 return m_time > datetime.m_time;
2008 }
2009
2010 inline bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1,
2011 const wxDateTime& t2) const
2012 {
2013 // no need for assert, will be checked by the functions we call
2014 return IsLaterThan(t1) && IsEarlierThan(t2);
2015 }
2016
2017 inline bool wxDateTime::IsBetween(const wxDateTime& t1,
2018 const wxDateTime& t2) const
2019 {
2020 // no need for assert, will be checked by the functions we call
2021 return IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2);
2022 }
2023
2024 inline bool wxDateTime::IsSameDate(const wxDateTime& dt) const
2025 {
2026 Tm tm1 = GetTm(),
2027 tm2 = dt.GetTm();
2028
2029 return tm1.year == tm2.year &&
2030 tm1.mon == tm2.mon &&
2031 tm1.mday == tm2.mday;
2032 }
2033
2034 inline bool wxDateTime::IsSameTime(const wxDateTime& dt) const
2035 {
2036 // notice that we can't do something like this:
2037 //
2038 // m_time % MILLISECONDS_PER_DAY == dt.m_time % MILLISECONDS_PER_DAY
2039 //
2040 // because we have also to deal with (possibly) different DST settings!
2041 Tm tm1 = GetTm(),
2042 tm2 = dt.GetTm();
2043
2044 return tm1.hour == tm2.hour &&
2045 tm1.min == tm2.min &&
2046 tm1.sec == tm2.sec &&
2047 tm1.msec == tm2.msec;
2048 }
2049
2050 inline bool wxDateTime::IsEqualUpTo(const wxDateTime& dt,
2051 const wxTimeSpan& ts) const
2052 {
2053 return IsBetween(dt.Subtract(ts), dt.Add(ts));
2054 }
2055
2056 // ----------------------------------------------------------------------------
2057 // wxDateTime arithmetics
2058 // ----------------------------------------------------------------------------
2059
2060 inline wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const
2061 {
2062 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
2063
2064 return wxDateTime(m_time + diff.GetValue());
2065 }
2066
2067 inline wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
2068 {
2069 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
2070
2071 m_time += diff.GetValue();
2072
2073 return *this;
2074 }
2075
2076 inline wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
2077 {
2078 return Add(diff);
2079 }
2080
2081 inline wxDateTime wxDateTime::Subtract(const wxTimeSpan& diff) const
2082 {
2083 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
2084
2085 return wxDateTime(m_time - diff.GetValue());
2086 }
2087
2088 inline wxDateTime& wxDateTime::Subtract(const wxTimeSpan& diff)
2089 {
2090 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
2091
2092 m_time -= diff.GetValue();
2093
2094 return *this;
2095 }
2096
2097 inline wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
2098 {
2099 return Subtract(diff);
2100 }
2101
2102 inline wxTimeSpan wxDateTime::Subtract(const wxDateTime& datetime) const
2103 {
2104 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
2105
2106 return wxTimeSpan(GetValue() - datetime.GetValue());
2107 }
2108
2109 inline wxTimeSpan wxDateTime::operator-(const wxDateTime& dt2) const
2110 {
2111 return this->Subtract(dt2);
2112 }
2113
2114 inline wxDateTime wxDateTime::Add(const wxDateSpan& diff) const
2115 {
2116 return wxDateTime(*this).Add(diff);
2117 }
2118
2119 inline wxDateTime& wxDateTime::Subtract(const wxDateSpan& diff)
2120 {
2121 return Add(diff.Negate());
2122 }
2123
2124 inline wxDateTime wxDateTime::Subtract(const wxDateSpan& diff) const
2125 {
2126 return wxDateTime(*this).Subtract(diff);
2127 }
2128
2129 inline wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
2130 {
2131 return Subtract(diff);
2132 }
2133
2134 inline wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
2135 {
2136 return Add(diff);
2137 }
2138
2139 // ----------------------------------------------------------------------------
2140 // wxDateTime and timezones
2141 // ----------------------------------------------------------------------------
2142
2143 inline wxDateTime
2144 wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz, bool noDST) const
2145 {
2146 MODIFY_AND_RETURN( MakeTimezone(tz, noDST) );
2147 }
2148
2149 inline wxDateTime
2150 wxDateTime::FromTimezone(const wxDateTime::TimeZone& tz, bool noDST) const
2151 {
2152 MODIFY_AND_RETURN( MakeFromTimezone(tz, noDST) );
2153 }
2154
2155 // ----------------------------------------------------------------------------
2156 // wxTimeSpan construction
2157 // ----------------------------------------------------------------------------
2158
2159 inline wxTimeSpan::wxTimeSpan(long hours,
2160 long minutes,
2161 wxLongLong seconds,
2162 wxLongLong milliseconds)
2163 {
2164 // assign first to avoid precision loss
2165 m_diff = hours;
2166 m_diff *= 60l;
2167 m_diff += minutes;
2168 m_diff *= 60l;
2169 m_diff += seconds;
2170 m_diff *= 1000l;
2171 m_diff += milliseconds;
2172 }
2173
2174 // ----------------------------------------------------------------------------
2175 // wxTimeSpan accessors
2176 // ----------------------------------------------------------------------------
2177
2178 inline wxLongLong wxTimeSpan::GetSeconds() const
2179 {
2180 return m_diff / 1000l;
2181 }
2182
2183 inline int wxTimeSpan::GetMinutes() const
2184 {
2185 // explicit cast to int suppresses a warning with CodeWarrior and possibly
2186 // others (changing the return type to long from int is impossible in 2.8)
2187 return (int)((GetSeconds() / 60l).GetLo());
2188 }
2189
2190 inline int wxTimeSpan::GetHours() const
2191 {
2192 return GetMinutes() / 60;
2193 }
2194
2195 inline int wxTimeSpan::GetDays() const
2196 {
2197 return GetHours() / 24;
2198 }
2199
2200 inline int wxTimeSpan::GetWeeks() const
2201 {
2202 return GetDays() / 7;
2203 }
2204
2205 // ----------------------------------------------------------------------------
2206 // wxTimeSpan arithmetics
2207 // ----------------------------------------------------------------------------
2208
2209 inline wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const
2210 {
2211 return wxTimeSpan(m_diff + diff.GetValue());
2212 }
2213
2214 inline wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
2215 {
2216 m_diff += diff.GetValue();
2217
2218 return *this;
2219 }
2220
2221 inline wxTimeSpan wxTimeSpan::Subtract(const wxTimeSpan& diff) const
2222 {
2223 return wxTimeSpan(m_diff - diff.GetValue());
2224 }
2225
2226 inline wxTimeSpan& wxTimeSpan::Subtract(const wxTimeSpan& diff)
2227 {
2228 m_diff -= diff.GetValue();
2229
2230 return *this;
2231 }
2232
2233 inline wxTimeSpan& wxTimeSpan::Multiply(int n)
2234 {
2235 m_diff *= (long)n;
2236
2237 return *this;
2238 }
2239
2240 inline wxTimeSpan wxTimeSpan::Multiply(int n) const
2241 {
2242 return wxTimeSpan(m_diff * (long)n);
2243 }
2244
2245 inline wxTimeSpan wxTimeSpan::Abs() const
2246 {
2247 return wxTimeSpan(GetValue().Abs());
2248 }
2249
2250 inline bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
2251 {
2252 return GetValue() == ts.GetValue();
2253 }
2254
2255 inline bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
2256 {
2257 return GetValue().Abs() > ts.GetValue().Abs();
2258 }
2259
2260 inline bool wxTimeSpan::IsShorterThan(const wxTimeSpan& ts) const
2261 {
2262 return GetValue().Abs() < ts.GetValue().Abs();
2263 }
2264
2265 // ----------------------------------------------------------------------------
2266 // wxDateSpan
2267 // ----------------------------------------------------------------------------
2268
2269 inline wxDateSpan& wxDateSpan::operator+=(const wxDateSpan& other)
2270 {
2271 m_years += other.m_years;
2272 m_months += other.m_months;
2273 m_weeks += other.m_weeks;
2274 m_days += other.m_days;
2275
2276 return *this;
2277 }
2278
2279 inline wxDateSpan& wxDateSpan::Add(const wxDateSpan& other)
2280 {
2281 return *this += other;
2282 }
2283
2284 inline wxDateSpan wxDateSpan::Add(const wxDateSpan& other) const
2285 {
2286 wxDateSpan ds(*this);
2287 ds.Add(other);
2288 return ds;
2289 }
2290
2291 inline wxDateSpan& wxDateSpan::Multiply(int factor)
2292 {
2293 m_years *= factor;
2294 m_months *= factor;
2295 m_weeks *= factor;
2296 m_days *= factor;
2297
2298 return *this;
2299 }
2300
2301 inline wxDateSpan wxDateSpan::Multiply(int factor) const
2302 {
2303 wxDateSpan ds(*this);
2304 ds.Multiply(factor);
2305 return ds;
2306 }
2307
2308 inline wxDateSpan wxDateSpan::Negate() const
2309 {
2310 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
2311 }
2312
2313 inline wxDateSpan& wxDateSpan::Neg()
2314 {
2315 m_years = -m_years;
2316 m_months = -m_months;
2317 m_weeks = -m_weeks;
2318 m_days = -m_days;
2319
2320 return *this;
2321 }
2322
2323 inline wxDateSpan& wxDateSpan::operator-=(const wxDateSpan& other)
2324 {
2325 return *this += other.Negate();
2326 }
2327
2328 inline wxDateSpan& wxDateSpan::Subtract(const wxDateSpan& other)
2329 {
2330 return *this -= other;
2331 }
2332
2333 inline wxDateSpan wxDateSpan::Subtract(const wxDateSpan& other) const
2334 {
2335 wxDateSpan ds(*this);
2336 ds.Subtract(other);
2337 return ds;
2338 }
2339
2340 #undef MILLISECONDS_PER_DAY
2341
2342 #undef MODIFY_AND_RETURN
2343
2344 // ============================================================================
2345 // binary operators
2346 // ============================================================================
2347
2348 // ----------------------------------------------------------------------------
2349 // wxTimeSpan operators
2350 // ----------------------------------------------------------------------------
2351
2352 wxTimeSpan WXDLLIMPEXP_BASE operator*(int n, const wxTimeSpan& ts);
2353
2354 // ----------------------------------------------------------------------------
2355 // wxDateSpan
2356 // ----------------------------------------------------------------------------
2357
2358 wxDateSpan WXDLLIMPEXP_BASE operator*(int n, const wxDateSpan& ds);
2359
2360 // ============================================================================
2361 // other helper functions
2362 // ============================================================================
2363
2364 // ----------------------------------------------------------------------------
2365 // iteration helpers: can be used to write a for loop over enum variable like
2366 // this:
2367 // for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) )
2368 // ----------------------------------------------------------------------------
2369
2370 WXDLLIMPEXP_BASE void wxNextMonth(wxDateTime::Month& m);
2371 WXDLLIMPEXP_BASE void wxPrevMonth(wxDateTime::Month& m);
2372 WXDLLIMPEXP_BASE void wxNextWDay(wxDateTime::WeekDay& wd);
2373 WXDLLIMPEXP_BASE void wxPrevWDay(wxDateTime::WeekDay& wd);
2374
2375 #endif // wxUSE_DATETIME
2376
2377 #endif // _WX_DATETIME_H