]> git.saurik.com Git - wxWidgets.git/blob - include/wx/datetime.h
1. wxBase compiles under BeOS
[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 // do these two objects refer to the same date?
795 inline bool IsSameDate(const wxDateTime& dt) const;
796
797 // do these two objects have the same time?
798 inline bool IsSameTime(const wxDateTime& dt) const;
799
800 // are these two objects equal up to given timespan?
801 inline bool IsEqualUpTo(const wxDateTime& dt, const wxTimeSpan& ts) const;
802
803 // arithmetics with dates (see also below for more operators)
804 // ------------------------------------------------------------------------
805
806 // return the sum of the date with a time span (positive or negative)
807 inline wxDateTime Add(const wxTimeSpan& diff) const;
808 // add a time span (positive or negative)
809 inline wxDateTime& Add(const wxTimeSpan& diff);
810 // add a time span (positive or negative)
811 inline wxDateTime& operator+=(const wxTimeSpan& diff);
812
813 // return the difference of the date with a time span
814 inline wxDateTime Substract(const wxTimeSpan& diff) const;
815 // substract a time span (positive or negative)
816 inline wxDateTime& Substract(const wxTimeSpan& diff);
817 // substract a time span (positive or negative)
818 inline wxDateTime& operator-=(const wxTimeSpan& diff);
819
820 // return the sum of the date with a date span
821 inline wxDateTime Add(const wxDateSpan& diff) const;
822 // add a date span (positive or negative)
823 wxDateTime& Add(const wxDateSpan& diff);
824 // add a date span (positive or negative)
825 inline wxDateTime& operator+=(const wxDateSpan& diff);
826
827 // return the difference of the date with a date span
828 inline wxDateTime Substract(const wxDateSpan& diff) const;
829 // substract a date span (positive or negative)
830 inline wxDateTime& Substract(const wxDateSpan& diff);
831 // substract a date span (positive or negative)
832 inline wxDateTime& operator-=(const wxDateSpan& diff);
833
834 // return the difference between two dates
835 inline wxTimeSpan Substract(const wxDateTime& dt) const;
836
837 // conversion to/from text: all conversions from text return the pointer to
838 // the next character following the date specification (i.e. the one where
839 // the scan had to stop) or NULL on failure.
840 // ------------------------------------------------------------------------
841
842 // parse a string in RFC 822 format (found e.g. in mail headers and
843 // having the form "Wed, 10 Feb 1999 19:07:07 +0100")
844 const wxChar *ParseRfc822Date(const wxChar* date);
845 // parse a date/time in the given format (see strptime(3)), fill in
846 // the missing (in the string) fields with the values of dateDef (by
847 // default, they will not change if they had valid values or will
848 // default to Today() otherwise)
849 const wxChar *ParseFormat(const wxChar *date,
850 const wxChar *format = _T("%c"),
851 const wxDateTime& dateDef = ms_InvDateTime);
852 // parse a string containing the date/time in "free" format, this
853 // function will try to make an educated guess at the string contents
854 const wxChar *ParseDateTime(const wxChar *datetime);
855 // parse a string containing the date only in "free" format (less
856 // flexible than ParseDateTime)
857 const wxChar *ParseDate(const wxChar *date);
858 // parse a string containing the time only in "free" format
859 const wxChar *ParseTime(const wxChar *time);
860
861 // this function accepts strftime()-like format string (default
862 // argument corresponds to the preferred date and time representation
863 // for the current locale) and returns the string containing the
864 // resulting text representation
865 wxString Format(const wxChar *format = _T("%c"),
866 const TimeZone& tz = Local) const;
867 // preferred date representation for the current locale
868 wxString FormatDate() const { return Format(_T("%x")); }
869 // preferred time representation for the current locale
870 wxString FormatTime() const { return Format(_T("%X")); }
871
872 // implementation
873 // ------------------------------------------------------------------------
874
875 // construct from internal representation
876 wxDateTime(const wxLongLong& time) { m_time = time; }
877
878 // get the internal representation
879 inline wxLongLong GetValue() const;
880
881 // a helper function to get the current time_t
882 static time_t GetTimeNow() { return time((time_t *)NULL); }
883
884 // another one to get the current time broken down
885 static struct tm *GetTmNow()
886 {
887 time_t t = GetTimeNow();
888 return localtime(&t);
889 }
890
891 private:
892 // the current country - as it's the same for all program objects (unless
893 // it runs on a _really_ big cluster system :-), this is a static member:
894 // see SetCountry() and GetCountry()
895 static Country ms_country;
896
897 // this constant is used to transform a time_t value to the internal
898 // representation, as time_t is in seconds and we use milliseconds it's
899 // fixed to 1000
900 static const long TIME_T_FACTOR;
901
902 // invalid wxDateTime object - returned by all functions which return
903 // "wxDateTime &" on failure
904 static wxDateTime ms_InvDateTime;
905
906 // returns TRUE if we fall in range in which we can use standard ANSI C
907 // functions
908 inline bool IsInStdRange() const;
909
910 // the internal representation of the time is the amount of milliseconds
911 // elapsed since the origin which is set by convention to the UNIX/C epoch
912 // value: the midnight of January 1, 1970 (UTC)
913 wxLongLong m_time;
914 };
915
916 // ----------------------------------------------------------------------------
917 // This class contains a difference between 2 wxDateTime values, so it makes
918 // sense to add it to wxDateTime and it is the result of substraction of 2
919 // objects of that class. See also wxDateSpan.
920 // ----------------------------------------------------------------------------
921
922 class WXDLLEXPORT wxTimeSpan
923 {
924 public:
925 // constructors
926 // ------------------------------------------------------------------------
927
928 // return the timespan for the given number of seconds
929 static wxTimeSpan Seconds(int sec) { return wxTimeSpan(0, 0, sec); }
930 static wxTimeSpan Second() { return Seconds(1); }
931
932 // return the timespan for the given number of minutes
933 static wxTimeSpan Minutes(int min) { return wxTimeSpan(0, min, 0 ); }
934 static wxTimeSpan Minute() { return Minutes(1); }
935
936 // return the timespan for the given number of hours
937 static wxTimeSpan Hours(int hours) { return wxTimeSpan(hours, 0, 0); }
938 static wxTimeSpan Hour() { return Hours(1); }
939
940 // return the timespan for the given number of days
941 static wxTimeSpan Days(int days) { return Hours(24 * days); }
942 static wxTimeSpan Day() { return Days(1); }
943
944 // return the timespan for the given number of weeks
945 static wxTimeSpan Weeks(int days) { return Days(7 * days); }
946 static wxTimeSpan Week() { return Weeks(1); }
947
948 // default ctor constructs the 0 time span
949 wxTimeSpan() { }
950
951 // from separate values for each component, date set to 0 (hours are
952 // not restricted to 0..24 range, neither are minutes, seconds or
953 // milliseconds)
954 inline wxTimeSpan(int hours,
955 int minutes = 0,
956 int seconds = 0,
957 int milliseconds = 0);
958
959 // default copy ctor is ok
960
961 // no dtor
962
963 // arithmetics with time spans (see also below for more operators)
964 // ------------------------------------------------------------------------
965
966 // return the sum of two timespans
967 inline wxTimeSpan Add(const wxTimeSpan& diff) const;
968 // add two timespans together
969 inline wxTimeSpan& Add(const wxTimeSpan& diff);
970 // add two timespans together
971 wxTimeSpan& operator+=(const wxTimeSpan& diff) { return Add(diff); }
972
973 // return the difference of two timespans
974 inline wxTimeSpan Substract(const wxTimeSpan& diff) const;
975 // substract another timespan
976 inline wxTimeSpan& Substract(const wxTimeSpan& diff);
977 // substract another timespan
978 wxTimeSpan& operator-=(const wxTimeSpan& diff) { return Substract(diff); }
979
980 // multiply timespan by a scalar
981 inline wxTimeSpan Multiply(int n) const;
982 // multiply timespan by a scalar
983 inline wxTimeSpan& Multiply(int n);
984 // multiply timespan by a scalar
985 wxTimeSpan& operator*=(int n) { return Multiply(n); }
986
987 // return this timespan with inversed sign
988 wxTimeSpan Negate() const { return wxTimeSpan(-GetValue()); }
989 // negate the value of the timespan
990 wxTimeSpan& Neg() { m_diff = -GetValue(); return *this; }
991 // negate the value of the timespan
992 wxTimeSpan& operator-() { return Neg(); }
993
994 // return the absolute value of the timespan: does _not_ modify the
995 // object
996 inline wxTimeSpan Abs() const;
997
998 // there is intentionally no division because we don't want to
999 // introduce rounding errors in time calculations
1000
1001 // comparaison (see also operator versions below)
1002 // ------------------------------------------------------------------------
1003
1004 // is the timespan null?
1005 bool IsNull() const { return m_diff == 0l; }
1006 // returns true if the timespan is null
1007 bool operator!() const { return !IsNull(); }
1008
1009 // is the timespan positive?
1010 bool IsPositive() const { return m_diff > 0l; }
1011
1012 // is the timespan negative?
1013 bool IsNegative() const { return m_diff < 0l; }
1014
1015 // are two timespans equal?
1016 inline bool IsEqualTo(const wxTimeSpan& ts) const;
1017 // compare two timestamps: works with the absolute values, i.e. -2
1018 // hours is longer than 1 hour. Also, it will return FALSE if the
1019 // timespans are equal in absolute value.
1020 inline bool IsLongerThan(const wxTimeSpan& ts) const;
1021 // compare two timestamps: works with the absolute values, i.e. 1
1022 // hour is shorter than -2 hours. Also, it will return FALSE if the
1023 // timespans are equal in absolute value.
1024 bool IsShorterThan(const wxTimeSpan& t) const { return !IsLongerThan(t); }
1025
1026 // breaking into days, hours, minutes and seconds
1027 // ------------------------------------------------------------------------
1028
1029 // get the max number of weeks in this timespan
1030 inline int GetWeeks() const;
1031 // get the max number of days in this timespan
1032 inline int GetDays() const;
1033 // get the max number of hours in this timespan
1034 inline int GetHours() const;
1035 // get the max number of minutes in this timespan
1036 inline int GetMinutes() const;
1037 // get the max number of seconds in this timespan
1038 inline wxLongLong GetSeconds() const;
1039 // get the number of milliseconds in this timespan
1040 wxLongLong GetMilliseconds() const { return m_diff; }
1041
1042 // conversion to text
1043 // ------------------------------------------------------------------------
1044
1045 // this function accepts strftime()-like format string (default
1046 // argument corresponds to the preferred date and time representation
1047 // for the current locale) and returns the string containing the
1048 // resulting text representation. Notice that only some of format
1049 // specifiers valid for wxDateTime are valid for wxTimeSpan: hours,
1050 // minutes and seconds make sense, but not "PM/AM" string for example.
1051 wxString Format(const wxChar *format = _T("%c")) const;
1052 // preferred date representation for the current locale
1053 wxString FormatDate() const { return Format(_T("%x")); }
1054 // preferred time representation for the current locale
1055 wxString FormatTime() const { return Format(_T("%X")); }
1056
1057 // implementation
1058 // ------------------------------------------------------------------------
1059
1060 // construct from internal representation
1061 wxTimeSpan(const wxLongLong& diff) { m_diff = diff; }
1062
1063 // get the internal representation
1064 wxLongLong GetValue() const { return m_diff; }
1065
1066 private:
1067 // the (signed) time span in milliseconds
1068 wxLongLong m_diff;
1069 };
1070
1071 // ----------------------------------------------------------------------------
1072 // This class is a "logical time span" and is useful for implementing program
1073 // logic for such things as "add one month to the date" which, in general,
1074 // doesn't mean to add 60*60*24*31 seconds to it, but to take the same date
1075 // the next month (to understand that this is indeed different consider adding
1076 // one month to Feb, 15 - we want to get Mar, 15, of course).
1077 //
1078 // When adding a month to the date, all lesser components (days, hours, ...)
1079 // won't be changed.
1080 //
1081 // wxDateSpan can be either positive or negative. They may be
1082 // multiplied by scalars which multiply all deltas by the scalar: i.e. 2*(1
1083 // month and 1 day) is 2 months and 2 days. They can be added together and
1084 // with wxDateTime or wxTimeSpan, but the type of result is different for each
1085 // case.
1086 //
1087 // Beware about weeks: if you specify both weeks and days, the total number of
1088 // days added will be 7*weeks + days! See also GetTotalDays() function.
1089 //
1090 // Finally, notice that for adding hours, minutes &c you don't need this
1091 // class: wxTimeSpan will do the job because there are no subtleties
1092 // associated with those.
1093 // ----------------------------------------------------------------------------
1094
1095 class WXDLLEXPORT wxDateSpan
1096 {
1097 public:
1098 // constructors
1099 // ------------------------------------------------------------------------
1100
1101 // this many years/months/weeks/days
1102 wxDateSpan(int years = 0, int months = 0, int weeks = 0, int days = 0)
1103 {
1104 m_years = years;
1105 m_months = months;
1106 m_weeks = weeks;
1107 m_days = days;
1108 }
1109
1110 // get an object for the given number of days
1111 static wxDateSpan Days(int days) { return wxDateSpan(0, 0, 0, days); }
1112 static wxDateSpan Day() { return Days(1); }
1113
1114 // get an object for the given number of weeks
1115 static wxDateSpan Weeks(int weeks) { return wxDateSpan(0, 0, weeks, 0); }
1116 static wxDateSpan Week() { return Weeks(1); }
1117
1118 // get an object for the given number of months
1119 static wxDateSpan Months(int mon) { return wxDateSpan(0, mon, 0, 0); }
1120 static wxDateSpan Month() { return Months(1); }
1121
1122 // get an object for the given number of years
1123 static wxDateSpan Years(int years) { return wxDateSpan(years, 0, 0, 0); }
1124 static wxDateSpan Year() { return Years(1); }
1125
1126 // default copy ctor is ok
1127
1128 // no dtor
1129
1130 // accessors (all SetXXX() return the (modified) wxDateSpan object)
1131 // ------------------------------------------------------------------------
1132
1133 // set number of years
1134 wxDateSpan& SetYears(int n) { m_years = n; return *this; }
1135 // set number of months
1136 wxDateSpan& SetMonths(int n) { m_months = n; return *this; }
1137 // set number of weeks
1138 wxDateSpan& SetWeeks(int n) { m_weeks = n; return *this; }
1139 // set number of days
1140 wxDateSpan& SetDays(int n) { m_days = n; return *this; }
1141
1142 // get number of years
1143 int GetYears() const { return m_years; }
1144 // get number of months
1145 int GetMonths() const { return m_months; }
1146 // get number of weeks
1147 int GetWeeks() const { return m_weeks; }
1148 // get number of days
1149 int GetDays() const { return m_days; }
1150 // returns 7*GetWeeks() + GetDays()
1151 int GetTotalDays() const { return 7*m_weeks + m_days; }
1152
1153 // arithmetics with date spans (see also below for more operators)
1154 // ------------------------------------------------------------------------
1155
1156 // return sum of two date spans
1157 inline wxDateSpan Add(const wxDateSpan& other) const;
1158 // add another wxDateSpan to us
1159 inline wxDateSpan& Add(const wxDateSpan& other);
1160 // add another wxDateSpan to us
1161 inline wxDateSpan& operator+=(const wxDateSpan& other);
1162
1163 // return difference of two date spans
1164 inline wxDateSpan Substract(const wxDateSpan& other) const;
1165 // substract another wxDateSpan from us
1166 inline wxDateSpan& Substract(const wxDateSpan& other);
1167 // substract another wxDateSpan from us
1168 inline wxDateSpan& operator-=(const wxDateSpan& other);
1169
1170 // return a copy of this time span with changed sign
1171 inline wxDateSpan Negate() const;
1172 // inverse the sign of this timespan
1173 inline wxDateSpan& Neg();
1174 // inverse the sign of this timespan
1175 wxDateSpan& operator-() { return Neg(); }
1176
1177 // return the date span proportional to this one with given factor
1178 inline wxDateSpan Multiply(int factor) const;
1179 // multiply all components by a (signed) number
1180 inline wxDateSpan& Multiply(int factor);
1181 // multiply all components by a (signed) number
1182 inline wxDateSpan& operator*=(int factor) { return Multiply(factor); }
1183
1184 private:
1185 int m_years,
1186 m_months,
1187 m_weeks,
1188 m_days;
1189 };
1190
1191 WXDLLEXPORT_DATA(extern wxDateSpan) wxYear;
1192 WXDLLEXPORT_DATA(extern wxDateSpan) wxMonth;
1193 WXDLLEXPORT_DATA(extern wxDateSpan) wxWeek;
1194 WXDLLEXPORT_DATA(extern wxDateSpan) wxDay;
1195
1196 // ============================================================================
1197 // inline functions implementation
1198 // ============================================================================
1199
1200 // don't include inline functions definitions when we're included from anything
1201 // else than datetime.cpp in debug builds: this minimizes rebuilds if we change
1202 // some inline function and the performance doesn't matter in the debug builds.
1203
1204 #if !defined(__WXDEBUG__) || defined(wxDEFINE_TIME_CONSTANTS)
1205 #define INCLUDED_FROM_WX_DATETIME_H
1206 #include "wx/datetime.inl"
1207 #undef INCLUDED_FROM_WX_DATETIME_H
1208 #endif
1209
1210 // if we defined it to be empty above, restore it now
1211 #undef inline
1212
1213 // ============================================================================
1214 // binary operators
1215 // ============================================================================
1216
1217 // ----------------------------------------------------------------------------
1218 // wxDateTime operators
1219 // ----------------------------------------------------------------------------
1220
1221 // arithmetics
1222 // -----------
1223
1224 // no need to check for validity - the member functions we call will do it
1225
1226 inline wxDateTime WXDLLEXPORT operator+(const wxDateTime& dt,
1227 const wxTimeSpan& ts)
1228 {
1229 return dt.Add(ts);
1230 }
1231
1232 inline wxDateTime WXDLLEXPORT operator-(const wxDateTime& dt,
1233 const wxTimeSpan& ts)
1234 {
1235 return dt.Substract(ts);
1236 }
1237
1238 inline wxDateTime WXDLLEXPORT operator+(const wxDateTime& dt,
1239 const wxDateSpan& ds)
1240 {
1241 return dt.Add(ds);
1242 }
1243
1244 inline wxDateTime WXDLLEXPORT operator-(const wxDateTime& dt,
1245 const wxDateSpan& ds)
1246 {
1247 return dt.Substract(ds);
1248 }
1249
1250 inline wxTimeSpan WXDLLEXPORT operator-(const wxDateTime& dt1,
1251 const wxDateTime& dt2)
1252 {
1253 return dt1.Substract(dt2);
1254 }
1255
1256 // comparison
1257 // ----------
1258
1259 inline bool WXDLLEXPORT operator<(const wxDateTime& t1, const wxDateTime& t2)
1260 {
1261 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") );
1262
1263 return t1.GetValue() < t2.GetValue();
1264 }
1265
1266 inline bool WXDLLEXPORT operator<=(const wxDateTime& t1, const wxDateTime& t2)
1267 {
1268 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") );
1269
1270 return t1.GetValue() <= t2.GetValue();
1271 }
1272
1273 inline bool WXDLLEXPORT operator>(const wxDateTime& t1, const wxDateTime& t2)
1274 {
1275 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") );
1276
1277 return t1.GetValue() > t2.GetValue();
1278 }
1279
1280 inline bool WXDLLEXPORT operator>=(const wxDateTime& t1, const wxDateTime& t2)
1281 {
1282 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") );
1283
1284 return t1.GetValue() >= t2.GetValue();
1285 }
1286
1287 inline bool WXDLLEXPORT operator==(const wxDateTime& t1, const wxDateTime& t2)
1288 {
1289 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") );
1290
1291 return t1.GetValue() == t2.GetValue();
1292 }
1293
1294 inline bool WXDLLEXPORT operator!=(const wxDateTime& t1, const wxDateTime& t2)
1295 {
1296 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") );
1297
1298 return t1.GetValue() != t2.GetValue();
1299 }
1300
1301 // ----------------------------------------------------------------------------
1302 // wxTimeSpan operators
1303 // ----------------------------------------------------------------------------
1304
1305 // arithmetics
1306 // -----------
1307
1308 inline wxTimeSpan WXDLLEXPORT operator+(const wxTimeSpan& ts1,
1309 const wxTimeSpan& ts2)
1310 {
1311 return wxTimeSpan(ts1.GetValue() + ts2.GetValue());
1312 }
1313
1314 inline wxTimeSpan WXDLLEXPORT operator-(const wxTimeSpan& ts1,
1315 const wxTimeSpan& ts2)
1316 {
1317 return wxTimeSpan(ts1.GetValue() - ts2.GetValue());
1318 }
1319
1320 inline wxTimeSpan WXDLLEXPORT operator*(const wxTimeSpan& ts, int n)
1321 {
1322 return wxTimeSpan(ts).Multiply(n);
1323 }
1324
1325 inline wxTimeSpan WXDLLEXPORT operator*(int n, const wxTimeSpan& ts)
1326 {
1327 return wxTimeSpan(ts).Multiply(n);
1328 }
1329
1330 // comparison
1331 // ----------
1332
1333 inline bool WXDLLEXPORT operator<(const wxTimeSpan &t1, const wxTimeSpan &t2)
1334 {
1335 return t1.GetValue() < t2.GetValue();
1336 }
1337
1338 inline bool WXDLLEXPORT operator<=(const wxTimeSpan &t1, const wxTimeSpan &t2)
1339 {
1340 return t1.GetValue() <= t2.GetValue();
1341 }
1342
1343 inline bool WXDLLEXPORT operator>(const wxTimeSpan &t1, const wxTimeSpan &t2)
1344 {
1345 return t1.GetValue() > t2.GetValue();
1346 }
1347
1348 inline bool WXDLLEXPORT operator>=(const wxTimeSpan &t1, const wxTimeSpan &t2)
1349 {
1350 return t1.GetValue() >= t2.GetValue();
1351 }
1352
1353 inline bool WXDLLEXPORT operator==(const wxTimeSpan &t1, const wxTimeSpan &t2)
1354 {
1355 return t1.GetValue() == t2.GetValue();
1356 }
1357
1358 inline bool WXDLLEXPORT operator!=(const wxTimeSpan &t1, const wxTimeSpan &t2)
1359 {
1360 return t1.GetValue() != t2.GetValue();
1361 }
1362
1363 // ----------------------------------------------------------------------------
1364 // wxDateSpan
1365 // ----------------------------------------------------------------------------
1366
1367 // arithmetics
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& ds1,
1380 const wxDateSpan& ds2)
1381 {
1382 return wxDateSpan(ds1.GetYears() - ds2.GetYears(),
1383 ds1.GetMonths() - ds2.GetMonths(),
1384 ds1.GetWeeks() - ds2.GetWeeks(),
1385 ds1.GetDays() - ds2.GetDays());
1386 }
1387
1388 inline WXDLLEXPORT wxDateSpan operator*(const wxDateSpan& ds, int n)
1389 {
1390 return wxDateSpan(ds).Multiply(n);
1391 }
1392
1393 inline WXDLLEXPORT wxDateSpan operator*(int n, const wxDateSpan& ds)
1394 {
1395 return wxDateSpan(ds).Multiply(n);
1396 }
1397
1398 // ============================================================================
1399 // other helper functions
1400 // ============================================================================
1401
1402 // ----------------------------------------------------------------------------
1403 // iteration helpers: can be used to write a for loop over enum variable like
1404 // this:
1405 // for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) )
1406 // ----------------------------------------------------------------------------
1407
1408 inline WXDLLEXPORT void wxNextMonth(wxDateTime::Month& m)
1409 {
1410 wxASSERT_MSG( m < wxDateTime::Inv_Month, _T("invalid month") );
1411
1412 // no wrapping or the for loop above would never end!
1413 m = (wxDateTime::Month)(m + 1);
1414 }
1415
1416 inline WXDLLEXPORT void wxPrevMonth(wxDateTime::Month& m)
1417 {
1418 wxASSERT_MSG( m < wxDateTime::Inv_Month, _T("invalid month") );
1419
1420 m = m == wxDateTime::Jan ? wxDateTime::Inv_Month
1421 : (wxDateTime::Month)(m - 1);
1422 }
1423
1424 inline WXDLLEXPORT void wxNextWDay(wxDateTime::WeekDay& wd)
1425 {
1426 wxASSERT_MSG( wd < wxDateTime::Inv_WeekDay, _T("invalid week day") );
1427
1428 // no wrapping or the for loop above would never end!
1429 wd = (wxDateTime::WeekDay)(wd + 1);
1430 }
1431
1432 inline WXDLLEXPORT void wxPrevWDay(wxDateTime::WeekDay& wd)
1433 {
1434 wxASSERT_MSG( wd < wxDateTime::Inv_WeekDay, _T("invalid week day") );
1435
1436 wd = wd == wxDateTime::Sun ? wxDateTime::Inv_WeekDay
1437 : (wxDateTime::WeekDay)(wd - 1);
1438 }
1439
1440 #endif // _WX_DATETIME_H