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
298 // or use the default country with any other
300 // TODO add more countries (for this we must know about DST and/or
301 // holidays for this country)
303 // Western European countries: we assume that they all follow the same
304 // DST rules (true or false?)
305 Country_WesternEurope_Start
,
306 Country_EEC
= Country_WesternEurope_Start
,
310 Country_WesternEurope_End
= UK
,
317 // symbolic names for the months
320 Jan
, Feb
, Mar
, Apr
, May
, Jun
, Jul
, Aug
, Sep
, Oct
, Nov
, Dec
, Inv_Month
323 // symbolic names for the weekdays
326 Sun
, Mon
, Tue
, Wed
, Thu
, Fri
, Sat
, Inv_WeekDay
329 // invalid value for the year
332 Inv_Year
= SHRT_MIN
// should hold in wxDateTime_t
336 // ------------------------------------------------------------------------
338 // a class representing a time zone: basicly, this is just an offset
339 // (in seconds) from GMT
344 TimeZone(wxDateTime_t offset
= 0) { m_offset
= offset
; }
346 int GetOffset() const { return m_offset
; }
349 // offset for this timezone from GMT in seconds
353 // standard struct tm is limited to the years from 1900 (because
354 // tm_year field is the offset from 1900), so we use our own struct
355 // instead to represent broken down time
357 // NB: this struct should always be kept normalized (i.e. mon should
358 // be < 12, 1 <= day <= 31 &c), so use AddMonths(), AddDays()
359 // instead of modifying the member fields directly!
362 wxDateTime_t msec
, sec
, min
, hour
, mday
;
366 // default ctor inits the object to an invalid value
369 // ctor from struct tm and the timezone
370 Tm(const struct tm
& tm
, const TimeZone
& tz
);
372 // check that the given date/time is valid (in Gregorian calendar)
373 bool IsValid() const;
376 WeekDay
GetWeekDay() // not const because wday may be changed
378 if ( wday
== Inv_WeekDay
)
381 return (WeekDay
)wday
;
384 // add the given number of months to the date keeping it normalized
385 void AddMonths(int monDiff
);
387 // add the given number of months to the date keeping it normalized
388 void AddDays(int dayDiff
);
391 // compute the weekday from other fields
392 void ComputeWeekDay();
394 // the timezone we correspond to
397 // these values can't be accessed directly because they're not always
398 // computed and we calculate them on demand
399 wxDateTime_t wday
, yday
;
403 // ------------------------------------------------------------------------
405 // set the current country
406 static void SetCountry(Country country
);
407 // get the current country
408 static Country
GetCountry();
410 // return TRUE if the country is a West European one (in practice,
411 // this means that the same DST rules as for EEC apply)
412 static bool IsWestEuropeanCountry(Country country
= Country_Default
);
414 // return the current year
415 static int GetCurrentYear(Calendar cal
= Gregorian
);
417 // convert the year as returned by wxDateTime::GetYear() to a year
418 // suitable for BC/AD notation. The difference is that BC year 1
419 // corresponds to the year 0 (while BC year 0 didn't exist) and AD
420 // year N is just year N.
421 static int ConvertYearToBC(int year
);
423 // return the current month
424 static Month
GetCurrentMonth(Calendar cal
= Gregorian
);
426 // returns TRUE if the given year is a leap year in the given calendar
427 static bool IsLeapYear(int year
= Inv_Year
, Calendar cal
= Gregorian
);
429 // get the century (19 for 1999, 20 for 2000 and -5 for 492 BC)
430 static int GetCentury(int year
= Inv_Year
);
432 // returns the number of days in this year (356 or 355 for Gregorian
433 // calendar usually :-)
434 static wxDateTime_t
GetNumberOfDays(int year
, Calendar cal
= Gregorian
);
436 // get the number of the days in the given month (default value for
437 // the year means the current one)
438 static wxDateTime_t
GetNumberOfDays(Month month
,
440 Calendar cal
= Gregorian
);
442 // get the full (default) or abbreviated month name in the current
443 // locale, returns empty string on error
444 static wxString
GetMonthName(Month month
, bool abbr
= FALSE
);
446 // get the full (default) or abbreviated weekday name in the current
447 // locale, returns empty string on error
448 static wxString
GetWeekDayName(WeekDay weekday
, bool abbr
= FALSE
);
450 // return TRUE if the given country uses DST for this year
451 static bool IsDSTApplicable(int year
= Inv_Year
,
452 Country country
= Country_Default
);
454 // get the beginning of DST for this year, will return invalid object
455 // if no DST applicable in this year. The default value of the
456 // parameter means to take the current year.
457 static wxDateTime
GetBeginDST(int year
= Inv_Year
,
458 Country country
= Country_Default
);
459 // get the end of DST for this year, will return invalid object
460 // if no DST applicable in this year. The default value of the
461 // parameter means to take the current year.
462 static wxDateTime
GetEndDST(int year
= Inv_Year
,
463 Country country
= Country_Default
);
465 // return the wxDateTime object for the current time
466 static inline wxDateTime
Now();
468 // constructors: you should test whether the constructor succeeded with
469 // IsValid() function. The values Inv_Month and Inv_Year for the
470 // parameters mean take current month and/or year values.
471 // ------------------------------------------------------------------------
473 // default ctor does not initialize the object, use Set()!
476 // from time_t: seconds since the Epoch 00:00:00 UTC, Jan 1, 1970)
477 inline wxDateTime(time_t timet
);
478 // from broken down time/date (only for standard Unix range)
479 inline wxDateTime(const struct tm
& tm
);
480 // from broken down time/date (any range)
481 inline wxDateTime(const Tm
& tm
);
483 // from JDN (beware of rounding errors)
484 inline wxDateTime(double jdn
);
486 // from separate values for each component, date set to today
487 inline wxDateTime(wxDateTime_t hour
,
488 wxDateTime_t minute
= 0,
489 wxDateTime_t second
= 0,
490 wxDateTime_t millisec
= 0);
491 // from separate values for each component with explicit date
492 inline wxDateTime(wxDateTime_t day
, // day of the month
493 Month month
= Inv_Month
,
494 int year
= Inv_Year
, // 1999, not 99 please!
495 wxDateTime_t hour
= 0,
496 wxDateTime_t minute
= 0,
497 wxDateTime_t second
= 0,
498 wxDateTime_t millisec
= 0);
500 // default copy ctor ok
504 // assignment operators and Set() functions: all non const methods return
505 // the reference to this object. IsValid() should be used to test whether
506 // the function succeeded.
507 // ------------------------------------------------------------------------
509 // set to the current time
510 inline wxDateTime
& SetToCurrent();
512 // set to given time_t value
513 inline wxDateTime
& Set(time_t timet
);
515 // set to given broken down time/date
516 wxDateTime
& Set(const struct tm
& tm
);
518 // set to given broken down time/date
519 inline wxDateTime
& Set(const Tm
& tm
);
521 // set to given JDN (beware of rounding errors)
522 wxDateTime
& Set(double jdn
);
524 // set to given time, date = today
525 wxDateTime
& Set(wxDateTime_t hour
,
526 wxDateTime_t minute
= 0,
527 wxDateTime_t second
= 0,
528 wxDateTime_t millisec
= 0);
530 // from separate values for each component with explicit date
531 // (defaults for month and year are the current values)
532 wxDateTime
& Set(wxDateTime_t day
,
533 Month month
= Inv_Month
,
534 int year
= Inv_Year
, // 1999, not 99 please!
535 wxDateTime_t hour
= 0,
536 wxDateTime_t minute
= 0,
537 wxDateTime_t second
= 0,
538 wxDateTime_t millisec
= 0);
540 // resets time to 00:00:00, doesn't change the date
541 wxDateTime
& ResetTime();
543 // the following functions don't change the values of the other
544 // fields, i.e. SetMinute() won't change either hour or seconds value
547 wxDateTime
& SetYear(int year
);
549 wxDateTime
& SetMonth(Month month
);
550 // set the day of the month
551 wxDateTime
& SetDay(wxDateTime_t day
);
553 wxDateTime
& SetHour(wxDateTime_t hour
);
555 wxDateTime
& SetMinute(wxDateTime_t minute
);
557 wxDateTime
& SetSecond(wxDateTime_t second
);
559 wxDateTime
& SetMillisecond(wxDateTime_t millisecond
);
561 // assignment operator from time_t
562 wxDateTime
& operator=(time_t timet
) { return Set(timet
); }
564 // assignment operator from broken down time/date
565 wxDateTime
& operator=(const struct tm
& tm
) { return Set(tm
); }
567 // assignment operator from broken down time/date
568 wxDateTime
& operator=(const Tm
& tm
) { return Set(tm
); }
570 // default assignment operator is ok
572 // calendar calculations (functions which set the date only leave the time
573 // unchanged, e.g. don't explictly zero it)
574 // ------------------------------------------------------------------------
576 // set to Nth occurence of given weekday in the given month of the
577 // given year (time is set to 0), return TRUE on success and FALSE on
578 // failure. n may be positive (1..5) or negative to count from the end
579 // of the month (see helper function SetToLastWeekDay())
580 bool SetToWeekDay(WeekDay weekday
,
582 Month month
= Inv_Month
,
583 int year
= Inv_Year
);
585 // sets to the last weekday in the given month, year
586 inline bool SetToLastWeekDay(WeekDay weekday
,
587 Month month
= Inv_Month
,
588 int year
= Inv_Year
);
590 // sets the date to the given day of the given week in the year,
591 // returns TRUE on success and FALSE if given date doesn't exist (e.g.
593 bool SetToTheWeek(wxDateTime_t numWeek
, WeekDay weekday
= Mon
);
595 // sets the date to the last day of the given (or current) month or the
596 // given (or current) year
597 wxDateTime
& SetToLastMonthDay(Month month
= Inv_Month
,
598 int year
= Inv_Year
);
600 // The definitions below were taken verbatim from
602 // http://www.capecod.net/~pbaum/date/date0.htm
604 // (Peter Baum's home page)
606 // definition: The Julian Day Number, Julian Day, or JD of a
607 // particular instant of time is the number of days and fractions of a
608 // day since 12 hours Universal Time (Greenwich mean noon) on January
609 // 1 of the year -4712, where the year is given in the Julian
610 // proleptic calendar. The idea of using this reference date was
611 // originally proposed by Joseph Scalizer in 1582 to count years but
612 // it was modified by 19th century astronomers to count days. One
613 // could have equivalently defined the reference time to be noon of
614 // November 24, -4713 if were understood that Gregorian calendar rules
615 // were applied. Julian days are Julian Day Numbers and are not to be
616 // confused with Julian dates.
618 // definition: The Rata Die number is a date specified as the number
619 // of days relative to a base date of December 31 of the year 0. Thus
620 // January 1 of the year 1 is Rata Die day 1.
622 // get the Julian Day number (the fractional part specifies the time of
623 // the day, related to noon - beware of rounding errors!)
624 double GetJulianDayNumber() const;
625 double GetJDN() const { return GetJulianDayNumber(); }
627 // get the Modified Julian Day number: it is equal to JDN - 2400000.5
628 // and so integral MJDs correspond to the midnights (and not noons).
629 // MJD 0 is Nov 17, 1858
630 double GetModifiedJulianDayNumber() const { return GetJDN() - 2400000.5; }
631 double GetMJD() const { return GetModifiedJulianDayNumber(); }
633 // get the Rata Die number
634 double GetRataDie() const;
636 // TODO algorithms for calculating some important dates, such as
637 // religious holidays (Easter...) or moon/solar eclipses? Some
638 // algorithms can be found in the calendar FAQ
640 // timezone stuff: a wxDateTime object constructed using given
641 // day/month/year/hour/min/sec values correspond to this moment in local
642 // time. Using the functions below, it may be converted to another time
643 // zone (for example, the Unix epoch is wxDateTime(1, Jan, 1970).ToGMT())
645 // Converting to the local time zone doesn't do anything.
646 // ------------------------------------------------------------------------
648 // transform to any given timezone
649 inline wxDateTime
ToTimezone(const TimeZone
& tz
) const;
650 wxDateTime
& MakeTimezone(const TimeZone
& tz
);
652 // transform to GMT/UTC
653 wxDateTime
ToGMT() const { return ToTimezone(GMT0
); }
654 wxDateTime
& MakeGMT() { return MakeTimezone(GMT0
); }
656 // is daylight savings time in effect at this moment according to the
657 // rules of the specified country?
659 // Return value is > 0 if DST is in effect, 0 if it is not and -1 if
660 // the information is not available (this is compatible with ANSI C)
661 int IsDST(Country country
= Country_Default
) const;
663 // accessors: many of them take the timezone parameter which indicates the
664 // timezone for which to make the calculations and the default value means
665 // to do it for the current timezone of this machine (even if the function
666 // only operates with the date it's necessary because a date may wrap as
667 // result of timezone shift)
668 // ------------------------------------------------------------------------
670 // is the date valid (FALSE for uninitialized objects as well as after
671 // the functions which failed to convert the date to supported range)
672 inline bool IsValid() const { return this != &ms_InvDateTime
; }
674 // get the broken down date/time representation in the given timezone
676 // If you wish to get several time components (day, month and year),
677 // consider getting the whole Tm strcuture first and retrieving the
678 // value from it - this is much more efficient
679 Tm
GetTm(const TimeZone
& tz
= Local
) const;
681 // get the number of seconds since the Unix epoch - returns (time_t)-1
682 // if the value is out of range
683 inline time_t GetTicks() const;
685 // get the year (returns Inv_Year if date is invalid)
686 int GetYear(const TimeZone
& tz
= Local
) const
687 { return GetTm(tz
).year
; }
688 // get the month (Inv_Month if date is invalid)
689 Month
GetMonth(const TimeZone
& tz
= Local
) const
690 { return (Month
)GetTm(tz
).mon
; }
691 // get the month day (in 1..31 range, 0 if date is invalid)
692 wxDateTime_t
GetDay(const TimeZone
& tz
= Local
) const
693 { return GetTm(tz
).mday
; }
694 // get the day of the week (Inv_WeekDay if date is invalid)
695 WeekDay
GetWeekDay(const TimeZone
& tz
= Local
) const
696 { return GetTm(tz
).GetWeekDay(); }
697 // get the hour of the day
698 wxDateTime_t
GetHour(const TimeZone
& tz
= Local
) const
699 { return GetTm(tz
).hour
; }
701 wxDateTime_t
GetMinute(const TimeZone
& tz
= Local
) const
702 { return GetTm(tz
).min
; }
704 wxDateTime_t
GetSecond(const TimeZone
& tz
= Local
) const
705 { return GetTm(tz
).sec
; }
707 wxDateTime_t
GetMillisecond(const TimeZone
& tz
= Local
) const
708 { return GetTm(tz
).msec
; }
710 // get the day since the year start (1..366, 0 if date is invalid)
711 wxDateTime_t
GetDayOfYear(const TimeZone
& tz
= Local
) const;
712 // get the week number since the year start (1..52, 0 if date is
714 wxDateTime_t
GetWeekOfYear(const TimeZone
& tz
= Local
) const;
716 // is this date a work day? This depends on a country, of course,
717 // because the holidays are different in different countries
718 bool IsWorkDay(Country country
= Country_Default
,
719 const TimeZone
& tz
= Local
) const;
721 // is this date later than Gregorian calendar introduction for the
722 // given country (see enum GregorianAdoption)?
724 // NB: this function shouldn't be considered as absolute authoiruty in
725 // the matter. Besides, for some countries the exact date of
726 // adoption of the Gregorian calendar is simply unknown.
727 bool IsGregorianDate(GregorianAdoption country
= Gr_Standard
) const;
729 // comparison (see also functions below for operator versions)
730 // ------------------------------------------------------------------------
732 // returns TRUE if the two moments are strictly identical
733 inline bool IsEqualTo(const wxDateTime
& datetime
) const;
735 // returns TRUE if the two moments are identical
736 inline bool operator==(const wxDateTime
& datetime
) const;
738 // returns TRUE if the two moments are different
739 inline bool operator!=(const wxDateTime
& datetime
) const;
741 // returns TRUE if the date is strictly earlier than the given one
742 inline bool IsEarlierThan(const wxDateTime
& datetime
) const;
744 // returns TRUE if the date is strictly later than the given one
745 inline bool IsLaterThan(const wxDateTime
& datetime
) const;
747 // returns TRUE if the date is strictly in the given range
748 inline bool IsStrictlyBetween(const wxDateTime
& t1
,
749 const wxDateTime
& t2
) const;
751 // returns TRUE if the date is in the given range
752 inline bool IsBetween(const wxDateTime
& t1
, const wxDateTime
& t2
) const;
754 // date operations: for the non-const methods, the return value is this
755 // object itself (see also functions below for operator versions)
756 // ------------------------------------------------------------------------
758 // add a time span (positive or negative)
759 inline wxDateTime
& Add(const wxTimeSpan
& diff
);
760 // add a time span (positive or negative)
761 inline wxDateTime
& operator+=(const wxTimeSpan
& diff
);
763 // substract a time span (positive or negative)
764 inline wxDateTime
& Substract(const wxTimeSpan
& diff
);
765 // substract a time span (positive or negative)
766 inline wxDateTime
& operator-=(const wxTimeSpan
& diff
);
768 // add a date span (positive or negative)
769 wxDateTime
& Add(const wxDateSpan
& diff
);
770 // add a date span (positive or negative)
771 inline wxDateTime
& operator+=(const wxDateSpan
& diff
);
773 // substract a date span (positive or negative)
774 inline wxDateTime
& Substract(const wxDateSpan
& diff
);
775 // substract a date span (positive or negative)
776 inline wxDateTime
& operator-=(const wxDateSpan
& diff
);
778 // substract a date (may result in positive or negative time span)
779 inline wxTimeSpan
Substract(const wxDateTime
& datetime
) const;
780 // substract a date (may result in positive or negative time span)
781 inline wxTimeSpan
operator-(const wxDateTime
& datetime
) const;
783 // conversion to/from text: all conversions from text return TRUE on
784 // success or FALSE if the date is malformed/out of supported range
785 // ------------------------------------------------------------------------
787 // parse a string in RFC 822 format (found e.g. in mail headers and
788 // having the form "Wed, 10 Feb 1999 19:07:07 +0100")
789 bool ParseRfc822Date(const wxString
& date
);
790 // parse a date/time in the given format (see strptime(3))
791 bool ParseFormat(const wxString
& date
, const char *format
= "%c");
792 // parse a string containing the date/time in "free" format, this
793 // function will try to make an educated guess at the string contents
794 // (and return FALSE if it fails)
795 bool ParseDateTime(const wxString
& date
);
797 // this function accepts strftime()-like format string (default
798 // argument corresponds to the preferred date and time representation
799 // for the current locale) and returns the string containing the
800 // resulting text representation
801 wxString
Format(const wxChar
*format
= _T("%c"),
802 const TimeZone
& tz
= Local
) const;
803 // preferred date representation for the current locale
804 wxString
FormatDate() const { return Format(_T("%x")); }
805 // preferred time representation for the current locale
806 wxString
FormatTime() const { return Format(_T("%X")); }
809 // ------------------------------------------------------------------------
811 // get the internal representation
812 inline wxLongLong
GetValue() const;
814 // a helper function to get the current time_t
815 static time_t GetTimeNow() { return time((time_t *)NULL
); }
817 // another one to get the current time broken down
818 static struct tm
*GetTmNow()
820 time_t t
= GetTimeNow();
821 return localtime(&t
);
825 // the current country - as it's the same for all program objects (unless
826 // it runs on a _really_ big cluster system :-), this is a static member:
827 // see SetCountry() and GetCountry()
828 static Country ms_country
;
830 // this constant is used to transform a time_t value to the internal
831 // representation, as time_t is in seconds and we use milliseconds it's
833 static const unsigned int TIME_T_FACTOR
;
835 // invalid wxDateTime object - returned by all functions which return
836 // "wxDateTime &" on failure
837 static wxDateTime ms_InvDateTime
;
839 // returns TRUE if we fall in range in which we can use standard ANSI C
841 inline bool IsInStdRange() const;
843 // the internal representation of the time is the amount of milliseconds
844 // elapsed since the origin which is set by convention to the UNIX/C epoch
845 // value: the midnight of January 1, 1970 (UTC)
849 // ----------------------------------------------------------------------------
850 // This class contains a difference between 2 wxDateTime values, so it makes
851 // sense to add it to wxDateTime and it is the result of substraction of 2
852 // objects of that class. See also wxDateSpan.
853 // ----------------------------------------------------------------------------
855 class WXDLLEXPORT wxTimeSpan
859 // ------------------------------------------------------------------------
861 // return the timespan for the given number of seconds
862 static wxTimeSpan
Seconds(int sec
) { return wxTimeSpan(0, 0, sec
); }
864 // return the timespan for the given number of minutes
865 static wxTimeSpan
Minutes(int min
) { return wxTimeSpan(0, min
, 0 ); }
867 // return the timespan for the given number of hours
868 static wxTimeSpan
Hours(int hours
) { return wxTimeSpan(hours
, 0, 0); }
870 // default ctor constructs the 0 time span
873 // from separate values for each component, date set to 0 (hours are
874 // not restricted to 0..24 range, neither are minutes, seconds or
876 inline wxTimeSpan(int hours
,
879 int milliseconds
= 0);
881 // from internal representation
882 wxTimeSpan(wxLongLong diff
) : m_diff(diff
) { }
884 // default copy ctor is ok
888 // arithmetics with time spans
889 // ------------------------------------------------------------------------
891 // add two timespans together
892 inline wxTimeSpan
& Add(const wxTimeSpan
& diff
);
893 // add two timespans together
894 wxTimeSpan
& operator+=(const wxTimeSpan
& diff
) { return Add(diff
); }
896 // substract another timespan
897 inline wxTimeSpan
& Substract(const wxTimeSpan
& diff
);
898 // substract another timespan
899 wxTimeSpan
& operator-=(const wxTimeSpan
& diff
) { return Substract(diff
); }
901 // multiply timespan by a scalar
902 inline wxTimeSpan
& Multiply(int n
);
903 // multiply timespan by a scalar
904 wxTimeSpan
& operator*=(int n
) { return Multiply(n
); }
905 // multiply timespan by a scalar
906 inline wxTimeSpan
operator*(int n
) const;
908 // return this timespan with inversed sign
909 wxTimeSpan
Negate() const { return wxTimeSpan(-GetValue()); }
910 // negate the value of the timespan
911 wxTimeSpan
& Neg() { m_diff
= -GetValue(); return *this; }
912 // negate the value of the timespan
913 wxTimeSpan
& operator-() { return Neg(); }
915 // return the absolute value of the timespan: does _not_ modify the
917 inline wxTimeSpan
Abs() const;
919 // there is intentionally no division because we don't want to
920 // introduce rounding errors in time calculations
922 // comparaison (see also operator versions below)
923 // ------------------------------------------------------------------------
925 // is the timespan null?
926 bool IsNull() const { return m_diff
== 0l; }
927 // returns true if the timespan is null
928 bool operator!() const { return !IsNull(); }
930 // is the timespan positive?
931 bool IsPositive() const { return m_diff
> 0l; }
933 // is the timespan negative?
934 bool IsNegative() const { return m_diff
< 0l; }
936 // are two timespans equal?
937 inline bool IsEqualTo(const wxTimeSpan
& ts
) const;
938 // compare two timestamps: works with the absolute values, i.e. -2
939 // hours is longer than 1 hour. Also, it will return FALSE if the
940 // timespans are equal in absolute value.
941 inline bool IsLongerThan(const wxTimeSpan
& ts
) const;
942 // compare two timestamps: works with the absolute values, i.e. 1
943 // hour is shorter than -2 hours. Also, it will return FALSE if the
944 // timespans are equal in absolute value.
945 bool IsShorterThan(const wxTimeSpan
& t
) const { return !IsLongerThan(t
); }
947 // breaking into days, hours, minutes and seconds
948 // ------------------------------------------------------------------------
950 // get the max number of weeks in this timespan
951 inline int GetWeeks() const;
952 // get the max number of days in this timespan
953 inline int GetDays() const;
954 // get the max number of hours in this timespan
955 inline int GetHours() const;
956 // get the max number of minutes in this timespan
957 inline int GetMinutes() const;
958 // get the max number of seconds in this timespan
959 inline wxLongLong
GetSeconds() const;
960 // get the number of milliseconds in this timespan
961 wxLongLong
GetMilliseconds() const { return m_diff
; }
963 // conversion to text
964 // ------------------------------------------------------------------------
966 // this function accepts strftime()-like format string (default
967 // argument corresponds to the preferred date and time representation
968 // for the current locale) and returns the string containing the
969 // resulting text representation. Notice that only some of format
970 // specifiers valid for wxDateTime are valid for wxTimeSpan: hours,
971 // minutes and seconds make sense, but not "PM/AM" string for example.
972 wxString
Format(const wxChar
*format
= _T("%c")) const;
973 // preferred date representation for the current locale
974 wxString
FormatDate() const { return Format(_T("%x")); }
975 // preferred time representation for the current locale
976 wxString
FormatTime() const { return Format(_T("%X")); }
979 // ------------------------------------------------------------------------
981 // get the internal representation
982 wxLongLong
GetValue() const { return m_diff
; }
985 // the (signed) time span in milliseconds
989 // ----------------------------------------------------------------------------
990 // This class is a "logical time span" and is useful for implementing program
991 // logic for such things as "add one month to the date" which, in general,
992 // doesn't mean to add 60*60*24*31 seconds to it, but to take the same date
993 // the next month (to understand that this is indeed different consider adding
994 // one month to Feb, 15 - we want to get Mar, 15, of course).
996 // When adding a month to the date, all lesser components (days, hours, ...)
999 // wxDateSpan can be either positive or negative. They may be
1000 // multiplied by scalars which multiply all deltas by the scalar: i.e. 2*(1
1001 // month and 1 day) is 2 months and 2 days. They can be added together and
1002 // with wxDateTime or wxTimeSpan, but the type of result is different for each
1005 // Beware about weeks: if you specify both weeks and days, the total number of
1006 // days added will be 7*weeks + days! See also GetTotalDays() function.
1008 // Finally, notice that for adding hours, minutes &c you don't need this
1009 // class: wxTimeSpan will do the job because there are no subtleties
1010 // associated with those.
1011 // ----------------------------------------------------------------------------
1013 class WXDLLEXPORT wxDateSpan
1017 // ------------------------------------------------------------------------
1019 // this many years/months/weeks/days
1020 wxDateSpan(int years
= 0, int months
= 0, int weeks
= 0, int days
= 0)
1028 // get an object for the given number of days
1029 static wxDateSpan
Days(int days
) { return wxDateSpan(0, 0, 0, days
); }
1031 // get an object for the given number of weeks
1032 static wxDateSpan
Weeks(int weeks
) { return wxDateSpan(0, 0, weeks
, 0); }
1034 // get an object for the given number of months
1035 static wxDateSpan
Months(int mon
) { return wxDateSpan(0, mon
, 0, 0); }
1037 // get an object for the given number of years
1038 static wxDateSpan
Years(int years
) { return wxDateSpan(years
, 0, 0, 0); }
1040 // default copy ctor is ok
1044 // accessors (all SetXXX() return the (modified) wxDateSpan object)
1045 // ------------------------------------------------------------------------
1047 // set number of years
1048 wxDateSpan
& SetYears(int n
) { m_years
= n
; return *this; }
1049 // set number of months
1050 wxDateSpan
& SetMonths(int n
) { m_months
= n
; return *this; }
1051 // set number of weeks
1052 wxDateSpan
& SetWeeks(int n
) { m_weeks
= n
; return *this; }
1053 // set number of days
1054 wxDateSpan
& SetDays(int n
) { m_days
= n
; return *this; }
1056 // get number of years
1057 int GetYears() const { return m_years
; }
1058 // get number of months
1059 int GetMonths() const { return m_months
; }
1060 // get number of weeks
1061 int GetWeeks() const { return m_weeks
; }
1062 // get number of days
1063 int GetDays() const { return m_days
; }
1064 // returns 7*GetWeeks() + GetDays()
1065 int GetTotalDays() const { return 7*m_weeks
+ m_days
; }
1068 // ------------------------------------------------------------------------
1070 // add another wxDateSpan to us
1071 inline wxDateSpan
& Add(const wxDateSpan
& other
);
1072 // add another wxDateSpan to us
1073 inline wxDateSpan
& operator+=(const wxDateSpan
& other
);
1075 // substract another wxDateSpan from us
1076 inline wxDateSpan
& Substract(const wxDateSpan
& other
);
1077 // substract another wxDateSpan from us
1078 inline wxDateSpan
& operator-=(const wxDateSpan
& other
);
1080 // return a copy of this time span with changed sign
1081 inline wxDateSpan
Negate() const;
1082 // inverse the sign of this timespan
1083 inline wxDateSpan
& Neg();
1084 // inverse the sign of this timespan
1085 wxDateSpan
& operator-() { return Neg(); }
1087 // multiply all components by a (signed) number
1088 inline wxDateSpan
& operator*=(int factor
);
1097 WXDLLEXPORT_DATA(extern wxDateSpan
) wxYear
;
1098 WXDLLEXPORT_DATA(extern wxDateSpan
) wxMonth
;
1099 WXDLLEXPORT_DATA(extern wxDateSpan
) wxWeek
;
1100 WXDLLEXPORT_DATA(extern wxDateSpan
) wxDay
;
1102 // ============================================================================
1103 // inline functions implementation
1104 // ============================================================================
1106 // don't include inline functions definitions when we're included from anything
1107 // else than datetime.cpp in debug builds: this minimizes rebuilds if we change
1108 // some inline function and the performance doesn't matter in the debug builds.
1110 #if !defined(__WXDEBUG__) || defined(wxDEFINE_TIME_CONSTANTS)
1111 #define INCLUDED_FROM_WX_DATETIME_H
1112 #include "wx/datetime.inl"
1113 #undef INCLUDED_FROM_WX_DATETIME_H
1116 // if we defined it to be empty above, restore it now
1119 // ============================================================================
1121 // ============================================================================
1123 // ----------------------------------------------------------------------------
1124 // wxDateTime operators
1125 // ----------------------------------------------------------------------------
1127 inline bool WXDLLEXPORT
operator<(const wxDateTime
&t1
, const wxDateTime
&t2
)
1129 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1131 return t1
.GetValue() < t2
.GetValue();
1134 inline bool WXDLLEXPORT
operator<=(const wxDateTime
&t1
, const wxDateTime
&t2
)
1136 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1138 return t1
.GetValue() <= t2
.GetValue();
1141 inline bool WXDLLEXPORT
operator>(const wxDateTime
&t1
, const wxDateTime
&t2
)
1143 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1145 return t1
.GetValue() > t2
.GetValue();
1148 inline bool WXDLLEXPORT
operator>=(const wxDateTime
&t1
, const wxDateTime
&t2
)
1150 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1152 return t1
.GetValue() >= t2
.GetValue();
1155 inline bool WXDLLEXPORT
operator==(const wxDateTime
&t1
, const wxDateTime
&t2
)
1157 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1159 return t1
.GetValue() == t2
.GetValue();
1162 inline bool WXDLLEXPORT
operator!=(const wxDateTime
&t1
, const wxDateTime
&t2
)
1164 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1166 return t1
.GetValue() != t2
.GetValue();
1169 inline wxTimeSpan WXDLLEXPORT
operator-(const wxDateTime
&t1
,
1170 const wxDateTime
&t2
)
1172 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1174 return wxTimeSpan(t1
.GetValue() - t2
.GetValue());
1177 // ----------------------------------------------------------------------------
1178 // wxTimeSpan operators
1179 // ----------------------------------------------------------------------------
1181 inline wxTimeSpan WXDLLEXPORT
operator+(const wxTimeSpan
& ts1
,
1182 const wxTimeSpan
& ts2
)
1184 return wxTimeSpan(ts1
.GetValue() + ts2
.GetValue());
1187 inline wxTimeSpan WXDLLEXPORT
operator-(const wxTimeSpan
& ts1
,
1188 const wxTimeSpan
& ts2
)
1190 return wxTimeSpan(ts1
.GetValue() - ts2
.GetValue());
1193 inline bool WXDLLEXPORT
operator<(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1195 return t1
.GetValue() < t2
.GetValue();
1198 inline bool WXDLLEXPORT
operator<=(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1200 return t1
.GetValue() <= t2
.GetValue();
1203 inline bool WXDLLEXPORT
operator>(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1205 return t1
.GetValue() > t2
.GetValue();
1208 inline bool WXDLLEXPORT
operator>=(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1210 return t1
.GetValue() >= t2
.GetValue();
1213 inline bool WXDLLEXPORT
operator==(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1215 return t1
.GetValue() == t2
.GetValue();
1218 inline bool WXDLLEXPORT
operator!=(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1220 return t1
.GetValue() != t2
.GetValue();
1223 // ----------------------------------------------------------------------------
1225 // ----------------------------------------------------------------------------
1227 inline WXDLLEXPORT wxDateSpan
operator+(const wxDateSpan
& rt1
,
1228 const wxDateSpan
& rt2
)
1230 return wxDateSpan(rt1
.GetYears() + rt2
.GetYears(),
1231 rt1
.GetMonths() + rt2
.GetMonths(),
1232 rt1
.GetWeeks() + rt2
.GetWeeks(),
1233 rt1
.GetDays() + rt2
.GetDays());
1236 #endif // _WX_DATETIME_H