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