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