1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: declarations of time/date related classes (wxDateTime,
5 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_DATETIME_H
13 #define _WX_DATETIME_H
20 #include "wx/msw/wince/time.h"
25 #include <limits.h> // for INT_MIN
27 #include "wx/longlong.h"
28 #include "wx/anystr.h"
30 class WXDLLIMPEXP_FWD_BASE wxDateTime
;
31 class WXDLLIMPEXP_FWD_BASE wxTimeSpan
;
32 class WXDLLIMPEXP_FWD_BASE wxDateSpan
;
37 #include "wx/dynarray.h"
39 // not all c-runtimes are based on 1/1/1970 being (time_t) 0
40 // set this to the corresponding value in seconds 1/1/1970 has on your
43 #define WX_TIME_BASE_OFFSET 0
48 * + 1. Time zones with minutes (make TimeZone a class)
49 * ? 2. getdate() function like under Solaris
50 * + 3. text conversion for wxDateSpan
51 * + 4. pluggable modules for the workdays calculations
52 * 5. wxDateTimeHolidayAuthority for Easter and other christian feasts
56 The three (main) classes declared in this header represent:
58 1. An absolute moment in the time (wxDateTime)
59 2. A difference between two moments in the time, positive or negative
61 3. A logical difference between two dates expressed in
62 years/months/weeks/days (wxDateSpan)
64 The following arithmetic operations are permitted (all others are not):
69 wxDateTime + wxTimeSpan = wxDateTime
70 wxDateTime + wxDateSpan = wxDateTime
71 wxTimeSpan + wxTimeSpan = wxTimeSpan
72 wxDateSpan + wxDateSpan = wxDateSpan
76 wxDateTime - wxDateTime = wxTimeSpan
77 wxDateTime - wxTimeSpan = wxDateTime
78 wxDateTime - wxDateSpan = wxDateTime
79 wxTimeSpan - wxTimeSpan = wxTimeSpan
80 wxDateSpan - wxDateSpan = wxDateSpan
84 wxTimeSpan * number = wxTimeSpan
85 number * wxTimeSpan = wxTimeSpan
86 wxDateSpan * number = wxDateSpan
87 number * wxDateSpan = wxDateSpan
91 -wxTimeSpan = wxTimeSpan
92 -wxDateSpan = wxDateSpan
94 For each binary operation OP (+, -, *) we have the following operatorOP=() as
95 a method and the method with a symbolic name OPER (Add, Subtract, Multiply)
96 as a synonym for it and another const method with the same name which returns
97 the changed copy of the object and operatorOP() as a global function which is
98 implemented in terms of the const version of OPEN. For the unary - we have
99 operator-() as a method, Neg() as synonym for it and Negate() which returns
100 the copy of the object with the changed sign.
103 // an invalid/default date time object which may be used as the default
104 // argument for arguments of type wxDateTime; it is also returned by all
105 // functions returning wxDateTime on failure (this is why it is also called
106 // wxInvalidDateTime)
107 class WXDLLIMPEXP_FWD_BASE wxDateTime
;
109 extern WXDLLIMPEXP_DATA_BASE(const char) wxDefaultDateTimeFormat
[];
110 extern WXDLLIMPEXP_DATA_BASE(const char) wxDefaultTimeSpanFormat
[];
111 extern WXDLLIMPEXP_DATA_BASE(const wxDateTime
) wxDefaultDateTime
;
113 #define wxInvalidDateTime wxDefaultDateTime
116 // ----------------------------------------------------------------------------
117 // conditional compilation
118 // ----------------------------------------------------------------------------
120 // if configure detected strftime(), we have it too
122 #define wxHAS_STRFTIME
123 // suppose everyone else has strftime except Win CE unless VC8 is used
124 #elif !defined(__WXWINCE__) || defined(__VISUALC8__)
125 #define wxHAS_STRFTIME
128 // ----------------------------------------------------------------------------
129 // wxDateTime represents an absolute moment in the time
130 // ----------------------------------------------------------------------------
132 class WXDLLIMPEXP_BASE wxDateTime
136 // ------------------------------------------------------------------------
138 // a small unsigned integer type for storing things like minutes,
139 // seconds &c. It should be at least short (i.e. not char) to contain
140 // the number of milliseconds - it may also be 'int' because there is
141 // no size penalty associated with it in our code, we don't store any
142 // data in this format
143 typedef unsigned short wxDateTime_t
;
146 // ------------------------------------------------------------------------
151 // the time in the current time zone
154 // zones from GMT (= Greenwich Mean Time): they're guaranteed to be
155 // consequent numbers, so writing something like `GMT0 + offset' is
156 // safe if abs(offset) <= 12
158 // underscore stands for minus
159 GMT_12
, GMT_11
, GMT_10
, GMT_9
, GMT_8
, GMT_7
,
160 GMT_6
, GMT_5
, GMT_4
, GMT_3
, GMT_2
, GMT_1
,
162 GMT1
, GMT2
, GMT3
, GMT4
, GMT5
, GMT6
,
163 GMT7
, GMT8
, GMT9
, GMT10
, GMT11
, GMT12
, GMT13
,
164 // Note that GMT12 and GMT_12 are not the same: there is a difference
165 // of exactly one day between them
167 // some symbolic names for TZ
170 WET
= GMT0
, // Western Europe Time
171 WEST
= GMT1
, // Western Europe Summer Time
172 CET
= GMT1
, // Central Europe Time
173 CEST
= GMT2
, // Central Europe Summer Time
174 EET
= GMT2
, // Eastern Europe Time
175 EEST
= GMT3
, // Eastern Europe Summer Time
176 MSK
= GMT3
, // Moscow Time
177 MSD
= GMT4
, // Moscow Summer Time
180 AST
= GMT_4
, // Atlantic Standard Time
181 ADT
= GMT_3
, // Atlantic Daylight Time
182 EST
= GMT_5
, // Eastern Standard Time
183 EDT
= GMT_4
, // Eastern Daylight Saving Time
184 CST
= GMT_6
, // Central Standard Time
185 CDT
= GMT_5
, // Central Daylight Saving Time
186 MST
= GMT_7
, // Mountain Standard Time
187 MDT
= GMT_6
, // Mountain Daylight Saving Time
188 PST
= GMT_8
, // Pacific Standard Time
189 PDT
= GMT_7
, // Pacific Daylight Saving Time
190 HST
= GMT_10
, // Hawaiian Standard Time
191 AKST
= GMT_9
, // Alaska Standard Time
192 AKDT
= GMT_8
, // Alaska Daylight Saving Time
196 A_WST
= GMT8
, // Western Standard Time
197 A_CST
= GMT13
+ 1, // Central Standard Time (+9.5)
198 A_EST
= GMT10
, // Eastern Standard Time
199 A_ESST
= GMT11
, // Eastern Summer Time
202 NZST
= GMT12
, // Standard Time
203 NZDT
= GMT13
, // Daylight Saving Time
205 // TODO add more symbolic timezone names here
207 // Universal Coordinated Time = the new and politically correct name
212 // the calendar systems we know about: notice that it's valid (for
213 // this classes purpose anyhow) to work with any of these calendars
214 // even with the dates before the historical appearance of the
218 Gregorian
, // current calendar
219 Julian
// calendar in use since -45 until the 1582 (or later)
221 // TODO Hebrew, Chinese, Maya, ... (just kidding) (or then may be not?)
224 // these values only are used to identify the different dates of
225 // adoption of the Gregorian calendar (see IsGregorian())
227 // All data and comments taken verbatim from "The Calendar FAQ (v 2.0)"
228 // by Claus Tøndering, http://www.pip.dknet.dk/~c-t/calendar.html
229 // except for the comments "we take".
231 // Symbol "->" should be read as "was followed by" in the comments
233 enum GregorianAdoption
235 Gr_Unknown
, // no data for this country or it's too uncertain to use
236 Gr_Standard
, // on the day 0 of Gregorian calendar: 15 Oct 1582
238 Gr_Alaska
, // Oct 1867 when Alaska became part of the USA
239 Gr_Albania
, // Dec 1912
241 Gr_Austria
= Gr_Unknown
, // Different regions on different dates
242 Gr_Austria_Brixen
, // 5 Oct 1583 -> 16 Oct 1583
243 Gr_Austria_Salzburg
= Gr_Austria_Brixen
,
244 Gr_Austria_Tyrol
= Gr_Austria_Brixen
,
245 Gr_Austria_Carinthia
, // 14 Dec 1583 -> 25 Dec 1583
246 Gr_Austria_Styria
= Gr_Austria_Carinthia
,
248 Gr_Belgium
, // Then part of the Netherlands
250 Gr_Bulgaria
= Gr_Unknown
, // Unknown precisely (from 1915 to 1920)
251 Gr_Bulgaria_1
, // 18 Mar 1916 -> 1 Apr 1916
252 Gr_Bulgaria_2
, // 31 Mar 1916 -> 14 Apr 1916
253 Gr_Bulgaria_3
, // 3 Sep 1920 -> 17 Sep 1920
255 Gr_Canada
= Gr_Unknown
, // Different regions followed the changes in
256 // Great Britain or France
258 Gr_China
= Gr_Unknown
, // Different authorities say:
259 Gr_China_1
, // 18 Dec 1911 -> 1 Jan 1912
260 Gr_China_2
, // 18 Dec 1928 -> 1 Jan 1929
262 Gr_Czechoslovakia
, // (Bohemia and Moravia) 6 Jan 1584 -> 17 Jan 1584
263 Gr_Denmark
, // (including Norway) 18 Feb 1700 -> 1 Mar 1700
266 Gr_Finland
, // Then part of Sweden
268 Gr_France
, // 9 Dec 1582 -> 20 Dec 1582
269 Gr_France_Alsace
, // 4 Feb 1682 -> 16 Feb 1682
270 Gr_France_Lorraine
, // 16 Feb 1760 -> 28 Feb 1760
271 Gr_France_Strasbourg
, // February 1682
273 Gr_Germany
= Gr_Unknown
, // Different states on different dates:
274 Gr_Germany_Catholic
, // 1583-1585 (we take 1584)
275 Gr_Germany_Prussia
, // 22 Aug 1610 -> 2 Sep 1610
276 Gr_Germany_Protestant
, // 18 Feb 1700 -> 1 Mar 1700
278 Gr_GreatBritain
, // 2 Sep 1752 -> 14 Sep 1752 (use 'cal(1)')
280 Gr_Greece
, // 9 Mar 1924 -> 23 Mar 1924
281 Gr_Hungary
, // 21 Oct 1587 -> 1 Nov 1587
282 Gr_Ireland
= Gr_GreatBritain
,
283 Gr_Italy
= Gr_Standard
,
285 Gr_Japan
= Gr_Unknown
, // Different authorities say:
286 Gr_Japan_1
, // 19 Dec 1872 -> 1 Jan 1873
287 Gr_Japan_2
, // 19 Dec 1892 -> 1 Jan 1893
288 Gr_Japan_3
, // 18 Dec 1918 -> 1 Jan 1919
290 Gr_Latvia
, // 1915-1918 (we take 1915)
291 Gr_Lithuania
, // 1915
292 Gr_Luxemburg
, // 14 Dec 1582 -> 25 Dec 1582
293 Gr_Netherlands
= Gr_Belgium
, // (including Belgium) 1 Jan 1583
295 // this is too weird to take into account: the Gregorian calendar was
296 // introduced twice in Groningen, first time 28 Feb 1583 was followed
297 // by 11 Mar 1583, then it has gone back to Julian in the summer of
298 // 1584 and then 13 Dec 1700 -> 12 Jan 1701 - which is
299 // the date we take here
300 Gr_Netherlands_Groningen
, // 13 Dec 1700 -> 12 Jan 1701
301 Gr_Netherlands_Gelderland
, // 30 Jun 1700 -> 12 Jul 1700
302 Gr_Netherlands_Utrecht
, // (and Overijssel) 30 Nov 1700->12 Dec 1700
303 Gr_Netherlands_Friesland
, // (and Drenthe) 31 Dec 1700 -> 12 Jan 1701
305 Gr_Norway
= Gr_Denmark
, // Then part of Denmark
306 Gr_Poland
= Gr_Standard
,
307 Gr_Portugal
= Gr_Standard
,
308 Gr_Romania
, // 31 Mar 1919 -> 14 Apr 1919
309 Gr_Russia
, // 31 Jan 1918 -> 14 Feb 1918
310 Gr_Scotland
= Gr_GreatBritain
,
311 Gr_Spain
= Gr_Standard
,
313 // Sweden has a curious history. Sweden decided to make a gradual
314 // change from the Julian to the Gregorian calendar. By dropping every
315 // leap year from 1700 through 1740 the eleven superfluous days would
316 // be omitted and from 1 Mar 1740 they would be in sync with the
317 // Gregorian calendar. (But in the meantime they would be in sync with
320 // So 1700 (which should have been a leap year in the Julian calendar)
321 // was not a leap year in Sweden. However, by mistake 1704 and 1708
322 // became leap years. This left Sweden out of synchronisation with
323 // both the Julian and the Gregorian world, so they decided to go back
324 // to the Julian calendar. In order to do this, they inserted an extra
325 // day in 1712, making that year a double leap year! So in 1712,
326 // February had 30 days in Sweden.
328 // Later, in 1753, Sweden changed to the Gregorian calendar by
329 // dropping 11 days like everyone else.
330 Gr_Sweden
= Gr_Finland
, // 17 Feb 1753 -> 1 Mar 1753
332 Gr_Switzerland
= Gr_Unknown
,// Different cantons used different dates
333 Gr_Switzerland_Catholic
, // 1583, 1584 or 1597 (we take 1584)
334 Gr_Switzerland_Protestant
, // 31 Dec 1700 -> 12 Jan 1701
336 Gr_Turkey
, // 1 Jan 1927
337 Gr_USA
= Gr_GreatBritain
,
338 Gr_Wales
= Gr_GreatBritain
,
339 Gr_Yugoslavia
// 1919
342 // the country parameter is used so far for calculating the start and
343 // the end of DST period and for deciding whether the date is a work
346 // TODO move this to intl.h
348 // Required for WinCE
355 Country_Unknown
, // no special information for this country
356 Country_Default
, // set the default country with SetCountry() method
357 // or use the default country with any other
359 // TODO add more countries (for this we must know about DST and/or
360 // holidays for this country)
362 // Western European countries: we assume that they all follow the same
363 // DST rules (true or false?)
364 Country_WesternEurope_Start
,
365 Country_EEC
= Country_WesternEurope_Start
,
369 Country_WesternEurope_End
= UK
,
374 // symbolic names for the months
377 Jan
, Feb
, Mar
, Apr
, May
, Jun
, Jul
, Aug
, Sep
, Oct
, Nov
, Dec
, Inv_Month
380 // symbolic names for the weekdays
383 Sun
, Mon
, Tue
, Wed
, Thu
, Fri
, Sat
, Inv_WeekDay
386 // invalid value for the year
389 Inv_Year
= SHRT_MIN
// should hold in wxDateTime_t
392 // flags for GetWeekDayName and GetMonthName
395 Name_Full
= 0x01, // return full name
396 Name_Abbr
= 0x02 // return abbreviated name
399 // flags for GetWeekOfYear and GetWeekOfMonth
402 Default_First
, // Sunday_First for US, Monday_First for the rest
403 Monday_First
, // week starts with a Monday
404 Sunday_First
// week starts with a Sunday
408 // ------------------------------------------------------------------------
410 // a class representing a time zone: basically, this is just an offset
411 // (in seconds) from GMT
412 class WXDLLIMPEXP_BASE TimeZone
417 // create time zone object with the given offset
418 TimeZone(long offset
= 0) { m_offset
= offset
; }
420 static TimeZone
Make(long offset
)
423 tz
.m_offset
= offset
;
427 long GetOffset() const { return m_offset
; }
430 // offset for this timezone from GMT in seconds
434 // standard struct tm is limited to the years from 1900 (because
435 // tm_year field is the offset from 1900), so we use our own struct
436 // instead to represent broken down time
438 // NB: this struct should always be kept normalized (i.e. mon should
439 // be < 12, 1 <= day <= 31 &c), so use AddMonths(), AddDays()
440 // instead of modifying the member fields directly!
441 struct WXDLLIMPEXP_BASE Tm
443 wxDateTime_t msec
, sec
, min
, hour
,
444 mday
, // Day of the month in 1..31 range.
445 yday
; // Day of the year in 0..365 range.
449 // default ctor inits the object to an invalid value
452 // ctor from struct tm and the timezone
453 Tm(const struct tm
& tm
, const TimeZone
& tz
);
455 // check that the given date/time is valid (in Gregorian calendar)
456 bool IsValid() const;
459 WeekDay
GetWeekDay() // not const because wday may be changed
461 if ( wday
== Inv_WeekDay
)
464 return (WeekDay
)wday
;
467 // add the given number of months to the date keeping it normalized
468 void AddMonths(int monDiff
);
470 // add the given number of months to the date keeping it normalized
471 void AddDays(int dayDiff
);
474 // compute the weekday from other fields
475 void ComputeWeekDay();
477 // the timezone we correspond to
480 // This value can only be accessed via GetWeekDay() and not directly
481 // because it's not always computed when creating this object and may
482 // need to be calculated on demand.
487 // ------------------------------------------------------------------------
489 // set the current country
490 static void SetCountry(Country country
);
491 // get the current country
492 static Country
GetCountry();
494 // return true if the country is a West European one (in practice,
495 // this means that the same DST rules as for EEC apply)
496 static bool IsWestEuropeanCountry(Country country
= Country_Default
);
498 // return the current year
499 static int GetCurrentYear(Calendar cal
= Gregorian
);
501 // convert the year as returned by wxDateTime::GetYear() to a year
502 // suitable for BC/AD notation. The difference is that BC year 1
503 // corresponds to the year 0 (while BC year 0 didn't exist) and AD
504 // year N is just year N.
505 static int ConvertYearToBC(int year
);
507 // return the current month
508 static Month
GetCurrentMonth(Calendar cal
= Gregorian
);
510 // returns true if the given year is a leap year in the given calendar
511 static bool IsLeapYear(int year
= Inv_Year
, Calendar cal
= Gregorian
);
513 // get the century (19 for 1999, 20 for 2000 and -5 for 492 BC)
514 static int GetCentury(int year
);
516 // returns the number of days in this year (356 or 355 for Gregorian
517 // calendar usually :-)
518 static wxDateTime_t
GetNumberOfDays(int year
, Calendar cal
= Gregorian
);
520 // get the number of the days in the given month (default value for
521 // the year means the current one)
522 static wxDateTime_t
GetNumberOfDays(Month month
,
524 Calendar cal
= Gregorian
);
527 // get the full (default) or abbreviated month name in the current
528 // locale, returns empty string on error
529 static wxString
GetMonthName(Month month
,
530 NameFlags flags
= Name_Full
);
532 // get the standard English full (default) or abbreviated month name
533 static wxString
GetEnglishMonthName(Month month
,
534 NameFlags flags
= Name_Full
);
536 // get the full (default) or abbreviated weekday name in the current
537 // locale, returns empty string on error
538 static wxString
GetWeekDayName(WeekDay weekday
,
539 NameFlags flags
= Name_Full
);
541 // get the standard English full (default) or abbreviated weekday name
542 static wxString
GetEnglishWeekDayName(WeekDay weekday
,
543 NameFlags flags
= Name_Full
);
545 // get the AM and PM strings in the current locale (may be empty)
546 static void GetAmPmStrings(wxString
*am
, wxString
*pm
);
548 // return true if the given country uses DST for this year
549 static bool IsDSTApplicable(int year
= Inv_Year
,
550 Country country
= Country_Default
);
552 // get the beginning of DST for this year, will return invalid object
553 // if no DST applicable in this year. The default value of the
554 // parameter means to take the current year.
555 static wxDateTime
GetBeginDST(int year
= Inv_Year
,
556 Country country
= Country_Default
);
557 // get the end of DST for this year, will return invalid object
558 // if no DST applicable in this year. The default value of the
559 // parameter means to take the current year.
560 static wxDateTime
GetEndDST(int year
= Inv_Year
,
561 Country country
= Country_Default
);
563 // return the wxDateTime object for the current time
564 static inline wxDateTime
Now();
566 // return the wxDateTime object for the current time with millisecond
567 // precision (if available on this platform)
568 static wxDateTime
UNow();
570 // return the wxDateTime object for today midnight: i.e. as Now() but
571 // with time set to 0
572 static inline wxDateTime
Today();
574 // constructors: you should test whether the constructor succeeded with
575 // IsValid() function. The values Inv_Month and Inv_Year for the
576 // parameters mean take current month and/or year values.
577 // ------------------------------------------------------------------------
579 // default ctor does not initialize the object, use Set()!
580 wxDateTime() { m_time
= wxLongLong(wxINT32_MIN
, 0); }
582 // from time_t: seconds since the Epoch 00:00:00 UTC, Jan 1, 1970)
583 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
584 // VA C++ confuses this with wxDateTime(double jdn) thinking it is a duplicate declaration
585 inline wxDateTime(time_t timet
);
587 // from broken down time/date (only for standard Unix range)
588 inline wxDateTime(const struct tm
& tm
);
589 // from broken down time/date (any range)
590 inline wxDateTime(const Tm
& tm
);
592 // from JDN (beware of rounding errors)
593 inline wxDateTime(double jdn
);
595 // from separate values for each component, date set to today
596 inline wxDateTime(wxDateTime_t hour
,
597 wxDateTime_t minute
= 0,
598 wxDateTime_t second
= 0,
599 wxDateTime_t millisec
= 0);
600 // from separate values for each component with explicit date
601 inline wxDateTime(wxDateTime_t day
, // day of the month
603 int year
= Inv_Year
, // 1999, not 99 please!
604 wxDateTime_t hour
= 0,
605 wxDateTime_t minute
= 0,
606 wxDateTime_t second
= 0,
607 wxDateTime_t millisec
= 0);
609 wxDateTime(const struct _SYSTEMTIME
& st
)
611 SetFromMSWSysTime(st
);
615 // default copy ctor ok
619 // assignment operators and Set() functions: all non const methods return
620 // the reference to this object. IsValid() should be used to test whether
621 // the function succeeded.
622 // ------------------------------------------------------------------------
624 // set to the current time
625 inline wxDateTime
& SetToCurrent();
627 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
628 // VA C++ confuses this with wxDateTime(double jdn) thinking it is a duplicate declaration
629 // set to given time_t value
630 inline wxDateTime
& Set(time_t timet
);
633 // set to given broken down time/date
634 wxDateTime
& Set(const struct tm
& tm
);
636 // set to given broken down time/date
637 inline wxDateTime
& Set(const Tm
& tm
);
639 // set to given JDN (beware of rounding errors)
640 wxDateTime
& Set(double jdn
);
642 // set to given time, date = today
643 wxDateTime
& Set(wxDateTime_t hour
,
644 wxDateTime_t minute
= 0,
645 wxDateTime_t second
= 0,
646 wxDateTime_t millisec
= 0);
648 // from separate values for each component with explicit date
649 // (defaults for month and year are the current values)
650 wxDateTime
& Set(wxDateTime_t day
,
652 int year
= Inv_Year
, // 1999, not 99 please!
653 wxDateTime_t hour
= 0,
654 wxDateTime_t minute
= 0,
655 wxDateTime_t second
= 0,
656 wxDateTime_t millisec
= 0);
658 // resets time to 00:00:00, doesn't change the date
659 wxDateTime
& ResetTime();
661 // get the date part of this object only, i.e. the object which has the
662 // same date as this one but time of 00:00:00
663 wxDateTime
GetDateOnly() const;
665 // the following functions don't change the values of the other
666 // fields, i.e. SetMinute() won't change either hour or seconds value
669 wxDateTime
& SetYear(int year
);
671 wxDateTime
& SetMonth(Month month
);
672 // set the day of the month
673 wxDateTime
& SetDay(wxDateTime_t day
);
675 wxDateTime
& SetHour(wxDateTime_t hour
);
677 wxDateTime
& SetMinute(wxDateTime_t minute
);
679 wxDateTime
& SetSecond(wxDateTime_t second
);
681 wxDateTime
& SetMillisecond(wxDateTime_t millisecond
);
683 // assignment operator from time_t
684 wxDateTime
& operator=(time_t timet
) { return Set(timet
); }
686 // assignment operator from broken down time/date
687 wxDateTime
& operator=(const struct tm
& tm
) { return Set(tm
); }
689 // assignment operator from broken down time/date
690 wxDateTime
& operator=(const Tm
& tm
) { return Set(tm
); }
692 // default assignment operator is ok
694 // calendar calculations (functions which set the date only leave the time
695 // unchanged, e.g. don't explicitly zero it): SetXXX() functions modify the
696 // object itself, GetXXX() ones return a new object.
697 // ------------------------------------------------------------------------
699 // set to the given week day in the same week as this one
700 wxDateTime
& SetToWeekDayInSameWeek(WeekDay weekday
,
701 WeekFlags flags
= Monday_First
);
702 inline wxDateTime
GetWeekDayInSameWeek(WeekDay weekday
,
703 WeekFlags flags
= Monday_First
) const;
705 // set to the next week day following this one
706 wxDateTime
& SetToNextWeekDay(WeekDay weekday
);
707 inline wxDateTime
GetNextWeekDay(WeekDay weekday
) const;
709 // set to the previous week day before this one
710 wxDateTime
& SetToPrevWeekDay(WeekDay weekday
);
711 inline wxDateTime
GetPrevWeekDay(WeekDay weekday
) const;
713 // set to Nth occurrence of given weekday in the given month of the
714 // given year (time is set to 0), return true on success and false on
715 // failure. n may be positive (1..5) or negative to count from the end
716 // of the month (see helper function SetToLastWeekDay())
717 bool SetToWeekDay(WeekDay weekday
,
719 Month month
= Inv_Month
,
720 int year
= Inv_Year
);
721 inline wxDateTime
GetWeekDay(WeekDay weekday
,
723 Month month
= Inv_Month
,
724 int year
= Inv_Year
) const;
726 // sets to the last weekday in the given month, year
727 inline bool SetToLastWeekDay(WeekDay weekday
,
728 Month month
= Inv_Month
,
729 int year
= Inv_Year
);
730 inline wxDateTime
GetLastWeekDay(WeekDay weekday
,
731 Month month
= Inv_Month
,
732 int year
= Inv_Year
);
734 #if WXWIN_COMPATIBILITY_2_6
735 // sets the date to the given day of the given week in the year,
736 // returns true on success and false if given date doesn't exist (e.g.
739 // these functions are badly defined as they're not the reverse of
740 // GetWeekOfYear(), use SetToTheWeekOfYear() instead
741 wxDEPRECATED( bool SetToTheWeek(wxDateTime_t numWeek
,
742 WeekDay weekday
= Mon
,
743 WeekFlags flags
= Monday_First
) );
744 wxDEPRECATED( wxDateTime
GetWeek(wxDateTime_t numWeek
,
745 WeekDay weekday
= Mon
,
746 WeekFlags flags
= Monday_First
) const );
747 #endif // WXWIN_COMPATIBILITY_2_6
749 // returns the date corresponding to the given week day of the given
750 // week (in ISO notation) of the specified year
751 static wxDateTime
SetToWeekOfYear(int year
,
752 wxDateTime_t numWeek
,
753 WeekDay weekday
= Mon
);
755 // sets the date to the last day of the given (or current) month or the
756 // given (or current) year
757 wxDateTime
& SetToLastMonthDay(Month month
= Inv_Month
,
758 int year
= Inv_Year
);
759 inline wxDateTime
GetLastMonthDay(Month month
= Inv_Month
,
760 int year
= Inv_Year
) const;
762 // sets to the given year day (1..365 or 366)
763 wxDateTime
& SetToYearDay(wxDateTime_t yday
);
764 inline wxDateTime
GetYearDay(wxDateTime_t yday
) const;
766 // The definitions below were taken verbatim from
768 // http://www.capecod.net/~pbaum/date/date0.htm
770 // (Peter Baum's home page)
772 // definition: The Julian Day Number, Julian Day, or JD of a
773 // particular instant of time is the number of days and fractions of a
774 // day since 12 hours Universal Time (Greenwich mean noon) on January
775 // 1 of the year -4712, where the year is given in the Julian
776 // proleptic calendar. The idea of using this reference date was
777 // originally proposed by Joseph Scalizer in 1582 to count years but
778 // it was modified by 19th century astronomers to count days. One
779 // could have equivalently defined the reference time to be noon of
780 // November 24, -4713 if were understood that Gregorian calendar rules
781 // were applied. Julian days are Julian Day Numbers and are not to be
782 // confused with Julian dates.
784 // definition: The Rata Die number is a date specified as the number
785 // of days relative to a base date of December 31 of the year 0. Thus
786 // January 1 of the year 1 is Rata Die day 1.
788 // get the Julian Day number (the fractional part specifies the time of
789 // the day, related to noon - beware of rounding errors!)
790 double GetJulianDayNumber() const;
791 double GetJDN() const { return GetJulianDayNumber(); }
793 // get the Modified Julian Day number: it is equal to JDN - 2400000.5
794 // and so integral MJDs correspond to the midnights (and not noons).
795 // MJD 0 is Nov 17, 1858
796 double GetModifiedJulianDayNumber() const { return GetJDN() - 2400000.5; }
797 double GetMJD() const { return GetModifiedJulianDayNumber(); }
799 // get the Rata Die number
800 double GetRataDie() const;
802 // TODO algorithms for calculating some important dates, such as
803 // religious holidays (Easter...) or moon/solar eclipses? Some
804 // algorithms can be found in the calendar FAQ
807 // Timezone stuff: a wxDateTime object constructed using given
808 // day/month/year/hour/min/sec values is interpreted as this moment in
809 // local time. Using the functions below, it may be converted to another
810 // time zone (e.g., the Unix epoch is wxDateTime(1, Jan, 1970).ToGMT()).
812 // These functions try to handle DST internally, but there is no magical
813 // way to know all rules for it in all countries in the world, so if the
814 // program can handle it itself (or doesn't want to handle it at all for
815 // whatever reason), the DST handling can be disabled with noDST.
816 // ------------------------------------------------------------------------
818 // transform to any given timezone
819 inline wxDateTime
ToTimezone(const TimeZone
& tz
, bool noDST
= false) const;
820 wxDateTime
& MakeTimezone(const TimeZone
& tz
, bool noDST
= false);
822 // interpret current value as being in another timezone and transform
824 inline wxDateTime
FromTimezone(const TimeZone
& tz
, bool noDST
= false) const;
825 wxDateTime
& MakeFromTimezone(const TimeZone
& tz
, bool noDST
= false);
827 // transform to/from GMT/UTC
828 wxDateTime
ToUTC(bool noDST
= false) const { return ToTimezone(UTC
, noDST
); }
829 wxDateTime
& MakeUTC(bool noDST
= false) { return MakeTimezone(UTC
, noDST
); }
831 wxDateTime
ToGMT(bool noDST
= false) const { return ToUTC(noDST
); }
832 wxDateTime
& MakeGMT(bool noDST
= false) { return MakeUTC(noDST
); }
834 wxDateTime
FromUTC(bool noDST
= false) const
835 { return FromTimezone(UTC
, noDST
); }
836 wxDateTime
& MakeFromUTC(bool noDST
= false)
837 { return MakeFromTimezone(UTC
, noDST
); }
839 // is daylight savings time in effect at this moment according to the
840 // rules of the specified country?
842 // Return value is > 0 if DST is in effect, 0 if it is not and -1 if
843 // the information is not available (this is compatible with ANSI C)
844 int IsDST(Country country
= Country_Default
) const;
847 // accessors: many of them take the timezone parameter which indicates the
848 // timezone for which to make the calculations and the default value means
849 // to do it for the current timezone of this machine (even if the function
850 // only operates with the date it's necessary because a date may wrap as
851 // result of timezone shift)
852 // ------------------------------------------------------------------------
854 // is the date valid?
855 inline bool IsValid() const { return m_time
!= wxInvalidDateTime
.m_time
; }
857 // get the broken down date/time representation in the given timezone
859 // If you wish to get several time components (day, month and year),
860 // consider getting the whole Tm strcuture first and retrieving the
861 // value from it - this is much more efficient
862 Tm
GetTm(const TimeZone
& tz
= Local
) const;
864 // get the number of seconds since the Unix epoch - returns (time_t)-1
865 // if the value is out of range
866 inline time_t GetTicks() const;
868 // get the century, same as GetCentury(GetYear())
869 int GetCentury(const TimeZone
& tz
= Local
) const
870 { return GetCentury(GetYear(tz
)); }
871 // get the year (returns Inv_Year if date is invalid)
872 int GetYear(const TimeZone
& tz
= Local
) const
873 { return GetTm(tz
).year
; }
874 // get the month (Inv_Month if date is invalid)
875 Month
GetMonth(const TimeZone
& tz
= Local
) const
876 { return (Month
)GetTm(tz
).mon
; }
877 // get the month day (in 1..31 range, 0 if date is invalid)
878 wxDateTime_t
GetDay(const TimeZone
& tz
= Local
) const
879 { return GetTm(tz
).mday
; }
880 // get the day of the week (Inv_WeekDay if date is invalid)
881 WeekDay
GetWeekDay(const TimeZone
& tz
= Local
) const
882 { return GetTm(tz
).GetWeekDay(); }
883 // get the hour of the day
884 wxDateTime_t
GetHour(const TimeZone
& tz
= Local
) const
885 { return GetTm(tz
).hour
; }
887 wxDateTime_t
GetMinute(const TimeZone
& tz
= Local
) const
888 { return GetTm(tz
).min
; }
890 wxDateTime_t
GetSecond(const TimeZone
& tz
= Local
) const
891 { return GetTm(tz
).sec
; }
893 wxDateTime_t
GetMillisecond(const TimeZone
& tz
= Local
) const
894 { return GetTm(tz
).msec
; }
896 // get the day since the year start (1..366, 0 if date is invalid)
897 wxDateTime_t
GetDayOfYear(const TimeZone
& tz
= Local
) const;
898 // get the week number since the year start (1..52 or 53, 0 if date is
900 wxDateTime_t
GetWeekOfYear(WeekFlags flags
= Monday_First
,
901 const TimeZone
& tz
= Local
) const;
902 // get the week number since the month start (1..5, 0 if date is
904 wxDateTime_t
GetWeekOfMonth(WeekFlags flags
= Monday_First
,
905 const TimeZone
& tz
= Local
) const;
907 // is this date a work day? This depends on a country, of course,
908 // because the holidays are different in different countries
909 bool IsWorkDay(Country country
= Country_Default
) const;
911 // is this date later than Gregorian calendar introduction for the
912 // given country (see enum GregorianAdoption)?
914 // NB: this function shouldn't be considered as absolute authority in
915 // the matter. Besides, for some countries the exact date of
916 // adoption of the Gregorian calendar is simply unknown.
917 bool IsGregorianDate(GregorianAdoption country
= Gr_Standard
) const;
919 // dos date and time format
920 // ------------------------------------------------------------------------
922 // set from the DOS packed format
923 wxDateTime
& SetFromDOS(unsigned long ddt
);
925 // pack the date in DOS format
926 unsigned long GetAsDOS() const;
929 // ------------------------------------------------------------------------
931 // convert SYSTEMTIME to wxDateTime
932 wxDateTime
& SetFromMSWSysTime(const struct _SYSTEMTIME
& st
);
934 // convert wxDateTime to SYSTEMTIME
935 void GetAsMSWSysTime(struct _SYSTEMTIME
* st
) const;
937 // same as above but only take date part into account, time is always zero
938 wxDateTime
& SetFromMSWSysDate(const struct _SYSTEMTIME
& st
);
939 void GetAsMSWSysDate(struct _SYSTEMTIME
* st
) const;
940 #endif // __WINDOWS__
942 // comparison (see also functions below for operator versions)
943 // ------------------------------------------------------------------------
945 // returns true if the two moments are strictly identical
946 inline bool IsEqualTo(const wxDateTime
& datetime
) const;
948 // returns true if the date is strictly earlier than the given one
949 inline bool IsEarlierThan(const wxDateTime
& datetime
) const;
951 // returns true if the date is strictly later than the given one
952 inline bool IsLaterThan(const wxDateTime
& datetime
) const;
954 // returns true if the date is strictly in the given range
955 inline bool IsStrictlyBetween(const wxDateTime
& t1
,
956 const wxDateTime
& t2
) const;
958 // returns true if the date is in the given range
959 inline bool IsBetween(const wxDateTime
& t1
, const wxDateTime
& t2
) const;
961 // do these two objects refer to the same date?
962 inline bool IsSameDate(const wxDateTime
& dt
) const;
964 // do these two objects have the same time?
965 inline bool IsSameTime(const wxDateTime
& dt
) const;
967 // are these two objects equal up to given timespan?
968 inline bool IsEqualUpTo(const wxDateTime
& dt
, const wxTimeSpan
& ts
) const;
970 inline bool operator<(const wxDateTime
& dt
) const
972 wxASSERT_MSG( IsValid() && dt
.IsValid(), wxT("invalid wxDateTime") );
973 return GetValue() < dt
.GetValue();
976 inline bool operator<=(const wxDateTime
& dt
) const
978 wxASSERT_MSG( IsValid() && dt
.IsValid(), wxT("invalid wxDateTime") );
979 return GetValue() <= dt
.GetValue();
982 inline bool operator>(const wxDateTime
& dt
) const
984 wxASSERT_MSG( IsValid() && dt
.IsValid(), wxT("invalid wxDateTime") );
985 return GetValue() > dt
.GetValue();
988 inline bool operator>=(const wxDateTime
& dt
) const
990 wxASSERT_MSG( IsValid() && dt
.IsValid(), wxT("invalid wxDateTime") );
991 return GetValue() >= dt
.GetValue();
994 inline bool operator==(const wxDateTime
& dt
) const
996 wxASSERT_MSG( IsValid() && dt
.IsValid(), wxT("invalid wxDateTime") );
997 return GetValue() == dt
.GetValue();
1000 inline bool operator!=(const wxDateTime
& dt
) const
1002 wxASSERT_MSG( IsValid() && dt
.IsValid(), wxT("invalid wxDateTime") );
1003 return GetValue() != dt
.GetValue();
1006 // arithmetics with dates (see also below for more operators)
1007 // ------------------------------------------------------------------------
1009 // return the sum of the date with a time span (positive or negative)
1010 inline wxDateTime
Add(const wxTimeSpan
& diff
) const;
1011 // add a time span (positive or negative)
1012 inline wxDateTime
& Add(const wxTimeSpan
& diff
);
1013 // add a time span (positive or negative)
1014 inline wxDateTime
& operator+=(const wxTimeSpan
& diff
);
1015 inline wxDateTime
operator+(const wxTimeSpan
& ts
) const
1017 wxDateTime
dt(*this);
1022 // return the difference of the date with a time span
1023 inline wxDateTime
Subtract(const wxTimeSpan
& diff
) const;
1024 // subtract a time span (positive or negative)
1025 inline wxDateTime
& Subtract(const wxTimeSpan
& diff
);
1026 // subtract a time span (positive or negative)
1027 inline wxDateTime
& operator-=(const wxTimeSpan
& diff
);
1028 inline wxDateTime
operator-(const wxTimeSpan
& ts
) const
1030 wxDateTime
dt(*this);
1035 // return the sum of the date with a date span
1036 inline wxDateTime
Add(const wxDateSpan
& diff
) const;
1037 // add a date span (positive or negative)
1038 wxDateTime
& Add(const wxDateSpan
& diff
);
1039 // add a date span (positive or negative)
1040 inline wxDateTime
& operator+=(const wxDateSpan
& diff
);
1041 inline wxDateTime
operator+(const wxDateSpan
& ds
) const
1043 wxDateTime
dt(*this);
1048 // return the difference of the date with a date span
1049 inline wxDateTime
Subtract(const wxDateSpan
& diff
) const;
1050 // subtract a date span (positive or negative)
1051 inline wxDateTime
& Subtract(const wxDateSpan
& diff
);
1052 // subtract a date span (positive or negative)
1053 inline wxDateTime
& operator-=(const wxDateSpan
& diff
);
1054 inline wxDateTime
operator-(const wxDateSpan
& ds
) const
1056 wxDateTime
dt(*this);
1061 // return the difference between two dates
1062 inline wxTimeSpan
Subtract(const wxDateTime
& dt
) const;
1063 inline wxTimeSpan
operator-(const wxDateTime
& dt2
) const;
1065 wxDateSpan
DiffAsDateSpan(const wxDateTime
& dt
) const;
1067 // conversion to/from text
1068 // ------------------------------------------------------------------------
1070 // all conversions functions return true to indicate whether parsing
1071 // succeeded or failed and fill in the provided end iterator, which must
1072 // not be NULL, with the location of the character where the parsing
1073 // stopped (this will be end() of the passed string if everything was
1076 // parse a string in RFC 822 format (found e.g. in mail headers and
1077 // having the form "Wed, 10 Feb 1999 19:07:07 +0100")
1078 bool ParseRfc822Date(const wxString
& date
,
1079 wxString::const_iterator
*end
);
1081 // parse a date/time in the given format (see strptime(3)), fill in
1082 // the missing (in the string) fields with the values of dateDef (by
1083 // default, they will not change if they had valid values or will
1084 // default to Today() otherwise)
1085 bool ParseFormat(const wxString
& date
,
1086 const wxString
& format
,
1087 const wxDateTime
& dateDef
,
1088 wxString::const_iterator
*end
);
1090 bool ParseFormat(const wxString
& date
,
1091 const wxString
& format
,
1092 wxString::const_iterator
*end
)
1094 return ParseFormat(date
, format
, wxDefaultDateTime
, end
);
1097 bool ParseFormat(const wxString
& date
,
1098 wxString::const_iterator
*end
)
1100 return ParseFormat(date
, wxDefaultDateTimeFormat
, wxDefaultDateTime
, end
);
1103 // parse a string containing date, time or both in ISO 8601 format
1105 // notice that these functions are new in wx 3.0 and so we don't
1106 // provide compatibility overloads for them
1107 bool ParseISODate(const wxString
& date
)
1109 wxString::const_iterator end
;
1110 return ParseFormat(date
, wxS("%Y-%m-%d"), &end
) && end
== date
.end();
1113 bool ParseISOTime(const wxString
& time
)
1115 wxString::const_iterator end
;
1116 return ParseFormat(time
, wxS("%H:%M:%S"), &end
) && end
== time
.end();
1119 bool ParseISOCombined(const wxString
& datetime
, char sep
= 'T')
1121 wxString::const_iterator end
;
1122 const wxString fmt
= wxS("%Y-%m-%d") + wxString(sep
) + wxS("%H:%M:%S");
1123 return ParseFormat(datetime
, fmt
, &end
) && end
== datetime
.end();
1126 // parse a string containing the date/time in "free" format, this
1127 // function will try to make an educated guess at the string contents
1128 bool ParseDateTime(const wxString
& datetime
,
1129 wxString::const_iterator
*end
);
1131 // parse a string containing the date only in "free" format (less
1132 // flexible than ParseDateTime)
1133 bool ParseDate(const wxString
& date
,
1134 wxString::const_iterator
*end
);
1136 // parse a string containing the time only in "free" format
1137 bool ParseTime(const wxString
& time
,
1138 wxString::const_iterator
*end
);
1141 // this function accepts strftime()-like format string (default
1142 // argument corresponds to the preferred date and time representation
1143 // for the current locale) and returns the string containing the
1144 // resulting text representation
1145 wxString
Format(const wxString
& format
= wxDefaultDateTimeFormat
,
1146 const TimeZone
& tz
= Local
) const;
1147 // preferred date representation for the current locale
1148 wxString
FormatDate() const { return Format(wxS("%x")); }
1149 // preferred time representation for the current locale
1150 wxString
FormatTime() const { return Format(wxS("%X")); }
1151 // returns the string representing the date in ISO 8601 format
1153 wxString
FormatISODate() const { return Format(wxS("%Y-%m-%d")); }
1154 // returns the string representing the time in ISO 8601 format
1156 wxString
FormatISOTime() const { return Format(wxS("%H:%M:%S")); }
1157 // return the combined date time representation in ISO 8601 format; the
1158 // separator character should be 'T' according to the standard but it
1159 // can also be useful to set it to ' '
1160 wxString
FormatISOCombined(char sep
= 'T') const
1161 { return FormatISODate() + sep
+ FormatISOTime(); }
1164 // backwards compatible versions of the parsing functions: they return an
1165 // object representing the next character following the date specification
1166 // (i.e. the one where the scan had to stop) or a special NULL-like object
1169 // they're not deprecated because a lot of existing code uses them and
1170 // there is no particular harm in keeping them but you should still prefer
1171 // the versions above in the new code
1172 wxAnyStrPtr
ParseRfc822Date(const wxString
& date
)
1174 wxString::const_iterator end
;
1175 return ParseRfc822Date(date
, &end
) ? wxAnyStrPtr(date
, end
)
1179 wxAnyStrPtr
ParseFormat(const wxString
& date
,
1180 const wxString
& format
= wxDefaultDateTimeFormat
,
1181 const wxDateTime
& dateDef
= wxDefaultDateTime
)
1183 wxString::const_iterator end
;
1184 return ParseFormat(date
, format
, dateDef
, &end
) ? wxAnyStrPtr(date
, end
)
1188 wxAnyStrPtr
ParseDateTime(const wxString
& datetime
)
1190 wxString::const_iterator end
;
1191 return ParseDateTime(datetime
, &end
) ? wxAnyStrPtr(datetime
, end
)
1195 wxAnyStrPtr
ParseDate(const wxString
& date
)
1197 wxString::const_iterator end
;
1198 return ParseDate(date
, &end
) ? wxAnyStrPtr(date
, end
)
1202 wxAnyStrPtr
ParseTime(const wxString
& time
)
1204 wxString::const_iterator end
;
1205 return ParseTime(time
, &end
) ? wxAnyStrPtr(time
, end
)
1209 // In addition to wxAnyStrPtr versions above we also must provide the
1210 // overloads for C strings as we must return a pointer into the original
1211 // string and not inside a temporary wxString which would have been created
1212 // if the overloads above were used.
1214 // And then we also have to provide the overloads for wxCStrData, as usual.
1215 // Unfortunately those ones can't return anything as we don't have any
1216 // sufficiently long-lived wxAnyStrPtr to return from them: any temporary
1217 // strings it would point to would be destroyed when this function returns
1218 // making it impossible to dereference the return value. So we just don't
1219 // return anything from here which at least allows to keep compatibility
1220 // with the code not testing the return value. Other uses of this method
1221 // need to be converted to use one of the new bool-returning overloads
1223 void ParseRfc822Date(const wxCStrData
& date
)
1224 { ParseRfc822Date(wxString(date
)); }
1225 const char* ParseRfc822Date(const char* date
);
1226 const wchar_t* ParseRfc822Date(const wchar_t* date
);
1228 void ParseFormat(const wxCStrData
& date
,
1229 const wxString
& format
= wxDefaultDateTimeFormat
,
1230 const wxDateTime
& dateDef
= wxDefaultDateTime
)
1231 { ParseFormat(wxString(date
), format
, dateDef
); }
1232 const char* ParseFormat(const char* date
,
1233 const wxString
& format
= wxDefaultDateTimeFormat
,
1234 const wxDateTime
& dateDef
= wxDefaultDateTime
);
1235 const wchar_t* ParseFormat(const wchar_t* date
,
1236 const wxString
& format
= wxDefaultDateTimeFormat
,
1237 const wxDateTime
& dateDef
= wxDefaultDateTime
);
1239 void ParseDateTime(const wxCStrData
& datetime
)
1240 { ParseDateTime(wxString(datetime
)); }
1241 const char* ParseDateTime(const char* datetime
);
1242 const wchar_t* ParseDateTime(const wchar_t* datetime
);
1244 void ParseDate(const wxCStrData
& date
)
1245 { ParseDate(wxString(date
)); }
1246 const char* ParseDate(const char* date
);
1247 const wchar_t* ParseDate(const wchar_t* date
);
1249 void ParseTime(const wxCStrData
& time
)
1250 { ParseTime(wxString(time
)); }
1251 const char* ParseTime(const char* time
);
1252 const wchar_t* ParseTime(const wchar_t* time
);
1256 // ------------------------------------------------------------------------
1258 // construct from internal representation
1259 wxDateTime(const wxLongLong
& time
) { m_time
= time
; }
1261 // get the internal representation
1262 inline wxLongLong
GetValue() const;
1264 // a helper function to get the current time_t
1265 static time_t GetTimeNow() { return time(NULL
); }
1267 // another one to get the current time broken down
1268 static struct tm
*GetTmNow()
1270 static struct tm l_CurrentTime
;
1271 return GetTmNow(&l_CurrentTime
);
1274 // get current time using thread-safe function
1275 static struct tm
*GetTmNow(struct tm
*tmstruct
);
1278 // the current country - as it's the same for all program objects (unless
1279 // it runs on a _really_ big cluster system :-), this is a static member:
1280 // see SetCountry() and GetCountry()
1281 static Country ms_country
;
1283 // this constant is used to transform a time_t value to the internal
1284 // representation, as time_t is in seconds and we use milliseconds it's
1286 static const long TIME_T_FACTOR
;
1288 // returns true if we fall in range in which we can use standard ANSI C
1290 inline bool IsInStdRange() const;
1292 // the internal representation of the time is the amount of milliseconds
1293 // elapsed since the origin which is set by convention to the UNIX/C epoch
1294 // value: the midnight of January 1, 1970 (UTC)
1298 // ----------------------------------------------------------------------------
1299 // This class contains a difference between 2 wxDateTime values, so it makes
1300 // sense to add it to wxDateTime and it is the result of subtraction of 2
1301 // objects of that class. See also wxDateSpan.
1302 // ----------------------------------------------------------------------------
1304 class WXDLLIMPEXP_BASE wxTimeSpan
1308 // ------------------------------------------------------------------------
1310 // return the timespan for the given number of milliseconds
1311 static wxTimeSpan
Milliseconds(wxLongLong ms
) { return wxTimeSpan(0, 0, 0, ms
); }
1312 static wxTimeSpan
Millisecond() { return Milliseconds(1); }
1314 // return the timespan for the given number of seconds
1315 static wxTimeSpan
Seconds(wxLongLong sec
) { return wxTimeSpan(0, 0, sec
); }
1316 static wxTimeSpan
Second() { return Seconds(1); }
1318 // return the timespan for the given number of minutes
1319 static wxTimeSpan
Minutes(long min
) { return wxTimeSpan(0, min
, 0 ); }
1320 static wxTimeSpan
Minute() { return Minutes(1); }
1322 // return the timespan for the given number of hours
1323 static wxTimeSpan
Hours(long hours
) { return wxTimeSpan(hours
, 0, 0); }
1324 static wxTimeSpan
Hour() { return Hours(1); }
1326 // return the timespan for the given number of days
1327 static wxTimeSpan
Days(long days
) { return Hours(24 * days
); }
1328 static wxTimeSpan
Day() { return Days(1); }
1330 // return the timespan for the given number of weeks
1331 static wxTimeSpan
Weeks(long days
) { return Days(7 * days
); }
1332 static wxTimeSpan
Week() { return Weeks(1); }
1334 // default ctor constructs the 0 time span
1337 // from separate values for each component, date set to 0 (hours are
1338 // not restricted to 0..24 range, neither are minutes, seconds or
1340 inline wxTimeSpan(long hours
,
1342 wxLongLong seconds
= 0,
1343 wxLongLong milliseconds
= 0);
1345 // default copy ctor is ok
1349 // arithmetics with time spans (see also below for more operators)
1350 // ------------------------------------------------------------------------
1352 // return the sum of two timespans
1353 inline wxTimeSpan
Add(const wxTimeSpan
& diff
) const;
1354 // add two timespans together
1355 inline wxTimeSpan
& Add(const wxTimeSpan
& diff
);
1356 // add two timespans together
1357 wxTimeSpan
& operator+=(const wxTimeSpan
& diff
) { return Add(diff
); }
1358 inline wxTimeSpan
operator+(const wxTimeSpan
& ts
) const
1360 return wxTimeSpan(GetValue() + ts
.GetValue());
1363 // return the difference of two timespans
1364 inline wxTimeSpan
Subtract(const wxTimeSpan
& diff
) const;
1365 // subtract another timespan
1366 inline wxTimeSpan
& Subtract(const wxTimeSpan
& diff
);
1367 // subtract another timespan
1368 wxTimeSpan
& operator-=(const wxTimeSpan
& diff
) { return Subtract(diff
); }
1369 inline wxTimeSpan
operator-(const wxTimeSpan
& ts
)
1371 return wxTimeSpan(GetValue() - ts
.GetValue());
1374 // multiply timespan by a scalar
1375 inline wxTimeSpan
Multiply(int n
) const;
1376 // multiply timespan by a scalar
1377 inline wxTimeSpan
& Multiply(int n
);
1378 // multiply timespan by a scalar
1379 wxTimeSpan
& operator*=(int n
) { return Multiply(n
); }
1380 inline wxTimeSpan
operator*(int n
) const
1382 return wxTimeSpan(*this).Multiply(n
);
1385 // return this timespan with opposite sign
1386 wxTimeSpan
Negate() const { return wxTimeSpan(-GetValue()); }
1387 // negate the value of the timespan
1388 wxTimeSpan
& Neg() { m_diff
= -GetValue(); return *this; }
1389 // negate the value of the timespan
1390 wxTimeSpan
& operator-() { return Neg(); }
1392 // return the absolute value of the timespan: does _not_ modify the
1394 inline wxTimeSpan
Abs() const;
1396 // there is intentionally no division because we don't want to
1397 // introduce rounding errors in time calculations
1399 // comparaison (see also operator versions below)
1400 // ------------------------------------------------------------------------
1402 // is the timespan null?
1403 bool IsNull() const { return m_diff
== 0l; }
1404 // returns true if the timespan is null
1405 bool operator!() const { return !IsNull(); }
1407 // is the timespan positive?
1408 bool IsPositive() const { return m_diff
> 0l; }
1410 // is the timespan negative?
1411 bool IsNegative() const { return m_diff
< 0l; }
1413 // are two timespans equal?
1414 inline bool IsEqualTo(const wxTimeSpan
& ts
) const;
1415 // compare two timestamps: works with the absolute values, i.e. -2
1416 // hours is longer than 1 hour. Also, it will return false if the
1417 // timespans are equal in absolute value.
1418 inline bool IsLongerThan(const wxTimeSpan
& ts
) const;
1419 // compare two timestamps: works with the absolute values, i.e. 1
1420 // hour is shorter than -2 hours. Also, it will return false if the
1421 // timespans are equal in absolute value.
1422 bool IsShorterThan(const wxTimeSpan
& t
) const;
1424 inline bool operator<(const wxTimeSpan
&ts
) const
1426 return GetValue() < ts
.GetValue();
1429 inline bool operator<=(const wxTimeSpan
&ts
) const
1431 return GetValue() <= ts
.GetValue();
1434 inline bool operator>(const wxTimeSpan
&ts
) const
1436 return GetValue() > ts
.GetValue();
1439 inline bool operator>=(const wxTimeSpan
&ts
) const
1441 return GetValue() >= ts
.GetValue();
1444 inline bool operator==(const wxTimeSpan
&ts
) const
1446 return GetValue() == ts
.GetValue();
1449 inline bool operator!=(const wxTimeSpan
&ts
) const
1451 return GetValue() != ts
.GetValue();
1454 // breaking into days, hours, minutes and seconds
1455 // ------------------------------------------------------------------------
1457 // get the max number of weeks in this timespan
1458 inline int GetWeeks() const;
1459 // get the max number of days in this timespan
1460 inline int GetDays() const;
1461 // get the max number of hours in this timespan
1462 inline int GetHours() const;
1463 // get the max number of minutes in this timespan
1464 inline int GetMinutes() const;
1465 // get the max number of seconds in this timespan
1466 inline wxLongLong
GetSeconds() const;
1467 // get the number of milliseconds in this timespan
1468 wxLongLong
GetMilliseconds() const { return m_diff
; }
1470 // conversion to text
1471 // ------------------------------------------------------------------------
1473 // this function accepts strftime()-like format string (default
1474 // argument corresponds to the preferred date and time representation
1475 // for the current locale) and returns the string containing the
1476 // resulting text representation. Notice that only some of format
1477 // specifiers valid for wxDateTime are valid for wxTimeSpan: hours,
1478 // minutes and seconds make sense, but not "PM/AM" string for example.
1479 wxString
Format(const wxString
& format
= wxDefaultTimeSpanFormat
) const;
1482 // ------------------------------------------------------------------------
1484 // construct from internal representation
1485 wxTimeSpan(const wxLongLong
& diff
) { m_diff
= diff
; }
1487 // get the internal representation
1488 wxLongLong
GetValue() const { return m_diff
; }
1491 // the (signed) time span in milliseconds
1495 // ----------------------------------------------------------------------------
1496 // This class is a "logical time span" and is useful for implementing program
1497 // logic for such things as "add one month to the date" which, in general,
1498 // doesn't mean to add 60*60*24*31 seconds to it, but to take the same date
1499 // the next month (to understand that this is indeed different consider adding
1500 // one month to Feb, 15 - we want to get Mar, 15, of course).
1502 // When adding a month to the date, all lesser components (days, hours, ...)
1503 // won't be changed unless the resulting date would be invalid: for example,
1504 // Jan 31 + 1 month will be Feb 28, not (non existing) Feb 31.
1506 // Because of this feature, adding and subtracting back again the same
1507 // wxDateSpan will *not*, in general give back the original date: Feb 28 - 1
1508 // month will be Jan 28, not Jan 31!
1510 // wxDateSpan can be either positive or negative. They may be
1511 // multiplied by scalars which multiply all deltas by the scalar: i.e. 2*(1
1512 // month and 1 day) is 2 months and 2 days. They can be added together and
1513 // with wxDateTime or wxTimeSpan, but the type of result is different for each
1516 // Beware about weeks: if you specify both weeks and days, the total number of
1517 // days added will be 7*weeks + days! See also GetTotalDays() function.
1519 // Equality operators are defined for wxDateSpans. Two datespans are equal if
1520 // they both give the same target date when added to *every* source date.
1521 // Thus wxDateSpan::Months(1) is not equal to wxDateSpan::Days(30), because
1522 // they not give the same date when added to 1 Feb. But wxDateSpan::Days(14) is
1523 // equal to wxDateSpan::Weeks(2)
1525 // Finally, notice that for adding hours, minutes &c you don't need this
1526 // class: wxTimeSpan will do the job because there are no subtleties
1527 // associated with those.
1528 // ----------------------------------------------------------------------------
1530 class WXDLLIMPEXP_BASE wxDateSpan
1534 // ------------------------------------------------------------------------
1536 // this many years/months/weeks/days
1537 wxDateSpan(int years
= 0, int months
= 0, int weeks
= 0, int days
= 0)
1545 // get an object for the given number of days
1546 static wxDateSpan
Days(int days
) { return wxDateSpan(0, 0, 0, days
); }
1547 static wxDateSpan
Day() { return Days(1); }
1549 // get an object for the given number of weeks
1550 static wxDateSpan
Weeks(int weeks
) { return wxDateSpan(0, 0, weeks
, 0); }
1551 static wxDateSpan
Week() { return Weeks(1); }
1553 // get an object for the given number of months
1554 static wxDateSpan
Months(int mon
) { return wxDateSpan(0, mon
, 0, 0); }
1555 static wxDateSpan
Month() { return Months(1); }
1557 // get an object for the given number of years
1558 static wxDateSpan
Years(int years
) { return wxDateSpan(years
, 0, 0, 0); }
1559 static wxDateSpan
Year() { return Years(1); }
1561 // default copy ctor is ok
1565 // accessors (all SetXXX() return the (modified) wxDateSpan object)
1566 // ------------------------------------------------------------------------
1568 // set number of years
1569 wxDateSpan
& SetYears(int n
) { m_years
= n
; return *this; }
1570 // set number of months
1571 wxDateSpan
& SetMonths(int n
) { m_months
= n
; return *this; }
1572 // set number of weeks
1573 wxDateSpan
& SetWeeks(int n
) { m_weeks
= n
; return *this; }
1574 // set number of days
1575 wxDateSpan
& SetDays(int n
) { m_days
= n
; return *this; }
1577 // get number of years
1578 int GetYears() const { return m_years
; }
1579 // get number of months
1580 int GetMonths() const { return m_months
; }
1581 // returns 12*GetYears() + GetMonths()
1582 int GetTotalMonths() const { return 12*m_years
+ m_months
; }
1583 // get number of weeks
1584 int GetWeeks() const { return m_weeks
; }
1585 // get number of days
1586 int GetDays() const { return m_days
; }
1587 // returns 7*GetWeeks() + GetDays()
1588 int GetTotalDays() const { return 7*m_weeks
+ m_days
; }
1590 // arithmetics with date spans (see also below for more operators)
1591 // ------------------------------------------------------------------------
1593 // return sum of two date spans
1594 inline wxDateSpan
Add(const wxDateSpan
& other
) const;
1595 // add another wxDateSpan to us
1596 inline wxDateSpan
& Add(const wxDateSpan
& other
);
1597 // add another wxDateSpan to us
1598 inline wxDateSpan
& operator+=(const wxDateSpan
& other
);
1599 inline wxDateSpan
operator+(const wxDateSpan
& ds
) const
1601 return wxDateSpan(GetYears() + ds
.GetYears(),
1602 GetMonths() + ds
.GetMonths(),
1603 GetWeeks() + ds
.GetWeeks(),
1604 GetDays() + ds
.GetDays());
1607 // return difference of two date spans
1608 inline wxDateSpan
Subtract(const wxDateSpan
& other
) const;
1609 // subtract another wxDateSpan from us
1610 inline wxDateSpan
& Subtract(const wxDateSpan
& other
);
1611 // subtract another wxDateSpan from us
1612 inline wxDateSpan
& operator-=(const wxDateSpan
& other
);
1613 inline wxDateSpan
operator-(const wxDateSpan
& ds
) const
1615 return wxDateSpan(GetYears() - ds
.GetYears(),
1616 GetMonths() - ds
.GetMonths(),
1617 GetWeeks() - ds
.GetWeeks(),
1618 GetDays() - ds
.GetDays());
1621 // return a copy of this time span with changed sign
1622 inline wxDateSpan
Negate() const;
1623 // inverse the sign of this timespan
1624 inline wxDateSpan
& Neg();
1625 // inverse the sign of this timespan
1626 wxDateSpan
& operator-() { return Neg(); }
1628 // return the date span proportional to this one with given factor
1629 inline wxDateSpan
Multiply(int factor
) const;
1630 // multiply all components by a (signed) number
1631 inline wxDateSpan
& Multiply(int factor
);
1632 // multiply all components by a (signed) number
1633 inline wxDateSpan
& operator*=(int factor
) { return Multiply(factor
); }
1634 inline wxDateSpan
operator*(int n
) const
1636 return wxDateSpan(*this).Multiply(n
);
1639 // ds1 == d2 if and only if for every wxDateTime t t + ds1 == t + ds2
1640 inline bool operator==(const wxDateSpan
& ds
) const
1642 return GetYears() == ds
.GetYears() &&
1643 GetMonths() == ds
.GetMonths() &&
1644 GetTotalDays() == ds
.GetTotalDays();
1647 inline bool operator!=(const wxDateSpan
& ds
) const
1649 return !(*this == ds
);
1659 // ----------------------------------------------------------------------------
1660 // wxDateTimeArray: array of dates.
1661 // ----------------------------------------------------------------------------
1663 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxDateTime
, wxDateTimeArray
, WXDLLIMPEXP_BASE
);
1665 // ----------------------------------------------------------------------------
1666 // wxDateTimeHolidayAuthority: an object of this class will decide whether a
1667 // given date is a holiday and is used by all functions working with "work
1670 // NB: the base class is an ABC, derived classes must implement the pure
1671 // virtual methods to work with the holidays they correspond to.
1672 // ----------------------------------------------------------------------------
1674 class WXDLLIMPEXP_FWD_BASE wxDateTimeHolidayAuthority
;
1675 WX_DEFINE_USER_EXPORTED_ARRAY_PTR(wxDateTimeHolidayAuthority
*,
1676 wxHolidayAuthoritiesArray
,
1677 class WXDLLIMPEXP_BASE
);
1679 class wxDateTimeHolidaysModule
;
1680 class WXDLLIMPEXP_BASE wxDateTimeHolidayAuthority
1682 friend class wxDateTimeHolidaysModule
;
1684 // returns true if the given date is a holiday
1685 static bool IsHoliday(const wxDateTime
& dt
);
1687 // fills the provided array with all holidays in the given range, returns
1688 // the number of them
1689 static size_t GetHolidaysInRange(const wxDateTime
& dtStart
,
1690 const wxDateTime
& dtEnd
,
1691 wxDateTimeArray
& holidays
);
1693 // clear the list of holiday authorities
1694 static void ClearAllAuthorities();
1696 // add a new holiday authority (the pointer will be deleted by
1697 // wxDateTimeHolidayAuthority)
1698 static void AddAuthority(wxDateTimeHolidayAuthority
*auth
);
1700 // the base class must have a virtual dtor
1701 virtual ~wxDateTimeHolidayAuthority();
1704 // this function is called to determine whether a given day is a holiday
1705 virtual bool DoIsHoliday(const wxDateTime
& dt
) const = 0;
1707 // this function should fill the array with all holidays between the two
1708 // given dates - it is implemented in the base class, but in a very
1709 // inefficient way (it just iterates over all days and uses IsHoliday() for
1710 // each of them), so it must be overridden in the derived class where the
1711 // base class version may be explicitly used if needed
1713 // returns the number of holidays in the given range and fills holidays
1715 virtual size_t DoGetHolidaysInRange(const wxDateTime
& dtStart
,
1716 const wxDateTime
& dtEnd
,
1717 wxDateTimeArray
& holidays
) const = 0;
1720 // all holiday authorities
1721 static wxHolidayAuthoritiesArray ms_authorities
;
1724 // the holidays for this class are all Saturdays and Sundays
1725 class WXDLLIMPEXP_BASE wxDateTimeWorkDays
: public wxDateTimeHolidayAuthority
1728 virtual bool DoIsHoliday(const wxDateTime
& dt
) const;
1729 virtual size_t DoGetHolidaysInRange(const wxDateTime
& dtStart
,
1730 const wxDateTime
& dtEnd
,
1731 wxDateTimeArray
& holidays
) const;
1734 // ============================================================================
1735 // inline functions implementation
1736 // ============================================================================
1738 // ----------------------------------------------------------------------------
1740 // ----------------------------------------------------------------------------
1742 #define MILLISECONDS_PER_DAY 86400000l
1744 // some broken compilers (HP-UX CC) refuse to compile the "normal" version, but
1745 // using a temp variable always might prevent other compilers from optimising
1746 // it away - hence use of this ugly macro
1748 #define MODIFY_AND_RETURN(op) return wxDateTime(*this).op
1750 #define MODIFY_AND_RETURN(op) wxDateTime dt(*this); dt.op; return dt
1753 // ----------------------------------------------------------------------------
1754 // wxDateTime construction
1755 // ----------------------------------------------------------------------------
1757 inline bool wxDateTime::IsInStdRange() const
1759 // currently we don't know what is the real type of time_t so prefer to err
1760 // on the safe side and limit it to 32 bit values which is safe everywhere
1761 return m_time
>= 0l && (m_time
/ TIME_T_FACTOR
) < wxINT32_MAX
;
1765 inline wxDateTime
wxDateTime::Now()
1768 return wxDateTime(*GetTmNow(&tmstruct
));
1772 inline wxDateTime
wxDateTime::Today()
1774 wxDateTime
dt(Now());
1780 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
1781 inline wxDateTime
& wxDateTime::Set(time_t timet
)
1783 if ( timet
== (time_t)-1 )
1785 m_time
= wxInvalidDateTime
.m_time
;
1789 // assign first to avoid long multiplication overflow!
1790 m_time
= timet
- WX_TIME_BASE_OFFSET
;
1791 m_time
*= TIME_T_FACTOR
;
1798 inline wxDateTime
& wxDateTime::SetToCurrent()
1804 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
1805 inline wxDateTime::wxDateTime(time_t timet
)
1811 inline wxDateTime::wxDateTime(const struct tm
& tm
)
1816 inline wxDateTime::wxDateTime(const Tm
& tm
)
1821 inline wxDateTime::wxDateTime(double jdn
)
1826 inline wxDateTime
& wxDateTime::Set(const Tm
& tm
)
1828 wxASSERT_MSG( tm
.IsValid(), wxT("invalid broken down date/time") );
1830 return Set(tm
.mday
, (Month
)tm
.mon
, tm
.year
,
1831 tm
.hour
, tm
.min
, tm
.sec
, tm
.msec
);
1834 inline wxDateTime::wxDateTime(wxDateTime_t hour
,
1835 wxDateTime_t minute
,
1836 wxDateTime_t second
,
1837 wxDateTime_t millisec
)
1839 Set(hour
, minute
, second
, millisec
);
1842 inline wxDateTime::wxDateTime(wxDateTime_t day
,
1846 wxDateTime_t minute
,
1847 wxDateTime_t second
,
1848 wxDateTime_t millisec
)
1850 Set(day
, month
, year
, hour
, minute
, second
, millisec
);
1853 // ----------------------------------------------------------------------------
1854 // wxDateTime accessors
1855 // ----------------------------------------------------------------------------
1857 inline wxLongLong
wxDateTime::GetValue() const
1859 wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime"));
1864 inline time_t wxDateTime::GetTicks() const
1866 wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime"));
1867 if ( !IsInStdRange() )
1872 return (time_t)((m_time
/ (long)TIME_T_FACTOR
).ToLong()) + WX_TIME_BASE_OFFSET
;
1875 inline bool wxDateTime::SetToLastWeekDay(WeekDay weekday
,
1879 return SetToWeekDay(weekday
, -1, month
, year
);
1883 wxDateTime::GetWeekDayInSameWeek(WeekDay weekday
,
1884 WeekFlags
WXUNUSED(flags
)) const
1886 MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday
) );
1889 inline wxDateTime
wxDateTime::GetNextWeekDay(WeekDay weekday
) const
1891 MODIFY_AND_RETURN( SetToNextWeekDay(weekday
) );
1894 inline wxDateTime
wxDateTime::GetPrevWeekDay(WeekDay weekday
) const
1896 MODIFY_AND_RETURN( SetToPrevWeekDay(weekday
) );
1899 inline wxDateTime
wxDateTime::GetWeekDay(WeekDay weekday
,
1904 wxDateTime
dt(*this);
1906 return dt
.SetToWeekDay(weekday
, n
, month
, year
) ? dt
: wxInvalidDateTime
;
1909 inline wxDateTime
wxDateTime::GetLastWeekDay(WeekDay weekday
,
1913 wxDateTime
dt(*this);
1915 return dt
.SetToLastWeekDay(weekday
, month
, year
) ? dt
: wxInvalidDateTime
;
1918 inline wxDateTime
wxDateTime::GetLastMonthDay(Month month
, int year
) const
1920 MODIFY_AND_RETURN( SetToLastMonthDay(month
, year
) );
1923 inline wxDateTime
wxDateTime::GetYearDay(wxDateTime_t yday
) const
1925 MODIFY_AND_RETURN( SetToYearDay(yday
) );
1928 // ----------------------------------------------------------------------------
1929 // wxDateTime comparison
1930 // ----------------------------------------------------------------------------
1932 inline bool wxDateTime::IsEqualTo(const wxDateTime
& datetime
) const
1934 wxASSERT_MSG( IsValid() && datetime
.IsValid(), wxT("invalid wxDateTime"));
1936 return m_time
== datetime
.m_time
;
1939 inline bool wxDateTime::IsEarlierThan(const wxDateTime
& datetime
) const
1941 wxASSERT_MSG( IsValid() && datetime
.IsValid(), wxT("invalid wxDateTime"));
1943 return m_time
< datetime
.m_time
;
1946 inline bool wxDateTime::IsLaterThan(const wxDateTime
& datetime
) const
1948 wxASSERT_MSG( IsValid() && datetime
.IsValid(), wxT("invalid wxDateTime"));
1950 return m_time
> datetime
.m_time
;
1953 inline bool wxDateTime::IsStrictlyBetween(const wxDateTime
& t1
,
1954 const wxDateTime
& t2
) const
1956 // no need for assert, will be checked by the functions we call
1957 return IsLaterThan(t1
) && IsEarlierThan(t2
);
1960 inline bool wxDateTime::IsBetween(const wxDateTime
& t1
,
1961 const wxDateTime
& t2
) const
1963 // no need for assert, will be checked by the functions we call
1964 return IsEqualTo(t1
) || IsEqualTo(t2
) || IsStrictlyBetween(t1
, t2
);
1967 inline bool wxDateTime::IsSameDate(const wxDateTime
& dt
) const
1972 return tm1
.year
== tm2
.year
&&
1973 tm1
.mon
== tm2
.mon
&&
1974 tm1
.mday
== tm2
.mday
;
1977 inline bool wxDateTime::IsSameTime(const wxDateTime
& dt
) const
1979 // notice that we can't do something like this:
1981 // m_time % MILLISECONDS_PER_DAY == dt.m_time % MILLISECONDS_PER_DAY
1983 // because we have also to deal with (possibly) different DST settings!
1987 return tm1
.hour
== tm2
.hour
&&
1988 tm1
.min
== tm2
.min
&&
1989 tm1
.sec
== tm2
.sec
&&
1990 tm1
.msec
== tm2
.msec
;
1993 inline bool wxDateTime::IsEqualUpTo(const wxDateTime
& dt
,
1994 const wxTimeSpan
& ts
) const
1996 return IsBetween(dt
.Subtract(ts
), dt
.Add(ts
));
1999 // ----------------------------------------------------------------------------
2000 // wxDateTime arithmetics
2001 // ----------------------------------------------------------------------------
2003 inline wxDateTime
wxDateTime::Add(const wxTimeSpan
& diff
) const
2005 wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime"));
2007 return wxDateTime(m_time
+ diff
.GetValue());
2010 inline wxDateTime
& wxDateTime::Add(const wxTimeSpan
& diff
)
2012 wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime"));
2014 m_time
+= diff
.GetValue();
2019 inline wxDateTime
& wxDateTime::operator+=(const wxTimeSpan
& diff
)
2024 inline wxDateTime
wxDateTime::Subtract(const wxTimeSpan
& diff
) const
2026 wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime"));
2028 return wxDateTime(m_time
- diff
.GetValue());
2031 inline wxDateTime
& wxDateTime::Subtract(const wxTimeSpan
& diff
)
2033 wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime"));
2035 m_time
-= diff
.GetValue();
2040 inline wxDateTime
& wxDateTime::operator-=(const wxTimeSpan
& diff
)
2042 return Subtract(diff
);
2045 inline wxTimeSpan
wxDateTime::Subtract(const wxDateTime
& datetime
) const
2047 wxASSERT_MSG( IsValid() && datetime
.IsValid(), wxT("invalid wxDateTime"));
2049 return wxTimeSpan(GetValue() - datetime
.GetValue());
2052 inline wxTimeSpan
wxDateTime::operator-(const wxDateTime
& dt2
) const
2054 return this->Subtract(dt2
);
2057 inline wxDateTime
wxDateTime::Add(const wxDateSpan
& diff
) const
2059 return wxDateTime(*this).Add(diff
);
2062 inline wxDateTime
& wxDateTime::Subtract(const wxDateSpan
& diff
)
2064 return Add(diff
.Negate());
2067 inline wxDateTime
wxDateTime::Subtract(const wxDateSpan
& diff
) const
2069 return wxDateTime(*this).Subtract(diff
);
2072 inline wxDateTime
& wxDateTime::operator-=(const wxDateSpan
& diff
)
2074 return Subtract(diff
);
2077 inline wxDateTime
& wxDateTime::operator+=(const wxDateSpan
& diff
)
2082 // ----------------------------------------------------------------------------
2083 // wxDateTime and timezones
2084 // ----------------------------------------------------------------------------
2087 wxDateTime::ToTimezone(const wxDateTime::TimeZone
& tz
, bool noDST
) const
2089 MODIFY_AND_RETURN( MakeTimezone(tz
, noDST
) );
2093 wxDateTime::FromTimezone(const wxDateTime::TimeZone
& tz
, bool noDST
) const
2095 MODIFY_AND_RETURN( MakeFromTimezone(tz
, noDST
) );
2098 // ----------------------------------------------------------------------------
2099 // wxTimeSpan construction
2100 // ----------------------------------------------------------------------------
2102 inline wxTimeSpan::wxTimeSpan(long hours
,
2105 wxLongLong milliseconds
)
2107 // assign first to avoid precision loss
2114 m_diff
+= milliseconds
;
2117 // ----------------------------------------------------------------------------
2118 // wxTimeSpan accessors
2119 // ----------------------------------------------------------------------------
2121 inline wxLongLong
wxTimeSpan::GetSeconds() const
2123 return m_diff
/ 1000l;
2126 inline int wxTimeSpan::GetMinutes() const
2128 // For compatibility, this method (and the other accessors) return int,
2129 // even though GetLo() actually returns unsigned long with greater range.
2130 return static_cast<int>((GetSeconds() / 60l).GetLo());
2133 inline int wxTimeSpan::GetHours() const
2135 return GetMinutes() / 60;
2138 inline int wxTimeSpan::GetDays() const
2140 return GetHours() / 24;
2143 inline int wxTimeSpan::GetWeeks() const
2145 return GetDays() / 7;
2148 // ----------------------------------------------------------------------------
2149 // wxTimeSpan arithmetics
2150 // ----------------------------------------------------------------------------
2152 inline wxTimeSpan
wxTimeSpan::Add(const wxTimeSpan
& diff
) const
2154 return wxTimeSpan(m_diff
+ diff
.GetValue());
2157 inline wxTimeSpan
& wxTimeSpan::Add(const wxTimeSpan
& diff
)
2159 m_diff
+= diff
.GetValue();
2164 inline wxTimeSpan
wxTimeSpan::Subtract(const wxTimeSpan
& diff
) const
2166 return wxTimeSpan(m_diff
- diff
.GetValue());
2169 inline wxTimeSpan
& wxTimeSpan::Subtract(const wxTimeSpan
& diff
)
2171 m_diff
-= diff
.GetValue();
2176 inline wxTimeSpan
& wxTimeSpan::Multiply(int n
)
2183 inline wxTimeSpan
wxTimeSpan::Multiply(int n
) const
2185 return wxTimeSpan(m_diff
* (long)n
);
2188 inline wxTimeSpan
wxTimeSpan::Abs() const
2190 return wxTimeSpan(GetValue().Abs());
2193 inline bool wxTimeSpan::IsEqualTo(const wxTimeSpan
& ts
) const
2195 return GetValue() == ts
.GetValue();
2198 inline bool wxTimeSpan::IsLongerThan(const wxTimeSpan
& ts
) const
2200 return GetValue().Abs() > ts
.GetValue().Abs();
2203 inline bool wxTimeSpan::IsShorterThan(const wxTimeSpan
& ts
) const
2205 return GetValue().Abs() < ts
.GetValue().Abs();
2208 // ----------------------------------------------------------------------------
2210 // ----------------------------------------------------------------------------
2212 inline wxDateSpan
& wxDateSpan::operator+=(const wxDateSpan
& other
)
2214 m_years
+= other
.m_years
;
2215 m_months
+= other
.m_months
;
2216 m_weeks
+= other
.m_weeks
;
2217 m_days
+= other
.m_days
;
2222 inline wxDateSpan
& wxDateSpan::Add(const wxDateSpan
& other
)
2224 return *this += other
;
2227 inline wxDateSpan
wxDateSpan::Add(const wxDateSpan
& other
) const
2229 wxDateSpan
ds(*this);
2234 inline wxDateSpan
& wxDateSpan::Multiply(int factor
)
2244 inline wxDateSpan
wxDateSpan::Multiply(int factor
) const
2246 wxDateSpan
ds(*this);
2247 ds
.Multiply(factor
);
2251 inline wxDateSpan
wxDateSpan::Negate() const
2253 return wxDateSpan(-m_years
, -m_months
, -m_weeks
, -m_days
);
2256 inline wxDateSpan
& wxDateSpan::Neg()
2259 m_months
= -m_months
;
2266 inline wxDateSpan
& wxDateSpan::operator-=(const wxDateSpan
& other
)
2268 return *this += other
.Negate();
2271 inline wxDateSpan
& wxDateSpan::Subtract(const wxDateSpan
& other
)
2273 return *this -= other
;
2276 inline wxDateSpan
wxDateSpan::Subtract(const wxDateSpan
& other
) const
2278 wxDateSpan
ds(*this);
2283 #undef MILLISECONDS_PER_DAY
2285 #undef MODIFY_AND_RETURN
2287 // ============================================================================
2289 // ============================================================================
2291 // ----------------------------------------------------------------------------
2292 // wxTimeSpan operators
2293 // ----------------------------------------------------------------------------
2295 wxTimeSpan WXDLLIMPEXP_BASE
operator*(int n
, const wxTimeSpan
& ts
);
2297 // ----------------------------------------------------------------------------
2299 // ----------------------------------------------------------------------------
2301 wxDateSpan WXDLLIMPEXP_BASE
operator*(int n
, const wxDateSpan
& ds
);
2303 // ============================================================================
2304 // other helper functions
2305 // ============================================================================
2307 // ----------------------------------------------------------------------------
2308 // iteration helpers: can be used to write a for loop over enum variable like
2310 // for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) )
2311 // ----------------------------------------------------------------------------
2313 WXDLLIMPEXP_BASE
void wxNextMonth(wxDateTime::Month
& m
);
2314 WXDLLIMPEXP_BASE
void wxPrevMonth(wxDateTime::Month
& m
);
2315 WXDLLIMPEXP_BASE
void wxNextWDay(wxDateTime::WeekDay
& wd
);
2316 WXDLLIMPEXP_BASE
void wxPrevWDay(wxDateTime::WeekDay
& wd
);
2318 #endif // wxUSE_DATETIME
2320 #endif // _WX_DATETIME_H