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"
22 #elif !defined(__WXPALMOS5__)
26 #include <limits.h> // for INT_MIN
28 #include "wx/longlong.h"
30 class WXDLLIMPEXP_FWD_BASE wxDateTime
;
31 class WXDLLIMPEXP_FWD_BASE wxTimeSpan
;
32 class WXDLLIMPEXP_FWD_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 #define WX_TIME_BASE_OFFSET 0
45 * + 1. Time zones with minutes (make TimeZone a class)
46 * ? 2. getdate() function like under Solaris
47 * + 3. text conversion for wxDateSpan
48 * + 4. pluggable modules for the workdays calculations
49 * 5. wxDateTimeHolidayAuthority for Easter and other christian feasts
52 /* Two wrapper functions for thread safety */
53 #ifdef HAVE_LOCALTIME_R
54 #define wxLocaltime_r localtime_r
56 WXDLLIMPEXP_BASE
struct tm
*wxLocaltime_r(const time_t*, struct tm
*);
57 #if wxUSE_THREADS && !defined(__WINDOWS__) && !defined(__WATCOMC__)
58 // On Windows, localtime _is_ threadsafe!
59 #warning using pseudo thread-safe wrapper for localtime to emulate localtime_r
64 #define wxGmtime_r gmtime_r
66 WXDLLIMPEXP_BASE
struct tm
*wxGmtime_r(const time_t*, struct tm
*);
67 #if wxUSE_THREADS && !defined(__WINDOWS__) && !defined(__WATCOMC__)
68 // On Windows, gmtime _is_ threadsafe!
69 #warning using pseudo thread-safe wrapper for gmtime to emulate gmtime_r
74 The three (main) classes declared in this header represent:
76 1. An absolute moment in the time (wxDateTime)
77 2. A difference between two moments in the time, positive or negative
79 3. A logical difference between two dates expressed in
80 years/months/weeks/days (wxDateSpan)
82 The following arithmetic operations are permitted (all others are not):
87 wxDateTime + wxTimeSpan = wxDateTime
88 wxDateTime + wxDateSpan = wxDateTime
89 wxTimeSpan + wxTimeSpan = wxTimeSpan
90 wxDateSpan + wxDateSpan = wxDateSpan
94 wxDateTime - wxDateTime = wxTimeSpan
95 wxDateTime - wxTimeSpan = wxDateTime
96 wxDateTime - wxDateSpan = wxDateTime
97 wxTimeSpan - wxTimeSpan = wxTimeSpan
98 wxDateSpan - wxDateSpan = wxDateSpan
102 wxTimeSpan * number = wxTimeSpan
103 number * wxTimeSpan = wxTimeSpan
104 wxDateSpan * number = wxDateSpan
105 number * wxDateSpan = wxDateSpan
109 -wxTimeSpan = wxTimeSpan
110 -wxDateSpan = wxDateSpan
112 For each binary operation OP (+, -, *) we have the following operatorOP=() as
113 a method and the method with a symbolic name OPER (Add, Subtract, Multiply)
114 as a synonym for it and another const method with the same name which returns
115 the changed copy of the object and operatorOP() as a global function which is
116 implemented in terms of the const version of OPEN. For the unary - we have
117 operator-() as a method, Neg() as synonym for it and Negate() which returns
118 the copy of the object with the changed sign.
121 // an invalid/default date time object which may be used as the default
122 // argument for arguments of type wxDateTime; it is also returned by all
123 // functions returning wxDateTime on failure (this is why it is also called
124 // wxInvalidDateTime)
125 class WXDLLIMPEXP_FWD_BASE wxDateTime
;
127 extern WXDLLIMPEXP_DATA_BASE(const char *) wxDefaultDateTimeFormat
;
128 extern WXDLLIMPEXP_DATA_BASE(const char *) wxDefaultTimeSpanFormat
;
129 extern WXDLLIMPEXP_DATA_BASE(const wxDateTime
) wxDefaultDateTime
;
131 #define wxInvalidDateTime wxDefaultDateTime
133 // ----------------------------------------------------------------------------
134 // wxDateTime represents an absolute moment in the time
135 // ----------------------------------------------------------------------------
137 class WXDLLIMPEXP_BASE wxDateTime
141 // ------------------------------------------------------------------------
143 // a small unsigned integer type for storing things like minutes,
144 // seconds &c. It should be at least short (i.e. not char) to contain
145 // the number of milliseconds - it may also be 'int' because there is
146 // no size penalty associated with it in our code, we don't store any
147 // data in this format
148 typedef unsigned short wxDateTime_t
;
151 // ------------------------------------------------------------------------
156 // the time in the current time zone
159 // zones from GMT (= Greenwhich Mean Time): they're guaranteed to be
160 // consequent numbers, so writing something like `GMT0 + offset' is
161 // safe if abs(offset) <= 12
163 // underscore stands for minus
164 GMT_12
, GMT_11
, GMT_10
, GMT_9
, GMT_8
, GMT_7
,
165 GMT_6
, GMT_5
, GMT_4
, GMT_3
, GMT_2
, GMT_1
,
167 GMT1
, GMT2
, GMT3
, GMT4
, GMT5
, GMT6
,
168 GMT7
, GMT8
, GMT9
, GMT10
, GMT11
, GMT12
, GMT13
,
169 // Note that GMT12 and GMT_12 are not the same: there is a difference
170 // of exactly one day between them
172 // some symbolic names for TZ
175 WET
= GMT0
, // Western Europe Time
176 WEST
= GMT1
, // Western Europe Summer Time
177 CET
= GMT1
, // Central Europe Time
178 CEST
= GMT2
, // Central Europe Summer Time
179 EET
= GMT2
, // Eastern Europe Time
180 EEST
= GMT3
, // Eastern Europe Summer Time
181 MSK
= GMT3
, // Moscow Time
182 MSD
= GMT4
, // Moscow Summer Time
185 AST
= GMT_4
, // Atlantic Standard Time
186 ADT
= GMT_3
, // Atlantic Daylight Time
187 EST
= GMT_5
, // Eastern Standard Time
188 EDT
= GMT_4
, // Eastern Daylight Saving Time
189 CST
= GMT_6
, // Central Standard Time
190 CDT
= GMT_5
, // Central Daylight Saving Time
191 MST
= GMT_7
, // Mountain Standard Time
192 MDT
= GMT_6
, // Mountain Daylight Saving Time
193 PST
= GMT_8
, // Pacific Standard Time
194 PDT
= GMT_7
, // Pacific Daylight Saving Time
195 HST
= GMT_10
, // Hawaiian Standard Time
196 AKST
= GMT_9
, // Alaska Standard Time
197 AKDT
= GMT_8
, // Alaska Daylight Saving Time
201 A_WST
= GMT8
, // Western Standard Time
202 A_CST
= GMT13
+ 1, // Central Standard Time (+9.5)
203 A_EST
= GMT10
, // Eastern Standard Time
204 A_ESST
= GMT11
, // Eastern Summer Time
207 NZST
= GMT12
, // Standard Time
208 NZDT
= GMT13
, // Daylight Saving Time
210 // TODO add more symbolic timezone names here
212 // Universal Coordinated Time = the new and politically correct name
217 // the calendar systems we know about: notice that it's valid (for
218 // this classes purpose anyhow) to work with any of these calendars
219 // even with the dates before the historical appearance of the
223 Gregorian
, // current calendar
224 Julian
// calendar in use since -45 until the 1582 (or later)
226 // TODO Hebrew, Chinese, Maya, ... (just kidding) (or then may be not?)
229 // these values only are used to identify the different dates of
230 // adoption of the Gregorian calendar (see IsGregorian())
232 // All data and comments taken verbatim from "The Calendar FAQ (v 2.0)"
233 // by Claus Tøndering, http://www.pip.dknet.dk/~c-t/calendar.html
234 // except for the comments "we take".
236 // Symbol "->" should be read as "was followed by" in the comments
238 enum GregorianAdoption
240 Gr_Unknown
, // no data for this country or it's too uncertain to use
241 Gr_Standard
, // on the day 0 of Gregorian calendar: 15 Oct 1582
243 Gr_Alaska
, // Oct 1867 when Alaska became part of the USA
244 Gr_Albania
, // Dec 1912
246 Gr_Austria
= Gr_Unknown
, // Different regions on different dates
247 Gr_Austria_Brixen
, // 5 Oct 1583 -> 16 Oct 1583
248 Gr_Austria_Salzburg
= Gr_Austria_Brixen
,
249 Gr_Austria_Tyrol
= Gr_Austria_Brixen
,
250 Gr_Austria_Carinthia
, // 14 Dec 1583 -> 25 Dec 1583
251 Gr_Austria_Styria
= Gr_Austria_Carinthia
,
253 Gr_Belgium
, // Then part of the Netherlands
255 Gr_Bulgaria
= Gr_Unknown
, // Unknown precisely (from 1915 to 1920)
256 Gr_Bulgaria_1
, // 18 Mar 1916 -> 1 Apr 1916
257 Gr_Bulgaria_2
, // 31 Mar 1916 -> 14 Apr 1916
258 Gr_Bulgaria_3
, // 3 Sep 1920 -> 17 Sep 1920
260 Gr_Canada
= Gr_Unknown
, // Different regions followed the changes in
261 // Great Britain or France
263 Gr_China
= Gr_Unknown
, // Different authorities say:
264 Gr_China_1
, // 18 Dec 1911 -> 1 Jan 1912
265 Gr_China_2
, // 18 Dec 1928 -> 1 Jan 1929
267 Gr_Czechoslovakia
, // (Bohemia and Moravia) 6 Jan 1584 -> 17 Jan 1584
268 Gr_Denmark
, // (including Norway) 18 Feb 1700 -> 1 Mar 1700
271 Gr_Finland
, // Then part of Sweden
273 Gr_France
, // 9 Dec 1582 -> 20 Dec 1582
274 Gr_France_Alsace
, // 4 Feb 1682 -> 16 Feb 1682
275 Gr_France_Lorraine
, // 16 Feb 1760 -> 28 Feb 1760
276 Gr_France_Strasbourg
, // February 1682
278 Gr_Germany
= Gr_Unknown
, // Different states on different dates:
279 Gr_Germany_Catholic
, // 1583-1585 (we take 1584)
280 Gr_Germany_Prussia
, // 22 Aug 1610 -> 2 Sep 1610
281 Gr_Germany_Protestant
, // 18 Feb 1700 -> 1 Mar 1700
283 Gr_GreatBritain
, // 2 Sep 1752 -> 14 Sep 1752 (use 'cal(1)')
285 Gr_Greece
, // 9 Mar 1924 -> 23 Mar 1924
286 Gr_Hungary
, // 21 Oct 1587 -> 1 Nov 1587
287 Gr_Ireland
= Gr_GreatBritain
,
288 Gr_Italy
= Gr_Standard
,
290 Gr_Japan
= Gr_Unknown
, // Different authorities say:
291 Gr_Japan_1
, // 19 Dec 1872 -> 1 Jan 1873
292 Gr_Japan_2
, // 19 Dec 1892 -> 1 Jan 1893
293 Gr_Japan_3
, // 18 Dec 1918 -> 1 Jan 1919
295 Gr_Latvia
, // 1915-1918 (we take 1915)
296 Gr_Lithuania
, // 1915
297 Gr_Luxemburg
, // 14 Dec 1582 -> 25 Dec 1582
298 Gr_Netherlands
= Gr_Belgium
, // (including Belgium) 1 Jan 1583
300 // this is too weird to take into account: the Gregorian calendar was
301 // introduced twice in Groningen, first time 28 Feb 1583 was followed
302 // by 11 Mar 1583, then it has gone back to Julian in the summer of
303 // 1584 and then 13 Dec 1700 -> 12 Jan 1701 - which is
304 // the date we take here
305 Gr_Netherlands_Groningen
, // 13 Dec 1700 -> 12 Jan 1701
306 Gr_Netherlands_Gelderland
, // 30 Jun 1700 -> 12 Jul 1700
307 Gr_Netherlands_Utrecht
, // (and Overijssel) 30 Nov 1700->12 Dec 1700
308 Gr_Netherlands_Friesland
, // (and Drenthe) 31 Dec 1700 -> 12 Jan 1701
310 Gr_Norway
= Gr_Denmark
, // Then part of Denmark
311 Gr_Poland
= Gr_Standard
,
312 Gr_Portugal
= Gr_Standard
,
313 Gr_Romania
, // 31 Mar 1919 -> 14 Apr 1919
314 Gr_Russia
, // 31 Jan 1918 -> 14 Feb 1918
315 Gr_Scotland
= Gr_GreatBritain
,
316 Gr_Spain
= Gr_Standard
,
318 // Sweden has a curious history. Sweden decided to make a gradual
319 // change from the Julian to the Gregorian calendar. By dropping every
320 // leap year from 1700 through 1740 the eleven superfluous days would
321 // be omitted and from 1 Mar 1740 they would be in sync with the
322 // Gregorian calendar. (But in the meantime they would be in sync with
325 // So 1700 (which should have been a leap year in the Julian calendar)
326 // was not a leap year in Sweden. However, by mistake 1704 and 1708
327 // became leap years. This left Sweden out of synchronisation with
328 // both the Julian and the Gregorian world, so they decided to go back
329 // to the Julian calendar. In order to do this, they inserted an extra
330 // day in 1712, making that year a double leap year! So in 1712,
331 // February had 30 days in Sweden.
333 // Later, in 1753, Sweden changed to the Gregorian calendar by
334 // dropping 11 days like everyone else.
335 Gr_Sweden
= Gr_Finland
, // 17 Feb 1753 -> 1 Mar 1753
337 Gr_Switzerland
= Gr_Unknown
,// Different cantons used different dates
338 Gr_Switzerland_Catholic
, // 1583, 1584 or 1597 (we take 1584)
339 Gr_Switzerland_Protestant
, // 31 Dec 1700 -> 12 Jan 1701
341 Gr_Turkey
, // 1 Jan 1927
342 Gr_USA
= Gr_GreatBritain
,
343 Gr_Wales
= Gr_GreatBritain
,
344 Gr_Yugoslavia
// 1919
347 // the country parameter is used so far for calculating the start and
348 // the end of DST period and for deciding whether the date is a work
351 // TODO move this to intl.h
353 // Required for WinCE
360 Country_Unknown
, // no special information for this country
361 Country_Default
, // set the default country with SetCountry() method
362 // or use the default country with any other
364 // TODO add more countries (for this we must know about DST and/or
365 // holidays for this country)
367 // Western European countries: we assume that they all follow the same
368 // DST rules (true or false?)
369 Country_WesternEurope_Start
,
370 Country_EEC
= Country_WesternEurope_Start
,
374 Country_WesternEurope_End
= UK
,
379 // symbolic names for the months
382 Jan
, Feb
, Mar
, Apr
, May
, Jun
, Jul
, Aug
, Sep
, Oct
, Nov
, Dec
, Inv_Month
385 // symbolic names for the weekdays
388 Sun
, Mon
, Tue
, Wed
, Thu
, Fri
, Sat
, Inv_WeekDay
391 // invalid value for the year
394 Inv_Year
= SHRT_MIN
// should hold in wxDateTime_t
397 // flags for GetWeekDayName and GetMonthName
400 Name_Full
= 0x01, // return full name
401 Name_Abbr
= 0x02 // return abbreviated name
404 // flags for GetWeekOfYear and GetWeekOfMonth
407 Default_First
, // Sunday_First for US, Monday_First for the rest
408 Monday_First
, // week starts with a Monday
409 Sunday_First
// week starts with a Sunday
413 // ------------------------------------------------------------------------
415 // a class representing a time zone: basicly, this is just an offset
416 // (in seconds) from GMT
417 class WXDLLIMPEXP_BASE TimeZone
422 // create time zone object with the given offset
423 TimeZone(long offset
= 0) { m_offset
= offset
; }
425 static TimeZone
Make(long offset
)
428 tz
.m_offset
= offset
;
432 long GetOffset() const { return m_offset
; }
435 // offset for this timezone from GMT in seconds
439 // standard struct tm is limited to the years from 1900 (because
440 // tm_year field is the offset from 1900), so we use our own struct
441 // instead to represent broken down time
443 // NB: this struct should always be kept normalized (i.e. mon should
444 // be < 12, 1 <= day <= 31 &c), so use AddMonths(), AddDays()
445 // instead of modifying the member fields directly!
446 struct WXDLLIMPEXP_BASE Tm
448 wxDateTime_t msec
, sec
, min
, hour
, mday
;
452 // default ctor inits the object to an invalid value
455 // ctor from struct tm and the timezone
456 Tm(const struct tm
& tm
, const TimeZone
& tz
);
458 // check that the given date/time is valid (in Gregorian calendar)
459 bool IsValid() const;
462 WeekDay
GetWeekDay() // not const because wday may be changed
464 if ( wday
== Inv_WeekDay
)
467 return (WeekDay
)wday
;
470 // add the given number of months to the date keeping it normalized
471 void AddMonths(int monDiff
);
473 // add the given number of months to the date keeping it normalized
474 void AddDays(int dayDiff
);
477 // compute the weekday from other fields
478 void ComputeWeekDay();
480 // the timezone we correspond to
483 // these values can't be accessed directly because they're not always
484 // computed and we calculate them on demand
485 wxDateTime_t wday
, yday
;
489 // ------------------------------------------------------------------------
491 // set the current country
492 static void SetCountry(Country country
);
493 // get the current country
494 static Country
GetCountry();
496 // return true if the country is a West European one (in practice,
497 // this means that the same DST rules as for EEC apply)
498 static bool IsWestEuropeanCountry(Country country
= Country_Default
);
500 // return the current year
501 static int GetCurrentYear(Calendar cal
= Gregorian
);
503 // convert the year as returned by wxDateTime::GetYear() to a year
504 // suitable for BC/AD notation. The difference is that BC year 1
505 // corresponds to the year 0 (while BC year 0 didn't exist) and AD
506 // year N is just year N.
507 static int ConvertYearToBC(int year
);
509 // return the current month
510 static Month
GetCurrentMonth(Calendar cal
= Gregorian
);
512 // returns true if the given year is a leap year in the given calendar
513 static bool IsLeapYear(int year
= Inv_Year
, Calendar cal
= Gregorian
);
515 // get the century (19 for 1999, 20 for 2000 and -5 for 492 BC)
516 static int GetCentury(int year
);
518 // returns the number of days in this year (356 or 355 for Gregorian
519 // calendar usually :-)
520 static wxDateTime_t
GetNumberOfDays(int year
, Calendar cal
= Gregorian
);
522 // get the number of the days in the given month (default value for
523 // the year means the current one)
524 static wxDateTime_t
GetNumberOfDays(Month month
,
526 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 full (default) or abbreviated weekday name in the current
534 // locale, returns empty string on error
535 static wxString
GetWeekDayName(WeekDay weekday
,
536 NameFlags flags
= Name_Full
);
538 // get the AM and PM strings in the current locale (may be empty)
539 static void GetAmPmStrings(wxString
*am
, wxString
*pm
);
541 // return true if the given country uses DST for this year
542 static bool IsDSTApplicable(int year
= Inv_Year
,
543 Country country
= Country_Default
);
545 // get the beginning of DST for this year, will return invalid object
546 // if no DST applicable in this year. The default value of the
547 // parameter means to take the current year.
548 static wxDateTime
GetBeginDST(int year
= Inv_Year
,
549 Country country
= Country_Default
);
550 // get the end of DST for this year, will return invalid object
551 // if no DST applicable in this year. The default value of the
552 // parameter means to take the current year.
553 static wxDateTime
GetEndDST(int year
= Inv_Year
,
554 Country country
= Country_Default
);
556 // return the wxDateTime object for the current time
557 static inline wxDateTime
Now();
559 // return the wxDateTime object for the current time with millisecond
560 // precision (if available on this platform)
561 static wxDateTime
UNow();
563 // return the wxDateTime object for today midnight: i.e. as Now() but
564 // with time set to 0
565 static inline wxDateTime
Today();
567 // constructors: you should test whether the constructor succeeded with
568 // IsValid() function. The values Inv_Month and Inv_Year for the
569 // parameters mean take current month and/or year values.
570 // ------------------------------------------------------------------------
572 // default ctor does not initialize the object, use Set()!
573 wxDateTime() { m_time
= wxLongLong(wxINT32_MIN
, 0); }
575 // from time_t: seconds since the Epoch 00:00:00 UTC, Jan 1, 1970)
576 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
577 // VA C++ confuses this with wxDateTime(double jdn) thinking it is a duplicate declaration
578 inline wxDateTime(time_t timet
);
580 // from broken down time/date (only for standard Unix range)
581 inline wxDateTime(const struct tm
& tm
);
582 // from broken down time/date (any range)
583 inline wxDateTime(const Tm
& tm
);
585 // from JDN (beware of rounding errors)
586 inline wxDateTime(double jdn
);
588 // from separate values for each component, date set to today
589 inline wxDateTime(wxDateTime_t hour
,
590 wxDateTime_t minute
= 0,
591 wxDateTime_t second
= 0,
592 wxDateTime_t millisec
= 0);
593 // from separate values for each component with explicit date
594 inline wxDateTime(wxDateTime_t day
, // day of the month
596 int year
= Inv_Year
, // 1999, not 99 please!
597 wxDateTime_t hour
= 0,
598 wxDateTime_t minute
= 0,
599 wxDateTime_t second
= 0,
600 wxDateTime_t millisec
= 0);
602 // default copy ctor ok
606 // assignment operators and Set() functions: all non const methods return
607 // the reference to this object. IsValid() should be used to test whether
608 // the function succeeded.
609 // ------------------------------------------------------------------------
611 // set to the current time
612 inline wxDateTime
& SetToCurrent();
614 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
615 // VA C++ confuses this with wxDateTime(double jdn) thinking it is a duplicate declaration
616 // set to given time_t value
617 inline wxDateTime
& Set(time_t timet
);
620 // set to given broken down time/date
621 wxDateTime
& Set(const struct tm
& tm
);
623 // set to given broken down time/date
624 inline wxDateTime
& Set(const Tm
& tm
);
626 // set to given JDN (beware of rounding errors)
627 wxDateTime
& Set(double jdn
);
629 // set to given time, date = today
630 wxDateTime
& Set(wxDateTime_t hour
,
631 wxDateTime_t minute
= 0,
632 wxDateTime_t second
= 0,
633 wxDateTime_t millisec
= 0);
635 // from separate values for each component with explicit date
636 // (defaults for month and year are the current values)
637 wxDateTime
& Set(wxDateTime_t day
,
639 int year
= Inv_Year
, // 1999, not 99 please!
640 wxDateTime_t hour
= 0,
641 wxDateTime_t minute
= 0,
642 wxDateTime_t second
= 0,
643 wxDateTime_t millisec
= 0);
645 // resets time to 00:00:00, doesn't change the date
646 wxDateTime
& ResetTime();
648 // get the date part of this object only, i.e. the object which has the
649 // same date as this one but time of 00:00:00
650 wxDateTime
GetDateOnly() const;
652 // the following functions don't change the values of the other
653 // fields, i.e. SetMinute() won't change either hour or seconds value
656 wxDateTime
& SetYear(int year
);
658 wxDateTime
& SetMonth(Month month
);
659 // set the day of the month
660 wxDateTime
& SetDay(wxDateTime_t day
);
662 wxDateTime
& SetHour(wxDateTime_t hour
);
664 wxDateTime
& SetMinute(wxDateTime_t minute
);
666 wxDateTime
& SetSecond(wxDateTime_t second
);
668 wxDateTime
& SetMillisecond(wxDateTime_t millisecond
);
670 // assignment operator from time_t
671 wxDateTime
& operator=(time_t timet
) { return Set(timet
); }
673 // assignment operator from broken down time/date
674 wxDateTime
& operator=(const struct tm
& tm
) { return Set(tm
); }
676 // assignment operator from broken down time/date
677 wxDateTime
& operator=(const Tm
& tm
) { return Set(tm
); }
679 // default assignment operator is ok
681 // calendar calculations (functions which set the date only leave the time
682 // unchanged, e.g. don't explictly zero it): SetXXX() functions modify the
683 // object itself, GetXXX() ones return a new object.
684 // ------------------------------------------------------------------------
686 // set to the given week day in the same week as this one
687 wxDateTime
& SetToWeekDayInSameWeek(WeekDay weekday
,
688 WeekFlags flags
= Monday_First
);
689 inline wxDateTime
GetWeekDayInSameWeek(WeekDay weekday
,
690 WeekFlags flags
= Monday_First
) const;
692 // set to the next week day following this one
693 wxDateTime
& SetToNextWeekDay(WeekDay weekday
);
694 inline wxDateTime
GetNextWeekDay(WeekDay weekday
) const;
696 // set to the previous week day before this one
697 wxDateTime
& SetToPrevWeekDay(WeekDay weekday
);
698 inline wxDateTime
GetPrevWeekDay(WeekDay weekday
) const;
700 // set to Nth occurence of given weekday in the given month of the
701 // given year (time is set to 0), return true on success and false on
702 // failure. n may be positive (1..5) or negative to count from the end
703 // of the month (see helper function SetToLastWeekDay())
704 bool SetToWeekDay(WeekDay weekday
,
706 Month month
= Inv_Month
,
707 int year
= Inv_Year
);
708 inline wxDateTime
GetWeekDay(WeekDay weekday
,
710 Month month
= Inv_Month
,
711 int year
= Inv_Year
) const;
713 // sets to the last weekday in the given month, year
714 inline bool SetToLastWeekDay(WeekDay weekday
,
715 Month month
= Inv_Month
,
716 int year
= Inv_Year
);
717 inline wxDateTime
GetLastWeekDay(WeekDay weekday
,
718 Month month
= Inv_Month
,
719 int year
= Inv_Year
);
721 #if WXWIN_COMPATIBILITY_2_6
722 // sets the date to the given day of the given week in the year,
723 // returns true on success and false if given date doesn't exist (e.g.
726 // these functions are badly defined as they're not the reverse of
727 // GetWeekOfYear(), use SetToTheWeekOfYear() instead
728 wxDEPRECATED( bool SetToTheWeek(wxDateTime_t numWeek
,
729 WeekDay weekday
= Mon
,
730 WeekFlags flags
= Monday_First
) );
731 wxDEPRECATED( wxDateTime
GetWeek(wxDateTime_t numWeek
,
732 WeekDay weekday
= Mon
,
733 WeekFlags flags
= Monday_First
) const );
734 #endif // WXWIN_COMPATIBILITY_2_6
736 // returns the date corresponding to the given week day of the given
737 // week (in ISO notation) of the specified year
738 static wxDateTime
SetToWeekOfYear(int year
,
739 wxDateTime_t numWeek
,
740 WeekDay weekday
= Mon
);
742 // sets the date to the last day of the given (or current) month or the
743 // given (or current) year
744 wxDateTime
& SetToLastMonthDay(Month month
= Inv_Month
,
745 int year
= Inv_Year
);
746 inline wxDateTime
GetLastMonthDay(Month month
= Inv_Month
,
747 int year
= Inv_Year
) const;
749 // sets to the given year day (1..365 or 366)
750 wxDateTime
& SetToYearDay(wxDateTime_t yday
);
751 inline wxDateTime
GetYearDay(wxDateTime_t yday
) const;
753 // The definitions below were taken verbatim from
755 // http://www.capecod.net/~pbaum/date/date0.htm
757 // (Peter Baum's home page)
759 // definition: The Julian Day Number, Julian Day, or JD of a
760 // particular instant of time is the number of days and fractions of a
761 // day since 12 hours Universal Time (Greenwich mean noon) on January
762 // 1 of the year -4712, where the year is given in the Julian
763 // proleptic calendar. The idea of using this reference date was
764 // originally proposed by Joseph Scalizer in 1582 to count years but
765 // it was modified by 19th century astronomers to count days. One
766 // could have equivalently defined the reference time to be noon of
767 // November 24, -4713 if were understood that Gregorian calendar rules
768 // were applied. Julian days are Julian Day Numbers and are not to be
769 // confused with Julian dates.
771 // definition: The Rata Die number is a date specified as the number
772 // of days relative to a base date of December 31 of the year 0. Thus
773 // January 1 of the year 1 is Rata Die day 1.
775 // get the Julian Day number (the fractional part specifies the time of
776 // the day, related to noon - beware of rounding errors!)
777 double GetJulianDayNumber() const;
778 double GetJDN() const { return GetJulianDayNumber(); }
780 // get the Modified Julian Day number: it is equal to JDN - 2400000.5
781 // and so integral MJDs correspond to the midnights (and not noons).
782 // MJD 0 is Nov 17, 1858
783 double GetModifiedJulianDayNumber() const { return GetJDN() - 2400000.5; }
784 double GetMJD() const { return GetModifiedJulianDayNumber(); }
786 // get the Rata Die number
787 double GetRataDie() const;
789 // TODO algorithms for calculating some important dates, such as
790 // religious holidays (Easter...) or moon/solar eclipses? Some
791 // algorithms can be found in the calendar FAQ
794 // Timezone stuff: a wxDateTime object constructed using given
795 // day/month/year/hour/min/sec values is interpreted as this moment in
796 // local time. Using the functions below, it may be converted to another
797 // time zone (e.g., the Unix epoch is wxDateTime(1, Jan, 1970).ToGMT()).
799 // These functions try to handle DST internally, but there is no magical
800 // way to know all rules for it in all countries in the world, so if the
801 // program can handle it itself (or doesn't want to handle it at all for
802 // whatever reason), the DST handling can be disabled with noDST.
803 // ------------------------------------------------------------------------
805 // transform to any given timezone
806 inline wxDateTime
ToTimezone(const TimeZone
& tz
, bool noDST
= false) const;
807 wxDateTime
& MakeTimezone(const TimeZone
& tz
, bool noDST
= false);
809 // interpret current value as being in another timezone and transform
811 inline wxDateTime
FromTimezone(const TimeZone
& tz
, bool noDST
= false) const;
812 wxDateTime
& MakeFromTimezone(const TimeZone
& tz
, bool noDST
= false);
814 // transform to/from GMT/UTC
815 wxDateTime
ToUTC(bool noDST
= false) const { return ToTimezone(UTC
, noDST
); }
816 wxDateTime
& MakeUTC(bool noDST
= false) { return MakeTimezone(UTC
, noDST
); }
818 wxDateTime
ToGMT(bool noDST
= false) const { return ToUTC(noDST
); }
819 wxDateTime
& MakeGMT(bool noDST
= false) { return MakeUTC(noDST
); }
821 wxDateTime
FromUTC(bool noDST
= false) const
822 { return FromTimezone(UTC
, noDST
); }
823 wxDateTime
& MakeFromUTC(bool noDST
= false)
824 { return MakeFromTimezone(UTC
, noDST
); }
826 // is daylight savings time in effect at this moment according to the
827 // rules of the specified country?
829 // Return value is > 0 if DST is in effect, 0 if it is not and -1 if
830 // the information is not available (this is compatible with ANSI C)
831 int IsDST(Country country
= Country_Default
) const;
834 // accessors: many of them take the timezone parameter which indicates the
835 // timezone for which to make the calculations and the default value means
836 // to do it for the current timezone of this machine (even if the function
837 // only operates with the date it's necessary because a date may wrap as
838 // result of timezone shift)
839 // ------------------------------------------------------------------------
841 // is the date valid?
842 inline bool IsValid() const { return m_time
!= wxInvalidDateTime
.m_time
; }
844 // get the broken down date/time representation in the given timezone
846 // If you wish to get several time components (day, month and year),
847 // consider getting the whole Tm strcuture first and retrieving the
848 // value from it - this is much more efficient
849 Tm
GetTm(const TimeZone
& tz
= Local
) const;
851 // get the number of seconds since the Unix epoch - returns (time_t)-1
852 // if the value is out of range
853 inline time_t GetTicks() const;
855 // get the century, same as GetCentury(GetYear())
856 int GetCentury(const TimeZone
& tz
= Local
) const
857 { return GetCentury(GetYear(tz
)); }
858 // get the year (returns Inv_Year if date is invalid)
859 int GetYear(const TimeZone
& tz
= Local
) const
860 { return GetTm(tz
).year
; }
861 // get the month (Inv_Month if date is invalid)
862 Month
GetMonth(const TimeZone
& tz
= Local
) const
863 { return (Month
)GetTm(tz
).mon
; }
864 // get the month day (in 1..31 range, 0 if date is invalid)
865 wxDateTime_t
GetDay(const TimeZone
& tz
= Local
) const
866 { return GetTm(tz
).mday
; }
867 // get the day of the week (Inv_WeekDay if date is invalid)
868 WeekDay
GetWeekDay(const TimeZone
& tz
= Local
) const
869 { return GetTm(tz
).GetWeekDay(); }
870 // get the hour of the day
871 wxDateTime_t
GetHour(const TimeZone
& tz
= Local
) const
872 { return GetTm(tz
).hour
; }
874 wxDateTime_t
GetMinute(const TimeZone
& tz
= Local
) const
875 { return GetTm(tz
).min
; }
877 wxDateTime_t
GetSecond(const TimeZone
& tz
= Local
) const
878 { return GetTm(tz
).sec
; }
880 wxDateTime_t
GetMillisecond(const TimeZone
& tz
= Local
) const
881 { return GetTm(tz
).msec
; }
883 // get the day since the year start (1..366, 0 if date is invalid)
884 wxDateTime_t
GetDayOfYear(const TimeZone
& tz
= Local
) const;
885 // get the week number since the year start (1..52 or 53, 0 if date is
887 wxDateTime_t
GetWeekOfYear(WeekFlags flags
= Monday_First
,
888 const TimeZone
& tz
= Local
) const;
889 // get the week number since the month start (1..5, 0 if date is
891 wxDateTime_t
GetWeekOfMonth(WeekFlags flags
= Monday_First
,
892 const TimeZone
& tz
= Local
) const;
894 // is this date a work day? This depends on a country, of course,
895 // because the holidays are different in different countries
896 bool IsWorkDay(Country country
= Country_Default
) const;
898 // is this date later than Gregorian calendar introduction for the
899 // given country (see enum GregorianAdoption)?
901 // NB: this function shouldn't be considered as absolute authority in
902 // the matter. Besides, for some countries the exact date of
903 // adoption of the Gregorian calendar is simply unknown.
904 bool IsGregorianDate(GregorianAdoption country
= Gr_Standard
) const;
906 // dos date and time format
907 // ------------------------------------------------------------------------
909 // set from the DOS packed format
910 wxDateTime
& SetFromDOS(unsigned long ddt
);
912 // pack the date in DOS format
913 unsigned long GetAsDOS() const;
915 // comparison (see also functions below for operator versions)
916 // ------------------------------------------------------------------------
918 // returns true if the two moments are strictly identical
919 inline bool IsEqualTo(const wxDateTime
& datetime
) const;
921 // returns true if the date is strictly earlier than the given one
922 inline bool IsEarlierThan(const wxDateTime
& datetime
) const;
924 // returns true if the date is strictly later than the given one
925 inline bool IsLaterThan(const wxDateTime
& datetime
) const;
927 // returns true if the date is strictly in the given range
928 inline bool IsStrictlyBetween(const wxDateTime
& t1
,
929 const wxDateTime
& t2
) const;
931 // returns true if the date is in the given range
932 inline bool IsBetween(const wxDateTime
& t1
, const wxDateTime
& t2
) const;
934 // do these two objects refer to the same date?
935 inline bool IsSameDate(const wxDateTime
& dt
) const;
937 // do these two objects have the same time?
938 inline bool IsSameTime(const wxDateTime
& dt
) const;
940 // are these two objects equal up to given timespan?
941 inline bool IsEqualUpTo(const wxDateTime
& dt
, const wxTimeSpan
& ts
) const;
943 inline bool operator<(const wxDateTime
& dt
) const
945 wxASSERT_MSG( IsValid() && dt
.IsValid(), _T("invalid wxDateTime") );
946 return GetValue() < dt
.GetValue();
949 inline bool operator<=(const wxDateTime
& dt
) const
951 wxASSERT_MSG( IsValid() && dt
.IsValid(), _T("invalid wxDateTime") );
952 return GetValue() <= dt
.GetValue();
955 inline bool operator>(const wxDateTime
& dt
) const
957 wxASSERT_MSG( IsValid() && dt
.IsValid(), _T("invalid wxDateTime") );
958 return GetValue() > dt
.GetValue();
961 inline bool operator>=(const wxDateTime
& dt
) const
963 wxASSERT_MSG( IsValid() && dt
.IsValid(), _T("invalid wxDateTime") );
964 return GetValue() >= dt
.GetValue();
967 inline bool operator==(const wxDateTime
& dt
) const
969 wxASSERT_MSG( IsValid() && dt
.IsValid(), _T("invalid wxDateTime") );
970 return GetValue() == dt
.GetValue();
973 inline bool operator!=(const wxDateTime
& dt
) const
975 wxASSERT_MSG( IsValid() && dt
.IsValid(), _T("invalid wxDateTime") );
976 return GetValue() != dt
.GetValue();
979 // arithmetics with dates (see also below for more operators)
980 // ------------------------------------------------------------------------
982 // return the sum of the date with a time span (positive or negative)
983 inline wxDateTime
Add(const wxTimeSpan
& diff
) const;
984 // add a time span (positive or negative)
985 inline wxDateTime
& Add(const wxTimeSpan
& diff
);
986 // add a time span (positive or negative)
987 inline wxDateTime
& operator+=(const wxTimeSpan
& diff
);
988 inline wxDateTime
operator+(const wxTimeSpan
& ts
) const
990 wxDateTime
dt(*this);
995 // return the difference of the date with a time span
996 inline wxDateTime
Subtract(const wxTimeSpan
& diff
) const;
997 // subtract a time span (positive or negative)
998 inline wxDateTime
& Subtract(const wxTimeSpan
& diff
);
999 // subtract a time span (positive or negative)
1000 inline wxDateTime
& operator-=(const wxTimeSpan
& diff
);
1001 inline wxDateTime
operator-(const wxTimeSpan
& ts
) const
1003 wxDateTime
dt(*this);
1008 // return the sum of the date with a date span
1009 inline wxDateTime
Add(const wxDateSpan
& diff
) const;
1010 // add a date span (positive or negative)
1011 wxDateTime
& Add(const wxDateSpan
& diff
);
1012 // add a date span (positive or negative)
1013 inline wxDateTime
& operator+=(const wxDateSpan
& diff
);
1014 inline wxDateTime
operator+(const wxDateSpan
& ds
) const
1016 wxDateTime
dt(*this);
1021 // return the difference of the date with a date span
1022 inline wxDateTime
Subtract(const wxDateSpan
& diff
) const;
1023 // subtract a date span (positive or negative)
1024 inline wxDateTime
& Subtract(const wxDateSpan
& diff
);
1025 // subtract a date span (positive or negative)
1026 inline wxDateTime
& operator-=(const wxDateSpan
& diff
);
1027 inline wxDateTime
operator-(const wxDateSpan
& ds
) const
1029 wxDateTime
dt(*this);
1034 // return the difference between two dates
1035 inline wxTimeSpan
Subtract(const wxDateTime
& dt
) const;
1036 inline wxTimeSpan
operator-(const wxDateTime
& dt2
) const;
1038 // conversion to/from text: all conversions from text return the pointer to
1039 // the next character following the date specification (i.e. the one where
1040 // the scan had to stop) or NULL on failure; for the versions taking
1041 // wxString or wxCStrData, we don't know if the user code needs char* or
1042 // wchar_t* pointer and so we return char* one for compatibility with the
1043 // existing ANSI code and also return iterator in another output parameter
1044 // (it will be equal to end if the entire string was parsed)
1045 // ------------------------------------------------------------------------
1047 // parse a string in RFC 822 format (found e.g. in mail headers and
1048 // having the form "Wed, 10 Feb 1999 19:07:07 +0100")
1049 const char *ParseRfc822Date(const wxString
& date
,
1050 wxString::const_iterator
*end
= NULL
);
1051 const char *ParseRfc822Date(const wxCStrData
& date
,
1052 wxString::const_iterator
*end
= NULL
)
1054 return ParseRfc822Date(date
.AsString(), end
);
1057 const wchar_t *ParseRfc822Date(const wchar_t* date
)
1059 return ReturnEndAsWidePtr(&wxDateTime::ParseRfc822Date
, date
);
1062 const char *ParseRfc822Date(const char* date
)
1064 return ParseRfc822Date(wxString(date
));
1067 // parse a date/time in the given format (see strptime(3)), fill in
1068 // the missing (in the string) fields with the values of dateDef (by
1069 // default, they will not change if they had valid values or will
1070 // default to Today() otherwise)
1071 const char *ParseFormat(const wxString
& date
,
1072 const wxString
& format
= wxDefaultDateTimeFormat
,
1073 const wxDateTime
& dateDef
= wxDefaultDateTime
,
1074 wxString::const_iterator
*end
= NULL
);
1076 const char *ParseFormat(const wxString
& date
,
1077 const char *format
= wxDefaultDateTimeFormat
,
1078 const wxDateTime
& dateDef
= wxDefaultDateTime
,
1079 wxString::const_iterator
*end
= NULL
)
1081 return ParseFormat(date
, wxString(format
), dateDef
, end
);
1084 const char *ParseFormat(const wxString
& date
,
1085 const wxString
& format
= wxDefaultDateTimeFormat
,
1086 wxString::const_iterator
*end
= NULL
)
1088 return ParseFormat(date
, format
, wxDefaultDateTime
, end
);
1091 const char *ParseFormat(const wxCStrData
& date
,
1092 const wxString
& format
= wxDefaultDateTimeFormat
,
1093 const wxDateTime
& dateDef
= wxDefaultDateTime
,
1094 wxString::const_iterator
*end
= NULL
)
1096 return ParseFormat(date
.AsString(), format
, dateDef
, end
);
1099 const wchar_t *ParseFormat(const wchar_t *date
,
1100 const wxString
& format
= wxDefaultDateTimeFormat
,
1101 const wxDateTime
& dateDef
= wxDefaultDateTime
)
1103 const wxString
datestr(date
);
1104 wxString::const_iterator end
;
1105 if ( !ParseFormat(datestr
, format
, dateDef
, &end
) )
1108 return date
+ (end
- datestr
.begin());
1111 const char *ParseFormat(const char *date
,
1112 const wxString
& format
= "%c",
1113 const wxDateTime
& dateDef
= wxDefaultDateTime
)
1115 return ParseFormat(wxString(date
), format
, dateDef
);
1118 const char *ParseFormat(const char *date
,
1119 const char *format
= wxDefaultDateTimeFormat
,
1120 const wxDateTime
& dateDef
= wxDefaultDateTime
)
1122 return ParseFormat(wxString(date
), wxString(format
), dateDef
);
1125 // parse a string containing date, time or both in ISO 8601 format
1127 // notice that these functions are new in wx 3.0 and so we don't
1128 // provide compatibility overloads for them
1129 bool ParseISODate(const wxString
& date
)
1131 wxString::const_iterator end
;
1132 return ParseFormat(date
, wxS("%Y-%m-%d"), &end
) && end
== date
.end();
1135 bool ParseISOTime(const wxString
& time
)
1137 wxString::const_iterator end
;
1138 return ParseFormat(time
, wxS("%H:%M:%S"), &end
) && end
== time
.end();
1141 bool ParseISOCombined(const wxString
& datetime
, char sep
= 'T')
1143 wxString::const_iterator end
;
1144 const wxString fmt
= wxS("%Y-%m-%d") + wxString(sep
) + wxS("%H:%M:%S");
1145 return ParseFormat(datetime
, fmt
, &end
) && end
== datetime
.end();
1148 // parse a string containing the date/time in "free" format, this
1149 // function will try to make an educated guess at the string contents
1150 const char *ParseDateTime(const wxString
& datetime
,
1151 wxString::const_iterator
*end
= NULL
);
1153 const char *ParseDateTime(const wxCStrData
& datetime
,
1154 wxString::const_iterator
*end
= NULL
)
1156 return ParseDateTime(datetime
.AsString(), end
);
1159 const wchar_t *ParseDateTime(const wchar_t *datetime
)
1161 return ReturnEndAsWidePtr(&wxDateTime::ParseDateTime
, datetime
);
1164 const char *ParseDateTime(const char *datetime
)
1166 return ParseDateTime(wxString(datetime
));
1169 // parse a string containing the date only in "free" format (less
1170 // flexible than ParseDateTime)
1171 const char *ParseDate(const wxString
& date
,
1172 wxString::const_iterator
*end
= NULL
);
1174 const char *ParseDate(const wxCStrData
& date
,
1175 wxString::const_iterator
*end
= NULL
)
1177 return ParseDate(date
.AsString(), end
);
1180 const wchar_t *ParseDate(const wchar_t *date
)
1182 return ReturnEndAsWidePtr(&wxDateTime::ParseDate
, date
);
1185 const char *ParseDate(const char *date
)
1187 return ParseDate(wxString(date
));
1190 // parse a string containing the time only in "free" format
1191 const char *ParseTime(const wxString
& time
,
1192 wxString::const_iterator
*end
= NULL
);
1194 const char *ParseTime(const wxCStrData
& time
,
1195 wxString::const_iterator
*end
= NULL
)
1197 return ParseTime(time
.AsString(), end
);
1200 const wchar_t *ParseTime(const wchar_t *time
)
1202 return ReturnEndAsWidePtr(&wxDateTime::ParseTime
, time
);
1205 const char *ParseTime(const char *time
)
1207 return ParseTime(wxString(time
));
1210 // this function accepts strftime()-like format string (default
1211 // argument corresponds to the preferred date and time representation
1212 // for the current locale) and returns the string containing the
1213 // resulting text representation
1214 wxString
Format(const wxString
& format
= wxDefaultDateTimeFormat
,
1215 const TimeZone
& tz
= Local
) const;
1216 // preferred date representation for the current locale
1217 wxString
FormatDate() const { return Format(wxS("%x")); }
1218 // preferred time representation for the current locale
1219 wxString
FormatTime() const { return Format(wxS("%X")); }
1220 // returns the string representing the date in ISO 8601 format
1222 wxString
FormatISODate() const { return Format(wxS("%Y-%m-%d")); }
1223 // returns the string representing the time in ISO 8601 format
1225 wxString
FormatISOTime() const { return Format(wxS("%H:%M:%S")); }
1226 // return the combined date time representation in ISO 8601 format; the
1227 // separator character should be 'T' according to the standard but it
1228 // can also be useful to set it to ' '
1229 wxString
FormatISOCombined(char sep
= 'T') const
1230 { return FormatISODate() + sep
+ FormatISOTime(); }
1233 // ------------------------------------------------------------------------
1235 // construct from internal representation
1236 wxDateTime(const wxLongLong
& time
) { m_time
= time
; }
1238 // get the internal representation
1239 inline wxLongLong
GetValue() const;
1241 // a helper function to get the current time_t
1242 static time_t GetTimeNow() { return time((time_t *)NULL
); }
1244 // another one to get the current time broken down
1245 static struct tm
*GetTmNow()
1247 static struct tm l_CurrentTime
;
1248 return GetTmNow(&l_CurrentTime
);
1251 // get current time using thread-safe function
1252 static struct tm
*GetTmNow(struct tm
*tmstruct
);
1255 // helper function for defining backward-compatible wrappers for code
1256 // using wchar_t* pointer instead of wxString iterators
1258 const char *(wxDateTime::*StringMethod
)(const wxString
& s
,
1259 wxString::const_iterator
*end
);
1261 const wchar_t *ReturnEndAsWidePtr(StringMethod func
, const wchar_t *p
)
1263 const wxString
s(p
);
1264 wxString::const_iterator end
;
1265 if ( !(this->*func
)(s
, &end
) )
1268 return p
+ (end
- s
.begin());
1272 // the current country - as it's the same for all program objects (unless
1273 // it runs on a _really_ big cluster system :-), this is a static member:
1274 // see SetCountry() and GetCountry()
1275 static Country ms_country
;
1277 // this constant is used to transform a time_t value to the internal
1278 // representation, as time_t is in seconds and we use milliseconds it's
1280 static const long TIME_T_FACTOR
;
1282 // returns true if we fall in range in which we can use standard ANSI C
1284 inline bool IsInStdRange() const;
1286 // the internal representation of the time is the amount of milliseconds
1287 // elapsed since the origin which is set by convention to the UNIX/C epoch
1288 // value: the midnight of January 1, 1970 (UTC)
1292 // ----------------------------------------------------------------------------
1293 // This class contains a difference between 2 wxDateTime values, so it makes
1294 // sense to add it to wxDateTime and it is the result of subtraction of 2
1295 // objects of that class. See also wxDateSpan.
1296 // ----------------------------------------------------------------------------
1298 class WXDLLIMPEXP_BASE wxTimeSpan
1302 // ------------------------------------------------------------------------
1304 // return the timespan for the given number of milliseconds
1305 static wxTimeSpan
Milliseconds(wxLongLong ms
) { return wxTimeSpan(0, 0, 0, ms
); }
1306 static wxTimeSpan
Millisecond() { return Milliseconds(1); }
1308 // return the timespan for the given number of seconds
1309 static wxTimeSpan
Seconds(wxLongLong sec
) { return wxTimeSpan(0, 0, sec
); }
1310 static wxTimeSpan
Second() { return Seconds(1); }
1312 // return the timespan for the given number of minutes
1313 static wxTimeSpan
Minutes(long min
) { return wxTimeSpan(0, min
, 0 ); }
1314 static wxTimeSpan
Minute() { return Minutes(1); }
1316 // return the timespan for the given number of hours
1317 static wxTimeSpan
Hours(long hours
) { return wxTimeSpan(hours
, 0, 0); }
1318 static wxTimeSpan
Hour() { return Hours(1); }
1320 // return the timespan for the given number of days
1321 static wxTimeSpan
Days(long days
) { return Hours(24 * days
); }
1322 static wxTimeSpan
Day() { return Days(1); }
1324 // return the timespan for the given number of weeks
1325 static wxTimeSpan
Weeks(long days
) { return Days(7 * days
); }
1326 static wxTimeSpan
Week() { return Weeks(1); }
1328 // default ctor constructs the 0 time span
1331 // from separate values for each component, date set to 0 (hours are
1332 // not restricted to 0..24 range, neither are minutes, seconds or
1334 inline wxTimeSpan(long hours
,
1336 wxLongLong seconds
= 0,
1337 wxLongLong milliseconds
= 0);
1339 // default copy ctor is ok
1343 // arithmetics with time spans (see also below for more operators)
1344 // ------------------------------------------------------------------------
1346 // return the sum of two timespans
1347 inline wxTimeSpan
Add(const wxTimeSpan
& diff
) const;
1348 // add two timespans together
1349 inline wxTimeSpan
& Add(const wxTimeSpan
& diff
);
1350 // add two timespans together
1351 wxTimeSpan
& operator+=(const wxTimeSpan
& diff
) { return Add(diff
); }
1352 inline wxTimeSpan
operator+(const wxTimeSpan
& ts
) const
1354 return wxTimeSpan(GetValue() + ts
.GetValue());
1357 // return the difference of two timespans
1358 inline wxTimeSpan
Subtract(const wxTimeSpan
& diff
) const;
1359 // subtract another timespan
1360 inline wxTimeSpan
& Subtract(const wxTimeSpan
& diff
);
1361 // subtract another timespan
1362 wxTimeSpan
& operator-=(const wxTimeSpan
& diff
) { return Subtract(diff
); }
1363 inline wxTimeSpan
operator-(const wxTimeSpan
& ts
)
1365 return wxTimeSpan(GetValue() - ts
.GetValue());
1368 // multiply timespan by a scalar
1369 inline wxTimeSpan
Multiply(int n
) const;
1370 // multiply timespan by a scalar
1371 inline wxTimeSpan
& Multiply(int n
);
1372 // multiply timespan by a scalar
1373 wxTimeSpan
& operator*=(int n
) { return Multiply(n
); }
1374 inline wxTimeSpan
operator*(int n
) const
1376 return wxTimeSpan(*this).Multiply(n
);
1379 // return this timespan with opposite sign
1380 wxTimeSpan
Negate() const { return wxTimeSpan(-GetValue()); }
1381 // negate the value of the timespan
1382 wxTimeSpan
& Neg() { m_diff
= -GetValue(); return *this; }
1383 // negate the value of the timespan
1384 wxTimeSpan
& operator-() { return Neg(); }
1386 // return the absolute value of the timespan: does _not_ modify the
1388 inline wxTimeSpan
Abs() const;
1390 // there is intentionally no division because we don't want to
1391 // introduce rounding errors in time calculations
1393 // comparaison (see also operator versions below)
1394 // ------------------------------------------------------------------------
1396 // is the timespan null?
1397 bool IsNull() const { return m_diff
== 0l; }
1398 // returns true if the timespan is null
1399 bool operator!() const { return !IsNull(); }
1401 // is the timespan positive?
1402 bool IsPositive() const { return m_diff
> 0l; }
1404 // is the timespan negative?
1405 bool IsNegative() const { return m_diff
< 0l; }
1407 // are two timespans equal?
1408 inline bool IsEqualTo(const wxTimeSpan
& ts
) const;
1409 // compare two timestamps: works with the absolute values, i.e. -2
1410 // hours is longer than 1 hour. Also, it will return false if the
1411 // timespans are equal in absolute value.
1412 inline bool IsLongerThan(const wxTimeSpan
& ts
) const;
1413 // compare two timestamps: works with the absolute values, i.e. 1
1414 // hour is shorter than -2 hours. Also, it will return false if the
1415 // timespans are equal in absolute value.
1416 bool IsShorterThan(const wxTimeSpan
& t
) const { return !IsLongerThan(t
); }
1418 inline bool operator<(const wxTimeSpan
&ts
) const
1420 return GetValue() < ts
.GetValue();
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 // breaking into days, hours, minutes and seconds
1449 // ------------------------------------------------------------------------
1451 // get the max number of weeks in this timespan
1452 inline int GetWeeks() const;
1453 // get the max number of days in this timespan
1454 inline int GetDays() const;
1455 // get the max number of hours in this timespan
1456 inline int GetHours() const;
1457 // get the max number of minutes in this timespan
1458 inline int GetMinutes() const;
1459 // get the max number of seconds in this timespan
1460 inline wxLongLong
GetSeconds() const;
1461 // get the number of milliseconds in this timespan
1462 wxLongLong
GetMilliseconds() const { return m_diff
; }
1464 // conversion to text
1465 // ------------------------------------------------------------------------
1467 // this function accepts strftime()-like format string (default
1468 // argument corresponds to the preferred date and time representation
1469 // for the current locale) and returns the string containing the
1470 // resulting text representation. Notice that only some of format
1471 // specifiers valid for wxDateTime are valid for wxTimeSpan: hours,
1472 // minutes and seconds make sense, but not "PM/AM" string for example.
1473 wxString
Format(const wxString
& format
= wxDefaultTimeSpanFormat
) const;
1476 // ------------------------------------------------------------------------
1478 // construct from internal representation
1479 wxTimeSpan(const wxLongLong
& diff
) { m_diff
= diff
; }
1481 // get the internal representation
1482 wxLongLong
GetValue() const { return m_diff
; }
1485 // the (signed) time span in milliseconds
1489 // ----------------------------------------------------------------------------
1490 // This class is a "logical time span" and is useful for implementing program
1491 // logic for such things as "add one month to the date" which, in general,
1492 // doesn't mean to add 60*60*24*31 seconds to it, but to take the same date
1493 // the next month (to understand that this is indeed different consider adding
1494 // one month to Feb, 15 - we want to get Mar, 15, of course).
1496 // When adding a month to the date, all lesser components (days, hours, ...)
1497 // won't be changed unless the resulting date would be invalid: for example,
1498 // Jan 31 + 1 month will be Feb 28, not (non existing) Feb 31.
1500 // Because of this feature, adding and subtracting back again the same
1501 // wxDateSpan will *not*, in general give back the original date: Feb 28 - 1
1502 // month will be Jan 28, not Jan 31!
1504 // wxDateSpan can be either positive or negative. They may be
1505 // multiplied by scalars which multiply all deltas by the scalar: i.e. 2*(1
1506 // month and 1 day) is 2 months and 2 days. They can be added together and
1507 // with wxDateTime or wxTimeSpan, but the type of result is different for each
1510 // Beware about weeks: if you specify both weeks and days, the total number of
1511 // days added will be 7*weeks + days! See also GetTotalDays() function.
1513 // Equality operators are defined for wxDateSpans. Two datespans are equal if
1514 // they both give the same target date when added to *every* source date.
1515 // Thus wxDateSpan::Months(1) is not equal to wxDateSpan::Days(30), because
1516 // they not give the same date when added to 1 Feb. But wxDateSpan::Days(14) is
1517 // equal to wxDateSpan::Weeks(2)
1519 // Finally, notice that for adding hours, minutes &c you don't need this
1520 // class: wxTimeSpan will do the job because there are no subtleties
1521 // associated with those.
1522 // ----------------------------------------------------------------------------
1524 class WXDLLIMPEXP_BASE wxDateSpan
1528 // ------------------------------------------------------------------------
1530 // this many years/months/weeks/days
1531 wxDateSpan(int years
= 0, int months
= 0, int weeks
= 0, int days
= 0)
1539 // get an object for the given number of days
1540 static wxDateSpan
Days(int days
) { return wxDateSpan(0, 0, 0, days
); }
1541 static wxDateSpan
Day() { return Days(1); }
1543 // get an object for the given number of weeks
1544 static wxDateSpan
Weeks(int weeks
) { return wxDateSpan(0, 0, weeks
, 0); }
1545 static wxDateSpan
Week() { return Weeks(1); }
1547 // get an object for the given number of months
1548 static wxDateSpan
Months(int mon
) { return wxDateSpan(0, mon
, 0, 0); }
1549 static wxDateSpan
Month() { return Months(1); }
1551 // get an object for the given number of years
1552 static wxDateSpan
Years(int years
) { return wxDateSpan(years
, 0, 0, 0); }
1553 static wxDateSpan
Year() { return Years(1); }
1555 // default copy ctor is ok
1559 // accessors (all SetXXX() return the (modified) wxDateSpan object)
1560 // ------------------------------------------------------------------------
1562 // set number of years
1563 wxDateSpan
& SetYears(int n
) { m_years
= n
; return *this; }
1564 // set number of months
1565 wxDateSpan
& SetMonths(int n
) { m_months
= n
; return *this; }
1566 // set number of weeks
1567 wxDateSpan
& SetWeeks(int n
) { m_weeks
= n
; return *this; }
1568 // set number of days
1569 wxDateSpan
& SetDays(int n
) { m_days
= n
; return *this; }
1571 // get number of years
1572 int GetYears() const { return m_years
; }
1573 // get number of months
1574 int GetMonths() const { return m_months
; }
1575 // get number of weeks
1576 int GetWeeks() const { return m_weeks
; }
1577 // get number of days
1578 int GetDays() const { return m_days
; }
1579 // returns 7*GetWeeks() + GetDays()
1580 int GetTotalDays() const { return 7*m_weeks
+ m_days
; }
1582 // arithmetics with date spans (see also below for more operators)
1583 // ------------------------------------------------------------------------
1585 // return sum of two date spans
1586 inline wxDateSpan
Add(const wxDateSpan
& other
) const;
1587 // add another wxDateSpan to us
1588 inline wxDateSpan
& Add(const wxDateSpan
& other
);
1589 // add another wxDateSpan to us
1590 inline wxDateSpan
& operator+=(const wxDateSpan
& other
);
1591 inline wxDateSpan
operator+(const wxDateSpan
& ds
) const
1593 return wxDateSpan(GetYears() + ds
.GetYears(),
1594 GetMonths() + ds
.GetMonths(),
1595 GetWeeks() + ds
.GetWeeks(),
1596 GetDays() + ds
.GetDays());
1599 // return difference of two date spans
1600 inline wxDateSpan
Subtract(const wxDateSpan
& other
) const;
1601 // subtract another wxDateSpan from us
1602 inline wxDateSpan
& Subtract(const wxDateSpan
& other
);
1603 // subtract another wxDateSpan from us
1604 inline wxDateSpan
& operator-=(const wxDateSpan
& other
);
1605 inline wxDateSpan
operator-(const wxDateSpan
& ds
) const
1607 return wxDateSpan(GetYears() - ds
.GetYears(),
1608 GetMonths() - ds
.GetMonths(),
1609 GetWeeks() - ds
.GetWeeks(),
1610 GetDays() - ds
.GetDays());
1613 // return a copy of this time span with changed sign
1614 inline wxDateSpan
Negate() const;
1615 // inverse the sign of this timespan
1616 inline wxDateSpan
& Neg();
1617 // inverse the sign of this timespan
1618 wxDateSpan
& operator-() { return Neg(); }
1620 // return the date span proportional to this one with given factor
1621 inline wxDateSpan
Multiply(int factor
) const;
1622 // multiply all components by a (signed) number
1623 inline wxDateSpan
& Multiply(int factor
);
1624 // multiply all components by a (signed) number
1625 inline wxDateSpan
& operator*=(int factor
) { return Multiply(factor
); }
1626 inline wxDateSpan
operator*(int n
) const
1628 return wxDateSpan(*this).Multiply(n
);
1631 // ds1 == d2 if and only if for every wxDateTime t t + ds1 == t + ds2
1632 inline bool operator==(const wxDateSpan
& ds
) const
1634 return GetYears() == ds
.GetYears() &&
1635 GetMonths() == ds
.GetMonths() &&
1636 GetTotalDays() == ds
.GetTotalDays();
1639 inline bool operator!=(const wxDateSpan
& ds
) const
1641 return !(*this == ds
);
1651 // ----------------------------------------------------------------------------
1652 // wxDateTimeArray: array of dates.
1653 // ----------------------------------------------------------------------------
1655 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxDateTime
, wxDateTimeArray
, WXDLLIMPEXP_BASE
);
1657 // ----------------------------------------------------------------------------
1658 // wxDateTimeHolidayAuthority: an object of this class will decide whether a
1659 // given date is a holiday and is used by all functions working with "work
1662 // NB: the base class is an ABC, derived classes must implement the pure
1663 // virtual methods to work with the holidays they correspond to.
1664 // ----------------------------------------------------------------------------
1666 class WXDLLIMPEXP_FWD_BASE wxDateTimeHolidayAuthority
;
1667 WX_DEFINE_USER_EXPORTED_ARRAY_PTR(wxDateTimeHolidayAuthority
*,
1668 wxHolidayAuthoritiesArray
,
1669 class WXDLLIMPEXP_BASE
);
1671 class wxDateTimeHolidaysModule
;
1672 class WXDLLIMPEXP_BASE wxDateTimeHolidayAuthority
1674 friend class wxDateTimeHolidaysModule
;
1676 // returns true if the given date is a holiday
1677 static bool IsHoliday(const wxDateTime
& dt
);
1679 // fills the provided array with all holidays in the given range, returns
1680 // the number of them
1681 static size_t GetHolidaysInRange(const wxDateTime
& dtStart
,
1682 const wxDateTime
& dtEnd
,
1683 wxDateTimeArray
& holidays
);
1685 // clear the list of holiday authorities
1686 static void ClearAllAuthorities();
1688 // add a new holiday authority (the pointer will be deleted by
1689 // wxDateTimeHolidayAuthority)
1690 static void AddAuthority(wxDateTimeHolidayAuthority
*auth
);
1692 // the base class must have a virtual dtor
1693 virtual ~wxDateTimeHolidayAuthority();
1696 // this function is called to determine whether a given day is a holiday
1697 virtual bool DoIsHoliday(const wxDateTime
& dt
) const = 0;
1699 // this function should fill the array with all holidays between the two
1700 // given dates - it is implemented in the base class, but in a very
1701 // inefficient way (it just iterates over all days and uses IsHoliday() for
1702 // each of them), so it must be overridden in the derived class where the
1703 // base class version may be explicitly used if needed
1705 // returns the number of holidays in the given range and fills holidays
1707 virtual size_t DoGetHolidaysInRange(const wxDateTime
& dtStart
,
1708 const wxDateTime
& dtEnd
,
1709 wxDateTimeArray
& holidays
) const = 0;
1712 // all holiday authorities
1713 static wxHolidayAuthoritiesArray ms_authorities
;
1716 // the holidays for this class are all Saturdays and Sundays
1717 class WXDLLIMPEXP_BASE wxDateTimeWorkDays
: public wxDateTimeHolidayAuthority
1720 virtual bool DoIsHoliday(const wxDateTime
& dt
) const;
1721 virtual size_t DoGetHolidaysInRange(const wxDateTime
& dtStart
,
1722 const wxDateTime
& dtEnd
,
1723 wxDateTimeArray
& holidays
) const;
1726 // ============================================================================
1727 // inline functions implementation
1728 // ============================================================================
1730 // ----------------------------------------------------------------------------
1732 // ----------------------------------------------------------------------------
1734 #define MILLISECONDS_PER_DAY 86400000l
1736 // some broken compilers (HP-UX CC) refuse to compile the "normal" version, but
1737 // using a temp variable always might prevent other compilers from optimising
1738 // it away - hence use of this ugly macro
1740 #define MODIFY_AND_RETURN(op) return wxDateTime(*this).op
1742 #define MODIFY_AND_RETURN(op) wxDateTime dt(*this); dt.op; return dt
1745 // ----------------------------------------------------------------------------
1746 // wxDateTime construction
1747 // ----------------------------------------------------------------------------
1749 inline bool wxDateTime::IsInStdRange() const
1751 return m_time
>= 0l && (m_time
/ TIME_T_FACTOR
) < LONG_MAX
;
1755 inline wxDateTime
wxDateTime::Now()
1758 return wxDateTime(*GetTmNow(&tmstruct
));
1762 inline wxDateTime
wxDateTime::Today()
1764 wxDateTime
dt(Now());
1770 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
1771 inline wxDateTime
& wxDateTime::Set(time_t timet
)
1773 // assign first to avoid long multiplication overflow!
1774 m_time
= timet
- WX_TIME_BASE_OFFSET
;
1775 m_time
*= TIME_T_FACTOR
;
1781 inline wxDateTime
& wxDateTime::SetToCurrent()
1787 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
1788 inline wxDateTime::wxDateTime(time_t timet
)
1794 inline wxDateTime::wxDateTime(const struct tm
& tm
)
1799 inline wxDateTime::wxDateTime(const Tm
& tm
)
1804 inline wxDateTime::wxDateTime(double jdn
)
1809 inline wxDateTime
& wxDateTime::Set(const Tm
& tm
)
1811 wxASSERT_MSG( tm
.IsValid(), _T("invalid broken down date/time") );
1813 return Set(tm
.mday
, (Month
)tm
.mon
, tm
.year
,
1814 tm
.hour
, tm
.min
, tm
.sec
, tm
.msec
);
1817 inline wxDateTime::wxDateTime(wxDateTime_t hour
,
1818 wxDateTime_t minute
,
1819 wxDateTime_t second
,
1820 wxDateTime_t millisec
)
1822 Set(hour
, minute
, second
, millisec
);
1825 inline wxDateTime::wxDateTime(wxDateTime_t day
,
1829 wxDateTime_t minute
,
1830 wxDateTime_t second
,
1831 wxDateTime_t millisec
)
1833 Set(day
, month
, year
, hour
, minute
, second
, millisec
);
1836 // ----------------------------------------------------------------------------
1837 // wxDateTime accessors
1838 // ----------------------------------------------------------------------------
1840 inline wxLongLong
wxDateTime::GetValue() const
1842 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1847 inline time_t wxDateTime::GetTicks() const
1849 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1850 if ( !IsInStdRange() )
1855 return (time_t)((m_time
/ (long)TIME_T_FACTOR
).ToLong()) + WX_TIME_BASE_OFFSET
;
1858 inline bool wxDateTime::SetToLastWeekDay(WeekDay weekday
,
1862 return SetToWeekDay(weekday
, -1, month
, year
);
1866 wxDateTime::GetWeekDayInSameWeek(WeekDay weekday
,
1867 WeekFlags
WXUNUSED(flags
)) const
1869 MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday
) );
1872 inline wxDateTime
wxDateTime::GetNextWeekDay(WeekDay weekday
) const
1874 MODIFY_AND_RETURN( SetToNextWeekDay(weekday
) );
1877 inline wxDateTime
wxDateTime::GetPrevWeekDay(WeekDay weekday
) const
1879 MODIFY_AND_RETURN( SetToPrevWeekDay(weekday
) );
1882 inline wxDateTime
wxDateTime::GetWeekDay(WeekDay weekday
,
1887 wxDateTime
dt(*this);
1889 return dt
.SetToWeekDay(weekday
, n
, month
, year
) ? dt
: wxInvalidDateTime
;
1892 inline wxDateTime
wxDateTime::GetLastWeekDay(WeekDay weekday
,
1896 wxDateTime
dt(*this);
1898 return dt
.SetToLastWeekDay(weekday
, month
, year
) ? dt
: wxInvalidDateTime
;
1901 inline wxDateTime
wxDateTime::GetLastMonthDay(Month month
, int year
) const
1903 MODIFY_AND_RETURN( SetToLastMonthDay(month
, year
) );
1906 inline wxDateTime
wxDateTime::GetYearDay(wxDateTime_t yday
) const
1908 MODIFY_AND_RETURN( SetToYearDay(yday
) );
1911 // ----------------------------------------------------------------------------
1912 // wxDateTime comparison
1913 // ----------------------------------------------------------------------------
1915 inline bool wxDateTime::IsEqualTo(const wxDateTime
& datetime
) const
1917 wxASSERT_MSG( IsValid() && datetime
.IsValid(), _T("invalid wxDateTime"));
1919 return m_time
== datetime
.m_time
;
1922 inline bool wxDateTime::IsEarlierThan(const wxDateTime
& datetime
) const
1924 wxASSERT_MSG( IsValid() && datetime
.IsValid(), _T("invalid wxDateTime"));
1926 return m_time
< datetime
.m_time
;
1929 inline bool wxDateTime::IsLaterThan(const wxDateTime
& datetime
) const
1931 wxASSERT_MSG( IsValid() && datetime
.IsValid(), _T("invalid wxDateTime"));
1933 return m_time
> datetime
.m_time
;
1936 inline bool wxDateTime::IsStrictlyBetween(const wxDateTime
& t1
,
1937 const wxDateTime
& t2
) const
1939 // no need for assert, will be checked by the functions we call
1940 return IsLaterThan(t1
) && IsEarlierThan(t2
);
1943 inline bool wxDateTime::IsBetween(const wxDateTime
& t1
,
1944 const wxDateTime
& t2
) const
1946 // no need for assert, will be checked by the functions we call
1947 return IsEqualTo(t1
) || IsEqualTo(t2
) || IsStrictlyBetween(t1
, t2
);
1950 inline bool wxDateTime::IsSameDate(const wxDateTime
& dt
) const
1955 return tm1
.year
== tm2
.year
&&
1956 tm1
.mon
== tm2
.mon
&&
1957 tm1
.mday
== tm2
.mday
;
1960 inline bool wxDateTime::IsSameTime(const wxDateTime
& dt
) const
1962 // notice that we can't do something like this:
1964 // m_time % MILLISECONDS_PER_DAY == dt.m_time % MILLISECONDS_PER_DAY
1966 // because we have also to deal with (possibly) different DST settings!
1970 return tm1
.hour
== tm2
.hour
&&
1971 tm1
.min
== tm2
.min
&&
1972 tm1
.sec
== tm2
.sec
&&
1973 tm1
.msec
== tm2
.msec
;
1976 inline bool wxDateTime::IsEqualUpTo(const wxDateTime
& dt
,
1977 const wxTimeSpan
& ts
) const
1979 return IsBetween(dt
.Subtract(ts
), dt
.Add(ts
));
1982 // ----------------------------------------------------------------------------
1983 // wxDateTime arithmetics
1984 // ----------------------------------------------------------------------------
1986 inline wxDateTime
wxDateTime::Add(const wxTimeSpan
& diff
) const
1988 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1990 return wxDateTime(m_time
+ diff
.GetValue());
1993 inline wxDateTime
& wxDateTime::Add(const wxTimeSpan
& diff
)
1995 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1997 m_time
+= diff
.GetValue();
2002 inline wxDateTime
& wxDateTime::operator+=(const wxTimeSpan
& diff
)
2007 inline wxDateTime
wxDateTime::Subtract(const wxTimeSpan
& diff
) const
2009 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
2011 return wxDateTime(m_time
- diff
.GetValue());
2014 inline wxDateTime
& wxDateTime::Subtract(const wxTimeSpan
& diff
)
2016 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
2018 m_time
-= diff
.GetValue();
2023 inline wxDateTime
& wxDateTime::operator-=(const wxTimeSpan
& diff
)
2025 return Subtract(diff
);
2028 inline wxTimeSpan
wxDateTime::Subtract(const wxDateTime
& datetime
) const
2030 wxASSERT_MSG( IsValid() && datetime
.IsValid(), _T("invalid wxDateTime"));
2032 return wxTimeSpan(GetValue() - datetime
.GetValue());
2035 inline wxTimeSpan
wxDateTime::operator-(const wxDateTime
& dt2
) const
2037 return this->Subtract(dt2
);
2040 inline wxDateTime
wxDateTime::Add(const wxDateSpan
& diff
) const
2042 return wxDateTime(*this).Add(diff
);
2045 inline wxDateTime
& wxDateTime::Subtract(const wxDateSpan
& diff
)
2047 return Add(diff
.Negate());
2050 inline wxDateTime
wxDateTime::Subtract(const wxDateSpan
& diff
) const
2052 return wxDateTime(*this).Subtract(diff
);
2055 inline wxDateTime
& wxDateTime::operator-=(const wxDateSpan
& diff
)
2057 return Subtract(diff
);
2060 inline wxDateTime
& wxDateTime::operator+=(const wxDateSpan
& diff
)
2065 // ----------------------------------------------------------------------------
2066 // wxDateTime and timezones
2067 // ----------------------------------------------------------------------------
2070 wxDateTime::ToTimezone(const wxDateTime::TimeZone
& tz
, bool noDST
) const
2072 MODIFY_AND_RETURN( MakeTimezone(tz
, noDST
) );
2076 wxDateTime::FromTimezone(const wxDateTime::TimeZone
& tz
, bool noDST
) const
2078 MODIFY_AND_RETURN( MakeFromTimezone(tz
, noDST
) );
2081 // ----------------------------------------------------------------------------
2082 // wxTimeSpan construction
2083 // ----------------------------------------------------------------------------
2085 inline wxTimeSpan::wxTimeSpan(long hours
,
2088 wxLongLong milliseconds
)
2090 // assign first to avoid precision loss
2097 m_diff
+= milliseconds
;
2100 // ----------------------------------------------------------------------------
2101 // wxTimeSpan accessors
2102 // ----------------------------------------------------------------------------
2104 inline wxLongLong
wxTimeSpan::GetSeconds() const
2106 return m_diff
/ 1000l;
2109 inline int wxTimeSpan::GetMinutes() const
2111 // explicit cast to int suppresses a warning with CodeWarrior and possibly
2112 // others (changing the return type to long from int is impossible in 2.8)
2113 return (int)((GetSeconds() / 60l).GetLo());
2116 inline int wxTimeSpan::GetHours() const
2118 return GetMinutes() / 60;
2121 inline int wxTimeSpan::GetDays() const
2123 return GetHours() / 24;
2126 inline int wxTimeSpan::GetWeeks() const
2128 return GetDays() / 7;
2131 // ----------------------------------------------------------------------------
2132 // wxTimeSpan arithmetics
2133 // ----------------------------------------------------------------------------
2135 inline wxTimeSpan
wxTimeSpan::Add(const wxTimeSpan
& diff
) const
2137 return wxTimeSpan(m_diff
+ diff
.GetValue());
2140 inline wxTimeSpan
& wxTimeSpan::Add(const wxTimeSpan
& diff
)
2142 m_diff
+= diff
.GetValue();
2147 inline wxTimeSpan
wxTimeSpan::Subtract(const wxTimeSpan
& diff
) const
2149 return wxTimeSpan(m_diff
- diff
.GetValue());
2152 inline wxTimeSpan
& wxTimeSpan::Subtract(const wxTimeSpan
& diff
)
2154 m_diff
-= diff
.GetValue();
2159 inline wxTimeSpan
& wxTimeSpan::Multiply(int n
)
2166 inline wxTimeSpan
wxTimeSpan::Multiply(int n
) const
2168 return wxTimeSpan(m_diff
* (long)n
);
2171 inline wxTimeSpan
wxTimeSpan::Abs() const
2173 return wxTimeSpan(GetValue().Abs());
2176 inline bool wxTimeSpan::IsEqualTo(const wxTimeSpan
& ts
) const
2178 return GetValue() == ts
.GetValue();
2181 inline bool wxTimeSpan::IsLongerThan(const wxTimeSpan
& ts
) const
2183 return GetValue().Abs() > ts
.GetValue().Abs();
2186 // ----------------------------------------------------------------------------
2188 // ----------------------------------------------------------------------------
2190 inline wxDateSpan
& wxDateSpan::operator+=(const wxDateSpan
& other
)
2192 m_years
+= other
.m_years
;
2193 m_months
+= other
.m_months
;
2194 m_weeks
+= other
.m_weeks
;
2195 m_days
+= other
.m_days
;
2200 inline wxDateSpan
& wxDateSpan::Add(const wxDateSpan
& other
)
2202 return *this += other
;
2205 inline wxDateSpan
wxDateSpan::Add(const wxDateSpan
& other
) const
2207 wxDateSpan
ds(*this);
2212 inline wxDateSpan
& wxDateSpan::Multiply(int factor
)
2222 inline wxDateSpan
wxDateSpan::Multiply(int factor
) const
2224 wxDateSpan
ds(*this);
2225 ds
.Multiply(factor
);
2229 inline wxDateSpan
wxDateSpan::Negate() const
2231 return wxDateSpan(-m_years
, -m_months
, -m_weeks
, -m_days
);
2234 inline wxDateSpan
& wxDateSpan::Neg()
2237 m_months
= -m_months
;
2244 inline wxDateSpan
& wxDateSpan::operator-=(const wxDateSpan
& other
)
2246 return *this += other
.Negate();
2249 inline wxDateSpan
& wxDateSpan::Subtract(const wxDateSpan
& other
)
2251 return *this -= other
;
2254 inline wxDateSpan
wxDateSpan::Subtract(const wxDateSpan
& other
) const
2256 wxDateSpan
ds(*this);
2261 #undef MILLISECONDS_PER_DAY
2263 #undef MODIFY_AND_RETURN
2265 // ============================================================================
2267 // ============================================================================
2269 // ----------------------------------------------------------------------------
2270 // wxTimeSpan operators
2271 // ----------------------------------------------------------------------------
2273 wxTimeSpan WXDLLIMPEXP_BASE
operator*(int n
, const wxTimeSpan
& ts
);
2275 // ----------------------------------------------------------------------------
2277 // ----------------------------------------------------------------------------
2279 wxDateSpan WXDLLIMPEXP_BASE
operator*(int n
, const wxDateSpan
& ds
);
2281 // ============================================================================
2282 // other helper functions
2283 // ============================================================================
2285 // ----------------------------------------------------------------------------
2286 // iteration helpers: can be used to write a for loop over enum variable like
2288 // for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) )
2289 // ----------------------------------------------------------------------------
2291 WXDLLIMPEXP_BASE
void wxNextMonth(wxDateTime::Month
& m
);
2292 WXDLLIMPEXP_BASE
void wxPrevMonth(wxDateTime::Month
& m
);
2293 WXDLLIMPEXP_BASE
void wxNextWDay(wxDateTime::WeekDay
& wd
);
2294 WXDLLIMPEXP_BASE
void wxPrevWDay(wxDateTime::WeekDay
& wd
);
2296 #endif // wxUSE_DATETIME
2298 #endif // _WX_DATETIME_H