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