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
;
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
55 /* Two wrapper functions for thread safety */
56 #ifdef HAVE_LOCALTIME_R
57 #define wxLocaltime_r localtime_r
59 WXDLLIMPEXP_BASE
struct tm
*wxLocaltime_r(const time_t*, struct tm
*);
60 #if wxUSE_THREADS && !defined(__WINDOWS__) && !defined(__WATCOMC__)
61 // On Windows, localtime _is_ threadsafe!
62 #warning using pseudo thread-safe wrapper for localtime to emulate localtime_r
67 #define wxGmtime_r gmtime_r
69 WXDLLIMPEXP_BASE
struct tm
*wxGmtime_r(const time_t*, struct tm
*);
70 #if wxUSE_THREADS && !defined(__WINDOWS__) && !defined(__WATCOMC__)
71 // On Windows, gmtime _is_ threadsafe!
72 #warning using pseudo thread-safe wrapper for gmtime to emulate gmtime_r
77 The three (main) classes declared in this header represent:
79 1. An absolute moment in the time (wxDateTime)
80 2. A difference between two moments in the time, positive or negative
82 3. A logical difference between two dates expressed in
83 years/months/weeks/days (wxDateSpan)
85 The following arithmetic operations are permitted (all others are not):
90 wxDateTime + wxTimeSpan = wxDateTime
91 wxDateTime + wxDateSpan = wxDateTime
92 wxTimeSpan + wxTimeSpan = wxTimeSpan
93 wxDateSpan + wxDateSpan = wxDateSpan
97 wxDateTime - wxDateTime = wxTimeSpan
98 wxDateTime - wxTimeSpan = wxDateTime
99 wxDateTime - wxDateSpan = wxDateTime
100 wxTimeSpan - wxTimeSpan = wxTimeSpan
101 wxDateSpan - wxDateSpan = wxDateSpan
105 wxTimeSpan * number = wxTimeSpan
106 number * wxTimeSpan = wxTimeSpan
107 wxDateSpan * number = wxDateSpan
108 number * wxDateSpan = wxDateSpan
112 -wxTimeSpan = wxTimeSpan
113 -wxDateSpan = wxDateSpan
115 For each binary operation OP (+, -, *) we have the following operatorOP=() as
116 a method and the method with a symbolic name OPER (Add, Subtract, Multiply)
117 as a synonym for it and another const method with the same name which returns
118 the changed copy of the object and operatorOP() as a global function which is
119 implemented in terms of the const version of OPEN. For the unary - we have
120 operator-() as a method, Neg() as synonym for it and Negate() which returns
121 the copy of the object with the changed sign.
124 // an invalid/default date time object which may be used as the default
125 // argument for arguments of type wxDateTime; it is also returned by all
126 // functions returning wxDateTime on failure (this is why it is also called
127 // wxInvalidDateTime)
128 class WXDLLIMPEXP_FWD_BASE wxDateTime
;
130 extern WXDLLIMPEXP_DATA_BASE(const char *) wxDefaultDateTimeFormat
;
131 extern WXDLLIMPEXP_DATA_BASE(const char *) wxDefaultTimeSpanFormat
;
132 extern WXDLLIMPEXP_DATA_BASE(const wxDateTime
) wxDefaultDateTime
;
134 #define wxInvalidDateTime wxDefaultDateTime
137 // ----------------------------------------------------------------------------
138 // conditional compilation
139 // ----------------------------------------------------------------------------
141 #if defined(HAVE_STRPTIME) && defined(__GLIBC__) && \
142 ((__GLIBC__ == 2) && (__GLIBC_MINOR__ == 0))
143 // glibc 2.0.7 strptime() is broken - the following snippet causes it to
144 // crash (instead of just failing):
146 // strncpy(buf, "Tue Dec 21 20:25:40 1999", 128);
147 // strptime(buf, "%x", &tm);
151 #endif // broken strptime()
153 #if defined(HAVE_STRPTIME) && defined(__DARWIN__) && defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS
154 // configure detects strptime as linkable because it's in the OS X
155 // System library but MSL headers don't declare it.
157 // char *strptime(const char *, const char *, struct tm *);
158 // However, we DON'T want to just provide it here because we would
159 // crash and/or overwrite data when strptime from OS X tries
160 // to fill in MW's struct tm which is two fields shorter (no TZ stuff)
161 // So for now let's just say we don't have strptime
165 // everyone has strftime except Win CE unless VC8 is used
166 #if !defined(__WXWINCE__) || defined(__VISUALC8__)
167 #define HAVE_STRFTIME
170 // ----------------------------------------------------------------------------
171 // wxDateTime represents an absolute moment in the time
172 // ----------------------------------------------------------------------------
174 class WXDLLIMPEXP_BASE wxDateTime
178 // ------------------------------------------------------------------------
180 // a small unsigned integer type for storing things like minutes,
181 // seconds &c. It should be at least short (i.e. not char) to contain
182 // the number of milliseconds - it may also be 'int' because there is
183 // no size penalty associated with it in our code, we don't store any
184 // data in this format
185 typedef unsigned short wxDateTime_t
;
188 // ------------------------------------------------------------------------
193 // the time in the current time zone
196 // zones from GMT (= Greenwhich Mean Time): they're guaranteed to be
197 // consequent numbers, so writing something like `GMT0 + offset' is
198 // safe if abs(offset) <= 12
200 // underscore stands for minus
201 GMT_12
, GMT_11
, GMT_10
, GMT_9
, GMT_8
, GMT_7
,
202 GMT_6
, GMT_5
, GMT_4
, GMT_3
, GMT_2
, GMT_1
,
204 GMT1
, GMT2
, GMT3
, GMT4
, GMT5
, GMT6
,
205 GMT7
, GMT8
, GMT9
, GMT10
, GMT11
, GMT12
, GMT13
,
206 // Note that GMT12 and GMT_12 are not the same: there is a difference
207 // of exactly one day between them
209 // some symbolic names for TZ
212 WET
= GMT0
, // Western Europe Time
213 WEST
= GMT1
, // Western Europe Summer Time
214 CET
= GMT1
, // Central Europe Time
215 CEST
= GMT2
, // Central Europe Summer Time
216 EET
= GMT2
, // Eastern Europe Time
217 EEST
= GMT3
, // Eastern Europe Summer Time
218 MSK
= GMT3
, // Moscow Time
219 MSD
= GMT4
, // Moscow Summer Time
222 AST
= GMT_4
, // Atlantic Standard Time
223 ADT
= GMT_3
, // Atlantic Daylight Time
224 EST
= GMT_5
, // Eastern Standard Time
225 EDT
= GMT_4
, // Eastern Daylight Saving Time
226 CST
= GMT_6
, // Central Standard Time
227 CDT
= GMT_5
, // Central Daylight Saving Time
228 MST
= GMT_7
, // Mountain Standard Time
229 MDT
= GMT_6
, // Mountain Daylight Saving Time
230 PST
= GMT_8
, // Pacific Standard Time
231 PDT
= GMT_7
, // Pacific Daylight Saving Time
232 HST
= GMT_10
, // Hawaiian Standard Time
233 AKST
= GMT_9
, // Alaska Standard Time
234 AKDT
= GMT_8
, // Alaska Daylight Saving Time
238 A_WST
= GMT8
, // Western Standard Time
239 A_CST
= GMT13
+ 1, // Central Standard Time (+9.5)
240 A_EST
= GMT10
, // Eastern Standard Time
241 A_ESST
= GMT11
, // Eastern Summer Time
244 NZST
= GMT12
, // Standard Time
245 NZDT
= GMT13
, // Daylight Saving Time
247 // TODO add more symbolic timezone names here
249 // Universal Coordinated Time = the new and politically correct name
254 // the calendar systems we know about: notice that it's valid (for
255 // this classes purpose anyhow) to work with any of these calendars
256 // even with the dates before the historical appearance of the
260 Gregorian
, // current calendar
261 Julian
// calendar in use since -45 until the 1582 (or later)
263 // TODO Hebrew, Chinese, Maya, ... (just kidding) (or then may be not?)
266 // these values only are used to identify the different dates of
267 // adoption of the Gregorian calendar (see IsGregorian())
269 // All data and comments taken verbatim from "The Calendar FAQ (v 2.0)"
270 // by Claus T�ndering, http://www.pip.dknet.dk/~c-t/calendar.html
271 // except for the comments "we take".
273 // Symbol "->" should be read as "was followed by" in the comments
275 enum GregorianAdoption
277 Gr_Unknown
, // no data for this country or it's too uncertain to use
278 Gr_Standard
, // on the day 0 of Gregorian calendar: 15 Oct 1582
280 Gr_Alaska
, // Oct 1867 when Alaska became part of the USA
281 Gr_Albania
, // Dec 1912
283 Gr_Austria
= Gr_Unknown
, // Different regions on different dates
284 Gr_Austria_Brixen
, // 5 Oct 1583 -> 16 Oct 1583
285 Gr_Austria_Salzburg
= Gr_Austria_Brixen
,
286 Gr_Austria_Tyrol
= Gr_Austria_Brixen
,
287 Gr_Austria_Carinthia
, // 14 Dec 1583 -> 25 Dec 1583
288 Gr_Austria_Styria
= Gr_Austria_Carinthia
,
290 Gr_Belgium
, // Then part of the Netherlands
292 Gr_Bulgaria
= Gr_Unknown
, // Unknown precisely (from 1915 to 1920)
293 Gr_Bulgaria_1
, // 18 Mar 1916 -> 1 Apr 1916
294 Gr_Bulgaria_2
, // 31 Mar 1916 -> 14 Apr 1916
295 Gr_Bulgaria_3
, // 3 Sep 1920 -> 17 Sep 1920
297 Gr_Canada
= Gr_Unknown
, // Different regions followed the changes in
298 // Great Britain or France
300 Gr_China
= Gr_Unknown
, // Different authorities say:
301 Gr_China_1
, // 18 Dec 1911 -> 1 Jan 1912
302 Gr_China_2
, // 18 Dec 1928 -> 1 Jan 1929
304 Gr_Czechoslovakia
, // (Bohemia and Moravia) 6 Jan 1584 -> 17 Jan 1584
305 Gr_Denmark
, // (including Norway) 18 Feb 1700 -> 1 Mar 1700
308 Gr_Finland
, // Then part of Sweden
310 Gr_France
, // 9 Dec 1582 -> 20 Dec 1582
311 Gr_France_Alsace
, // 4 Feb 1682 -> 16 Feb 1682
312 Gr_France_Lorraine
, // 16 Feb 1760 -> 28 Feb 1760
313 Gr_France_Strasbourg
, // February 1682
315 Gr_Germany
= Gr_Unknown
, // Different states on different dates:
316 Gr_Germany_Catholic
, // 1583-1585 (we take 1584)
317 Gr_Germany_Prussia
, // 22 Aug 1610 -> 2 Sep 1610
318 Gr_Germany_Protestant
, // 18 Feb 1700 -> 1 Mar 1700
320 Gr_GreatBritain
, // 2 Sep 1752 -> 14 Sep 1752 (use 'cal(1)')
322 Gr_Greece
, // 9 Mar 1924 -> 23 Mar 1924
323 Gr_Hungary
, // 21 Oct 1587 -> 1 Nov 1587
324 Gr_Ireland
= Gr_GreatBritain
,
325 Gr_Italy
= Gr_Standard
,
327 Gr_Japan
= Gr_Unknown
, // Different authorities say:
328 Gr_Japan_1
, // 19 Dec 1872 -> 1 Jan 1873
329 Gr_Japan_2
, // 19 Dec 1892 -> 1 Jan 1893
330 Gr_Japan_3
, // 18 Dec 1918 -> 1 Jan 1919
332 Gr_Latvia
, // 1915-1918 (we take 1915)
333 Gr_Lithuania
, // 1915
334 Gr_Luxemburg
, // 14 Dec 1582 -> 25 Dec 1582
335 Gr_Netherlands
= Gr_Belgium
, // (including Belgium) 1 Jan 1583
337 // this is too weird to take into account: the Gregorian calendar was
338 // introduced twice in Groningen, first time 28 Feb 1583 was followed
339 // by 11 Mar 1583, then it has gone back to Julian in the summer of
340 // 1584 and then 13 Dec 1700 -> 12 Jan 1701 - which is
341 // the date we take here
342 Gr_Netherlands_Groningen
, // 13 Dec 1700 -> 12 Jan 1701
343 Gr_Netherlands_Gelderland
, // 30 Jun 1700 -> 12 Jul 1700
344 Gr_Netherlands_Utrecht
, // (and Overijssel) 30 Nov 1700->12 Dec 1700
345 Gr_Netherlands_Friesland
, // (and Drenthe) 31 Dec 1700 -> 12 Jan 1701
347 Gr_Norway
= Gr_Denmark
, // Then part of Denmark
348 Gr_Poland
= Gr_Standard
,
349 Gr_Portugal
= Gr_Standard
,
350 Gr_Romania
, // 31 Mar 1919 -> 14 Apr 1919
351 Gr_Russia
, // 31 Jan 1918 -> 14 Feb 1918
352 Gr_Scotland
= Gr_GreatBritain
,
353 Gr_Spain
= Gr_Standard
,
355 // Sweden has a curious history. Sweden decided to make a gradual
356 // change from the Julian to the Gregorian calendar. By dropping every
357 // leap year from 1700 through 1740 the eleven superfluous days would
358 // be omitted and from 1 Mar 1740 they would be in sync with the
359 // Gregorian calendar. (But in the meantime they would be in sync with
362 // So 1700 (which should have been a leap year in the Julian calendar)
363 // was not a leap year in Sweden. However, by mistake 1704 and 1708
364 // became leap years. This left Sweden out of synchronisation with
365 // both the Julian and the Gregorian world, so they decided to go back
366 // to the Julian calendar. In order to do this, they inserted an extra
367 // day in 1712, making that year a double leap year! So in 1712,
368 // February had 30 days in Sweden.
370 // Later, in 1753, Sweden changed to the Gregorian calendar by
371 // dropping 11 days like everyone else.
372 Gr_Sweden
= Gr_Finland
, // 17 Feb 1753 -> 1 Mar 1753
374 Gr_Switzerland
= Gr_Unknown
,// Different cantons used different dates
375 Gr_Switzerland_Catholic
, // 1583, 1584 or 1597 (we take 1584)
376 Gr_Switzerland_Protestant
, // 31 Dec 1700 -> 12 Jan 1701
378 Gr_Turkey
, // 1 Jan 1927
379 Gr_USA
= Gr_GreatBritain
,
380 Gr_Wales
= Gr_GreatBritain
,
381 Gr_Yugoslavia
// 1919
384 // the country parameter is used so far for calculating the start and
385 // the end of DST period and for deciding whether the date is a work
388 // TODO move this to intl.h
390 // Required for WinCE
397 Country_Unknown
, // no special information for this country
398 Country_Default
, // set the default country with SetCountry() method
399 // or use the default country with any other
401 // TODO add more countries (for this we must know about DST and/or
402 // holidays for this country)
404 // Western European countries: we assume that they all follow the same
405 // DST rules (true or false?)
406 Country_WesternEurope_Start
,
407 Country_EEC
= Country_WesternEurope_Start
,
411 Country_WesternEurope_End
= UK
,
416 // symbolic names for the months
419 Jan
, Feb
, Mar
, Apr
, May
, Jun
, Jul
, Aug
, Sep
, Oct
, Nov
, Dec
, Inv_Month
422 // symbolic names for the weekdays
425 Sun
, Mon
, Tue
, Wed
, Thu
, Fri
, Sat
, Inv_WeekDay
428 // invalid value for the year
431 Inv_Year
= SHRT_MIN
// should hold in wxDateTime_t
434 // flags for GetWeekDayName and GetMonthName
437 Name_Full
= 0x01, // return full name
438 Name_Abbr
= 0x02 // return abbreviated name
441 // flags for GetWeekOfYear and GetWeekOfMonth
444 Default_First
, // Sunday_First for US, Monday_First for the rest
445 Monday_First
, // week starts with a Monday
446 Sunday_First
// week starts with a Sunday
450 // ------------------------------------------------------------------------
452 // a class representing a time zone: basicly, this is just an offset
453 // (in seconds) from GMT
454 class WXDLLIMPEXP_BASE TimeZone
459 // create time zone object with the given offset
460 TimeZone(long offset
= 0) { m_offset
= offset
; }
462 static TimeZone
Make(long offset
)
465 tz
.m_offset
= offset
;
469 long GetOffset() const { return m_offset
; }
472 // offset for this timezone from GMT in seconds
476 // standard struct tm is limited to the years from 1900 (because
477 // tm_year field is the offset from 1900), so we use our own struct
478 // instead to represent broken down time
480 // NB: this struct should always be kept normalized (i.e. mon should
481 // be < 12, 1 <= day <= 31 &c), so use AddMonths(), AddDays()
482 // instead of modifying the member fields directly!
483 struct WXDLLIMPEXP_BASE Tm
485 wxDateTime_t msec
, sec
, min
, hour
, mday
;
489 // default ctor inits the object to an invalid value
492 // ctor from struct tm and the timezone
493 Tm(const struct tm
& tm
, const TimeZone
& tz
);
495 // check that the given date/time is valid (in Gregorian calendar)
496 bool IsValid() const;
499 WeekDay
GetWeekDay() // not const because wday may be changed
501 if ( wday
== Inv_WeekDay
)
504 return (WeekDay
)wday
;
507 // add the given number of months to the date keeping it normalized
508 void AddMonths(int monDiff
);
510 // add the given number of months to the date keeping it normalized
511 void AddDays(int dayDiff
);
514 // compute the weekday from other fields
515 void ComputeWeekDay();
517 // the timezone we correspond to
520 // these values can't be accessed directly because they're not always
521 // computed and we calculate them on demand
522 wxDateTime_t wday
, yday
;
526 // ------------------------------------------------------------------------
528 // set the current country
529 static void SetCountry(Country country
);
530 // get the current country
531 static Country
GetCountry();
533 // return true if the country is a West European one (in practice,
534 // this means that the same DST rules as for EEC apply)
535 static bool IsWestEuropeanCountry(Country country
= Country_Default
);
537 // return the current year
538 static int GetCurrentYear(Calendar cal
= Gregorian
);
540 // convert the year as returned by wxDateTime::GetYear() to a year
541 // suitable for BC/AD notation. The difference is that BC year 1
542 // corresponds to the year 0 (while BC year 0 didn't exist) and AD
543 // year N is just year N.
544 static int ConvertYearToBC(int year
);
546 // return the current month
547 static Month
GetCurrentMonth(Calendar cal
= Gregorian
);
549 // returns true if the given year is a leap year in the given calendar
550 static bool IsLeapYear(int year
= Inv_Year
, Calendar cal
= Gregorian
);
552 // get the century (19 for 1999, 20 for 2000 and -5 for 492 BC)
553 static int GetCentury(int year
);
555 // returns the number of days in this year (356 or 355 for Gregorian
556 // calendar usually :-)
557 static wxDateTime_t
GetNumberOfDays(int year
, Calendar cal
= Gregorian
);
559 // get the number of the days in the given month (default value for
560 // the year means the current one)
561 static wxDateTime_t
GetNumberOfDays(Month month
,
563 Calendar cal
= Gregorian
);
565 // get the full (default) or abbreviated month name in the current
566 // locale, returns empty string on error
567 static wxString
GetMonthName(Month month
,
568 NameFlags flags
= Name_Full
);
570 // get the full (default) or abbreviated weekday name in the current
571 // locale, returns empty string on error
572 static wxString
GetWeekDayName(WeekDay weekday
,
573 NameFlags flags
= Name_Full
);
575 // get the AM and PM strings in the current locale (may be empty)
576 static void GetAmPmStrings(wxString
*am
, wxString
*pm
);
578 // return true if the given country uses DST for this year
579 static bool IsDSTApplicable(int year
= Inv_Year
,
580 Country country
= Country_Default
);
582 // get the beginning of DST for this year, will return invalid object
583 // if no DST applicable in this year. The default value of the
584 // parameter means to take the current year.
585 static wxDateTime
GetBeginDST(int year
= Inv_Year
,
586 Country country
= Country_Default
);
587 // get the end of DST for this year, will return invalid object
588 // if no DST applicable in this year. The default value of the
589 // parameter means to take the current year.
590 static wxDateTime
GetEndDST(int year
= Inv_Year
,
591 Country country
= Country_Default
);
593 // return the wxDateTime object for the current time
594 static inline wxDateTime
Now();
596 // return the wxDateTime object for the current time with millisecond
597 // precision (if available on this platform)
598 static wxDateTime
UNow();
600 // return the wxDateTime object for today midnight: i.e. as Now() but
601 // with time set to 0
602 static inline wxDateTime
Today();
604 // constructors: you should test whether the constructor succeeded with
605 // IsValid() function. The values Inv_Month and Inv_Year for the
606 // parameters mean take current month and/or year values.
607 // ------------------------------------------------------------------------
609 // default ctor does not initialize the object, use Set()!
610 wxDateTime() { m_time
= wxLongLong(wxINT32_MIN
, 0); }
612 // from time_t: seconds since the Epoch 00:00:00 UTC, Jan 1, 1970)
613 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
614 // VA C++ confuses this with wxDateTime(double jdn) thinking it is a duplicate declaration
615 inline wxDateTime(time_t timet
);
617 // from broken down time/date (only for standard Unix range)
618 inline wxDateTime(const struct tm
& tm
);
619 // from broken down time/date (any range)
620 inline wxDateTime(const Tm
& tm
);
622 // from JDN (beware of rounding errors)
623 inline wxDateTime(double jdn
);
625 // from separate values for each component, date set to today
626 inline wxDateTime(wxDateTime_t hour
,
627 wxDateTime_t minute
= 0,
628 wxDateTime_t second
= 0,
629 wxDateTime_t millisec
= 0);
630 // from separate values for each component with explicit date
631 inline wxDateTime(wxDateTime_t day
, // day of the month
633 int year
= Inv_Year
, // 1999, not 99 please!
634 wxDateTime_t hour
= 0,
635 wxDateTime_t minute
= 0,
636 wxDateTime_t second
= 0,
637 wxDateTime_t millisec
= 0);
639 wxDateTime(const struct _SYSTEMTIME
& st
)
641 SetFromMSWSysTime(st
);
645 // default copy ctor ok
649 // assignment operators and Set() functions: all non const methods return
650 // the reference to this object. IsValid() should be used to test whether
651 // the function succeeded.
652 // ------------------------------------------------------------------------
654 // set to the current time
655 inline wxDateTime
& SetToCurrent();
657 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
658 // VA C++ confuses this with wxDateTime(double jdn) thinking it is a duplicate declaration
659 // set to given time_t value
660 inline wxDateTime
& Set(time_t timet
);
663 // set to given broken down time/date
664 wxDateTime
& Set(const struct tm
& tm
);
666 // set to given broken down time/date
667 inline wxDateTime
& Set(const Tm
& tm
);
669 // set to given JDN (beware of rounding errors)
670 wxDateTime
& Set(double jdn
);
672 // set to given time, date = today
673 wxDateTime
& Set(wxDateTime_t hour
,
674 wxDateTime_t minute
= 0,
675 wxDateTime_t second
= 0,
676 wxDateTime_t millisec
= 0);
678 // from separate values for each component with explicit date
679 // (defaults for month and year are the current values)
680 wxDateTime
& Set(wxDateTime_t day
,
682 int year
= Inv_Year
, // 1999, not 99 please!
683 wxDateTime_t hour
= 0,
684 wxDateTime_t minute
= 0,
685 wxDateTime_t second
= 0,
686 wxDateTime_t millisec
= 0);
688 // resets time to 00:00:00, doesn't change the date
689 wxDateTime
& ResetTime();
691 // get the date part of this object only, i.e. the object which has the
692 // same date as this one but time of 00:00:00
693 wxDateTime
GetDateOnly() const;
695 // the following functions don't change the values of the other
696 // fields, i.e. SetMinute() won't change either hour or seconds value
699 wxDateTime
& SetYear(int year
);
701 wxDateTime
& SetMonth(Month month
);
702 // set the day of the month
703 wxDateTime
& SetDay(wxDateTime_t day
);
705 wxDateTime
& SetHour(wxDateTime_t hour
);
707 wxDateTime
& SetMinute(wxDateTime_t minute
);
709 wxDateTime
& SetSecond(wxDateTime_t second
);
711 wxDateTime
& SetMillisecond(wxDateTime_t millisecond
);
713 // assignment operator from time_t
714 wxDateTime
& operator=(time_t timet
) { return Set(timet
); }
716 // assignment operator from broken down time/date
717 wxDateTime
& operator=(const struct tm
& tm
) { return Set(tm
); }
719 // assignment operator from broken down time/date
720 wxDateTime
& operator=(const Tm
& tm
) { return Set(tm
); }
722 // default assignment operator is ok
724 // calendar calculations (functions which set the date only leave the time
725 // unchanged, e.g. don't explictly zero it): SetXXX() functions modify the
726 // object itself, GetXXX() ones return a new object.
727 // ------------------------------------------------------------------------
729 // set to the given week day in the same week as this one
730 wxDateTime
& SetToWeekDayInSameWeek(WeekDay weekday
,
731 WeekFlags flags
= Monday_First
);
732 inline wxDateTime
GetWeekDayInSameWeek(WeekDay weekday
,
733 WeekFlags flags
= Monday_First
) const;
735 // set to the next week day following this one
736 wxDateTime
& SetToNextWeekDay(WeekDay weekday
);
737 inline wxDateTime
GetNextWeekDay(WeekDay weekday
) const;
739 // set to the previous week day before this one
740 wxDateTime
& SetToPrevWeekDay(WeekDay weekday
);
741 inline wxDateTime
GetPrevWeekDay(WeekDay weekday
) const;
743 // set to Nth occurrence of given weekday in the given month of the
744 // given year (time is set to 0), return true on success and false on
745 // failure. n may be positive (1..5) or negative to count from the end
746 // of the month (see helper function SetToLastWeekDay())
747 bool SetToWeekDay(WeekDay weekday
,
749 Month month
= Inv_Month
,
750 int year
= Inv_Year
);
751 inline wxDateTime
GetWeekDay(WeekDay weekday
,
753 Month month
= Inv_Month
,
754 int year
= Inv_Year
) const;
756 // sets to the last weekday in the given month, year
757 inline bool SetToLastWeekDay(WeekDay weekday
,
758 Month month
= Inv_Month
,
759 int year
= Inv_Year
);
760 inline wxDateTime
GetLastWeekDay(WeekDay weekday
,
761 Month month
= Inv_Month
,
762 int year
= Inv_Year
);
764 #if WXWIN_COMPATIBILITY_2_6
765 // sets the date to the given day of the given week in the year,
766 // returns true on success and false if given date doesn't exist (e.g.
769 // these functions are badly defined as they're not the reverse of
770 // GetWeekOfYear(), use SetToTheWeekOfYear() instead
771 wxDEPRECATED( bool SetToTheWeek(wxDateTime_t numWeek
,
772 WeekDay weekday
= Mon
,
773 WeekFlags flags
= Monday_First
) );
774 wxDEPRECATED( wxDateTime
GetWeek(wxDateTime_t numWeek
,
775 WeekDay weekday
= Mon
,
776 WeekFlags flags
= Monday_First
) const );
777 #endif // WXWIN_COMPATIBILITY_2_6
779 // returns the date corresponding to the given week day of the given
780 // week (in ISO notation) of the specified year
781 static wxDateTime
SetToWeekOfYear(int year
,
782 wxDateTime_t numWeek
,
783 WeekDay weekday
= Mon
);
785 // sets the date to the last day of the given (or current) month or the
786 // given (or current) year
787 wxDateTime
& SetToLastMonthDay(Month month
= Inv_Month
,
788 int year
= Inv_Year
);
789 inline wxDateTime
GetLastMonthDay(Month month
= Inv_Month
,
790 int year
= Inv_Year
) const;
792 // sets to the given year day (1..365 or 366)
793 wxDateTime
& SetToYearDay(wxDateTime_t yday
);
794 inline wxDateTime
GetYearDay(wxDateTime_t yday
) const;
796 // The definitions below were taken verbatim from
798 // http://www.capecod.net/~pbaum/date/date0.htm
800 // (Peter Baum's home page)
802 // definition: The Julian Day Number, Julian Day, or JD of a
803 // particular instant of time is the number of days and fractions of a
804 // day since 12 hours Universal Time (Greenwich mean noon) on January
805 // 1 of the year -4712, where the year is given in the Julian
806 // proleptic calendar. The idea of using this reference date was
807 // originally proposed by Joseph Scalizer in 1582 to count years but
808 // it was modified by 19th century astronomers to count days. One
809 // could have equivalently defined the reference time to be noon of
810 // November 24, -4713 if were understood that Gregorian calendar rules
811 // were applied. Julian days are Julian Day Numbers and are not to be
812 // confused with Julian dates.
814 // definition: The Rata Die number is a date specified as the number
815 // of days relative to a base date of December 31 of the year 0. Thus
816 // January 1 of the year 1 is Rata Die day 1.
818 // get the Julian Day number (the fractional part specifies the time of
819 // the day, related to noon - beware of rounding errors!)
820 double GetJulianDayNumber() const;
821 double GetJDN() const { return GetJulianDayNumber(); }
823 // get the Modified Julian Day number: it is equal to JDN - 2400000.5
824 // and so integral MJDs correspond to the midnights (and not noons).
825 // MJD 0 is Nov 17, 1858
826 double GetModifiedJulianDayNumber() const { return GetJDN() - 2400000.5; }
827 double GetMJD() const { return GetModifiedJulianDayNumber(); }
829 // get the Rata Die number
830 double GetRataDie() const;
832 // TODO algorithms for calculating some important dates, such as
833 // religious holidays (Easter...) or moon/solar eclipses? Some
834 // algorithms can be found in the calendar FAQ
837 // Timezone stuff: a wxDateTime object constructed using given
838 // day/month/year/hour/min/sec values is interpreted as this moment in
839 // local time. Using the functions below, it may be converted to another
840 // time zone (e.g., the Unix epoch is wxDateTime(1, Jan, 1970).ToGMT()).
842 // These functions try to handle DST internally, but there is no magical
843 // way to know all rules for it in all countries in the world, so if the
844 // program can handle it itself (or doesn't want to handle it at all for
845 // whatever reason), the DST handling can be disabled with noDST.
846 // ------------------------------------------------------------------------
848 // transform to any given timezone
849 inline wxDateTime
ToTimezone(const TimeZone
& tz
, bool noDST
= false) const;
850 wxDateTime
& MakeTimezone(const TimeZone
& tz
, bool noDST
= false);
852 // interpret current value as being in another timezone and transform
854 inline wxDateTime
FromTimezone(const TimeZone
& tz
, bool noDST
= false) const;
855 wxDateTime
& MakeFromTimezone(const TimeZone
& tz
, bool noDST
= false);
857 // transform to/from GMT/UTC
858 wxDateTime
ToUTC(bool noDST
= false) const { return ToTimezone(UTC
, noDST
); }
859 wxDateTime
& MakeUTC(bool noDST
= false) { return MakeTimezone(UTC
, noDST
); }
861 wxDateTime
ToGMT(bool noDST
= false) const { return ToUTC(noDST
); }
862 wxDateTime
& MakeGMT(bool noDST
= false) { return MakeUTC(noDST
); }
864 wxDateTime
FromUTC(bool noDST
= false) const
865 { return FromTimezone(UTC
, noDST
); }
866 wxDateTime
& MakeFromUTC(bool noDST
= false)
867 { return MakeFromTimezone(UTC
, noDST
); }
869 // is daylight savings time in effect at this moment according to the
870 // rules of the specified country?
872 // Return value is > 0 if DST is in effect, 0 if it is not and -1 if
873 // the information is not available (this is compatible with ANSI C)
874 int IsDST(Country country
= Country_Default
) const;
877 // accessors: many of them take the timezone parameter which indicates the
878 // timezone for which to make the calculations and the default value means
879 // to do it for the current timezone of this machine (even if the function
880 // only operates with the date it's necessary because a date may wrap as
881 // result of timezone shift)
882 // ------------------------------------------------------------------------
884 // is the date valid?
885 inline bool IsValid() const { return m_time
!= wxInvalidDateTime
.m_time
; }
887 // get the broken down date/time representation in the given timezone
889 // If you wish to get several time components (day, month and year),
890 // consider getting the whole Tm strcuture first and retrieving the
891 // value from it - this is much more efficient
892 Tm
GetTm(const TimeZone
& tz
= Local
) const;
894 // get the number of seconds since the Unix epoch - returns (time_t)-1
895 // if the value is out of range
896 inline time_t GetTicks() const;
898 // get the century, same as GetCentury(GetYear())
899 int GetCentury(const TimeZone
& tz
= Local
) const
900 { return GetCentury(GetYear(tz
)); }
901 // get the year (returns Inv_Year if date is invalid)
902 int GetYear(const TimeZone
& tz
= Local
) const
903 { return GetTm(tz
).year
; }
904 // get the month (Inv_Month if date is invalid)
905 Month
GetMonth(const TimeZone
& tz
= Local
) const
906 { return (Month
)GetTm(tz
).mon
; }
907 // get the month day (in 1..31 range, 0 if date is invalid)
908 wxDateTime_t
GetDay(const TimeZone
& tz
= Local
) const
909 { return GetTm(tz
).mday
; }
910 // get the day of the week (Inv_WeekDay if date is invalid)
911 WeekDay
GetWeekDay(const TimeZone
& tz
= Local
) const
912 { return GetTm(tz
).GetWeekDay(); }
913 // get the hour of the day
914 wxDateTime_t
GetHour(const TimeZone
& tz
= Local
) const
915 { return GetTm(tz
).hour
; }
917 wxDateTime_t
GetMinute(const TimeZone
& tz
= Local
) const
918 { return GetTm(tz
).min
; }
920 wxDateTime_t
GetSecond(const TimeZone
& tz
= Local
) const
921 { return GetTm(tz
).sec
; }
923 wxDateTime_t
GetMillisecond(const TimeZone
& tz
= Local
) const
924 { return GetTm(tz
).msec
; }
926 // get the day since the year start (1..366, 0 if date is invalid)
927 wxDateTime_t
GetDayOfYear(const TimeZone
& tz
= Local
) const;
928 // get the week number since the year start (1..52 or 53, 0 if date is
930 wxDateTime_t
GetWeekOfYear(WeekFlags flags
= Monday_First
,
931 const TimeZone
& tz
= Local
) const;
932 // get the week number since the month start (1..5, 0 if date is
934 wxDateTime_t
GetWeekOfMonth(WeekFlags flags
= Monday_First
,
935 const TimeZone
& tz
= Local
) const;
937 // is this date a work day? This depends on a country, of course,
938 // because the holidays are different in different countries
939 bool IsWorkDay(Country country
= Country_Default
) const;
941 // is this date later than Gregorian calendar introduction for the
942 // given country (see enum GregorianAdoption)?
944 // NB: this function shouldn't be considered as absolute authority in
945 // the matter. Besides, for some countries the exact date of
946 // adoption of the Gregorian calendar is simply unknown.
947 bool IsGregorianDate(GregorianAdoption country
= Gr_Standard
) const;
949 // dos date and time format
950 // ------------------------------------------------------------------------
952 // set from the DOS packed format
953 wxDateTime
& SetFromDOS(unsigned long ddt
);
955 // pack the date in DOS format
956 unsigned long GetAsDOS() const;
959 // ------------------------------------------------------------------------
962 // convert SYSTEMTIME to wxDateTime
963 wxDateTime
& SetFromMSWSysTime(const struct _SYSTEMTIME
&);
965 // convert wxDateTime to SYSTEMTIME
966 void GetAsMSWSysTime(struct _SYSTEMTIME
*) const;
969 // comparison (see also functions below for operator versions)
970 // ------------------------------------------------------------------------
972 // returns true if the two moments are strictly identical
973 inline bool IsEqualTo(const wxDateTime
& datetime
) const;
975 // returns true if the date is strictly earlier than the given one
976 inline bool IsEarlierThan(const wxDateTime
& datetime
) const;
978 // returns true if the date is strictly later than the given one
979 inline bool IsLaterThan(const wxDateTime
& datetime
) const;
981 // returns true if the date is strictly in the given range
982 inline bool IsStrictlyBetween(const wxDateTime
& t1
,
983 const wxDateTime
& t2
) const;
985 // returns true if the date is in the given range
986 inline bool IsBetween(const wxDateTime
& t1
, const wxDateTime
& t2
) const;
988 // do these two objects refer to the same date?
989 inline bool IsSameDate(const wxDateTime
& dt
) const;
991 // do these two objects have the same time?
992 inline bool IsSameTime(const wxDateTime
& dt
) const;
994 // are these two objects equal up to given timespan?
995 inline bool IsEqualUpTo(const wxDateTime
& dt
, const wxTimeSpan
& ts
) const;
997 inline bool operator<(const wxDateTime
& dt
) const
999 wxASSERT_MSG( IsValid() && dt
.IsValid(), _T("invalid wxDateTime") );
1000 return GetValue() < dt
.GetValue();
1003 inline bool operator<=(const wxDateTime
& dt
) const
1005 wxASSERT_MSG( IsValid() && dt
.IsValid(), _T("invalid wxDateTime") );
1006 return GetValue() <= dt
.GetValue();
1009 inline bool operator>(const wxDateTime
& dt
) const
1011 wxASSERT_MSG( IsValid() && dt
.IsValid(), _T("invalid wxDateTime") );
1012 return GetValue() > dt
.GetValue();
1015 inline bool operator>=(const wxDateTime
& dt
) const
1017 wxASSERT_MSG( IsValid() && dt
.IsValid(), _T("invalid wxDateTime") );
1018 return GetValue() >= dt
.GetValue();
1021 inline bool operator==(const wxDateTime
& dt
) const
1023 wxASSERT_MSG( IsValid() && dt
.IsValid(), _T("invalid wxDateTime") );
1024 return GetValue() == dt
.GetValue();
1027 inline bool operator!=(const wxDateTime
& dt
) const
1029 wxASSERT_MSG( IsValid() && dt
.IsValid(), _T("invalid wxDateTime") );
1030 return GetValue() != dt
.GetValue();
1033 // arithmetics with dates (see also below for more operators)
1034 // ------------------------------------------------------------------------
1036 // return the sum of the date with a time span (positive or negative)
1037 inline wxDateTime
Add(const wxTimeSpan
& diff
) const;
1038 // add a time span (positive or negative)
1039 inline wxDateTime
& Add(const wxTimeSpan
& diff
);
1040 // add a time span (positive or negative)
1041 inline wxDateTime
& operator+=(const wxTimeSpan
& diff
);
1042 inline wxDateTime
operator+(const wxTimeSpan
& ts
) const
1044 wxDateTime
dt(*this);
1049 // return the difference of the date with a time span
1050 inline wxDateTime
Subtract(const wxTimeSpan
& diff
) const;
1051 // subtract a time span (positive or negative)
1052 inline wxDateTime
& Subtract(const wxTimeSpan
& diff
);
1053 // subtract a time span (positive or negative)
1054 inline wxDateTime
& operator-=(const wxTimeSpan
& diff
);
1055 inline wxDateTime
operator-(const wxTimeSpan
& ts
) const
1057 wxDateTime
dt(*this);
1062 // return the sum of the date with a date span
1063 inline wxDateTime
Add(const wxDateSpan
& diff
) const;
1064 // add a date span (positive or negative)
1065 wxDateTime
& Add(const wxDateSpan
& diff
);
1066 // add a date span (positive or negative)
1067 inline wxDateTime
& operator+=(const wxDateSpan
& diff
);
1068 inline wxDateTime
operator+(const wxDateSpan
& ds
) const
1070 wxDateTime
dt(*this);
1075 // return the difference of the date with a date span
1076 inline wxDateTime
Subtract(const wxDateSpan
& diff
) const;
1077 // subtract a date span (positive or negative)
1078 inline wxDateTime
& Subtract(const wxDateSpan
& diff
);
1079 // subtract a date span (positive or negative)
1080 inline wxDateTime
& operator-=(const wxDateSpan
& diff
);
1081 inline wxDateTime
operator-(const wxDateSpan
& ds
) const
1083 wxDateTime
dt(*this);
1088 // return the difference between two dates
1089 inline wxTimeSpan
Subtract(const wxDateTime
& dt
) const;
1090 inline wxTimeSpan
operator-(const wxDateTime
& dt2
) const;
1092 // conversion to/from text: all conversions from text return the pointer to
1093 // the next character following the date specification (i.e. the one where
1094 // the scan had to stop) or NULL on failure; for the versions taking
1095 // wxString or wxCStrData, we don't know if the user code needs char* or
1096 // wchar_t* pointer and so we return char* one for compatibility with the
1097 // existing ANSI code and also return iterator in another output parameter
1098 // (it will be equal to end if the entire string was parsed)
1099 // ------------------------------------------------------------------------
1101 // parse a string in RFC 822 format (found e.g. in mail headers and
1102 // having the form "Wed, 10 Feb 1999 19:07:07 +0100")
1103 const char *ParseRfc822Date(const wxString
& date
,
1104 wxString::const_iterator
*end
= NULL
);
1105 const char *ParseRfc822Date(const wxCStrData
& date
,
1106 wxString::const_iterator
*end
= NULL
)
1108 return ParseRfc822Date(date
.AsString(), end
);
1111 const wchar_t *ParseRfc822Date(const wchar_t* date
)
1113 return ReturnEndAsWidePtr(&wxDateTime::ParseRfc822Date
, date
);
1116 const char *ParseRfc822Date(const char* date
)
1118 return ParseRfc822Date(wxString(date
));
1121 // parse a date/time in the given format (see strptime(3)), fill in
1122 // the missing (in the string) fields with the values of dateDef (by
1123 // default, they will not change if they had valid values or will
1124 // default to Today() otherwise)
1126 // notice that we unfortunately need all those overloads because we use
1127 // the type of the date string to select the return value of the
1128 // function: it's wchar_t if a wide string is passed for compatibility
1129 // with the code doing "const wxChar *p = dt.ParseFormat(_T("..."))",
1130 // but char* in all other cases for compatibility with ANSI build which
1131 // allowed code like "const char *p = dt.ParseFormat("...")"
1133 // so we need wchar_t overload and now passing s.c_str() as first
1134 // argument is ambiguous because it's convertible to both wxString and
1135 // wchar_t* and now it's passing char* which becomes ambiguous as it is
1136 // convertible to both wxString and wxCStrData hence we need char*
1139 // and to make our life more miserable we also pay for having the
1140 // optional dateDef parameter: as it's almost never used, we want to
1141 // allow people to omit it when specifying the end iterator output
1142 // parameter but we still have to allow specifying dateDef too, so we
1143 // need another overload for this
1145 // FIXME: all this mess could be avoided by using some class similar to
1146 // wxFormatString, i.e. remembering string [pointer] of any type
1147 // and convertible to either char* or wchar_t* as wxCStrData and
1148 // having only 1 (or 2, because of the last paragraph above)
1149 // overload taking it, see #9560
1150 const char *ParseFormat(const wxString
& date
,
1151 const wxString
& format
= wxDefaultDateTimeFormat
,
1152 const wxDateTime
& dateDef
= wxDefaultDateTime
,
1153 wxString::const_iterator
*end
= NULL
);
1155 const char *ParseFormat(const wxString
& date
,
1156 const wxString
& format
,
1157 wxString::const_iterator
*end
)
1159 return ParseFormat(date
, format
, wxDefaultDateTime
, end
);
1162 const char *ParseFormat(const wxCStrData
& date
,
1163 const wxString
& format
= wxDefaultDateTimeFormat
,
1164 const wxDateTime
& dateDef
= wxDefaultDateTime
,
1165 wxString::const_iterator
*end
= NULL
)
1167 return ParseFormat(date
.AsString(), format
, dateDef
, end
);
1170 const wchar_t *ParseFormat(const wchar_t *date
,
1171 const wxString
& format
= wxDefaultDateTimeFormat
,
1172 const wxDateTime
& dateDef
= wxDefaultDateTime
)
1174 const wxString
datestr(date
);
1175 wxString::const_iterator end
;
1176 if ( !ParseFormat(datestr
, format
, dateDef
, &end
) )
1179 return date
+ (end
- datestr
.begin());
1182 const char *ParseFormat(const char *date
,
1183 const wxString
& format
= "%c",
1184 const wxDateTime
& dateDef
= wxDefaultDateTime
)
1186 return ParseFormat(wxString(date
), format
, dateDef
);
1190 // parse a string containing date, time or both in ISO 8601 format
1192 // notice that these functions are new in wx 3.0 and so we don't
1193 // provide compatibility overloads for them
1194 bool ParseISODate(const wxString
& date
)
1196 wxString::const_iterator end
;
1197 return ParseFormat(date
, wxS("%Y-%m-%d"), &end
) && end
== date
.end();
1200 bool ParseISOTime(const wxString
& time
)
1202 wxString::const_iterator end
;
1203 return ParseFormat(time
, wxS("%H:%M:%S"), &end
) && end
== time
.end();
1206 bool ParseISOCombined(const wxString
& datetime
, char sep
= 'T')
1208 wxString::const_iterator end
;
1209 const wxString fmt
= wxS("%Y-%m-%d") + wxString(sep
) + wxS("%H:%M:%S");
1210 return ParseFormat(datetime
, fmt
, &end
) && end
== datetime
.end();
1213 // parse a string containing the date/time in "free" format, this
1214 // function will try to make an educated guess at the string contents
1215 const char *ParseDateTime(const wxString
& datetime
,
1216 wxString::const_iterator
*end
= NULL
);
1218 const char *ParseDateTime(const wxCStrData
& datetime
,
1219 wxString::const_iterator
*end
= NULL
)
1221 return ParseDateTime(datetime
.AsString(), end
);
1224 const wchar_t *ParseDateTime(const wchar_t *datetime
)
1226 return ReturnEndAsWidePtr(&wxDateTime::ParseDateTime
, datetime
);
1229 const char *ParseDateTime(const char *datetime
)
1231 return ParseDateTime(wxString(datetime
));
1234 // parse a string containing the date only in "free" format (less
1235 // flexible than ParseDateTime)
1236 const char *ParseDate(const wxString
& date
,
1237 wxString::const_iterator
*end
= NULL
);
1239 const char *ParseDate(const wxCStrData
& date
,
1240 wxString::const_iterator
*end
= NULL
)
1242 return ParseDate(date
.AsString(), end
);
1245 const wchar_t *ParseDate(const wchar_t *date
)
1247 return ReturnEndAsWidePtr(&wxDateTime::ParseDate
, date
);
1250 const char *ParseDate(const char *date
)
1252 return ParseDate(wxString(date
));
1255 // parse a string containing the time only in "free" format
1256 const char *ParseTime(const wxString
& time
,
1257 wxString::const_iterator
*end
= NULL
);
1259 const char *ParseTime(const wxCStrData
& time
,
1260 wxString::const_iterator
*end
= NULL
)
1262 return ParseTime(time
.AsString(), end
);
1265 const wchar_t *ParseTime(const wchar_t *time
)
1267 return ReturnEndAsWidePtr(&wxDateTime::ParseTime
, time
);
1270 const char *ParseTime(const char *time
)
1272 return ParseTime(wxString(time
));
1275 // this function accepts strftime()-like format string (default
1276 // argument corresponds to the preferred date and time representation
1277 // for the current locale) and returns the string containing the
1278 // resulting text representation
1279 wxString
Format(const wxString
& format
= wxDefaultDateTimeFormat
,
1280 const TimeZone
& tz
= Local
) const;
1281 // preferred date representation for the current locale
1282 wxString
FormatDate() const { return Format(wxS("%x")); }
1283 // preferred time representation for the current locale
1284 wxString
FormatTime() const { return Format(wxS("%X")); }
1285 // returns the string representing the date in ISO 8601 format
1287 wxString
FormatISODate() const { return Format(wxS("%Y-%m-%d")); }
1288 // returns the string representing the time in ISO 8601 format
1290 wxString
FormatISOTime() const { return Format(wxS("%H:%M:%S")); }
1291 // return the combined date time representation in ISO 8601 format; the
1292 // separator character should be 'T' according to the standard but it
1293 // can also be useful to set it to ' '
1294 wxString
FormatISOCombined(char sep
= 'T') const
1295 { return FormatISODate() + sep
+ FormatISOTime(); }
1298 // ------------------------------------------------------------------------
1300 // construct from internal representation
1301 wxDateTime(const wxLongLong
& time
) { m_time
= time
; }
1303 // get the internal representation
1304 inline wxLongLong
GetValue() const;
1306 // a helper function to get the current time_t
1307 static time_t GetTimeNow() { return time(NULL
); }
1309 // another one to get the current time broken down
1310 static struct tm
*GetTmNow()
1312 static struct tm l_CurrentTime
;
1313 return GetTmNow(&l_CurrentTime
);
1316 // get current time using thread-safe function
1317 static struct tm
*GetTmNow(struct tm
*tmstruct
);
1320 // helper function for defining backward-compatible wrappers for code
1321 // using wchar_t* pointer instead of wxString iterators
1323 const char *(wxDateTime::*StringMethod
)(const wxString
& s
,
1324 wxString::const_iterator
*end
);
1326 const wchar_t *ReturnEndAsWidePtr(StringMethod func
, const wchar_t *p
)
1328 const wxString
s(p
);
1329 wxString::const_iterator end
;
1330 if ( !(this->*func
)(s
, &end
) )
1333 return p
+ (end
- s
.begin());
1337 // the current country - as it's the same for all program objects (unless
1338 // it runs on a _really_ big cluster system :-), this is a static member:
1339 // see SetCountry() and GetCountry()
1340 static Country ms_country
;
1342 // this constant is used to transform a time_t value to the internal
1343 // representation, as time_t is in seconds and we use milliseconds it's
1345 static const long TIME_T_FACTOR
;
1347 // returns true if we fall in range in which we can use standard ANSI C
1349 inline bool IsInStdRange() const;
1351 // the internal representation of the time is the amount of milliseconds
1352 // elapsed since the origin which is set by convention to the UNIX/C epoch
1353 // value: the midnight of January 1, 1970 (UTC)
1357 // ----------------------------------------------------------------------------
1358 // This class contains a difference between 2 wxDateTime values, so it makes
1359 // sense to add it to wxDateTime and it is the result of subtraction of 2
1360 // objects of that class. See also wxDateSpan.
1361 // ----------------------------------------------------------------------------
1363 class WXDLLIMPEXP_BASE wxTimeSpan
1367 // ------------------------------------------------------------------------
1369 // return the timespan for the given number of milliseconds
1370 static wxTimeSpan
Milliseconds(wxLongLong ms
) { return wxTimeSpan(0, 0, 0, ms
); }
1371 static wxTimeSpan
Millisecond() { return Milliseconds(1); }
1373 // return the timespan for the given number of seconds
1374 static wxTimeSpan
Seconds(wxLongLong sec
) { return wxTimeSpan(0, 0, sec
); }
1375 static wxTimeSpan
Second() { return Seconds(1); }
1377 // return the timespan for the given number of minutes
1378 static wxTimeSpan
Minutes(long min
) { return wxTimeSpan(0, min
, 0 ); }
1379 static wxTimeSpan
Minute() { return Minutes(1); }
1381 // return the timespan for the given number of hours
1382 static wxTimeSpan
Hours(long hours
) { return wxTimeSpan(hours
, 0, 0); }
1383 static wxTimeSpan
Hour() { return Hours(1); }
1385 // return the timespan for the given number of days
1386 static wxTimeSpan
Days(long days
) { return Hours(24 * days
); }
1387 static wxTimeSpan
Day() { return Days(1); }
1389 // return the timespan for the given number of weeks
1390 static wxTimeSpan
Weeks(long days
) { return Days(7 * days
); }
1391 static wxTimeSpan
Week() { return Weeks(1); }
1393 // default ctor constructs the 0 time span
1396 // from separate values for each component, date set to 0 (hours are
1397 // not restricted to 0..24 range, neither are minutes, seconds or
1399 inline wxTimeSpan(long hours
,
1401 wxLongLong seconds
= 0,
1402 wxLongLong milliseconds
= 0);
1404 // default copy ctor is ok
1408 // arithmetics with time spans (see also below for more operators)
1409 // ------------------------------------------------------------------------
1411 // return the sum of two timespans
1412 inline wxTimeSpan
Add(const wxTimeSpan
& diff
) const;
1413 // add two timespans together
1414 inline wxTimeSpan
& Add(const wxTimeSpan
& diff
);
1415 // add two timespans together
1416 wxTimeSpan
& operator+=(const wxTimeSpan
& diff
) { return Add(diff
); }
1417 inline wxTimeSpan
operator+(const wxTimeSpan
& ts
) const
1419 return wxTimeSpan(GetValue() + ts
.GetValue());
1422 // return the difference of two timespans
1423 inline wxTimeSpan
Subtract(const wxTimeSpan
& diff
) const;
1424 // subtract another timespan
1425 inline wxTimeSpan
& Subtract(const wxTimeSpan
& diff
);
1426 // subtract another timespan
1427 wxTimeSpan
& operator-=(const wxTimeSpan
& diff
) { return Subtract(diff
); }
1428 inline wxTimeSpan
operator-(const wxTimeSpan
& ts
)
1430 return wxTimeSpan(GetValue() - ts
.GetValue());
1433 // multiply timespan by a scalar
1434 inline wxTimeSpan
Multiply(int n
) const;
1435 // multiply timespan by a scalar
1436 inline wxTimeSpan
& Multiply(int n
);
1437 // multiply timespan by a scalar
1438 wxTimeSpan
& operator*=(int n
) { return Multiply(n
); }
1439 inline wxTimeSpan
operator*(int n
) const
1441 return wxTimeSpan(*this).Multiply(n
);
1444 // return this timespan with opposite sign
1445 wxTimeSpan
Negate() const { return wxTimeSpan(-GetValue()); }
1446 // negate the value of the timespan
1447 wxTimeSpan
& Neg() { m_diff
= -GetValue(); return *this; }
1448 // negate the value of the timespan
1449 wxTimeSpan
& operator-() { return Neg(); }
1451 // return the absolute value of the timespan: does _not_ modify the
1453 inline wxTimeSpan
Abs() const;
1455 // there is intentionally no division because we don't want to
1456 // introduce rounding errors in time calculations
1458 // comparaison (see also operator versions below)
1459 // ------------------------------------------------------------------------
1461 // is the timespan null?
1462 bool IsNull() const { return m_diff
== 0l; }
1463 // returns true if the timespan is null
1464 bool operator!() const { return !IsNull(); }
1466 // is the timespan positive?
1467 bool IsPositive() const { return m_diff
> 0l; }
1469 // is the timespan negative?
1470 bool IsNegative() const { return m_diff
< 0l; }
1472 // are two timespans equal?
1473 inline bool IsEqualTo(const wxTimeSpan
& ts
) const;
1474 // compare two timestamps: works with the absolute values, i.e. -2
1475 // hours is longer than 1 hour. Also, it will return false if the
1476 // timespans are equal in absolute value.
1477 inline bool IsLongerThan(const wxTimeSpan
& ts
) const;
1478 // compare two timestamps: works with the absolute values, i.e. 1
1479 // hour is shorter than -2 hours. Also, it will return false if the
1480 // timespans are equal in absolute value.
1481 bool IsShorterThan(const wxTimeSpan
& t
) const;
1483 inline bool operator<(const wxTimeSpan
&ts
) const
1485 return GetValue() < ts
.GetValue();
1488 inline bool operator<=(const wxTimeSpan
&ts
) const
1490 return GetValue() <= ts
.GetValue();
1493 inline bool operator>(const wxTimeSpan
&ts
) const
1495 return GetValue() > ts
.GetValue();
1498 inline bool operator>=(const wxTimeSpan
&ts
) const
1500 return GetValue() >= ts
.GetValue();
1503 inline bool operator==(const wxTimeSpan
&ts
) const
1505 return GetValue() == ts
.GetValue();
1508 inline bool operator!=(const wxTimeSpan
&ts
) const
1510 return GetValue() != ts
.GetValue();
1513 // breaking into days, hours, minutes and seconds
1514 // ------------------------------------------------------------------------
1516 // get the max number of weeks in this timespan
1517 inline int GetWeeks() const;
1518 // get the max number of days in this timespan
1519 inline int GetDays() const;
1520 // get the max number of hours in this timespan
1521 inline int GetHours() const;
1522 // get the max number of minutes in this timespan
1523 inline int GetMinutes() const;
1524 // get the max number of seconds in this timespan
1525 inline wxLongLong
GetSeconds() const;
1526 // get the number of milliseconds in this timespan
1527 wxLongLong
GetMilliseconds() const { return m_diff
; }
1529 // conversion to text
1530 // ------------------------------------------------------------------------
1532 // this function accepts strftime()-like format string (default
1533 // argument corresponds to the preferred date and time representation
1534 // for the current locale) and returns the string containing the
1535 // resulting text representation. Notice that only some of format
1536 // specifiers valid for wxDateTime are valid for wxTimeSpan: hours,
1537 // minutes and seconds make sense, but not "PM/AM" string for example.
1538 wxString
Format(const wxString
& format
= wxDefaultTimeSpanFormat
) const;
1541 // ------------------------------------------------------------------------
1543 // construct from internal representation
1544 wxTimeSpan(const wxLongLong
& diff
) { m_diff
= diff
; }
1546 // get the internal representation
1547 wxLongLong
GetValue() const { return m_diff
; }
1550 // the (signed) time span in milliseconds
1554 // ----------------------------------------------------------------------------
1555 // This class is a "logical time span" and is useful for implementing program
1556 // logic for such things as "add one month to the date" which, in general,
1557 // doesn't mean to add 60*60*24*31 seconds to it, but to take the same date
1558 // the next month (to understand that this is indeed different consider adding
1559 // one month to Feb, 15 - we want to get Mar, 15, of course).
1561 // When adding a month to the date, all lesser components (days, hours, ...)
1562 // won't be changed unless the resulting date would be invalid: for example,
1563 // Jan 31 + 1 month will be Feb 28, not (non existing) Feb 31.
1565 // Because of this feature, adding and subtracting back again the same
1566 // wxDateSpan will *not*, in general give back the original date: Feb 28 - 1
1567 // month will be Jan 28, not Jan 31!
1569 // wxDateSpan can be either positive or negative. They may be
1570 // multiplied by scalars which multiply all deltas by the scalar: i.e. 2*(1
1571 // month and 1 day) is 2 months and 2 days. They can be added together and
1572 // with wxDateTime or wxTimeSpan, but the type of result is different for each
1575 // Beware about weeks: if you specify both weeks and days, the total number of
1576 // days added will be 7*weeks + days! See also GetTotalDays() function.
1578 // Equality operators are defined for wxDateSpans. Two datespans are equal if
1579 // they both give the same target date when added to *every* source date.
1580 // Thus wxDateSpan::Months(1) is not equal to wxDateSpan::Days(30), because
1581 // they not give the same date when added to 1 Feb. But wxDateSpan::Days(14) is
1582 // equal to wxDateSpan::Weeks(2)
1584 // Finally, notice that for adding hours, minutes &c you don't need this
1585 // class: wxTimeSpan will do the job because there are no subtleties
1586 // associated with those.
1587 // ----------------------------------------------------------------------------
1589 class WXDLLIMPEXP_BASE wxDateSpan
1593 // ------------------------------------------------------------------------
1595 // this many years/months/weeks/days
1596 wxDateSpan(int years
= 0, int months
= 0, int weeks
= 0, int days
= 0)
1604 // get an object for the given number of days
1605 static wxDateSpan
Days(int days
) { return wxDateSpan(0, 0, 0, days
); }
1606 static wxDateSpan
Day() { return Days(1); }
1608 // get an object for the given number of weeks
1609 static wxDateSpan
Weeks(int weeks
) { return wxDateSpan(0, 0, weeks
, 0); }
1610 static wxDateSpan
Week() { return Weeks(1); }
1612 // get an object for the given number of months
1613 static wxDateSpan
Months(int mon
) { return wxDateSpan(0, mon
, 0, 0); }
1614 static wxDateSpan
Month() { return Months(1); }
1616 // get an object for the given number of years
1617 static wxDateSpan
Years(int years
) { return wxDateSpan(years
, 0, 0, 0); }
1618 static wxDateSpan
Year() { return Years(1); }
1620 // default copy ctor is ok
1624 // accessors (all SetXXX() return the (modified) wxDateSpan object)
1625 // ------------------------------------------------------------------------
1627 // set number of years
1628 wxDateSpan
& SetYears(int n
) { m_years
= n
; return *this; }
1629 // set number of months
1630 wxDateSpan
& SetMonths(int n
) { m_months
= n
; return *this; }
1631 // set number of weeks
1632 wxDateSpan
& SetWeeks(int n
) { m_weeks
= n
; return *this; }
1633 // set number of days
1634 wxDateSpan
& SetDays(int n
) { m_days
= n
; return *this; }
1636 // get number of years
1637 int GetYears() const { return m_years
; }
1638 // get number of months
1639 int GetMonths() const { return m_months
; }
1640 // get number of weeks
1641 int GetWeeks() const { return m_weeks
; }
1642 // get number of days
1643 int GetDays() const { return m_days
; }
1644 // returns 7*GetWeeks() + GetDays()
1645 int GetTotalDays() const { return 7*m_weeks
+ m_days
; }
1647 // arithmetics with date spans (see also below for more operators)
1648 // ------------------------------------------------------------------------
1650 // return sum of two date spans
1651 inline wxDateSpan
Add(const wxDateSpan
& other
) const;
1652 // add another wxDateSpan to us
1653 inline wxDateSpan
& Add(const wxDateSpan
& other
);
1654 // add another wxDateSpan to us
1655 inline wxDateSpan
& operator+=(const wxDateSpan
& other
);
1656 inline wxDateSpan
operator+(const wxDateSpan
& ds
) const
1658 return wxDateSpan(GetYears() + ds
.GetYears(),
1659 GetMonths() + ds
.GetMonths(),
1660 GetWeeks() + ds
.GetWeeks(),
1661 GetDays() + ds
.GetDays());
1664 // return difference of two date spans
1665 inline wxDateSpan
Subtract(const wxDateSpan
& other
) const;
1666 // subtract another wxDateSpan from us
1667 inline wxDateSpan
& Subtract(const wxDateSpan
& other
);
1668 // subtract another wxDateSpan from us
1669 inline wxDateSpan
& operator-=(const wxDateSpan
& other
);
1670 inline wxDateSpan
operator-(const wxDateSpan
& ds
) const
1672 return wxDateSpan(GetYears() - ds
.GetYears(),
1673 GetMonths() - ds
.GetMonths(),
1674 GetWeeks() - ds
.GetWeeks(),
1675 GetDays() - ds
.GetDays());
1678 // return a copy of this time span with changed sign
1679 inline wxDateSpan
Negate() const;
1680 // inverse the sign of this timespan
1681 inline wxDateSpan
& Neg();
1682 // inverse the sign of this timespan
1683 wxDateSpan
& operator-() { return Neg(); }
1685 // return the date span proportional to this one with given factor
1686 inline wxDateSpan
Multiply(int factor
) const;
1687 // multiply all components by a (signed) number
1688 inline wxDateSpan
& Multiply(int factor
);
1689 // multiply all components by a (signed) number
1690 inline wxDateSpan
& operator*=(int factor
) { return Multiply(factor
); }
1691 inline wxDateSpan
operator*(int n
) const
1693 return wxDateSpan(*this).Multiply(n
);
1696 // ds1 == d2 if and only if for every wxDateTime t t + ds1 == t + ds2
1697 inline bool operator==(const wxDateSpan
& ds
) const
1699 return GetYears() == ds
.GetYears() &&
1700 GetMonths() == ds
.GetMonths() &&
1701 GetTotalDays() == ds
.GetTotalDays();
1704 inline bool operator!=(const wxDateSpan
& ds
) const
1706 return !(*this == ds
);
1716 // ----------------------------------------------------------------------------
1717 // wxDateTimeArray: array of dates.
1718 // ----------------------------------------------------------------------------
1720 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxDateTime
, wxDateTimeArray
, WXDLLIMPEXP_BASE
);
1722 // ----------------------------------------------------------------------------
1723 // wxDateTimeHolidayAuthority: an object of this class will decide whether a
1724 // given date is a holiday and is used by all functions working with "work
1727 // NB: the base class is an ABC, derived classes must implement the pure
1728 // virtual methods to work with the holidays they correspond to.
1729 // ----------------------------------------------------------------------------
1731 class WXDLLIMPEXP_FWD_BASE wxDateTimeHolidayAuthority
;
1732 WX_DEFINE_USER_EXPORTED_ARRAY_PTR(wxDateTimeHolidayAuthority
*,
1733 wxHolidayAuthoritiesArray
,
1734 class WXDLLIMPEXP_BASE
);
1736 class wxDateTimeHolidaysModule
;
1737 class WXDLLIMPEXP_BASE wxDateTimeHolidayAuthority
1739 friend class wxDateTimeHolidaysModule
;
1741 // returns true if the given date is a holiday
1742 static bool IsHoliday(const wxDateTime
& dt
);
1744 // fills the provided array with all holidays in the given range, returns
1745 // the number of them
1746 static size_t GetHolidaysInRange(const wxDateTime
& dtStart
,
1747 const wxDateTime
& dtEnd
,
1748 wxDateTimeArray
& holidays
);
1750 // clear the list of holiday authorities
1751 static void ClearAllAuthorities();
1753 // add a new holiday authority (the pointer will be deleted by
1754 // wxDateTimeHolidayAuthority)
1755 static void AddAuthority(wxDateTimeHolidayAuthority
*auth
);
1757 // the base class must have a virtual dtor
1758 virtual ~wxDateTimeHolidayAuthority();
1761 // this function is called to determine whether a given day is a holiday
1762 virtual bool DoIsHoliday(const wxDateTime
& dt
) const = 0;
1764 // this function should fill the array with all holidays between the two
1765 // given dates - it is implemented in the base class, but in a very
1766 // inefficient way (it just iterates over all days and uses IsHoliday() for
1767 // each of them), so it must be overridden in the derived class where the
1768 // base class version may be explicitly used if needed
1770 // returns the number of holidays in the given range and fills holidays
1772 virtual size_t DoGetHolidaysInRange(const wxDateTime
& dtStart
,
1773 const wxDateTime
& dtEnd
,
1774 wxDateTimeArray
& holidays
) const = 0;
1777 // all holiday authorities
1778 static wxHolidayAuthoritiesArray ms_authorities
;
1781 // the holidays for this class are all Saturdays and Sundays
1782 class WXDLLIMPEXP_BASE wxDateTimeWorkDays
: public wxDateTimeHolidayAuthority
1785 virtual bool DoIsHoliday(const wxDateTime
& dt
) const;
1786 virtual size_t DoGetHolidaysInRange(const wxDateTime
& dtStart
,
1787 const wxDateTime
& dtEnd
,
1788 wxDateTimeArray
& holidays
) const;
1791 // ============================================================================
1792 // inline functions implementation
1793 // ============================================================================
1795 // ----------------------------------------------------------------------------
1797 // ----------------------------------------------------------------------------
1799 #define MILLISECONDS_PER_DAY 86400000l
1801 // some broken compilers (HP-UX CC) refuse to compile the "normal" version, but
1802 // using a temp variable always might prevent other compilers from optimising
1803 // it away - hence use of this ugly macro
1805 #define MODIFY_AND_RETURN(op) return wxDateTime(*this).op
1807 #define MODIFY_AND_RETURN(op) wxDateTime dt(*this); dt.op; return dt
1810 // ----------------------------------------------------------------------------
1811 // wxDateTime construction
1812 // ----------------------------------------------------------------------------
1814 inline bool wxDateTime::IsInStdRange() const
1816 return m_time
>= 0l && (m_time
/ TIME_T_FACTOR
) < LONG_MAX
;
1820 inline wxDateTime
wxDateTime::Now()
1823 return wxDateTime(*GetTmNow(&tmstruct
));
1827 inline wxDateTime
wxDateTime::Today()
1829 wxDateTime
dt(Now());
1835 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
1836 inline wxDateTime
& wxDateTime::Set(time_t timet
)
1838 // assign first to avoid long multiplication overflow!
1839 m_time
= timet
- WX_TIME_BASE_OFFSET
;
1840 m_time
*= TIME_T_FACTOR
;
1846 inline wxDateTime
& wxDateTime::SetToCurrent()
1852 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
1853 inline wxDateTime::wxDateTime(time_t timet
)
1859 inline wxDateTime::wxDateTime(const struct tm
& tm
)
1864 inline wxDateTime::wxDateTime(const Tm
& tm
)
1869 inline wxDateTime::wxDateTime(double jdn
)
1874 inline wxDateTime
& wxDateTime::Set(const Tm
& tm
)
1876 wxASSERT_MSG( tm
.IsValid(), _T("invalid broken down date/time") );
1878 return Set(tm
.mday
, (Month
)tm
.mon
, tm
.year
,
1879 tm
.hour
, tm
.min
, tm
.sec
, tm
.msec
);
1882 inline wxDateTime::wxDateTime(wxDateTime_t hour
,
1883 wxDateTime_t minute
,
1884 wxDateTime_t second
,
1885 wxDateTime_t millisec
)
1887 Set(hour
, minute
, second
, millisec
);
1890 inline wxDateTime::wxDateTime(wxDateTime_t day
,
1894 wxDateTime_t minute
,
1895 wxDateTime_t second
,
1896 wxDateTime_t millisec
)
1898 Set(day
, month
, year
, hour
, minute
, second
, millisec
);
1901 // ----------------------------------------------------------------------------
1902 // wxDateTime accessors
1903 // ----------------------------------------------------------------------------
1905 inline wxLongLong
wxDateTime::GetValue() const
1907 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1912 inline time_t wxDateTime::GetTicks() const
1914 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1915 if ( !IsInStdRange() )
1920 return (time_t)((m_time
/ (long)TIME_T_FACTOR
).ToLong()) + WX_TIME_BASE_OFFSET
;
1923 inline bool wxDateTime::SetToLastWeekDay(WeekDay weekday
,
1927 return SetToWeekDay(weekday
, -1, month
, year
);
1931 wxDateTime::GetWeekDayInSameWeek(WeekDay weekday
,
1932 WeekFlags
WXUNUSED(flags
)) const
1934 MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday
) );
1937 inline wxDateTime
wxDateTime::GetNextWeekDay(WeekDay weekday
) const
1939 MODIFY_AND_RETURN( SetToNextWeekDay(weekday
) );
1942 inline wxDateTime
wxDateTime::GetPrevWeekDay(WeekDay weekday
) const
1944 MODIFY_AND_RETURN( SetToPrevWeekDay(weekday
) );
1947 inline wxDateTime
wxDateTime::GetWeekDay(WeekDay weekday
,
1952 wxDateTime
dt(*this);
1954 return dt
.SetToWeekDay(weekday
, n
, month
, year
) ? dt
: wxInvalidDateTime
;
1957 inline wxDateTime
wxDateTime::GetLastWeekDay(WeekDay weekday
,
1961 wxDateTime
dt(*this);
1963 return dt
.SetToLastWeekDay(weekday
, month
, year
) ? dt
: wxInvalidDateTime
;
1966 inline wxDateTime
wxDateTime::GetLastMonthDay(Month month
, int year
) const
1968 MODIFY_AND_RETURN( SetToLastMonthDay(month
, year
) );
1971 inline wxDateTime
wxDateTime::GetYearDay(wxDateTime_t yday
) const
1973 MODIFY_AND_RETURN( SetToYearDay(yday
) );
1976 // ----------------------------------------------------------------------------
1977 // wxDateTime comparison
1978 // ----------------------------------------------------------------------------
1980 inline bool wxDateTime::IsEqualTo(const wxDateTime
& datetime
) const
1982 wxASSERT_MSG( IsValid() && datetime
.IsValid(), _T("invalid wxDateTime"));
1984 return m_time
== datetime
.m_time
;
1987 inline bool wxDateTime::IsEarlierThan(const wxDateTime
& datetime
) const
1989 wxASSERT_MSG( IsValid() && datetime
.IsValid(), _T("invalid wxDateTime"));
1991 return m_time
< datetime
.m_time
;
1994 inline bool wxDateTime::IsLaterThan(const wxDateTime
& datetime
) const
1996 wxASSERT_MSG( IsValid() && datetime
.IsValid(), _T("invalid wxDateTime"));
1998 return m_time
> datetime
.m_time
;
2001 inline bool wxDateTime::IsStrictlyBetween(const wxDateTime
& t1
,
2002 const wxDateTime
& t2
) const
2004 // no need for assert, will be checked by the functions we call
2005 return IsLaterThan(t1
) && IsEarlierThan(t2
);
2008 inline bool wxDateTime::IsBetween(const wxDateTime
& t1
,
2009 const wxDateTime
& t2
) const
2011 // no need for assert, will be checked by the functions we call
2012 return IsEqualTo(t1
) || IsEqualTo(t2
) || IsStrictlyBetween(t1
, t2
);
2015 inline bool wxDateTime::IsSameDate(const wxDateTime
& dt
) const
2020 return tm1
.year
== tm2
.year
&&
2021 tm1
.mon
== tm2
.mon
&&
2022 tm1
.mday
== tm2
.mday
;
2025 inline bool wxDateTime::IsSameTime(const wxDateTime
& dt
) const
2027 // notice that we can't do something like this:
2029 // m_time % MILLISECONDS_PER_DAY == dt.m_time % MILLISECONDS_PER_DAY
2031 // because we have also to deal with (possibly) different DST settings!
2035 return tm1
.hour
== tm2
.hour
&&
2036 tm1
.min
== tm2
.min
&&
2037 tm1
.sec
== tm2
.sec
&&
2038 tm1
.msec
== tm2
.msec
;
2041 inline bool wxDateTime::IsEqualUpTo(const wxDateTime
& dt
,
2042 const wxTimeSpan
& ts
) const
2044 return IsBetween(dt
.Subtract(ts
), dt
.Add(ts
));
2047 // ----------------------------------------------------------------------------
2048 // wxDateTime arithmetics
2049 // ----------------------------------------------------------------------------
2051 inline wxDateTime
wxDateTime::Add(const wxTimeSpan
& diff
) const
2053 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
2055 return wxDateTime(m_time
+ diff
.GetValue());
2058 inline wxDateTime
& wxDateTime::Add(const wxTimeSpan
& diff
)
2060 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
2062 m_time
+= diff
.GetValue();
2067 inline wxDateTime
& wxDateTime::operator+=(const wxTimeSpan
& diff
)
2072 inline wxDateTime
wxDateTime::Subtract(const wxTimeSpan
& diff
) const
2074 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
2076 return wxDateTime(m_time
- diff
.GetValue());
2079 inline wxDateTime
& wxDateTime::Subtract(const wxTimeSpan
& diff
)
2081 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
2083 m_time
-= diff
.GetValue();
2088 inline wxDateTime
& wxDateTime::operator-=(const wxTimeSpan
& diff
)
2090 return Subtract(diff
);
2093 inline wxTimeSpan
wxDateTime::Subtract(const wxDateTime
& datetime
) const
2095 wxASSERT_MSG( IsValid() && datetime
.IsValid(), _T("invalid wxDateTime"));
2097 return wxTimeSpan(GetValue() - datetime
.GetValue());
2100 inline wxTimeSpan
wxDateTime::operator-(const wxDateTime
& dt2
) const
2102 return this->Subtract(dt2
);
2105 inline wxDateTime
wxDateTime::Add(const wxDateSpan
& diff
) const
2107 return wxDateTime(*this).Add(diff
);
2110 inline wxDateTime
& wxDateTime::Subtract(const wxDateSpan
& diff
)
2112 return Add(diff
.Negate());
2115 inline wxDateTime
wxDateTime::Subtract(const wxDateSpan
& diff
) const
2117 return wxDateTime(*this).Subtract(diff
);
2120 inline wxDateTime
& wxDateTime::operator-=(const wxDateSpan
& diff
)
2122 return Subtract(diff
);
2125 inline wxDateTime
& wxDateTime::operator+=(const wxDateSpan
& diff
)
2130 // ----------------------------------------------------------------------------
2131 // wxDateTime and timezones
2132 // ----------------------------------------------------------------------------
2135 wxDateTime::ToTimezone(const wxDateTime::TimeZone
& tz
, bool noDST
) const
2137 MODIFY_AND_RETURN( MakeTimezone(tz
, noDST
) );
2141 wxDateTime::FromTimezone(const wxDateTime::TimeZone
& tz
, bool noDST
) const
2143 MODIFY_AND_RETURN( MakeFromTimezone(tz
, noDST
) );
2146 // ----------------------------------------------------------------------------
2147 // wxTimeSpan construction
2148 // ----------------------------------------------------------------------------
2150 inline wxTimeSpan::wxTimeSpan(long hours
,
2153 wxLongLong milliseconds
)
2155 // assign first to avoid precision loss
2162 m_diff
+= milliseconds
;
2165 // ----------------------------------------------------------------------------
2166 // wxTimeSpan accessors
2167 // ----------------------------------------------------------------------------
2169 inline wxLongLong
wxTimeSpan::GetSeconds() const
2171 return m_diff
/ 1000l;
2174 inline int wxTimeSpan::GetMinutes() const
2176 // explicit cast to int suppresses a warning with CodeWarrior and possibly
2177 // others (changing the return type to long from int is impossible in 2.8)
2178 return (int)((GetSeconds() / 60l).GetLo());
2181 inline int wxTimeSpan::GetHours() const
2183 return GetMinutes() / 60;
2186 inline int wxTimeSpan::GetDays() const
2188 return GetHours() / 24;
2191 inline int wxTimeSpan::GetWeeks() const
2193 return GetDays() / 7;
2196 // ----------------------------------------------------------------------------
2197 // wxTimeSpan arithmetics
2198 // ----------------------------------------------------------------------------
2200 inline wxTimeSpan
wxTimeSpan::Add(const wxTimeSpan
& diff
) const
2202 return wxTimeSpan(m_diff
+ diff
.GetValue());
2205 inline wxTimeSpan
& wxTimeSpan::Add(const wxTimeSpan
& diff
)
2207 m_diff
+= diff
.GetValue();
2212 inline wxTimeSpan
wxTimeSpan::Subtract(const wxTimeSpan
& diff
) const
2214 return wxTimeSpan(m_diff
- diff
.GetValue());
2217 inline wxTimeSpan
& wxTimeSpan::Subtract(const wxTimeSpan
& diff
)
2219 m_diff
-= diff
.GetValue();
2224 inline wxTimeSpan
& wxTimeSpan::Multiply(int n
)
2231 inline wxTimeSpan
wxTimeSpan::Multiply(int n
) const
2233 return wxTimeSpan(m_diff
* (long)n
);
2236 inline wxTimeSpan
wxTimeSpan::Abs() const
2238 return wxTimeSpan(GetValue().Abs());
2241 inline bool wxTimeSpan::IsEqualTo(const wxTimeSpan
& ts
) const
2243 return GetValue() == ts
.GetValue();
2246 inline bool wxTimeSpan::IsLongerThan(const wxTimeSpan
& ts
) const
2248 return GetValue().Abs() > ts
.GetValue().Abs();
2251 inline bool wxTimeSpan::IsShorterThan(const wxTimeSpan
& ts
) const
2253 return GetValue().Abs() < ts
.GetValue().Abs();
2256 // ----------------------------------------------------------------------------
2258 // ----------------------------------------------------------------------------
2260 inline wxDateSpan
& wxDateSpan::operator+=(const wxDateSpan
& other
)
2262 m_years
+= other
.m_years
;
2263 m_months
+= other
.m_months
;
2264 m_weeks
+= other
.m_weeks
;
2265 m_days
+= other
.m_days
;
2270 inline wxDateSpan
& wxDateSpan::Add(const wxDateSpan
& other
)
2272 return *this += other
;
2275 inline wxDateSpan
wxDateSpan::Add(const wxDateSpan
& other
) const
2277 wxDateSpan
ds(*this);
2282 inline wxDateSpan
& wxDateSpan::Multiply(int factor
)
2292 inline wxDateSpan
wxDateSpan::Multiply(int factor
) const
2294 wxDateSpan
ds(*this);
2295 ds
.Multiply(factor
);
2299 inline wxDateSpan
wxDateSpan::Negate() const
2301 return wxDateSpan(-m_years
, -m_months
, -m_weeks
, -m_days
);
2304 inline wxDateSpan
& wxDateSpan::Neg()
2307 m_months
= -m_months
;
2314 inline wxDateSpan
& wxDateSpan::operator-=(const wxDateSpan
& other
)
2316 return *this += other
.Negate();
2319 inline wxDateSpan
& wxDateSpan::Subtract(const wxDateSpan
& other
)
2321 return *this -= other
;
2324 inline wxDateSpan
wxDateSpan::Subtract(const wxDateSpan
& other
) const
2326 wxDateSpan
ds(*this);
2331 #undef MILLISECONDS_PER_DAY
2333 #undef MODIFY_AND_RETURN
2335 // ============================================================================
2337 // ============================================================================
2339 // ----------------------------------------------------------------------------
2340 // wxTimeSpan operators
2341 // ----------------------------------------------------------------------------
2343 wxTimeSpan WXDLLIMPEXP_BASE
operator*(int n
, const wxTimeSpan
& ts
);
2345 // ----------------------------------------------------------------------------
2347 // ----------------------------------------------------------------------------
2349 wxDateSpan WXDLLIMPEXP_BASE
operator*(int n
, const wxDateSpan
& ds
);
2351 // ============================================================================
2352 // other helper functions
2353 // ============================================================================
2355 // ----------------------------------------------------------------------------
2356 // iteration helpers: can be used to write a for loop over enum variable like
2358 // for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) )
2359 // ----------------------------------------------------------------------------
2361 WXDLLIMPEXP_BASE
void wxNextMonth(wxDateTime::Month
& m
);
2362 WXDLLIMPEXP_BASE
void wxPrevMonth(wxDateTime::Month
& m
);
2363 WXDLLIMPEXP_BASE
void wxNextWDay(wxDateTime::WeekDay
& wd
);
2364 WXDLLIMPEXP_BASE
void wxPrevWDay(wxDateTime::WeekDay
& wd
);
2366 #endif // wxUSE_DATETIME
2368 #endif // _WX_DATETIME_H