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