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 license
11 /////////////////////////////////////////////////////////////////////////////
13 #ifndef _WX_DATETIME_H
14 #define _WX_DATETIME_H
17 #pragma interface "datetime.h"
21 #include <limits.h> // for INT_MIN
23 #include "wx/longlong.h"
25 class WXDLLEXPORT wxDateTime
;
26 class WXDLLEXPORT wxTimeSpan
;
27 class WXDLLEXPORT wxDateSpan
;
29 // don't use inline functions in debug builds - we don't care about
30 // performances and this only leads to increased rebuild time (because every
31 // time an inline method is changed, all files including the header must be
38 * TODO Well, everything :-)
40 * + 1. Time zones with minutes (make TimeZone a class)
41 * 2. getdate() function like under Solaris
42 * + 3. text conversion for wxDateSpan
43 * 4. pluggable modules for the workdays calculations
47 The three (main) classes declared in this header represent:
49 1. An absolute moment in the time (wxDateTime)
50 2. A difference between two moments in the time, positive or negative
52 3. A logical difference between two dates expressed in
53 years/months/weeks/days (wxDateSpan)
55 The following arithmetic operations are permitted (all others are not):
60 wxDateTime + wxTimeSpan = wxDateTime
61 wxDateTime + wxDateSpan = wxDateTime
62 wxTimeSpan + wxTimeSpan = wxTimeSpan
63 wxDateSpan + wxDateSpan = wxDateSpan
67 wxDateTime - wxDateTime = wxTimeSpan
68 wxTimeSpan - wxTimeSpan = wxTimeSpan
69 wxDateSpan - wxDateSpan = wxDateSpan
73 wxTimeSpan * number = wxTimeSpan
74 wxDateSpan * number = wxDateSpan
78 -wxTimeSpan = wxTimeSpan
79 -wxDateSpan = wxDateSpan
82 // ----------------------------------------------------------------------------
83 // This class represents an absolute moment in the time
84 // ----------------------------------------------------------------------------
86 class WXDLLEXPORT wxDateTime
90 // ------------------------------------------------------------------------
92 // a small unsigned integer type for storing things like seconds, days
93 // of the week, &c. It should be at least short (i.e. not char) to
94 // contain the number of milliseconds - it may also be 'int' because
95 // there is no size penalty associated with it in our code, we don't
96 // store any data in this format
97 typedef unsigned short wxDateTime_t
;
102 // the time in the current time zone
105 // zones from GMT (= Greenwhich Mean Time): they're guaranteed to be
106 // consequent numbers, so writing something like `GMT0 + offset' is
107 // safe if abs(offset) <= 12
109 // underscore stands for minus
110 GMT_12
, GMT_11
, GMT_10
, GMT_9
, GMT_8
, GMT_7
,
111 GMT_6
, GMT_5
, GMT_4
, GMT_3
, GMT_2
, GMT_1
,
113 GMT1
, GMT2
, GMT3
, GMT4
, GMT5
, GMT6
,
114 GMT7
, GMT8
, GMT9
, GMT10
, GMT11
, GMT12
,
115 // Note that GMT12 and GMT_12 are not the same: there is a difference
116 // of exactly one day between them
118 // some symbolic names for TZ
121 WET
= GMT0
, // Western Europe Time
122 WEST
= GMT1
, // Western Europe Summer Time
123 CET
= GMT1
, // Central Europe Time
124 CEST
= GMT2
, // Central Europe Summer Time
125 EET
= GMT2
, // Eastern Europe Time
126 EEST
= GMT3
, // Eastern Europe Summer Time
127 MSK
= GMT3
, // Moscow Time
128 MSD
= GMT4
, // Moscow Summer Time
131 AST
= GMT_4
, // Atlantic Standard Time
132 ADT
= GMT_3
, // Atlantic Daylight Time
133 EST
= GMT_5
, // Eastern Standard Time
134 EDT
= GMT_4
, // Eastern Daylight Saving Time
135 CST
= GMT_6
, // Central Standard Time
136 CDT
= GMT_5
, // Central Daylight Saving Time
137 MST
= GMT_7
, // Mountain Standard Time
138 MDT
= GMT_6
, // Mountain Daylight Saving Time
139 PST
= GMT_8
, // Pacific Standard Time
140 PDT
= GMT_7
, // Pacific Daylight Saving Time
141 HST
= GMT_10
, // Hawaiian Standard Time
142 AKST
= GMT_9
, // Alaska Standard Time
143 AKDT
= GMT_8
, // Alaska Daylight Saving Time
147 A_WST
= GMT8
, // Western Standard Time
148 A_CST
= GMT12
+ 1, // Central Standard Time (+9.5)
149 A_EST
= GMT10
, // Eastern Standard Time
150 A_ESST
= GMT11
, // Eastern Summer Time
152 // TODO add more symbolic timezone names here
154 // Universal Coordinated Time = the new and politically correct name
159 // the calendar systems we know about: notice that it's valid (for
160 // this classes purpose anyhow) to work with any of these calendars
161 // even with the dates before the historical appearance of the
165 Gregorian
, // current calendar
166 Julian
// calendar in use since -45 until the 1582 (or later)
168 // TODO Hebrew, Chinese, Maya, ... (just kidding) (or then may be not?)
171 // these values only are used to identify the different dates of
172 // adoption of the Gregorian calendar (see IsGregorian())
174 // All data and comments taken verbatim from "The Calendar FAQ (v 2.0)"
175 // by Claus Tøndering, http://www.pip.dknet.dk/~c-t/calendar.html
176 // except for the comments "we take".
178 // Symbol "->" should be read as "was followed by" in the comments
180 enum GregorianAdoption
182 Gr_Unknown
, // no data for this country or it's too uncertain to use
183 Gr_Standard
, // on the day 0 of Gregorian calendar: 15 Oct 1582
185 Gr_Alaska
, // Oct 1867 when Alaska became part of the USA
186 Gr_Albania
, // Dec 1912
188 Gr_Austria
= Gr_Unknown
, // Different regions on different dates
189 Gr_Austria_Brixen
, // 5 Oct 1583 -> 16 Oct 1583
190 Gr_Austria_Salzburg
= Gr_Austria_Brixen
,
191 Gr_Austria_Tyrol
= Gr_Austria_Brixen
,
192 Gr_Austria_Carinthia
, // 14 Dec 1583 -> 25 Dec 1583
193 Gr_Austria_Styria
= Gr_Austria_Carinthia
,
195 Gr_Belgium
, // Then part of the Netherlands
197 Gr_Bulgaria
= Gr_Unknown
, // Unknown precisely (from 1915 to 1920)
198 Gr_Bulgaria_1
, // 18 Mar 1916 -> 1 Apr 1916
199 Gr_Bulgaria_2
, // 31 Mar 1916 -> 14 Apr 1916
200 Gr_Bulgaria_3
, // 3 Sep 1920 -> 17 Sep 1920
202 Gr_Canada
= Gr_Unknown
, // Different regions followed the changes in
203 // Great Britain or France
205 Gr_China
= Gr_Unknown
, // Different authorities say:
206 Gr_China_1
, // 18 Dec 1911 -> 1 Jan 1912
207 Gr_China_2
, // 18 Dec 1928 -> 1 Jan 1929
209 Gr_Czechoslovakia
, // (Bohemia and Moravia) 6 Jan 1584 -> 17 Jan 1584
210 Gr_Denmark
, // (including Norway) 18 Feb 1700 -> 1 Mar 1700
213 Gr_Finland
, // Then part of Sweden
215 Gr_France
, // 9 Dec 1582 -> 20 Dec 1582
216 Gr_France_Alsace
, // 4 Feb 1682 -> 16 Feb 1682
217 Gr_France_Lorraine
, // 16 Feb 1760 -> 28 Feb 1760
218 Gr_France_Strasbourg
, // February 1682
220 Gr_Germany
= Gr_Unknown
, // Different states on different dates:
221 Gr_Germany_Catholic
, // 1583-1585 (we take 1584)
222 Gr_Germany_Prussia
, // 22 Aug 1610 -> 2 Sep 1610
223 Gr_Germany_Protestant
, // 18 Feb 1700 -> 1 Mar 1700
225 Gr_GreatBritain
, // 2 Sep 1752 -> 14 Sep 1752 (use 'cal(1)')
227 Gr_Greece
, // 9 Mar 1924 -> 23 Mar 1924
228 Gr_Hungary
, // 21 Oct 1587 -> 1 Nov 1587
229 Gr_Ireland
= Gr_GreatBritain
,
230 Gr_Italy
= Gr_Standard
,
232 Gr_Japan
= Gr_Unknown
, // Different authorities say:
233 Gr_Japan_1
, // 19 Dec 1872 -> 1 Jan 1873
234 Gr_Japan_2
, // 19 Dec 1892 -> 1 Jan 1893
235 Gr_Japan_3
, // 18 Dec 1918 -> 1 Jan 1919
237 Gr_Latvia
, // 1915-1918 (we take 1915)
238 Gr_Lithuania
, // 1915
239 Gr_Luxemburg
, // 14 Dec 1582 -> 25 Dec 1582
240 Gr_Netherlands
= Gr_Belgium
, // (including Belgium) 1 Jan 1583
242 // this is too weird to take into account: the Gregorian calendar was
243 // introduced twice in Groningen, first time 28 Feb 1583 was followed
244 // by 11 Mar 1583, then it has gone back to Julian in the summer of
245 // 1584 and then 13 Dec 1700 -> 12 Jan 1701 - which is
246 // the date we take here
247 Gr_Netherlands_Groningen
, // 13 Dec 1700 -> 12 Jan 1701
248 Gr_Netherlands_Gelderland
, // 30 Jun 1700 -> 12 Jul 1700
249 Gr_Netherlands_Utrecht
, // (and Overijssel) 30 Nov 1700->12 Dec 1700
250 Gr_Netherlands_Friesland
, // (and Drenthe) 31 Dec 1700 -> 12 Jan 1701
252 Gr_Norway
= Gr_Denmark
, // Then part of Denmark
253 Gr_Poland
= Gr_Standard
,
254 Gr_Portugal
= Gr_Standard
,
255 Gr_Romania
, // 31 Mar 1919 -> 14 Apr 1919
256 Gr_Russia
, // 31 Jan 1918 -> 14 Feb 1918
257 Gr_Scotland
= Gr_GreatBritain
,
258 Gr_Spain
= Gr_Standard
,
260 // Sweden has a curious history. Sweden decided to make a gradual
261 // change from the Julian to the Gregorian calendar. By dropping every
262 // leap year from 1700 through 1740 the eleven superfluous days would
263 // be omitted and from 1 Mar 1740 they would be in sync with the
264 // Gregorian calendar. (But in the meantime they would be in sync with
267 // So 1700 (which should have been a leap year in the Julian calendar)
268 // was not a leap year in Sweden. However, by mistake 1704 and 1708
269 // became leap years. This left Sweden out of synchronisation with
270 // both the Julian and the Gregorian world, so they decided to go back
271 // to the Julian calendar. In order to do this, they inserted an extra
272 // day in 1712, making that year a double leap year! So in 1712,
273 // February had 30 days in Sweden.
275 // Later, in 1753, Sweden changed to the Gregorian calendar by
276 // dropping 11 days like everyone else.
277 Gr_Sweden
= Gr_Finland
, // 17 Feb 1753 -> 1 Mar 1753
279 Gr_Switzerland
= Gr_Unknown
,// Different cantons used different dates
280 Gr_Switzerland_Catholic
, // 1583, 1584 or 1597 (we take 1584)
281 Gr_Switzerland_Protestant
, // 31 Dec 1700 -> 12 Jan 1701
283 Gr_Turkey
, // 1 Jan 1927
284 Gr_USA
= Gr_GreatBritain
,
285 Gr_Wales
= Gr_GreatBritain
,
286 Gr_Yugoslavia
// 1919
289 // the country parameter is used so far for calculating the start and
290 // the end of DST period and for deciding whether the date is a work
293 // TODO move this to intl.h
296 Country_Unknown
, // no special information for this country
297 Country_Default
, // set the default country with SetCountry() method
299 // TODO add more countries (for this we must know about DST and/or
300 // holidays for this country)
305 // symbolic names for the months
308 Jan
, Feb
, Mar
, Apr
, May
, Jun
, Jul
, Aug
, Sep
, Oct
, Nov
, Dec
, Inv_Month
311 // symbolic names for the weekdays
314 Sun
, Mon
, Tue
, Wed
, Thu
, Fri
, Sat
, Inv_WeekDay
317 // invalid value for the year
320 Inv_Year
= SHRT_MIN
// should hold in wxDateTime_t
324 // ------------------------------------------------------------------------
326 // a class representing a time zone: basicly, this is just an offset
327 // (in seconds) from GMT
332 TimeZone(wxDateTime_t offset
= 0) { m_offset
= offset
; }
334 int GetOffset() const { return m_offset
; }
337 // offset for this timezone from GMT in seconds
341 // standard struct tm is limited to the years from 1900 (because
342 // tm_year field is the offset from 1900), so we use our own struct
343 // instead to represent broken down time
345 // NB: this struct should always be kept normalized (i.e. mon should
346 // be < 12, 1 <= day <= 31 &c), so use AddMonths(), AddDays()
347 // instead of modifying the member fields directly!
350 wxDateTime_t msec
, sec
, min
, hour
, mday
;
354 // default ctor inits the object to an invalid value
357 // ctor from struct tm and the timezone
358 Tm(const struct tm
& tm
, const TimeZone
& tz
);
360 // check that the given date/time is valid (in Gregorian calendar)
361 bool IsValid() const;
364 WeekDay
GetWeekDay() // not const because wday may be changed
366 if ( wday
== Inv_WeekDay
)
369 return (WeekDay
)wday
;
372 // add the given number of months to the date keeping it normalized
373 void AddMonths(int monDiff
);
375 // add the given number of months to the date keeping it normalized
376 void AddDays(int dayDiff
);
379 // compute the weekday from other fields
380 void ComputeWeekDay();
382 // the timezone we correspond to
385 // these values can't be accessed directly because they're not always
386 // computed and we calculate them on demand
387 wxDateTime_t wday
, yday
;
391 // ------------------------------------------------------------------------
393 // set the current country
394 static void SetCountry(Country country
);
395 // get the current country
396 static inline Country
GetCountry();
398 // return the current year
399 static int GetCurrentYear(Calendar cal
= Gregorian
);
401 // convert the year as returned by wxDateTime::GetYear() to a year
402 // suitable for BC/AD notation. The difference is that BC year 1
403 // corresponds to the year 0 (while BC year 0 didn't exist) and AD
404 // year N is just year N.
405 static int ConvertYearToBC(int year
);
407 // return the current month
408 static Month
GetCurrentMonth(Calendar cal
= Gregorian
);
410 // returns TRUE if the given year is a leap year in the given calendar
411 static bool IsLeapYear(int year
= Inv_Year
, Calendar cal
= Gregorian
);
413 // get the century (19 for 1999, 20 for 2000 and -5 for 492 BC)
414 static int GetCentury(int year
= Inv_Year
);
416 // returns the number of days in this year (356 or 355 for Gregorian
417 // calendar usually :-)
418 static wxDateTime_t
GetNumberOfDays(int year
, Calendar cal
= Gregorian
);
420 // get the number of the days in the given month (default value for
421 // the year means the current one)
422 static wxDateTime_t
GetNumberOfDays(Month month
,
424 Calendar cal
= Gregorian
);
426 // get the full (default) or abbreviated month name in the current
427 // locale, returns empty string on error
428 static wxString
GetMonthName(Month month
, bool abbr
= FALSE
);
430 // get the full (default) or abbreviated weekday name in the current
431 // locale, returns empty string on error
432 static wxString
GetWeekDayName(WeekDay weekday
, bool abbr
= FALSE
);
434 // get the beginning of DST for this year, will return invalid object
435 // if no DST applicable in this year. The default value of the
436 // parameter means to take the current year.
437 static wxDateTime
GetBeginDST(int year
= Inv_Year
);
438 // get the end of DST for this year, will return invalid object
439 // if no DST applicable in this year. The default value of the
440 // parameter means to take the current year.
441 static wxDateTime
GetEndDST(int year
= Inv_Year
);
443 // return the wxDateTime object for the current time
444 static inline wxDateTime
Now();
446 // constructors: you should test whether the constructor succeeded with
447 // IsValid() function. The values Inv_Month and Inv_Year for the
448 // parameters mean take current month and/or year values.
449 // ------------------------------------------------------------------------
451 // default ctor does not initialize the object, use Set()!
454 // from time_t: seconds since the Epoch 00:00:00 UTC, Jan 1, 1970)
455 inline wxDateTime(time_t timet
);
456 // from broken down time/date (only for standard Unix range)
457 inline wxDateTime(const struct tm
& tm
);
458 // from broken down time/date (any range)
459 inline wxDateTime(const Tm
& tm
);
461 // from JDN (beware of rounding errors)
462 inline wxDateTime(double jdn
);
464 // from separate values for each component, date set to today
465 inline wxDateTime(wxDateTime_t hour
,
466 wxDateTime_t minute
= 0,
467 wxDateTime_t second
= 0,
468 wxDateTime_t millisec
= 0);
469 // from separate values for each component with explicit date
470 inline wxDateTime(wxDateTime_t day
, // day of the month
471 Month month
= Inv_Month
,
472 int year
= Inv_Year
, // 1999, not 99 please!
473 wxDateTime_t hour
= 0,
474 wxDateTime_t minute
= 0,
475 wxDateTime_t second
= 0,
476 wxDateTime_t millisec
= 0);
478 // default copy ctor ok
482 // assignment operators and Set() functions: all non const methods return
483 // the reference to this object. IsValid() should be used to test whether
484 // the function succeeded.
485 // ------------------------------------------------------------------------
487 // set to the current time
488 inline wxDateTime
& SetToCurrent();
490 // set to given time_t value
491 inline wxDateTime
& Set(time_t timet
);
493 // set to given broken down time/date
494 wxDateTime
& Set(const struct tm
& tm
);
496 // set to given broken down time/date
497 inline wxDateTime
& Set(const Tm
& tm
);
499 // set to given JDN (beware of rounding errors)
500 wxDateTime
& Set(double jdn
);
502 // set to given time, date = today
503 wxDateTime
& Set(wxDateTime_t hour
,
504 wxDateTime_t minute
= 0,
505 wxDateTime_t second
= 0,
506 wxDateTime_t millisec
= 0);
508 // from separate values for each component with explicit date
509 // (defaults for month and year are the current values)
510 wxDateTime
& Set(wxDateTime_t day
,
511 Month month
= Inv_Month
,
512 int year
= Inv_Year
, // 1999, not 99 please!
513 wxDateTime_t hour
= 0,
514 wxDateTime_t minute
= 0,
515 wxDateTime_t second
= 0,
516 wxDateTime_t millisec
= 0);
518 // resets time to 00:00:00, doesn't change the date
519 wxDateTime
& ResetTime();
521 // the following functions don't change the values of the other
522 // fields, i.e. SetMinute() won't change either hour or seconds value
525 wxDateTime
& SetYear(int year
);
527 wxDateTime
& SetMonth(Month month
);
528 // set the day of the month
529 wxDateTime
& SetDay(wxDateTime_t day
);
531 wxDateTime
& SetHour(wxDateTime_t hour
);
533 wxDateTime
& SetMinute(wxDateTime_t minute
);
535 wxDateTime
& SetSecond(wxDateTime_t second
);
537 wxDateTime
& SetMillisecond(wxDateTime_t millisecond
);
539 // assignment operator from time_t
540 wxDateTime
& operator=(time_t timet
) { return Set(timet
); }
542 // assignment operator from broken down time/date
543 wxDateTime
& operator=(const struct tm
& tm
) { return Set(tm
); }
545 // assignment operator from broken down time/date
546 wxDateTime
& operator=(const Tm
& tm
) { return Set(tm
); }
548 // default assignment operator is ok
550 // calendar calculations (functions which set the date only leave the time
551 // unchanged, e.g. don't explictly zero it)
552 // ------------------------------------------------------------------------
554 // set to Nth occurence of given weekday in the given month of the
555 // given year (time is set to 0), return TRUE on success and FALSE on
556 // failure. n may be positive (1..5) or negative to count from the end
557 // of the month (see helper function SetToLastWeekDay())
558 bool SetToWeekDay(WeekDay weekday
,
560 Month month
= Inv_Month
,
561 int year
= Inv_Year
);
563 // sets to the last weekday in the given month, year
564 inline bool SetToLastWeekDay(WeekDay weekday
,
565 Month month
= Inv_Month
,
566 int year
= Inv_Year
);
568 // sets the date to the given day of the given week in the year,
569 // returns TRUE on success and FALSE if given date doesn't exist (e.g.
571 bool SetToTheWeek(wxDateTime_t numWeek
, WeekDay weekday
= Mon
);
573 // sets the date to the last day of the given (or current) month or the
574 // given (or current) year
575 wxDateTime
& SetToLastMonthDay(Month month
= Inv_Month
,
576 int year
= Inv_Year
);
578 // The definitions below were taken verbatim from
580 // http://www.capecod.net/~pbaum/date/date0.htm
582 // (Peter Baum's home page)
584 // definition: The Julian Day Number, Julian Day, or JD of a
585 // particular instant of time is the number of days and fractions of a
586 // day since 12 hours Universal Time (Greenwich mean noon) on January
587 // 1 of the year -4712, where the year is given in the Julian
588 // proleptic calendar. The idea of using this reference date was
589 // originally proposed by Joseph Scalizer in 1582 to count years but
590 // it was modified by 19th century astronomers to count days. One
591 // could have equivalently defined the reference time to be noon of
592 // November 24, -4713 if were understood that Gregorian calendar rules
593 // were applied. Julian days are Julian Day Numbers and are not to be
594 // confused with Julian dates.
596 // definition: The Rata Die number is a date specified as the number
597 // of days relative to a base date of December 31 of the year 0. Thus
598 // January 1 of the year 1 is Rata Die day 1.
600 // get the Julian Day number (the fractional part specifies the time of
601 // the day, related to noon - beware of rounding errors!)
602 double GetJulianDayNumber() const;
604 // get the Rata Die number
605 double GetRataDie() const;
607 // TODO algorithms for calculating some important dates, such as
608 // religious holidays (Easter...) or moon/solar eclipses? Some
609 // algorithms can be found in the calendar FAQ
611 // timezone stuff: a wxDateTime object constructed using given
612 // day/month/year/hour/min/sec values correspond to this moment in local
613 // time. Using the functions below, it may be converted to another time
614 // zone (for example, the Unix epoch is wxDateTime(1, Jan, 1970).ToGMT())
616 // Converting to the local time zone doesn't do anything.
617 // ------------------------------------------------------------------------
619 // transform to any given timezone
620 inline wxDateTime
ToTimezone(const TimeZone
& tz
) const;
621 wxDateTime
& MakeTimezone(const TimeZone
& tz
);
623 // transform to GMT/UTC
624 wxDateTime
ToGMT() const { return ToTimezone(GMT0
); }
625 wxDateTime
& MakeGMT() { return MakeTimezone(GMT0
); }
627 // accessors: many of them take the timezone parameter which indicates the
628 // timezone for which to make the calculations and the default value means
629 // to do it for the current timezone of this machine (even if the function
630 // only operates with the date it's necessary because a date may wrap as
631 // result of timezone shift)
632 // ------------------------------------------------------------------------
634 // is the date valid (FALSE for uninitialized objects as well as after
635 // the functions which failed to convert the date to supported range)
636 inline bool IsValid() const { return this != &ms_InvDateTime
; }
638 // get the broken down date/time representation in the given timezone
640 // If you wish to get several time components (day, month and year),
641 // consider getting the whole Tm strcuture first and retrieving the
642 // value from it - this is much more efficient
643 Tm
GetTm(const TimeZone
& tz
= Local
) const;
645 // get the number of seconds since the Unix epoch - returns (time_t)-1
646 // if the value is out of range
647 inline time_t GetTicks() const;
649 // get the year (returns Inv_Year if date is invalid)
650 int GetYear(const TimeZone
& tz
= Local
) const
651 { return GetTm(tz
).year
; }
652 // get the month (Inv_Month if date is invalid)
653 Month
GetMonth(const TimeZone
& tz
= Local
) const
654 { return (Month
)GetTm(tz
).mon
; }
655 // get the month day (in 1..31 range, 0 if date is invalid)
656 wxDateTime_t
GetDay(const TimeZone
& tz
= Local
) const
657 { return GetTm(tz
).mday
; }
658 // get the day of the week (Inv_WeekDay if date is invalid)
659 WeekDay
GetWeekDay(const TimeZone
& tz
= Local
) const
660 { return GetTm(tz
).GetWeekDay(); }
661 // get the hour of the day
662 wxDateTime_t
GetHour(const TimeZone
& tz
= Local
) const
663 { return GetTm(tz
).hour
; }
665 wxDateTime_t
GetMinute(const TimeZone
& tz
= Local
) const
666 { return GetTm(tz
).min
; }
668 wxDateTime_t
GetSecond(const TimeZone
& tz
= Local
) const
669 { return GetTm(tz
).sec
; }
671 wxDateTime_t
GetMillisecond(const TimeZone
& tz
= Local
) const
672 { return GetTm(tz
).msec
; }
674 // get the day since the year start (1..366, 0 if date is invalid)
675 wxDateTime_t
GetDayOfYear(const TimeZone
& tz
= Local
) const;
676 // get the week number since the year start (1..52, 0 if date is
678 wxDateTime_t
GetWeekOfYear(const TimeZone
& tz
= Local
) const;
680 // is this date a work day? This depends on a country, of course,
681 // because the holidays are different in different countries
682 bool IsWorkDay(Country country
= Country_Default
,
683 const TimeZone
& tz
= Local
) const;
685 // is this date later than Gregorian calendar introduction for the
686 // given country (see enum GregorianAdoption)?
688 // NB: this function shouldn't be considered as absolute authoiruty in
689 // the matter. Besides, for some countries the exact date of
690 // adoption of the Gregorian calendar is simply unknown.
691 bool IsGregorianDate(GregorianAdoption country
= Gr_Standard
) const;
693 // is daylight savings time in effect at this moment according to the
694 // rules of the specified country?
696 // Return value is > 0 if DST is in effect, 0 if it is not and -1 if
697 // the information is not available (this is compatible with ANSI C)
698 int IsDST(Country country
= Country_Default
) const;
700 // comparison (see also functions below for operator versions)
701 // ------------------------------------------------------------------------
703 // returns TRUE if the two moments are strictly identical
704 inline bool IsEqualTo(const wxDateTime
& datetime
) const;
706 // returns TRUE if the two moments are identical
707 inline bool operator==(const wxDateTime
& datetime
) const;
709 // returns TRUE if the two moments are different
710 inline bool operator!=(const wxDateTime
& datetime
) const;
712 // returns TRUE if the date is strictly earlier than the given one
713 inline bool IsEarlierThan(const wxDateTime
& datetime
) const;
715 // returns TRUE if the date is strictly later than the given one
716 inline bool IsLaterThan(const wxDateTime
& datetime
) const;
718 // returns TRUE if the date is strictly in the given range
719 inline bool IsStrictlyBetween(const wxDateTime
& t1
,
720 const wxDateTime
& t2
) const;
722 // returns TRUE if the date is in the given range
723 inline bool IsBetween(const wxDateTime
& t1
, const wxDateTime
& t2
) const;
725 // date operations: for the non-const methods, the return value is this
726 // object itself (see also functions below for operator versions)
727 // ------------------------------------------------------------------------
729 // add a time span (positive or negative)
730 inline wxDateTime
& Add(const wxTimeSpan
& diff
);
731 // add a time span (positive or negative)
732 inline wxDateTime
& operator+=(const wxTimeSpan
& diff
);
734 // substract a time span (positive or negative)
735 inline wxDateTime
& Substract(const wxTimeSpan
& diff
);
736 // substract a time span (positive or negative)
737 inline wxDateTime
& operator-=(const wxTimeSpan
& diff
);
739 // add a date span (positive or negative)
740 wxDateTime
& Add(const wxDateSpan
& diff
);
741 // add a date span (positive or negative)
742 inline wxDateTime
& operator+=(const wxDateSpan
& diff
);
744 // substract a date span (positive or negative)
745 inline wxDateTime
& Substract(const wxDateSpan
& diff
);
746 // substract a date span (positive or negative)
747 inline wxDateTime
& operator-=(const wxDateSpan
& diff
);
749 // substract a date (may result in positive or negative time span)
750 inline wxTimeSpan
Substract(const wxDateTime
& datetime
) const;
751 // substract a date (may result in positive or negative time span)
752 inline wxTimeSpan
operator-(const wxDateTime
& datetime
) const;
754 // conversion to/from text: all conversions from text return TRUE on
755 // success or FALSE if the date is malformed/out of supported range
756 // ------------------------------------------------------------------------
758 // parse a string in RFC 822 format (found e.g. in mail headers and
759 // having the form "Wed, 10 Feb 1999 19:07:07 +0100")
760 bool ParseRfc822Date(const wxString
& date
);
761 // parse a date/time in the given format (see strptime(3))
762 bool ParseFormat(const wxString
& date
, const char *format
= "%c");
763 // parse a string containing the date/time in "free" format, this
764 // function will try to make an educated guess at the string contents
765 // (and return FALSE if it fails)
766 bool ParseDateTime(const wxString
& date
);
768 // this function accepts strftime()-like format string (default
769 // argument corresponds to the preferred date and time representation
770 // for the current locale) and returns the string containing the
771 // resulting text representation
772 wxString
Format(const wxChar
*format
= _T("%c"),
773 const TimeZone
& tz
= Local
) const;
774 // preferred date representation for the current locale
775 wxString
FormatDate() const { return Format(_T("%x")); }
776 // preferred time representation for the current locale
777 wxString
FormatTime() const { return Format(_T("%X")); }
780 // ------------------------------------------------------------------------
782 // get the internal representation
783 inline wxLongLong
GetValue() const;
785 // a helper function to get the current time_t
786 static time_t GetTimeNow() { return time((time_t *)NULL
); }
788 // another one to get the current time broken down
789 static struct tm
*GetTmNow()
791 time_t t
= GetTimeNow();
792 return localtime(&t
);
796 // the current country - as it's the same for all program objects (unless
797 // it runs on a _really_ big cluster system :-), this is a static member:
798 // see SetCountry() and GetCountry()
799 static Country ms_country
;
801 // this constant is used to transform a time_t value to the internal
802 // representation, as time_t is in seconds and we use milliseconds it's
804 static const unsigned int TIME_T_FACTOR
;
806 // invalid wxDateTime object - returned by all functions which return
807 // "wxDateTime &" on failure
808 static wxDateTime ms_InvDateTime
;
810 // returns TRUE if we fall in range in which we can use standard ANSI C
812 inline bool IsInStdRange() const;
814 // the internal representation of the time is the amount of milliseconds
815 // elapsed since the origin which is set by convention to the UNIX/C epoch
816 // value: the midnight of January 1, 1970 (UTC)
820 // ----------------------------------------------------------------------------
821 // This class contains a difference between 2 wxDateTime values, so it makes
822 // sense to add it to wxDateTime and it is the result of substraction of 2
823 // objects of that class. See also wxDateSpan.
824 // ----------------------------------------------------------------------------
826 class WXDLLEXPORT wxTimeSpan
830 // ------------------------------------------------------------------------
832 // return the timespan for the given number of seconds
833 static wxTimeSpan
Seconds(int sec
) { return wxTimeSpan(0, 0, sec
); }
835 // return the timespan for the given number of minutes
836 static wxTimeSpan
Minutes(int min
) { return wxTimeSpan(0, min
, 0 ); }
838 // return the timespan for the given number of hours
839 static wxTimeSpan
Hours(int hours
) { return wxTimeSpan(hours
, 0, 0); }
841 // default ctor constructs the 0 time span
844 // from separate values for each component, date set to 0 (hours are
845 // not restricted to 0..24 range, neither are minutes, seconds or
847 inline wxTimeSpan(int hours
,
850 int milliseconds
= 0);
852 // from internal representation
853 wxTimeSpan(wxLongLong diff
) : m_diff(diff
) { }
855 // default copy ctor is ok
859 // arithmetics with time spans
860 // ------------------------------------------------------------------------
862 // add two timespans together
863 inline wxTimeSpan
& Add(const wxTimeSpan
& diff
);
864 // add two timespans together
865 wxTimeSpan
& operator+=(const wxTimeSpan
& diff
) { return Add(diff
); }
867 // substract another timespan
868 inline wxTimeSpan
& Substract(const wxTimeSpan
& diff
);
869 // substract another timespan
870 wxTimeSpan
& operator-=(const wxTimeSpan
& diff
) { return Substract(diff
); }
872 // multiply timespan by a scalar
873 inline wxTimeSpan
& Multiply(int n
);
874 // multiply timespan by a scalar
875 wxTimeSpan
& operator*=(int n
) { return Multiply(n
); }
876 // multiply timespan by a scalar
877 inline wxTimeSpan
operator*(int n
) const;
879 // return this timespan with inversed sign
880 wxTimeSpan
Negate() const { return wxTimeSpan(-GetValue()); }
881 // negate the value of the timespan
882 wxTimeSpan
& Neg() { m_diff
= -GetValue(); return *this; }
883 // negate the value of the timespan
884 wxTimeSpan
& operator-() { return Neg(); }
886 // return the absolute value of the timespan: does _not_ modify the
888 inline wxTimeSpan
Abs() const;
890 // there is intentionally no division because we don't want to
891 // introduce rounding errors in time calculations
893 // comparaison (see also operator versions below)
894 // ------------------------------------------------------------------------
896 // is the timespan null?
897 bool IsNull() const { return m_diff
== 0l; }
898 // returns true if the timespan is null
899 bool operator!() const { return !IsNull(); }
901 // is the timespan positive?
902 bool IsPositive() const { return m_diff
> 0l; }
904 // is the timespan negative?
905 bool IsNegative() const { return m_diff
< 0l; }
907 // are two timespans equal?
908 inline bool IsEqualTo(const wxTimeSpan
& ts
) const;
909 // compare two timestamps: works with the absolute values, i.e. -2
910 // hours is longer than 1 hour. Also, it will return FALSE if the
911 // timespans are equal in absolute value.
912 inline bool IsLongerThan(const wxTimeSpan
& ts
) const;
913 // compare two timestamps: works with the absolute values, i.e. 1
914 // hour is shorter than -2 hours. Also, it will return FALSE if the
915 // timespans are equal in absolute value.
916 bool IsShorterThan(const wxTimeSpan
& t
) const { return !IsLongerThan(t
); }
918 // breaking into days, hours, minutes and seconds
919 // ------------------------------------------------------------------------
921 // get the max number of weeks in this timespan
922 inline int GetWeeks() const;
923 // get the max number of days in this timespan
924 inline int GetDays() const;
925 // get the max number of hours in this timespan
926 inline int GetHours() const;
927 // get the max number of minutes in this timespan
928 inline int GetMinutes() const;
929 // get the max number of seconds in this timespan
930 inline wxLongLong
GetSeconds() const;
931 // get the number of milliseconds in this timespan
932 wxLongLong
GetMilliseconds() const { return m_diff
; }
934 // conversion to text
935 // ------------------------------------------------------------------------
937 // this function accepts strftime()-like format string (default
938 // argument corresponds to the preferred date and time representation
939 // for the current locale) and returns the string containing the
940 // resulting text representation. Notice that only some of format
941 // specifiers valid for wxDateTime are valid for wxTimeSpan: hours,
942 // minutes and seconds make sense, but not "PM/AM" string for example.
943 wxString
Format(const wxChar
*format
= _T("%c")) const;
944 // preferred date representation for the current locale
945 wxString
FormatDate() const { return Format(_T("%x")); }
946 // preferred time representation for the current locale
947 wxString
FormatTime() const { return Format(_T("%X")); }
950 // ------------------------------------------------------------------------
952 // get the internal representation
953 wxLongLong
GetValue() const { return m_diff
; }
956 // the (signed) time span in milliseconds
960 // ----------------------------------------------------------------------------
961 // This class is a "logical time span" and is useful for implementing program
962 // logic for such things as "add one month to the date" which, in general,
963 // doesn't mean to add 60*60*24*31 seconds to it, but to take the same date
964 // the next month (to understand that this is indeed different consider adding
965 // one month to Feb, 15 - we want to get Mar, 15, of course).
967 // When adding a month to the date, all lesser components (days, hours, ...)
970 // wxDateSpan can be either positive or negative. They may be
971 // multiplied by scalars which multiply all deltas by the scalar: i.e. 2*(1
972 // month and 1 day) is 2 months and 2 days. They can be added together and
973 // with wxDateTime or wxTimeSpan, but the type of result is different for each
976 // Beware about weeks: if you specify both weeks and days, the total number of
977 // days added will be 7*weeks + days! See also GetTotalDays() function.
979 // Finally, notice that for adding hours, minutes &c you don't need this
980 // class: wxTimeSpan will do the job because there are no subtleties
981 // associated with those.
982 // ----------------------------------------------------------------------------
984 class WXDLLEXPORT wxDateSpan
988 // ------------------------------------------------------------------------
990 // this many years/months/weeks/days
991 wxDateSpan(int years
= 0, int months
= 0, int weeks
= 0, int days
= 0)
999 // get an object for the given number of days
1000 static wxDateSpan
Days(int days
) { return wxDateSpan(0, 0, 0, days
); }
1002 // get an object for the given number of weeks
1003 static wxDateSpan
Weeks(int weeks
) { return wxDateSpan(0, 0, weeks
, 0); }
1005 // get an object for the given number of months
1006 static wxDateSpan
Months(int mon
) { return wxDateSpan(0, mon
, 0, 0); }
1008 // get an object for the given number of years
1009 static wxDateSpan
Years(int years
) { return wxDateSpan(years
, 0, 0, 0); }
1011 // default copy ctor is ok
1015 // accessors (all SetXXX() return the (modified) wxDateSpan object)
1016 // ------------------------------------------------------------------------
1018 // set number of years
1019 wxDateSpan
& SetYears(int n
) { m_years
= n
; return *this; }
1020 // set number of months
1021 wxDateSpan
& SetMonths(int n
) { m_months
= n
; return *this; }
1022 // set number of weeks
1023 wxDateSpan
& SetWeeks(int n
) { m_weeks
= n
; return *this; }
1024 // set number of days
1025 wxDateSpan
& SetDays(int n
) { m_days
= n
; return *this; }
1027 // get number of years
1028 int GetYears() const { return m_years
; }
1029 // get number of months
1030 int GetMonths() const { return m_months
; }
1031 // get number of weeks
1032 int GetWeeks() const { return m_weeks
; }
1033 // get number of days
1034 int GetDays() const { return m_days
; }
1035 // returns 7*GetWeeks() + GetDays()
1036 int GetTotalDays() const { return 7*m_weeks
+ m_days
; }
1039 // ------------------------------------------------------------------------
1041 // add another wxDateSpan to us
1042 inline wxDateSpan
& Add(const wxDateSpan
& other
);
1043 // add another wxDateSpan to us
1044 inline wxDateSpan
& operator+=(const wxDateSpan
& other
);
1046 // substract another wxDateSpan from us
1047 inline wxDateSpan
& Substract(const wxDateSpan
& other
);
1048 // substract another wxDateSpan from us
1049 inline wxDateSpan
& operator-=(const wxDateSpan
& other
);
1051 // return a copy of this time span with changed sign
1052 inline wxDateSpan
Negate() const;
1053 // inverse the sign of this timespan
1054 inline wxDateSpan
& Neg();
1055 // inverse the sign of this timespan
1056 wxDateSpan
& operator-() { return Neg(); }
1058 // multiply all components by a (signed) number
1059 inline wxDateSpan
& operator*=(int factor
);
1068 WXDLLEXPORT_DATA(extern wxDateSpan
) wxYear
;
1069 WXDLLEXPORT_DATA(extern wxDateSpan
) wxMonth
;
1070 WXDLLEXPORT_DATA(extern wxDateSpan
) wxWeek
;
1071 WXDLLEXPORT_DATA(extern wxDateSpan
) wxDay
;
1073 // ============================================================================
1074 // inline functions implementation
1075 // ============================================================================
1077 // don't include inline functions definitions when we're included from anything
1078 // else than datetime.cpp in debug builds: this minimizes rebuilds if we change
1079 // some inline function and the performance doesn't matter in the debug builds.
1081 #if !defined(__WXDEBUG__) || defined(wxDEFINE_TIME_CONSTANTS)
1082 #define INCLUDED_FROM_WX_DATETIME_H
1083 #include "wx/datetime.inl"
1084 #undef INCLUDED_FROM_WX_DATETIME_H
1087 // if we defined it to be empty above, restore it now
1090 // ============================================================================
1092 // ============================================================================
1094 // ----------------------------------------------------------------------------
1095 // wxDateTime operators
1096 // ----------------------------------------------------------------------------
1098 inline bool WXDLLEXPORT
operator<(const wxDateTime
&t1
, const wxDateTime
&t2
)
1100 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1102 return t1
.GetValue() < t2
.GetValue();
1105 inline bool WXDLLEXPORT
operator<=(const wxDateTime
&t1
, const wxDateTime
&t2
)
1107 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1109 return t1
.GetValue() <= t2
.GetValue();
1112 inline bool WXDLLEXPORT
operator>(const wxDateTime
&t1
, const wxDateTime
&t2
)
1114 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1116 return t1
.GetValue() > t2
.GetValue();
1119 inline bool WXDLLEXPORT
operator>=(const wxDateTime
&t1
, const wxDateTime
&t2
)
1121 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1123 return t1
.GetValue() >= t2
.GetValue();
1126 inline bool WXDLLEXPORT
operator==(const wxDateTime
&t1
, const wxDateTime
&t2
)
1128 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1130 return t1
.GetValue() == t2
.GetValue();
1133 inline bool WXDLLEXPORT
operator!=(const wxDateTime
&t1
, const wxDateTime
&t2
)
1135 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1137 return t1
.GetValue() != t2
.GetValue();
1140 inline wxTimeSpan WXDLLEXPORT
operator-(const wxDateTime
&t1
,
1141 const wxDateTime
&t2
)
1143 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1145 return wxTimeSpan(t1
.GetValue() - t2
.GetValue());
1148 // ----------------------------------------------------------------------------
1149 // wxTimeSpan operators
1150 // ----------------------------------------------------------------------------
1152 inline wxTimeSpan WXDLLEXPORT
operator+(const wxTimeSpan
& ts1
,
1153 const wxTimeSpan
& ts2
)
1155 return wxTimeSpan(ts1
.GetValue() + ts2
.GetValue());
1158 inline wxTimeSpan WXDLLEXPORT
operator-(const wxTimeSpan
& ts1
,
1159 const wxTimeSpan
& ts2
)
1161 return wxTimeSpan(ts1
.GetValue() - ts2
.GetValue());
1164 inline bool WXDLLEXPORT
operator<(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1166 return t1
.GetValue() < t2
.GetValue();
1169 inline bool WXDLLEXPORT
operator<=(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1171 return t1
.GetValue() <= t2
.GetValue();
1174 inline bool WXDLLEXPORT
operator>(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1176 return t1
.GetValue() > t2
.GetValue();
1179 inline bool WXDLLEXPORT
operator>=(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1181 return t1
.GetValue() >= t2
.GetValue();
1184 inline bool WXDLLEXPORT
operator==(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1186 return t1
.GetValue() == t2
.GetValue();
1189 inline bool WXDLLEXPORT
operator!=(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1191 return t1
.GetValue() != t2
.GetValue();
1194 // ----------------------------------------------------------------------------
1196 // ----------------------------------------------------------------------------
1198 inline WXDLLEXPORT wxDateSpan
operator+(const wxDateSpan
& rt1
,
1199 const wxDateSpan
& rt2
)
1201 return wxDateSpan(rt1
.GetYears() + rt2
.GetYears(),
1202 rt1
.GetMonths() + rt2
.GetMonths(),
1203 rt1
.GetWeeks() + rt2
.GetWeeks(),
1204 rt1
.GetDays() + rt2
.GetDays());
1207 #endif // _WX_DATETIME_H