1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: declarations of time/date related classes (wxDateTime,
5 // Author: Vadim Zeitlin
9 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 #ifndef _WX_DATETIME_H
14 #define _WX_DATETIME_H
16 #if defined(__GNUG__) && !defined(__APPLE__)
17 #pragma interface "datetime.h"
25 #include <limits.h> // for INT_MIN
27 #include "wx/longlong.h"
29 class WXDLLEXPORT wxDateTime
;
30 class WXDLLEXPORT wxTimeSpan
;
31 class WXDLLEXPORT wxDateSpan
;
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
38 // For Mingw32, causes a link error. (VZ: why?)
39 #if defined( __WXDEBUG__) && !defined(__MINGW32__) && !(defined(_MSC_VER) && wxUSE_ACCESSIBILITY)
40 #define wxDATETIME_DONT_INLINE
46 #undef wxDATETIME_DONT_INLINE
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
53 #if defined(__WXMAC__) && !defined(__DARWIN__) && __MSL__ < 0x6000
54 #define WX_TIME_BASE_OFFSET ( 2082844800L + 126144000L )
56 #define WX_TIME_BASE_OFFSET 0
61 * + 1. Time zones with minutes (make TimeZone a class)
62 * ? 2. getdate() function like under Solaris
63 * + 3. text conversion for wxDateSpan
64 * + 4. pluggable modules for the workdays calculations
65 * 5. wxDateTimeHolidayAuthority for Easter and other christian feasts
69 The three (main) classes declared in this header represent:
71 1. An absolute moment in the time (wxDateTime)
72 2. A difference between two moments in the time, positive or negative
74 3. A logical difference between two dates expressed in
75 years/months/weeks/days (wxDateSpan)
77 The following arithmetic operations are permitted (all others are not):
82 wxDateTime + wxTimeSpan = wxDateTime
83 wxDateTime + wxDateSpan = wxDateTime
84 wxTimeSpan + wxTimeSpan = wxTimeSpan
85 wxDateSpan + wxDateSpan = wxDateSpan
89 wxDateTime - wxDateTime = wxTimeSpan
90 wxDateTime - wxTimeSpan = wxDateTime
91 wxDateTime - wxDateSpan = wxDateTime
92 wxTimeSpan - wxTimeSpan = wxTimeSpan
93 wxDateSpan - wxDateSpan = wxDateSpan
97 wxTimeSpan * number = wxTimeSpan
98 number * wxTimeSpan = wxTimeSpan
99 wxDateSpan * number = wxDateSpan
100 number * wxDateSpan = wxDateSpan
104 -wxTimeSpan = wxTimeSpan
105 -wxDateSpan = wxDateSpan
107 For each binary operation OP (+, -, *) we have the following operatorOP=() as
108 a method and the method with a symbolic name OPER (Add, Subtract, Multiply)
109 as a synonym for it and another const method with the same name which returns
110 the changed copy of the object and operatorOP() as a global function which is
111 implemented in terms of the const version of OPEN. For the unary - we have
112 operator-() as a method, Neg() as synonym for it and Negate() which returns
113 the copy of the object with the changed sign.
116 // an invalid/default date time object which may be used as the default
117 // argument for arguments of type wxDateTime; it is also returned by all
118 // functions returning wxDateTime on failure (this is why it is also called
119 // wxInvalidDateTime)
120 class WXDLLEXPORT wxDateTime
;
122 WXDLLEXPORT_DATA(extern const wxDateTime
) wxDefaultDateTime
;
123 #define wxInvalidDateTime wxDefaultDateTime
125 // ----------------------------------------------------------------------------
126 // wxDateTime represents an absolute moment in the time
127 // ----------------------------------------------------------------------------
129 class WXDLLEXPORT wxDateTime
133 // ------------------------------------------------------------------------
135 // a small unsigned integer type for storing things like minutes,
136 // seconds &c. It should be at least short (i.e. not char) to contain
137 // the number of milliseconds - it may also be 'int' because there is
138 // no size penalty associated with it in our code, we don't store any
139 // data in this format
140 typedef unsigned short wxDateTime_t
;
143 // ------------------------------------------------------------------------
148 // the time in the current time zone
151 // zones from GMT (= Greenwhich Mean Time): they're guaranteed to be
152 // consequent numbers, so writing something like `GMT0 + offset' is
153 // safe if abs(offset) <= 12
155 // underscore stands for minus
156 GMT_12
, GMT_11
, GMT_10
, GMT_9
, GMT_8
, GMT_7
,
157 GMT_6
, GMT_5
, GMT_4
, GMT_3
, GMT_2
, GMT_1
,
159 GMT1
, GMT2
, GMT3
, GMT4
, GMT5
, GMT6
,
160 GMT7
, GMT8
, GMT9
, GMT10
, GMT11
, GMT12
,
161 // Note that GMT12 and GMT_12 are not the same: there is a difference
162 // of exactly one day between them
164 // some symbolic names for TZ
167 WET
= GMT0
, // Western Europe Time
168 WEST
= GMT1
, // Western Europe Summer Time
169 CET
= GMT1
, // Central Europe Time
170 CEST
= GMT2
, // Central Europe Summer Time
171 EET
= GMT2
, // Eastern Europe Time
172 EEST
= GMT3
, // Eastern Europe Summer Time
173 MSK
= GMT3
, // Moscow Time
174 MSD
= GMT4
, // Moscow Summer Time
177 AST
= GMT_4
, // Atlantic Standard Time
178 ADT
= GMT_3
, // Atlantic Daylight Time
179 EST
= GMT_5
, // Eastern Standard Time
180 EDT
= GMT_4
, // Eastern Daylight Saving Time
181 CST
= GMT_6
, // Central Standard Time
182 CDT
= GMT_5
, // Central Daylight Saving Time
183 MST
= GMT_7
, // Mountain Standard Time
184 MDT
= GMT_6
, // Mountain Daylight Saving Time
185 PST
= GMT_8
, // Pacific Standard Time
186 PDT
= GMT_7
, // Pacific Daylight Saving Time
187 HST
= GMT_10
, // Hawaiian Standard Time
188 AKST
= GMT_9
, // Alaska Standard Time
189 AKDT
= GMT_8
, // Alaska Daylight Saving Time
193 A_WST
= GMT8
, // Western Standard Time
194 A_CST
= GMT12
+ 1, // Central Standard Time (+9.5)
195 A_EST
= GMT10
, // Eastern Standard Time
196 A_ESST
= GMT11
, // Eastern Summer Time
198 // TODO add more symbolic timezone names here
200 // Universal Coordinated Time = the new and politically correct name
205 // the calendar systems we know about: notice that it's valid (for
206 // this classes purpose anyhow) to work with any of these calendars
207 // even with the dates before the historical appearance of the
211 Gregorian
, // current calendar
212 Julian
// calendar in use since -45 until the 1582 (or later)
214 // TODO Hebrew, Chinese, Maya, ... (just kidding) (or then may be not?)
217 // these values only are used to identify the different dates of
218 // adoption of the Gregorian calendar (see IsGregorian())
220 // All data and comments taken verbatim from "The Calendar FAQ (v 2.0)"
221 // by Claus Tøndering, http://www.pip.dknet.dk/~c-t/calendar.html
222 // except for the comments "we take".
224 // Symbol "->" should be read as "was followed by" in the comments
226 enum GregorianAdoption
228 Gr_Unknown
, // no data for this country or it's too uncertain to use
229 Gr_Standard
, // on the day 0 of Gregorian calendar: 15 Oct 1582
231 Gr_Alaska
, // Oct 1867 when Alaska became part of the USA
232 Gr_Albania
, // Dec 1912
234 Gr_Austria
= Gr_Unknown
, // Different regions on different dates
235 Gr_Austria_Brixen
, // 5 Oct 1583 -> 16 Oct 1583
236 Gr_Austria_Salzburg
= Gr_Austria_Brixen
,
237 Gr_Austria_Tyrol
= Gr_Austria_Brixen
,
238 Gr_Austria_Carinthia
, // 14 Dec 1583 -> 25 Dec 1583
239 Gr_Austria_Styria
= Gr_Austria_Carinthia
,
241 Gr_Belgium
, // Then part of the Netherlands
243 Gr_Bulgaria
= Gr_Unknown
, // Unknown precisely (from 1915 to 1920)
244 Gr_Bulgaria_1
, // 18 Mar 1916 -> 1 Apr 1916
245 Gr_Bulgaria_2
, // 31 Mar 1916 -> 14 Apr 1916
246 Gr_Bulgaria_3
, // 3 Sep 1920 -> 17 Sep 1920
248 Gr_Canada
= Gr_Unknown
, // Different regions followed the changes in
249 // Great Britain or France
251 Gr_China
= Gr_Unknown
, // Different authorities say:
252 Gr_China_1
, // 18 Dec 1911 -> 1 Jan 1912
253 Gr_China_2
, // 18 Dec 1928 -> 1 Jan 1929
255 Gr_Czechoslovakia
, // (Bohemia and Moravia) 6 Jan 1584 -> 17 Jan 1584
256 Gr_Denmark
, // (including Norway) 18 Feb 1700 -> 1 Mar 1700
259 Gr_Finland
, // Then part of Sweden
261 Gr_France
, // 9 Dec 1582 -> 20 Dec 1582
262 Gr_France_Alsace
, // 4 Feb 1682 -> 16 Feb 1682
263 Gr_France_Lorraine
, // 16 Feb 1760 -> 28 Feb 1760
264 Gr_France_Strasbourg
, // February 1682
266 Gr_Germany
= Gr_Unknown
, // Different states on different dates:
267 Gr_Germany_Catholic
, // 1583-1585 (we take 1584)
268 Gr_Germany_Prussia
, // 22 Aug 1610 -> 2 Sep 1610
269 Gr_Germany_Protestant
, // 18 Feb 1700 -> 1 Mar 1700
271 Gr_GreatBritain
, // 2 Sep 1752 -> 14 Sep 1752 (use 'cal(1)')
273 Gr_Greece
, // 9 Mar 1924 -> 23 Mar 1924
274 Gr_Hungary
, // 21 Oct 1587 -> 1 Nov 1587
275 Gr_Ireland
= Gr_GreatBritain
,
276 Gr_Italy
= Gr_Standard
,
278 Gr_Japan
= Gr_Unknown
, // Different authorities say:
279 Gr_Japan_1
, // 19 Dec 1872 -> 1 Jan 1873
280 Gr_Japan_2
, // 19 Dec 1892 -> 1 Jan 1893
281 Gr_Japan_3
, // 18 Dec 1918 -> 1 Jan 1919
283 Gr_Latvia
, // 1915-1918 (we take 1915)
284 Gr_Lithuania
, // 1915
285 Gr_Luxemburg
, // 14 Dec 1582 -> 25 Dec 1582
286 Gr_Netherlands
= Gr_Belgium
, // (including Belgium) 1 Jan 1583
288 // this is too weird to take into account: the Gregorian calendar was
289 // introduced twice in Groningen, first time 28 Feb 1583 was followed
290 // by 11 Mar 1583, then it has gone back to Julian in the summer of
291 // 1584 and then 13 Dec 1700 -> 12 Jan 1701 - which is
292 // the date we take here
293 Gr_Netherlands_Groningen
, // 13 Dec 1700 -> 12 Jan 1701
294 Gr_Netherlands_Gelderland
, // 30 Jun 1700 -> 12 Jul 1700
295 Gr_Netherlands_Utrecht
, // (and Overijssel) 30 Nov 1700->12 Dec 1700
296 Gr_Netherlands_Friesland
, // (and Drenthe) 31 Dec 1700 -> 12 Jan 1701
298 Gr_Norway
= Gr_Denmark
, // Then part of Denmark
299 Gr_Poland
= Gr_Standard
,
300 Gr_Portugal
= Gr_Standard
,
301 Gr_Romania
, // 31 Mar 1919 -> 14 Apr 1919
302 Gr_Russia
, // 31 Jan 1918 -> 14 Feb 1918
303 Gr_Scotland
= Gr_GreatBritain
,
304 Gr_Spain
= Gr_Standard
,
306 // Sweden has a curious history. Sweden decided to make a gradual
307 // change from the Julian to the Gregorian calendar. By dropping every
308 // leap year from 1700 through 1740 the eleven superfluous days would
309 // be omitted and from 1 Mar 1740 they would be in sync with the
310 // Gregorian calendar. (But in the meantime they would be in sync with
313 // So 1700 (which should have been a leap year in the Julian calendar)
314 // was not a leap year in Sweden. However, by mistake 1704 and 1708
315 // became leap years. This left Sweden out of synchronisation with
316 // both the Julian and the Gregorian world, so they decided to go back
317 // to the Julian calendar. In order to do this, they inserted an extra
318 // day in 1712, making that year a double leap year! So in 1712,
319 // February had 30 days in Sweden.
321 // Later, in 1753, Sweden changed to the Gregorian calendar by
322 // dropping 11 days like everyone else.
323 Gr_Sweden
= Gr_Finland
, // 17 Feb 1753 -> 1 Mar 1753
325 Gr_Switzerland
= Gr_Unknown
,// Different cantons used different dates
326 Gr_Switzerland_Catholic
, // 1583, 1584 or 1597 (we take 1584)
327 Gr_Switzerland_Protestant
, // 31 Dec 1700 -> 12 Jan 1701
329 Gr_Turkey
, // 1 Jan 1927
330 Gr_USA
= Gr_GreatBritain
,
331 Gr_Wales
= Gr_GreatBritain
,
332 Gr_Yugoslavia
// 1919
335 // the country parameter is used so far for calculating the start and
336 // the end of DST period and for deciding whether the date is a work
339 // TODO move this to intl.h
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
346 // TODO add more countries (for this we must know about DST and/or
347 // holidays for this country)
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
,
356 Country_WesternEurope_End
= UK
,
363 // symbolic names for the months
366 Jan
, Feb
, Mar
, Apr
, May
, Jun
, Jul
, Aug
, Sep
, Oct
, Nov
, Dec
, Inv_Month
369 // symbolic names for the weekdays
372 Sun
, Mon
, Tue
, Wed
, Thu
, Fri
, Sat
, Inv_WeekDay
375 // invalid value for the year
378 Inv_Year
= SHRT_MIN
// should hold in wxDateTime_t
381 // flags for GetWeekDayName and GetMonthName
384 Name_Full
= 0x01, // return full name
385 Name_Abbr
= 0x02 // return abbreviated name
388 // flags for GetWeekOfYear and GetWeekOfMonth
391 Default_First
, // Sunday_First for US, Monday_First for the rest
392 Monday_First
, // week starts with a Monday
393 Sunday_First
// week starts with a Sunday
397 // ------------------------------------------------------------------------
399 // a class representing a time zone: basicly, this is just an offset
400 // (in seconds) from GMT
401 class WXDLLEXPORT TimeZone
405 TimeZone(wxDateTime_t offset
= 0) { m_offset
= offset
; }
407 long GetOffset() const { return m_offset
; }
410 // offset for this timezone from GMT in seconds
414 // standard struct tm is limited to the years from 1900 (because
415 // tm_year field is the offset from 1900), so we use our own struct
416 // instead to represent broken down time
418 // NB: this struct should always be kept normalized (i.e. mon should
419 // be < 12, 1 <= day <= 31 &c), so use AddMonths(), AddDays()
420 // instead of modifying the member fields directly!
421 struct WXDLLEXPORT Tm
423 wxDateTime_t msec
, sec
, min
, hour
, mday
;
427 // default ctor inits the object to an invalid value
430 // ctor from struct tm and the timezone
431 Tm(const struct tm
& tm
, const TimeZone
& tz
);
433 // check that the given date/time is valid (in Gregorian calendar)
434 bool IsValid() const;
437 WeekDay
GetWeekDay() // not const because wday may be changed
439 if ( wday
== Inv_WeekDay
)
442 return (WeekDay
)wday
;
445 // add the given number of months to the date keeping it normalized
446 void AddMonths(int monDiff
);
448 // add the given number of months to the date keeping it normalized
449 void AddDays(int dayDiff
);
452 // compute the weekday from other fields
453 void ComputeWeekDay();
455 // the timezone we correspond to
458 // these values can't be accessed directly because they're not always
459 // computed and we calculate them on demand
460 wxDateTime_t wday
, yday
;
464 // ------------------------------------------------------------------------
466 // set the current country
467 static void SetCountry(Country country
);
468 // get the current country
469 static Country
GetCountry();
471 // return TRUE if the country is a West European one (in practice,
472 // this means that the same DST rules as for EEC apply)
473 static bool IsWestEuropeanCountry(Country country
= Country_Default
);
475 // return the current year
476 static int GetCurrentYear(Calendar cal
= Gregorian
);
478 // convert the year as returned by wxDateTime::GetYear() to a year
479 // suitable for BC/AD notation. The difference is that BC year 1
480 // corresponds to the year 0 (while BC year 0 didn't exist) and AD
481 // year N is just year N.
482 static int ConvertYearToBC(int year
);
484 // return the current month
485 static Month
GetCurrentMonth(Calendar cal
= Gregorian
);
487 // returns TRUE if the given year is a leap year in the given calendar
488 static bool IsLeapYear(int year
= Inv_Year
, Calendar cal
= Gregorian
);
490 // get the century (19 for 1999, 20 for 2000 and -5 for 492 BC)
491 static int GetCentury(int year
= Inv_Year
);
493 // returns the number of days in this year (356 or 355 for Gregorian
494 // calendar usually :-)
495 static wxDateTime_t
GetNumberOfDays(int year
, Calendar cal
= Gregorian
);
497 // get the number of the days in the given month (default value for
498 // the year means the current one)
499 static wxDateTime_t
GetNumberOfDays(Month month
,
501 Calendar cal
= Gregorian
);
503 // get the full (default) or abbreviated month name in the current
504 // locale, returns empty string on error
505 static wxString
GetMonthName(Month month
,
506 NameFlags flags
= Name_Full
);
508 // get the full (default) or abbreviated weekday name in the current
509 // locale, returns empty string on error
510 static wxString
GetWeekDayName(WeekDay weekday
,
511 NameFlags flags
= Name_Full
);
513 // get the AM and PM strings in the current locale (may be empty)
514 static void GetAmPmStrings(wxString
*am
, wxString
*pm
);
516 // return TRUE if the given country uses DST for this year
517 static bool IsDSTApplicable(int year
= Inv_Year
,
518 Country country
= Country_Default
);
520 // get the beginning of DST for this year, will return invalid object
521 // if no DST applicable in this year. The default value of the
522 // parameter means to take the current year.
523 static wxDateTime
GetBeginDST(int year
= Inv_Year
,
524 Country country
= Country_Default
);
525 // get the end of DST for this year, will return invalid object
526 // if no DST applicable in this year. The default value of the
527 // parameter means to take the current year.
528 static wxDateTime
GetEndDST(int year
= Inv_Year
,
529 Country country
= Country_Default
);
531 // return the wxDateTime object for the current time
532 static inline wxDateTime
Now();
534 // return the wxDateTime object for the current time with millisecond
535 // precision (if available on this platform)
536 static wxDateTime
UNow();
538 // return the wxDateTime object for today midnight: i.e. as Now() but
539 // with time set to 0
540 static inline wxDateTime
Today();
542 // constructors: you should test whether the constructor succeeded with
543 // IsValid() function. The values Inv_Month and Inv_Year for the
544 // parameters mean take current month and/or year values.
545 // ------------------------------------------------------------------------
547 // default ctor does not initialize the object, use Set()!
548 wxDateTime() { m_time
= wxLongLong((long)ULONG_MAX
, ULONG_MAX
); }
550 // from time_t: seconds since the Epoch 00:00:00 UTC, Jan 1, 1970)
551 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
552 // VA C++ confuses this with wxDateTime(double jdn) thinking it is a duplicate declaration
553 inline wxDateTime(time_t timet
);
555 // from broken down time/date (only for standard Unix range)
556 inline wxDateTime(const struct tm
& tm
);
557 // from broken down time/date (any range)
558 inline wxDateTime(const Tm
& tm
);
560 // from JDN (beware of rounding errors)
561 inline wxDateTime(double jdn
);
563 // from separate values for each component, date set to today
564 inline wxDateTime(wxDateTime_t hour
,
565 wxDateTime_t minute
= 0,
566 wxDateTime_t second
= 0,
567 wxDateTime_t millisec
= 0);
568 // from separate values for each component with explicit date
569 inline wxDateTime(wxDateTime_t day
, // day of the month
571 int year
= Inv_Year
, // 1999, not 99 please!
572 wxDateTime_t hour
= 0,
573 wxDateTime_t minute
= 0,
574 wxDateTime_t second
= 0,
575 wxDateTime_t millisec
= 0);
577 // default copy ctor ok
581 // assignment operators and Set() functions: all non const methods return
582 // the reference to this object. IsValid() should be used to test whether
583 // the function succeeded.
584 // ------------------------------------------------------------------------
586 // set to the current time
587 inline wxDateTime
& SetToCurrent();
589 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
590 // VA C++ confuses this with wxDateTime(double jdn) thinking it is a duplicate declaration
591 // set to given time_t value
592 inline wxDateTime
& Set(time_t timet
);
595 // set to given broken down time/date
596 wxDateTime
& Set(const struct tm
& tm
);
598 // set to given broken down time/date
599 inline wxDateTime
& Set(const Tm
& tm
);
601 // set to given JDN (beware of rounding errors)
602 wxDateTime
& Set(double jdn
);
604 // set to given time, date = today
605 wxDateTime
& Set(wxDateTime_t hour
,
606 wxDateTime_t minute
= 0,
607 wxDateTime_t second
= 0,
608 wxDateTime_t millisec
= 0);
610 // from separate values for each component with explicit date
611 // (defaults for month and year are the current values)
612 wxDateTime
& Set(wxDateTime_t day
,
614 int year
= Inv_Year
, // 1999, not 99 please!
615 wxDateTime_t hour
= 0,
616 wxDateTime_t minute
= 0,
617 wxDateTime_t second
= 0,
618 wxDateTime_t millisec
= 0);
620 // resets time to 00:00:00, doesn't change the date
621 wxDateTime
& ResetTime();
623 // the following functions don't change the values of the other
624 // fields, i.e. SetMinute() won't change either hour or seconds value
627 wxDateTime
& SetYear(int year
);
629 wxDateTime
& SetMonth(Month month
);
630 // set the day of the month
631 wxDateTime
& SetDay(wxDateTime_t day
);
633 wxDateTime
& SetHour(wxDateTime_t hour
);
635 wxDateTime
& SetMinute(wxDateTime_t minute
);
637 wxDateTime
& SetSecond(wxDateTime_t second
);
639 wxDateTime
& SetMillisecond(wxDateTime_t millisecond
);
641 // assignment operator from time_t
642 wxDateTime
& operator=(time_t timet
) { return Set(timet
); }
644 // assignment operator from broken down time/date
645 wxDateTime
& operator=(const struct tm
& tm
) { return Set(tm
); }
647 // assignment operator from broken down time/date
648 wxDateTime
& operator=(const Tm
& tm
) { return Set(tm
); }
650 // default assignment operator is ok
652 // calendar calculations (functions which set the date only leave the time
653 // unchanged, e.g. don't explictly zero it): SetXXX() functions modify the
654 // object itself, GetXXX() ones return a new object.
655 // ------------------------------------------------------------------------
657 // set to the given week day in the same week as this one
658 wxDateTime
& SetToWeekDayInSameWeek(WeekDay weekday
,
659 WeekFlags flags
= Monday_First
);
660 inline wxDateTime
GetWeekDayInSameWeek(WeekDay weekday
,
661 WeekFlags flags
= Monday_First
) const;
663 // set to the next week day following this one
664 wxDateTime
& SetToNextWeekDay(WeekDay weekday
);
665 inline wxDateTime
GetNextWeekDay(WeekDay weekday
) const;
667 // set to the previous week day before this one
668 wxDateTime
& SetToPrevWeekDay(WeekDay weekday
);
669 inline wxDateTime
GetPrevWeekDay(WeekDay weekday
) const;
671 // set to Nth occurence of given weekday in the given month of the
672 // given year (time is set to 0), return TRUE on success and FALSE on
673 // failure. n may be positive (1..5) or negative to count from the end
674 // of the month (see helper function SetToLastWeekDay())
675 bool SetToWeekDay(WeekDay weekday
,
677 Month month
= Inv_Month
,
678 int year
= Inv_Year
);
679 inline wxDateTime
GetWeekDay(WeekDay weekday
,
681 Month month
= Inv_Month
,
682 int year
= Inv_Year
) const;
684 // sets to the last weekday in the given month, year
685 inline bool SetToLastWeekDay(WeekDay weekday
,
686 Month month
= Inv_Month
,
687 int year
= Inv_Year
);
688 inline wxDateTime
GetLastWeekDay(WeekDay weekday
,
689 Month month
= Inv_Month
,
690 int year
= Inv_Year
);
692 // sets the date to the given day of the given week in the year,
693 // returns TRUE on success and FALSE if given date doesn't exist (e.g.
695 bool SetToTheWeek(wxDateTime_t numWeek
,
696 WeekDay weekday
= Mon
,
697 WeekFlags flags
= Monday_First
);
698 inline wxDateTime
GetWeek(wxDateTime_t numWeek
,
699 WeekDay weekday
= Mon
,
700 WeekFlags flags
= Monday_First
) const;
702 // sets the date to the last day of the given (or current) month or the
703 // given (or current) year
704 wxDateTime
& SetToLastMonthDay(Month month
= Inv_Month
,
705 int year
= Inv_Year
);
706 inline wxDateTime
GetLastMonthDay(Month month
= Inv_Month
,
707 int year
= Inv_Year
) const;
709 // sets to the given year day (1..365 or 366)
710 wxDateTime
& SetToYearDay(wxDateTime_t yday
);
711 inline wxDateTime
GetYearDay(wxDateTime_t yday
) const;
713 // The definitions below were taken verbatim from
715 // http://www.capecod.net/~pbaum/date/date0.htm
717 // (Peter Baum's home page)
719 // definition: The Julian Day Number, Julian Day, or JD of a
720 // particular instant of time is the number of days and fractions of a
721 // day since 12 hours Universal Time (Greenwich mean noon) on January
722 // 1 of the year -4712, where the year is given in the Julian
723 // proleptic calendar. The idea of using this reference date was
724 // originally proposed by Joseph Scalizer in 1582 to count years but
725 // it was modified by 19th century astronomers to count days. One
726 // could have equivalently defined the reference time to be noon of
727 // November 24, -4713 if were understood that Gregorian calendar rules
728 // were applied. Julian days are Julian Day Numbers and are not to be
729 // confused with Julian dates.
731 // definition: The Rata Die number is a date specified as the number
732 // of days relative to a base date of December 31 of the year 0. Thus
733 // January 1 of the year 1 is Rata Die day 1.
735 // get the Julian Day number (the fractional part specifies the time of
736 // the day, related to noon - beware of rounding errors!)
737 double GetJulianDayNumber() const;
738 double GetJDN() const { return GetJulianDayNumber(); }
740 // get the Modified Julian Day number: it is equal to JDN - 2400000.5
741 // and so integral MJDs correspond to the midnights (and not noons).
742 // MJD 0 is Nov 17, 1858
743 double GetModifiedJulianDayNumber() const { return GetJDN() - 2400000.5; }
744 double GetMJD() const { return GetModifiedJulianDayNumber(); }
746 // get the Rata Die number
747 double GetRataDie() const;
749 // TODO algorithms for calculating some important dates, such as
750 // religious holidays (Easter...) or moon/solar eclipses? Some
751 // algorithms can be found in the calendar FAQ
753 // timezone stuff: a wxDateTime object constructed using given
754 // day/month/year/hour/min/sec values correspond to this moment in local
755 // time. Using the functions below, it may be converted to another time
756 // zone (for example, the Unix epoch is wxDateTime(1, Jan, 1970).ToGMT())
758 // these functions try to handle DST internally, but there is no magical
759 // way to know all rules for it in all countries in the world, so if the
760 // program can handle it itself (or doesn't want to handle it at all for
761 // whatever reason), the DST handling can be disabled with noDST.
763 // Converting to the local time zone doesn't do anything.
764 // ------------------------------------------------------------------------
766 // transform to any given timezone
767 inline wxDateTime
ToTimezone(const TimeZone
& tz
, bool noDST
= FALSE
) const;
768 wxDateTime
& MakeTimezone(const TimeZone
& tz
, bool noDST
= FALSE
);
770 // transform to GMT/UTC
771 wxDateTime
ToGMT(bool noDST
= FALSE
) const { return ToTimezone(GMT0
, noDST
); }
772 wxDateTime
& MakeGMT(bool noDST
= FALSE
) { return MakeTimezone(GMT0
, noDST
); }
774 // is daylight savings time in effect at this moment according to the
775 // rules of the specified country?
777 // Return value is > 0 if DST is in effect, 0 if it is not and -1 if
778 // the information is not available (this is compatible with ANSI C)
779 int IsDST(Country country
= Country_Default
) const;
781 // accessors: many of them take the timezone parameter which indicates the
782 // timezone for which to make the calculations and the default value means
783 // to do it for the current timezone of this machine (even if the function
784 // only operates with the date it's necessary because a date may wrap as
785 // result of timezone shift)
786 // ------------------------------------------------------------------------
788 // is the date valid?
789 inline bool IsValid() const { return m_time
!= wxInvalidDateTime
.m_time
; }
791 // get the broken down date/time representation in the given timezone
793 // If you wish to get several time components (day, month and year),
794 // consider getting the whole Tm strcuture first and retrieving the
795 // value from it - this is much more efficient
796 Tm
GetTm(const TimeZone
& tz
= Local
) const;
798 // get the number of seconds since the Unix epoch - returns (time_t)-1
799 // if the value is out of range
800 inline time_t GetTicks() const;
802 // get the year (returns Inv_Year if date is invalid)
803 int GetYear(const TimeZone
& tz
= Local
) const
804 { return GetTm(tz
).year
; }
805 // get the month (Inv_Month if date is invalid)
806 Month
GetMonth(const TimeZone
& tz
= Local
) const
807 { return (Month
)GetTm(tz
).mon
; }
808 // get the month day (in 1..31 range, 0 if date is invalid)
809 wxDateTime_t
GetDay(const TimeZone
& tz
= Local
) const
810 { return GetTm(tz
).mday
; }
811 // get the day of the week (Inv_WeekDay if date is invalid)
812 WeekDay
GetWeekDay(const TimeZone
& tz
= Local
) const
813 { return GetTm(tz
).GetWeekDay(); }
814 // get the hour of the day
815 wxDateTime_t
GetHour(const TimeZone
& tz
= Local
) const
816 { return GetTm(tz
).hour
; }
818 wxDateTime_t
GetMinute(const TimeZone
& tz
= Local
) const
819 { return GetTm(tz
).min
; }
821 wxDateTime_t
GetSecond(const TimeZone
& tz
= Local
) const
822 { return GetTm(tz
).sec
; }
824 wxDateTime_t
GetMillisecond(const TimeZone
& tz
= Local
) const
825 { return GetTm(tz
).msec
; }
827 // get the day since the year start (1..366, 0 if date is invalid)
828 wxDateTime_t
GetDayOfYear(const TimeZone
& tz
= Local
) const;
829 // get the week number since the year start (1..52 or 53, 0 if date is
831 wxDateTime_t
GetWeekOfYear(WeekFlags flags
= Monday_First
,
832 const TimeZone
& tz
= Local
) const;
833 // get the week number since the month start (1..5, 0 if date is
835 wxDateTime_t
GetWeekOfMonth(WeekFlags flags
= Monday_First
,
836 const TimeZone
& tz
= Local
) const;
838 // is this date a work day? This depends on a country, of course,
839 // because the holidays are different in different countries
840 bool IsWorkDay(Country country
= Country_Default
) const;
842 // is this date later than Gregorian calendar introduction for the
843 // given country (see enum GregorianAdoption)?
845 // NB: this function shouldn't be considered as absolute authority in
846 // the matter. Besides, for some countries the exact date of
847 // adoption of the Gregorian calendar is simply unknown.
848 bool IsGregorianDate(GregorianAdoption country
= Gr_Standard
) const;
850 // dos date and time format
851 // ------------------------------------------------------------------------
853 // set from the DOS packed format
854 wxDateTime
& SetFromDOS(unsigned long ddt
);
856 // pack the date in DOS format
857 unsigned long GetAsDOS() const;
859 // comparison (see also functions below for operator versions)
860 // ------------------------------------------------------------------------
862 // returns TRUE if the two moments are strictly identical
863 inline bool IsEqualTo(const wxDateTime
& datetime
) const;
865 // returns TRUE if the date is strictly earlier than the given one
866 inline bool IsEarlierThan(const wxDateTime
& datetime
) const;
868 // returns TRUE if the date is strictly later than the given one
869 inline bool IsLaterThan(const wxDateTime
& datetime
) const;
871 // returns TRUE if the date is strictly in the given range
872 inline bool IsStrictlyBetween(const wxDateTime
& t1
,
873 const wxDateTime
& t2
) const;
875 // returns TRUE if the date is in the given range
876 inline bool IsBetween(const wxDateTime
& t1
, const wxDateTime
& t2
) const;
878 // do these two objects refer to the same date?
879 inline bool IsSameDate(const wxDateTime
& dt
) const;
881 // do these two objects have the same time?
882 inline bool IsSameTime(const wxDateTime
& dt
) const;
884 // are these two objects equal up to given timespan?
885 inline bool IsEqualUpTo(const wxDateTime
& dt
, const wxTimeSpan
& ts
) const;
887 // arithmetics with dates (see also below for more operators)
888 // ------------------------------------------------------------------------
890 // return the sum of the date with a time span (positive or negative)
891 inline wxDateTime
Add(const wxTimeSpan
& diff
) const;
892 // add a time span (positive or negative)
893 inline wxDateTime
& Add(const wxTimeSpan
& diff
);
894 // add a time span (positive or negative)
895 inline wxDateTime
& operator+=(const wxTimeSpan
& diff
);
897 // return the difference of the date with a time span
898 inline wxDateTime
Subtract(const wxTimeSpan
& diff
) const;
899 // subtract a time span (positive or negative)
900 inline wxDateTime
& Subtract(const wxTimeSpan
& diff
);
901 // subtract a time span (positive or negative)
902 inline wxDateTime
& operator-=(const wxTimeSpan
& diff
);
904 // return the sum of the date with a date span
905 inline wxDateTime
Add(const wxDateSpan
& diff
) const;
906 // add a date span (positive or negative)
907 wxDateTime
& Add(const wxDateSpan
& diff
);
908 // add a date span (positive or negative)
909 inline wxDateTime
& operator+=(const wxDateSpan
& diff
);
911 // return the difference of the date with a date span
912 inline wxDateTime
Subtract(const wxDateSpan
& diff
) const;
913 // subtract a date span (positive or negative)
914 inline wxDateTime
& Subtract(const wxDateSpan
& diff
);
915 // subtract a date span (positive or negative)
916 inline wxDateTime
& operator-=(const wxDateSpan
& diff
);
918 // return the difference between two dates
919 inline wxTimeSpan
Subtract(const wxDateTime
& dt
) const;
921 // conversion to/from text: all conversions from text return the pointer to
922 // the next character following the date specification (i.e. the one where
923 // the scan had to stop) or NULL on failure.
924 // ------------------------------------------------------------------------
926 // parse a string in RFC 822 format (found e.g. in mail headers and
927 // having the form "Wed, 10 Feb 1999 19:07:07 +0100")
928 const wxChar
*ParseRfc822Date(const wxChar
* date
);
929 // parse a date/time in the given format (see strptime(3)), fill in
930 // the missing (in the string) fields with the values of dateDef (by
931 // default, they will not change if they had valid values or will
932 // default to Today() otherwise)
933 const wxChar
*ParseFormat(const wxChar
*date
,
934 const wxChar
*format
= _T("%c"),
935 const wxDateTime
& dateDef
= wxDefaultDateTime
);
936 // parse a string containing the date/time in "free" format, this
937 // function will try to make an educated guess at the string contents
938 const wxChar
*ParseDateTime(const wxChar
*datetime
);
939 // parse a string containing the date only in "free" format (less
940 // flexible than ParseDateTime)
941 const wxChar
*ParseDate(const wxChar
*date
);
942 // parse a string containing the time only in "free" format
943 const wxChar
*ParseTime(const wxChar
*time
);
945 // this function accepts strftime()-like format string (default
946 // argument corresponds to the preferred date and time representation
947 // for the current locale) and returns the string containing the
948 // resulting text representation
949 wxString
Format(const wxChar
*format
= _T("%c"),
950 const TimeZone
& tz
= Local
) const;
951 // preferred date representation for the current locale
952 wxString
FormatDate() const { return Format(_T("%x")); }
953 // preferred time representation for the current locale
954 wxString
FormatTime() const { return Format(_T("%X")); }
955 // returns the string representing the date in ISO 8601 format
957 wxString
FormatISODate() const { return Format(_T("%Y-%m-%d")); }
958 // returns the string representing the time in ISO 8601 format
960 wxString
FormatISOTime() const { return Format(_T("%H:%M:%S")); }
963 // ------------------------------------------------------------------------
965 // construct from internal representation
966 wxDateTime(const wxLongLong
& time
) { m_time
= time
; }
968 // get the internal representation
969 inline wxLongLong
GetValue() const;
971 // a helper function to get the current time_t
972 static time_t GetTimeNow() { return time((time_t *)NULL
); }
974 // another one to get the current time broken down
975 static struct tm
*GetTmNow()
977 time_t t
= GetTimeNow();
978 return localtime(&t
);
982 // the current country - as it's the same for all program objects (unless
983 // it runs on a _really_ big cluster system :-), this is a static member:
984 // see SetCountry() and GetCountry()
985 static Country ms_country
;
987 // this constant is used to transform a time_t value to the internal
988 // representation, as time_t is in seconds and we use milliseconds it's
990 static const long TIME_T_FACTOR
;
992 // returns TRUE if we fall in range in which we can use standard ANSI C
994 inline bool IsInStdRange() const;
996 // the internal representation of the time is the amount of milliseconds
997 // elapsed since the origin which is set by convention to the UNIX/C epoch
998 // value: the midnight of January 1, 1970 (UTC)
1002 // ----------------------------------------------------------------------------
1003 // This class contains a difference between 2 wxDateTime values, so it makes
1004 // sense to add it to wxDateTime and it is the result of subtraction of 2
1005 // objects of that class. See also wxDateSpan.
1006 // ----------------------------------------------------------------------------
1008 class WXDLLEXPORT wxTimeSpan
1012 // ------------------------------------------------------------------------
1014 // return the timespan for the given number of seconds
1015 static wxTimeSpan
Seconds(long sec
) { return wxTimeSpan(0, 0, sec
); }
1016 static wxTimeSpan
Second() { return Seconds(1); }
1018 // return the timespan for the given number of minutes
1019 static wxTimeSpan
Minutes(long min
) { return wxTimeSpan(0, min
, 0 ); }
1020 static wxTimeSpan
Minute() { return Minutes(1); }
1022 // return the timespan for the given number of hours
1023 static wxTimeSpan
Hours(long hours
) { return wxTimeSpan(hours
, 0, 0); }
1024 static wxTimeSpan
Hour() { return Hours(1); }
1026 // return the timespan for the given number of days
1027 static wxTimeSpan
Days(long days
) { return Hours(24 * days
); }
1028 static wxTimeSpan
Day() { return Days(1); }
1030 // return the timespan for the given number of weeks
1031 static wxTimeSpan
Weeks(long days
) { return Days(7 * days
); }
1032 static wxTimeSpan
Week() { return Weeks(1); }
1034 // default ctor constructs the 0 time span
1037 // from separate values for each component, date set to 0 (hours are
1038 // not restricted to 0..24 range, neither are minutes, seconds or
1040 inline wxTimeSpan(long hours
,
1043 long milliseconds
= 0);
1045 // default copy ctor is ok
1049 // arithmetics with time spans (see also below for more operators)
1050 // ------------------------------------------------------------------------
1052 // return the sum of two timespans
1053 inline wxTimeSpan
Add(const wxTimeSpan
& diff
) const;
1054 // add two timespans together
1055 inline wxTimeSpan
& Add(const wxTimeSpan
& diff
);
1056 // add two timespans together
1057 wxTimeSpan
& operator+=(const wxTimeSpan
& diff
) { return Add(diff
); }
1059 // return the difference of two timespans
1060 inline wxTimeSpan
Subtract(const wxTimeSpan
& diff
) const;
1061 // subtract another timespan
1062 inline wxTimeSpan
& Subtract(const wxTimeSpan
& diff
);
1063 // subtract another timespan
1064 wxTimeSpan
& operator-=(const wxTimeSpan
& diff
) { return Subtract(diff
); }
1066 // multiply timespan by a scalar
1067 inline wxTimeSpan
Multiply(int n
) const;
1068 // multiply timespan by a scalar
1069 inline wxTimeSpan
& Multiply(int n
);
1070 // multiply timespan by a scalar
1071 wxTimeSpan
& operator*=(int n
) { return Multiply(n
); }
1073 // return this timespan with inversed sign
1074 wxTimeSpan
Negate() const { return wxTimeSpan(-GetValue()); }
1075 // negate the value of the timespan
1076 wxTimeSpan
& Neg() { m_diff
= -GetValue(); return *this; }
1077 // negate the value of the timespan
1078 wxTimeSpan
& operator-() { return Neg(); }
1080 // return the absolute value of the timespan: does _not_ modify the
1082 inline wxTimeSpan
Abs() const;
1084 // there is intentionally no division because we don't want to
1085 // introduce rounding errors in time calculations
1087 // comparaison (see also operator versions below)
1088 // ------------------------------------------------------------------------
1090 // is the timespan null?
1091 bool IsNull() const { return m_diff
== 0l; }
1092 // returns true if the timespan is null
1093 bool operator!() const { return !IsNull(); }
1095 // is the timespan positive?
1096 bool IsPositive() const { return m_diff
> 0l; }
1098 // is the timespan negative?
1099 bool IsNegative() const { return m_diff
< 0l; }
1101 // are two timespans equal?
1102 inline bool IsEqualTo(const wxTimeSpan
& ts
) const;
1103 // compare two timestamps: works with the absolute values, i.e. -2
1104 // hours is longer than 1 hour. Also, it will return FALSE if the
1105 // timespans are equal in absolute value.
1106 inline bool IsLongerThan(const wxTimeSpan
& ts
) const;
1107 // compare two timestamps: works with the absolute values, i.e. 1
1108 // hour is shorter than -2 hours. Also, it will return FALSE if the
1109 // timespans are equal in absolute value.
1110 bool IsShorterThan(const wxTimeSpan
& t
) const { return !IsLongerThan(t
); }
1112 // breaking into days, hours, minutes and seconds
1113 // ------------------------------------------------------------------------
1115 // get the max number of weeks in this timespan
1116 inline int GetWeeks() const;
1117 // get the max number of days in this timespan
1118 inline int GetDays() const;
1119 // get the max number of hours in this timespan
1120 inline int GetHours() const;
1121 // get the max number of minutes in this timespan
1122 inline int GetMinutes() const;
1123 // get the max number of seconds in this timespan
1124 inline wxLongLong
GetSeconds() const;
1125 // get the number of milliseconds in this timespan
1126 wxLongLong
GetMilliseconds() const { return m_diff
; }
1128 // conversion to text
1129 // ------------------------------------------------------------------------
1131 // this function accepts strftime()-like format string (default
1132 // argument corresponds to the preferred date and time representation
1133 // for the current locale) and returns the string containing the
1134 // resulting text representation. Notice that only some of format
1135 // specifiers valid for wxDateTime are valid for wxTimeSpan: hours,
1136 // minutes and seconds make sense, but not "PM/AM" string for example.
1137 wxString
Format(const wxChar
*format
= _T("%H:%M:%S")) const;
1140 // ------------------------------------------------------------------------
1142 // construct from internal representation
1143 wxTimeSpan(const wxLongLong
& diff
) { m_diff
= diff
; }
1145 // get the internal representation
1146 wxLongLong
GetValue() const { return m_diff
; }
1149 // the (signed) time span in milliseconds
1153 // ----------------------------------------------------------------------------
1154 // This class is a "logical time span" and is useful for implementing program
1155 // logic for such things as "add one month to the date" which, in general,
1156 // doesn't mean to add 60*60*24*31 seconds to it, but to take the same date
1157 // the next month (to understand that this is indeed different consider adding
1158 // one month to Feb, 15 - we want to get Mar, 15, of course).
1160 // When adding a month to the date, all lesser components (days, hours, ...)
1161 // won't be changed unless the resulting date would be invalid: for example,
1162 // Jan 31 + 1 month will be Feb 28, not (non existing) Feb 31.
1164 // Because of this feature, adding and subtracting back again the same
1165 // wxDateSpan will *not*, in general give back the original date: Feb 28 - 1
1166 // month will be Jan 28, not Jan 31!
1168 // wxDateSpan can be either positive or negative. They may be
1169 // multiplied by scalars which multiply all deltas by the scalar: i.e. 2*(1
1170 // month and 1 day) is 2 months and 2 days. They can be added together and
1171 // with wxDateTime or wxTimeSpan, but the type of result is different for each
1174 // Beware about weeks: if you specify both weeks and days, the total number of
1175 // days added will be 7*weeks + days! See also GetTotalDays() function.
1177 // Equality operators are defined for wxDateSpans. Two datespans are equal if
1178 // they both give the same target date when added to *every* source date.
1179 // Thus wxDateSpan::Months(1) is not equal to wxDateSpan::Days(30), because
1180 // they not give the same date when added to 1 Feb. But wxDateSpan::Days(14) is
1181 // equal to wxDateSpan::Weeks(2)
1183 // Finally, notice that for adding hours, minutes &c you don't need this
1184 // class: wxTimeSpan will do the job because there are no subtleties
1185 // associated with those.
1186 // ----------------------------------------------------------------------------
1188 class WXDLLEXPORT wxDateSpan
1192 // ------------------------------------------------------------------------
1194 // this many years/months/weeks/days
1195 wxDateSpan(int years
= 0, int months
= 0, int weeks
= 0, int days
= 0)
1203 // get an object for the given number of days
1204 static wxDateSpan
Days(int days
) { return wxDateSpan(0, 0, 0, days
); }
1205 static wxDateSpan
Day() { return Days(1); }
1207 // get an object for the given number of weeks
1208 static wxDateSpan
Weeks(int weeks
) { return wxDateSpan(0, 0, weeks
, 0); }
1209 static wxDateSpan
Week() { return Weeks(1); }
1211 // get an object for the given number of months
1212 static wxDateSpan
Months(int mon
) { return wxDateSpan(0, mon
, 0, 0); }
1213 static wxDateSpan
Month() { return Months(1); }
1215 // get an object for the given number of years
1216 static wxDateSpan
Years(int years
) { return wxDateSpan(years
, 0, 0, 0); }
1217 static wxDateSpan
Year() { return Years(1); }
1219 // default copy ctor is ok
1223 // accessors (all SetXXX() return the (modified) wxDateSpan object)
1224 // ------------------------------------------------------------------------
1226 // set number of years
1227 wxDateSpan
& SetYears(int n
) { m_years
= n
; return *this; }
1228 // set number of months
1229 wxDateSpan
& SetMonths(int n
) { m_months
= n
; return *this; }
1230 // set number of weeks
1231 wxDateSpan
& SetWeeks(int n
) { m_weeks
= n
; return *this; }
1232 // set number of days
1233 wxDateSpan
& SetDays(int n
) { m_days
= n
; return *this; }
1235 // get number of years
1236 int GetYears() const { return m_years
; }
1237 // get number of months
1238 int GetMonths() const { return m_months
; }
1239 // get number of weeks
1240 int GetWeeks() const { return m_weeks
; }
1241 // get number of days
1242 int GetDays() const { return m_days
; }
1243 // returns 7*GetWeeks() + GetDays()
1244 int GetTotalDays() const { return 7*m_weeks
+ m_days
; }
1246 // arithmetics with date spans (see also below for more operators)
1247 // ------------------------------------------------------------------------
1249 // return sum of two date spans
1250 inline wxDateSpan
Add(const wxDateSpan
& other
) const;
1251 // add another wxDateSpan to us
1252 inline wxDateSpan
& Add(const wxDateSpan
& other
);
1253 // add another wxDateSpan to us
1254 inline wxDateSpan
& operator+=(const wxDateSpan
& other
);
1256 // return difference of two date spans
1257 inline wxDateSpan
Subtract(const wxDateSpan
& other
) const;
1258 // subtract another wxDateSpan from us
1259 inline wxDateSpan
& Subtract(const wxDateSpan
& other
);
1260 // subtract another wxDateSpan from us
1261 inline wxDateSpan
& operator-=(const wxDateSpan
& other
);
1263 // return a copy of this time span with changed sign
1264 inline wxDateSpan
Negate() const;
1265 // inverse the sign of this timespan
1266 inline wxDateSpan
& Neg();
1267 // inverse the sign of this timespan
1268 wxDateSpan
& operator-() { return Neg(); }
1270 // return the date span proportional to this one with given factor
1271 inline wxDateSpan
Multiply(int factor
) const;
1272 // multiply all components by a (signed) number
1273 inline wxDateSpan
& Multiply(int factor
);
1274 // multiply all components by a (signed) number
1275 inline wxDateSpan
& operator*=(int factor
) { return Multiply(factor
); }
1284 // ----------------------------------------------------------------------------
1285 // wxDateTimeArray: array of dates.
1286 // ----------------------------------------------------------------------------
1288 #include "wx/dynarray.h"
1290 WX_DECLARE_EXPORTED_OBJARRAY(wxDateTime
, wxDateTimeArray
);
1292 // ----------------------------------------------------------------------------
1293 // wxDateTimeHolidayAuthority: an object of this class will decide whether a
1294 // given date is a holiday and is used by all functions working with "work
1297 // NB: the base class is an ABC, derived classes must implement the pure
1298 // virtual methods to work with the holidays they correspond to.
1299 // ----------------------------------------------------------------------------
1301 class WXDLLEXPORT wxDateTimeHolidayAuthority
;
1302 WX_DEFINE_EXPORTED_ARRAY(wxDateTimeHolidayAuthority
*, wxHolidayAuthoritiesArray
);
1304 class wxDateTimeHolidaysModule
;
1305 class WXDLLEXPORT wxDateTimeHolidayAuthority
1307 friend class wxDateTimeHolidaysModule
;
1309 // returns TRUE if the given date is a holiday
1310 static bool IsHoliday(const wxDateTime
& dt
);
1312 // fills the provided array with all holidays in the given range, returns
1313 // the number of them
1314 static size_t GetHolidaysInRange(const wxDateTime
& dtStart
,
1315 const wxDateTime
& dtEnd
,
1316 wxDateTimeArray
& holidays
);
1318 // clear the list of holiday authorities
1319 static void ClearAllAuthorities();
1321 // add a new holiday authority (the pointer will be deleted by
1322 // wxDateTimeHolidayAuthority)
1323 static void AddAuthority(wxDateTimeHolidayAuthority
*auth
);
1325 // the base class must have a virtual dtor
1326 virtual ~wxDateTimeHolidayAuthority();
1329 // this function is called to determine whether a given day is a holiday
1330 virtual bool DoIsHoliday(const wxDateTime
& dt
) const = 0;
1332 // this function should fill the array with all holidays between the two
1333 // given dates - it is implemented in the base class, but in a very
1334 // inefficient way (it just iterates over all days and uses IsHoliday() for
1335 // each of them), so it must be overridden in the derived class where the
1336 // base class version may be explicitly used if needed
1338 // returns the number of holidays in the given range and fills holidays
1340 virtual size_t DoGetHolidaysInRange(const wxDateTime
& dtStart
,
1341 const wxDateTime
& dtEnd
,
1342 wxDateTimeArray
& holidays
) const = 0;
1345 // all holiday authorities
1346 static wxHolidayAuthoritiesArray ms_authorities
;
1349 // the holidays for this class are all Saturdays and Sundays
1350 class WXDLLEXPORT wxDateTimeWorkDays
: public wxDateTimeHolidayAuthority
1353 virtual bool DoIsHoliday(const wxDateTime
& dt
) const;
1354 virtual size_t DoGetHolidaysInRange(const wxDateTime
& dtStart
,
1355 const wxDateTime
& dtEnd
,
1356 wxDateTimeArray
& holidays
) const;
1359 // ============================================================================
1360 // inline functions implementation
1361 // ============================================================================
1363 // don't include inline functions definitions when we're included from anything
1364 // else than datetime.cpp in debug builds: this minimizes rebuilds if we change
1365 // some inline function and the performance doesn't matter in the debug builds.
1367 #if !defined(wxDATETIME_DONT_INLINE) || defined(wxDEFINE_TIME_CONSTANTS)
1368 #define INCLUDED_FROM_WX_DATETIME_H
1369 #include "wx/datetime.inl"
1370 #undef INCLUDED_FROM_WX_DATETIME_H
1373 // if we defined it to be empty above, restore it now
1374 #ifdef wxDATETIME_DONT_INLINE
1378 // ============================================================================
1380 // ============================================================================
1382 // ----------------------------------------------------------------------------
1383 // wxDateTime operators
1384 // ----------------------------------------------------------------------------
1389 // no need to check for validity - the member functions we call will do it
1391 inline wxDateTime WXDLLEXPORT
operator+(const wxDateTime
& dt
,
1392 const wxTimeSpan
& ts
)
1397 inline wxDateTime WXDLLEXPORT
operator-(const wxDateTime
& dt
,
1398 const wxTimeSpan
& ts
)
1400 return dt
.Subtract(ts
);
1403 inline wxDateTime WXDLLEXPORT
operator+(const wxDateTime
& dt
,
1404 const wxDateSpan
& ds
)
1409 inline wxDateTime WXDLLEXPORT
operator-(const wxDateTime
& dt
,
1410 const wxDateSpan
& ds
)
1412 return dt
.Subtract(ds
);
1415 inline wxTimeSpan WXDLLEXPORT
operator-(const wxDateTime
& dt1
,
1416 const wxDateTime
& dt2
)
1418 return dt1
.Subtract(dt2
);
1424 inline bool WXDLLEXPORT
operator<(const wxDateTime
& t1
, const wxDateTime
& t2
)
1426 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), _T("invalid wxDateTime") );
1428 return t1
.GetValue() < t2
.GetValue();
1431 inline bool WXDLLEXPORT
operator<=(const wxDateTime
& t1
, const wxDateTime
& t2
)
1433 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), _T("invalid wxDateTime") );
1435 return t1
.GetValue() <= t2
.GetValue();
1438 inline bool WXDLLEXPORT
operator>(const wxDateTime
& t1
, const wxDateTime
& t2
)
1440 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), _T("invalid wxDateTime") );
1442 return t1
.GetValue() > t2
.GetValue();
1445 inline bool WXDLLEXPORT
operator>=(const wxDateTime
& t1
, const wxDateTime
& t2
)
1447 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), _T("invalid wxDateTime") );
1449 return t1
.GetValue() >= t2
.GetValue();
1452 inline bool WXDLLEXPORT
operator==(const wxDateTime
& t1
, const wxDateTime
& t2
)
1454 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), _T("invalid wxDateTime") );
1456 return t1
.GetValue() == t2
.GetValue();
1459 inline bool WXDLLEXPORT
operator!=(const wxDateTime
& t1
, const wxDateTime
& t2
)
1461 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), _T("invalid wxDateTime") );
1463 return t1
.GetValue() != t2
.GetValue();
1466 // ----------------------------------------------------------------------------
1467 // wxTimeSpan operators
1468 // ----------------------------------------------------------------------------
1473 inline wxTimeSpan WXDLLEXPORT
operator+(const wxTimeSpan
& ts1
,
1474 const wxTimeSpan
& ts2
)
1476 return wxTimeSpan(ts1
.GetValue() + ts2
.GetValue());
1479 inline wxTimeSpan WXDLLEXPORT
operator-(const wxTimeSpan
& ts1
,
1480 const wxTimeSpan
& ts2
)
1482 return wxTimeSpan(ts1
.GetValue() - ts2
.GetValue());
1485 inline wxTimeSpan WXDLLEXPORT
operator*(const wxTimeSpan
& ts
, int n
)
1487 return wxTimeSpan(ts
).Multiply(n
);
1490 inline wxTimeSpan WXDLLEXPORT
operator*(int n
, const wxTimeSpan
& ts
)
1492 return wxTimeSpan(ts
).Multiply(n
);
1498 inline bool WXDLLEXPORT
operator<(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1500 return t1
.GetValue() < t2
.GetValue();
1503 inline bool WXDLLEXPORT
operator<=(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1505 return t1
.GetValue() <= t2
.GetValue();
1508 inline bool WXDLLEXPORT
operator>(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1510 return t1
.GetValue() > t2
.GetValue();
1513 inline bool WXDLLEXPORT
operator>=(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1515 return t1
.GetValue() >= t2
.GetValue();
1518 inline bool WXDLLEXPORT
operator==(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1520 return t1
.GetValue() == t2
.GetValue();
1523 inline bool WXDLLEXPORT
operator!=(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1525 return t1
.GetValue() != t2
.GetValue();
1528 // ----------------------------------------------------------------------------
1530 // ----------------------------------------------------------------------------
1535 // ds1 == d2 if and only if for every wxDateTime t t + ds1 == t + ds2
1536 inline WXDLLEXPORT
bool operator==(const wxDateSpan
& ds1
,
1537 const wxDateSpan
& ds2
)
1539 return ds1
.GetYears() == ds2
.GetYears() &&
1540 ds1
.GetMonths() == ds2
.GetMonths() &&
1541 ds1
.GetTotalDays() == ds2
.GetTotalDays();
1544 inline WXDLLEXPORT
bool operator!=(const wxDateSpan
& ds1
,
1545 const wxDateSpan
& ds2
)
1547 return !(ds1
== ds2
);
1553 inline WXDLLEXPORT wxDateSpan
operator+(const wxDateSpan
& ds1
,
1554 const wxDateSpan
& ds2
)
1556 return wxDateSpan(ds1
.GetYears() + ds2
.GetYears(),
1557 ds1
.GetMonths() + ds2
.GetMonths(),
1558 ds1
.GetWeeks() + ds2
.GetWeeks(),
1559 ds1
.GetDays() + ds2
.GetDays());
1562 inline WXDLLEXPORT wxDateSpan
operator-(const wxDateSpan
& ds1
,
1563 const wxDateSpan
& ds2
)
1565 return wxDateSpan(ds1
.GetYears() - ds2
.GetYears(),
1566 ds1
.GetMonths() - ds2
.GetMonths(),
1567 ds1
.GetWeeks() - ds2
.GetWeeks(),
1568 ds1
.GetDays() - ds2
.GetDays());
1571 inline WXDLLEXPORT wxDateSpan
operator*(const wxDateSpan
& ds
, int n
)
1573 return wxDateSpan(ds
).Multiply(n
);
1576 inline WXDLLEXPORT wxDateSpan
operator*(int n
, const wxDateSpan
& ds
)
1578 return wxDateSpan(ds
).Multiply(n
);
1581 // ============================================================================
1582 // other helper functions
1583 // ============================================================================
1585 // ----------------------------------------------------------------------------
1586 // iteration helpers: can be used to write a for loop over enum variable like
1588 // for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) )
1589 // ----------------------------------------------------------------------------
1591 inline WXDLLEXPORT
void wxNextMonth(wxDateTime::Month
& m
)
1593 wxASSERT_MSG( m
< wxDateTime::Inv_Month
, _T("invalid month") );
1595 // no wrapping or the for loop above would never end!
1596 m
= (wxDateTime::Month
)(m
+ 1);
1599 inline WXDLLEXPORT
void wxPrevMonth(wxDateTime::Month
& m
)
1601 wxASSERT_MSG( m
< wxDateTime::Inv_Month
, _T("invalid month") );
1603 m
= m
== wxDateTime::Jan
? wxDateTime::Inv_Month
1604 : (wxDateTime::Month
)(m
- 1);
1607 inline WXDLLEXPORT
void wxNextWDay(wxDateTime::WeekDay
& wd
)
1609 wxASSERT_MSG( wd
< wxDateTime::Inv_WeekDay
, _T("invalid week day") );
1611 // no wrapping or the for loop above would never end!
1612 wd
= (wxDateTime::WeekDay
)(wd
+ 1);
1615 inline WXDLLEXPORT
void wxPrevWDay(wxDateTime::WeekDay
& wd
)
1617 wxASSERT_MSG( wd
< wxDateTime::Inv_WeekDay
, _T("invalid week day") );
1619 wd
= wd
== wxDateTime::Sun
? wxDateTime::Inv_WeekDay
1620 : (wxDateTime::WeekDay
)(wd
- 1);
1623 #endif // wxUSE_DATETIME
1625 #endif // _WX_DATETIME_H