wxDateTime::ParseFormat() and ParseTime() added (compile but don't work)
[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 license
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_DATETIME_H
14 #define _WX_DATETIME_H
15
16 #ifdef __GNUG__
17 #pragma interface "datetime.h"
18 #endif
19
20 #include <time.h>
21 #include <limits.h> // for INT_MIN
22
23 #include "wx/longlong.h"
24
25 class WXDLLEXPORT wxDateTime;
26 class WXDLLEXPORT wxTimeSpan;
27 class WXDLLEXPORT wxDateSpan;
28
29 // don't use inline functions in debug builds - we don't care about
30 // performances and this only leads to increased rebuild time (because every
31 // time an inline method is changed, all files including the header must be
32 // rebuilt)
33 #ifdef __WXDEBUG__
34 #define inline
35 #endif // Debug
36
37 /*
38 * TODO Well, everything :-)
39 *
40 * + 1. Time zones with minutes (make TimeZone a class)
41 * 2. getdate() function like under Solaris
42 * + 3. text conversion for wxDateSpan
43 * 4. pluggable modules for the workdays calculations
44 */
45
46 /*
47 The three (main) classes declared in this header represent:
48
49 1. An absolute moment in the time (wxDateTime)
50 2. A difference between two moments in the time, positive or negative
51 (wxTimeSpan)
52 3. A logical difference between two dates expressed in
53 years/months/weeks/days (wxDateSpan)
54
55 The following arithmetic operations are permitted (all others are not):
56
57 addition
58 --------
59
60 wxDateTime + wxTimeSpan = wxDateTime
61 wxDateTime + wxDateSpan = wxDateTime
62 wxTimeSpan + wxTimeSpan = wxTimeSpan
63 wxDateSpan + wxDateSpan = wxDateSpan
64
65 substraction
66 ------------
67 wxDateTime - wxDateTime = wxTimeSpan
68 wxDateTime - wxTimeSpan = wxDateTime
69 wxDateTime - wxDateSpan = wxDateTime
70 wxTimeSpan - wxTimeSpan = wxTimeSpan
71 wxDateSpan - wxDateSpan = wxDateSpan
72
73 multiplication
74 --------------
75 wxTimeSpan * number = wxTimeSpan
76 number * wxTimeSpan = wxTimeSpan
77 wxDateSpan * number = wxDateSpan
78 number * wxDateSpan = wxDateSpan
79
80 unitary minus
81 -------------
82 -wxTimeSpan = wxTimeSpan
83 -wxDateSpan = wxDateSpan
84
85 For each binary operation OP (+, -, *) we have the following operatorOP=() as
86 a method and the method with a symbolic name OPER (Add, Substract, Multiply)
87 as a synonym for it and another const method with the same name which returns
88 the changed copy of the object and operatorOP() as a global function which is
89 implemented in terms of the const version of OPEN. For the unary - we have
90 operator-() as a method, Neg() as synonym for it and Negate() which returns
91 the copy of the object with the changed sign.
92 */
93
94 // ----------------------------------------------------------------------------
95 // wxDateTime represents an absolute moment in the time
96 // ----------------------------------------------------------------------------
97
98 class WXDLLEXPORT wxDateTime
99 {
100 public:
101 // types
102 // ------------------------------------------------------------------------
103
104 // a small unsigned integer type for storing things like minutes,
105 // seconds &c. It should be at least short (i.e. not char) to contain
106 // the number of milliseconds - it may also be 'int' because there is
107 // no size penalty associated with it in our code, we don't store any
108 // data in this format
109 typedef unsigned short wxDateTime_t;
110
111 // constants
112 // ------------------------------------------------------------------------
113
114 // the timezones
115 enum TZ
116 {
117 // the time in the current time zone
118 Local,
119
120 // zones from GMT (= Greenwhich Mean Time): they're guaranteed to be
121 // consequent numbers, so writing something like `GMT0 + offset' is
122 // safe if abs(offset) <= 12
123
124 // underscore stands for minus
125 GMT_12, GMT_11, GMT_10, GMT_9, GMT_8, GMT_7,
126 GMT_6, GMT_5, GMT_4, GMT_3, GMT_2, GMT_1,
127 GMT0,
128 GMT1, GMT2, GMT3, GMT4, GMT5, GMT6,
129 GMT7, GMT8, GMT9, GMT10, GMT11, GMT12,
130 // Note that GMT12 and GMT_12 are not the same: there is a difference
131 // of exactly one day between them
132
133 // some symbolic names for TZ
134
135 // Europe
136 WET = GMT0, // Western Europe Time
137 WEST = GMT1, // Western Europe Summer Time
138 CET = GMT1, // Central Europe Time
139 CEST = GMT2, // Central Europe Summer Time
140 EET = GMT2, // Eastern Europe Time
141 EEST = GMT3, // Eastern Europe Summer Time
142 MSK = GMT3, // Moscow Time
143 MSD = GMT4, // Moscow Summer Time
144
145 // US and Canada
146 AST = GMT_4, // Atlantic Standard Time
147 ADT = GMT_3, // Atlantic Daylight Time
148 EST = GMT_5, // Eastern Standard Time
149 EDT = GMT_4, // Eastern Daylight Saving Time
150 CST = GMT_6, // Central Standard Time
151 CDT = GMT_5, // Central Daylight Saving Time
152 MST = GMT_7, // Mountain Standard Time
153 MDT = GMT_6, // Mountain Daylight Saving Time
154 PST = GMT_8, // Pacific Standard Time
155 PDT = GMT_7, // Pacific Daylight Saving Time
156 HST = GMT_10, // Hawaiian Standard Time
157 AKST = GMT_9, // Alaska Standard Time
158 AKDT = GMT_8, // Alaska Daylight Saving Time
159
160 // Australia
161
162 A_WST = GMT8, // Western Standard Time
163 A_CST = GMT12 + 1, // Central Standard Time (+9.5)
164 A_EST = GMT10, // Eastern Standard Time
165 A_ESST = GMT11, // Eastern Summer Time
166
167 // TODO add more symbolic timezone names here
168
169 // Universal Coordinated Time = the new and politically correct name
170 // for GMT
171 UTC = GMT0
172 };
173
174 // the calendar systems we know about: notice that it's valid (for
175 // this classes purpose anyhow) to work with any of these calendars
176 // even with the dates before the historical appearance of the
177 // calendar
178 enum Calendar
179 {
180 Gregorian, // current calendar
181 Julian // calendar in use since -45 until the 1582 (or later)
182
183 // TODO Hebrew, Chinese, Maya, ... (just kidding) (or then may be not?)
184 };
185
186 // these values only are used to identify the different dates of
187 // adoption of the Gregorian calendar (see IsGregorian())
188 //
189 // All data and comments taken verbatim from "The Calendar FAQ (v 2.0)"
190 // by Claus Tøndering, http://www.pip.dknet.dk/~c-t/calendar.html
191 // except for the comments "we take".
192 //
193 // Symbol "->" should be read as "was followed by" in the comments
194 // which follow.
195 enum GregorianAdoption
196 {
197 Gr_Unknown, // no data for this country or it's too uncertain to use
198 Gr_Standard, // on the day 0 of Gregorian calendar: 15 Oct 1582
199
200 Gr_Alaska, // Oct 1867 when Alaska became part of the USA
201 Gr_Albania, // Dec 1912
202
203 Gr_Austria = Gr_Unknown, // Different regions on different dates
204 Gr_Austria_Brixen, // 5 Oct 1583 -> 16 Oct 1583
205 Gr_Austria_Salzburg = Gr_Austria_Brixen,
206 Gr_Austria_Tyrol = Gr_Austria_Brixen,
207 Gr_Austria_Carinthia, // 14 Dec 1583 -> 25 Dec 1583
208 Gr_Austria_Styria = Gr_Austria_Carinthia,
209
210 Gr_Belgium, // Then part of the Netherlands
211
212 Gr_Bulgaria = Gr_Unknown, // Unknown precisely (from 1915 to 1920)
213 Gr_Bulgaria_1, // 18 Mar 1916 -> 1 Apr 1916
214 Gr_Bulgaria_2, // 31 Mar 1916 -> 14 Apr 1916
215 Gr_Bulgaria_3, // 3 Sep 1920 -> 17 Sep 1920
216
217 Gr_Canada = Gr_Unknown, // Different regions followed the changes in
218 // Great Britain or France
219
220 Gr_China = Gr_Unknown, // Different authorities say:
221 Gr_China_1, // 18 Dec 1911 -> 1 Jan 1912
222 Gr_China_2, // 18 Dec 1928 -> 1 Jan 1929
223
224 Gr_Czechoslovakia, // (Bohemia and Moravia) 6 Jan 1584 -> 17 Jan 1584
225 Gr_Denmark, // (including Norway) 18 Feb 1700 -> 1 Mar 1700
226 Gr_Egypt, // 1875
227 Gr_Estonia, // 1918
228 Gr_Finland, // Then part of Sweden
229
230 Gr_France, // 9 Dec 1582 -> 20 Dec 1582
231 Gr_France_Alsace, // 4 Feb 1682 -> 16 Feb 1682
232 Gr_France_Lorraine, // 16 Feb 1760 -> 28 Feb 1760
233 Gr_France_Strasbourg, // February 1682
234
235 Gr_Germany = Gr_Unknown, // Different states on different dates:
236 Gr_Germany_Catholic, // 1583-1585 (we take 1584)
237 Gr_Germany_Prussia, // 22 Aug 1610 -> 2 Sep 1610
238 Gr_Germany_Protestant, // 18 Feb 1700 -> 1 Mar 1700
239
240 Gr_GreatBritain, // 2 Sep 1752 -> 14 Sep 1752 (use 'cal(1)')
241
242 Gr_Greece, // 9 Mar 1924 -> 23 Mar 1924
243 Gr_Hungary, // 21 Oct 1587 -> 1 Nov 1587
244 Gr_Ireland = Gr_GreatBritain,
245 Gr_Italy = Gr_Standard,
246
247 Gr_Japan = Gr_Unknown, // Different authorities say:
248 Gr_Japan_1, // 19 Dec 1872 -> 1 Jan 1873
249 Gr_Japan_2, // 19 Dec 1892 -> 1 Jan 1893
250 Gr_Japan_3, // 18 Dec 1918 -> 1 Jan 1919
251
252 Gr_Latvia, // 1915-1918 (we take 1915)
253 Gr_Lithuania, // 1915
254 Gr_Luxemburg, // 14 Dec 1582 -> 25 Dec 1582
255 Gr_Netherlands = Gr_Belgium, // (including Belgium) 1 Jan 1583
256
257 // this is too weird to take into account: the Gregorian calendar was
258 // introduced twice in Groningen, first time 28 Feb 1583 was followed
259 // by 11 Mar 1583, then it has gone back to Julian in the summer of
260 // 1584 and then 13 Dec 1700 -> 12 Jan 1701 - which is
261 // the date we take here
262 Gr_Netherlands_Groningen, // 13 Dec 1700 -> 12 Jan 1701
263 Gr_Netherlands_Gelderland, // 30 Jun 1700 -> 12 Jul 1700
264 Gr_Netherlands_Utrecht, // (and Overijssel) 30 Nov 1700->12 Dec 1700
265 Gr_Netherlands_Friesland, // (and Drenthe) 31 Dec 1700 -> 12 Jan 1701
266
267 Gr_Norway = Gr_Denmark, // Then part of Denmark
268 Gr_Poland = Gr_Standard,
269 Gr_Portugal = Gr_Standard,
270 Gr_Romania, // 31 Mar 1919 -> 14 Apr 1919
271 Gr_Russia, // 31 Jan 1918 -> 14 Feb 1918
272 Gr_Scotland = Gr_GreatBritain,
273 Gr_Spain = Gr_Standard,
274
275 // Sweden has a curious history. Sweden decided to make a gradual
276 // change from the Julian to the Gregorian calendar. By dropping every
277 // leap year from 1700 through 1740 the eleven superfluous days would
278 // be omitted and from 1 Mar 1740 they would be in sync with the
279 // Gregorian calendar. (But in the meantime they would be in sync with
280 // nobody!)
281 //
282 // So 1700 (which should have been a leap year in the Julian calendar)
283 // was not a leap year in Sweden. However, by mistake 1704 and 1708
284 // became leap years. This left Sweden out of synchronisation with
285 // both the Julian and the Gregorian world, so they decided to go back
286 // to the Julian calendar. In order to do this, they inserted an extra
287 // day in 1712, making that year a double leap year! So in 1712,
288 // February had 30 days in Sweden.
289 //
290 // Later, in 1753, Sweden changed to the Gregorian calendar by
291 // dropping 11 days like everyone else.
292 Gr_Sweden = Gr_Finland, // 17 Feb 1753 -> 1 Mar 1753
293
294 Gr_Switzerland = Gr_Unknown,// Different cantons used different dates
295 Gr_Switzerland_Catholic, // 1583, 1584 or 1597 (we take 1584)
296 Gr_Switzerland_Protestant, // 31 Dec 1700 -> 12 Jan 1701
297
298 Gr_Turkey, // 1 Jan 1927
299 Gr_USA = Gr_GreatBritain,
300 Gr_Wales = Gr_GreatBritain,
301 Gr_Yugoslavia // 1919
302 };
303
304 // the country parameter is used so far for calculating the start and
305 // the end of DST period and for deciding whether the date is a work
306 // day or not
307 //
308 // TODO move this to intl.h
309 enum Country
310 {
311 Country_Unknown, // no special information for this country
312 Country_Default, // set the default country with SetCountry() method
313 // or use the default country with any other
314
315 // TODO add more countries (for this we must know about DST and/or
316 // holidays for this country)
317
318 // Western European countries: we assume that they all follow the same
319 // DST rules (true or false?)
320 Country_WesternEurope_Start,
321 Country_EEC = Country_WesternEurope_Start,
322 France,
323 Germany,
324 UK,
325 Country_WesternEurope_End = UK,
326
327 Russia,
328
329 USA
330 };
331
332 // symbolic names for the months
333 enum Month
334 {
335 Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec, Inv_Month
336 };
337
338 // symbolic names for the weekdays
339 enum WeekDay
340 {
341 Sun, Mon, Tue, Wed, Thu, Fri, Sat, Inv_WeekDay
342 };
343
344 // invalid value for the year
345 enum Year
346 {
347 Inv_Year = SHRT_MIN // should hold in wxDateTime_t
348 };
349
350 // flags for GetWeekDayName and GetMonthName
351 enum NameFlags
352 {
353 Name_Full = 0x01, // return full name
354 Name_Abbr = 0x02 // return abbreviated name
355 };
356
357 // helper classes
358 // ------------------------------------------------------------------------
359
360 // a class representing a time zone: basicly, this is just an offset
361 // (in seconds) from GMT
362 class TimeZone
363 {
364 public:
365 TimeZone(TZ tz);
366 TimeZone(wxDateTime_t offset = 0) { m_offset = offset; }
367
368 long GetOffset() const { return m_offset; }
369
370 private:
371 // offset for this timezone from GMT in seconds
372 long m_offset;
373 };
374
375 // standard struct tm is limited to the years from 1900 (because
376 // tm_year field is the offset from 1900), so we use our own struct
377 // instead to represent broken down time
378 //
379 // NB: this struct should always be kept normalized (i.e. mon should
380 // be < 12, 1 <= day <= 31 &c), so use AddMonths(), AddDays()
381 // instead of modifying the member fields directly!
382 struct Tm
383 {
384 wxDateTime_t msec, sec, min, hour, mday;
385 Month mon;
386 int year;
387
388 // default ctor inits the object to an invalid value
389 Tm();
390
391 // ctor from struct tm and the timezone
392 Tm(const struct tm& tm, const TimeZone& tz);
393
394 // check that the given date/time is valid (in Gregorian calendar)
395 bool IsValid() const;
396
397 // get the week day
398 WeekDay GetWeekDay() // not const because wday may be changed
399 {
400 if ( wday == Inv_WeekDay )
401 ComputeWeekDay();
402
403 return (WeekDay)wday;
404 }
405
406 // add the given number of months to the date keeping it normalized
407 void AddMonths(int monDiff);
408
409 // add the given number of months to the date keeping it normalized
410 void AddDays(int dayDiff);
411
412 private:
413 // compute the weekday from other fields
414 void ComputeWeekDay();
415
416 // the timezone we correspond to
417 TimeZone m_tz;
418
419 // these values can't be accessed directly because they're not always
420 // computed and we calculate them on demand
421 wxDateTime_t wday, yday;
422 };
423
424 // static methods
425 // ------------------------------------------------------------------------
426
427 // set the current country
428 static void SetCountry(Country country);
429 // get the current country
430 static Country GetCountry();
431
432 // return TRUE if the country is a West European one (in practice,
433 // this means that the same DST rules as for EEC apply)
434 static bool IsWestEuropeanCountry(Country country = Country_Default);
435
436 // return the current year
437 static int GetCurrentYear(Calendar cal = Gregorian);
438
439 // convert the year as returned by wxDateTime::GetYear() to a year
440 // suitable for BC/AD notation. The difference is that BC year 1
441 // corresponds to the year 0 (while BC year 0 didn't exist) and AD
442 // year N is just year N.
443 static int ConvertYearToBC(int year);
444
445 // return the current month
446 static Month GetCurrentMonth(Calendar cal = Gregorian);
447
448 // returns TRUE if the given year is a leap year in the given calendar
449 static bool IsLeapYear(int year = Inv_Year, Calendar cal = Gregorian);
450
451 // get the century (19 for 1999, 20 for 2000 and -5 for 492 BC)
452 static int GetCentury(int year = Inv_Year);
453
454 // returns the number of days in this year (356 or 355 for Gregorian
455 // calendar usually :-)
456 static wxDateTime_t GetNumberOfDays(int year, Calendar cal = Gregorian);
457
458 // get the number of the days in the given month (default value for
459 // the year means the current one)
460 static wxDateTime_t GetNumberOfDays(Month month,
461 int year = Inv_Year,
462 Calendar cal = Gregorian);
463
464 // get the full (default) or abbreviated month name in the current
465 // locale, returns empty string on error
466 static wxString GetMonthName(Month month,
467 NameFlags flags = Name_Full);
468
469 // get the full (default) or abbreviated weekday name in the current
470 // locale, returns empty string on error
471 static wxString GetWeekDayName(WeekDay weekday,
472 NameFlags flags = Name_Full);
473
474 // get the AM and PM strings in the current locale (may be empty)
475 static void GetAmPmStrings(wxString *am, wxString *pm);
476
477 // return TRUE if the given country uses DST for this year
478 static bool IsDSTApplicable(int year = Inv_Year,
479 Country country = Country_Default);
480
481 // get the beginning of DST for this year, will return invalid object
482 // if no DST applicable in this year. The default value of the
483 // parameter means to take the current year.
484 static wxDateTime GetBeginDST(int year = Inv_Year,
485 Country country = Country_Default);
486 // get the end of DST for this year, will return invalid object
487 // if no DST applicable in this year. The default value of the
488 // parameter means to take the current year.
489 static wxDateTime GetEndDST(int year = Inv_Year,
490 Country country = Country_Default);
491
492 // return the wxDateTime object for the current time
493 static inline wxDateTime Now();
494
495 // return the wxDateTime object for today midnight: i.e. as Now() but
496 // with time set to 0
497 static inline wxDateTime Today();
498
499 // constructors: you should test whether the constructor succeeded with
500 // IsValid() function. The values Inv_Month and Inv_Year for the
501 // parameters mean take current month and/or year values.
502 // ------------------------------------------------------------------------
503
504 // default ctor does not initialize the object, use Set()!
505 wxDateTime() { }
506
507 // from time_t: seconds since the Epoch 00:00:00 UTC, Jan 1, 1970)
508 inline wxDateTime(time_t timet);
509 // from broken down time/date (only for standard Unix range)
510 inline wxDateTime(const struct tm& tm);
511 // from broken down time/date (any range)
512 inline wxDateTime(const Tm& tm);
513
514 // from JDN (beware of rounding errors)
515 inline wxDateTime(double jdn);
516
517 // from separate values for each component, date set to today
518 inline wxDateTime(wxDateTime_t hour,
519 wxDateTime_t minute = 0,
520 wxDateTime_t second = 0,
521 wxDateTime_t millisec = 0);
522 // from separate values for each component with explicit date
523 inline wxDateTime(wxDateTime_t day, // day of the month
524 Month month = Inv_Month,
525 int year = Inv_Year, // 1999, not 99 please!
526 wxDateTime_t hour = 0,
527 wxDateTime_t minute = 0,
528 wxDateTime_t second = 0,
529 wxDateTime_t millisec = 0);
530
531 // default copy ctor ok
532
533 // no dtor
534
535 // assignment operators and Set() functions: all non const methods return
536 // the reference to this object. IsValid() should be used to test whether
537 // the function succeeded.
538 // ------------------------------------------------------------------------
539
540 // set to the current time
541 inline wxDateTime& SetToCurrent();
542
543 // set to given time_t value
544 inline wxDateTime& Set(time_t timet);
545
546 // set to given broken down time/date
547 wxDateTime& Set(const struct tm& tm);
548
549 // set to given broken down time/date
550 inline wxDateTime& Set(const Tm& tm);
551
552 // set to given JDN (beware of rounding errors)
553 wxDateTime& Set(double jdn);
554
555 // set to given time, date = today
556 wxDateTime& Set(wxDateTime_t hour,
557 wxDateTime_t minute = 0,
558 wxDateTime_t second = 0,
559 wxDateTime_t millisec = 0);
560
561 // from separate values for each component with explicit date
562 // (defaults for month and year are the current values)
563 wxDateTime& Set(wxDateTime_t day,
564 Month month = Inv_Month,
565 int year = Inv_Year, // 1999, not 99 please!
566 wxDateTime_t hour = 0,
567 wxDateTime_t minute = 0,
568 wxDateTime_t second = 0,
569 wxDateTime_t millisec = 0);
570
571 // resets time to 00:00:00, doesn't change the date
572 wxDateTime& ResetTime();
573
574 // the following functions don't change the values of the other
575 // fields, i.e. SetMinute() won't change either hour or seconds value
576
577 // set the year
578 wxDateTime& SetYear(int year);
579 // set the month
580 wxDateTime& SetMonth(Month month);
581 // set the day of the month
582 wxDateTime& SetDay(wxDateTime_t day);
583 // set hour
584 wxDateTime& SetHour(wxDateTime_t hour);
585 // set minute
586 wxDateTime& SetMinute(wxDateTime_t minute);
587 // set second
588 wxDateTime& SetSecond(wxDateTime_t second);
589 // set millisecond
590 wxDateTime& SetMillisecond(wxDateTime_t millisecond);
591
592 // assignment operator from time_t
593 wxDateTime& operator=(time_t timet) { return Set(timet); }
594
595 // assignment operator from broken down time/date
596 wxDateTime& operator=(const struct tm& tm) { return Set(tm); }
597
598 // assignment operator from broken down time/date
599 wxDateTime& operator=(const Tm& tm) { return Set(tm); }
600
601 // default assignment operator is ok
602
603 // calendar calculations (functions which set the date only leave the time
604 // unchanged, e.g. don't explictly zero it)
605 // ------------------------------------------------------------------------
606
607 // set to the given week day in the same week as this one
608 wxDateTime& SetToWeekDayInSameWeek(WeekDay weekday);
609
610 // set to the next week day following this one
611 wxDateTime& SetToNextWeekDay(WeekDay weekday);
612
613 // set to the previous week day following this one
614 wxDateTime& SetToPrevWeekDay(WeekDay weekday);
615
616 // set to Nth occurence of given weekday in the given month of the
617 // given year (time is set to 0), return TRUE on success and FALSE on
618 // failure. n may be positive (1..5) or negative to count from the end
619 // of the month (see helper function SetToLastWeekDay())
620 bool SetToWeekDay(WeekDay weekday,
621 int n = 1,
622 Month month = Inv_Month,
623 int year = Inv_Year);
624
625 // sets to the last weekday in the given month, year
626 inline bool SetToLastWeekDay(WeekDay weekday,
627 Month month = Inv_Month,
628 int year = Inv_Year);
629
630 // sets the date to the given day of the given week in the year,
631 // returns TRUE on success and FALSE if given date doesn't exist (e.g.
632 // numWeek is > 53)
633 bool SetToTheWeek(wxDateTime_t numWeek, WeekDay weekday = Mon);
634
635 // sets the date to the last day of the given (or current) month or the
636 // given (or current) year
637 wxDateTime& SetToLastMonthDay(Month month = Inv_Month,
638 int year = Inv_Year);
639
640 // sets to the given year day (1..365 or 366)
641 wxDateTime& SetToYearDay(wxDateTime_t yday);
642
643 // The definitions below were taken verbatim from
644 //
645 // http://www.capecod.net/~pbaum/date/date0.htm
646 //
647 // (Peter Baum's home page)
648 //
649 // definition: The Julian Day Number, Julian Day, or JD of a
650 // particular instant of time is the number of days and fractions of a
651 // day since 12 hours Universal Time (Greenwich mean noon) on January
652 // 1 of the year -4712, where the year is given in the Julian
653 // proleptic calendar. The idea of using this reference date was
654 // originally proposed by Joseph Scalizer in 1582 to count years but
655 // it was modified by 19th century astronomers to count days. One
656 // could have equivalently defined the reference time to be noon of
657 // November 24, -4713 if were understood that Gregorian calendar rules
658 // were applied. Julian days are Julian Day Numbers and are not to be
659 // confused with Julian dates.
660 //
661 // definition: The Rata Die number is a date specified as the number
662 // of days relative to a base date of December 31 of the year 0. Thus
663 // January 1 of the year 1 is Rata Die day 1.
664
665 // get the Julian Day number (the fractional part specifies the time of
666 // the day, related to noon - beware of rounding errors!)
667 double GetJulianDayNumber() const;
668 double GetJDN() const { return GetJulianDayNumber(); }
669
670 // get the Modified Julian Day number: it is equal to JDN - 2400000.5
671 // and so integral MJDs correspond to the midnights (and not noons).
672 // MJD 0 is Nov 17, 1858
673 double GetModifiedJulianDayNumber() const { return GetJDN() - 2400000.5; }
674 double GetMJD() const { return GetModifiedJulianDayNumber(); }
675
676 // get the Rata Die number
677 double GetRataDie() const;
678
679 // TODO algorithms for calculating some important dates, such as
680 // religious holidays (Easter...) or moon/solar eclipses? Some
681 // algorithms can be found in the calendar FAQ
682
683 // timezone stuff: a wxDateTime object constructed using given
684 // day/month/year/hour/min/sec values correspond to this moment in local
685 // time. Using the functions below, it may be converted to another time
686 // zone (for example, the Unix epoch is wxDateTime(1, Jan, 1970).ToGMT())
687 //
688 // Converting to the local time zone doesn't do anything.
689 // ------------------------------------------------------------------------
690
691 // transform to any given timezone
692 inline wxDateTime ToTimezone(const TimeZone& tz) const;
693 wxDateTime& MakeTimezone(const TimeZone& tz);
694
695 // transform to GMT/UTC
696 wxDateTime ToGMT() const { return ToTimezone(GMT0); }
697 wxDateTime& MakeGMT() { return MakeTimezone(GMT0); }
698
699 // is daylight savings time in effect at this moment according to the
700 // rules of the specified country?
701 //
702 // Return value is > 0 if DST is in effect, 0 if it is not and -1 if
703 // the information is not available (this is compatible with ANSI C)
704 int IsDST(Country country = Country_Default) const;
705
706 // accessors: many of them take the timezone parameter which indicates the
707 // timezone for which to make the calculations and the default value means
708 // to do it for the current timezone of this machine (even if the function
709 // only operates with the date it's necessary because a date may wrap as
710 // result of timezone shift)
711 // ------------------------------------------------------------------------
712
713 // is the date valid (FALSE for uninitialized objects as well as after
714 // the functions which failed to convert the date to supported range)
715 inline bool IsValid() const { return this != &ms_InvDateTime; }
716
717 // get the broken down date/time representation in the given timezone
718 //
719 // If you wish to get several time components (day, month and year),
720 // consider getting the whole Tm strcuture first and retrieving the
721 // value from it - this is much more efficient
722 Tm GetTm(const TimeZone& tz = Local) const;
723
724 // get the number of seconds since the Unix epoch - returns (time_t)-1
725 // if the value is out of range
726 inline time_t GetTicks() const;
727
728 // get the year (returns Inv_Year if date is invalid)
729 int GetYear(const TimeZone& tz = Local) const
730 { return GetTm(tz).year; }
731 // get the month (Inv_Month if date is invalid)
732 Month GetMonth(const TimeZone& tz = Local) const
733 { return (Month)GetTm(tz).mon; }
734 // get the month day (in 1..31 range, 0 if date is invalid)
735 wxDateTime_t GetDay(const TimeZone& tz = Local) const
736 { return GetTm(tz).mday; }
737 // get the day of the week (Inv_WeekDay if date is invalid)
738 WeekDay GetWeekDay(const TimeZone& tz = Local) const
739 { return GetTm(tz).GetWeekDay(); }
740 // get the hour of the day
741 wxDateTime_t GetHour(const TimeZone& tz = Local) const
742 { return GetTm(tz).hour; }
743 // get the minute
744 wxDateTime_t GetMinute(const TimeZone& tz = Local) const
745 { return GetTm(tz).min; }
746 // get the second
747 wxDateTime_t GetSecond(const TimeZone& tz = Local) const
748 { return GetTm(tz).sec; }
749 // get milliseconds
750 wxDateTime_t GetMillisecond(const TimeZone& tz = Local) const
751 { return GetTm(tz).msec; }
752
753 // get the day since the year start (1..366, 0 if date is invalid)
754 wxDateTime_t GetDayOfYear(const TimeZone& tz = Local) const;
755 // get the week number since the year start (1..52 or 53, 0 if date is
756 // invalid)
757 wxDateTime_t GetWeekOfYear(const TimeZone& tz = Local) const;
758 // get the week number since the month start (1..5, 0 if date is
759 // invalid)
760 wxDateTime_t GetWeekOfMonth(const TimeZone& tz = Local) const;
761
762 // is this date a work day? This depends on a country, of course,
763 // because the holidays are different in different countries
764 bool IsWorkDay(Country country = Country_Default,
765 const TimeZone& tz = Local) const;
766
767 // is this date later than Gregorian calendar introduction for the
768 // given country (see enum GregorianAdoption)?
769 //
770 // NB: this function shouldn't be considered as absolute authoiruty in
771 // the matter. Besides, for some countries the exact date of
772 // adoption of the Gregorian calendar is simply unknown.
773 bool IsGregorianDate(GregorianAdoption country = Gr_Standard) const;
774
775 // comparison (see also functions below for operator versions)
776 // ------------------------------------------------------------------------
777
778 // returns TRUE if the two moments are strictly identical
779 inline bool IsEqualTo(const wxDateTime& datetime) const;
780
781 // returns TRUE if the date is strictly earlier than the given one
782 inline bool IsEarlierThan(const wxDateTime& datetime) const;
783
784 // returns TRUE if the date is strictly later than the given one
785 inline bool IsLaterThan(const wxDateTime& datetime) const;
786
787 // returns TRUE if the date is strictly in the given range
788 inline bool IsStrictlyBetween(const wxDateTime& t1,
789 const wxDateTime& t2) const;
790
791 // returns TRUE if the date is in the given range
792 inline bool IsBetween(const wxDateTime& t1, const wxDateTime& t2) const;
793
794 // arithmetics with dates (see also below for more operators)
795 // ------------------------------------------------------------------------
796
797 // return the sum of the date with a time span (positive or negative)
798 inline wxDateTime Add(const wxTimeSpan& diff) const;
799 // add a time span (positive or negative)
800 inline wxDateTime& Add(const wxTimeSpan& diff);
801 // add a time span (positive or negative)
802 inline wxDateTime& operator+=(const wxTimeSpan& diff);
803
804 // return the difference of the date with a time span
805 inline wxDateTime Substract(const wxTimeSpan& diff) const;
806 // substract a time span (positive or negative)
807 inline wxDateTime& Substract(const wxTimeSpan& diff);
808 // substract a time span (positive or negative)
809 inline wxDateTime& operator-=(const wxTimeSpan& diff);
810
811 // return the sum of the date with a date span
812 inline wxDateTime Add(const wxDateSpan& diff) const;
813 // add a date span (positive or negative)
814 wxDateTime& Add(const wxDateSpan& diff);
815 // add a date span (positive or negative)
816 inline wxDateTime& operator+=(const wxDateSpan& diff);
817
818 // return the difference of the date with a date span
819 inline wxDateTime Substract(const wxDateSpan& diff) const;
820 // substract a date span (positive or negative)
821 inline wxDateTime& Substract(const wxDateSpan& diff);
822 // substract a date span (positive or negative)
823 inline wxDateTime& operator-=(const wxDateSpan& diff);
824
825 // return the difference between two dates
826 inline wxTimeSpan Substract(const wxDateTime& dt) const;
827
828 // conversion to/from text: all conversions from text return the pointer to
829 // the next character following the date specification (i.e. the one where
830 // the scan had to stop) or NULL on failure.
831 // ------------------------------------------------------------------------
832
833 // parse a string in RFC 822 format (found e.g. in mail headers and
834 // having the form "Wed, 10 Feb 1999 19:07:07 +0100")
835 const wxChar *ParseRfc822Date(const wxChar* date);
836 // parse a date/time in the given format (see strptime(3)), fill in
837 // the missing (in the string) fields with the values of dateDef (by
838 // default, they will not change if they had valid values or will
839 // default to Today() otherwise)
840 const wxChar *ParseFormat(const wxChar *date,
841 const wxChar *format = _T("%c"),
842 const wxDateTime& dateDef = ms_InvDateTime);
843 // parse a string containing the date/time in "free" format, this
844 // function will try to make an educated guess at the string contents
845 const wxChar *ParseDateTime(const wxChar *datetime);
846 // parse a string containing the date only in "free" format (less
847 // flexible than ParseDateTime)
848 const wxChar *ParseDate(const wxChar *date);
849 // parse a string containing the time only in "free" format
850 const wxChar *ParseTime(const wxChar *time);
851
852 // this function accepts strftime()-like format string (default
853 // argument corresponds to the preferred date and time representation
854 // for the current locale) and returns the string containing the
855 // resulting text representation
856 wxString Format(const wxChar *format = _T("%c"),
857 const TimeZone& tz = Local) const;
858 // preferred date representation for the current locale
859 wxString FormatDate() const { return Format(_T("%x")); }
860 // preferred time representation for the current locale
861 wxString FormatTime() const { return Format(_T("%X")); }
862
863 // implementation
864 // ------------------------------------------------------------------------
865
866 // construct from internal representation
867 wxDateTime(const wxLongLong& time) { m_time = time; }
868
869 // get the internal representation
870 inline wxLongLong GetValue() const;
871
872 // a helper function to get the current time_t
873 static time_t GetTimeNow() { return time((time_t *)NULL); }
874
875 // another one to get the current time broken down
876 static struct tm *GetTmNow()
877 {
878 time_t t = GetTimeNow();
879 return localtime(&t);
880 }
881
882 private:
883 // the current country - as it's the same for all program objects (unless
884 // it runs on a _really_ big cluster system :-), this is a static member:
885 // see SetCountry() and GetCountry()
886 static Country ms_country;
887
888 // this constant is used to transform a time_t value to the internal
889 // representation, as time_t is in seconds and we use milliseconds it's
890 // fixed to 1000
891 static const long TIME_T_FACTOR;
892
893 // invalid wxDateTime object - returned by all functions which return
894 // "wxDateTime &" on failure
895 static wxDateTime ms_InvDateTime;
896
897 // returns TRUE if we fall in range in which we can use standard ANSI C
898 // functions
899 inline bool IsInStdRange() const;
900
901 // the internal representation of the time is the amount of milliseconds
902 // elapsed since the origin which is set by convention to the UNIX/C epoch
903 // value: the midnight of January 1, 1970 (UTC)
904 wxLongLong m_time;
905 };
906
907 // ----------------------------------------------------------------------------
908 // This class contains a difference between 2 wxDateTime values, so it makes
909 // sense to add it to wxDateTime and it is the result of substraction of 2
910 // objects of that class. See also wxDateSpan.
911 // ----------------------------------------------------------------------------
912
913 class WXDLLEXPORT wxTimeSpan
914 {
915 public:
916 // constructors
917 // ------------------------------------------------------------------------
918
919 // return the timespan for the given number of seconds
920 static wxTimeSpan Seconds(int sec) { return wxTimeSpan(0, 0, sec); }
921 static wxTimeSpan Second() { return Seconds(1); }
922
923 // return the timespan for the given number of minutes
924 static wxTimeSpan Minutes(int min) { return wxTimeSpan(0, min, 0 ); }
925 static wxTimeSpan Minute() { return Minutes(1); }
926
927 // return the timespan for the given number of hours
928 static wxTimeSpan Hours(int hours) { return wxTimeSpan(hours, 0, 0); }
929 static wxTimeSpan Hour() { return Hours(1); }
930
931 // return the timespan for the given number of days
932 static wxTimeSpan Days(int days) { return Hours(24 * days); }
933 static wxTimeSpan Day() { return Days(1); }
934
935 // return the timespan for the given number of weeks
936 static wxTimeSpan Weeks(int days) { return Days(7 * days); }
937 static wxTimeSpan Week() { return Weeks(1); }
938
939 // default ctor constructs the 0 time span
940 wxTimeSpan() { }
941
942 // from separate values for each component, date set to 0 (hours are
943 // not restricted to 0..24 range, neither are minutes, seconds or
944 // milliseconds)
945 inline wxTimeSpan(int hours,
946 int minutes = 0,
947 int seconds = 0,
948 int milliseconds = 0);
949
950 // default copy ctor is ok
951
952 // no dtor
953
954 // arithmetics with time spans (see also below for more operators)
955 // ------------------------------------------------------------------------
956
957 // return the sum of two timespans
958 inline wxTimeSpan Add(const wxTimeSpan& diff) const;
959 // add two timespans together
960 inline wxTimeSpan& Add(const wxTimeSpan& diff);
961 // add two timespans together
962 wxTimeSpan& operator+=(const wxTimeSpan& diff) { return Add(diff); }
963
964 // return the difference of two timespans
965 inline wxTimeSpan Substract(const wxTimeSpan& diff) const;
966 // substract another timespan
967 inline wxTimeSpan& Substract(const wxTimeSpan& diff);
968 // substract another timespan
969 wxTimeSpan& operator-=(const wxTimeSpan& diff) { return Substract(diff); }
970
971 // multiply timespan by a scalar
972 inline wxTimeSpan Multiply(int n) const;
973 // multiply timespan by a scalar
974 inline wxTimeSpan& Multiply(int n);
975 // multiply timespan by a scalar
976 wxTimeSpan& operator*=(int n) { return Multiply(n); }
977
978 // return this timespan with inversed sign
979 wxTimeSpan Negate() const { return wxTimeSpan(-GetValue()); }
980 // negate the value of the timespan
981 wxTimeSpan& Neg() { m_diff = -GetValue(); return *this; }
982 // negate the value of the timespan
983 wxTimeSpan& operator-() { return Neg(); }
984
985 // return the absolute value of the timespan: does _not_ modify the
986 // object
987 inline wxTimeSpan Abs() const;
988
989 // there is intentionally no division because we don't want to
990 // introduce rounding errors in time calculations
991
992 // comparaison (see also operator versions below)
993 // ------------------------------------------------------------------------
994
995 // is the timespan null?
996 bool IsNull() const { return m_diff == 0l; }
997 // returns true if the timespan is null
998 bool operator!() const { return !IsNull(); }
999
1000 // is the timespan positive?
1001 bool IsPositive() const { return m_diff > 0l; }
1002
1003 // is the timespan negative?
1004 bool IsNegative() const { return m_diff < 0l; }
1005
1006 // are two timespans equal?
1007 inline bool IsEqualTo(const wxTimeSpan& ts) const;
1008 // compare two timestamps: works with the absolute values, i.e. -2
1009 // hours is longer than 1 hour. Also, it will return FALSE if the
1010 // timespans are equal in absolute value.
1011 inline bool IsLongerThan(const wxTimeSpan& ts) const;
1012 // compare two timestamps: works with the absolute values, i.e. 1
1013 // hour is shorter than -2 hours. Also, it will return FALSE if the
1014 // timespans are equal in absolute value.
1015 bool IsShorterThan(const wxTimeSpan& t) const { return !IsLongerThan(t); }
1016
1017 // breaking into days, hours, minutes and seconds
1018 // ------------------------------------------------------------------------
1019
1020 // get the max number of weeks in this timespan
1021 inline int GetWeeks() const;
1022 // get the max number of days in this timespan
1023 inline int GetDays() const;
1024 // get the max number of hours in this timespan
1025 inline int GetHours() const;
1026 // get the max number of minutes in this timespan
1027 inline int GetMinutes() const;
1028 // get the max number of seconds in this timespan
1029 inline wxLongLong GetSeconds() const;
1030 // get the number of milliseconds in this timespan
1031 wxLongLong GetMilliseconds() const { return m_diff; }
1032
1033 // conversion to text
1034 // ------------------------------------------------------------------------
1035
1036 // this function accepts strftime()-like format string (default
1037 // argument corresponds to the preferred date and time representation
1038 // for the current locale) and returns the string containing the
1039 // resulting text representation. Notice that only some of format
1040 // specifiers valid for wxDateTime are valid for wxTimeSpan: hours,
1041 // minutes and seconds make sense, but not "PM/AM" string for example.
1042 wxString Format(const wxChar *format = _T("%c")) const;
1043 // preferred date representation for the current locale
1044 wxString FormatDate() const { return Format(_T("%x")); }
1045 // preferred time representation for the current locale
1046 wxString FormatTime() const { return Format(_T("%X")); }
1047
1048 // implementation
1049 // ------------------------------------------------------------------------
1050
1051 // construct from internal representation
1052 wxTimeSpan(const wxLongLong& diff) { m_diff = diff; }
1053
1054 // get the internal representation
1055 wxLongLong GetValue() const { return m_diff; }
1056
1057 private:
1058 // the (signed) time span in milliseconds
1059 wxLongLong m_diff;
1060 };
1061
1062 // ----------------------------------------------------------------------------
1063 // This class is a "logical time span" and is useful for implementing program
1064 // logic for such things as "add one month to the date" which, in general,
1065 // doesn't mean to add 60*60*24*31 seconds to it, but to take the same date
1066 // the next month (to understand that this is indeed different consider adding
1067 // one month to Feb, 15 - we want to get Mar, 15, of course).
1068 //
1069 // When adding a month to the date, all lesser components (days, hours, ...)
1070 // won't be changed.
1071 //
1072 // wxDateSpan can be either positive or negative. They may be
1073 // multiplied by scalars which multiply all deltas by the scalar: i.e. 2*(1
1074 // month and 1 day) is 2 months and 2 days. They can be added together and
1075 // with wxDateTime or wxTimeSpan, but the type of result is different for each
1076 // case.
1077 //
1078 // Beware about weeks: if you specify both weeks and days, the total number of
1079 // days added will be 7*weeks + days! See also GetTotalDays() function.
1080 //
1081 // Finally, notice that for adding hours, minutes &c you don't need this
1082 // class: wxTimeSpan will do the job because there are no subtleties
1083 // associated with those.
1084 // ----------------------------------------------------------------------------
1085
1086 class WXDLLEXPORT wxDateSpan
1087 {
1088 public:
1089 // constructors
1090 // ------------------------------------------------------------------------
1091
1092 // this many years/months/weeks/days
1093 wxDateSpan(int years = 0, int months = 0, int weeks = 0, int days = 0)
1094 {
1095 m_years = years;
1096 m_months = months;
1097 m_weeks = weeks;
1098 m_days = days;
1099 }
1100
1101 // get an object for the given number of days
1102 static wxDateSpan Days(int days) { return wxDateSpan(0, 0, 0, days); }
1103 static wxDateSpan Day() { return Days(1); }
1104
1105 // get an object for the given number of weeks
1106 static wxDateSpan Weeks(int weeks) { return wxDateSpan(0, 0, weeks, 0); }
1107 static wxDateSpan Week() { return Weeks(1); }
1108
1109 // get an object for the given number of months
1110 static wxDateSpan Months(int mon) { return wxDateSpan(0, mon, 0, 0); }
1111 static wxDateSpan Month() { return Months(1); }
1112
1113 // get an object for the given number of years
1114 static wxDateSpan Years(int years) { return wxDateSpan(years, 0, 0, 0); }
1115 static wxDateSpan Year() { return Years(1); }
1116
1117 // default copy ctor is ok
1118
1119 // no dtor
1120
1121 // accessors (all SetXXX() return the (modified) wxDateSpan object)
1122 // ------------------------------------------------------------------------
1123
1124 // set number of years
1125 wxDateSpan& SetYears(int n) { m_years = n; return *this; }
1126 // set number of months
1127 wxDateSpan& SetMonths(int n) { m_months = n; return *this; }
1128 // set number of weeks
1129 wxDateSpan& SetWeeks(int n) { m_weeks = n; return *this; }
1130 // set number of days
1131 wxDateSpan& SetDays(int n) { m_days = n; return *this; }
1132
1133 // get number of years
1134 int GetYears() const { return m_years; }
1135 // get number of months
1136 int GetMonths() const { return m_months; }
1137 // get number of weeks
1138 int GetWeeks() const { return m_weeks; }
1139 // get number of days
1140 int GetDays() const { return m_days; }
1141 // returns 7*GetWeeks() + GetDays()
1142 int GetTotalDays() const { return 7*m_weeks + m_days; }
1143
1144 // arithmetics with date spans (see also below for more operators)
1145 // ------------------------------------------------------------------------
1146
1147 // return sum of two date spans
1148 inline wxDateSpan Add(const wxDateSpan& other) const;
1149 // add another wxDateSpan to us
1150 inline wxDateSpan& Add(const wxDateSpan& other);
1151 // add another wxDateSpan to us
1152 inline wxDateSpan& operator+=(const wxDateSpan& other);
1153
1154 // return difference of two date spans
1155 inline wxDateSpan Substract(const wxDateSpan& other) const;
1156 // substract another wxDateSpan from us
1157 inline wxDateSpan& Substract(const wxDateSpan& other);
1158 // substract another wxDateSpan from us
1159 inline wxDateSpan& operator-=(const wxDateSpan& other);
1160
1161 // return a copy of this time span with changed sign
1162 inline wxDateSpan Negate() const;
1163 // inverse the sign of this timespan
1164 inline wxDateSpan& Neg();
1165 // inverse the sign of this timespan
1166 wxDateSpan& operator-() { return Neg(); }
1167
1168 // return the date span proportional to this one with given factor
1169 inline wxDateSpan Multiply(int factor) const;
1170 // multiply all components by a (signed) number
1171 inline wxDateSpan& Multiply(int factor);
1172 // multiply all components by a (signed) number
1173 inline wxDateSpan& operator*=(int factor) { return Multiply(factor); }
1174
1175 private:
1176 int m_years,
1177 m_months,
1178 m_weeks,
1179 m_days;
1180 };
1181
1182 WXDLLEXPORT_DATA(extern wxDateSpan) wxYear;
1183 WXDLLEXPORT_DATA(extern wxDateSpan) wxMonth;
1184 WXDLLEXPORT_DATA(extern wxDateSpan) wxWeek;
1185 WXDLLEXPORT_DATA(extern wxDateSpan) wxDay;
1186
1187 // ============================================================================
1188 // inline functions implementation
1189 // ============================================================================
1190
1191 // don't include inline functions definitions when we're included from anything
1192 // else than datetime.cpp in debug builds: this minimizes rebuilds if we change
1193 // some inline function and the performance doesn't matter in the debug builds.
1194
1195 #if !defined(__WXDEBUG__) || defined(wxDEFINE_TIME_CONSTANTS)
1196 #define INCLUDED_FROM_WX_DATETIME_H
1197 #include "wx/datetime.inl"
1198 #undef INCLUDED_FROM_WX_DATETIME_H
1199 #endif
1200
1201 // if we defined it to be empty above, restore it now
1202 #undef inline
1203
1204 // ============================================================================
1205 // binary operators
1206 // ============================================================================
1207
1208 // ----------------------------------------------------------------------------
1209 // wxDateTime operators
1210 // ----------------------------------------------------------------------------
1211
1212 // arithmetics
1213 // -----------
1214
1215 // no need to check for validity - the member functions we call will do it
1216
1217 inline wxDateTime WXDLLEXPORT operator+(const wxDateTime& dt,
1218 const wxTimeSpan& ts)
1219 {
1220 return dt.Add(ts);
1221 }
1222
1223 inline wxDateTime WXDLLEXPORT operator-(const wxDateTime& dt,
1224 const wxTimeSpan& ts)
1225 {
1226 return dt.Substract(ts);
1227 }
1228
1229 inline wxDateTime WXDLLEXPORT operator+(const wxDateTime& dt,
1230 const wxDateSpan& ds)
1231 {
1232 return dt.Add(ds);
1233 }
1234
1235 inline wxDateTime WXDLLEXPORT operator-(const wxDateTime& dt,
1236 const wxDateSpan& ds)
1237 {
1238 return dt.Substract(ds);
1239 }
1240
1241 inline wxTimeSpan WXDLLEXPORT operator-(const wxDateTime& dt1,
1242 const wxDateTime& dt2)
1243 {
1244 return dt1.Substract(dt2);
1245 }
1246
1247 // comparison
1248 // ----------
1249
1250 inline bool WXDLLEXPORT operator<(const wxDateTime& t1, const wxDateTime& t2)
1251 {
1252 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") );
1253
1254 return t1.GetValue() < t2.GetValue();
1255 }
1256
1257 inline bool WXDLLEXPORT operator<=(const wxDateTime& t1, const wxDateTime& t2)
1258 {
1259 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") );
1260
1261 return t1.GetValue() <= t2.GetValue();
1262 }
1263
1264 inline bool WXDLLEXPORT operator>(const wxDateTime& t1, const wxDateTime& t2)
1265 {
1266 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") );
1267
1268 return t1.GetValue() > t2.GetValue();
1269 }
1270
1271 inline bool WXDLLEXPORT operator>=(const wxDateTime& t1, const wxDateTime& t2)
1272 {
1273 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") );
1274
1275 return t1.GetValue() >= t2.GetValue();
1276 }
1277
1278 inline bool WXDLLEXPORT operator==(const wxDateTime& t1, const wxDateTime& t2)
1279 {
1280 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") );
1281
1282 return t1.GetValue() == t2.GetValue();
1283 }
1284
1285 inline bool WXDLLEXPORT operator!=(const wxDateTime& t1, const wxDateTime& t2)
1286 {
1287 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") );
1288
1289 return t1.GetValue() != t2.GetValue();
1290 }
1291
1292 // ----------------------------------------------------------------------------
1293 // wxTimeSpan operators
1294 // ----------------------------------------------------------------------------
1295
1296 // arithmetics
1297 // -----------
1298
1299 inline wxTimeSpan WXDLLEXPORT operator+(const wxTimeSpan& ts1,
1300 const wxTimeSpan& ts2)
1301 {
1302 return wxTimeSpan(ts1.GetValue() + ts2.GetValue());
1303 }
1304
1305 inline wxTimeSpan WXDLLEXPORT operator-(const wxTimeSpan& ts1,
1306 const wxTimeSpan& ts2)
1307 {
1308 return wxTimeSpan(ts1.GetValue() - ts2.GetValue());
1309 }
1310
1311 inline wxTimeSpan WXDLLEXPORT operator*(const wxTimeSpan& ts, int n)
1312 {
1313 return wxTimeSpan(ts).Multiply(n);
1314 }
1315
1316 inline wxTimeSpan WXDLLEXPORT operator*(int n, const wxTimeSpan& ts)
1317 {
1318 return wxTimeSpan(ts).Multiply(n);
1319 }
1320
1321 // comparison
1322 // ----------
1323
1324 inline bool WXDLLEXPORT operator<(const wxTimeSpan &t1, const wxTimeSpan &t2)
1325 {
1326 return t1.GetValue() < t2.GetValue();
1327 }
1328
1329 inline bool WXDLLEXPORT operator<=(const wxTimeSpan &t1, const wxTimeSpan &t2)
1330 {
1331 return t1.GetValue() <= t2.GetValue();
1332 }
1333
1334 inline bool WXDLLEXPORT operator>(const wxTimeSpan &t1, const wxTimeSpan &t2)
1335 {
1336 return t1.GetValue() > t2.GetValue();
1337 }
1338
1339 inline bool WXDLLEXPORT operator>=(const wxTimeSpan &t1, const wxTimeSpan &t2)
1340 {
1341 return t1.GetValue() >= t2.GetValue();
1342 }
1343
1344 inline bool WXDLLEXPORT operator==(const wxTimeSpan &t1, const wxTimeSpan &t2)
1345 {
1346 return t1.GetValue() == t2.GetValue();
1347 }
1348
1349 inline bool WXDLLEXPORT operator!=(const wxTimeSpan &t1, const wxTimeSpan &t2)
1350 {
1351 return t1.GetValue() != t2.GetValue();
1352 }
1353
1354 // ----------------------------------------------------------------------------
1355 // wxDateSpan
1356 // ----------------------------------------------------------------------------
1357
1358 // arithmetics
1359 // -----------
1360
1361 inline WXDLLEXPORT wxDateSpan operator+(const wxDateSpan& ds1,
1362 const wxDateSpan& ds2)
1363 {
1364 return wxDateSpan(ds1.GetYears() + ds2.GetYears(),
1365 ds1.GetMonths() + ds2.GetMonths(),
1366 ds1.GetWeeks() + ds2.GetWeeks(),
1367 ds1.GetDays() + ds2.GetDays());
1368 }
1369
1370 inline WXDLLEXPORT wxDateSpan operator-(const wxDateSpan& ds1,
1371 const wxDateSpan& ds2)
1372 {
1373 return wxDateSpan(ds1.GetYears() - ds2.GetYears(),
1374 ds1.GetMonths() - ds2.GetMonths(),
1375 ds1.GetWeeks() - ds2.GetWeeks(),
1376 ds1.GetDays() - ds2.GetDays());
1377 }
1378
1379 inline WXDLLEXPORT wxDateSpan operator*(const wxDateSpan& ds, int n)
1380 {
1381 return wxDateSpan(ds).Multiply(n);
1382 }
1383
1384 inline WXDLLEXPORT wxDateSpan operator*(int n, const wxDateSpan& ds)
1385 {
1386 return wxDateSpan(ds).Multiply(n);
1387 }
1388
1389 // ============================================================================
1390 // other helper functions
1391 // ============================================================================
1392
1393 // ----------------------------------------------------------------------------
1394 // iteration helpers: can be used to write a for loop over enum variable like
1395 // this:
1396 // for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) )
1397 // ----------------------------------------------------------------------------
1398
1399 inline WXDLLEXPORT void wxNextMonth(wxDateTime::Month& m)
1400 {
1401 wxASSERT_MSG( m < wxDateTime::Inv_Month, _T("invalid month") );
1402
1403 // no wrapping or the for loop above would never end!
1404 m = (wxDateTime::Month)(m + 1);
1405 }
1406
1407 inline WXDLLEXPORT void wxPrevMonth(wxDateTime::Month& m)
1408 {
1409 wxASSERT_MSG( m < wxDateTime::Inv_Month, _T("invalid month") );
1410
1411 m = m == wxDateTime::Jan ? wxDateTime::Inv_Month
1412 : (wxDateTime::Month)(m - 1);
1413 }
1414
1415 inline WXDLLEXPORT void wxNextWDay(wxDateTime::WeekDay& wd)
1416 {
1417 wxASSERT_MSG( wd < wxDateTime::Inv_WeekDay, _T("invalid week day") );
1418
1419 // no wrapping or the for loop above would never end!
1420 wd = (wxDateTime::WeekDay)(wd + 1);
1421 }
1422
1423 inline WXDLLEXPORT void wxPrevWDay(wxDateTime::WeekDay& wd)
1424 {
1425 wxASSERT_MSG( wd < wxDateTime::Inv_WeekDay, _T("invalid week day") );
1426
1427 wd = wd == wxDateTime::Sun ? wxDateTime::Inv_WeekDay
1428 : (wxDateTime::WeekDay)(wd - 1);
1429 }
1430
1431 #endif // _WX_DATETIME_H