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
23 #include "wx/msw/wince/time.h"
26 #include <limits.h> // for INT_MIN
28 #include "wx/longlong.h"
30 class WXDLLIMPEXP_BASE wxDateTime
;
31 class WXDLLIMPEXP_BASE wxTimeSpan
;
32 class WXDLLIMPEXP_BASE wxDateSpan
;
34 #include "wx/dynarray.h"
36 // not all c-runtimes are based on 1/1/1970 being (time_t) 0
37 // set this to the corresponding value in seconds 1/1/1970 has on your
40 #if defined(__WXMAC__) && !defined(__DARWIN__) && __MSL__ < 0x6000
41 #define WX_TIME_BASE_OFFSET ( 2082844800L + 126144000L )
43 #define WX_TIME_BASE_OFFSET 0
48 * + 1. Time zones with minutes (make TimeZone a class)
49 * ? 2. getdate() function like under Solaris
50 * + 3. text conversion for wxDateSpan
51 * + 4. pluggable modules for the workdays calculations
52 * 5. wxDateTimeHolidayAuthority for Easter and other christian feasts
56 The three (main) classes declared in this header represent:
58 1. An absolute moment in the time (wxDateTime)
59 2. A difference between two moments in the time, positive or negative
61 3. A logical difference between two dates expressed in
62 years/months/weeks/days (wxDateSpan)
64 The following arithmetic operations are permitted (all others are not):
69 wxDateTime + wxTimeSpan = wxDateTime
70 wxDateTime + wxDateSpan = wxDateTime
71 wxTimeSpan + wxTimeSpan = wxTimeSpan
72 wxDateSpan + wxDateSpan = wxDateSpan
76 wxDateTime - wxDateTime = wxTimeSpan
77 wxDateTime - wxTimeSpan = wxDateTime
78 wxDateTime - wxDateSpan = wxDateTime
79 wxTimeSpan - wxTimeSpan = wxTimeSpan
80 wxDateSpan - wxDateSpan = wxDateSpan
84 wxTimeSpan * number = wxTimeSpan
85 number * wxTimeSpan = wxTimeSpan
86 wxDateSpan * number = wxDateSpan
87 number * wxDateSpan = wxDateSpan
91 -wxTimeSpan = wxTimeSpan
92 -wxDateSpan = wxDateSpan
94 For each binary operation OP (+, -, *) we have the following operatorOP=() as
95 a method and the method with a symbolic name OPER (Add, Subtract, Multiply)
96 as a synonym for it and another const method with the same name which returns
97 the changed copy of the object and operatorOP() as a global function which is
98 implemented in terms of the const version of OPEN. For the unary - we have
99 operator-() as a method, Neg() as synonym for it and Negate() which returns
100 the copy of the object with the changed sign.
103 // an invalid/default date time object which may be used as the default
104 // argument for arguments of type wxDateTime; it is also returned by all
105 // functions returning wxDateTime on failure (this is why it is also called
106 // wxInvalidDateTime)
107 class WXDLLIMPEXP_BASE wxDateTime
;
109 extern WXDLLIMPEXP_DATA_BASE(const wxChar
*) wxDefaultDateTimeFormat
;
110 extern WXDLLIMPEXP_DATA_BASE(const wxChar
*) wxDefaultTimeSpanFormat
;
111 extern WXDLLIMPEXP_DATA_BASE(const wxDateTime
) wxDefaultDateTime
;
113 #define wxInvalidDateTime wxDefaultDateTime
115 // ----------------------------------------------------------------------------
116 // wxDateTime represents an absolute moment in the time
117 // ----------------------------------------------------------------------------
119 class WXDLLIMPEXP_BASE wxDateTime
123 // ------------------------------------------------------------------------
125 // a small unsigned integer type for storing things like minutes,
126 // seconds &c. It should be at least short (i.e. not char) to contain
127 // the number of milliseconds - it may also be 'int' because there is
128 // no size penalty associated with it in our code, we don't store any
129 // data in this format
130 typedef unsigned short wxDateTime_t
;
133 // ------------------------------------------------------------------------
138 // the time in the current time zone
141 // zones from GMT (= Greenwhich Mean Time): they're guaranteed to be
142 // consequent numbers, so writing something like `GMT0 + offset' is
143 // safe if abs(offset) <= 12
145 // underscore stands for minus
146 GMT_12
, GMT_11
, GMT_10
, GMT_9
, GMT_8
, GMT_7
,
147 GMT_6
, GMT_5
, GMT_4
, GMT_3
, GMT_2
, GMT_1
,
149 GMT1
, GMT2
, GMT3
, GMT4
, GMT5
, GMT6
,
150 GMT7
, GMT8
, GMT9
, GMT10
, GMT11
, GMT12
,
151 // Note that GMT12 and GMT_12 are not the same: there is a difference
152 // of exactly one day between them
154 // some symbolic names for TZ
157 WET
= GMT0
, // Western Europe Time
158 WEST
= GMT1
, // Western Europe Summer Time
159 CET
= GMT1
, // Central Europe Time
160 CEST
= GMT2
, // Central Europe Summer Time
161 EET
= GMT2
, // Eastern Europe Time
162 EEST
= GMT3
, // Eastern Europe Summer Time
163 MSK
= GMT3
, // Moscow Time
164 MSD
= GMT4
, // Moscow Summer Time
167 AST
= GMT_4
, // Atlantic Standard Time
168 ADT
= GMT_3
, // Atlantic Daylight Time
169 EST
= GMT_5
, // Eastern Standard Time
170 EDT
= GMT_4
, // Eastern Daylight Saving Time
171 CST
= GMT_6
, // Central Standard Time
172 CDT
= GMT_5
, // Central Daylight Saving Time
173 MST
= GMT_7
, // Mountain Standard Time
174 MDT
= GMT_6
, // Mountain Daylight Saving Time
175 PST
= GMT_8
, // Pacific Standard Time
176 PDT
= GMT_7
, // Pacific Daylight Saving Time
177 HST
= GMT_10
, // Hawaiian Standard Time
178 AKST
= GMT_9
, // Alaska Standard Time
179 AKDT
= GMT_8
, // Alaska Daylight Saving Time
183 A_WST
= GMT8
, // Western Standard Time
184 A_CST
= GMT12
+ 1, // Central Standard Time (+9.5)
185 A_EST
= GMT10
, // Eastern Standard Time
186 A_ESST
= GMT11
, // Eastern Summer Time
188 // TODO add more symbolic timezone names here
190 // Universal Coordinated Time = the new and politically correct name
195 // the calendar systems we know about: notice that it's valid (for
196 // this classes purpose anyhow) to work with any of these calendars
197 // even with the dates before the historical appearance of the
201 Gregorian
, // current calendar
202 Julian
// calendar in use since -45 until the 1582 (or later)
204 // TODO Hebrew, Chinese, Maya, ... (just kidding) (or then may be not?)
207 // these values only are used to identify the different dates of
208 // adoption of the Gregorian calendar (see IsGregorian())
210 // All data and comments taken verbatim from "The Calendar FAQ (v 2.0)"
211 // by Claus Tøndering, http://www.pip.dknet.dk/~c-t/calendar.html
212 // except for the comments "we take".
214 // Symbol "->" should be read as "was followed by" in the comments
216 enum GregorianAdoption
218 Gr_Unknown
, // no data for this country or it's too uncertain to use
219 Gr_Standard
, // on the day 0 of Gregorian calendar: 15 Oct 1582
221 Gr_Alaska
, // Oct 1867 when Alaska became part of the USA
222 Gr_Albania
, // Dec 1912
224 Gr_Austria
= Gr_Unknown
, // Different regions on different dates
225 Gr_Austria_Brixen
, // 5 Oct 1583 -> 16 Oct 1583
226 Gr_Austria_Salzburg
= Gr_Austria_Brixen
,
227 Gr_Austria_Tyrol
= Gr_Austria_Brixen
,
228 Gr_Austria_Carinthia
, // 14 Dec 1583 -> 25 Dec 1583
229 Gr_Austria_Styria
= Gr_Austria_Carinthia
,
231 Gr_Belgium
, // Then part of the Netherlands
233 Gr_Bulgaria
= Gr_Unknown
, // Unknown precisely (from 1915 to 1920)
234 Gr_Bulgaria_1
, // 18 Mar 1916 -> 1 Apr 1916
235 Gr_Bulgaria_2
, // 31 Mar 1916 -> 14 Apr 1916
236 Gr_Bulgaria_3
, // 3 Sep 1920 -> 17 Sep 1920
238 Gr_Canada
= Gr_Unknown
, // Different regions followed the changes in
239 // Great Britain or France
241 Gr_China
= Gr_Unknown
, // Different authorities say:
242 Gr_China_1
, // 18 Dec 1911 -> 1 Jan 1912
243 Gr_China_2
, // 18 Dec 1928 -> 1 Jan 1929
245 Gr_Czechoslovakia
, // (Bohemia and Moravia) 6 Jan 1584 -> 17 Jan 1584
246 Gr_Denmark
, // (including Norway) 18 Feb 1700 -> 1 Mar 1700
249 Gr_Finland
, // Then part of Sweden
251 Gr_France
, // 9 Dec 1582 -> 20 Dec 1582
252 Gr_France_Alsace
, // 4 Feb 1682 -> 16 Feb 1682
253 Gr_France_Lorraine
, // 16 Feb 1760 -> 28 Feb 1760
254 Gr_France_Strasbourg
, // February 1682
256 Gr_Germany
= Gr_Unknown
, // Different states on different dates:
257 Gr_Germany_Catholic
, // 1583-1585 (we take 1584)
258 Gr_Germany_Prussia
, // 22 Aug 1610 -> 2 Sep 1610
259 Gr_Germany_Protestant
, // 18 Feb 1700 -> 1 Mar 1700
261 Gr_GreatBritain
, // 2 Sep 1752 -> 14 Sep 1752 (use 'cal(1)')
263 Gr_Greece
, // 9 Mar 1924 -> 23 Mar 1924
264 Gr_Hungary
, // 21 Oct 1587 -> 1 Nov 1587
265 Gr_Ireland
= Gr_GreatBritain
,
266 Gr_Italy
= Gr_Standard
,
268 Gr_Japan
= Gr_Unknown
, // Different authorities say:
269 Gr_Japan_1
, // 19 Dec 1872 -> 1 Jan 1873
270 Gr_Japan_2
, // 19 Dec 1892 -> 1 Jan 1893
271 Gr_Japan_3
, // 18 Dec 1918 -> 1 Jan 1919
273 Gr_Latvia
, // 1915-1918 (we take 1915)
274 Gr_Lithuania
, // 1915
275 Gr_Luxemburg
, // 14 Dec 1582 -> 25 Dec 1582
276 Gr_Netherlands
= Gr_Belgium
, // (including Belgium) 1 Jan 1583
278 // this is too weird to take into account: the Gregorian calendar was
279 // introduced twice in Groningen, first time 28 Feb 1583 was followed
280 // by 11 Mar 1583, then it has gone back to Julian in the summer of
281 // 1584 and then 13 Dec 1700 -> 12 Jan 1701 - which is
282 // the date we take here
283 Gr_Netherlands_Groningen
, // 13 Dec 1700 -> 12 Jan 1701
284 Gr_Netherlands_Gelderland
, // 30 Jun 1700 -> 12 Jul 1700
285 Gr_Netherlands_Utrecht
, // (and Overijssel) 30 Nov 1700->12 Dec 1700
286 Gr_Netherlands_Friesland
, // (and Drenthe) 31 Dec 1700 -> 12 Jan 1701
288 Gr_Norway
= Gr_Denmark
, // Then part of Denmark
289 Gr_Poland
= Gr_Standard
,
290 Gr_Portugal
= Gr_Standard
,
291 Gr_Romania
, // 31 Mar 1919 -> 14 Apr 1919
292 Gr_Russia
, // 31 Jan 1918 -> 14 Feb 1918
293 Gr_Scotland
= Gr_GreatBritain
,
294 Gr_Spain
= Gr_Standard
,
296 // Sweden has a curious history. Sweden decided to make a gradual
297 // change from the Julian to the Gregorian calendar. By dropping every
298 // leap year from 1700 through 1740 the eleven superfluous days would
299 // be omitted and from 1 Mar 1740 they would be in sync with the
300 // Gregorian calendar. (But in the meantime they would be in sync with
303 // So 1700 (which should have been a leap year in the Julian calendar)
304 // was not a leap year in Sweden. However, by mistake 1704 and 1708
305 // became leap years. This left Sweden out of synchronisation with
306 // both the Julian and the Gregorian world, so they decided to go back
307 // to the Julian calendar. In order to do this, they inserted an extra
308 // day in 1712, making that year a double leap year! So in 1712,
309 // February had 30 days in Sweden.
311 // Later, in 1753, Sweden changed to the Gregorian calendar by
312 // dropping 11 days like everyone else.
313 Gr_Sweden
= Gr_Finland
, // 17 Feb 1753 -> 1 Mar 1753
315 Gr_Switzerland
= Gr_Unknown
,// Different cantons used different dates
316 Gr_Switzerland_Catholic
, // 1583, 1584 or 1597 (we take 1584)
317 Gr_Switzerland_Protestant
, // 31 Dec 1700 -> 12 Jan 1701
319 Gr_Turkey
, // 1 Jan 1927
320 Gr_USA
= Gr_GreatBritain
,
321 Gr_Wales
= Gr_GreatBritain
,
322 Gr_Yugoslavia
// 1919
325 // the country parameter is used so far for calculating the start and
326 // the end of DST period and for deciding whether the date is a work
329 // TODO move this to intl.h
331 // Required for WinCE
338 Country_Unknown
, // no special information for this country
339 Country_Default
, // set the default country with SetCountry() method
340 // or use the default country with any other
342 // TODO add more countries (for this we must know about DST and/or
343 // holidays for this country)
345 // Western European countries: we assume that they all follow the same
346 // DST rules (true or false?)
347 Country_WesternEurope_Start
,
348 Country_EEC
= Country_WesternEurope_Start
,
352 Country_WesternEurope_End
= UK
,
357 // symbolic names for the months
360 Jan
, Feb
, Mar
, Apr
, May
, Jun
, Jul
, Aug
, Sep
, Oct
, Nov
, Dec
, Inv_Month
363 // symbolic names for the weekdays
366 Sun
, Mon
, Tue
, Wed
, Thu
, Fri
, Sat
, Inv_WeekDay
369 // invalid value for the year
372 Inv_Year
= SHRT_MIN
// should hold in wxDateTime_t
375 // flags for GetWeekDayName and GetMonthName
378 Name_Full
= 0x01, // return full name
379 Name_Abbr
= 0x02 // return abbreviated name
382 // flags for GetWeekOfYear and GetWeekOfMonth
385 Default_First
, // Sunday_First for US, Monday_First for the rest
386 Monday_First
, // week starts with a Monday
387 Sunday_First
// week starts with a Sunday
391 // ------------------------------------------------------------------------
393 // a class representing a time zone: basicly, this is just an offset
394 // (in seconds) from GMT
395 class WXDLLIMPEXP_BASE TimeZone
399 TimeZone(wxDateTime_t offset
= 0) { m_offset
= offset
; }
401 long GetOffset() const { return m_offset
; }
404 // offset for this timezone from GMT in seconds
408 // standard struct tm is limited to the years from 1900 (because
409 // tm_year field is the offset from 1900), so we use our own struct
410 // instead to represent broken down time
412 // NB: this struct should always be kept normalized (i.e. mon should
413 // be < 12, 1 <= day <= 31 &c), so use AddMonths(), AddDays()
414 // instead of modifying the member fields directly!
415 struct WXDLLIMPEXP_BASE Tm
417 wxDateTime_t msec
, sec
, min
, hour
, mday
;
421 // default ctor inits the object to an invalid value
424 // ctor from struct tm and the timezone
425 Tm(const struct tm
& tm
, const TimeZone
& tz
);
427 // check that the given date/time is valid (in Gregorian calendar)
428 bool IsValid() const;
431 WeekDay
GetWeekDay() // not const because wday may be changed
433 if ( wday
== Inv_WeekDay
)
436 return (WeekDay
)wday
;
439 // add the given number of months to the date keeping it normalized
440 void AddMonths(int monDiff
);
442 // add the given number of months to the date keeping it normalized
443 void AddDays(int dayDiff
);
446 // compute the weekday from other fields
447 void ComputeWeekDay();
449 // the timezone we correspond to
452 // these values can't be accessed directly because they're not always
453 // computed and we calculate them on demand
454 wxDateTime_t wday
, yday
;
458 // ------------------------------------------------------------------------
460 // set the current country
461 static void SetCountry(Country country
);
462 // get the current country
463 static Country
GetCountry();
465 // return true if the country is a West European one (in practice,
466 // this means that the same DST rules as for EEC apply)
467 static bool IsWestEuropeanCountry(Country country
= Country_Default
);
469 // return the current year
470 static int GetCurrentYear(Calendar cal
= Gregorian
);
472 // convert the year as returned by wxDateTime::GetYear() to a year
473 // suitable for BC/AD notation. The difference is that BC year 1
474 // corresponds to the year 0 (while BC year 0 didn't exist) and AD
475 // year N is just year N.
476 static int ConvertYearToBC(int year
);
478 // return the current month
479 static Month
GetCurrentMonth(Calendar cal
= Gregorian
);
481 // returns true if the given year is a leap year in the given calendar
482 static bool IsLeapYear(int year
= Inv_Year
, Calendar cal
= Gregorian
);
484 // get the century (19 for 1999, 20 for 2000 and -5 for 492 BC)
485 static int GetCentury(int year
= Inv_Year
);
487 // returns the number of days in this year (356 or 355 for Gregorian
488 // calendar usually :-)
489 static wxDateTime_t
GetNumberOfDays(int year
, Calendar cal
= Gregorian
);
491 // get the number of the days in the given month (default value for
492 // the year means the current one)
493 static wxDateTime_t
GetNumberOfDays(Month month
,
495 Calendar cal
= Gregorian
);
497 // get the full (default) or abbreviated month name in the current
498 // locale, returns empty string on error
499 static wxString
GetMonthName(Month month
,
500 NameFlags flags
= Name_Full
);
502 // get the full (default) or abbreviated weekday name in the current
503 // locale, returns empty string on error
504 static wxString
GetWeekDayName(WeekDay weekday
,
505 NameFlags flags
= Name_Full
);
507 // get the AM and PM strings in the current locale (may be empty)
508 static void GetAmPmStrings(wxString
*am
, wxString
*pm
);
510 // return true if the given country uses DST for this year
511 static bool IsDSTApplicable(int year
= Inv_Year
,
512 Country country
= Country_Default
);
514 // get the beginning of DST for this year, will return invalid object
515 // if no DST applicable in this year. The default value of the
516 // parameter means to take the current year.
517 static wxDateTime
GetBeginDST(int year
= Inv_Year
,
518 Country country
= Country_Default
);
519 // get the end of DST for this year, will return invalid object
520 // if no DST applicable in this year. The default value of the
521 // parameter means to take the current year.
522 static wxDateTime
GetEndDST(int year
= Inv_Year
,
523 Country country
= Country_Default
);
525 // return the wxDateTime object for the current time
526 static inline wxDateTime
Now();
528 // return the wxDateTime object for the current time with millisecond
529 // precision (if available on this platform)
530 static wxDateTime
UNow();
532 // return the wxDateTime object for today midnight: i.e. as Now() but
533 // with time set to 0
534 static inline wxDateTime
Today();
536 // constructors: you should test whether the constructor succeeded with
537 // IsValid() function. The values Inv_Month and Inv_Year for the
538 // parameters mean take current month and/or year values.
539 // ------------------------------------------------------------------------
541 // default ctor does not initialize the object, use Set()!
542 wxDateTime() { m_time
= wxLongLong((long)ULONG_MAX
, ULONG_MAX
); }
544 // from time_t: seconds since the Epoch 00:00:00 UTC, Jan 1, 1970)
545 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
546 // VA C++ confuses this with wxDateTime(double jdn) thinking it is a duplicate declaration
547 inline wxDateTime(time_t timet
);
549 // from broken down time/date (only for standard Unix range)
550 inline wxDateTime(const struct tm
& tm
);
551 // from broken down time/date (any range)
552 inline wxDateTime(const Tm
& tm
);
554 // from JDN (beware of rounding errors)
555 inline wxDateTime(double jdn
);
557 // from separate values for each component, date set to today
558 inline wxDateTime(wxDateTime_t hour
,
559 wxDateTime_t minute
= 0,
560 wxDateTime_t second
= 0,
561 wxDateTime_t millisec
= 0);
562 // from separate values for each component with explicit date
563 inline wxDateTime(wxDateTime_t day
, // day of the month
565 int year
= Inv_Year
, // 1999, not 99 please!
566 wxDateTime_t hour
= 0,
567 wxDateTime_t minute
= 0,
568 wxDateTime_t second
= 0,
569 wxDateTime_t millisec
= 0);
571 // default copy ctor ok
575 // assignment operators and Set() functions: all non const methods return
576 // the reference to this object. IsValid() should be used to test whether
577 // the function succeeded.
578 // ------------------------------------------------------------------------
580 // set to the current time
581 inline wxDateTime
& SetToCurrent();
583 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
584 // VA C++ confuses this with wxDateTime(double jdn) thinking it is a duplicate declaration
585 // set to given time_t value
586 inline wxDateTime
& Set(time_t timet
);
589 // set to given broken down time/date
590 wxDateTime
& Set(const struct tm
& tm
);
592 // set to given broken down time/date
593 inline wxDateTime
& Set(const Tm
& tm
);
595 // set to given JDN (beware of rounding errors)
596 wxDateTime
& Set(double jdn
);
598 // set to given time, date = today
599 wxDateTime
& Set(wxDateTime_t hour
,
600 wxDateTime_t minute
= 0,
601 wxDateTime_t second
= 0,
602 wxDateTime_t millisec
= 0);
604 // from separate values for each component with explicit date
605 // (defaults for month and year are the current values)
606 wxDateTime
& Set(wxDateTime_t day
,
608 int year
= Inv_Year
, // 1999, not 99 please!
609 wxDateTime_t hour
= 0,
610 wxDateTime_t minute
= 0,
611 wxDateTime_t second
= 0,
612 wxDateTime_t millisec
= 0);
614 // resets time to 00:00:00, doesn't change the date
615 wxDateTime
& ResetTime();
617 // the following functions don't change the values of the other
618 // fields, i.e. SetMinute() won't change either hour or seconds value
621 wxDateTime
& SetYear(int year
);
623 wxDateTime
& SetMonth(Month month
);
624 // set the day of the month
625 wxDateTime
& SetDay(wxDateTime_t day
);
627 wxDateTime
& SetHour(wxDateTime_t hour
);
629 wxDateTime
& SetMinute(wxDateTime_t minute
);
631 wxDateTime
& SetSecond(wxDateTime_t second
);
633 wxDateTime
& SetMillisecond(wxDateTime_t millisecond
);
635 // assignment operator from time_t
636 wxDateTime
& operator=(time_t timet
) { return Set(timet
); }
638 // assignment operator from broken down time/date
639 wxDateTime
& operator=(const struct tm
& tm
) { return Set(tm
); }
641 // assignment operator from broken down time/date
642 wxDateTime
& operator=(const Tm
& tm
) { return Set(tm
); }
644 // default assignment operator is ok
646 // calendar calculations (functions which set the date only leave the time
647 // unchanged, e.g. don't explictly zero it): SetXXX() functions modify the
648 // object itself, GetXXX() ones return a new object.
649 // ------------------------------------------------------------------------
651 // set to the given week day in the same week as this one
652 wxDateTime
& SetToWeekDayInSameWeek(WeekDay weekday
,
653 WeekFlags flags
= Monday_First
);
654 inline wxDateTime
GetWeekDayInSameWeek(WeekDay weekday
,
655 WeekFlags flags
= Monday_First
) const;
657 // set to the next week day following this one
658 wxDateTime
& SetToNextWeekDay(WeekDay weekday
);
659 inline wxDateTime
GetNextWeekDay(WeekDay weekday
) const;
661 // set to the previous week day before this one
662 wxDateTime
& SetToPrevWeekDay(WeekDay weekday
);
663 inline wxDateTime
GetPrevWeekDay(WeekDay weekday
) const;
665 // set to Nth occurence of given weekday in the given month of the
666 // given year (time is set to 0), return true on success and false on
667 // failure. n may be positive (1..5) or negative to count from the end
668 // of the month (see helper function SetToLastWeekDay())
669 bool SetToWeekDay(WeekDay weekday
,
671 Month month
= Inv_Month
,
672 int year
= Inv_Year
);
673 inline wxDateTime
GetWeekDay(WeekDay weekday
,
675 Month month
= Inv_Month
,
676 int year
= Inv_Year
) const;
678 // sets to the last weekday in the given month, year
679 inline bool SetToLastWeekDay(WeekDay weekday
,
680 Month month
= Inv_Month
,
681 int year
= Inv_Year
);
682 inline wxDateTime
GetLastWeekDay(WeekDay weekday
,
683 Month month
= Inv_Month
,
684 int year
= Inv_Year
);
686 // sets the date to the given day of the given week in the year,
687 // returns true on success and false if given date doesn't exist (e.g.
690 // these functions are badly defined as they're not the reverse of
691 // GetWeekOfYear(), use SetToTheWeekOfYear() instead
692 wxDEPRECATED( bool SetToTheWeek(wxDateTime_t numWeek
,
693 WeekDay weekday
= Mon
,
694 WeekFlags flags
= Monday_First
) );
695 wxDEPRECATED( wxDateTime
GetWeek(wxDateTime_t numWeek
,
696 WeekDay weekday
= Mon
,
697 WeekFlags flags
= Monday_First
) const );
699 // returns the date corresponding to the given week day of the given
700 // week (in ISO notation) of the specified year
701 static wxDateTime
SetToWeekOfYear(int year
,
702 wxDateTime_t numWeek
,
703 WeekDay weekday
= Mon
);
705 // sets the date to the last day of the given (or current) month or the
706 // given (or current) year
707 wxDateTime
& SetToLastMonthDay(Month month
= Inv_Month
,
708 int year
= Inv_Year
);
709 inline wxDateTime
GetLastMonthDay(Month month
= Inv_Month
,
710 int year
= Inv_Year
) const;
712 // sets to the given year day (1..365 or 366)
713 wxDateTime
& SetToYearDay(wxDateTime_t yday
);
714 inline wxDateTime
GetYearDay(wxDateTime_t yday
) const;
716 // The definitions below were taken verbatim from
718 // http://www.capecod.net/~pbaum/date/date0.htm
720 // (Peter Baum's home page)
722 // definition: The Julian Day Number, Julian Day, or JD of a
723 // particular instant of time is the number of days and fractions of a
724 // day since 12 hours Universal Time (Greenwich mean noon) on January
725 // 1 of the year -4712, where the year is given in the Julian
726 // proleptic calendar. The idea of using this reference date was
727 // originally proposed by Joseph Scalizer in 1582 to count years but
728 // it was modified by 19th century astronomers to count days. One
729 // could have equivalently defined the reference time to be noon of
730 // November 24, -4713 if were understood that Gregorian calendar rules
731 // were applied. Julian days are Julian Day Numbers and are not to be
732 // confused with Julian dates.
734 // definition: The Rata Die number is a date specified as the number
735 // of days relative to a base date of December 31 of the year 0. Thus
736 // January 1 of the year 1 is Rata Die day 1.
738 // get the Julian Day number (the fractional part specifies the time of
739 // the day, related to noon - beware of rounding errors!)
740 double GetJulianDayNumber() const;
741 double GetJDN() const { return GetJulianDayNumber(); }
743 // get the Modified Julian Day number: it is equal to JDN - 2400000.5
744 // and so integral MJDs correspond to the midnights (and not noons).
745 // MJD 0 is Nov 17, 1858
746 double GetModifiedJulianDayNumber() const { return GetJDN() - 2400000.5; }
747 double GetMJD() const { return GetModifiedJulianDayNumber(); }
749 // get the Rata Die number
750 double GetRataDie() const;
752 // TODO algorithms for calculating some important dates, such as
753 // religious holidays (Easter...) or moon/solar eclipses? Some
754 // algorithms can be found in the calendar FAQ
757 // Timezone stuff: a wxDateTime object constructed using given
758 // day/month/year/hour/min/sec values is interpreted as this moment in
759 // local time. Using the functions below, it may be converted to another
760 // time zone (e.g., the Unix epoch is wxDateTime(1, Jan, 1970).ToGMT()).
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 // ------------------------------------------------------------------------
768 // transform to any given timezone
769 inline wxDateTime
ToTimezone(const TimeZone
& tz
, bool noDST
= false) const;
770 wxDateTime
& MakeTimezone(const TimeZone
& tz
, bool noDST
= false);
772 // interpret current value as being in another timezone and transform
774 inline wxDateTime
FromTimezone(const TimeZone
& tz
, bool noDST
= false) const;
775 wxDateTime
& MakeFromTimezone(const TimeZone
& tz
, bool noDST
= false);
777 // transform to/from GMT/UTC
778 wxDateTime
ToUTC(bool noDST
= false) const { return ToTimezone(UTC
, noDST
); }
779 wxDateTime
& MakeUTC(bool noDST
= false) { return MakeTimezone(UTC
, noDST
); }
781 wxDateTime
ToGMT(bool noDST
= false) const { return ToUTC(noDST
); }
782 wxDateTime
& MakeGMT(bool noDST
= false) { return MakeUTC(noDST
); }
784 wxDateTime
FromUTC(bool noDST
= false) const
785 { return FromTimezone(UTC
, noDST
); }
786 wxDateTime
& MakeFromUTC(bool noDST
= false)
787 { return MakeFromTimezone(UTC
, noDST
); }
789 // is daylight savings time in effect at this moment according to the
790 // rules of the specified country?
792 // Return value is > 0 if DST is in effect, 0 if it is not and -1 if
793 // the information is not available (this is compatible with ANSI C)
794 int IsDST(Country country
= Country_Default
) const;
797 // accessors: many of them take the timezone parameter which indicates the
798 // timezone for which to make the calculations and the default value means
799 // to do it for the current timezone of this machine (even if the function
800 // only operates with the date it's necessary because a date may wrap as
801 // result of timezone shift)
802 // ------------------------------------------------------------------------
804 // is the date valid?
805 inline bool IsValid() const { return m_time
!= wxInvalidDateTime
.m_time
; }
807 // get the broken down date/time representation in the given timezone
809 // If you wish to get several time components (day, month and year),
810 // consider getting the whole Tm strcuture first and retrieving the
811 // value from it - this is much more efficient
812 Tm
GetTm(const TimeZone
& tz
= Local
) const;
814 // get the number of seconds since the Unix epoch - returns (time_t)-1
815 // if the value is out of range
816 inline time_t GetTicks() const;
818 // get the year (returns Inv_Year if date is invalid)
819 int GetYear(const TimeZone
& tz
= Local
) const
820 { return GetTm(tz
).year
; }
821 // get the month (Inv_Month if date is invalid)
822 Month
GetMonth(const TimeZone
& tz
= Local
) const
823 { return (Month
)GetTm(tz
).mon
; }
824 // get the month day (in 1..31 range, 0 if date is invalid)
825 wxDateTime_t
GetDay(const TimeZone
& tz
= Local
) const
826 { return GetTm(tz
).mday
; }
827 // get the day of the week (Inv_WeekDay if date is invalid)
828 WeekDay
GetWeekDay(const TimeZone
& tz
= Local
) const
829 { return GetTm(tz
).GetWeekDay(); }
830 // get the hour of the day
831 wxDateTime_t
GetHour(const TimeZone
& tz
= Local
) const
832 { return GetTm(tz
).hour
; }
834 wxDateTime_t
GetMinute(const TimeZone
& tz
= Local
) const
835 { return GetTm(tz
).min
; }
837 wxDateTime_t
GetSecond(const TimeZone
& tz
= Local
) const
838 { return GetTm(tz
).sec
; }
840 wxDateTime_t
GetMillisecond(const TimeZone
& tz
= Local
) const
841 { return GetTm(tz
).msec
; }
843 // get the day since the year start (1..366, 0 if date is invalid)
844 wxDateTime_t
GetDayOfYear(const TimeZone
& tz
= Local
) const;
845 // get the week number since the year start (1..52 or 53, 0 if date is
847 wxDateTime_t
GetWeekOfYear(WeekFlags flags
= Monday_First
,
848 const TimeZone
& tz
= Local
) const;
849 // get the week number since the month start (1..5, 0 if date is
851 wxDateTime_t
GetWeekOfMonth(WeekFlags flags
= Monday_First
,
852 const TimeZone
& tz
= Local
) const;
854 // is this date a work day? This depends on a country, of course,
855 // because the holidays are different in different countries
856 bool IsWorkDay(Country country
= Country_Default
) const;
858 // is this date later than Gregorian calendar introduction for the
859 // given country (see enum GregorianAdoption)?
861 // NB: this function shouldn't be considered as absolute authority in
862 // the matter. Besides, for some countries the exact date of
863 // adoption of the Gregorian calendar is simply unknown.
864 bool IsGregorianDate(GregorianAdoption country
= Gr_Standard
) const;
866 // dos date and time format
867 // ------------------------------------------------------------------------
869 // set from the DOS packed format
870 wxDateTime
& SetFromDOS(unsigned long ddt
);
872 // pack the date in DOS format
873 unsigned long GetAsDOS() const;
875 // comparison (see also functions below for operator versions)
876 // ------------------------------------------------------------------------
878 // returns true if the two moments are strictly identical
879 inline bool IsEqualTo(const wxDateTime
& datetime
) const;
881 // returns true if the date is strictly earlier than the given one
882 inline bool IsEarlierThan(const wxDateTime
& datetime
) const;
884 // returns true if the date is strictly later than the given one
885 inline bool IsLaterThan(const wxDateTime
& datetime
) const;
887 // returns true if the date is strictly in the given range
888 inline bool IsStrictlyBetween(const wxDateTime
& t1
,
889 const wxDateTime
& t2
) const;
891 // returns true if the date is in the given range
892 inline bool IsBetween(const wxDateTime
& t1
, const wxDateTime
& t2
) const;
894 // do these two objects refer to the same date?
895 inline bool IsSameDate(const wxDateTime
& dt
) const;
897 // do these two objects have the same time?
898 inline bool IsSameTime(const wxDateTime
& dt
) const;
900 // are these two objects equal up to given timespan?
901 inline bool IsEqualUpTo(const wxDateTime
& dt
, const wxTimeSpan
& ts
) const;
903 inline bool operator<(const wxDateTime
& dt
) const
905 wxASSERT_MSG( IsValid() && dt
.IsValid(), _T("invalid wxDateTime") );
906 return GetValue() < dt
.GetValue();
909 inline bool operator<=(const wxDateTime
& dt
) const
911 wxASSERT_MSG( IsValid() && dt
.IsValid(), _T("invalid wxDateTime") );
912 return GetValue() <= dt
.GetValue();
915 inline bool operator>(const wxDateTime
& dt
) const
917 wxASSERT_MSG( IsValid() && dt
.IsValid(), _T("invalid wxDateTime") );
918 return GetValue() > dt
.GetValue();
921 inline bool operator>=(const wxDateTime
& dt
) const
923 wxASSERT_MSG( IsValid() && dt
.IsValid(), _T("invalid wxDateTime") );
924 return GetValue() >= dt
.GetValue();
927 inline bool operator==(const wxDateTime
& dt
) const
929 wxASSERT_MSG( IsValid() && dt
.IsValid(), _T("invalid wxDateTime") );
930 return GetValue() == dt
.GetValue();
933 inline bool operator!=(const wxDateTime
& dt
) const
935 wxASSERT_MSG( IsValid() && dt
.IsValid(), _T("invalid wxDateTime") );
936 return GetValue() != dt
.GetValue();
939 // arithmetics with dates (see also below for more operators)
940 // ------------------------------------------------------------------------
942 // return the sum of the date with a time span (positive or negative)
943 inline wxDateTime
Add(const wxTimeSpan
& diff
) const;
944 // add a time span (positive or negative)
945 inline wxDateTime
& Add(const wxTimeSpan
& diff
);
946 // add a time span (positive or negative)
947 inline wxDateTime
& operator+=(const wxTimeSpan
& diff
);
948 inline wxDateTime
operator+(const wxTimeSpan
& ts
) const
950 wxDateTime
dt(*this);
955 // return the difference of the date with a time span
956 inline wxDateTime
Subtract(const wxTimeSpan
& diff
) const;
957 // subtract a time span (positive or negative)
958 inline wxDateTime
& Subtract(const wxTimeSpan
& diff
);
959 // subtract a time span (positive or negative)
960 inline wxDateTime
& operator-=(const wxTimeSpan
& diff
);
961 inline wxDateTime
operator-(const wxTimeSpan
& ts
) const
963 wxDateTime
dt(*this);
968 // return the sum of the date with a date span
969 inline wxDateTime
Add(const wxDateSpan
& diff
) const;
970 // add a date span (positive or negative)
971 wxDateTime
& Add(const wxDateSpan
& diff
);
972 // add a date span (positive or negative)
973 inline wxDateTime
& operator+=(const wxDateSpan
& diff
);
974 inline wxDateTime
operator+(const wxDateSpan
& ds
) const
976 wxDateTime
dt(*this);
981 // return the difference of the date with a date span
982 inline wxDateTime
Subtract(const wxDateSpan
& diff
) const;
983 // subtract a date span (positive or negative)
984 inline wxDateTime
& Subtract(const wxDateSpan
& diff
);
985 // subtract a date span (positive or negative)
986 inline wxDateTime
& operator-=(const wxDateSpan
& diff
);
987 inline wxDateTime
operator-(const wxDateSpan
& ds
) const
989 wxDateTime
dt(*this);
994 // return the difference between two dates
995 inline wxTimeSpan
Subtract(const wxDateTime
& dt
) const;
996 inline wxTimeSpan
operator-(const wxDateTime
& dt2
) const;
998 // conversion to/from text: all conversions from text return the pointer to
999 // the next character following the date specification (i.e. the one where
1000 // the scan had to stop) or NULL on failure.
1001 // ------------------------------------------------------------------------
1003 // parse a string in RFC 822 format (found e.g. in mail headers and
1004 // having the form "Wed, 10 Feb 1999 19:07:07 +0100")
1005 const wxChar
*ParseRfc822Date(const wxChar
* date
);
1006 // parse a date/time in the given format (see strptime(3)), fill in
1007 // the missing (in the string) fields with the values of dateDef (by
1008 // default, they will not change if they had valid values or will
1009 // default to Today() otherwise)
1010 const wxChar
*ParseFormat(const wxChar
*date
,
1011 const wxChar
*format
= wxDefaultDateTimeFormat
,
1012 const wxDateTime
& dateDef
= wxDefaultDateTime
);
1013 // parse a string containing the date/time in "free" format, this
1014 // function will try to make an educated guess at the string contents
1015 const wxChar
*ParseDateTime(const wxChar
*datetime
);
1016 // parse a string containing the date only in "free" format (less
1017 // flexible than ParseDateTime)
1018 const wxChar
*ParseDate(const wxChar
*date
);
1019 // parse a string containing the time only in "free" format
1020 const wxChar
*ParseTime(const wxChar
*time
);
1022 // this function accepts strftime()-like format string (default
1023 // argument corresponds to the preferred date and time representation
1024 // for the current locale) and returns the string containing the
1025 // resulting text representation
1026 wxString
Format(const wxChar
*format
= wxDefaultDateTimeFormat
,
1027 const TimeZone
& tz
= Local
) const;
1028 // preferred date representation for the current locale
1029 wxString
FormatDate() const { return Format(_T("%x")); }
1030 // preferred time representation for the current locale
1031 wxString
FormatTime() const { return Format(_T("%X")); }
1032 // returns the string representing the date in ISO 8601 format
1034 wxString
FormatISODate() const { return Format(_T("%Y-%m-%d")); }
1035 // returns the string representing the time in ISO 8601 format
1037 wxString
FormatISOTime() const { return Format(_T("%H:%M:%S")); }
1040 // ------------------------------------------------------------------------
1042 // construct from internal representation
1043 wxDateTime(const wxLongLong
& time
) { m_time
= time
; }
1045 // get the internal representation
1046 inline wxLongLong
GetValue() const;
1048 // a helper function to get the current time_t
1049 static time_t GetTimeNow() { return time((time_t *)NULL
); }
1051 // another one to get the current time broken down
1052 static struct tm
*GetTmNow()
1054 time_t t
= GetTimeNow();
1055 return localtime(&t
);
1059 // the current country - as it's the same for all program objects (unless
1060 // it runs on a _really_ big cluster system :-), this is a static member:
1061 // see SetCountry() and GetCountry()
1062 static Country ms_country
;
1064 // this constant is used to transform a time_t value to the internal
1065 // representation, as time_t is in seconds and we use milliseconds it's
1067 static const long TIME_T_FACTOR
;
1069 // returns true if we fall in range in which we can use standard ANSI C
1071 inline bool IsInStdRange() const;
1073 // the internal representation of the time is the amount of milliseconds
1074 // elapsed since the origin which is set by convention to the UNIX/C epoch
1075 // value: the midnight of January 1, 1970 (UTC)
1079 // ----------------------------------------------------------------------------
1080 // This class contains a difference between 2 wxDateTime values, so it makes
1081 // sense to add it to wxDateTime and it is the result of subtraction of 2
1082 // objects of that class. See also wxDateSpan.
1083 // ----------------------------------------------------------------------------
1085 class WXDLLIMPEXP_BASE wxTimeSpan
1089 // ------------------------------------------------------------------------
1091 // return the timespan for the given number of seconds
1092 static wxTimeSpan
Seconds(long sec
) { return wxTimeSpan(0, 0, sec
); }
1093 static wxTimeSpan
Second() { return Seconds(1); }
1095 // return the timespan for the given number of minutes
1096 static wxTimeSpan
Minutes(long min
) { return wxTimeSpan(0, min
, 0 ); }
1097 static wxTimeSpan
Minute() { return Minutes(1); }
1099 // return the timespan for the given number of hours
1100 static wxTimeSpan
Hours(long hours
) { return wxTimeSpan(hours
, 0, 0); }
1101 static wxTimeSpan
Hour() { return Hours(1); }
1103 // return the timespan for the given number of days
1104 static wxTimeSpan
Days(long days
) { return Hours(24 * days
); }
1105 static wxTimeSpan
Day() { return Days(1); }
1107 // return the timespan for the given number of weeks
1108 static wxTimeSpan
Weeks(long days
) { return Days(7 * days
); }
1109 static wxTimeSpan
Week() { return Weeks(1); }
1111 // default ctor constructs the 0 time span
1114 // from separate values for each component, date set to 0 (hours are
1115 // not restricted to 0..24 range, neither are minutes, seconds or
1117 inline wxTimeSpan(long hours
,
1120 long milliseconds
= 0);
1122 // default copy ctor is ok
1126 // arithmetics with time spans (see also below for more operators)
1127 // ------------------------------------------------------------------------
1129 // return the sum of two timespans
1130 inline wxTimeSpan
Add(const wxTimeSpan
& diff
) const;
1131 // add two timespans together
1132 inline wxTimeSpan
& Add(const wxTimeSpan
& diff
);
1133 // add two timespans together
1134 wxTimeSpan
& operator+=(const wxTimeSpan
& diff
) { return Add(diff
); }
1135 inline wxTimeSpan
operator+(const wxTimeSpan
& ts
) const
1137 return wxTimeSpan(GetValue() + ts
.GetValue());
1140 // return the difference of two timespans
1141 inline wxTimeSpan
Subtract(const wxTimeSpan
& diff
) const;
1142 // subtract another timespan
1143 inline wxTimeSpan
& Subtract(const wxTimeSpan
& diff
);
1144 // subtract another timespan
1145 wxTimeSpan
& operator-=(const wxTimeSpan
& diff
) { return Subtract(diff
); }
1146 inline wxTimeSpan
operator-(const wxTimeSpan
& ts
)
1148 return wxTimeSpan(GetValue() - ts
.GetValue());
1151 // multiply timespan by a scalar
1152 inline wxTimeSpan
Multiply(int n
) const;
1153 // multiply timespan by a scalar
1154 inline wxTimeSpan
& Multiply(int n
);
1155 // multiply timespan by a scalar
1156 wxTimeSpan
& operator*=(int n
) { return Multiply(n
); }
1157 inline wxTimeSpan
operator*(int n
) const
1159 return wxTimeSpan(*this).Multiply(n
);
1162 // return this timespan with opposite sign
1163 wxTimeSpan
Negate() const { return wxTimeSpan(-GetValue()); }
1164 // negate the value of the timespan
1165 wxTimeSpan
& Neg() { m_diff
= -GetValue(); return *this; }
1166 // negate the value of the timespan
1167 wxTimeSpan
& operator-() { return Neg(); }
1169 // return the absolute value of the timespan: does _not_ modify the
1171 inline wxTimeSpan
Abs() const;
1173 // there is intentionally no division because we don't want to
1174 // introduce rounding errors in time calculations
1176 // comparaison (see also operator versions below)
1177 // ------------------------------------------------------------------------
1179 // is the timespan null?
1180 bool IsNull() const { return m_diff
== 0l; }
1181 // returns true if the timespan is null
1182 bool operator!() const { return !IsNull(); }
1184 // is the timespan positive?
1185 bool IsPositive() const { return m_diff
> 0l; }
1187 // is the timespan negative?
1188 bool IsNegative() const { return m_diff
< 0l; }
1190 // are two timespans equal?
1191 inline bool IsEqualTo(const wxTimeSpan
& ts
) const;
1192 // compare two timestamps: works with the absolute values, i.e. -2
1193 // hours is longer than 1 hour. Also, it will return false if the
1194 // timespans are equal in absolute value.
1195 inline bool IsLongerThan(const wxTimeSpan
& ts
) const;
1196 // compare two timestamps: works with the absolute values, i.e. 1
1197 // hour is shorter than -2 hours. Also, it will return false if the
1198 // timespans are equal in absolute value.
1199 bool IsShorterThan(const wxTimeSpan
& t
) const { return !IsLongerThan(t
); }
1201 inline bool operator<(const wxTimeSpan
&ts
) const
1203 return GetValue() < ts
.GetValue();
1206 inline bool operator<=(const wxTimeSpan
&ts
) const
1208 return GetValue() <= ts
.GetValue();
1211 inline bool operator>(const wxTimeSpan
&ts
) const
1213 return GetValue() > ts
.GetValue();
1216 inline bool operator>=(const wxTimeSpan
&ts
) const
1218 return GetValue() >= ts
.GetValue();
1221 inline bool operator==(const wxTimeSpan
&ts
) const
1223 return GetValue() == ts
.GetValue();
1226 inline bool operator!=(const wxTimeSpan
&ts
) const
1228 return GetValue() != ts
.GetValue();
1231 // breaking into days, hours, minutes and seconds
1232 // ------------------------------------------------------------------------
1234 // get the max number of weeks in this timespan
1235 inline int GetWeeks() const;
1236 // get the max number of days in this timespan
1237 inline int GetDays() const;
1238 // get the max number of hours in this timespan
1239 inline int GetHours() const;
1240 // get the max number of minutes in this timespan
1241 inline int GetMinutes() const;
1242 // get the max number of seconds in this timespan
1243 inline wxLongLong
GetSeconds() const;
1244 // get the number of milliseconds in this timespan
1245 wxLongLong
GetMilliseconds() const { return m_diff
; }
1247 // conversion to text
1248 // ------------------------------------------------------------------------
1250 // this function accepts strftime()-like format string (default
1251 // argument corresponds to the preferred date and time representation
1252 // for the current locale) and returns the string containing the
1253 // resulting text representation. Notice that only some of format
1254 // specifiers valid for wxDateTime are valid for wxTimeSpan: hours,
1255 // minutes and seconds make sense, but not "PM/AM" string for example.
1256 wxString
Format(const wxChar
*format
= wxDefaultTimeSpanFormat
) const;
1259 // ------------------------------------------------------------------------
1261 // construct from internal representation
1262 wxTimeSpan(const wxLongLong
& diff
) { m_diff
= diff
; }
1264 // get the internal representation
1265 wxLongLong
GetValue() const { return m_diff
; }
1268 // the (signed) time span in milliseconds
1272 // ----------------------------------------------------------------------------
1273 // This class is a "logical time span" and is useful for implementing program
1274 // logic for such things as "add one month to the date" which, in general,
1275 // doesn't mean to add 60*60*24*31 seconds to it, but to take the same date
1276 // the next month (to understand that this is indeed different consider adding
1277 // one month to Feb, 15 - we want to get Mar, 15, of course).
1279 // When adding a month to the date, all lesser components (days, hours, ...)
1280 // won't be changed unless the resulting date would be invalid: for example,
1281 // Jan 31 + 1 month will be Feb 28, not (non existing) Feb 31.
1283 // Because of this feature, adding and subtracting back again the same
1284 // wxDateSpan will *not*, in general give back the original date: Feb 28 - 1
1285 // month will be Jan 28, not Jan 31!
1287 // wxDateSpan can be either positive or negative. They may be
1288 // multiplied by scalars which multiply all deltas by the scalar: i.e. 2*(1
1289 // month and 1 day) is 2 months and 2 days. They can be added together and
1290 // with wxDateTime or wxTimeSpan, but the type of result is different for each
1293 // Beware about weeks: if you specify both weeks and days, the total number of
1294 // days added will be 7*weeks + days! See also GetTotalDays() function.
1296 // Equality operators are defined for wxDateSpans. Two datespans are equal if
1297 // they both give the same target date when added to *every* source date.
1298 // Thus wxDateSpan::Months(1) is not equal to wxDateSpan::Days(30), because
1299 // they not give the same date when added to 1 Feb. But wxDateSpan::Days(14) is
1300 // equal to wxDateSpan::Weeks(2)
1302 // Finally, notice that for adding hours, minutes &c you don't need this
1303 // class: wxTimeSpan will do the job because there are no subtleties
1304 // associated with those.
1305 // ----------------------------------------------------------------------------
1307 class WXDLLIMPEXP_BASE wxDateSpan
1311 // ------------------------------------------------------------------------
1313 // this many years/months/weeks/days
1314 wxDateSpan(int years
= 0, int months
= 0, int weeks
= 0, int days
= 0)
1322 // get an object for the given number of days
1323 static wxDateSpan
Days(int days
) { return wxDateSpan(0, 0, 0, days
); }
1324 static wxDateSpan
Day() { return Days(1); }
1326 // get an object for the given number of weeks
1327 static wxDateSpan
Weeks(int weeks
) { return wxDateSpan(0, 0, weeks
, 0); }
1328 static wxDateSpan
Week() { return Weeks(1); }
1330 // get an object for the given number of months
1331 static wxDateSpan
Months(int mon
) { return wxDateSpan(0, mon
, 0, 0); }
1332 static wxDateSpan
Month() { return Months(1); }
1334 // get an object for the given number of years
1335 static wxDateSpan
Years(int years
) { return wxDateSpan(years
, 0, 0, 0); }
1336 static wxDateSpan
Year() { return Years(1); }
1338 // default copy ctor is ok
1342 // accessors (all SetXXX() return the (modified) wxDateSpan object)
1343 // ------------------------------------------------------------------------
1345 // set number of years
1346 wxDateSpan
& SetYears(int n
) { m_years
= n
; return *this; }
1347 // set number of months
1348 wxDateSpan
& SetMonths(int n
) { m_months
= n
; return *this; }
1349 // set number of weeks
1350 wxDateSpan
& SetWeeks(int n
) { m_weeks
= n
; return *this; }
1351 // set number of days
1352 wxDateSpan
& SetDays(int n
) { m_days
= n
; return *this; }
1354 // get number of years
1355 int GetYears() const { return m_years
; }
1356 // get number of months
1357 int GetMonths() const { return m_months
; }
1358 // get number of weeks
1359 int GetWeeks() const { return m_weeks
; }
1360 // get number of days
1361 int GetDays() const { return m_days
; }
1362 // returns 7*GetWeeks() + GetDays()
1363 int GetTotalDays() const { return 7*m_weeks
+ m_days
; }
1365 // arithmetics with date spans (see also below for more operators)
1366 // ------------------------------------------------------------------------
1368 // return sum of two date spans
1369 inline wxDateSpan
Add(const wxDateSpan
& other
) const;
1370 // add another wxDateSpan to us
1371 inline wxDateSpan
& Add(const wxDateSpan
& other
);
1372 // add another wxDateSpan to us
1373 inline wxDateSpan
& operator+=(const wxDateSpan
& other
);
1374 inline wxDateSpan
operator+(const wxDateSpan
& ds
) const
1376 return wxDateSpan(GetYears() + ds
.GetYears(),
1377 GetMonths() + ds
.GetMonths(),
1378 GetWeeks() + ds
.GetWeeks(),
1379 GetDays() + ds
.GetDays());
1382 // return difference of two date spans
1383 inline wxDateSpan
Subtract(const wxDateSpan
& other
) const;
1384 // subtract another wxDateSpan from us
1385 inline wxDateSpan
& Subtract(const wxDateSpan
& other
);
1386 // subtract another wxDateSpan from us
1387 inline wxDateSpan
& operator-=(const wxDateSpan
& other
);
1388 inline wxDateSpan
operator-(const wxDateSpan
& ds
) const
1390 return wxDateSpan(GetYears() - ds
.GetYears(),
1391 GetMonths() - ds
.GetMonths(),
1392 GetWeeks() - ds
.GetWeeks(),
1393 GetDays() - ds
.GetDays());
1396 // return a copy of this time span with changed sign
1397 inline wxDateSpan
Negate() const;
1398 // inverse the sign of this timespan
1399 inline wxDateSpan
& Neg();
1400 // inverse the sign of this timespan
1401 wxDateSpan
& operator-() { return Neg(); }
1403 // return the date span proportional to this one with given factor
1404 inline wxDateSpan
Multiply(int factor
) const;
1405 // multiply all components by a (signed) number
1406 inline wxDateSpan
& Multiply(int factor
);
1407 // multiply all components by a (signed) number
1408 inline wxDateSpan
& operator*=(int factor
) { return Multiply(factor
); }
1409 inline wxDateSpan
operator*(int n
) const
1411 return wxDateSpan(*this).Multiply(n
);
1414 // ds1 == d2 if and only if for every wxDateTime t t + ds1 == t + ds2
1415 inline bool operator==(const wxDateSpan
& ds
) const
1417 return GetYears() == ds
.GetYears() &&
1418 GetMonths() == ds
.GetMonths() &&
1419 GetTotalDays() == ds
.GetTotalDays();
1422 inline bool operator!=(const wxDateSpan
& ds
) const
1424 return !(*this == ds
);
1434 // ----------------------------------------------------------------------------
1435 // wxDateTimeArray: array of dates.
1436 // ----------------------------------------------------------------------------
1438 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxDateTime
, wxDateTimeArray
, WXDLLIMPEXP_BASE
);
1440 // ----------------------------------------------------------------------------
1441 // wxDateTimeHolidayAuthority: an object of this class will decide whether a
1442 // given date is a holiday and is used by all functions working with "work
1445 // NB: the base class is an ABC, derived classes must implement the pure
1446 // virtual methods to work with the holidays they correspond to.
1447 // ----------------------------------------------------------------------------
1449 class WXDLLIMPEXP_BASE wxDateTimeHolidayAuthority
;
1450 WX_DEFINE_USER_EXPORTED_ARRAY_PTR(wxDateTimeHolidayAuthority
*,
1451 wxHolidayAuthoritiesArray
,
1452 class WXDLLIMPEXP_BASE
);
1454 class wxDateTimeHolidaysModule
;
1455 class WXDLLIMPEXP_BASE wxDateTimeHolidayAuthority
1457 friend class wxDateTimeHolidaysModule
;
1459 // returns true if the given date is a holiday
1460 static bool IsHoliday(const wxDateTime
& dt
);
1462 // fills the provided array with all holidays in the given range, returns
1463 // the number of them
1464 static size_t GetHolidaysInRange(const wxDateTime
& dtStart
,
1465 const wxDateTime
& dtEnd
,
1466 wxDateTimeArray
& holidays
);
1468 // clear the list of holiday authorities
1469 static void ClearAllAuthorities();
1471 // add a new holiday authority (the pointer will be deleted by
1472 // wxDateTimeHolidayAuthority)
1473 static void AddAuthority(wxDateTimeHolidayAuthority
*auth
);
1475 // the base class must have a virtual dtor
1476 virtual ~wxDateTimeHolidayAuthority();
1479 // this function is called to determine whether a given day is a holiday
1480 virtual bool DoIsHoliday(const wxDateTime
& dt
) const = 0;
1482 // this function should fill the array with all holidays between the two
1483 // given dates - it is implemented in the base class, but in a very
1484 // inefficient way (it just iterates over all days and uses IsHoliday() for
1485 // each of them), so it must be overridden in the derived class where the
1486 // base class version may be explicitly used if needed
1488 // returns the number of holidays in the given range and fills holidays
1490 virtual size_t DoGetHolidaysInRange(const wxDateTime
& dtStart
,
1491 const wxDateTime
& dtEnd
,
1492 wxDateTimeArray
& holidays
) const = 0;
1495 // all holiday authorities
1496 static wxHolidayAuthoritiesArray ms_authorities
;
1499 // the holidays for this class are all Saturdays and Sundays
1500 class WXDLLIMPEXP_BASE wxDateTimeWorkDays
: public wxDateTimeHolidayAuthority
1503 virtual bool DoIsHoliday(const wxDateTime
& dt
) const;
1504 virtual size_t DoGetHolidaysInRange(const wxDateTime
& dtStart
,
1505 const wxDateTime
& dtEnd
,
1506 wxDateTimeArray
& holidays
) const;
1509 // ============================================================================
1510 // inline functions implementation
1511 // ============================================================================
1513 // ----------------------------------------------------------------------------
1515 // ----------------------------------------------------------------------------
1517 #define MILLISECONDS_PER_DAY 86400000l
1519 // some broken compilers (HP-UX CC) refuse to compile the "normal" version, but
1520 // using a temp variable always might prevent other compilers from optimising
1521 // it away - hence use of this ugly macro
1523 #define MODIFY_AND_RETURN(op) return wxDateTime(*this).op
1525 #define MODIFY_AND_RETURN(op) wxDateTime dt(*this); dt.op; return dt
1528 // ----------------------------------------------------------------------------
1529 // wxDateTime construction
1530 // ----------------------------------------------------------------------------
1532 inline bool wxDateTime::IsInStdRange() const
1534 return m_time
>= 0l && (m_time
/ TIME_T_FACTOR
) < LONG_MAX
;
1538 inline wxDateTime
wxDateTime::Now()
1540 return wxDateTime(*GetTmNow());
1544 inline wxDateTime
wxDateTime::Today()
1546 wxDateTime
dt(Now());
1552 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
1553 inline wxDateTime
& wxDateTime::Set(time_t timet
)
1555 // assign first to avoid long multiplication overflow!
1556 m_time
= timet
- WX_TIME_BASE_OFFSET
;
1557 m_time
*= TIME_T_FACTOR
;
1563 inline wxDateTime
& wxDateTime::SetToCurrent()
1569 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
1570 inline wxDateTime::wxDateTime(time_t timet
)
1576 inline wxDateTime::wxDateTime(const struct tm
& tm
)
1581 inline wxDateTime::wxDateTime(const Tm
& tm
)
1586 inline wxDateTime::wxDateTime(double jdn
)
1591 inline wxDateTime
& wxDateTime::Set(const Tm
& tm
)
1593 wxASSERT_MSG( tm
.IsValid(), _T("invalid broken down date/time") );
1595 return Set(tm
.mday
, (Month
)tm
.mon
, tm
.year
,
1596 tm
.hour
, tm
.min
, tm
.sec
, tm
.msec
);
1599 inline wxDateTime::wxDateTime(wxDateTime_t hour
,
1600 wxDateTime_t minute
,
1601 wxDateTime_t second
,
1602 wxDateTime_t millisec
)
1604 Set(hour
, minute
, second
, millisec
);
1607 inline wxDateTime::wxDateTime(wxDateTime_t day
,
1611 wxDateTime_t minute
,
1612 wxDateTime_t second
,
1613 wxDateTime_t millisec
)
1615 Set(day
, month
, year
, hour
, minute
, second
, millisec
);
1618 // ----------------------------------------------------------------------------
1619 // wxDateTime accessors
1620 // ----------------------------------------------------------------------------
1622 inline wxLongLong
wxDateTime::GetValue() const
1624 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1629 inline time_t wxDateTime::GetTicks() const
1631 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1632 if ( !IsInStdRange() )
1637 return (time_t)((m_time
/ (long)TIME_T_FACTOR
).GetLo())+WX_TIME_BASE_OFFSET
;
1640 inline bool wxDateTime::SetToLastWeekDay(WeekDay weekday
,
1644 return SetToWeekDay(weekday
, -1, month
, year
);
1648 wxDateTime::GetWeekDayInSameWeek(WeekDay weekday
,
1649 WeekFlags
WXUNUSED(flags
)) const
1651 MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday
) );
1654 inline wxDateTime
wxDateTime::GetNextWeekDay(WeekDay weekday
) const
1656 MODIFY_AND_RETURN( SetToNextWeekDay(weekday
) );
1659 inline wxDateTime
wxDateTime::GetPrevWeekDay(WeekDay weekday
) const
1661 MODIFY_AND_RETURN( SetToPrevWeekDay(weekday
) );
1664 inline wxDateTime
wxDateTime::GetWeekDay(WeekDay weekday
,
1669 wxDateTime
dt(*this);
1671 return dt
.SetToWeekDay(weekday
, n
, month
, year
) ? dt
: wxInvalidDateTime
;
1674 inline wxDateTime
wxDateTime::GetLastWeekDay(WeekDay weekday
,
1678 wxDateTime
dt(*this);
1680 return dt
.SetToLastWeekDay(weekday
, month
, year
) ? dt
: wxInvalidDateTime
;
1683 inline wxDateTime
wxDateTime::GetLastMonthDay(Month month
, int year
) const
1685 MODIFY_AND_RETURN( SetToLastMonthDay(month
, year
) );
1688 inline wxDateTime
wxDateTime::GetYearDay(wxDateTime_t yday
) const
1690 MODIFY_AND_RETURN( SetToYearDay(yday
) );
1693 // ----------------------------------------------------------------------------
1694 // wxDateTime comparison
1695 // ----------------------------------------------------------------------------
1697 inline bool wxDateTime::IsEqualTo(const wxDateTime
& datetime
) const
1699 wxASSERT_MSG( IsValid() && datetime
.IsValid(), _T("invalid wxDateTime"));
1701 return m_time
== datetime
.m_time
;
1704 inline bool wxDateTime::IsEarlierThan(const wxDateTime
& datetime
) const
1706 wxASSERT_MSG( IsValid() && datetime
.IsValid(), _T("invalid wxDateTime"));
1708 return m_time
< datetime
.m_time
;
1711 inline bool wxDateTime::IsLaterThan(const wxDateTime
& datetime
) const
1713 wxASSERT_MSG( IsValid() && datetime
.IsValid(), _T("invalid wxDateTime"));
1715 return m_time
> datetime
.m_time
;
1718 inline bool wxDateTime::IsStrictlyBetween(const wxDateTime
& t1
,
1719 const wxDateTime
& t2
) const
1721 // no need for assert, will be checked by the functions we call
1722 return IsLaterThan(t1
) && IsEarlierThan(t2
);
1725 inline bool wxDateTime::IsBetween(const wxDateTime
& t1
,
1726 const wxDateTime
& t2
) const
1728 // no need for assert, will be checked by the functions we call
1729 return IsEqualTo(t1
) || IsEqualTo(t2
) || IsStrictlyBetween(t1
, t2
);
1732 inline bool wxDateTime::IsSameDate(const wxDateTime
& dt
) const
1737 return tm1
.year
== tm2
.year
&&
1738 tm1
.mon
== tm2
.mon
&&
1739 tm1
.mday
== tm2
.mday
;
1742 inline bool wxDateTime::IsSameTime(const wxDateTime
& dt
) const
1744 // notice that we can't do something like this:
1746 // m_time % MILLISECONDS_PER_DAY == dt.m_time % MILLISECONDS_PER_DAY
1748 // because we have also to deal with (possibly) different DST settings!
1752 return tm1
.hour
== tm2
.hour
&&
1753 tm1
.min
== tm2
.min
&&
1754 tm1
.sec
== tm2
.sec
&&
1755 tm1
.msec
== tm2
.msec
;
1758 inline bool wxDateTime::IsEqualUpTo(const wxDateTime
& dt
,
1759 const wxTimeSpan
& ts
) const
1761 return IsBetween(dt
.Subtract(ts
), dt
.Add(ts
));
1764 // ----------------------------------------------------------------------------
1765 // wxDateTime arithmetics
1766 // ----------------------------------------------------------------------------
1768 inline wxDateTime
wxDateTime::Add(const wxTimeSpan
& diff
) const
1770 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1772 return wxDateTime(m_time
+ diff
.GetValue());
1775 inline wxDateTime
& wxDateTime::Add(const wxTimeSpan
& diff
)
1777 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1779 m_time
+= diff
.GetValue();
1784 inline wxDateTime
& wxDateTime::operator+=(const wxTimeSpan
& diff
)
1789 inline wxDateTime
wxDateTime::Subtract(const wxTimeSpan
& diff
) const
1791 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1793 return wxDateTime(m_time
- diff
.GetValue());
1796 inline wxDateTime
& wxDateTime::Subtract(const wxTimeSpan
& diff
)
1798 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1800 m_time
-= diff
.GetValue();
1805 inline wxDateTime
& wxDateTime::operator-=(const wxTimeSpan
& diff
)
1807 return Subtract(diff
);
1810 inline wxTimeSpan
wxDateTime::Subtract(const wxDateTime
& datetime
) const
1812 wxASSERT_MSG( IsValid() && datetime
.IsValid(), _T("invalid wxDateTime"));
1814 return wxTimeSpan(GetValue() - datetime
.GetValue());
1817 inline wxTimeSpan
wxDateTime::operator-(const wxDateTime
& dt2
) const
1819 return this->Subtract(dt2
);
1822 inline wxDateTime
wxDateTime::Add(const wxDateSpan
& diff
) const
1824 return wxDateTime(*this).Add(diff
);
1827 inline wxDateTime
& wxDateTime::Subtract(const wxDateSpan
& diff
)
1829 return Add(diff
.Negate());
1832 inline wxDateTime
wxDateTime::Subtract(const wxDateSpan
& diff
) const
1834 return wxDateTime(*this).Subtract(diff
);
1837 inline wxDateTime
& wxDateTime::operator-=(const wxDateSpan
& diff
)
1839 return Subtract(diff
);
1842 inline wxDateTime
& wxDateTime::operator+=(const wxDateSpan
& diff
)
1847 // ----------------------------------------------------------------------------
1848 // wxDateTime and timezones
1849 // ----------------------------------------------------------------------------
1852 wxDateTime::ToTimezone(const wxDateTime::TimeZone
& tz
, bool noDST
) const
1854 MODIFY_AND_RETURN( MakeTimezone(tz
, noDST
) );
1858 wxDateTime::FromTimezone(const wxDateTime::TimeZone
& tz
, bool noDST
) const
1860 MODIFY_AND_RETURN( MakeFromTimezone(tz
, noDST
) );
1863 // ----------------------------------------------------------------------------
1864 // wxTimeSpan construction
1865 // ----------------------------------------------------------------------------
1867 inline wxTimeSpan::wxTimeSpan(long hours
,
1872 // assign first to avoid precision loss
1879 m_diff
+= milliseconds
;
1882 // ----------------------------------------------------------------------------
1883 // wxTimeSpan accessors
1884 // ----------------------------------------------------------------------------
1886 inline wxLongLong
wxTimeSpan::GetSeconds() const
1888 return m_diff
/ 1000l;
1891 inline int wxTimeSpan::GetMinutes() const
1893 return (GetSeconds() / 60l).GetLo();
1896 inline int wxTimeSpan::GetHours() const
1898 return GetMinutes() / 60;
1901 inline int wxTimeSpan::GetDays() const
1903 return GetHours() / 24;
1906 inline int wxTimeSpan::GetWeeks() const
1908 return GetDays() / 7;
1911 // ----------------------------------------------------------------------------
1912 // wxTimeSpan arithmetics
1913 // ----------------------------------------------------------------------------
1915 inline wxTimeSpan
wxTimeSpan::Add(const wxTimeSpan
& diff
) const
1917 return wxTimeSpan(m_diff
+ diff
.GetValue());
1920 inline wxTimeSpan
& wxTimeSpan::Add(const wxTimeSpan
& diff
)
1922 m_diff
+= diff
.GetValue();
1927 inline wxTimeSpan
wxTimeSpan::Subtract(const wxTimeSpan
& diff
) const
1929 return wxTimeSpan(m_diff
- diff
.GetValue());
1932 inline wxTimeSpan
& wxTimeSpan::Subtract(const wxTimeSpan
& diff
)
1934 m_diff
-= diff
.GetValue();
1939 inline wxTimeSpan
& wxTimeSpan::Multiply(int n
)
1946 inline wxTimeSpan
wxTimeSpan::Multiply(int n
) const
1948 return wxTimeSpan(m_diff
* (long)n
);
1951 inline wxTimeSpan
wxTimeSpan::Abs() const
1953 return wxTimeSpan(GetValue().Abs());
1956 inline bool wxTimeSpan::IsEqualTo(const wxTimeSpan
& ts
) const
1958 return GetValue() == ts
.GetValue();
1961 inline bool wxTimeSpan::IsLongerThan(const wxTimeSpan
& ts
) const
1963 return GetValue().Abs() > ts
.GetValue().Abs();
1966 // ----------------------------------------------------------------------------
1968 // ----------------------------------------------------------------------------
1970 inline wxDateSpan
& wxDateSpan::operator+=(const wxDateSpan
& other
)
1972 m_years
+= other
.m_years
;
1973 m_months
+= other
.m_months
;
1974 m_weeks
+= other
.m_weeks
;
1975 m_days
+= other
.m_days
;
1980 inline wxDateSpan
& wxDateSpan::Add(const wxDateSpan
& other
)
1982 return *this += other
;
1985 inline wxDateSpan
wxDateSpan::Add(const wxDateSpan
& other
) const
1987 wxDateSpan
ds(*this);
1992 inline wxDateSpan
& wxDateSpan::Multiply(int factor
)
2002 inline wxDateSpan
wxDateSpan::Multiply(int factor
) const
2004 wxDateSpan
ds(*this);
2005 ds
.Multiply(factor
);
2009 inline wxDateSpan
wxDateSpan::Negate() const
2011 return wxDateSpan(-m_years
, -m_months
, -m_weeks
, -m_days
);
2014 inline wxDateSpan
& wxDateSpan::Neg()
2017 m_months
= -m_months
;
2024 inline wxDateSpan
& wxDateSpan::operator-=(const wxDateSpan
& other
)
2026 return *this += other
.Negate();
2029 inline wxDateSpan
& wxDateSpan::Subtract(const wxDateSpan
& other
)
2031 return *this -= other
;
2034 inline wxDateSpan
wxDateSpan::Subtract(const wxDateSpan
& other
) const
2036 wxDateSpan
ds(*this);
2041 #undef MILLISECONDS_PER_DAY
2043 #undef MODIFY_AND_RETURN
2045 // ============================================================================
2047 // ============================================================================
2049 // ----------------------------------------------------------------------------
2050 // wxTimeSpan operators
2051 // ----------------------------------------------------------------------------
2053 wxTimeSpan WXDLLIMPEXP_BASE
operator*(int n
, const wxTimeSpan
& ts
);
2055 // ----------------------------------------------------------------------------
2057 // ----------------------------------------------------------------------------
2059 wxDateSpan WXDLLIMPEXP_BASE
operator*(int n
, const wxDateSpan
& ds
);
2061 // ============================================================================
2062 // other helper functions
2063 // ============================================================================
2065 // ----------------------------------------------------------------------------
2066 // iteration helpers: can be used to write a for loop over enum variable like
2068 // for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) )
2069 // ----------------------------------------------------------------------------
2071 WXDLLIMPEXP_BASE
void wxNextMonth(wxDateTime::Month
& m
);
2072 WXDLLIMPEXP_BASE
void wxPrevMonth(wxDateTime::Month
& m
);
2073 WXDLLIMPEXP_BASE
void wxNextWDay(wxDateTime::WeekDay
& wd
);
2074 WXDLLIMPEXP_BASE
void wxPrevWDay(wxDateTime::WeekDay
& wd
);
2076 #endif // wxUSE_DATETIME
2078 #endif // _WX_DATETIME_H