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