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