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