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 wxTimeZone a class)
41 * 2. getdate() function like under Solaris
42 * 3. text conversion for wxDateSpan
46 The three classes declared in this header represent:
48 1. An absolute moment in the time (wxDateTime)
49 2. A difference between two moments in the time, positive or negative
51 3. A logical difference between two dates expressed in
52 years/months/weeks/days (wxDateSpan)
54 The following arithmetic operations are permitted (all others are not):
59 wxDateTime + wxTimeSpan = wxDateTime
60 wxDateTime + wxDateSpan = wxDateTime
61 wxTimeSpan + wxTimeSpan = wxTimeSpan
62 wxDateSpan + wxDateSpan = wxDateSpan
66 wxDateTime - wxDateTime = wxTimeSpan
67 wxTimeSpan - wxTimeSpan = wxTimeSpan
68 wxDateSpan - wxDateSpan = wxDateSpan
72 wxTimeSpan * number = wxTimeSpan
73 wxDateSpan * number = wxDateSpan
77 -wxTimeSpan = wxTimeSpan
78 -wxDateSpan = wxDateSpan
81 // ----------------------------------------------------------------------------
82 // This class represents an absolute moment in the time
83 // ----------------------------------------------------------------------------
85 class WXDLLEXPORT wxDateTime
89 // ------------------------------------------------------------------------
91 // a small unsigned integer type for storing things like seconds, days
92 // of the week, &c. It should be at least short (i.e. not char) to
93 // contain the number of milliseconds - it may also be 'int' because
94 // there is no size penalty associated with it in our code, we don't
95 // store any data in this format
96 typedef unsigned short wxDateTime_t
;
101 // the time in the current time zone
104 // zones from GMT (= Greenwhich Mean Time): they're guaranteed to be
105 // consequent numbers, so writing something like `GMT0 + offset' is
106 // safe if abs(offset) <= 12
108 // underscore stands for minus
109 GMT_12
, GMT_11
, GMT_10
, GMT_9
, GMT_8
, GMT_7
,
110 GMT_6
, GMT_5
, GMT_4
, GMT_3
, GMT_2
, GMT_1
,
112 GMT1
, GMT2
, GMT3
, GMT4
, GMT5
, GMT6
,
113 GMT7
, GMT8
, GMT9
, GMT10
, GMT11
, GMT12
,
114 // Note that GMT12 and GMT_12 are not the same: there is a difference
115 // of exactly one day between them
117 // Universal Coordinated Time
120 // TODO add symbolic names for TZ (EST, MET, ...)?
123 // the calendar systems we know about: notice that it's valid (for
124 // this classes purpose anyhow) to work with any of these calendars
125 // even with the dates before the historical appearance of the
129 Gregorian
, // current calendar
130 Julian
// calendar in use since -45 until the 1582 (or later)
132 // TODO Hebrew, Chinese, Maya, ... (just kidding) (or then may be not?)
135 // these values only are used to identify the different dates of
136 // adoption of the Gregorian calendar (see IsGregorian())
138 // All data and comments taken verbatim from "The Calendar FAQ (v 2.0)"
139 // by Claus Tøndering, http://www.pip.dknet.dk/~c-t/calendar.html
140 // except for the comments "we take".
142 // Symbol "->" should be read as "was followed by" in the comments
144 enum GregorianAdoption
146 Gr_Unknown
, // no data for this country or it's too uncertain to use
147 Gr_Standard
, // on the day 0 of Gregorian calendar: 15 Oct 1582
149 Gr_Alaska
, // Oct 1867 when Alaska became part of the USA
150 Gr_Albania
, // Dec 1912
152 Gr_Austria
= Gr_Unknown
, // Different regions on different dates
153 Gr_Austria_Brixen
, // 5 Oct 1583 -> 16 Oct 1583
154 Gr_Austria_Salzburg
= Gr_Austria_Brixen
,
155 Gr_Austria_Tyrol
= Gr_Austria_Brixen
,
156 Gr_Austria_Carinthia
, // 14 Dec 1583 -> 25 Dec 1583
157 Gr_Austria_Styria
= Gr_Austria_Carinthia
,
159 Gr_Belgium
, // Then part of the Netherlands
161 Gr_Bulgaria
= Gr_Unknown
, // Unknown precisely (from 1915 to 1920)
162 Gr_Bulgaria_1
, // 18 Mar 1916 -> 1 Apr 1916
163 Gr_Bulgaria_2
, // 31 Mar 1916 -> 14 Apr 1916
164 Gr_Bulgaria_3
, // 3 Sep 1920 -> 17 Sep 1920
166 Gr_Canada
= Gr_Unknown
, // Different regions followed the changes in
167 // Great Britain or France
169 Gr_China
= Gr_Unknown
, // Different authorities say:
170 Gr_China_1
, // 18 Dec 1911 -> 1 Jan 1912
171 Gr_China_2
, // 18 Dec 1928 -> 1 Jan 1929
173 Gr_Czechoslovakia
, // (Bohemia and Moravia) 6 Jan 1584 -> 17 Jan 1584
174 Gr_Denmark
, // (including Norway) 18 Feb 1700 -> 1 Mar 1700
177 Gr_Finland
, // Then part of Sweden
179 Gr_France
, // 9 Dec 1582 -> 20 Dec 1582
180 Gr_France_Alsace
, // 4 Feb 1682 -> 16 Feb 1682
181 Gr_France_Lorraine
, // 16 Feb 1760 -> 28 Feb 1760
182 Gr_France_Strasbourg
, // February 1682
184 Gr_Germany
= Gr_Unknown
, // Different states on different dates:
185 Gr_Germany_Catholic
, // 1583-1585 (we take 1584)
186 Gr_Germany_Prussia
, // 22 Aug 1610 -> 2 Sep 1610
187 Gr_Germany_Protestant
, // 18 Feb 1700 -> 1 Mar 1700
189 Gr_GreatBritain
, // 2 Sep 1752 -> 14 Sep 1752 (use 'cal(1)')
191 Gr_Greece
, // 9 Mar 1924 -> 23 Mar 1924
192 Gr_Hungary
, // 21 Oct 1587 -> 1 Nov 1587
193 Gr_Ireland
= Gr_GreatBritain
,
194 Gr_Italy
= Gr_Standard
,
196 Gr_Japan
= Gr_Unknown
, // Different authorities say:
197 Gr_Japan_1
, // 19 Dec 1872 -> 1 Jan 1873
198 Gr_Japan_2
, // 19 Dec 1892 -> 1 Jan 1893
199 Gr_Japan_3
, // 18 Dec 1918 -> 1 Jan 1919
201 Gr_Latvia
, // 1915-1918 (we take 1915)
202 Gr_Lithuania
, // 1915
203 Gr_Luxemburg
, // 14 Dec 1582 -> 25 Dec 1582
204 Gr_Netherlands
= Gr_Belgium
, // (including Belgium) 1 Jan 1583
206 // this is too weird to take into account: the Gregorian calendar was
207 // introduced twice in Groningen, first time 28 Feb 1583 was followed
208 // by 11 Mar 1583, then it has gone back to Julian in the summer of
209 // 1584 and then 13 Dec 1700 -> 12 Jan 1701 - which is
210 // the date we take here
211 Gr_Netherlands_Groningen
, // 13 Dec 1700 -> 12 Jan 1701
212 Gr_Netherlands_Gelderland
, // 30 Jun 1700 -> 12 Jul 1700
213 Gr_Netherlands_Utrecht
, // (and Overijssel) 30 Nov 1700->12 Dec 1700
214 Gr_Netherlands_Friesland
, // (and Drenthe) 31 Dec 1700 -> 12 Jan 1701
216 Gr_Norway
= Gr_Denmark
, // Then part of Denmark
217 Gr_Poland
= Gr_Standard
,
218 Gr_Portugal
= Gr_Standard
,
219 Gr_Romania
, // 31 Mar 1919 -> 14 Apr 1919
220 Gr_Russia
, // 31 Jan 1918 -> 14 Feb 1918
221 Gr_Scotland
= Gr_GreatBritain
,
222 Gr_Spain
= Gr_Standard
,
224 // Sweden has a curious history. Sweden decided to make a gradual
225 // change from the Julian to the Gregorian calendar. By dropping every
226 // leap year from 1700 through 1740 the eleven superfluous days would
227 // be omitted and from 1 Mar 1740 they would be in sync with the
228 // Gregorian calendar. (But in the meantime they would be in sync with
231 // So 1700 (which should have been a leap year in the Julian calendar)
232 // was not a leap year in Sweden. However, by mistake 1704 and 1708
233 // became leap years. This left Sweden out of synchronisation with
234 // both the Julian and the Gregorian world, so they decided to go back
235 // to the Julian calendar. In order to do this, they inserted an extra
236 // day in 1712, making that year a double leap year! So in 1712,
237 // February had 30 days in Sweden.
239 // Later, in 1753, Sweden changed to the Gregorian calendar by
240 // dropping 11 days like everyone else.
241 Gr_Sweden
= Gr_Finland
, // 17 Feb 1753 -> 1 Mar 1753
243 Gr_Switzerland
= Gr_Unknown
,// Different cantons used different dates
244 Gr_Switzerland_Catholic
, // 1583, 1584 or 1597 (we take 1584)
245 Gr_Switzerland_Protestant
, // 31 Dec 1700 -> 12 Jan 1701
247 Gr_Turkey
, // 1 Jan 1927
248 Gr_USA
= Gr_GreatBritain
,
249 Gr_Wales
= Gr_GreatBritain
,
250 Gr_Yugoslavia
// 1919
253 // the country parameter is used so far for calculating the start and
254 // the end of DST period and for deciding whether the date is a work
257 // TODO move this to intl.h
260 Country_Unknown
, // no special information for this country
261 Country_Default
, // set the default country with SetCountry() method
263 // TODO add more countries (for this we must know about DST and/or
264 // holidays for this country)
269 // symbolic names for the months
272 Jan
, Feb
, Mar
, Apr
, May
, Jun
, Jul
, Aug
, Sep
, Oct
, Nov
, Dec
, Inv_Month
275 // symbolic names for the weekdays
278 Sun
, Mon
, Tue
, Wed
, Thu
, Fri
, Sat
, Inv_WeekDay
281 // invalid value for the year
284 Inv_Year
= SHRT_MIN
// should hold in wxDateTime_t
288 // ------------------------------------------------------------------------
290 // standard struct tm is limited to the years from 1900 (because
291 // tm_year field is the offset from 1900), so we use our own struct
292 // instead to represent broken down time
295 wxDateTime_t sec
, min
, hour
,
298 // default ctor inits the object to an invalid value
301 // ctor from struct tm
302 Tm(const struct tm
& tm
);
304 // check that the given date/time is valid (in Gregorian calendar)
305 bool IsValid() const;
308 WeekDay
GetWeekDay() // not const because wday may be changed
310 if ( wday
== Inv_WeekDay
)
313 return (WeekDay
)wday
;
317 // compute the weekday from other fields
318 void ComputeWeekDay();
320 // these values can't be accessed directly because they're not always
321 // computed and we calculate them on demand
322 wxDateTime_t wday
, yday
;
325 // a class representing a time zone: basicly, this is just an offset
326 // (in minutes) from GMT
331 TimeZone(wxDateTime_t offset
) { m_offset
= offset
; }
333 wxDateTime_t
GetOffset() const { return m_offset
; }
336 // offset for this timezone from GMT in minutes
337 wxDateTime_t m_offset
;
341 // ------------------------------------------------------------------------
343 // set the current country
344 static void SetCountry(Country country
);
345 // get the current country
346 static inline Country
GetCountry();
348 // return the current year
349 static int GetCurrentYear(Calendar cal
= Gregorian
);
351 // convert the year as returned by wxDateTime::GetYear() to a year
352 // suitable for BC/AD notation. The difference is that BC year 1
353 // corresponds to the year 0 (while BC year 0 didn't exist) and AD
354 // year N is just year N.
355 static int ConvertYearToBC(int year
);
357 // return the current month
358 static Month
GetCurrentMonth(Calendar cal
= Gregorian
);
360 // returns TRUE if the given year is a leap year in the given calendar
361 static bool IsLeapYear(int year
= Inv_Year
, Calendar cal
= Gregorian
);
363 // get the century (19 for 1999, 20 for 2000 and -5 for 492 BC)
364 static int GetCentury(int year
= Inv_Year
);
366 // returns the number of days in this year (356 or 355 for Gregorian
367 // calendar usually :-)
368 static wxDateTime_t
GetNumberOfDays(int year
, Calendar cal
= Gregorian
);
370 // get the number of the days in the given month (default value for
371 // the year means the current one)
372 static wxDateTime_t
GetNumberOfDays(Month month
,
374 Calendar cal
= Gregorian
);
376 // get the full (default) or abbreviated month name in the current
377 // locale, returns empty string on error
378 static wxString
GetMonthName(Month month
, bool abbr
= FALSE
);
380 // get the full (default) or abbreviated weekday name in the current
381 // locale, returns empty string on error
382 static wxString
GetWeekDayName(WeekDay weekday
, bool abbr
= FALSE
);
384 // get the beginning of DST for this year, will return invalid object
385 // if no DST applicable in this year. The default value of the
386 // parameter means to take the current year.
387 static wxDateTime
GetBeginDST(int year
= Inv_Year
);
388 // get the end of DST for this year, will return invalid object
389 // if no DST applicable in this year. The default value of the
390 // parameter means to take the current year.
391 static wxDateTime
GetEndDST(int year
= Inv_Year
);
393 // return the wxDateTime object for the current time
394 static inline wxDateTime
Now();
396 // constructors: you should test whether the constructor succeeded with
397 // IsValid() function. The values Inv_Month and Inv_Year for the
398 // parameters mean take current month and/or year values.
400 // All new wxDateTime correspond to the local time, use ToUTC() or
401 // MakeUTC() to get the time in UTC/GMT.
402 // ------------------------------------------------------------------------
404 // default ctor does not initialize the object, use Set()!
407 // from time_t: seconds since the Epoch 00:00:00 UTC, Jan 1, 1970)
408 inline wxDateTime(time_t timet
);
409 // from broken down time/date (only for standard Unix range)
410 inline wxDateTime(const struct tm
& tm
);
411 // from broken down time/date (any range)
412 inline wxDateTime(const Tm
& tm
);
414 // from separate values for each component, date set to today
415 inline wxDateTime(wxDateTime_t hour
,
416 wxDateTime_t minute
= 0,
417 wxDateTime_t second
= 0,
418 wxDateTime_t millisec
= 0);
419 // from separate values for each component with explicit date
420 inline wxDateTime(wxDateTime_t day
, // day of the month
421 Month month
= Inv_Month
,
422 int year
= Inv_Year
, // 1999, not 99 please!
423 wxDateTime_t hour
= 0,
424 wxDateTime_t minute
= 0,
425 wxDateTime_t second
= 0,
426 wxDateTime_t millisec
= 0);
428 // default copy ctor ok
432 // assignment operators and Set() functions: all non const methods return
433 // the reference to this object. IsValid() should be used to test whether
434 // the function succeeded.
435 // ------------------------------------------------------------------------
437 // set to the current time
438 inline wxDateTime
& SetToCurrent();
440 // set to given time_t value
441 inline wxDateTime
& Set(time_t timet
);
443 // set to given broken down time/date
444 wxDateTime
& Set(const struct tm
& tm
);
446 // set to given broken down time/date
447 inline wxDateTime
& Set(const Tm
& tm
);
449 // set to given time, date = today
450 wxDateTime
& Set(wxDateTime_t hour
,
451 wxDateTime_t minute
= 0,
452 wxDateTime_t second
= 0,
453 wxDateTime_t millisec
= 0);
455 // from separate values for each component with explicit date
456 // (defaults for month and year are the current values)
457 wxDateTime
& Set(wxDateTime_t day
,
458 Month month
= Inv_Month
,
459 int year
= Inv_Year
, // 1999, not 99 please!
460 wxDateTime_t hour
= 0,
461 wxDateTime_t minute
= 0,
462 wxDateTime_t second
= 0,
463 wxDateTime_t millisec
= 0);
465 // resets time to 00:00:00, doesn't change the date
466 wxDateTime
& ResetTime();
468 // the following functions don't change the values of the other
469 // fields, i.e. SetMinute() won't change either hour or seconds value
472 wxDateTime
& SetYear(int year
);
474 wxDateTime
& SetMonth(Month month
);
475 // set the day of the month
476 wxDateTime
& SetDay(wxDateTime_t day
);
478 wxDateTime
& SetHour(wxDateTime_t hour
);
480 wxDateTime
& SetMinute(wxDateTime_t minute
);
482 wxDateTime
& SetSecond(wxDateTime_t second
);
484 wxDateTime
& SetMillisecond(wxDateTime_t millisecond
);
486 // assignment operator from time_t
487 wxDateTime
& operator=(time_t timet
) { return Set(timet
); }
489 // assignment operator from broken down time/date
490 wxDateTime
& operator=(const struct tm
& tm
) { return Set(tm
); }
492 // assignment operator from broken down time/date
493 wxDateTime
& operator=(const Tm
& tm
) { return Set(tm
); }
495 // default assignment operator is ok
497 // calendar calculations (functions which set the date only leave the time
498 // unchanged, e.g. don't explictly zero it)
499 // ------------------------------------------------------------------------
501 // set to Nth occurence of given weekday in the given month of the
502 // given year (time is set to 0), return TRUE on success and FALSE on
503 // failure. n may be positive (1..5) or negative to count from the end
504 // of the month (see helper function SetToLastWeekDay())
505 bool SetToWeekDay(WeekDay weekday
,
507 Month month
= Inv_Month
,
508 int year
= Inv_Year
);
510 // sets to the last weekday in the given month, year
511 inline bool SetToLastWeekDay(WeekDay weekday
,
512 Month month
= Inv_Month
,
513 int year
= Inv_Year
);
515 // sets the date to the given day of the given week in the year,
516 // returns TRUE on success and FALSE if given date doesn't exist (e.g.
518 bool SetToTheWeek(wxDateTime_t numWeek
, WeekDay weekday
= Mon
);
520 // sets the date to the last day of the given (or current) month or the
521 // given (or current) year
522 wxDateTime
& SetToLastMonthDay(Month month
= Inv_Month
,
523 int year
= Inv_Year
);
525 // The definitions below were taken verbatim from
527 // http://www.capecod.net/~pbaum/date/date0.htm
529 // (Peter Baum's home page)
531 // definition: The Julian Day Number, Julian Day, or JD of a
532 // particular instant of time is the number of days and fractions of a
533 // day since 12 hours Universal Time (Greenwich mean noon) on January
534 // 1 of the year -4712, where the year is given in the Julian
535 // proleptic calendar. The idea of using this reference date was
536 // originally proposed by Joseph Scalizer in 1582 to count years but
537 // it was modified by 19th century astronomers to count days. One
538 // could have equivalently defined the reference time to be noon of
539 // November 24, -4713 if were understood that Gregorian calendar rules
540 // were applied. Julian days are Julian Day Numbers and are not to be
541 // confused with Julian dates.
543 // definition: The Rata Die number is a date specified as the number
544 // of days relative to a base date of December 31 of the year 0. Thus
545 // January 1 of the year 1 is Rata Die day 1.
547 // get the Julian Day number
548 long GetJulianDay() const;
550 // get the Rata Die number
551 long GetRataDie() const;
553 // TODO algorithms for calculating some important dates, such as
554 // religious holidays (Easter...) or moon/solar eclipses? Some
555 // algorithms can be found in the calendar FAQ
557 // timezone stuff: by default, we always work with local times, to get
558 // anything else, it should be requested explicitly
559 // ------------------------------------------------------------------------
561 // get the time corresponding to this one in UTC/GMT
562 wxDateTime
ToUTC() const;
563 wxDateTime
ToGMT() const { return ToUTC(); }
565 // transform this object to UTC/GMT
566 wxDateTime
& MakeUTC();
567 wxDateTime
& MakeGMT() { return MakeUTC(); }
569 // generic version: transform time to any given timezone
570 wxDateTime
ToTimezone(const TimeZone
& tz
);
571 wxDateTime
& MakeTimezone(const TimeZone
& tz
);
573 // accessors: many of them take the timezone parameter which indicates the
574 // timezone for which to make the calculations and the default value means
575 // to do it for the current timezone of this machine (even if the function
576 // only operates with the date it's necessary because a date may wrap as
577 // result of timezone shift)
578 // ------------------------------------------------------------------------
580 // is the date valid (FALSE for uninitialized objects as well as after
581 // the functions which failed to convert the date to supported range)
582 inline bool IsValid() const { return this != &ms_InvDateTime
; }
584 // get the broken down date/time representation
587 // get the number of seconds since the Unix epoch - returns (time_t)-1
588 // if the value is out of range
589 inline time_t GetTicks() const;
591 // get the year (returns Inv_Year if date is invalid)
592 int GetYear() const { return GetTm().year
; }
593 // get the month (Inv_Month if date is invalid)
594 Month
GetMonth() const { return (Month
)GetTm().mon
; }
595 // get the month day (in 1..31 range, 0 if date is invalid)
596 wxDateTime_t
GetDay() const { return GetTm().mday
; }
597 // get the day of the week (Inv_WeekDay if date is invalid)
598 WeekDay
GetWeekDay() const { return GetTm().GetWeekDay(); }
599 // get the hour of the day
600 wxDateTime_t
GetHour() const { return GetTm().hour
; }
602 wxDateTime_t
GetMinute() const { return GetTm().min
; }
604 wxDateTime_t
GetSecond() const { return GetTm().sec
; }
606 wxDateTime_t
GetMillisecond() const { return m_time
.GetLo() % 1000; }
608 // get the day since the year start (1..366, 0 if date is invalid)
609 wxDateTime_t
GetDayOfYear() const;
610 // get the week number since the year start (1..52, 0 if date is
612 wxDateTime_t
GetWeekOfYear() const;
614 // is this date a work day? This depends on a country, of course,
615 // because the holidays are different in different countries
616 bool IsWorkDay(Country country
= Country_Default
,
617 TimeZone zone
= Local
) const;
619 // is this date later than Gregorian calendar introduction for the
620 // given country (see enum GregorianAdoption)?
622 // NB: this function shouldn't be considered as absolute authoiruty in
623 // the matter. Besides, for some countries the exact date of
624 // adoption of the Gregorian calendar is simply unknown.
625 bool IsGregorianDate(GregorianAdoption country
= Gr_Standard
) const;
627 // is daylight savings time in effect at this moment?
629 // Return value is > 0 if DST is in effect, 0 if it is not and -1 if
630 // the information is not available (this is compatible with ANSI C)
631 int IsDST(Country country
= Country_Default
, TimeZone zone
= Local
) const;
633 // comparison (see also functions below for operator versions)
634 // ------------------------------------------------------------------------
636 // returns TRUE if the two moments are strictly identical
637 inline bool IsEqualTo(const wxDateTime
& datetime
) const;
639 // returns TRUE if the two moments are identical
640 inline bool operator==(const wxDateTime
& datetime
) const;
642 // returns TRUE if the two moments are different
643 inline bool operator!=(const wxDateTime
& datetime
) const;
645 // returns TRUE if the date is strictly earlier than the given one
646 inline bool IsEarlierThan(const wxDateTime
& datetime
) const;
648 // returns TRUE if the date is strictly later than the given one
649 inline bool IsLaterThan(const wxDateTime
& datetime
) const;
651 // returns TRUE if the date is strictly in the given range
652 inline bool IsStrictlyBetween(const wxDateTime
& t1
,
653 const wxDateTime
& t2
) const;
655 // returns TRUE if the date is in the given range
656 inline bool IsBetween(const wxDateTime
& t1
, const wxDateTime
& t2
) const;
658 // date operations: for the non-const methods, the return value is this
659 // object itself (see also functions below for operator versions)
660 // ------------------------------------------------------------------------
662 // add a time span (positive or negative)
663 inline wxDateTime
& Add(const wxTimeSpan
& diff
);
664 // add a time span (positive or negative)
665 inline wxDateTime
& operator+=(const wxTimeSpan
& diff
);
667 // substract a time span (positive or negative)
668 inline wxDateTime
& Substract(const wxTimeSpan
& diff
);
669 // substract a time span (positive or negative)
670 inline wxDateTime
& operator-=(const wxTimeSpan
& diff
);
672 // add a date span (positive or negative)
673 wxDateTime
& Add(const wxDateSpan
& diff
);
674 // add a date span (positive or negative)
675 inline wxDateTime
& operator+=(const wxDateSpan
& diff
);
677 // substract a date span (positive or negative)
678 inline wxDateTime
& Substract(const wxDateSpan
& diff
);
679 // substract a date span (positive or negative)
680 inline wxDateTime
& operator-=(const wxDateSpan
& diff
);
682 // substract a date (may result in positive or negative time span)
683 inline wxTimeSpan
Substract(const wxDateTime
& datetime
) const;
684 // substract a date (may result in positive or negative time span)
685 inline wxTimeSpan
operator-(const wxDateTime
& datetime
) const;
687 // conversion to/from text: all conversions from text return TRUE on
688 // success or FALSE if the date is malformed/out of supported range
689 // ------------------------------------------------------------------------
691 // parse a string in RFC 822 format (found e.g. in mail headers and
692 // having the form "Wed, 10 Feb 1999 19:07:07 +0100")
693 bool ParseRfc822Date(const wxString
& date
);
694 // parse a date/time in the given format (see strptime(3))
695 bool ParseFormat(const wxString
& date
, const char *format
= "%c");
696 // parse a string containing the date/time in "free" format, this
697 // function will try to make an educated guess at the string contents
698 // (and return FALSE if it fails)
699 bool ParseDateTime(const wxString
& date
);
701 // this function accepts strftime()-like format string (default
702 // argument corresponds to the preferred date and time representation
703 // for the current locale) and returns the string containing the
704 // resulting text representation
705 wxString
Format(const wxChar
*format
= _T("%c")) const;
706 // preferred date representation for the current locale
707 wxString
FormatDate() const { return Format(_T("%x")); }
708 // preferred time representation for the current locale
709 wxString
FormatTime() const { return Format(_T("%X")); }
712 // ------------------------------------------------------------------------
714 // get the internal representation
715 inline wxLongLong
GetValue() const;
717 // a helper function to get the current time_t
718 static time_t GetTimeNow() { return time((time_t *)NULL
); }
720 // another one to get the current time broken down
721 static struct tm
*GetTmNow()
723 time_t t
= GetTimeNow();
724 return localtime(&t
);
728 // the current country - as it's the same for all program objects (unless
729 // it runs on a _really_ big cluster system :-), this is a static member:
730 // see SetCountry() and GetCountry()
731 static Country ms_country
;
733 // this constant is used to transform a time_t value to the internal
734 // representation, as time_t is in seconds and we use milliseconds it's
736 static const unsigned int TIME_T_FACTOR
;
738 // invalid wxDateTime object - returned by all functions which return
739 // "wxDateTime &" on failure
740 static wxDateTime ms_InvDateTime
;
742 // returns TRUE if we fall in range in which we can use standard ANSI C
744 inline IsInStdRange() const;
746 // the internal representation of the time is the amount of milliseconds
747 // elapsed since the origin which is set by convention to the UNIX/C epoch
748 // value: the midnight of January 1, 1970 (UTC)
752 // ----------------------------------------------------------------------------
753 // This class contains a difference between 2 wxDateTime values, so it makes
754 // sense to add it to wxDateTime and it is the result of substraction of 2
755 // objects of that class. See also wxDateSpan.
756 // ----------------------------------------------------------------------------
758 class WXDLLEXPORT wxTimeSpan
762 // ------------------------------------------------------------------------
764 // default ctor constructs the 0 time span
767 // from separate values for each component, date set to 0 (hours are
768 // not restricted to 0..24 range, neither are minutes, seconds or
770 wxTimeSpan(int hours
,
773 int milliseconds
= 0);
774 // from separate values for each component with explicit date (none of
775 // the parameters isn't restricted to any range)
776 wxTimeSpan(int years
,
782 int milliseconds
= 0);
784 // from internal representation
785 wxTimeSpan(wxLongLong diff
) : m_diff(diff
) { }
787 // default copy ctor is ok
791 // arithmetics with time spans
792 // ------------------------------------------------------------------------
794 // add two timespans together
795 inline wxTimeSpan
& Add(const wxTimeSpan
& diff
);
796 // add two timespans together
797 wxTimeSpan
& operator+=(const wxTimeSpan
& diff
) { return Add(diff
); }
799 // substract another timespan
800 inline wxTimeSpan
& Substract(const wxTimeSpan
& diff
);
801 // substract another timespan
802 wxTimeSpan
& operator-=(const wxTimeSpan
& diff
) { return Substract(diff
); }
804 // multiply timespan by a scalar
805 inline wxTimeSpan
& Multiply(int n
);
806 // multiply timespan by a scalar
807 wxTimeSpan
& operator*=(int n
) { return Multiply(n
); }
808 // multiply timespan by a scalar
809 inline wxTimeSpan
operator*(int n
) const;
811 // return this timespan with inversed sign
812 wxTimeSpan
Negate() const { return wxTimeSpan(-GetValue()); }
813 // negate the value of the timespan
814 wxTimeSpan
& Neg() { m_diff
= -GetValue(); return *this; }
815 // negate the value of the timespan
816 wxTimeSpan
& operator-() { return Neg(); }
818 // return the absolute value of the timespan: does _not_ modify the
820 inline wxTimeSpan
Abs() const;
822 // there is intentionally no division because we don't want to
823 // introduce rounding errors in time calculations
825 // comparaison (see also operator versions below)
826 // ------------------------------------------------------------------------
828 // is the timespan null?
829 bool IsNull() const { return m_diff
== 0l; }
830 // returns true if the timespan is null
831 bool operator!() const { return !IsNull(); }
833 // is the timespan positive?
834 bool IsPositive() const { return m_diff
> 0l; }
836 // is the timespan negative?
837 bool IsNegative() const { return m_diff
< 0l; }
839 // are two timespans equal?
840 inline bool IsEqualTo(const wxTimeSpan
& ts
) const;
841 // compare two timestamps: works with the absolute values, i.e. -2
842 // hours is longer than 1 hour. Also, it will return FALSE if the
843 // timespans are equal in absolute value.
844 inline bool IsLongerThan(const wxTimeSpan
& ts
) const;
845 // compare two timestamps: works with the absolute values, i.e. 1
846 // hour is shorter than -2 hours. Also, it will return FALSE if the
847 // timespans are equal in absolute value.
848 bool IsShorterThan(const wxTimeSpan
& t
) const { return !IsLongerThan(t
); }
850 // breaking into years, ..., days, ..., seconds: all these functions
851 // behave like GetYears() which returns 1 for the timespan of 1 year and 1
852 // day, but 0 (and not -1) for the negative timespan of 1 year without 1
853 // day. IOW, (ts - wxTimeSpan(ts.GetYears())).GetYears() is always 0.
854 // ------------------------------------------------------------------------
856 // get the max number of years in this timespan
857 inline int GetYears() const;
858 // get the max number of months in this timespan
859 inline int GetMonths() const;
860 // get the max number of weeks in this timespan
861 inline int GetWeeks() const;
862 // get the max number of days in this timespan
863 inline int GetDays() const;
864 // get the max number of hours in this timespan
865 inline int GetHours() const;
866 // get the max number of minutes in this timespan
867 inline int GetMinutes() const;
868 // get the max number of seconds in this timespan
869 inline int GetSeconds() const;
870 // get the number of milliseconds in this timespan
871 wxLongLong
GetMilliseconds() const { return m_diff
; }
873 // conversion to text
874 // ------------------------------------------------------------------------
876 // this function accepts strftime()-like format string (default
877 // argument corresponds to the preferred date and time representation
878 // for the current locale) and returns the string containing the
879 // resulting text representation. Notice that only some of format
880 // specifiers valid for wxDateTime are valid for wxTimeSpan: hours,
881 // minutes and seconds make sense, but not "PM/AM" string for example.
882 wxString
Format(const char *format
= "%c") const;
883 // preferred date representation for the current locale
884 wxString
FormatDate() const { return Format("%x"); }
885 // preferred time representation for the current locale
886 wxString
FormatTime() const { return Format("%X"); }
889 // ------------------------------------------------------------------------
891 // get the internal representation
892 wxLongLong
GetValue() const { return m_diff
; }
895 // the (signed) time span in milliseconds
899 // ----------------------------------------------------------------------------
900 // This class is a "logical time span" and is useful for implementing program
901 // logic for such things as "add one month to the date" which, in general,
902 // doesn't mean to add 60*60*24*31 seconds to it, but to take the same date
903 // the next month (to understand that this is indeed different consider adding
904 // one month to Feb, 15 - we want to get Mar, 15, of course).
906 // When adding a month to the date, all lesser components (days, hours, ...)
909 // wxDateSpan can be either positive or negative. They may be
910 // multiplied by scalars which multiply all deltas by the scalar: i.e. 2*(1
911 // month and 1 day) is 2 months and 2 days. They can be added together and
912 // with wxDateTime or wxTimeSpan, but the type of result is different for each
915 // Beware about weeks: if you specify both weeks and days, the total number of
916 // days added will be 7*weeks + days! See also GetTotalDays() function.
918 // Finally, notice that for adding hours, minutes &c you don't need this
919 // class: wxTimeSpan will do the job because there are no subtleties
920 // associated with those.
921 // ----------------------------------------------------------------------------
923 class WXDLLEXPORT wxDateSpan
927 // ------------------------------------------------------------------------
929 // this many years/months/weeks/days
930 wxDateSpan(int years
= 0, int months
= 0, int weeks
= 0, int days
= 0)
938 // get an object for the given number of days
939 static wxDateSpan
Days(int days
) { return wxDateSpan(0, 0, 0, days
); }
941 // get an object for the given number of weeks
942 static wxDateSpan
Weeks(int weeks
) { return wxDateSpan(0, 0, weeks
, 0); }
944 // get an object for the given number of months
945 static wxDateSpan
Months(int mon
) { return wxDateSpan(0, mon
, 0, 0); }
947 // get an object for the given number of years
948 static wxDateSpan
Years(int years
) { return wxDateSpan(years
, 0, 0, 0); }
950 // default copy ctor is ok
954 // accessors (all SetXXX() return the (modified) wxDateSpan object)
955 // ------------------------------------------------------------------------
957 // set number of years
958 wxDateSpan
& SetYears(int n
) { m_years
= n
; return *this; }
959 // set number of months
960 wxDateSpan
& SetMonths(int n
) { m_months
= n
; return *this; }
961 // set number of weeks
962 wxDateSpan
& SetWeeks(int n
) { m_weeks
= n
; return *this; }
963 // set number of days
964 wxDateSpan
& SetDays(int n
) { m_days
= n
; return *this; }
966 // get number of years
967 int GetYears() const { return m_years
; }
968 // get number of months
969 int GetMonths() const { return m_months
; }
970 // get number of weeks
971 int GetWeeks() const { return m_weeks
; }
972 // get number of days
973 int GetDays() const { return m_days
; }
974 // returns 7*GetWeeks() + GetDays()
975 int GetTotalDays() const { return 7*m_weeks
+ m_days
; }
978 // ------------------------------------------------------------------------
980 // add another wxDateSpan to us
981 inline wxDateSpan
& Add(const wxDateSpan
& other
);
982 // add another wxDateSpan to us
983 inline wxDateSpan
& operator+=(const wxDateSpan
& other
);
985 // substract another wxDateSpan from us
986 inline wxDateSpan
& Substract(const wxDateSpan
& other
);
987 // substract another wxDateSpan from us
988 inline wxDateSpan
& operator-=(const wxDateSpan
& other
);
990 // return a copy of this time span with changed sign
991 inline wxDateSpan
Negate() const;
992 // inverse the sign of this timespan
993 inline wxDateSpan
& Neg();
994 // inverse the sign of this timespan
995 wxDateSpan
& operator-() { return Neg(); }
997 // multiply all components by a (signed) number
998 inline wxDateSpan
& operator*=(int factor
);
1007 WXDLLEXPORT_DATA(extern wxDateSpan
) wxYear
;
1008 WXDLLEXPORT_DATA(extern wxDateSpan
) wxMonth
;
1009 WXDLLEXPORT_DATA(extern wxDateSpan
) wxWeek
;
1010 WXDLLEXPORT_DATA(extern wxDateSpan
) wxDay
;
1012 // ============================================================================
1013 // inline functions implementation
1014 // ============================================================================
1016 // don't include inline functions definitions when we're included from anything
1017 // else than datetime.cpp in debug builds: this minimizes rebuilds if we change
1018 // some inline function and the performance doesn't matter in the debug builds.
1020 #if !defined(__WXDEBUG__) || defined(wxDEFINE_TIME_CONSTANTS)
1021 #define INCLUDED_FROM_WX_DATETIME_H
1022 #include "wx/datetime.inl"
1023 #undef INCLUDED_FROM_WX_DATETIME_H
1026 // if we defined it to be empty above, restore it now
1029 // ============================================================================
1031 // ============================================================================
1033 // ----------------------------------------------------------------------------
1034 // wxDateTime operators
1035 // ----------------------------------------------------------------------------
1037 inline bool WXDLLEXPORT
operator<(const wxDateTime
&t1
, const wxDateTime
&t2
)
1039 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1041 return t1
.GetValue() < t2
.GetValue();
1044 inline bool WXDLLEXPORT
operator<=(const wxDateTime
&t1
, const wxDateTime
&t2
)
1046 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1048 return t1
.GetValue() <= t2
.GetValue();
1051 inline bool WXDLLEXPORT
operator>(const wxDateTime
&t1
, const wxDateTime
&t2
)
1053 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1055 return t1
.GetValue() > t2
.GetValue();
1058 inline bool WXDLLEXPORT
operator>=(const wxDateTime
&t1
, const wxDateTime
&t2
)
1060 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1062 return t1
.GetValue() >= t2
.GetValue();
1065 inline bool WXDLLEXPORT
operator==(const wxDateTime
&t1
, const wxDateTime
&t2
)
1067 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1069 return t1
.GetValue() == t2
.GetValue();
1072 inline bool WXDLLEXPORT
operator!=(const wxDateTime
&t1
, const wxDateTime
&t2
)
1074 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1076 return t1
.GetValue() != t2
.GetValue();
1079 inline wxTimeSpan WXDLLEXPORT
operator-(const wxDateTime
&t1
,
1080 const wxDateTime
&t2
)
1082 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1084 return wxTimeSpan(t1
.GetValue() - t2
.GetValue());
1087 // ----------------------------------------------------------------------------
1088 // wxTimeSpan operators
1089 // ----------------------------------------------------------------------------
1091 inline wxTimeSpan WXDLLEXPORT
operator+(const wxTimeSpan
& ts1
,
1092 const wxTimeSpan
& ts2
)
1094 return wxTimeSpan(ts1
.GetValue() + ts2
.GetValue());
1097 inline wxTimeSpan WXDLLEXPORT
operator-(const wxTimeSpan
& ts1
,
1098 const wxTimeSpan
& ts2
)
1100 return wxTimeSpan(ts1
.GetValue() - ts2
.GetValue());
1103 inline bool WXDLLEXPORT
operator<(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1105 return t1
.GetValue() < t2
.GetValue();
1108 inline bool WXDLLEXPORT
operator<=(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1110 return t1
.GetValue() <= t2
.GetValue();
1113 inline bool WXDLLEXPORT
operator>(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1115 return t1
.GetValue() > t2
.GetValue();
1118 inline bool WXDLLEXPORT
operator>=(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1120 return t1
.GetValue() >= t2
.GetValue();
1123 inline bool WXDLLEXPORT
operator==(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1125 return t1
.GetValue() == t2
.GetValue();
1128 inline bool WXDLLEXPORT
operator!=(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1130 return t1
.GetValue() != t2
.GetValue();
1133 // ----------------------------------------------------------------------------
1135 // ----------------------------------------------------------------------------
1137 inline WXDLLEXPORT wxDateSpan
operator+(const wxDateSpan
& rt1
,
1138 const wxDateSpan
& rt2
)
1140 return wxDateSpan(rt1
.GetYears() + rt2
.GetYears(),
1141 rt1
.GetMonths() + rt2
.GetMonths(),
1142 rt1
.GetWeeks() + rt2
.GetWeeks(),
1143 rt1
.GetDays() + rt2
.GetDays());
1146 #endif // _WX_DATETIME_H