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