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
21 #include "wx/msw/wince/time.h"
26 #include <limits.h> // for INT_MIN
28 #include "wx/longlong.h"
29 #include "wx/anystr.h"
31 class WXDLLIMPEXP_FWD_BASE wxDateTime
;
32 class WXDLLIMPEXP_FWD_BASE wxTimeSpan
;
33 class WXDLLIMPEXP_FWD_BASE wxDateSpan
;
38 #include "wx/dynarray.h"
40 // not all c-runtimes are based on 1/1/1970 being (time_t) 0
41 // set this to the corresponding value in seconds 1/1/1970 has on your
44 #define WX_TIME_BASE_OFFSET 0
49 * + 1. Time zones with minutes (make TimeZone a class)
50 * ? 2. getdate() function like under Solaris
51 * + 3. text conversion for wxDateSpan
52 * + 4. pluggable modules for the workdays calculations
53 * 5. wxDateTimeHolidayAuthority for Easter and other christian feasts
57 The three (main) classes declared in this header represent:
59 1. An absolute moment in the time (wxDateTime)
60 2. A difference between two moments in the time, positive or negative
62 3. A logical difference between two dates expressed in
63 years/months/weeks/days (wxDateSpan)
65 The following arithmetic operations are permitted (all others are not):
70 wxDateTime + wxTimeSpan = wxDateTime
71 wxDateTime + wxDateSpan = wxDateTime
72 wxTimeSpan + wxTimeSpan = wxTimeSpan
73 wxDateSpan + wxDateSpan = wxDateSpan
77 wxDateTime - wxDateTime = wxTimeSpan
78 wxDateTime - wxTimeSpan = wxDateTime
79 wxDateTime - wxDateSpan = wxDateTime
80 wxTimeSpan - wxTimeSpan = wxTimeSpan
81 wxDateSpan - wxDateSpan = wxDateSpan
85 wxTimeSpan * number = wxTimeSpan
86 number * wxTimeSpan = wxTimeSpan
87 wxDateSpan * number = wxDateSpan
88 number * wxDateSpan = wxDateSpan
92 -wxTimeSpan = wxTimeSpan
93 -wxDateSpan = wxDateSpan
95 For each binary operation OP (+, -, *) we have the following operatorOP=() as
96 a method and the method with a symbolic name OPER (Add, Subtract, Multiply)
97 as a synonym for it and another const method with the same name which returns
98 the changed copy of the object and operatorOP() as a global function which is
99 implemented in terms of the const version of OPEN. For the unary - we have
100 operator-() as a method, Neg() as synonym for it and Negate() which returns
101 the copy of the object with the changed sign.
104 // an invalid/default date time object which may be used as the default
105 // argument for arguments of type wxDateTime; it is also returned by all
106 // functions returning wxDateTime on failure (this is why it is also called
107 // wxInvalidDateTime)
108 class WXDLLIMPEXP_FWD_BASE wxDateTime
;
110 extern WXDLLIMPEXP_DATA_BASE(const char) wxDefaultDateTimeFormat
[];
111 extern WXDLLIMPEXP_DATA_BASE(const char) wxDefaultTimeSpanFormat
[];
112 extern WXDLLIMPEXP_DATA_BASE(const wxDateTime
) wxDefaultDateTime
;
114 #define wxInvalidDateTime wxDefaultDateTime
117 // ----------------------------------------------------------------------------
118 // conditional compilation
119 // ----------------------------------------------------------------------------
121 // if configure detected strftime(), we have it too
123 #define wxHAS_STRFTIME
124 // suppose everyone else has strftime except Win CE unless VC8 is used
125 #elif !defined(__WXWINCE__) || defined(__VISUALC8__)
126 #define wxHAS_STRFTIME
129 // ----------------------------------------------------------------------------
130 // wxDateTime represents an absolute moment in the time
131 // ----------------------------------------------------------------------------
133 class WXDLLIMPEXP_BASE wxDateTime
137 // ------------------------------------------------------------------------
139 // a small unsigned integer type for storing things like minutes,
140 // seconds &c. It should be at least short (i.e. not char) to contain
141 // the number of milliseconds - it may also be 'int' because there is
142 // no size penalty associated with it in our code, we don't store any
143 // data in this format
144 typedef unsigned short wxDateTime_t
;
147 // ------------------------------------------------------------------------
152 // the time in the current time zone
155 // zones from GMT (= Greenwich Mean Time): they're guaranteed to be
156 // consequent numbers, so writing something like `GMT0 + offset' is
157 // safe if abs(offset) <= 12
159 // underscore stands for minus
160 GMT_12
, GMT_11
, GMT_10
, GMT_9
, GMT_8
, GMT_7
,
161 GMT_6
, GMT_5
, GMT_4
, GMT_3
, GMT_2
, GMT_1
,
163 GMT1
, GMT2
, GMT3
, GMT4
, GMT5
, GMT6
,
164 GMT7
, GMT8
, GMT9
, GMT10
, GMT11
, GMT12
, GMT13
,
165 // Note that GMT12 and GMT_12 are not the same: there is a difference
166 // of exactly one day between them
168 // some symbolic names for TZ
171 WET
= GMT0
, // Western Europe Time
172 WEST
= GMT1
, // Western Europe Summer Time
173 CET
= GMT1
, // Central Europe Time
174 CEST
= GMT2
, // Central Europe Summer Time
175 EET
= GMT2
, // Eastern Europe Time
176 EEST
= GMT3
, // Eastern Europe Summer Time
177 MSK
= GMT3
, // Moscow Time
178 MSD
= GMT4
, // Moscow Summer Time
181 AST
= GMT_4
, // Atlantic Standard Time
182 ADT
= GMT_3
, // Atlantic Daylight Time
183 EST
= GMT_5
, // Eastern Standard Time
184 EDT
= GMT_4
, // Eastern Daylight Saving Time
185 CST
= GMT_6
, // Central Standard Time
186 CDT
= GMT_5
, // Central Daylight Saving Time
187 MST
= GMT_7
, // Mountain Standard Time
188 MDT
= GMT_6
, // Mountain Daylight Saving Time
189 PST
= GMT_8
, // Pacific Standard Time
190 PDT
= GMT_7
, // Pacific Daylight Saving Time
191 HST
= GMT_10
, // Hawaiian Standard Time
192 AKST
= GMT_9
, // Alaska Standard Time
193 AKDT
= GMT_8
, // Alaska Daylight Saving Time
197 A_WST
= GMT8
, // Western Standard Time
198 A_CST
= GMT13
+ 1, // Central Standard Time (+9.5)
199 A_EST
= GMT10
, // Eastern Standard Time
200 A_ESST
= GMT11
, // Eastern Summer Time
203 NZST
= GMT12
, // Standard Time
204 NZDT
= GMT13
, // Daylight Saving Time
206 // TODO add more symbolic timezone names here
208 // Universal Coordinated Time = the new and politically correct name
213 // the calendar systems we know about: notice that it's valid (for
214 // this classes purpose anyhow) to work with any of these calendars
215 // even with the dates before the historical appearance of the
219 Gregorian
, // current calendar
220 Julian
// calendar in use since -45 until the 1582 (or later)
222 // TODO Hebrew, Chinese, Maya, ... (just kidding) (or then may be not?)
225 // these values only are used to identify the different dates of
226 // adoption of the Gregorian calendar (see IsGregorian())
228 // All data and comments taken verbatim from "The Calendar FAQ (v 2.0)"
229 // by Claus Tøndering, http://www.pip.dknet.dk/~c-t/calendar.html
230 // except for the comments "we take".
232 // Symbol "->" should be read as "was followed by" in the comments
234 enum GregorianAdoption
236 Gr_Unknown
, // no data for this country or it's too uncertain to use
237 Gr_Standard
, // on the day 0 of Gregorian calendar: 15 Oct 1582
239 Gr_Alaska
, // Oct 1867 when Alaska became part of the USA
240 Gr_Albania
, // Dec 1912
242 Gr_Austria
= Gr_Unknown
, // Different regions on different dates
243 Gr_Austria_Brixen
, // 5 Oct 1583 -> 16 Oct 1583
244 Gr_Austria_Salzburg
= Gr_Austria_Brixen
,
245 Gr_Austria_Tyrol
= Gr_Austria_Brixen
,
246 Gr_Austria_Carinthia
, // 14 Dec 1583 -> 25 Dec 1583
247 Gr_Austria_Styria
= Gr_Austria_Carinthia
,
249 Gr_Belgium
, // Then part of the Netherlands
251 Gr_Bulgaria
= Gr_Unknown
, // Unknown precisely (from 1915 to 1920)
252 Gr_Bulgaria_1
, // 18 Mar 1916 -> 1 Apr 1916
253 Gr_Bulgaria_2
, // 31 Mar 1916 -> 14 Apr 1916
254 Gr_Bulgaria_3
, // 3 Sep 1920 -> 17 Sep 1920
256 Gr_Canada
= Gr_Unknown
, // Different regions followed the changes in
257 // Great Britain or France
259 Gr_China
= Gr_Unknown
, // Different authorities say:
260 Gr_China_1
, // 18 Dec 1911 -> 1 Jan 1912
261 Gr_China_2
, // 18 Dec 1928 -> 1 Jan 1929
263 Gr_Czechoslovakia
, // (Bohemia and Moravia) 6 Jan 1584 -> 17 Jan 1584
264 Gr_Denmark
, // (including Norway) 18 Feb 1700 -> 1 Mar 1700
267 Gr_Finland
, // Then part of Sweden
269 Gr_France
, // 9 Dec 1582 -> 20 Dec 1582
270 Gr_France_Alsace
, // 4 Feb 1682 -> 16 Feb 1682
271 Gr_France_Lorraine
, // 16 Feb 1760 -> 28 Feb 1760
272 Gr_France_Strasbourg
, // February 1682
274 Gr_Germany
= Gr_Unknown
, // Different states on different dates:
275 Gr_Germany_Catholic
, // 1583-1585 (we take 1584)
276 Gr_Germany_Prussia
, // 22 Aug 1610 -> 2 Sep 1610
277 Gr_Germany_Protestant
, // 18 Feb 1700 -> 1 Mar 1700
279 Gr_GreatBritain
, // 2 Sep 1752 -> 14 Sep 1752 (use 'cal(1)')
281 Gr_Greece
, // 9 Mar 1924 -> 23 Mar 1924
282 Gr_Hungary
, // 21 Oct 1587 -> 1 Nov 1587
283 Gr_Ireland
= Gr_GreatBritain
,
284 Gr_Italy
= Gr_Standard
,
286 Gr_Japan
= Gr_Unknown
, // Different authorities say:
287 Gr_Japan_1
, // 19 Dec 1872 -> 1 Jan 1873
288 Gr_Japan_2
, // 19 Dec 1892 -> 1 Jan 1893
289 Gr_Japan_3
, // 18 Dec 1918 -> 1 Jan 1919
291 Gr_Latvia
, // 1915-1918 (we take 1915)
292 Gr_Lithuania
, // 1915
293 Gr_Luxemburg
, // 14 Dec 1582 -> 25 Dec 1582
294 Gr_Netherlands
= Gr_Belgium
, // (including Belgium) 1 Jan 1583
296 // this is too weird to take into account: the Gregorian calendar was
297 // introduced twice in Groningen, first time 28 Feb 1583 was followed
298 // by 11 Mar 1583, then it has gone back to Julian in the summer of
299 // 1584 and then 13 Dec 1700 -> 12 Jan 1701 - which is
300 // the date we take here
301 Gr_Netherlands_Groningen
, // 13 Dec 1700 -> 12 Jan 1701
302 Gr_Netherlands_Gelderland
, // 30 Jun 1700 -> 12 Jul 1700
303 Gr_Netherlands_Utrecht
, // (and Overijssel) 30 Nov 1700->12 Dec 1700
304 Gr_Netherlands_Friesland
, // (and Drenthe) 31 Dec 1700 -> 12 Jan 1701
306 Gr_Norway
= Gr_Denmark
, // Then part of Denmark
307 Gr_Poland
= Gr_Standard
,
308 Gr_Portugal
= Gr_Standard
,
309 Gr_Romania
, // 31 Mar 1919 -> 14 Apr 1919
310 Gr_Russia
, // 31 Jan 1918 -> 14 Feb 1918
311 Gr_Scotland
= Gr_GreatBritain
,
312 Gr_Spain
= Gr_Standard
,
314 // Sweden has a curious history. Sweden decided to make a gradual
315 // change from the Julian to the Gregorian calendar. By dropping every
316 // leap year from 1700 through 1740 the eleven superfluous days would
317 // be omitted and from 1 Mar 1740 they would be in sync with the
318 // Gregorian calendar. (But in the meantime they would be in sync with
321 // So 1700 (which should have been a leap year in the Julian calendar)
322 // was not a leap year in Sweden. However, by mistake 1704 and 1708
323 // became leap years. This left Sweden out of synchronisation with
324 // both the Julian and the Gregorian world, so they decided to go back
325 // to the Julian calendar. In order to do this, they inserted an extra
326 // day in 1712, making that year a double leap year! So in 1712,
327 // February had 30 days in Sweden.
329 // Later, in 1753, Sweden changed to the Gregorian calendar by
330 // dropping 11 days like everyone else.
331 Gr_Sweden
= Gr_Finland
, // 17 Feb 1753 -> 1 Mar 1753
333 Gr_Switzerland
= Gr_Unknown
,// Different cantons used different dates
334 Gr_Switzerland_Catholic
, // 1583, 1584 or 1597 (we take 1584)
335 Gr_Switzerland_Protestant
, // 31 Dec 1700 -> 12 Jan 1701
337 Gr_Turkey
, // 1 Jan 1927
338 Gr_USA
= Gr_GreatBritain
,
339 Gr_Wales
= Gr_GreatBritain
,
340 Gr_Yugoslavia
// 1919
343 // the country parameter is used so far for calculating the start and
344 // the end of DST period and for deciding whether the date is a work
347 // TODO move this to intl.h
349 // Required for WinCE
356 Country_Unknown
, // no special information for this country
357 Country_Default
, // set the default country with SetCountry() method
358 // or use the default country with any other
360 // TODO add more countries (for this we must know about DST and/or
361 // holidays for this country)
363 // Western European countries: we assume that they all follow the same
364 // DST rules (true or false?)
365 Country_WesternEurope_Start
,
366 Country_EEC
= Country_WesternEurope_Start
,
370 Country_WesternEurope_End
= UK
,
375 // symbolic names for the months
378 Jan
, Feb
, Mar
, Apr
, May
, Jun
, Jul
, Aug
, Sep
, Oct
, Nov
, Dec
, Inv_Month
381 // symbolic names for the weekdays
384 Sun
, Mon
, Tue
, Wed
, Thu
, Fri
, Sat
, Inv_WeekDay
387 // invalid value for the year
390 Inv_Year
= SHRT_MIN
// should hold in wxDateTime_t
393 // flags for GetWeekDayName and GetMonthName
396 Name_Full
= 0x01, // return full name
397 Name_Abbr
= 0x02 // return abbreviated name
400 // flags for GetWeekOfYear and GetWeekOfMonth
403 Default_First
, // Sunday_First for US, Monday_First for the rest
404 Monday_First
, // week starts with a Monday
405 Sunday_First
// week starts with a Sunday
409 // ------------------------------------------------------------------------
411 // a class representing a time zone: basically, this is just an offset
412 // (in seconds) from GMT
413 class WXDLLIMPEXP_BASE TimeZone
418 // create time zone object with the given offset
419 TimeZone(long offset
= 0) { m_offset
= offset
; }
421 static TimeZone
Make(long offset
)
424 tz
.m_offset
= offset
;
428 long GetOffset() const { return m_offset
; }
431 // offset for this timezone from GMT in seconds
435 // standard struct tm is limited to the years from 1900 (because
436 // tm_year field is the offset from 1900), so we use our own struct
437 // instead to represent broken down time
439 // NB: this struct should always be kept normalized (i.e. mon should
440 // be < 12, 1 <= day <= 31 &c), so use AddMonths(), AddDays()
441 // instead of modifying the member fields directly!
442 struct WXDLLIMPEXP_BASE Tm
444 wxDateTime_t msec
, sec
, min
, hour
,
445 mday
, // Day of the month in 1..31 range.
446 yday
; // Day of the year in 0..365 range.
450 // default ctor inits the object to an invalid value
453 // ctor from struct tm and the timezone
454 Tm(const struct tm
& tm
, const TimeZone
& tz
);
456 // check that the given date/time is valid (in Gregorian calendar)
457 bool IsValid() const;
460 WeekDay
GetWeekDay() // not const because wday may be changed
462 if ( wday
== Inv_WeekDay
)
465 return (WeekDay
)wday
;
468 // add the given number of months to the date keeping it normalized
469 void AddMonths(int monDiff
);
471 // add the given number of months to the date keeping it normalized
472 void AddDays(int dayDiff
);
475 // compute the weekday from other fields
476 void ComputeWeekDay();
478 // the timezone we correspond to
481 // This value can only be accessed via GetWeekDay() and not directly
482 // because it's not always computed when creating this object and may
483 // need to be calculated on demand.
488 // ------------------------------------------------------------------------
490 // set the current country
491 static void SetCountry(Country country
);
492 // get the current country
493 static Country
GetCountry();
495 // return true if the country is a West European one (in practice,
496 // this means that the same DST rules as for EEC apply)
497 static bool IsWestEuropeanCountry(Country country
= Country_Default
);
499 // return the current year
500 static int GetCurrentYear(Calendar cal
= Gregorian
);
502 // convert the year as returned by wxDateTime::GetYear() to a year
503 // suitable for BC/AD notation. The difference is that BC year 1
504 // corresponds to the year 0 (while BC year 0 didn't exist) and AD
505 // year N is just year N.
506 static int ConvertYearToBC(int year
);
508 // return the current month
509 static Month
GetCurrentMonth(Calendar cal
= Gregorian
);
511 // returns true if the given year is a leap year in the given calendar
512 static bool IsLeapYear(int year
= Inv_Year
, Calendar cal
= Gregorian
);
514 // get the century (19 for 1999, 20 for 2000 and -5 for 492 BC)
515 static int GetCentury(int year
);
517 // returns the number of days in this year (356 or 355 for Gregorian
518 // calendar usually :-)
519 static wxDateTime_t
GetNumberOfDays(int year
, Calendar cal
= Gregorian
);
521 // get the number of the days in the given month (default value for
522 // the year means the current one)
523 static wxDateTime_t
GetNumberOfDays(Month month
,
525 Calendar cal
= Gregorian
);
528 // get the full (default) or abbreviated month name in the current
529 // locale, returns empty string on error
530 static wxString
GetMonthName(Month month
,
531 NameFlags flags
= Name_Full
);
533 // get the standard English full (default) or abbreviated month name
534 static wxString
GetEnglishMonthName(Month month
,
535 NameFlags flags
= Name_Full
);
537 // get the full (default) or abbreviated weekday name in the current
538 // locale, returns empty string on error
539 static wxString
GetWeekDayName(WeekDay weekday
,
540 NameFlags flags
= Name_Full
);
542 // get the standard English full (default) or abbreviated weekday name
543 static wxString
GetEnglishWeekDayName(WeekDay weekday
,
544 NameFlags flags
= Name_Full
);
546 // get the AM and PM strings in the current locale (may be empty)
547 static void GetAmPmStrings(wxString
*am
, wxString
*pm
);
549 // return true if the given country uses DST for this year
550 static bool IsDSTApplicable(int year
= Inv_Year
,
551 Country country
= Country_Default
);
553 // get the beginning of DST for this year, will return invalid object
554 // if no DST applicable in this year. The default value of the
555 // parameter means to take the current year.
556 static wxDateTime
GetBeginDST(int year
= Inv_Year
,
557 Country country
= Country_Default
);
558 // get the end of DST for this year, will return invalid object
559 // if no DST applicable in this year. The default value of the
560 // parameter means to take the current year.
561 static wxDateTime
GetEndDST(int year
= Inv_Year
,
562 Country country
= Country_Default
);
564 // return the wxDateTime object for the current time
565 static inline wxDateTime
Now();
567 // return the wxDateTime object for the current time with millisecond
568 // precision (if available on this platform)
569 static wxDateTime
UNow();
571 // return the wxDateTime object for today midnight: i.e. as Now() but
572 // with time set to 0
573 static inline wxDateTime
Today();
575 // constructors: you should test whether the constructor succeeded with
576 // IsValid() function. The values Inv_Month and Inv_Year for the
577 // parameters mean take current month and/or year values.
578 // ------------------------------------------------------------------------
580 // default ctor does not initialize the object, use Set()!
581 wxDateTime() { m_time
= wxLongLong(wxINT32_MIN
, 0); }
583 // from time_t: seconds since the Epoch 00:00:00 UTC, Jan 1, 1970)
584 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
585 // VA C++ confuses this with wxDateTime(double jdn) thinking it is a duplicate declaration
586 inline wxDateTime(time_t timet
);
588 // from broken down time/date (only for standard Unix range)
589 inline wxDateTime(const struct tm
& tm
);
590 // from broken down time/date (any range)
591 inline wxDateTime(const Tm
& tm
);
593 // from JDN (beware of rounding errors)
594 inline wxDateTime(double jdn
);
596 // from separate values for each component, date set to today
597 inline wxDateTime(wxDateTime_t hour
,
598 wxDateTime_t minute
= 0,
599 wxDateTime_t second
= 0,
600 wxDateTime_t millisec
= 0);
601 // from separate values for each component with explicit date
602 inline wxDateTime(wxDateTime_t day
, // day of the month
604 int year
= Inv_Year
, // 1999, not 99 please!
605 wxDateTime_t hour
= 0,
606 wxDateTime_t minute
= 0,
607 wxDateTime_t second
= 0,
608 wxDateTime_t millisec
= 0);
610 wxDateTime(const struct _SYSTEMTIME
& st
)
612 SetFromMSWSysTime(st
);
616 // default copy ctor ok
620 // assignment operators and Set() functions: all non const methods return
621 // the reference to this object. IsValid() should be used to test whether
622 // the function succeeded.
623 // ------------------------------------------------------------------------
625 // set to the current time
626 inline wxDateTime
& SetToCurrent();
628 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
629 // VA C++ confuses this with wxDateTime(double jdn) thinking it is a duplicate declaration
630 // set to given time_t value
631 inline wxDateTime
& Set(time_t timet
);
634 // set to given broken down time/date
635 wxDateTime
& Set(const struct tm
& tm
);
637 // set to given broken down time/date
638 inline wxDateTime
& Set(const Tm
& tm
);
640 // set to given JDN (beware of rounding errors)
641 wxDateTime
& Set(double jdn
);
643 // set to given time, date = today
644 wxDateTime
& Set(wxDateTime_t hour
,
645 wxDateTime_t minute
= 0,
646 wxDateTime_t second
= 0,
647 wxDateTime_t millisec
= 0);
649 // from separate values for each component with explicit date
650 // (defaults for month and year are the current values)
651 wxDateTime
& Set(wxDateTime_t day
,
653 int year
= Inv_Year
, // 1999, not 99 please!
654 wxDateTime_t hour
= 0,
655 wxDateTime_t minute
= 0,
656 wxDateTime_t second
= 0,
657 wxDateTime_t millisec
= 0);
659 // resets time to 00:00:00, doesn't change the date
660 wxDateTime
& ResetTime();
662 // get the date part of this object only, i.e. the object which has the
663 // same date as this one but time of 00:00:00
664 wxDateTime
GetDateOnly() const;
666 // the following functions don't change the values of the other
667 // fields, i.e. SetMinute() won't change either hour or seconds value
670 wxDateTime
& SetYear(int year
);
672 wxDateTime
& SetMonth(Month month
);
673 // set the day of the month
674 wxDateTime
& SetDay(wxDateTime_t day
);
676 wxDateTime
& SetHour(wxDateTime_t hour
);
678 wxDateTime
& SetMinute(wxDateTime_t minute
);
680 wxDateTime
& SetSecond(wxDateTime_t second
);
682 wxDateTime
& SetMillisecond(wxDateTime_t millisecond
);
684 // assignment operator from time_t
685 wxDateTime
& operator=(time_t timet
) { return Set(timet
); }
687 // assignment operator from broken down time/date
688 wxDateTime
& operator=(const struct tm
& tm
) { return Set(tm
); }
690 // assignment operator from broken down time/date
691 wxDateTime
& operator=(const Tm
& tm
) { return Set(tm
); }
693 // default assignment operator is ok
695 // calendar calculations (functions which set the date only leave the time
696 // unchanged, e.g. don't explicitly zero it): SetXXX() functions modify the
697 // object itself, GetXXX() ones return a new object.
698 // ------------------------------------------------------------------------
700 // set to the given week day in the same week as this one
701 wxDateTime
& SetToWeekDayInSameWeek(WeekDay weekday
,
702 WeekFlags flags
= Monday_First
);
703 inline wxDateTime
GetWeekDayInSameWeek(WeekDay weekday
,
704 WeekFlags flags
= Monday_First
) const;
706 // set to the next week day following this one
707 wxDateTime
& SetToNextWeekDay(WeekDay weekday
);
708 inline wxDateTime
GetNextWeekDay(WeekDay weekday
) const;
710 // set to the previous week day before this one
711 wxDateTime
& SetToPrevWeekDay(WeekDay weekday
);
712 inline wxDateTime
GetPrevWeekDay(WeekDay weekday
) const;
714 // set to Nth occurrence of given weekday in the given month of the
715 // given year (time is set to 0), return true on success and false on
716 // failure. n may be positive (1..5) or negative to count from the end
717 // of the month (see helper function SetToLastWeekDay())
718 bool SetToWeekDay(WeekDay weekday
,
720 Month month
= Inv_Month
,
721 int year
= Inv_Year
);
722 inline wxDateTime
GetWeekDay(WeekDay weekday
,
724 Month month
= Inv_Month
,
725 int year
= Inv_Year
) const;
727 // sets to the last weekday in the given month, year
728 inline bool SetToLastWeekDay(WeekDay weekday
,
729 Month month
= Inv_Month
,
730 int year
= Inv_Year
);
731 inline wxDateTime
GetLastWeekDay(WeekDay weekday
,
732 Month month
= Inv_Month
,
733 int year
= Inv_Year
);
735 #if WXWIN_COMPATIBILITY_2_6
736 // sets the date to the given day of the given week in the year,
737 // returns true on success and false if given date doesn't exist (e.g.
740 // these functions are badly defined as they're not the reverse of
741 // GetWeekOfYear(), use SetToTheWeekOfYear() instead
742 wxDEPRECATED( bool SetToTheWeek(wxDateTime_t numWeek
,
743 WeekDay weekday
= Mon
,
744 WeekFlags flags
= Monday_First
) );
745 wxDEPRECATED( wxDateTime
GetWeek(wxDateTime_t numWeek
,
746 WeekDay weekday
= Mon
,
747 WeekFlags flags
= Monday_First
) const );
748 #endif // WXWIN_COMPATIBILITY_2_6
750 // returns the date corresponding to the given week day of the given
751 // week (in ISO notation) of the specified year
752 static wxDateTime
SetToWeekOfYear(int year
,
753 wxDateTime_t numWeek
,
754 WeekDay weekday
= Mon
);
756 // sets the date to the last day of the given (or current) month or the
757 // given (or current) year
758 wxDateTime
& SetToLastMonthDay(Month month
= Inv_Month
,
759 int year
= Inv_Year
);
760 inline wxDateTime
GetLastMonthDay(Month month
= Inv_Month
,
761 int year
= Inv_Year
) const;
763 // sets to the given year day (1..365 or 366)
764 wxDateTime
& SetToYearDay(wxDateTime_t yday
);
765 inline wxDateTime
GetYearDay(wxDateTime_t yday
) const;
767 // The definitions below were taken verbatim from
769 // http://www.capecod.net/~pbaum/date/date0.htm
771 // (Peter Baum's home page)
773 // definition: The Julian Day Number, Julian Day, or JD of a
774 // particular instant of time is the number of days and fractions of a
775 // day since 12 hours Universal Time (Greenwich mean noon) on January
776 // 1 of the year -4712, where the year is given in the Julian
777 // proleptic calendar. The idea of using this reference date was
778 // originally proposed by Joseph Scalizer in 1582 to count years but
779 // it was modified by 19th century astronomers to count days. One
780 // could have equivalently defined the reference time to be noon of
781 // November 24, -4713 if were understood that Gregorian calendar rules
782 // were applied. Julian days are Julian Day Numbers and are not to be
783 // confused with Julian dates.
785 // definition: The Rata Die number is a date specified as the number
786 // of days relative to a base date of December 31 of the year 0. Thus
787 // January 1 of the year 1 is Rata Die day 1.
789 // get the Julian Day number (the fractional part specifies the time of
790 // the day, related to noon - beware of rounding errors!)
791 double GetJulianDayNumber() const;
792 double GetJDN() const { return GetJulianDayNumber(); }
794 // get the Modified Julian Day number: it is equal to JDN - 2400000.5
795 // and so integral MJDs correspond to the midnights (and not noons).
796 // MJD 0 is Nov 17, 1858
797 double GetModifiedJulianDayNumber() const { return GetJDN() - 2400000.5; }
798 double GetMJD() const { return GetModifiedJulianDayNumber(); }
800 // get the Rata Die number
801 double GetRataDie() const;
803 // TODO algorithms for calculating some important dates, such as
804 // religious holidays (Easter...) or moon/solar eclipses? Some
805 // algorithms can be found in the calendar FAQ
808 // Timezone stuff: a wxDateTime object constructed using given
809 // day/month/year/hour/min/sec values is interpreted as this moment in
810 // local time. Using the functions below, it may be converted to another
811 // time zone (e.g., the Unix epoch is wxDateTime(1, Jan, 1970).ToGMT()).
813 // These functions try to handle DST internally, but there is no magical
814 // way to know all rules for it in all countries in the world, so if the
815 // program can handle it itself (or doesn't want to handle it at all for
816 // whatever reason), the DST handling can be disabled with noDST.
817 // ------------------------------------------------------------------------
819 // transform to any given timezone
820 inline wxDateTime
ToTimezone(const TimeZone
& tz
, bool noDST
= false) const;
821 wxDateTime
& MakeTimezone(const TimeZone
& tz
, bool noDST
= false);
823 // interpret current value as being in another timezone and transform
825 inline wxDateTime
FromTimezone(const TimeZone
& tz
, bool noDST
= false) const;
826 wxDateTime
& MakeFromTimezone(const TimeZone
& tz
, bool noDST
= false);
828 // transform to/from GMT/UTC
829 wxDateTime
ToUTC(bool noDST
= false) const { return ToTimezone(UTC
, noDST
); }
830 wxDateTime
& MakeUTC(bool noDST
= false) { return MakeTimezone(UTC
, noDST
); }
832 wxDateTime
ToGMT(bool noDST
= false) const { return ToUTC(noDST
); }
833 wxDateTime
& MakeGMT(bool noDST
= false) { return MakeUTC(noDST
); }
835 wxDateTime
FromUTC(bool noDST
= false) const
836 { return FromTimezone(UTC
, noDST
); }
837 wxDateTime
& MakeFromUTC(bool noDST
= false)
838 { return MakeFromTimezone(UTC
, noDST
); }
840 // is daylight savings time in effect at this moment according to the
841 // rules of the specified country?
843 // Return value is > 0 if DST is in effect, 0 if it is not and -1 if
844 // the information is not available (this is compatible with ANSI C)
845 int IsDST(Country country
= Country_Default
) const;
848 // accessors: many of them take the timezone parameter which indicates the
849 // timezone for which to make the calculations and the default value means
850 // to do it for the current timezone of this machine (even if the function
851 // only operates with the date it's necessary because a date may wrap as
852 // result of timezone shift)
853 // ------------------------------------------------------------------------
855 // is the date valid?
856 inline bool IsValid() const { return m_time
!= wxInvalidDateTime
.m_time
; }
858 // get the broken down date/time representation in the given timezone
860 // If you wish to get several time components (day, month and year),
861 // consider getting the whole Tm strcuture first and retrieving the
862 // value from it - this is much more efficient
863 Tm
GetTm(const TimeZone
& tz
= Local
) const;
865 // get the number of seconds since the Unix epoch - returns (time_t)-1
866 // if the value is out of range
867 inline time_t GetTicks() const;
869 // get the century, same as GetCentury(GetYear())
870 int GetCentury(const TimeZone
& tz
= Local
) const
871 { return GetCentury(GetYear(tz
)); }
872 // get the year (returns Inv_Year if date is invalid)
873 int GetYear(const TimeZone
& tz
= Local
) const
874 { return GetTm(tz
).year
; }
875 // get the month (Inv_Month if date is invalid)
876 Month
GetMonth(const TimeZone
& tz
= Local
) const
877 { return (Month
)GetTm(tz
).mon
; }
878 // get the month day (in 1..31 range, 0 if date is invalid)
879 wxDateTime_t
GetDay(const TimeZone
& tz
= Local
) const
880 { return GetTm(tz
).mday
; }
881 // get the day of the week (Inv_WeekDay if date is invalid)
882 WeekDay
GetWeekDay(const TimeZone
& tz
= Local
) const
883 { return GetTm(tz
).GetWeekDay(); }
884 // get the hour of the day
885 wxDateTime_t
GetHour(const TimeZone
& tz
= Local
) const
886 { return GetTm(tz
).hour
; }
888 wxDateTime_t
GetMinute(const TimeZone
& tz
= Local
) const
889 { return GetTm(tz
).min
; }
891 wxDateTime_t
GetSecond(const TimeZone
& tz
= Local
) const
892 { return GetTm(tz
).sec
; }
894 wxDateTime_t
GetMillisecond(const TimeZone
& tz
= Local
) const
895 { return GetTm(tz
).msec
; }
897 // get the day since the year start (1..366, 0 if date is invalid)
898 wxDateTime_t
GetDayOfYear(const TimeZone
& tz
= Local
) const;
899 // get the week number since the year start (1..52 or 53, 0 if date is
901 wxDateTime_t
GetWeekOfYear(WeekFlags flags
= Monday_First
,
902 const TimeZone
& tz
= Local
) const;
903 // get the week number since the month start (1..5, 0 if date is
905 wxDateTime_t
GetWeekOfMonth(WeekFlags flags
= Monday_First
,
906 const TimeZone
& tz
= Local
) const;
908 // is this date a work day? This depends on a country, of course,
909 // because the holidays are different in different countries
910 bool IsWorkDay(Country country
= Country_Default
) const;
912 // is this date later than Gregorian calendar introduction for the
913 // given country (see enum GregorianAdoption)?
915 // NB: this function shouldn't be considered as absolute authority in
916 // the matter. Besides, for some countries the exact date of
917 // adoption of the Gregorian calendar is simply unknown.
918 bool IsGregorianDate(GregorianAdoption country
= Gr_Standard
) const;
920 // dos date and time format
921 // ------------------------------------------------------------------------
923 // set from the DOS packed format
924 wxDateTime
& SetFromDOS(unsigned long ddt
);
926 // pack the date in DOS format
927 unsigned long GetAsDOS() const;
930 // ------------------------------------------------------------------------
932 // convert SYSTEMTIME to wxDateTime
933 wxDateTime
& SetFromMSWSysTime(const struct _SYSTEMTIME
& st
);
935 // convert wxDateTime to SYSTEMTIME
936 void GetAsMSWSysTime(struct _SYSTEMTIME
* st
) const;
938 // same as above but only take date part into account, time is always zero
939 wxDateTime
& SetFromMSWSysDate(const struct _SYSTEMTIME
& st
);
940 void GetAsMSWSysDate(struct _SYSTEMTIME
* st
) const;
941 #endif // __WINDOWS__
943 // comparison (see also functions below for operator versions)
944 // ------------------------------------------------------------------------
946 // returns true if the two moments are strictly identical
947 inline bool IsEqualTo(const wxDateTime
& datetime
) const;
949 // returns true if the date is strictly earlier than the given one
950 inline bool IsEarlierThan(const wxDateTime
& datetime
) const;
952 // returns true if the date is strictly later than the given one
953 inline bool IsLaterThan(const wxDateTime
& datetime
) const;
955 // returns true if the date is strictly in the given range
956 inline bool IsStrictlyBetween(const wxDateTime
& t1
,
957 const wxDateTime
& t2
) const;
959 // returns true if the date is in the given range
960 inline bool IsBetween(const wxDateTime
& t1
, const wxDateTime
& t2
) const;
962 // do these two objects refer to the same date?
963 inline bool IsSameDate(const wxDateTime
& dt
) const;
965 // do these two objects have the same time?
966 inline bool IsSameTime(const wxDateTime
& dt
) const;
968 // are these two objects equal up to given timespan?
969 inline bool IsEqualUpTo(const wxDateTime
& dt
, const wxTimeSpan
& ts
) const;
971 inline bool operator<(const wxDateTime
& dt
) const
973 wxASSERT_MSG( IsValid() && dt
.IsValid(), wxT("invalid wxDateTime") );
974 return GetValue() < dt
.GetValue();
977 inline bool operator<=(const wxDateTime
& dt
) const
979 wxASSERT_MSG( IsValid() && dt
.IsValid(), wxT("invalid wxDateTime") );
980 return GetValue() <= dt
.GetValue();
983 inline bool operator>(const wxDateTime
& dt
) const
985 wxASSERT_MSG( IsValid() && dt
.IsValid(), wxT("invalid wxDateTime") );
986 return GetValue() > dt
.GetValue();
989 inline bool operator>=(const wxDateTime
& dt
) const
991 wxASSERT_MSG( IsValid() && dt
.IsValid(), wxT("invalid wxDateTime") );
992 return GetValue() >= dt
.GetValue();
995 inline bool operator==(const wxDateTime
& dt
) const
997 wxASSERT_MSG( IsValid() && dt
.IsValid(), wxT("invalid wxDateTime") );
998 return GetValue() == dt
.GetValue();
1001 inline bool operator!=(const wxDateTime
& dt
) const
1003 wxASSERT_MSG( IsValid() && dt
.IsValid(), wxT("invalid wxDateTime") );
1004 return GetValue() != dt
.GetValue();
1007 // arithmetics with dates (see also below for more operators)
1008 // ------------------------------------------------------------------------
1010 // return the sum of the date with a time span (positive or negative)
1011 inline wxDateTime
Add(const wxTimeSpan
& diff
) const;
1012 // add a time span (positive or negative)
1013 inline wxDateTime
& Add(const wxTimeSpan
& diff
);
1014 // add a time span (positive or negative)
1015 inline wxDateTime
& operator+=(const wxTimeSpan
& diff
);
1016 inline wxDateTime
operator+(const wxTimeSpan
& ts
) const
1018 wxDateTime
dt(*this);
1023 // return the difference of the date with a time span
1024 inline wxDateTime
Subtract(const wxTimeSpan
& diff
) const;
1025 // subtract a time span (positive or negative)
1026 inline wxDateTime
& Subtract(const wxTimeSpan
& diff
);
1027 // subtract a time span (positive or negative)
1028 inline wxDateTime
& operator-=(const wxTimeSpan
& diff
);
1029 inline wxDateTime
operator-(const wxTimeSpan
& ts
) const
1031 wxDateTime
dt(*this);
1036 // return the sum of the date with a date span
1037 inline wxDateTime
Add(const wxDateSpan
& diff
) const;
1038 // add a date span (positive or negative)
1039 wxDateTime
& Add(const wxDateSpan
& diff
);
1040 // add a date span (positive or negative)
1041 inline wxDateTime
& operator+=(const wxDateSpan
& diff
);
1042 inline wxDateTime
operator+(const wxDateSpan
& ds
) const
1044 wxDateTime
dt(*this);
1049 // return the difference of the date with a date span
1050 inline wxDateTime
Subtract(const wxDateSpan
& diff
) const;
1051 // subtract a date span (positive or negative)
1052 inline wxDateTime
& Subtract(const wxDateSpan
& diff
);
1053 // subtract a date span (positive or negative)
1054 inline wxDateTime
& operator-=(const wxDateSpan
& diff
);
1055 inline wxDateTime
operator-(const wxDateSpan
& ds
) const
1057 wxDateTime
dt(*this);
1062 // return the difference between two dates
1063 inline wxTimeSpan
Subtract(const wxDateTime
& dt
) const;
1064 inline wxTimeSpan
operator-(const wxDateTime
& dt2
) const;
1066 // conversion to/from text
1067 // ------------------------------------------------------------------------
1069 // all conversions functions return true to indicate whether parsing
1070 // succeeded or failed and fill in the provided end iterator, which must
1071 // not be NULL, with the location of the character where the parsing
1072 // stopped (this will be end() of the passed string if everything was
1075 // parse a string in RFC 822 format (found e.g. in mail headers and
1076 // having the form "Wed, 10 Feb 1999 19:07:07 +0100")
1077 bool ParseRfc822Date(const wxString
& date
,
1078 wxString::const_iterator
*end
);
1080 // parse a date/time in the given format (see strptime(3)), fill in
1081 // the missing (in the string) fields with the values of dateDef (by
1082 // default, they will not change if they had valid values or will
1083 // default to Today() otherwise)
1084 bool ParseFormat(const wxString
& date
,
1085 const wxString
& format
,
1086 const wxDateTime
& dateDef
,
1087 wxString::const_iterator
*end
);
1089 bool ParseFormat(const wxString
& date
,
1090 const wxString
& format
,
1091 wxString::const_iterator
*end
)
1093 return ParseFormat(date
, format
, wxDefaultDateTime
, end
);
1096 bool ParseFormat(const wxString
& date
,
1097 wxString::const_iterator
*end
)
1099 return ParseFormat(date
, wxDefaultDateTimeFormat
, wxDefaultDateTime
, end
);
1102 // parse a string containing date, time or both in ISO 8601 format
1104 // notice that these functions are new in wx 3.0 and so we don't
1105 // provide compatibility overloads for them
1106 bool ParseISODate(const wxString
& date
)
1108 wxString::const_iterator end
;
1109 return ParseFormat(date
, wxS("%Y-%m-%d"), &end
) && end
== date
.end();
1112 bool ParseISOTime(const wxString
& time
)
1114 wxString::const_iterator end
;
1115 return ParseFormat(time
, wxS("%H:%M:%S"), &end
) && end
== time
.end();
1118 bool ParseISOCombined(const wxString
& datetime
, char sep
= 'T')
1120 wxString::const_iterator end
;
1121 const wxString fmt
= wxS("%Y-%m-%d") + wxString(sep
) + wxS("%H:%M:%S");
1122 return ParseFormat(datetime
, fmt
, &end
) && end
== datetime
.end();
1125 // parse a string containing the date/time in "free" format, this
1126 // function will try to make an educated guess at the string contents
1127 bool ParseDateTime(const wxString
& datetime
,
1128 wxString::const_iterator
*end
);
1130 // parse a string containing the date only in "free" format (less
1131 // flexible than ParseDateTime)
1132 bool ParseDate(const wxString
& date
,
1133 wxString::const_iterator
*end
);
1135 // parse a string containing the time only in "free" format
1136 bool ParseTime(const wxString
& time
,
1137 wxString::const_iterator
*end
);
1140 // this function accepts strftime()-like format string (default
1141 // argument corresponds to the preferred date and time representation
1142 // for the current locale) and returns the string containing the
1143 // resulting text representation
1144 wxString
Format(const wxString
& format
= wxDefaultDateTimeFormat
,
1145 const TimeZone
& tz
= Local
) const;
1146 // preferred date representation for the current locale
1147 wxString
FormatDate() const { return Format(wxS("%x")); }
1148 // preferred time representation for the current locale
1149 wxString
FormatTime() const { return Format(wxS("%X")); }
1150 // returns the string representing the date in ISO 8601 format
1152 wxString
FormatISODate() const { return Format(wxS("%Y-%m-%d")); }
1153 // returns the string representing the time in ISO 8601 format
1155 wxString
FormatISOTime() const { return Format(wxS("%H:%M:%S")); }
1156 // return the combined date time representation in ISO 8601 format; the
1157 // separator character should be 'T' according to the standard but it
1158 // can also be useful to set it to ' '
1159 wxString
FormatISOCombined(char sep
= 'T') const
1160 { return FormatISODate() + sep
+ FormatISOTime(); }
1163 // backwards compatible versions of the parsing functions: they return an
1164 // object representing the next character following the date specification
1165 // (i.e. the one where the scan had to stop) or a special NULL-like object
1168 // they're not deprecated because a lot of existing code uses them and
1169 // there is no particular harm in keeping them but you should still prefer
1170 // the versions above in the new code
1171 wxAnyStrPtr
ParseRfc822Date(const wxString
& date
)
1173 wxString::const_iterator end
;
1174 return ParseRfc822Date(date
, &end
) ? wxAnyStrPtr(date
, end
)
1178 wxAnyStrPtr
ParseFormat(const wxString
& date
,
1179 const wxString
& format
= wxDefaultDateTimeFormat
,
1180 const wxDateTime
& dateDef
= wxDefaultDateTime
)
1182 wxString::const_iterator end
;
1183 return ParseFormat(date
, format
, dateDef
, &end
) ? wxAnyStrPtr(date
, end
)
1187 wxAnyStrPtr
ParseDateTime(const wxString
& datetime
)
1189 wxString::const_iterator end
;
1190 return ParseDateTime(datetime
, &end
) ? wxAnyStrPtr(datetime
, end
)
1194 wxAnyStrPtr
ParseDate(const wxString
& date
)
1196 wxString::const_iterator end
;
1197 return ParseDate(date
, &end
) ? wxAnyStrPtr(date
, end
)
1201 wxAnyStrPtr
ParseTime(const wxString
& time
)
1203 wxString::const_iterator end
;
1204 return ParseTime(time
, &end
) ? wxAnyStrPtr(time
, end
)
1208 // In addition to wxAnyStrPtr versions above we also must provide the
1209 // overloads for C strings as we must return a pointer into the original
1210 // string and not inside a temporary wxString which would have been created
1211 // if the overloads above were used.
1213 // And then we also have to provide the overloads for wxCStrData, as usual.
1214 // Unfortunately those ones can't return anything as we don't have any
1215 // sufficiently long-lived wxAnyStrPtr to return from them: any temporary
1216 // strings it would point to would be destroyed when this function returns
1217 // making it impossible to dereference the return value. So we just don't
1218 // return anything from here which at least allows to keep compatibility
1219 // with the code not testing the return value. Other uses of this method
1220 // need to be converted to use one of the new bool-returning overloads
1222 void ParseRfc822Date(const wxCStrData
& date
)
1223 { ParseRfc822Date(wxString(date
)); }
1224 const char* ParseRfc822Date(const char* date
);
1225 const wchar_t* ParseRfc822Date(const wchar_t* date
);
1227 void ParseFormat(const wxCStrData
& date
,
1228 const wxString
& format
= wxDefaultDateTimeFormat
,
1229 const wxDateTime
& dateDef
= wxDefaultDateTime
)
1230 { ParseFormat(wxString(date
), format
, dateDef
); }
1231 const char* ParseFormat(const char* date
,
1232 const wxString
& format
= wxDefaultDateTimeFormat
,
1233 const wxDateTime
& dateDef
= wxDefaultDateTime
);
1234 const wchar_t* ParseFormat(const wchar_t* date
,
1235 const wxString
& format
= wxDefaultDateTimeFormat
,
1236 const wxDateTime
& dateDef
= wxDefaultDateTime
);
1238 void ParseDateTime(const wxCStrData
& datetime
)
1239 { ParseDateTime(wxString(datetime
)); }
1240 const char* ParseDateTime(const char* datetime
);
1241 const wchar_t* ParseDateTime(const wchar_t* datetime
);
1243 void ParseDate(const wxCStrData
& date
)
1244 { ParseDate(wxString(date
)); }
1245 const char* ParseDate(const char* date
);
1246 const wchar_t* ParseDate(const wchar_t* date
);
1248 void ParseTime(const wxCStrData
& time
)
1249 { ParseTime(wxString(time
)); }
1250 const char* ParseTime(const char* time
);
1251 const wchar_t* ParseTime(const wchar_t* time
);
1255 // ------------------------------------------------------------------------
1257 // construct from internal representation
1258 wxDateTime(const wxLongLong
& time
) { m_time
= time
; }
1260 // get the internal representation
1261 inline wxLongLong
GetValue() const;
1263 // a helper function to get the current time_t
1264 static time_t GetTimeNow() { return time(NULL
); }
1266 // another one to get the current time broken down
1267 static struct tm
*GetTmNow()
1269 static struct tm l_CurrentTime
;
1270 return GetTmNow(&l_CurrentTime
);
1273 // get current time using thread-safe function
1274 static struct tm
*GetTmNow(struct tm
*tmstruct
);
1277 // the current country - as it's the same for all program objects (unless
1278 // it runs on a _really_ big cluster system :-), this is a static member:
1279 // see SetCountry() and GetCountry()
1280 static Country ms_country
;
1282 // this constant is used to transform a time_t value to the internal
1283 // representation, as time_t is in seconds and we use milliseconds it's
1285 static const long TIME_T_FACTOR
;
1287 // returns true if we fall in range in which we can use standard ANSI C
1289 inline bool IsInStdRange() const;
1291 // the internal representation of the time is the amount of milliseconds
1292 // elapsed since the origin which is set by convention to the UNIX/C epoch
1293 // value: the midnight of January 1, 1970 (UTC)
1297 // ----------------------------------------------------------------------------
1298 // This class contains a difference between 2 wxDateTime values, so it makes
1299 // sense to add it to wxDateTime and it is the result of subtraction of 2
1300 // objects of that class. See also wxDateSpan.
1301 // ----------------------------------------------------------------------------
1303 class WXDLLIMPEXP_BASE wxTimeSpan
1307 // ------------------------------------------------------------------------
1309 // return the timespan for the given number of milliseconds
1310 static wxTimeSpan
Milliseconds(wxLongLong ms
) { return wxTimeSpan(0, 0, 0, ms
); }
1311 static wxTimeSpan
Millisecond() { return Milliseconds(1); }
1313 // return the timespan for the given number of seconds
1314 static wxTimeSpan
Seconds(wxLongLong sec
) { return wxTimeSpan(0, 0, sec
); }
1315 static wxTimeSpan
Second() { return Seconds(1); }
1317 // return the timespan for the given number of minutes
1318 static wxTimeSpan
Minutes(long min
) { return wxTimeSpan(0, min
, 0 ); }
1319 static wxTimeSpan
Minute() { return Minutes(1); }
1321 // return the timespan for the given number of hours
1322 static wxTimeSpan
Hours(long hours
) { return wxTimeSpan(hours
, 0, 0); }
1323 static wxTimeSpan
Hour() { return Hours(1); }
1325 // return the timespan for the given number of days
1326 static wxTimeSpan
Days(long days
) { return Hours(24 * days
); }
1327 static wxTimeSpan
Day() { return Days(1); }
1329 // return the timespan for the given number of weeks
1330 static wxTimeSpan
Weeks(long days
) { return Days(7 * days
); }
1331 static wxTimeSpan
Week() { return Weeks(1); }
1333 // default ctor constructs the 0 time span
1336 // from separate values for each component, date set to 0 (hours are
1337 // not restricted to 0..24 range, neither are minutes, seconds or
1339 inline wxTimeSpan(long hours
,
1341 wxLongLong seconds
= 0,
1342 wxLongLong milliseconds
= 0);
1344 // default copy ctor is ok
1348 // arithmetics with time spans (see also below for more operators)
1349 // ------------------------------------------------------------------------
1351 // return the sum of two timespans
1352 inline wxTimeSpan
Add(const wxTimeSpan
& diff
) const;
1353 // add two timespans together
1354 inline wxTimeSpan
& Add(const wxTimeSpan
& diff
);
1355 // add two timespans together
1356 wxTimeSpan
& operator+=(const wxTimeSpan
& diff
) { return Add(diff
); }
1357 inline wxTimeSpan
operator+(const wxTimeSpan
& ts
) const
1359 return wxTimeSpan(GetValue() + ts
.GetValue());
1362 // return the difference of two timespans
1363 inline wxTimeSpan
Subtract(const wxTimeSpan
& diff
) const;
1364 // subtract another timespan
1365 inline wxTimeSpan
& Subtract(const wxTimeSpan
& diff
);
1366 // subtract another timespan
1367 wxTimeSpan
& operator-=(const wxTimeSpan
& diff
) { return Subtract(diff
); }
1368 inline wxTimeSpan
operator-(const wxTimeSpan
& ts
)
1370 return wxTimeSpan(GetValue() - ts
.GetValue());
1373 // multiply timespan by a scalar
1374 inline wxTimeSpan
Multiply(int n
) const;
1375 // multiply timespan by a scalar
1376 inline wxTimeSpan
& Multiply(int n
);
1377 // multiply timespan by a scalar
1378 wxTimeSpan
& operator*=(int n
) { return Multiply(n
); }
1379 inline wxTimeSpan
operator*(int n
) const
1381 return wxTimeSpan(*this).Multiply(n
);
1384 // return this timespan with opposite sign
1385 wxTimeSpan
Negate() const { return wxTimeSpan(-GetValue()); }
1386 // negate the value of the timespan
1387 wxTimeSpan
& Neg() { m_diff
= -GetValue(); return *this; }
1388 // negate the value of the timespan
1389 wxTimeSpan
& operator-() { return Neg(); }
1391 // return the absolute value of the timespan: does _not_ modify the
1393 inline wxTimeSpan
Abs() const;
1395 // there is intentionally no division because we don't want to
1396 // introduce rounding errors in time calculations
1398 // comparaison (see also operator versions below)
1399 // ------------------------------------------------------------------------
1401 // is the timespan null?
1402 bool IsNull() const { return m_diff
== 0l; }
1403 // returns true if the timespan is null
1404 bool operator!() const { return !IsNull(); }
1406 // is the timespan positive?
1407 bool IsPositive() const { return m_diff
> 0l; }
1409 // is the timespan negative?
1410 bool IsNegative() const { return m_diff
< 0l; }
1412 // are two timespans equal?
1413 inline bool IsEqualTo(const wxTimeSpan
& ts
) const;
1414 // compare two timestamps: works with the absolute values, i.e. -2
1415 // hours is longer than 1 hour. Also, it will return false if the
1416 // timespans are equal in absolute value.
1417 inline bool IsLongerThan(const wxTimeSpan
& ts
) const;
1418 // compare two timestamps: works with the absolute values, i.e. 1
1419 // hour is shorter than -2 hours. Also, it will return false if the
1420 // timespans are equal in absolute value.
1421 bool IsShorterThan(const wxTimeSpan
& t
) const;
1423 inline bool operator<(const wxTimeSpan
&ts
) const
1425 return GetValue() < ts
.GetValue();
1428 inline bool operator<=(const wxTimeSpan
&ts
) const
1430 return GetValue() <= ts
.GetValue();
1433 inline bool operator>(const wxTimeSpan
&ts
) const
1435 return GetValue() > ts
.GetValue();
1438 inline bool operator>=(const wxTimeSpan
&ts
) const
1440 return GetValue() >= ts
.GetValue();
1443 inline bool operator==(const wxTimeSpan
&ts
) const
1445 return GetValue() == ts
.GetValue();
1448 inline bool operator!=(const wxTimeSpan
&ts
) const
1450 return GetValue() != ts
.GetValue();
1453 // breaking into days, hours, minutes and seconds
1454 // ------------------------------------------------------------------------
1456 // get the max number of weeks in this timespan
1457 inline int GetWeeks() const;
1458 // get the max number of days in this timespan
1459 inline int GetDays() const;
1460 // get the max number of hours in this timespan
1461 inline int GetHours() const;
1462 // get the max number of minutes in this timespan
1463 inline int GetMinutes() const;
1464 // get the max number of seconds in this timespan
1465 inline wxLongLong
GetSeconds() const;
1466 // get the number of milliseconds in this timespan
1467 wxLongLong
GetMilliseconds() const { return m_diff
; }
1469 // conversion to text
1470 // ------------------------------------------------------------------------
1472 // this function accepts strftime()-like format string (default
1473 // argument corresponds to the preferred date and time representation
1474 // for the current locale) and returns the string containing the
1475 // resulting text representation. Notice that only some of format
1476 // specifiers valid for wxDateTime are valid for wxTimeSpan: hours,
1477 // minutes and seconds make sense, but not "PM/AM" string for example.
1478 wxString
Format(const wxString
& format
= wxDefaultTimeSpanFormat
) const;
1481 // ------------------------------------------------------------------------
1483 // construct from internal representation
1484 wxTimeSpan(const wxLongLong
& diff
) { m_diff
= diff
; }
1486 // get the internal representation
1487 wxLongLong
GetValue() const { return m_diff
; }
1490 // the (signed) time span in milliseconds
1494 // ----------------------------------------------------------------------------
1495 // This class is a "logical time span" and is useful for implementing program
1496 // logic for such things as "add one month to the date" which, in general,
1497 // doesn't mean to add 60*60*24*31 seconds to it, but to take the same date
1498 // the next month (to understand that this is indeed different consider adding
1499 // one month to Feb, 15 - we want to get Mar, 15, of course).
1501 // When adding a month to the date, all lesser components (days, hours, ...)
1502 // won't be changed unless the resulting date would be invalid: for example,
1503 // Jan 31 + 1 month will be Feb 28, not (non existing) Feb 31.
1505 // Because of this feature, adding and subtracting back again the same
1506 // wxDateSpan will *not*, in general give back the original date: Feb 28 - 1
1507 // month will be Jan 28, not Jan 31!
1509 // wxDateSpan can be either positive or negative. They may be
1510 // multiplied by scalars which multiply all deltas by the scalar: i.e. 2*(1
1511 // month and 1 day) is 2 months and 2 days. They can be added together and
1512 // with wxDateTime or wxTimeSpan, but the type of result is different for each
1515 // Beware about weeks: if you specify both weeks and days, the total number of
1516 // days added will be 7*weeks + days! See also GetTotalDays() function.
1518 // Equality operators are defined for wxDateSpans. Two datespans are equal if
1519 // they both give the same target date when added to *every* source date.
1520 // Thus wxDateSpan::Months(1) is not equal to wxDateSpan::Days(30), because
1521 // they not give the same date when added to 1 Feb. But wxDateSpan::Days(14) is
1522 // equal to wxDateSpan::Weeks(2)
1524 // Finally, notice that for adding hours, minutes &c you don't need this
1525 // class: wxTimeSpan will do the job because there are no subtleties
1526 // associated with those.
1527 // ----------------------------------------------------------------------------
1529 class WXDLLIMPEXP_BASE wxDateSpan
1533 // ------------------------------------------------------------------------
1535 // this many years/months/weeks/days
1536 wxDateSpan(int years
= 0, int months
= 0, int weeks
= 0, int days
= 0)
1544 // get an object for the given number of days
1545 static wxDateSpan
Days(int days
) { return wxDateSpan(0, 0, 0, days
); }
1546 static wxDateSpan
Day() { return Days(1); }
1548 // get an object for the given number of weeks
1549 static wxDateSpan
Weeks(int weeks
) { return wxDateSpan(0, 0, weeks
, 0); }
1550 static wxDateSpan
Week() { return Weeks(1); }
1552 // get an object for the given number of months
1553 static wxDateSpan
Months(int mon
) { return wxDateSpan(0, mon
, 0, 0); }
1554 static wxDateSpan
Month() { return Months(1); }
1556 // get an object for the given number of years
1557 static wxDateSpan
Years(int years
) { return wxDateSpan(years
, 0, 0, 0); }
1558 static wxDateSpan
Year() { return Years(1); }
1560 // default copy ctor is ok
1564 // accessors (all SetXXX() return the (modified) wxDateSpan object)
1565 // ------------------------------------------------------------------------
1567 // set number of years
1568 wxDateSpan
& SetYears(int n
) { m_years
= n
; return *this; }
1569 // set number of months
1570 wxDateSpan
& SetMonths(int n
) { m_months
= n
; return *this; }
1571 // set number of weeks
1572 wxDateSpan
& SetWeeks(int n
) { m_weeks
= n
; return *this; }
1573 // set number of days
1574 wxDateSpan
& SetDays(int n
) { m_days
= n
; return *this; }
1576 // get number of years
1577 int GetYears() const { return m_years
; }
1578 // get number of months
1579 int GetMonths() const { return m_months
; }
1580 // get number of weeks
1581 int GetWeeks() const { return m_weeks
; }
1582 // get number of days
1583 int GetDays() const { return m_days
; }
1584 // returns 7*GetWeeks() + GetDays()
1585 int GetTotalDays() const { return 7*m_weeks
+ m_days
; }
1587 // arithmetics with date spans (see also below for more operators)
1588 // ------------------------------------------------------------------------
1590 // return sum of two date spans
1591 inline wxDateSpan
Add(const wxDateSpan
& other
) const;
1592 // add another wxDateSpan to us
1593 inline wxDateSpan
& Add(const wxDateSpan
& other
);
1594 // add another wxDateSpan to us
1595 inline wxDateSpan
& operator+=(const wxDateSpan
& other
);
1596 inline wxDateSpan
operator+(const wxDateSpan
& ds
) const
1598 return wxDateSpan(GetYears() + ds
.GetYears(),
1599 GetMonths() + ds
.GetMonths(),
1600 GetWeeks() + ds
.GetWeeks(),
1601 GetDays() + ds
.GetDays());
1604 // return difference of two date spans
1605 inline wxDateSpan
Subtract(const wxDateSpan
& other
) const;
1606 // subtract another wxDateSpan from us
1607 inline wxDateSpan
& Subtract(const wxDateSpan
& other
);
1608 // subtract another wxDateSpan from us
1609 inline wxDateSpan
& operator-=(const wxDateSpan
& other
);
1610 inline wxDateSpan
operator-(const wxDateSpan
& ds
) const
1612 return wxDateSpan(GetYears() - ds
.GetYears(),
1613 GetMonths() - ds
.GetMonths(),
1614 GetWeeks() - ds
.GetWeeks(),
1615 GetDays() - ds
.GetDays());
1618 // return a copy of this time span with changed sign
1619 inline wxDateSpan
Negate() const;
1620 // inverse the sign of this timespan
1621 inline wxDateSpan
& Neg();
1622 // inverse the sign of this timespan
1623 wxDateSpan
& operator-() { return Neg(); }
1625 // return the date span proportional to this one with given factor
1626 inline wxDateSpan
Multiply(int factor
) const;
1627 // multiply all components by a (signed) number
1628 inline wxDateSpan
& Multiply(int factor
);
1629 // multiply all components by a (signed) number
1630 inline wxDateSpan
& operator*=(int factor
) { return Multiply(factor
); }
1631 inline wxDateSpan
operator*(int n
) const
1633 return wxDateSpan(*this).Multiply(n
);
1636 // ds1 == d2 if and only if for every wxDateTime t t + ds1 == t + ds2
1637 inline bool operator==(const wxDateSpan
& ds
) const
1639 return GetYears() == ds
.GetYears() &&
1640 GetMonths() == ds
.GetMonths() &&
1641 GetTotalDays() == ds
.GetTotalDays();
1644 inline bool operator!=(const wxDateSpan
& ds
) const
1646 return !(*this == ds
);
1656 // ----------------------------------------------------------------------------
1657 // wxDateTimeArray: array of dates.
1658 // ----------------------------------------------------------------------------
1660 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxDateTime
, wxDateTimeArray
, WXDLLIMPEXP_BASE
);
1662 // ----------------------------------------------------------------------------
1663 // wxDateTimeHolidayAuthority: an object of this class will decide whether a
1664 // given date is a holiday and is used by all functions working with "work
1667 // NB: the base class is an ABC, derived classes must implement the pure
1668 // virtual methods to work with the holidays they correspond to.
1669 // ----------------------------------------------------------------------------
1671 class WXDLLIMPEXP_FWD_BASE wxDateTimeHolidayAuthority
;
1672 WX_DEFINE_USER_EXPORTED_ARRAY_PTR(wxDateTimeHolidayAuthority
*,
1673 wxHolidayAuthoritiesArray
,
1674 class WXDLLIMPEXP_BASE
);
1676 class wxDateTimeHolidaysModule
;
1677 class WXDLLIMPEXP_BASE wxDateTimeHolidayAuthority
1679 friend class wxDateTimeHolidaysModule
;
1681 // returns true if the given date is a holiday
1682 static bool IsHoliday(const wxDateTime
& dt
);
1684 // fills the provided array with all holidays in the given range, returns
1685 // the number of them
1686 static size_t GetHolidaysInRange(const wxDateTime
& dtStart
,
1687 const wxDateTime
& dtEnd
,
1688 wxDateTimeArray
& holidays
);
1690 // clear the list of holiday authorities
1691 static void ClearAllAuthorities();
1693 // add a new holiday authority (the pointer will be deleted by
1694 // wxDateTimeHolidayAuthority)
1695 static void AddAuthority(wxDateTimeHolidayAuthority
*auth
);
1697 // the base class must have a virtual dtor
1698 virtual ~wxDateTimeHolidayAuthority();
1701 // this function is called to determine whether a given day is a holiday
1702 virtual bool DoIsHoliday(const wxDateTime
& dt
) const = 0;
1704 // this function should fill the array with all holidays between the two
1705 // given dates - it is implemented in the base class, but in a very
1706 // inefficient way (it just iterates over all days and uses IsHoliday() for
1707 // each of them), so it must be overridden in the derived class where the
1708 // base class version may be explicitly used if needed
1710 // returns the number of holidays in the given range and fills holidays
1712 virtual size_t DoGetHolidaysInRange(const wxDateTime
& dtStart
,
1713 const wxDateTime
& dtEnd
,
1714 wxDateTimeArray
& holidays
) const = 0;
1717 // all holiday authorities
1718 static wxHolidayAuthoritiesArray ms_authorities
;
1721 // the holidays for this class are all Saturdays and Sundays
1722 class WXDLLIMPEXP_BASE wxDateTimeWorkDays
: public wxDateTimeHolidayAuthority
1725 virtual bool DoIsHoliday(const wxDateTime
& dt
) const;
1726 virtual size_t DoGetHolidaysInRange(const wxDateTime
& dtStart
,
1727 const wxDateTime
& dtEnd
,
1728 wxDateTimeArray
& holidays
) const;
1731 // ============================================================================
1732 // inline functions implementation
1733 // ============================================================================
1735 // ----------------------------------------------------------------------------
1737 // ----------------------------------------------------------------------------
1739 #define MILLISECONDS_PER_DAY 86400000l
1741 // some broken compilers (HP-UX CC) refuse to compile the "normal" version, but
1742 // using a temp variable always might prevent other compilers from optimising
1743 // it away - hence use of this ugly macro
1745 #define MODIFY_AND_RETURN(op) return wxDateTime(*this).op
1747 #define MODIFY_AND_RETURN(op) wxDateTime dt(*this); dt.op; return dt
1750 // ----------------------------------------------------------------------------
1751 // wxDateTime construction
1752 // ----------------------------------------------------------------------------
1754 inline bool wxDateTime::IsInStdRange() const
1756 // currently we don't know what is the real type of time_t so prefer to err
1757 // on the safe side and limit it to 32 bit values which is safe everywhere
1758 return m_time
>= 0l && (m_time
/ TIME_T_FACTOR
) < wxINT32_MAX
;
1762 inline wxDateTime
wxDateTime::Now()
1765 return wxDateTime(*GetTmNow(&tmstruct
));
1769 inline wxDateTime
wxDateTime::Today()
1771 wxDateTime
dt(Now());
1777 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
1778 inline wxDateTime
& wxDateTime::Set(time_t timet
)
1780 // assign first to avoid long multiplication overflow!
1781 m_time
= timet
- WX_TIME_BASE_OFFSET
;
1782 m_time
*= TIME_T_FACTOR
;
1788 inline wxDateTime
& wxDateTime::SetToCurrent()
1794 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
1795 inline wxDateTime::wxDateTime(time_t timet
)
1801 inline wxDateTime::wxDateTime(const struct tm
& tm
)
1806 inline wxDateTime::wxDateTime(const Tm
& tm
)
1811 inline wxDateTime::wxDateTime(double jdn
)
1816 inline wxDateTime
& wxDateTime::Set(const Tm
& tm
)
1818 wxASSERT_MSG( tm
.IsValid(), wxT("invalid broken down date/time") );
1820 return Set(tm
.mday
, (Month
)tm
.mon
, tm
.year
,
1821 tm
.hour
, tm
.min
, tm
.sec
, tm
.msec
);
1824 inline wxDateTime::wxDateTime(wxDateTime_t hour
,
1825 wxDateTime_t minute
,
1826 wxDateTime_t second
,
1827 wxDateTime_t millisec
)
1829 Set(hour
, minute
, second
, millisec
);
1832 inline wxDateTime::wxDateTime(wxDateTime_t day
,
1836 wxDateTime_t minute
,
1837 wxDateTime_t second
,
1838 wxDateTime_t millisec
)
1840 Set(day
, month
, year
, hour
, minute
, second
, millisec
);
1843 // ----------------------------------------------------------------------------
1844 // wxDateTime accessors
1845 // ----------------------------------------------------------------------------
1847 inline wxLongLong
wxDateTime::GetValue() const
1849 wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime"));
1854 inline time_t wxDateTime::GetTicks() const
1856 wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime"));
1857 if ( !IsInStdRange() )
1862 return (time_t)((m_time
/ (long)TIME_T_FACTOR
).ToLong()) + WX_TIME_BASE_OFFSET
;
1865 inline bool wxDateTime::SetToLastWeekDay(WeekDay weekday
,
1869 return SetToWeekDay(weekday
, -1, month
, year
);
1873 wxDateTime::GetWeekDayInSameWeek(WeekDay weekday
,
1874 WeekFlags
WXUNUSED(flags
)) const
1876 MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday
) );
1879 inline wxDateTime
wxDateTime::GetNextWeekDay(WeekDay weekday
) const
1881 MODIFY_AND_RETURN( SetToNextWeekDay(weekday
) );
1884 inline wxDateTime
wxDateTime::GetPrevWeekDay(WeekDay weekday
) const
1886 MODIFY_AND_RETURN( SetToPrevWeekDay(weekday
) );
1889 inline wxDateTime
wxDateTime::GetWeekDay(WeekDay weekday
,
1894 wxDateTime
dt(*this);
1896 return dt
.SetToWeekDay(weekday
, n
, month
, year
) ? dt
: wxInvalidDateTime
;
1899 inline wxDateTime
wxDateTime::GetLastWeekDay(WeekDay weekday
,
1903 wxDateTime
dt(*this);
1905 return dt
.SetToLastWeekDay(weekday
, month
, year
) ? dt
: wxInvalidDateTime
;
1908 inline wxDateTime
wxDateTime::GetLastMonthDay(Month month
, int year
) const
1910 MODIFY_AND_RETURN( SetToLastMonthDay(month
, year
) );
1913 inline wxDateTime
wxDateTime::GetYearDay(wxDateTime_t yday
) const
1915 MODIFY_AND_RETURN( SetToYearDay(yday
) );
1918 // ----------------------------------------------------------------------------
1919 // wxDateTime comparison
1920 // ----------------------------------------------------------------------------
1922 inline bool wxDateTime::IsEqualTo(const wxDateTime
& datetime
) const
1924 wxASSERT_MSG( IsValid() && datetime
.IsValid(), wxT("invalid wxDateTime"));
1926 return m_time
== datetime
.m_time
;
1929 inline bool wxDateTime::IsEarlierThan(const wxDateTime
& datetime
) const
1931 wxASSERT_MSG( IsValid() && datetime
.IsValid(), wxT("invalid wxDateTime"));
1933 return m_time
< datetime
.m_time
;
1936 inline bool wxDateTime::IsLaterThan(const wxDateTime
& datetime
) const
1938 wxASSERT_MSG( IsValid() && datetime
.IsValid(), wxT("invalid wxDateTime"));
1940 return m_time
> datetime
.m_time
;
1943 inline bool wxDateTime::IsStrictlyBetween(const wxDateTime
& t1
,
1944 const wxDateTime
& t2
) const
1946 // no need for assert, will be checked by the functions we call
1947 return IsLaterThan(t1
) && IsEarlierThan(t2
);
1950 inline bool wxDateTime::IsBetween(const wxDateTime
& t1
,
1951 const wxDateTime
& t2
) const
1953 // no need for assert, will be checked by the functions we call
1954 return IsEqualTo(t1
) || IsEqualTo(t2
) || IsStrictlyBetween(t1
, t2
);
1957 inline bool wxDateTime::IsSameDate(const wxDateTime
& dt
) const
1962 return tm1
.year
== tm2
.year
&&
1963 tm1
.mon
== tm2
.mon
&&
1964 tm1
.mday
== tm2
.mday
;
1967 inline bool wxDateTime::IsSameTime(const wxDateTime
& dt
) const
1969 // notice that we can't do something like this:
1971 // m_time % MILLISECONDS_PER_DAY == dt.m_time % MILLISECONDS_PER_DAY
1973 // because we have also to deal with (possibly) different DST settings!
1977 return tm1
.hour
== tm2
.hour
&&
1978 tm1
.min
== tm2
.min
&&
1979 tm1
.sec
== tm2
.sec
&&
1980 tm1
.msec
== tm2
.msec
;
1983 inline bool wxDateTime::IsEqualUpTo(const wxDateTime
& dt
,
1984 const wxTimeSpan
& ts
) const
1986 return IsBetween(dt
.Subtract(ts
), dt
.Add(ts
));
1989 // ----------------------------------------------------------------------------
1990 // wxDateTime arithmetics
1991 // ----------------------------------------------------------------------------
1993 inline wxDateTime
wxDateTime::Add(const wxTimeSpan
& diff
) const
1995 wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime"));
1997 return wxDateTime(m_time
+ diff
.GetValue());
2000 inline wxDateTime
& wxDateTime::Add(const wxTimeSpan
& diff
)
2002 wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime"));
2004 m_time
+= diff
.GetValue();
2009 inline wxDateTime
& wxDateTime::operator+=(const wxTimeSpan
& diff
)
2014 inline wxDateTime
wxDateTime::Subtract(const wxTimeSpan
& diff
) const
2016 wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime"));
2018 return wxDateTime(m_time
- diff
.GetValue());
2021 inline wxDateTime
& wxDateTime::Subtract(const wxTimeSpan
& diff
)
2023 wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime"));
2025 m_time
-= diff
.GetValue();
2030 inline wxDateTime
& wxDateTime::operator-=(const wxTimeSpan
& diff
)
2032 return Subtract(diff
);
2035 inline wxTimeSpan
wxDateTime::Subtract(const wxDateTime
& datetime
) const
2037 wxASSERT_MSG( IsValid() && datetime
.IsValid(), wxT("invalid wxDateTime"));
2039 return wxTimeSpan(GetValue() - datetime
.GetValue());
2042 inline wxTimeSpan
wxDateTime::operator-(const wxDateTime
& dt2
) const
2044 return this->Subtract(dt2
);
2047 inline wxDateTime
wxDateTime::Add(const wxDateSpan
& diff
) const
2049 return wxDateTime(*this).Add(diff
);
2052 inline wxDateTime
& wxDateTime::Subtract(const wxDateSpan
& diff
)
2054 return Add(diff
.Negate());
2057 inline wxDateTime
wxDateTime::Subtract(const wxDateSpan
& diff
) const
2059 return wxDateTime(*this).Subtract(diff
);
2062 inline wxDateTime
& wxDateTime::operator-=(const wxDateSpan
& diff
)
2064 return Subtract(diff
);
2067 inline wxDateTime
& wxDateTime::operator+=(const wxDateSpan
& diff
)
2072 // ----------------------------------------------------------------------------
2073 // wxDateTime and timezones
2074 // ----------------------------------------------------------------------------
2077 wxDateTime::ToTimezone(const wxDateTime::TimeZone
& tz
, bool noDST
) const
2079 MODIFY_AND_RETURN( MakeTimezone(tz
, noDST
) );
2083 wxDateTime::FromTimezone(const wxDateTime::TimeZone
& tz
, bool noDST
) const
2085 MODIFY_AND_RETURN( MakeFromTimezone(tz
, noDST
) );
2088 // ----------------------------------------------------------------------------
2089 // wxTimeSpan construction
2090 // ----------------------------------------------------------------------------
2092 inline wxTimeSpan::wxTimeSpan(long hours
,
2095 wxLongLong milliseconds
)
2097 // assign first to avoid precision loss
2104 m_diff
+= milliseconds
;
2107 // ----------------------------------------------------------------------------
2108 // wxTimeSpan accessors
2109 // ----------------------------------------------------------------------------
2111 inline wxLongLong
wxTimeSpan::GetSeconds() const
2113 return m_diff
/ 1000l;
2116 inline int wxTimeSpan::GetMinutes() const
2118 // For compatibility, this method (and the other accessors) return int,
2119 // even though GetLo() actually returns unsigned long with greater range.
2120 return static_cast<int>((GetSeconds() / 60l).GetLo());
2123 inline int wxTimeSpan::GetHours() const
2125 return GetMinutes() / 60;
2128 inline int wxTimeSpan::GetDays() const
2130 return GetHours() / 24;
2133 inline int wxTimeSpan::GetWeeks() const
2135 return GetDays() / 7;
2138 // ----------------------------------------------------------------------------
2139 // wxTimeSpan arithmetics
2140 // ----------------------------------------------------------------------------
2142 inline wxTimeSpan
wxTimeSpan::Add(const wxTimeSpan
& diff
) const
2144 return wxTimeSpan(m_diff
+ diff
.GetValue());
2147 inline wxTimeSpan
& wxTimeSpan::Add(const wxTimeSpan
& diff
)
2149 m_diff
+= diff
.GetValue();
2154 inline wxTimeSpan
wxTimeSpan::Subtract(const wxTimeSpan
& diff
) const
2156 return wxTimeSpan(m_diff
- diff
.GetValue());
2159 inline wxTimeSpan
& wxTimeSpan::Subtract(const wxTimeSpan
& diff
)
2161 m_diff
-= diff
.GetValue();
2166 inline wxTimeSpan
& wxTimeSpan::Multiply(int n
)
2173 inline wxTimeSpan
wxTimeSpan::Multiply(int n
) const
2175 return wxTimeSpan(m_diff
* (long)n
);
2178 inline wxTimeSpan
wxTimeSpan::Abs() const
2180 return wxTimeSpan(GetValue().Abs());
2183 inline bool wxTimeSpan::IsEqualTo(const wxTimeSpan
& ts
) const
2185 return GetValue() == ts
.GetValue();
2188 inline bool wxTimeSpan::IsLongerThan(const wxTimeSpan
& ts
) const
2190 return GetValue().Abs() > ts
.GetValue().Abs();
2193 inline bool wxTimeSpan::IsShorterThan(const wxTimeSpan
& ts
) const
2195 return GetValue().Abs() < ts
.GetValue().Abs();
2198 // ----------------------------------------------------------------------------
2200 // ----------------------------------------------------------------------------
2202 inline wxDateSpan
& wxDateSpan::operator+=(const wxDateSpan
& other
)
2204 m_years
+= other
.m_years
;
2205 m_months
+= other
.m_months
;
2206 m_weeks
+= other
.m_weeks
;
2207 m_days
+= other
.m_days
;
2212 inline wxDateSpan
& wxDateSpan::Add(const wxDateSpan
& other
)
2214 return *this += other
;
2217 inline wxDateSpan
wxDateSpan::Add(const wxDateSpan
& other
) const
2219 wxDateSpan
ds(*this);
2224 inline wxDateSpan
& wxDateSpan::Multiply(int factor
)
2234 inline wxDateSpan
wxDateSpan::Multiply(int factor
) const
2236 wxDateSpan
ds(*this);
2237 ds
.Multiply(factor
);
2241 inline wxDateSpan
wxDateSpan::Negate() const
2243 return wxDateSpan(-m_years
, -m_months
, -m_weeks
, -m_days
);
2246 inline wxDateSpan
& wxDateSpan::Neg()
2249 m_months
= -m_months
;
2256 inline wxDateSpan
& wxDateSpan::operator-=(const wxDateSpan
& other
)
2258 return *this += other
.Negate();
2261 inline wxDateSpan
& wxDateSpan::Subtract(const wxDateSpan
& other
)
2263 return *this -= other
;
2266 inline wxDateSpan
wxDateSpan::Subtract(const wxDateSpan
& other
) const
2268 wxDateSpan
ds(*this);
2273 #undef MILLISECONDS_PER_DAY
2275 #undef MODIFY_AND_RETURN
2277 // ============================================================================
2279 // ============================================================================
2281 // ----------------------------------------------------------------------------
2282 // wxTimeSpan operators
2283 // ----------------------------------------------------------------------------
2285 wxTimeSpan WXDLLIMPEXP_BASE
operator*(int n
, const wxTimeSpan
& ts
);
2287 // ----------------------------------------------------------------------------
2289 // ----------------------------------------------------------------------------
2291 wxDateSpan WXDLLIMPEXP_BASE
operator*(int n
, const wxDateSpan
& ds
);
2293 // ============================================================================
2294 // other helper functions
2295 // ============================================================================
2297 // ----------------------------------------------------------------------------
2298 // iteration helpers: can be used to write a for loop over enum variable like
2300 // for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) )
2301 // ----------------------------------------------------------------------------
2303 WXDLLIMPEXP_BASE
void wxNextMonth(wxDateTime::Month
& m
);
2304 WXDLLIMPEXP_BASE
void wxPrevMonth(wxDateTime::Month
& m
);
2305 WXDLLIMPEXP_BASE
void wxNextWDay(wxDateTime::WeekDay
& wd
);
2306 WXDLLIMPEXP_BASE
void wxPrevWDay(wxDateTime::WeekDay
& wd
);
2308 #endif // wxUSE_DATETIME
2310 #endif // _WX_DATETIME_H