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
);
566 // get the full (default) or abbreviated month name in the current
567 // locale, returns empty string on error
568 static wxString
GetMonthName(Month month
,
569 NameFlags flags
= Name_Full
);
571 // get the standard English full (default) or abbreviated month name
572 static wxString
GetEnglishMonthName(Month month
,
573 NameFlags flags
= Name_Full
);
575 // get the full (default) or abbreviated weekday name in the current
576 // locale, returns empty string on error
577 static wxString
GetWeekDayName(WeekDay weekday
,
578 NameFlags flags
= Name_Full
);
580 // get the standard English full (default) or abbreviated weekday name
581 static wxString
GetEnglishWeekDayName(WeekDay weekday
,
582 NameFlags flags
= Name_Full
);
584 // get the AM and PM strings in the current locale (may be empty)
585 static void GetAmPmStrings(wxString
*am
, wxString
*pm
);
587 // return true if the given country uses DST for this year
588 static bool IsDSTApplicable(int year
= Inv_Year
,
589 Country country
= Country_Default
);
591 // get the beginning of DST for this year, will return invalid object
592 // if no DST applicable in this year. The default value of the
593 // parameter means to take the current year.
594 static wxDateTime
GetBeginDST(int year
= Inv_Year
,
595 Country country
= Country_Default
);
596 // get the end of DST for this year, will return invalid object
597 // if no DST applicable in this year. The default value of the
598 // parameter means to take the current year.
599 static wxDateTime
GetEndDST(int year
= Inv_Year
,
600 Country country
= Country_Default
);
602 // return the wxDateTime object for the current time
603 static inline wxDateTime
Now();
605 // return the wxDateTime object for the current time with millisecond
606 // precision (if available on this platform)
607 static wxDateTime
UNow();
609 // return the wxDateTime object for today midnight: i.e. as Now() but
610 // with time set to 0
611 static inline wxDateTime
Today();
613 // constructors: you should test whether the constructor succeeded with
614 // IsValid() function. The values Inv_Month and Inv_Year for the
615 // parameters mean take current month and/or year values.
616 // ------------------------------------------------------------------------
618 // default ctor does not initialize the object, use Set()!
619 wxDateTime() { m_time
= wxLongLong(wxINT32_MIN
, 0); }
621 // from time_t: seconds since the Epoch 00:00:00 UTC, Jan 1, 1970)
622 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
623 // VA C++ confuses this with wxDateTime(double jdn) thinking it is a duplicate declaration
624 inline wxDateTime(time_t timet
);
626 // from broken down time/date (only for standard Unix range)
627 inline wxDateTime(const struct tm
& tm
);
628 // from broken down time/date (any range)
629 inline wxDateTime(const Tm
& tm
);
631 // from JDN (beware of rounding errors)
632 inline wxDateTime(double jdn
);
634 // from separate values for each component, date set to today
635 inline wxDateTime(wxDateTime_t hour
,
636 wxDateTime_t minute
= 0,
637 wxDateTime_t second
= 0,
638 wxDateTime_t millisec
= 0);
639 // from separate values for each component with explicit date
640 inline wxDateTime(wxDateTime_t day
, // day of the month
642 int year
= Inv_Year
, // 1999, not 99 please!
643 wxDateTime_t hour
= 0,
644 wxDateTime_t minute
= 0,
645 wxDateTime_t second
= 0,
646 wxDateTime_t millisec
= 0);
648 wxDateTime(const struct _SYSTEMTIME
& st
)
650 SetFromMSWSysTime(st
);
654 // default copy ctor ok
658 // assignment operators and Set() functions: all non const methods return
659 // the reference to this object. IsValid() should be used to test whether
660 // the function succeeded.
661 // ------------------------------------------------------------------------
663 // set to the current time
664 inline wxDateTime
& SetToCurrent();
666 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
667 // VA C++ confuses this with wxDateTime(double jdn) thinking it is a duplicate declaration
668 // set to given time_t value
669 inline wxDateTime
& Set(time_t timet
);
672 // set to given broken down time/date
673 wxDateTime
& Set(const struct tm
& tm
);
675 // set to given broken down time/date
676 inline wxDateTime
& Set(const Tm
& tm
);
678 // set to given JDN (beware of rounding errors)
679 wxDateTime
& Set(double jdn
);
681 // set to given time, date = today
682 wxDateTime
& Set(wxDateTime_t hour
,
683 wxDateTime_t minute
= 0,
684 wxDateTime_t second
= 0,
685 wxDateTime_t millisec
= 0);
687 // from separate values for each component with explicit date
688 // (defaults for month and year are the current values)
689 wxDateTime
& Set(wxDateTime_t day
,
691 int year
= Inv_Year
, // 1999, not 99 please!
692 wxDateTime_t hour
= 0,
693 wxDateTime_t minute
= 0,
694 wxDateTime_t second
= 0,
695 wxDateTime_t millisec
= 0);
697 // resets time to 00:00:00, doesn't change the date
698 wxDateTime
& ResetTime();
700 // get the date part of this object only, i.e. the object which has the
701 // same date as this one but time of 00:00:00
702 wxDateTime
GetDateOnly() const;
704 // the following functions don't change the values of the other
705 // fields, i.e. SetMinute() won't change either hour or seconds value
708 wxDateTime
& SetYear(int year
);
710 wxDateTime
& SetMonth(Month month
);
711 // set the day of the month
712 wxDateTime
& SetDay(wxDateTime_t day
);
714 wxDateTime
& SetHour(wxDateTime_t hour
);
716 wxDateTime
& SetMinute(wxDateTime_t minute
);
718 wxDateTime
& SetSecond(wxDateTime_t second
);
720 wxDateTime
& SetMillisecond(wxDateTime_t millisecond
);
722 // assignment operator from time_t
723 wxDateTime
& operator=(time_t timet
) { return Set(timet
); }
725 // assignment operator from broken down time/date
726 wxDateTime
& operator=(const struct tm
& tm
) { return Set(tm
); }
728 // assignment operator from broken down time/date
729 wxDateTime
& operator=(const Tm
& tm
) { return Set(tm
); }
731 // default assignment operator is ok
733 // calendar calculations (functions which set the date only leave the time
734 // unchanged, e.g. don't explictly zero it): SetXXX() functions modify the
735 // object itself, GetXXX() ones return a new object.
736 // ------------------------------------------------------------------------
738 // set to the given week day in the same week as this one
739 wxDateTime
& SetToWeekDayInSameWeek(WeekDay weekday
,
740 WeekFlags flags
= Monday_First
);
741 inline wxDateTime
GetWeekDayInSameWeek(WeekDay weekday
,
742 WeekFlags flags
= Monday_First
) const;
744 // set to the next week day following this one
745 wxDateTime
& SetToNextWeekDay(WeekDay weekday
);
746 inline wxDateTime
GetNextWeekDay(WeekDay weekday
) const;
748 // set to the previous week day before this one
749 wxDateTime
& SetToPrevWeekDay(WeekDay weekday
);
750 inline wxDateTime
GetPrevWeekDay(WeekDay weekday
) const;
752 // set to Nth occurrence of given weekday in the given month of the
753 // given year (time is set to 0), return true on success and false on
754 // failure. n may be positive (1..5) or negative to count from the end
755 // of the month (see helper function SetToLastWeekDay())
756 bool SetToWeekDay(WeekDay weekday
,
758 Month month
= Inv_Month
,
759 int year
= Inv_Year
);
760 inline wxDateTime
GetWeekDay(WeekDay weekday
,
762 Month month
= Inv_Month
,
763 int year
= Inv_Year
) const;
765 // sets to the last weekday in the given month, year
766 inline bool SetToLastWeekDay(WeekDay weekday
,
767 Month month
= Inv_Month
,
768 int year
= Inv_Year
);
769 inline wxDateTime
GetLastWeekDay(WeekDay weekday
,
770 Month month
= Inv_Month
,
771 int year
= Inv_Year
);
773 #if WXWIN_COMPATIBILITY_2_6
774 // sets the date to the given day of the given week in the year,
775 // returns true on success and false if given date doesn't exist (e.g.
778 // these functions are badly defined as they're not the reverse of
779 // GetWeekOfYear(), use SetToTheWeekOfYear() instead
780 wxDEPRECATED( bool SetToTheWeek(wxDateTime_t numWeek
,
781 WeekDay weekday
= Mon
,
782 WeekFlags flags
= Monday_First
) );
783 wxDEPRECATED( wxDateTime
GetWeek(wxDateTime_t numWeek
,
784 WeekDay weekday
= Mon
,
785 WeekFlags flags
= Monday_First
) const );
786 #endif // WXWIN_COMPATIBILITY_2_6
788 // returns the date corresponding to the given week day of the given
789 // week (in ISO notation) of the specified year
790 static wxDateTime
SetToWeekOfYear(int year
,
791 wxDateTime_t numWeek
,
792 WeekDay weekday
= Mon
);
794 // sets the date to the last day of the given (or current) month or the
795 // given (or current) year
796 wxDateTime
& SetToLastMonthDay(Month month
= Inv_Month
,
797 int year
= Inv_Year
);
798 inline wxDateTime
GetLastMonthDay(Month month
= Inv_Month
,
799 int year
= Inv_Year
) const;
801 // sets to the given year day (1..365 or 366)
802 wxDateTime
& SetToYearDay(wxDateTime_t yday
);
803 inline wxDateTime
GetYearDay(wxDateTime_t yday
) const;
805 // The definitions below were taken verbatim from
807 // http://www.capecod.net/~pbaum/date/date0.htm
809 // (Peter Baum's home page)
811 // definition: The Julian Day Number, Julian Day, or JD of a
812 // particular instant of time is the number of days and fractions of a
813 // day since 12 hours Universal Time (Greenwich mean noon) on January
814 // 1 of the year -4712, where the year is given in the Julian
815 // proleptic calendar. The idea of using this reference date was
816 // originally proposed by Joseph Scalizer in 1582 to count years but
817 // it was modified by 19th century astronomers to count days. One
818 // could have equivalently defined the reference time to be noon of
819 // November 24, -4713 if were understood that Gregorian calendar rules
820 // were applied. Julian days are Julian Day Numbers and are not to be
821 // confused with Julian dates.
823 // definition: The Rata Die number is a date specified as the number
824 // of days relative to a base date of December 31 of the year 0. Thus
825 // January 1 of the year 1 is Rata Die day 1.
827 // get the Julian Day number (the fractional part specifies the time of
828 // the day, related to noon - beware of rounding errors!)
829 double GetJulianDayNumber() const;
830 double GetJDN() const { return GetJulianDayNumber(); }
832 // get the Modified Julian Day number: it is equal to JDN - 2400000.5
833 // and so integral MJDs correspond to the midnights (and not noons).
834 // MJD 0 is Nov 17, 1858
835 double GetModifiedJulianDayNumber() const { return GetJDN() - 2400000.5; }
836 double GetMJD() const { return GetModifiedJulianDayNumber(); }
838 // get the Rata Die number
839 double GetRataDie() const;
841 // TODO algorithms for calculating some important dates, such as
842 // religious holidays (Easter...) or moon/solar eclipses? Some
843 // algorithms can be found in the calendar FAQ
846 // Timezone stuff: a wxDateTime object constructed using given
847 // day/month/year/hour/min/sec values is interpreted as this moment in
848 // local time. Using the functions below, it may be converted to another
849 // time zone (e.g., the Unix epoch is wxDateTime(1, Jan, 1970).ToGMT()).
851 // These functions try to handle DST internally, but there is no magical
852 // way to know all rules for it in all countries in the world, so if the
853 // program can handle it itself (or doesn't want to handle it at all for
854 // whatever reason), the DST handling can be disabled with noDST.
855 // ------------------------------------------------------------------------
857 // transform to any given timezone
858 inline wxDateTime
ToTimezone(const TimeZone
& tz
, bool noDST
= false) const;
859 wxDateTime
& MakeTimezone(const TimeZone
& tz
, bool noDST
= false);
861 // interpret current value as being in another timezone and transform
863 inline wxDateTime
FromTimezone(const TimeZone
& tz
, bool noDST
= false) const;
864 wxDateTime
& MakeFromTimezone(const TimeZone
& tz
, bool noDST
= false);
866 // transform to/from GMT/UTC
867 wxDateTime
ToUTC(bool noDST
= false) const { return ToTimezone(UTC
, noDST
); }
868 wxDateTime
& MakeUTC(bool noDST
= false) { return MakeTimezone(UTC
, noDST
); }
870 wxDateTime
ToGMT(bool noDST
= false) const { return ToUTC(noDST
); }
871 wxDateTime
& MakeGMT(bool noDST
= false) { return MakeUTC(noDST
); }
873 wxDateTime
FromUTC(bool noDST
= false) const
874 { return FromTimezone(UTC
, noDST
); }
875 wxDateTime
& MakeFromUTC(bool noDST
= false)
876 { return MakeFromTimezone(UTC
, noDST
); }
878 // is daylight savings time in effect at this moment according to the
879 // rules of the specified country?
881 // Return value is > 0 if DST is in effect, 0 if it is not and -1 if
882 // the information is not available (this is compatible with ANSI C)
883 int IsDST(Country country
= Country_Default
) const;
886 // accessors: many of them take the timezone parameter which indicates the
887 // timezone for which to make the calculations and the default value means
888 // to do it for the current timezone of this machine (even if the function
889 // only operates with the date it's necessary because a date may wrap as
890 // result of timezone shift)
891 // ------------------------------------------------------------------------
893 // is the date valid?
894 inline bool IsValid() const { return m_time
!= wxInvalidDateTime
.m_time
; }
896 // get the broken down date/time representation in the given timezone
898 // If you wish to get several time components (day, month and year),
899 // consider getting the whole Tm strcuture first and retrieving the
900 // value from it - this is much more efficient
901 Tm
GetTm(const TimeZone
& tz
= Local
) const;
903 // get the number of seconds since the Unix epoch - returns (time_t)-1
904 // if the value is out of range
905 inline time_t GetTicks() const;
907 // get the century, same as GetCentury(GetYear())
908 int GetCentury(const TimeZone
& tz
= Local
) const
909 { return GetCentury(GetYear(tz
)); }
910 // get the year (returns Inv_Year if date is invalid)
911 int GetYear(const TimeZone
& tz
= Local
) const
912 { return GetTm(tz
).year
; }
913 // get the month (Inv_Month if date is invalid)
914 Month
GetMonth(const TimeZone
& tz
= Local
) const
915 { return (Month
)GetTm(tz
).mon
; }
916 // get the month day (in 1..31 range, 0 if date is invalid)
917 wxDateTime_t
GetDay(const TimeZone
& tz
= Local
) const
918 { return GetTm(tz
).mday
; }
919 // get the day of the week (Inv_WeekDay if date is invalid)
920 WeekDay
GetWeekDay(const TimeZone
& tz
= Local
) const
921 { return GetTm(tz
).GetWeekDay(); }
922 // get the hour of the day
923 wxDateTime_t
GetHour(const TimeZone
& tz
= Local
) const
924 { return GetTm(tz
).hour
; }
926 wxDateTime_t
GetMinute(const TimeZone
& tz
= Local
) const
927 { return GetTm(tz
).min
; }
929 wxDateTime_t
GetSecond(const TimeZone
& tz
= Local
) const
930 { return GetTm(tz
).sec
; }
932 wxDateTime_t
GetMillisecond(const TimeZone
& tz
= Local
) const
933 { return GetTm(tz
).msec
; }
935 // get the day since the year start (1..366, 0 if date is invalid)
936 wxDateTime_t
GetDayOfYear(const TimeZone
& tz
= Local
) const;
937 // get the week number since the year start (1..52 or 53, 0 if date is
939 wxDateTime_t
GetWeekOfYear(WeekFlags flags
= Monday_First
,
940 const TimeZone
& tz
= Local
) const;
941 // get the week number since the month start (1..5, 0 if date is
943 wxDateTime_t
GetWeekOfMonth(WeekFlags flags
= Monday_First
,
944 const TimeZone
& tz
= Local
) const;
946 // is this date a work day? This depends on a country, of course,
947 // because the holidays are different in different countries
948 bool IsWorkDay(Country country
= Country_Default
) const;
950 // is this date later than Gregorian calendar introduction for the
951 // given country (see enum GregorianAdoption)?
953 // NB: this function shouldn't be considered as absolute authority in
954 // the matter. Besides, for some countries the exact date of
955 // adoption of the Gregorian calendar is simply unknown.
956 bool IsGregorianDate(GregorianAdoption country
= Gr_Standard
) const;
958 // dos date and time format
959 // ------------------------------------------------------------------------
961 // set from the DOS packed format
962 wxDateTime
& SetFromDOS(unsigned long ddt
);
964 // pack the date in DOS format
965 unsigned long GetAsDOS() const;
968 // ------------------------------------------------------------------------
971 // convert SYSTEMTIME to wxDateTime
972 wxDateTime
& SetFromMSWSysTime(const struct _SYSTEMTIME
&);
974 // convert wxDateTime to SYSTEMTIME
975 void GetAsMSWSysTime(struct _SYSTEMTIME
*) const;
978 // comparison (see also functions below for operator versions)
979 // ------------------------------------------------------------------------
981 // returns true if the two moments are strictly identical
982 inline bool IsEqualTo(const wxDateTime
& datetime
) const;
984 // returns true if the date is strictly earlier than the given one
985 inline bool IsEarlierThan(const wxDateTime
& datetime
) const;
987 // returns true if the date is strictly later than the given one
988 inline bool IsLaterThan(const wxDateTime
& datetime
) const;
990 // returns true if the date is strictly in the given range
991 inline bool IsStrictlyBetween(const wxDateTime
& t1
,
992 const wxDateTime
& t2
) const;
994 // returns true if the date is in the given range
995 inline bool IsBetween(const wxDateTime
& t1
, const wxDateTime
& t2
) const;
997 // do these two objects refer to the same date?
998 inline bool IsSameDate(const wxDateTime
& dt
) const;
1000 // do these two objects have the same time?
1001 inline bool IsSameTime(const wxDateTime
& dt
) const;
1003 // are these two objects equal up to given timespan?
1004 inline bool IsEqualUpTo(const wxDateTime
& dt
, const wxTimeSpan
& ts
) const;
1006 inline bool operator<(const wxDateTime
& dt
) const
1008 wxASSERT_MSG( IsValid() && dt
.IsValid(), _T("invalid wxDateTime") );
1009 return GetValue() < dt
.GetValue();
1012 inline bool operator<=(const wxDateTime
& dt
) const
1014 wxASSERT_MSG( IsValid() && dt
.IsValid(), _T("invalid wxDateTime") );
1015 return GetValue() <= dt
.GetValue();
1018 inline bool operator>(const wxDateTime
& dt
) const
1020 wxASSERT_MSG( IsValid() && dt
.IsValid(), _T("invalid wxDateTime") );
1021 return GetValue() > dt
.GetValue();
1024 inline bool operator>=(const wxDateTime
& dt
) const
1026 wxASSERT_MSG( IsValid() && dt
.IsValid(), _T("invalid wxDateTime") );
1027 return GetValue() >= dt
.GetValue();
1030 inline bool operator==(const wxDateTime
& dt
) const
1032 wxASSERT_MSG( IsValid() && dt
.IsValid(), _T("invalid wxDateTime") );
1033 return GetValue() == dt
.GetValue();
1036 inline bool operator!=(const wxDateTime
& dt
) const
1038 wxASSERT_MSG( IsValid() && dt
.IsValid(), _T("invalid wxDateTime") );
1039 return GetValue() != dt
.GetValue();
1042 // arithmetics with dates (see also below for more operators)
1043 // ------------------------------------------------------------------------
1045 // return the sum of the date with a time span (positive or negative)
1046 inline wxDateTime
Add(const wxTimeSpan
& diff
) const;
1047 // add a time span (positive or negative)
1048 inline wxDateTime
& Add(const wxTimeSpan
& diff
);
1049 // add a time span (positive or negative)
1050 inline wxDateTime
& operator+=(const wxTimeSpan
& diff
);
1051 inline wxDateTime
operator+(const wxTimeSpan
& ts
) const
1053 wxDateTime
dt(*this);
1058 // return the difference of the date with a time span
1059 inline wxDateTime
Subtract(const wxTimeSpan
& diff
) const;
1060 // subtract a time span (positive or negative)
1061 inline wxDateTime
& Subtract(const wxTimeSpan
& diff
);
1062 // subtract a time span (positive or negative)
1063 inline wxDateTime
& operator-=(const wxTimeSpan
& diff
);
1064 inline wxDateTime
operator-(const wxTimeSpan
& ts
) const
1066 wxDateTime
dt(*this);
1071 // return the sum of the date with a date span
1072 inline wxDateTime
Add(const wxDateSpan
& diff
) const;
1073 // add a date span (positive or negative)
1074 wxDateTime
& Add(const wxDateSpan
& diff
);
1075 // add a date span (positive or negative)
1076 inline wxDateTime
& operator+=(const wxDateSpan
& diff
);
1077 inline wxDateTime
operator+(const wxDateSpan
& ds
) const
1079 wxDateTime
dt(*this);
1084 // return the difference of the date with a date span
1085 inline wxDateTime
Subtract(const wxDateSpan
& diff
) const;
1086 // subtract a date span (positive or negative)
1087 inline wxDateTime
& Subtract(const wxDateSpan
& diff
);
1088 // subtract a date span (positive or negative)
1089 inline wxDateTime
& operator-=(const wxDateSpan
& diff
);
1090 inline wxDateTime
operator-(const wxDateSpan
& ds
) const
1092 wxDateTime
dt(*this);
1097 // return the difference between two dates
1098 inline wxTimeSpan
Subtract(const wxDateTime
& dt
) const;
1099 inline wxTimeSpan
operator-(const wxDateTime
& dt2
) const;
1101 // conversion to/from text: all conversions from text return the pointer to
1102 // the next character following the date specification (i.e. the one where
1103 // the scan had to stop) or NULL on failure; for the versions taking
1104 // wxString or wxCStrData, we don't know if the user code needs char* or
1105 // wchar_t* pointer and so we return char* one for compatibility with the
1106 // existing ANSI code and also return iterator in another output parameter
1107 // (it will be equal to end if the entire string was parsed)
1108 // ------------------------------------------------------------------------
1110 // parse a string in RFC 822 format (found e.g. in mail headers and
1111 // having the form "Wed, 10 Feb 1999 19:07:07 +0100")
1112 const char *ParseRfc822Date(const wxString
& date
,
1113 wxString::const_iterator
*end
= NULL
);
1114 const char *ParseRfc822Date(const wxCStrData
& date
,
1115 wxString::const_iterator
*end
= NULL
)
1117 return ParseRfc822Date(date
.AsString(), end
);
1120 const wchar_t *ParseRfc822Date(const wchar_t* date
)
1122 return ReturnEndAsWidePtr(&wxDateTime::ParseRfc822Date
, date
);
1125 const char *ParseRfc822Date(const char* date
)
1127 return ParseRfc822Date(wxString(date
));
1130 // parse a date/time in the given format (see strptime(3)), fill in
1131 // the missing (in the string) fields with the values of dateDef (by
1132 // default, they will not change if they had valid values or will
1133 // default to Today() otherwise)
1135 // notice that we unfortunately need all those overloads because we use
1136 // the type of the date string to select the return value of the
1137 // function: it's wchar_t if a wide string is passed for compatibility
1138 // with the code doing "const wxChar *p = dt.ParseFormat(_T("..."))",
1139 // but char* in all other cases for compatibility with ANSI build which
1140 // allowed code like "const char *p = dt.ParseFormat("...")"
1142 // so we need wchar_t overload and now passing s.c_str() as first
1143 // argument is ambiguous because it's convertible to both wxString and
1144 // wchar_t* and now it's passing char* which becomes ambiguous as it is
1145 // convertible to both wxString and wxCStrData hence we need char*
1148 // and to make our life more miserable we also pay for having the
1149 // optional dateDef parameter: as it's almost never used, we want to
1150 // allow people to omit it when specifying the end iterator output
1151 // parameter but we still have to allow specifying dateDef too, so we
1152 // need another overload for this
1154 // FIXME: all this mess could be avoided by using some class similar to
1155 // wxFormatString, i.e. remembering string [pointer] of any type
1156 // and convertible to either char* or wchar_t* as wxCStrData and
1157 // having only 1 (or 2, because of the last paragraph above)
1158 // overload taking it, see #9560
1159 const char *ParseFormat(const wxString
& date
,
1160 const wxString
& format
= wxDefaultDateTimeFormat
,
1161 const wxDateTime
& dateDef
= wxDefaultDateTime
,
1162 wxString::const_iterator
*end
= NULL
);
1164 const char *ParseFormat(const wxString
& date
,
1165 const wxString
& format
,
1166 wxString::const_iterator
*end
)
1168 return ParseFormat(date
, format
, wxDefaultDateTime
, end
);
1171 const char *ParseFormat(const wxCStrData
& date
,
1172 const wxString
& format
= wxDefaultDateTimeFormat
,
1173 const wxDateTime
& dateDef
= wxDefaultDateTime
,
1174 wxString::const_iterator
*end
= NULL
)
1176 return ParseFormat(date
.AsString(), format
, dateDef
, end
);
1179 const wchar_t *ParseFormat(const wchar_t *date
,
1180 const wxString
& format
= wxDefaultDateTimeFormat
,
1181 const wxDateTime
& dateDef
= wxDefaultDateTime
)
1183 const wxString
datestr(date
);
1184 wxString::const_iterator end
;
1185 if ( !ParseFormat(datestr
, format
, dateDef
, &end
) )
1188 return date
+ (end
- datestr
.begin());
1191 const char *ParseFormat(const char *date
,
1192 const wxString
& format
= "%c",
1193 const wxDateTime
& dateDef
= wxDefaultDateTime
)
1195 return ParseFormat(wxString(date
), format
, dateDef
);
1199 // parse a string containing date, time or both in ISO 8601 format
1201 // notice that these functions are new in wx 3.0 and so we don't
1202 // provide compatibility overloads for them
1203 bool ParseISODate(const wxString
& date
)
1205 wxString::const_iterator end
;
1206 return ParseFormat(date
, wxS("%Y-%m-%d"), &end
) && end
== date
.end();
1209 bool ParseISOTime(const wxString
& time
)
1211 wxString::const_iterator end
;
1212 return ParseFormat(time
, wxS("%H:%M:%S"), &end
) && end
== time
.end();
1215 bool ParseISOCombined(const wxString
& datetime
, char sep
= 'T')
1217 wxString::const_iterator end
;
1218 const wxString fmt
= wxS("%Y-%m-%d") + wxString(sep
) + wxS("%H:%M:%S");
1219 return ParseFormat(datetime
, fmt
, &end
) && end
== datetime
.end();
1222 // parse a string containing the date/time in "free" format, this
1223 // function will try to make an educated guess at the string contents
1224 const char *ParseDateTime(const wxString
& datetime
,
1225 wxString::const_iterator
*end
= NULL
);
1227 const char *ParseDateTime(const wxCStrData
& datetime
,
1228 wxString::const_iterator
*end
= NULL
)
1230 return ParseDateTime(datetime
.AsString(), end
);
1233 const wchar_t *ParseDateTime(const wchar_t *datetime
)
1235 return ReturnEndAsWidePtr(&wxDateTime::ParseDateTime
, datetime
);
1238 const char *ParseDateTime(const char *datetime
)
1240 return ParseDateTime(wxString(datetime
));
1243 // parse a string containing the date only in "free" format (less
1244 // flexible than ParseDateTime)
1245 const char *ParseDate(const wxString
& date
,
1246 wxString::const_iterator
*end
= NULL
);
1248 const char *ParseDate(const wxCStrData
& date
,
1249 wxString::const_iterator
*end
= NULL
)
1251 return ParseDate(date
.AsString(), end
);
1254 const wchar_t *ParseDate(const wchar_t *date
)
1256 return ReturnEndAsWidePtr(&wxDateTime::ParseDate
, date
);
1259 const char *ParseDate(const char *date
)
1261 return ParseDate(wxString(date
));
1264 // parse a string containing the time only in "free" format
1265 const char *ParseTime(const wxString
& time
,
1266 wxString::const_iterator
*end
= NULL
);
1268 const char *ParseTime(const wxCStrData
& time
,
1269 wxString::const_iterator
*end
= NULL
)
1271 return ParseTime(time
.AsString(), end
);
1274 const wchar_t *ParseTime(const wchar_t *time
)
1276 return ReturnEndAsWidePtr(&wxDateTime::ParseTime
, time
);
1279 const char *ParseTime(const char *time
)
1281 return ParseTime(wxString(time
));
1284 // this function accepts strftime()-like format string (default
1285 // argument corresponds to the preferred date and time representation
1286 // for the current locale) and returns the string containing the
1287 // resulting text representation
1288 wxString
Format(const wxString
& format
= wxDefaultDateTimeFormat
,
1289 const TimeZone
& tz
= Local
) const;
1290 // preferred date representation for the current locale
1291 wxString
FormatDate() const { return Format(wxS("%x")); }
1292 // preferred time representation for the current locale
1293 wxString
FormatTime() const { return Format(wxS("%X")); }
1294 // returns the string representing the date in ISO 8601 format
1296 wxString
FormatISODate() const { return Format(wxS("%Y-%m-%d")); }
1297 // returns the string representing the time in ISO 8601 format
1299 wxString
FormatISOTime() const { return Format(wxS("%H:%M:%S")); }
1300 // return the combined date time representation in ISO 8601 format; the
1301 // separator character should be 'T' according to the standard but it
1302 // can also be useful to set it to ' '
1303 wxString
FormatISOCombined(char sep
= 'T') const
1304 { return FormatISODate() + sep
+ FormatISOTime(); }
1307 // ------------------------------------------------------------------------
1309 // construct from internal representation
1310 wxDateTime(const wxLongLong
& time
) { m_time
= time
; }
1312 // get the internal representation
1313 inline wxLongLong
GetValue() const;
1315 // a helper function to get the current time_t
1316 static time_t GetTimeNow() { return time(NULL
); }
1318 // another one to get the current time broken down
1319 static struct tm
*GetTmNow()
1321 static struct tm l_CurrentTime
;
1322 return GetTmNow(&l_CurrentTime
);
1325 // get current time using thread-safe function
1326 static struct tm
*GetTmNow(struct tm
*tmstruct
);
1329 // helper function for defining backward-compatible wrappers for code
1330 // using wchar_t* pointer instead of wxString iterators
1332 const char *(wxDateTime::*StringMethod
)(const wxString
& s
,
1333 wxString::const_iterator
*end
);
1335 const wchar_t *ReturnEndAsWidePtr(StringMethod func
, const wchar_t *p
)
1337 const wxString
s(p
);
1338 wxString::const_iterator end
;
1339 if ( !(this->*func
)(s
, &end
) )
1342 return p
+ (end
- s
.begin());
1346 // the current country - as it's the same for all program objects (unless
1347 // it runs on a _really_ big cluster system :-), this is a static member:
1348 // see SetCountry() and GetCountry()
1349 static Country ms_country
;
1351 // this constant is used to transform a time_t value to the internal
1352 // representation, as time_t is in seconds and we use milliseconds it's
1354 static const long TIME_T_FACTOR
;
1356 // returns true if we fall in range in which we can use standard ANSI C
1358 inline bool IsInStdRange() const;
1360 // the internal representation of the time is the amount of milliseconds
1361 // elapsed since the origin which is set by convention to the UNIX/C epoch
1362 // value: the midnight of January 1, 1970 (UTC)
1366 // ----------------------------------------------------------------------------
1367 // This class contains a difference between 2 wxDateTime values, so it makes
1368 // sense to add it to wxDateTime and it is the result of subtraction of 2
1369 // objects of that class. See also wxDateSpan.
1370 // ----------------------------------------------------------------------------
1372 class WXDLLIMPEXP_BASE wxTimeSpan
1376 // ------------------------------------------------------------------------
1378 // return the timespan for the given number of milliseconds
1379 static wxTimeSpan
Milliseconds(wxLongLong ms
) { return wxTimeSpan(0, 0, 0, ms
); }
1380 static wxTimeSpan
Millisecond() { return Milliseconds(1); }
1382 // return the timespan for the given number of seconds
1383 static wxTimeSpan
Seconds(wxLongLong sec
) { return wxTimeSpan(0, 0, sec
); }
1384 static wxTimeSpan
Second() { return Seconds(1); }
1386 // return the timespan for the given number of minutes
1387 static wxTimeSpan
Minutes(long min
) { return wxTimeSpan(0, min
, 0 ); }
1388 static wxTimeSpan
Minute() { return Minutes(1); }
1390 // return the timespan for the given number of hours
1391 static wxTimeSpan
Hours(long hours
) { return wxTimeSpan(hours
, 0, 0); }
1392 static wxTimeSpan
Hour() { return Hours(1); }
1394 // return the timespan for the given number of days
1395 static wxTimeSpan
Days(long days
) { return Hours(24 * days
); }
1396 static wxTimeSpan
Day() { return Days(1); }
1398 // return the timespan for the given number of weeks
1399 static wxTimeSpan
Weeks(long days
) { return Days(7 * days
); }
1400 static wxTimeSpan
Week() { return Weeks(1); }
1402 // default ctor constructs the 0 time span
1405 // from separate values for each component, date set to 0 (hours are
1406 // not restricted to 0..24 range, neither are minutes, seconds or
1408 inline wxTimeSpan(long hours
,
1410 wxLongLong seconds
= 0,
1411 wxLongLong milliseconds
= 0);
1413 // default copy ctor is ok
1417 // arithmetics with time spans (see also below for more operators)
1418 // ------------------------------------------------------------------------
1420 // return the sum of two timespans
1421 inline wxTimeSpan
Add(const wxTimeSpan
& diff
) const;
1422 // add two timespans together
1423 inline wxTimeSpan
& Add(const wxTimeSpan
& diff
);
1424 // add two timespans together
1425 wxTimeSpan
& operator+=(const wxTimeSpan
& diff
) { return Add(diff
); }
1426 inline wxTimeSpan
operator+(const wxTimeSpan
& ts
) const
1428 return wxTimeSpan(GetValue() + ts
.GetValue());
1431 // return the difference of two timespans
1432 inline wxTimeSpan
Subtract(const wxTimeSpan
& diff
) const;
1433 // subtract another timespan
1434 inline wxTimeSpan
& Subtract(const wxTimeSpan
& diff
);
1435 // subtract another timespan
1436 wxTimeSpan
& operator-=(const wxTimeSpan
& diff
) { return Subtract(diff
); }
1437 inline wxTimeSpan
operator-(const wxTimeSpan
& ts
)
1439 return wxTimeSpan(GetValue() - ts
.GetValue());
1442 // multiply timespan by a scalar
1443 inline wxTimeSpan
Multiply(int n
) const;
1444 // multiply timespan by a scalar
1445 inline wxTimeSpan
& Multiply(int n
);
1446 // multiply timespan by a scalar
1447 wxTimeSpan
& operator*=(int n
) { return Multiply(n
); }
1448 inline wxTimeSpan
operator*(int n
) const
1450 return wxTimeSpan(*this).Multiply(n
);
1453 // return this timespan with opposite sign
1454 wxTimeSpan
Negate() const { return wxTimeSpan(-GetValue()); }
1455 // negate the value of the timespan
1456 wxTimeSpan
& Neg() { m_diff
= -GetValue(); return *this; }
1457 // negate the value of the timespan
1458 wxTimeSpan
& operator-() { return Neg(); }
1460 // return the absolute value of the timespan: does _not_ modify the
1462 inline wxTimeSpan
Abs() const;
1464 // there is intentionally no division because we don't want to
1465 // introduce rounding errors in time calculations
1467 // comparaison (see also operator versions below)
1468 // ------------------------------------------------------------------------
1470 // is the timespan null?
1471 bool IsNull() const { return m_diff
== 0l; }
1472 // returns true if the timespan is null
1473 bool operator!() const { return !IsNull(); }
1475 // is the timespan positive?
1476 bool IsPositive() const { return m_diff
> 0l; }
1478 // is the timespan negative?
1479 bool IsNegative() const { return m_diff
< 0l; }
1481 // are two timespans equal?
1482 inline bool IsEqualTo(const wxTimeSpan
& ts
) const;
1483 // compare two timestamps: works with the absolute values, i.e. -2
1484 // hours is longer than 1 hour. Also, it will return false if the
1485 // timespans are equal in absolute value.
1486 inline bool IsLongerThan(const wxTimeSpan
& ts
) const;
1487 // compare two timestamps: works with the absolute values, i.e. 1
1488 // hour is shorter than -2 hours. Also, it will return false if the
1489 // timespans are equal in absolute value.
1490 bool IsShorterThan(const wxTimeSpan
& t
) const;
1492 inline bool operator<(const wxTimeSpan
&ts
) const
1494 return GetValue() < ts
.GetValue();
1497 inline bool operator<=(const wxTimeSpan
&ts
) const
1499 return GetValue() <= ts
.GetValue();
1502 inline bool operator>(const wxTimeSpan
&ts
) const
1504 return GetValue() > ts
.GetValue();
1507 inline bool operator>=(const wxTimeSpan
&ts
) const
1509 return GetValue() >= ts
.GetValue();
1512 inline bool operator==(const wxTimeSpan
&ts
) const
1514 return GetValue() == ts
.GetValue();
1517 inline bool operator!=(const wxTimeSpan
&ts
) const
1519 return GetValue() != ts
.GetValue();
1522 // breaking into days, hours, minutes and seconds
1523 // ------------------------------------------------------------------------
1525 // get the max number of weeks in this timespan
1526 inline int GetWeeks() const;
1527 // get the max number of days in this timespan
1528 inline int GetDays() const;
1529 // get the max number of hours in this timespan
1530 inline int GetHours() const;
1531 // get the max number of minutes in this timespan
1532 inline int GetMinutes() const;
1533 // get the max number of seconds in this timespan
1534 inline wxLongLong
GetSeconds() const;
1535 // get the number of milliseconds in this timespan
1536 wxLongLong
GetMilliseconds() const { return m_diff
; }
1538 // conversion to text
1539 // ------------------------------------------------------------------------
1541 // this function accepts strftime()-like format string (default
1542 // argument corresponds to the preferred date and time representation
1543 // for the current locale) and returns the string containing the
1544 // resulting text representation. Notice that only some of format
1545 // specifiers valid for wxDateTime are valid for wxTimeSpan: hours,
1546 // minutes and seconds make sense, but not "PM/AM" string for example.
1547 wxString
Format(const wxString
& format
= wxDefaultTimeSpanFormat
) const;
1550 // ------------------------------------------------------------------------
1552 // construct from internal representation
1553 wxTimeSpan(const wxLongLong
& diff
) { m_diff
= diff
; }
1555 // get the internal representation
1556 wxLongLong
GetValue() const { return m_diff
; }
1559 // the (signed) time span in milliseconds
1563 // ----------------------------------------------------------------------------
1564 // This class is a "logical time span" and is useful for implementing program
1565 // logic for such things as "add one month to the date" which, in general,
1566 // doesn't mean to add 60*60*24*31 seconds to it, but to take the same date
1567 // the next month (to understand that this is indeed different consider adding
1568 // one month to Feb, 15 - we want to get Mar, 15, of course).
1570 // When adding a month to the date, all lesser components (days, hours, ...)
1571 // won't be changed unless the resulting date would be invalid: for example,
1572 // Jan 31 + 1 month will be Feb 28, not (non existing) Feb 31.
1574 // Because of this feature, adding and subtracting back again the same
1575 // wxDateSpan will *not*, in general give back the original date: Feb 28 - 1
1576 // month will be Jan 28, not Jan 31!
1578 // wxDateSpan can be either positive or negative. They may be
1579 // multiplied by scalars which multiply all deltas by the scalar: i.e. 2*(1
1580 // month and 1 day) is 2 months and 2 days. They can be added together and
1581 // with wxDateTime or wxTimeSpan, but the type of result is different for each
1584 // Beware about weeks: if you specify both weeks and days, the total number of
1585 // days added will be 7*weeks + days! See also GetTotalDays() function.
1587 // Equality operators are defined for wxDateSpans. Two datespans are equal if
1588 // they both give the same target date when added to *every* source date.
1589 // Thus wxDateSpan::Months(1) is not equal to wxDateSpan::Days(30), because
1590 // they not give the same date when added to 1 Feb. But wxDateSpan::Days(14) is
1591 // equal to wxDateSpan::Weeks(2)
1593 // Finally, notice that for adding hours, minutes &c you don't need this
1594 // class: wxTimeSpan will do the job because there are no subtleties
1595 // associated with those.
1596 // ----------------------------------------------------------------------------
1598 class WXDLLIMPEXP_BASE wxDateSpan
1602 // ------------------------------------------------------------------------
1604 // this many years/months/weeks/days
1605 wxDateSpan(int years
= 0, int months
= 0, int weeks
= 0, int days
= 0)
1613 // get an object for the given number of days
1614 static wxDateSpan
Days(int days
) { return wxDateSpan(0, 0, 0, days
); }
1615 static wxDateSpan
Day() { return Days(1); }
1617 // get an object for the given number of weeks
1618 static wxDateSpan
Weeks(int weeks
) { return wxDateSpan(0, 0, weeks
, 0); }
1619 static wxDateSpan
Week() { return Weeks(1); }
1621 // get an object for the given number of months
1622 static wxDateSpan
Months(int mon
) { return wxDateSpan(0, mon
, 0, 0); }
1623 static wxDateSpan
Month() { return Months(1); }
1625 // get an object for the given number of years
1626 static wxDateSpan
Years(int years
) { return wxDateSpan(years
, 0, 0, 0); }
1627 static wxDateSpan
Year() { return Years(1); }
1629 // default copy ctor is ok
1633 // accessors (all SetXXX() return the (modified) wxDateSpan object)
1634 // ------------------------------------------------------------------------
1636 // set number of years
1637 wxDateSpan
& SetYears(int n
) { m_years
= n
; return *this; }
1638 // set number of months
1639 wxDateSpan
& SetMonths(int n
) { m_months
= n
; return *this; }
1640 // set number of weeks
1641 wxDateSpan
& SetWeeks(int n
) { m_weeks
= n
; return *this; }
1642 // set number of days
1643 wxDateSpan
& SetDays(int n
) { m_days
= n
; return *this; }
1645 // get number of years
1646 int GetYears() const { return m_years
; }
1647 // get number of months
1648 int GetMonths() const { return m_months
; }
1649 // get number of weeks
1650 int GetWeeks() const { return m_weeks
; }
1651 // get number of days
1652 int GetDays() const { return m_days
; }
1653 // returns 7*GetWeeks() + GetDays()
1654 int GetTotalDays() const { return 7*m_weeks
+ m_days
; }
1656 // arithmetics with date spans (see also below for more operators)
1657 // ------------------------------------------------------------------------
1659 // return sum of two date spans
1660 inline wxDateSpan
Add(const wxDateSpan
& other
) const;
1661 // add another wxDateSpan to us
1662 inline wxDateSpan
& Add(const wxDateSpan
& other
);
1663 // add another wxDateSpan to us
1664 inline wxDateSpan
& operator+=(const wxDateSpan
& other
);
1665 inline wxDateSpan
operator+(const wxDateSpan
& ds
) const
1667 return wxDateSpan(GetYears() + ds
.GetYears(),
1668 GetMonths() + ds
.GetMonths(),
1669 GetWeeks() + ds
.GetWeeks(),
1670 GetDays() + ds
.GetDays());
1673 // return difference of two date spans
1674 inline wxDateSpan
Subtract(const wxDateSpan
& other
) const;
1675 // subtract another wxDateSpan from us
1676 inline wxDateSpan
& Subtract(const wxDateSpan
& other
);
1677 // subtract another wxDateSpan from us
1678 inline wxDateSpan
& operator-=(const wxDateSpan
& other
);
1679 inline wxDateSpan
operator-(const wxDateSpan
& ds
) const
1681 return wxDateSpan(GetYears() - ds
.GetYears(),
1682 GetMonths() - ds
.GetMonths(),
1683 GetWeeks() - ds
.GetWeeks(),
1684 GetDays() - ds
.GetDays());
1687 // return a copy of this time span with changed sign
1688 inline wxDateSpan
Negate() const;
1689 // inverse the sign of this timespan
1690 inline wxDateSpan
& Neg();
1691 // inverse the sign of this timespan
1692 wxDateSpan
& operator-() { return Neg(); }
1694 // return the date span proportional to this one with given factor
1695 inline wxDateSpan
Multiply(int factor
) const;
1696 // multiply all components by a (signed) number
1697 inline wxDateSpan
& Multiply(int factor
);
1698 // multiply all components by a (signed) number
1699 inline wxDateSpan
& operator*=(int factor
) { return Multiply(factor
); }
1700 inline wxDateSpan
operator*(int n
) const
1702 return wxDateSpan(*this).Multiply(n
);
1705 // ds1 == d2 if and only if for every wxDateTime t t + ds1 == t + ds2
1706 inline bool operator==(const wxDateSpan
& ds
) const
1708 return GetYears() == ds
.GetYears() &&
1709 GetMonths() == ds
.GetMonths() &&
1710 GetTotalDays() == ds
.GetTotalDays();
1713 inline bool operator!=(const wxDateSpan
& ds
) const
1715 return !(*this == ds
);
1725 // ----------------------------------------------------------------------------
1726 // wxDateTimeArray: array of dates.
1727 // ----------------------------------------------------------------------------
1729 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxDateTime
, wxDateTimeArray
, WXDLLIMPEXP_BASE
);
1731 // ----------------------------------------------------------------------------
1732 // wxDateTimeHolidayAuthority: an object of this class will decide whether a
1733 // given date is a holiday and is used by all functions working with "work
1736 // NB: the base class is an ABC, derived classes must implement the pure
1737 // virtual methods to work with the holidays they correspond to.
1738 // ----------------------------------------------------------------------------
1740 class WXDLLIMPEXP_FWD_BASE wxDateTimeHolidayAuthority
;
1741 WX_DEFINE_USER_EXPORTED_ARRAY_PTR(wxDateTimeHolidayAuthority
*,
1742 wxHolidayAuthoritiesArray
,
1743 class WXDLLIMPEXP_BASE
);
1745 class wxDateTimeHolidaysModule
;
1746 class WXDLLIMPEXP_BASE wxDateTimeHolidayAuthority
1748 friend class wxDateTimeHolidaysModule
;
1750 // returns true if the given date is a holiday
1751 static bool IsHoliday(const wxDateTime
& dt
);
1753 // fills the provided array with all holidays in the given range, returns
1754 // the number of them
1755 static size_t GetHolidaysInRange(const wxDateTime
& dtStart
,
1756 const wxDateTime
& dtEnd
,
1757 wxDateTimeArray
& holidays
);
1759 // clear the list of holiday authorities
1760 static void ClearAllAuthorities();
1762 // add a new holiday authority (the pointer will be deleted by
1763 // wxDateTimeHolidayAuthority)
1764 static void AddAuthority(wxDateTimeHolidayAuthority
*auth
);
1766 // the base class must have a virtual dtor
1767 virtual ~wxDateTimeHolidayAuthority();
1770 // this function is called to determine whether a given day is a holiday
1771 virtual bool DoIsHoliday(const wxDateTime
& dt
) const = 0;
1773 // this function should fill the array with all holidays between the two
1774 // given dates - it is implemented in the base class, but in a very
1775 // inefficient way (it just iterates over all days and uses IsHoliday() for
1776 // each of them), so it must be overridden in the derived class where the
1777 // base class version may be explicitly used if needed
1779 // returns the number of holidays in the given range and fills holidays
1781 virtual size_t DoGetHolidaysInRange(const wxDateTime
& dtStart
,
1782 const wxDateTime
& dtEnd
,
1783 wxDateTimeArray
& holidays
) const = 0;
1786 // all holiday authorities
1787 static wxHolidayAuthoritiesArray ms_authorities
;
1790 // the holidays for this class are all Saturdays and Sundays
1791 class WXDLLIMPEXP_BASE wxDateTimeWorkDays
: public wxDateTimeHolidayAuthority
1794 virtual bool DoIsHoliday(const wxDateTime
& dt
) const;
1795 virtual size_t DoGetHolidaysInRange(const wxDateTime
& dtStart
,
1796 const wxDateTime
& dtEnd
,
1797 wxDateTimeArray
& holidays
) const;
1800 // ============================================================================
1801 // inline functions implementation
1802 // ============================================================================
1804 // ----------------------------------------------------------------------------
1806 // ----------------------------------------------------------------------------
1808 #define MILLISECONDS_PER_DAY 86400000l
1810 // some broken compilers (HP-UX CC) refuse to compile the "normal" version, but
1811 // using a temp variable always might prevent other compilers from optimising
1812 // it away - hence use of this ugly macro
1814 #define MODIFY_AND_RETURN(op) return wxDateTime(*this).op
1816 #define MODIFY_AND_RETURN(op) wxDateTime dt(*this); dt.op; return dt
1819 // ----------------------------------------------------------------------------
1820 // wxDateTime construction
1821 // ----------------------------------------------------------------------------
1823 inline bool wxDateTime::IsInStdRange() const
1825 return m_time
>= 0l && (m_time
/ TIME_T_FACTOR
) < LONG_MAX
;
1829 inline wxDateTime
wxDateTime::Now()
1832 return wxDateTime(*GetTmNow(&tmstruct
));
1836 inline wxDateTime
wxDateTime::Today()
1838 wxDateTime
dt(Now());
1844 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
1845 inline wxDateTime
& wxDateTime::Set(time_t timet
)
1847 // assign first to avoid long multiplication overflow!
1848 m_time
= timet
- WX_TIME_BASE_OFFSET
;
1849 m_time
*= TIME_T_FACTOR
;
1855 inline wxDateTime
& wxDateTime::SetToCurrent()
1861 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
1862 inline wxDateTime::wxDateTime(time_t timet
)
1868 inline wxDateTime::wxDateTime(const struct tm
& tm
)
1873 inline wxDateTime::wxDateTime(const Tm
& tm
)
1878 inline wxDateTime::wxDateTime(double jdn
)
1883 inline wxDateTime
& wxDateTime::Set(const Tm
& tm
)
1885 wxASSERT_MSG( tm
.IsValid(), _T("invalid broken down date/time") );
1887 return Set(tm
.mday
, (Month
)tm
.mon
, tm
.year
,
1888 tm
.hour
, tm
.min
, tm
.sec
, tm
.msec
);
1891 inline wxDateTime::wxDateTime(wxDateTime_t hour
,
1892 wxDateTime_t minute
,
1893 wxDateTime_t second
,
1894 wxDateTime_t millisec
)
1896 Set(hour
, minute
, second
, millisec
);
1899 inline wxDateTime::wxDateTime(wxDateTime_t day
,
1903 wxDateTime_t minute
,
1904 wxDateTime_t second
,
1905 wxDateTime_t millisec
)
1907 Set(day
, month
, year
, hour
, minute
, second
, millisec
);
1910 // ----------------------------------------------------------------------------
1911 // wxDateTime accessors
1912 // ----------------------------------------------------------------------------
1914 inline wxLongLong
wxDateTime::GetValue() const
1916 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1921 inline time_t wxDateTime::GetTicks() const
1923 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1924 if ( !IsInStdRange() )
1929 return (time_t)((m_time
/ (long)TIME_T_FACTOR
).ToLong()) + WX_TIME_BASE_OFFSET
;
1932 inline bool wxDateTime::SetToLastWeekDay(WeekDay weekday
,
1936 return SetToWeekDay(weekday
, -1, month
, year
);
1940 wxDateTime::GetWeekDayInSameWeek(WeekDay weekday
,
1941 WeekFlags
WXUNUSED(flags
)) const
1943 MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday
) );
1946 inline wxDateTime
wxDateTime::GetNextWeekDay(WeekDay weekday
) const
1948 MODIFY_AND_RETURN( SetToNextWeekDay(weekday
) );
1951 inline wxDateTime
wxDateTime::GetPrevWeekDay(WeekDay weekday
) const
1953 MODIFY_AND_RETURN( SetToPrevWeekDay(weekday
) );
1956 inline wxDateTime
wxDateTime::GetWeekDay(WeekDay weekday
,
1961 wxDateTime
dt(*this);
1963 return dt
.SetToWeekDay(weekday
, n
, month
, year
) ? dt
: wxInvalidDateTime
;
1966 inline wxDateTime
wxDateTime::GetLastWeekDay(WeekDay weekday
,
1970 wxDateTime
dt(*this);
1972 return dt
.SetToLastWeekDay(weekday
, month
, year
) ? dt
: wxInvalidDateTime
;
1975 inline wxDateTime
wxDateTime::GetLastMonthDay(Month month
, int year
) const
1977 MODIFY_AND_RETURN( SetToLastMonthDay(month
, year
) );
1980 inline wxDateTime
wxDateTime::GetYearDay(wxDateTime_t yday
) const
1982 MODIFY_AND_RETURN( SetToYearDay(yday
) );
1985 // ----------------------------------------------------------------------------
1986 // wxDateTime comparison
1987 // ----------------------------------------------------------------------------
1989 inline bool wxDateTime::IsEqualTo(const wxDateTime
& datetime
) const
1991 wxASSERT_MSG( IsValid() && datetime
.IsValid(), _T("invalid wxDateTime"));
1993 return m_time
== datetime
.m_time
;
1996 inline bool wxDateTime::IsEarlierThan(const wxDateTime
& datetime
) const
1998 wxASSERT_MSG( IsValid() && datetime
.IsValid(), _T("invalid wxDateTime"));
2000 return m_time
< datetime
.m_time
;
2003 inline bool wxDateTime::IsLaterThan(const wxDateTime
& datetime
) const
2005 wxASSERT_MSG( IsValid() && datetime
.IsValid(), _T("invalid wxDateTime"));
2007 return m_time
> datetime
.m_time
;
2010 inline bool wxDateTime::IsStrictlyBetween(const wxDateTime
& t1
,
2011 const wxDateTime
& t2
) const
2013 // no need for assert, will be checked by the functions we call
2014 return IsLaterThan(t1
) && IsEarlierThan(t2
);
2017 inline bool wxDateTime::IsBetween(const wxDateTime
& t1
,
2018 const wxDateTime
& t2
) const
2020 // no need for assert, will be checked by the functions we call
2021 return IsEqualTo(t1
) || IsEqualTo(t2
) || IsStrictlyBetween(t1
, t2
);
2024 inline bool wxDateTime::IsSameDate(const wxDateTime
& dt
) const
2029 return tm1
.year
== tm2
.year
&&
2030 tm1
.mon
== tm2
.mon
&&
2031 tm1
.mday
== tm2
.mday
;
2034 inline bool wxDateTime::IsSameTime(const wxDateTime
& dt
) const
2036 // notice that we can't do something like this:
2038 // m_time % MILLISECONDS_PER_DAY == dt.m_time % MILLISECONDS_PER_DAY
2040 // because we have also to deal with (possibly) different DST settings!
2044 return tm1
.hour
== tm2
.hour
&&
2045 tm1
.min
== tm2
.min
&&
2046 tm1
.sec
== tm2
.sec
&&
2047 tm1
.msec
== tm2
.msec
;
2050 inline bool wxDateTime::IsEqualUpTo(const wxDateTime
& dt
,
2051 const wxTimeSpan
& ts
) const
2053 return IsBetween(dt
.Subtract(ts
), dt
.Add(ts
));
2056 // ----------------------------------------------------------------------------
2057 // wxDateTime arithmetics
2058 // ----------------------------------------------------------------------------
2060 inline wxDateTime
wxDateTime::Add(const wxTimeSpan
& diff
) const
2062 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
2064 return wxDateTime(m_time
+ diff
.GetValue());
2067 inline wxDateTime
& wxDateTime::Add(const wxTimeSpan
& diff
)
2069 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
2071 m_time
+= diff
.GetValue();
2076 inline wxDateTime
& wxDateTime::operator+=(const wxTimeSpan
& diff
)
2081 inline wxDateTime
wxDateTime::Subtract(const wxTimeSpan
& diff
) const
2083 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
2085 return wxDateTime(m_time
- diff
.GetValue());
2088 inline wxDateTime
& wxDateTime::Subtract(const wxTimeSpan
& diff
)
2090 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
2092 m_time
-= diff
.GetValue();
2097 inline wxDateTime
& wxDateTime::operator-=(const wxTimeSpan
& diff
)
2099 return Subtract(diff
);
2102 inline wxTimeSpan
wxDateTime::Subtract(const wxDateTime
& datetime
) const
2104 wxASSERT_MSG( IsValid() && datetime
.IsValid(), _T("invalid wxDateTime"));
2106 return wxTimeSpan(GetValue() - datetime
.GetValue());
2109 inline wxTimeSpan
wxDateTime::operator-(const wxDateTime
& dt2
) const
2111 return this->Subtract(dt2
);
2114 inline wxDateTime
wxDateTime::Add(const wxDateSpan
& diff
) const
2116 return wxDateTime(*this).Add(diff
);
2119 inline wxDateTime
& wxDateTime::Subtract(const wxDateSpan
& diff
)
2121 return Add(diff
.Negate());
2124 inline wxDateTime
wxDateTime::Subtract(const wxDateSpan
& diff
) const
2126 return wxDateTime(*this).Subtract(diff
);
2129 inline wxDateTime
& wxDateTime::operator-=(const wxDateSpan
& diff
)
2131 return Subtract(diff
);
2134 inline wxDateTime
& wxDateTime::operator+=(const wxDateSpan
& diff
)
2139 // ----------------------------------------------------------------------------
2140 // wxDateTime and timezones
2141 // ----------------------------------------------------------------------------
2144 wxDateTime::ToTimezone(const wxDateTime::TimeZone
& tz
, bool noDST
) const
2146 MODIFY_AND_RETURN( MakeTimezone(tz
, noDST
) );
2150 wxDateTime::FromTimezone(const wxDateTime::TimeZone
& tz
, bool noDST
) const
2152 MODIFY_AND_RETURN( MakeFromTimezone(tz
, noDST
) );
2155 // ----------------------------------------------------------------------------
2156 // wxTimeSpan construction
2157 // ----------------------------------------------------------------------------
2159 inline wxTimeSpan::wxTimeSpan(long hours
,
2162 wxLongLong milliseconds
)
2164 // assign first to avoid precision loss
2171 m_diff
+= milliseconds
;
2174 // ----------------------------------------------------------------------------
2175 // wxTimeSpan accessors
2176 // ----------------------------------------------------------------------------
2178 inline wxLongLong
wxTimeSpan::GetSeconds() const
2180 return m_diff
/ 1000l;
2183 inline int wxTimeSpan::GetMinutes() const
2185 // explicit cast to int suppresses a warning with CodeWarrior and possibly
2186 // others (changing the return type to long from int is impossible in 2.8)
2187 return (int)((GetSeconds() / 60l).GetLo());
2190 inline int wxTimeSpan::GetHours() const
2192 return GetMinutes() / 60;
2195 inline int wxTimeSpan::GetDays() const
2197 return GetHours() / 24;
2200 inline int wxTimeSpan::GetWeeks() const
2202 return GetDays() / 7;
2205 // ----------------------------------------------------------------------------
2206 // wxTimeSpan arithmetics
2207 // ----------------------------------------------------------------------------
2209 inline wxTimeSpan
wxTimeSpan::Add(const wxTimeSpan
& diff
) const
2211 return wxTimeSpan(m_diff
+ diff
.GetValue());
2214 inline wxTimeSpan
& wxTimeSpan::Add(const wxTimeSpan
& diff
)
2216 m_diff
+= diff
.GetValue();
2221 inline wxTimeSpan
wxTimeSpan::Subtract(const wxTimeSpan
& diff
) const
2223 return wxTimeSpan(m_diff
- diff
.GetValue());
2226 inline wxTimeSpan
& wxTimeSpan::Subtract(const wxTimeSpan
& diff
)
2228 m_diff
-= diff
.GetValue();
2233 inline wxTimeSpan
& wxTimeSpan::Multiply(int n
)
2240 inline wxTimeSpan
wxTimeSpan::Multiply(int n
) const
2242 return wxTimeSpan(m_diff
* (long)n
);
2245 inline wxTimeSpan
wxTimeSpan::Abs() const
2247 return wxTimeSpan(GetValue().Abs());
2250 inline bool wxTimeSpan::IsEqualTo(const wxTimeSpan
& ts
) const
2252 return GetValue() == ts
.GetValue();
2255 inline bool wxTimeSpan::IsLongerThan(const wxTimeSpan
& ts
) const
2257 return GetValue().Abs() > ts
.GetValue().Abs();
2260 inline bool wxTimeSpan::IsShorterThan(const wxTimeSpan
& ts
) const
2262 return GetValue().Abs() < ts
.GetValue().Abs();
2265 // ----------------------------------------------------------------------------
2267 // ----------------------------------------------------------------------------
2269 inline wxDateSpan
& wxDateSpan::operator+=(const wxDateSpan
& other
)
2271 m_years
+= other
.m_years
;
2272 m_months
+= other
.m_months
;
2273 m_weeks
+= other
.m_weeks
;
2274 m_days
+= other
.m_days
;
2279 inline wxDateSpan
& wxDateSpan::Add(const wxDateSpan
& other
)
2281 return *this += other
;
2284 inline wxDateSpan
wxDateSpan::Add(const wxDateSpan
& other
) const
2286 wxDateSpan
ds(*this);
2291 inline wxDateSpan
& wxDateSpan::Multiply(int factor
)
2301 inline wxDateSpan
wxDateSpan::Multiply(int factor
) const
2303 wxDateSpan
ds(*this);
2304 ds
.Multiply(factor
);
2308 inline wxDateSpan
wxDateSpan::Negate() const
2310 return wxDateSpan(-m_years
, -m_months
, -m_weeks
, -m_days
);
2313 inline wxDateSpan
& wxDateSpan::Neg()
2316 m_months
= -m_months
;
2323 inline wxDateSpan
& wxDateSpan::operator-=(const wxDateSpan
& other
)
2325 return *this += other
.Negate();
2328 inline wxDateSpan
& wxDateSpan::Subtract(const wxDateSpan
& other
)
2330 return *this -= other
;
2333 inline wxDateSpan
wxDateSpan::Subtract(const wxDateSpan
& other
) const
2335 wxDateSpan
ds(*this);
2340 #undef MILLISECONDS_PER_DAY
2342 #undef MODIFY_AND_RETURN
2344 // ============================================================================
2346 // ============================================================================
2348 // ----------------------------------------------------------------------------
2349 // wxTimeSpan operators
2350 // ----------------------------------------------------------------------------
2352 wxTimeSpan WXDLLIMPEXP_BASE
operator*(int n
, const wxTimeSpan
& ts
);
2354 // ----------------------------------------------------------------------------
2356 // ----------------------------------------------------------------------------
2358 wxDateSpan WXDLLIMPEXP_BASE
operator*(int n
, const wxDateSpan
& ds
);
2360 // ============================================================================
2361 // other helper functions
2362 // ============================================================================
2364 // ----------------------------------------------------------------------------
2365 // iteration helpers: can be used to write a for loop over enum variable like
2367 // for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) )
2368 // ----------------------------------------------------------------------------
2370 WXDLLIMPEXP_BASE
void wxNextMonth(wxDateTime::Month
& m
);
2371 WXDLLIMPEXP_BASE
void wxPrevMonth(wxDateTime::Month
& m
);
2372 WXDLLIMPEXP_BASE
void wxNextWDay(wxDateTime::WeekDay
& wd
);
2373 WXDLLIMPEXP_BASE
void wxPrevWDay(wxDateTime::WeekDay
& wd
);
2375 #endif // wxUSE_DATETIME
2377 #endif // _WX_DATETIME_H