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 /////////////////////////////////////////////////////////////////////////////
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
;
30 * TODO Well, everything :-)
32 * 1. Time zones with minutes (make wxTimeZone a class)
33 * 2. getdate() function like under Solaris
34 * 3. text conversion for wxDateSpan
38 The three classes declared in this header represent:
40 1. An absolute moment in the time (wxDateTime)
41 2. A difference between two moments in the time, positive or negative
43 3. A logical difference between two dates expressed in
44 years/months/weeks/days (wxDateSpan)
46 The following arithmetic operations are permitted (all others are not):
51 wxDateTime + wxTimeSpan = wxDateTime
52 wxDateTime + wxDateSpan = wxDateTime
53 wxTimeSpan + wxTimeSpan = wxTimeSpan
54 wxDateSpan + wxDateSpan = wxDateSpan
58 wxDateTime - wxDateTime = wxTimeSpan
59 wxTimeSpan - wxTimeSpan = wxTimeSpan
60 wxDateSpan - wxDateSpan = wxDateSpan
64 wxTimeSpan * number = wxTimeSpan
65 wxDateSpan * number = wxDateSpan
69 -wxTimeSpan = wxTimeSpan
70 -wxDateSpan = wxDateSpan
73 // ----------------------------------------------------------------------------
74 // This class represents an absolute moment in the time
75 // ----------------------------------------------------------------------------
77 class WXDLLEXPORT wxDateTime
81 // ------------------------------------------------------------------------
83 // a small unsigned integer type for storing things like seconds, days
84 // of the week, &c. It should be at least short (i.e. not char) to
85 // contain the number of milliseconds - it may also be 'int' because
86 // there is no size penalty associated with it in our code, we don't
87 // store any data in this format
88 typedef unsigned short wxDateTime_t
;
93 // the time in the current time zone
96 // zones from GMT (= Greenwhich Mean Time): they're guaranteed to be
97 // consequent numbers, so writing something like `GMT0 + offset' is
98 // safe if abs(offset) <= 12
100 // underscore stands for minus
101 GMT_12
, GMT_11
, GMT_10
, GMT_9
, GMT_8
, GMT_7
,
102 GMT_6
, GMT_5
, GMT_4
, GMT_3
, GMT_2
, GMT_1
,
104 GMT1
, GMT2
, GMT3
, GMT4
, GMT5
, GMT6
,
105 GMT7
, GMT8
, GMT9
, GMT10
, GMT11
, GMT12
,
106 // Note that GMT12 and GMT_12 are not the same: there is a difference
107 // of exactly one day between them
109 // Universal Coordinated Time
112 // TODO add symbolic names for TZ (EST, MET, ...)?
115 // the calendar systems we know about: notice that it's valid (for
116 // this classes purpose anyhow) to work with any of these calendars
117 // even with the dates before the historical appearance of the
121 Gregorian
, // current calendar
122 Julian
// calendar in use since -45 until the 1582 (or later)
124 // TODO Hebrew, Chinese, Maya, ... (just kidding) (or then may be not?)
127 // these values only are used to identify the different dates of
128 // adoption of the Gregorian calendar (see IsGregorian())
130 // All data and comments taken verbatim from "The Calendar FAQ (v 2.0)"
131 // by Claus Tøndering, http://www.pip.dknet.dk/~c-t/calendar.html
132 // except for the comments "we take".
134 // Symbol "->" should be read as "was followed by" in the comments
136 enum GregorianAdoption
138 Gr_Unknown
, // no data for this country or it's too uncertain to use
139 Gr_Standard
, // on the day 0 of Gregorian calendar: 15 Oct 1582
141 Gr_Alaska
, // Oct 1867 when Alaska became part of the USA
142 Gr_Albania
, // Dec 1912
144 Gr_Austria
= Gr_Unknown
, // Different regions on different dates
145 Gr_Austria_Brixen
, // 5 Oct 1583 -> 16 Oct 1583
146 Gr_Austria_Salzburg
= Gr_Austria_Brixen
,
147 Gr_Austria_Tyrol
= Gr_Austria_Brixen
,
148 Gr_Austria_Carinthia
, // 14 Dec 1583 -> 25 Dec 1583
149 Gr_Austria_Styria
= Gr_Austria_Carinthia
,
151 Gr_Belgium
, // Then part of the Netherlands
153 Gr_Bulgaria
= Gr_Unknown
, // Unknown precisely (from 1915 to 1920)
154 Gr_Bulgaria_1
, // 18 Mar 1916 -> 1 Apr 1916
155 Gr_Bulgaria_2
, // 31 Mar 1916 -> 14 Apr 1916
156 Gr_Bulgaria_3
, // 3 Sep 1920 -> 17 Sep 1920
158 Gr_Canada
= Gr_Unknown
, // Different regions followed the changes in
159 // Great Britain or France
161 Gr_China
= Gr_Unknown
, // Different authorities say:
162 Gr_China_1
, // 18 Dec 1911 -> 1 Jan 1912
163 Gr_China_2
, // 18 Dec 1928 -> 1 Jan 1929
165 Gr_Czechoslovakia
, // (Bohemia and Moravia) 6 Jan 1584 -> 17 Jan 1584
166 Gr_Denmark
, // (including Norway) 18 Feb 1700 -> 1 Mar 1700
169 Gr_Finland
, // Then part of Sweden
171 Gr_France
, // 9 Dec 1582 -> 20 Dec 1582
172 Gr_France_Alsace
, // 4 Feb 1682 -> 16 Feb 1682
173 Gr_France_Lorraine
, // 16 Feb 1760 -> 28 Feb 1760
174 Gr_France_Strasbourg
, // February 1682
176 Gr_Germany
= Gr_Unknown
, // Different states on different dates:
177 Gr_Germany_Catholic
, // 1583-1585 (we take 1584)
178 Gr_Germany_Prussia
, // 22 Aug 1610 -> 2 Sep 1610
179 Gr_Germany_Protestant
, // 18 Feb 1700 -> 1 Mar 1700
181 Gr_GreatBritain
, // 2 Sep 1752 -> 14 Sep 1752 (use 'cal(1)')
183 Gr_Greece
, // 9 Mar 1924 -> 23 Mar 1924
184 Gr_Hungary
, // 21 Oct 1587 -> 1 Nov 1587
185 Gr_Ireland
= Gr_GreatBritain
,
186 Gr_Italy
= Gr_Standard
,
188 Gr_Japan
= Gr_Unknown
, // Different authorities say:
189 Gr_Japan_1
, // 19 Dec 1872 -> 1 Jan 1873
190 Gr_Japan_2
, // 19 Dec 1892 -> 1 Jan 1893
191 Gr_Japan_3
, // 18 Dec 1918 -> 1 Jan 1919
193 Gr_Latvia
, // 1915-1918 (we take 1915)
194 Gr_Lithuania
, // 1915
195 Gr_Luxemburg
, // 14 Dec 1582 -> 25 Dec 1582
196 Gr_Netherlands
= Gr_Belgium
, // (including Belgium) 1 Jan 1583
198 // this is too weird to take into account: the Gregorian calendar was
199 // introduced twice in Groningen, first time 28 Feb 1583 was followed
200 // by 11 Mar 1583, then it has gone back to Julian in the summer of
201 // 1584 and then 13 Dec 1700 -> 12 Jan 1701 - which is
202 // the date we take here
203 Gr_Netherlands_Groningen
, // 13 Dec 1700 -> 12 Jan 1701
204 Gr_Netherlands_Gelderland
, // 30 Jun 1700 -> 12 Jul 1700
205 Gr_Netherlands_Utrecht
, // (and Overijssel) 30 Nov 1700->12 Dec 1700
206 Gr_Netherlands_Friesland
, // (and Drenthe) 31 Dec 1700 -> 12 Jan 1701
208 Gr_Norway
= Gr_Denmark
, // Then part of Denmark
209 Gr_Poland
= Gr_Standard
,
210 Gr_Portugal
= Gr_Standard
,
211 Gr_Romania
, // 31 Mar 1919 -> 14 Apr 1919
212 Gr_Russia
, // 31 Jan 1918 -> 14 Feb 1918
213 Gr_Scotland
= Gr_GreatBritain
,
214 Gr_Spain
= Gr_Standard
,
216 // Sweden has a curious history. Sweden decided to make a gradual
217 // change from the Julian to the Gregorian calendar. By dropping every
218 // leap year from 1700 through 1740 the eleven superfluous days would
219 // be omitted and from 1 Mar 1740 they would be in sync with the
220 // Gregorian calendar. (But in the meantime they would be in sync with
223 // So 1700 (which should have been a leap year in the Julian calendar)
224 // was not a leap year in Sweden. However, by mistake 1704 and 1708
225 // became leap years. This left Sweden out of synchronisation with
226 // both the Julian and the Gregorian world, so they decided to go back
227 // to the Julian calendar. In order to do this, they inserted an extra
228 // day in 1712, making that year a double leap year! So in 1712,
229 // February had 30 days in Sweden.
231 // Later, in 1753, Sweden changed to the Gregorian calendar by
232 // dropping 11 days like everyone else.
233 Gr_Sweden
= Gr_Finland
, // 17 Feb 1753 -> 1 Mar 1753
235 Gr_Switzerland
= Gr_Unknown
,// Different cantons used different dates
236 Gr_Switzerland_Catholic
, // 1583, 1584 or 1597 (we take 1584)
237 Gr_Switzerland_Protestant
, // 31 Dec 1700 -> 12 Jan 1701
239 Gr_Turkey
, // 1 Jan 1927
240 Gr_USA
= Gr_GreatBritain
,
241 Gr_Wales
= Gr_GreatBritain
,
242 Gr_Yugoslavia
// 1919
245 // the country parameter is used so far for calculating the start and
246 // the end of DST period and for deciding whether the date is a work
249 // TODO move this to intl.h
252 Country_Unknown
, // no special information for this country
253 Country_Default
, // set the default country with SetCountry() method
255 // TODO add more countries (for this we must know about DST and/or
256 // holidays for this country)
261 // symbolic names for the months
264 Jan
, Feb
, Mar
, Apr
, May
, Jun
, Jul
, Aug
, Sep
, Oct
, Nov
, Dec
, Inv_Month
267 // symbolic names for the weekdays
270 Sun
, Mon
, Tue
, Wed
, Thu
, Fri
, Sat
, Inv_WeekDay
273 // invalid value for the year
276 Inv_Year
= SHRT_MIN
// should hold in wxDateTime_t
280 // ------------------------------------------------------------------------
282 // standard struct tm is limited to the years from 1900 (because
283 // tm_year field is the offset from 1900), so we use our own struct
284 // instead to represent broken down time
287 wxDateTime_t sec
, min
, hour
,
290 // default ctor inits the object to an invalid value
293 // ctor from struct tm
294 Tm(const struct tm
& tm
);
296 // check that the given date/time is valid (in Gregorian calendar)
297 bool IsValid() const;
300 WeekDay
GetWeekDay() // not const because wday may be changed
302 if ( wday
== Inv_WeekDay
)
305 return (WeekDay
)wday
;
309 // compute the weekday from other fields
310 void ComputeWeekDay();
312 // these values can't be accessed directly because they're not always
313 // computed and we calculate them on demand
314 wxDateTime_t wday
, yday
;
317 // a class representing a time zone: basicly, this is just an offset
318 // (in minutes) from GMT
323 TimeZone(wxDateTime_t offset
) { m_offset
= offset
; }
325 wxDateTime_t
GetOffset() const { return m_offset
; }
328 // offset for this timezone from GMT in minutes
329 wxDateTime_t m_offset
;
333 // ------------------------------------------------------------------------
335 // set the current country
336 static void SetCountry(Country country
);
337 // get the current country
338 static inline Country
GetCountry();
340 // return the current year
341 static int GetCurrentYear(Calendar cal
= Gregorian
);
343 // convert the year as returned by wxDateTime::GetYear() to a year
344 // suitable for BC/AD notation. The difference is that BC year 1
345 // corresponds to the year 0 (while BC year 0 didn't exist) and AD
346 // year N is just year N.
347 static int ConvertYearToBC(int year
);
349 // return the current month
350 static Month
GetCurrentMonth(Calendar cal
= Gregorian
);
352 // returns TRUE if the given year is a leap year in the given calendar
353 static bool IsLeapYear(int year
, Calendar cal
= Gregorian
);
355 // returns the number of days in this year (356 or 355 for Gregorian
356 // calendar usually :-)
357 static wxDateTime_t
GetNumberOfDays(int year
, Calendar cal
= Gregorian
);
359 // get the number of the days in the given month (default value for
360 // the year means the current one)
361 static wxDateTime_t
GetNumberOfDays(Month month
,
363 Calendar cal
= Gregorian
);
365 // get the full (default) or abbreviated month name in the current
366 // locale, returns empty string on error
367 static wxString
GetMonthName(Month month
, bool abbr
= FALSE
);
369 // get the full (default) or abbreviated weekday name in the current
370 // locale, returns empty string on error
371 static wxString
GetWeekDayName(WeekDay weekday
, bool abbr
= FALSE
);
373 // get the beginning of DST for this year, will return invalid object
374 // if no DST applicable in this year. The default value of the
375 // parameter means to take the current year.
376 static wxDateTime
GetBeginDST(int year
= Inv_Year
);
377 // get the end of DST for this year, will return invalid object
378 // if no DST applicable in this year. The default value of the
379 // parameter means to take the current year.
380 static wxDateTime
GetEndDST(int year
= Inv_Year
);
382 // return the wxDateTime object for the current time
383 static inline wxDateTime
Now();
385 // constructors: you should test whether the constructor succeeded with
386 // IsValid() function. The values Inv_Month and Inv_Year for the
387 // parameters mean take current month and/or year values.
389 // All new wxDateTime correspond to the local time, use ToUTC() or
390 // MakeUTC() to get the time in UTC/GMT.
391 // ------------------------------------------------------------------------
393 // default ctor does not initialize the object, use Set()!
396 // from time_t: seconds since the Epoch 00:00:00 UTC, Jan 1, 1970)
397 inline wxDateTime(time_t timet
);
398 // from broken down time/date (only for standard Unix range)
399 inline wxDateTime(const struct tm
& tm
);
400 // from broken down time/date (any range)
401 inline wxDateTime(const Tm
& tm
);
403 // from separate values for each component, date set to today
404 inline wxDateTime(wxDateTime_t hour
,
405 wxDateTime_t minute
= 0,
406 wxDateTime_t second
= 0,
407 wxDateTime_t millisec
= 0);
408 // from separate values for each component with explicit date
409 inline wxDateTime(wxDateTime_t day
, // day of the month
410 Month month
= Inv_Month
,
411 int year
= Inv_Year
, // 1999, not 99 please!
412 wxDateTime_t hour
= 0,
413 wxDateTime_t minute
= 0,
414 wxDateTime_t second
= 0,
415 wxDateTime_t millisec
= 0);
417 // default copy ctor ok
421 // assignment operators and Set() functions: all non const methods return
422 // the reference to this object. IsValid() should be used to test whether
423 // the function succeeded.
424 // ------------------------------------------------------------------------
426 // set to the current time
427 inline wxDateTime
& SetToCurrent();
429 // set to given time_t value
430 inline wxDateTime
& Set(time_t timet
);
432 // set to given broken down time/date
433 wxDateTime
& Set(const struct tm
& tm
);
435 // set to given broken down time/date
436 inline wxDateTime
& Set(const Tm
& tm
);
438 // set to given time, date = today
439 wxDateTime
& Set(wxDateTime_t hour
,
440 wxDateTime_t minute
= 0,
441 wxDateTime_t second
= 0,
442 wxDateTime_t millisec
= 0);
444 // from separate values for each component with explicit date
445 // (defaults for month and year are the current values)
446 wxDateTime
& Set(wxDateTime_t day
,
447 Month month
= Inv_Month
,
448 int year
= Inv_Year
, // 1999, not 99 please!
449 wxDateTime_t hour
= 0,
450 wxDateTime_t minute
= 0,
451 wxDateTime_t second
= 0,
452 wxDateTime_t millisec
= 0);
454 // resets time to 00:00:00, doesn't change the date
455 wxDateTime
& ResetTime();
457 // the following functions don't change the values of the other
458 // fields, i.e. SetMinute() won't change either hour or seconds value
461 wxDateTime
& SetYear(int year
);
463 wxDateTime
& SetMonth(Month month
);
464 // set the day of the month
465 wxDateTime
& SetDay(wxDateTime_t day
);
467 wxDateTime
& SetHour(wxDateTime_t hour
);
469 wxDateTime
& SetMinute(wxDateTime_t minute
);
471 wxDateTime
& SetSecond(wxDateTime_t second
);
473 wxDateTime
& SetMillisecond(wxDateTime_t millisecond
);
475 // assignment operator from time_t
476 wxDateTime
& operator=(time_t timet
) { return Set(timet
); }
478 // assignment operator from broken down time/date
479 wxDateTime
& operator=(const struct tm
& tm
) { return Set(tm
); }
481 // assignment operator from broken down time/date
482 wxDateTime
& operator=(const Tm
& tm
) { return Set(tm
); }
484 // default assignment operator is ok
486 // calendar calculations (functions which set the date only leave the time
487 // unchanged, e.g. don't explictly zero it)
488 // ------------------------------------------------------------------------
490 // set to Nth occurence of given weekday in the given month of the
491 // given year (time is set to 0), return TRUE on success and FALSE on
492 // failure. n may be positive (1..5) or negative to count from the end
493 // of the month (see helper function SetToLastWeekDay())
494 bool SetToWeekDay(WeekDay weekday
,
496 wxDateTime_t month
= Inv_Month
,
497 int year
= Inv_Year
);
499 // sets to the last weekday in the given month, year
500 inline bool SetToLastWeekDay(WeekDay weekday
,
501 wxDateTime_t month
= Inv_Month
,
502 int year
= Inv_Year
);
504 // sets the date to the given day of the given week in the year,
505 // returns TRUE on success and FALSE if given date doesn't exist (e.g.
507 bool SetToTheWeek(wxDateTime_t numWeek
, WeekDay weekday
= Mon
);
509 // get the century (19 for 1999, 20 for 2000 and -5 for 492 BC)
510 int GetCentury() const;
512 // The definitions below were taken verbatim from
514 // http://www.capecod.net/~pbaum/date/date0.htm
516 // (Peter Baum's home page)
518 // definition: The Julian Day Number, Julian Day, or JD of a
519 // particular instant of time is the number of days and fractions of a
520 // day since 12 hours Universal Time (Greenwich mean noon) on January
521 // 1 of the year -4712, where the year is given in the Julian
522 // proleptic calendar. The idea of using this reference date was
523 // originally proposed by Joseph Scalizer in 1582 to count years but
524 // it was modified by 19th century astronomers to count days. One
525 // could have equivalently defined the reference time to be noon of
526 // November 24, -4713 if were understood that Gregorian calendar rules
527 // were applied. Julian days are Julian Day Numbers and are not to be
528 // confused with Julian dates.
530 // definition: The Rata Die number is a date specified as the number
531 // of days relative to a base date of December 31 of the year 0. Thus
532 // January 1 of the year 1 is Rata Die day 1.
534 // get the Julian Day number
535 long GetJulianDay() const;
537 // get the Rata Die number
538 long GetRataDie() const;
540 // TODO algorithms for calculating some important dates, such as
541 // religious holidays (Easter...) or moon/solar eclipses? Some
542 // algorithms can be found in the calendar FAQ
544 // timezone stuff: by default, we always work with local times, to get
545 // anything else, it should be requested explicitly
546 // ------------------------------------------------------------------------
548 // get the time corresponding to this one in UTC/GMT
549 wxDateTime
ToUTC() const;
550 wxDateTime
ToGMT() const { return ToUTC(); }
552 // transform this object to UTC/GMT
553 wxDateTime
& MakeUTC();
554 wxDateTime
& MakeGMT() { return MakeUTC(); }
556 // generic version: transform time to any given timezone
557 wxDateTime
ToTimezone(const TimeZone
& tz
);
558 wxDateTime
& MakeTimezone(const TimeZone
& tz
);
560 // accessors: many of them take the timezone parameter which indicates the
561 // timezone for which to make the calculations and the default value means
562 // to do it for the current timezone of this machine (even if the function
563 // only operates with the date it's necessary because a date may wrap as
564 // result of timezone shift)
565 // ------------------------------------------------------------------------
567 // is the date valid (FALSE for uninitialized objects as well as after
568 // the functions which failed to convert the date to supported range)
569 inline bool IsValid() const { return this != &ms_InvDateTime
; }
571 // get the broken down date/time representation
574 // get the number of seconds since the Unix epoch - returns (time_t)-1
575 // if the value is out of range
576 inline time_t GetTicks() const;
578 // get the year (returns Inv_Year if date is invalid)
579 int GetYear() const { return GetTm().year
; }
580 // get the month (Inv_Month if date is invalid)
581 Month
GetMonth() const { return (Month
)GetTm().mon
; }
582 // get the month day (in 1..31 range, 0 if date is invalid)
583 wxDateTime_t
GetDay() const { return GetTm().mday
; }
584 // get the day of the week (Inv_WeekDay if date is invalid)
585 WeekDay
GetDayOfWeek() const { return GetTm().GetWeekDay(); }
586 // get the hour of the day
587 wxDateTime_t
GetHour() const { return GetTm().hour
; }
589 wxDateTime_t
GetMinute() const { return GetTm().min
; }
591 wxDateTime_t
GetSecond() const { return GetTm().sec
; }
593 wxDateTime_t
GetMillisecond() const { return m_time
.GetLo() % 1000; }
595 // get the day since the year start (1..366, 0 if date is invalid)
596 wxDateTime_t
GetDayOfYear() const;
597 // get the week number since the year start (1..52, 0 if date is
599 wxDateTime_t
GetWeekOfYear() const;
601 // is this date a work day? This depends on a country, of course,
602 // because the holidays are different in different countries
603 bool IsWorkDay(Country country
= Country_Default
,
604 TimeZone zone
= Local
) const;
606 // is this date later than Gregorian calendar introduction for the
607 // given country (see enum GregorianAdoption)?
609 // NB: this function shouldn't be considered as absolute authoiruty in
610 // the matter. Besides, for some countries the exact date of
611 // adoption of the Gregorian calendar is simply unknown.
612 bool IsGregorianDate(GregorianAdoption country
= Gr_Standard
) const;
614 // is daylight savings time in effect at this moment?
616 // Return value is > 0 if DST is in effect, 0 if it is not and -1 if
617 // the information is not available (this is compatible with ANSI C)
618 int IsDST(Country country
= Country_Default
, TimeZone zone
= Local
) const;
620 // comparison (see also functions below for operator versions)
621 // ------------------------------------------------------------------------
623 // returns TRUE if the two moments are strictly identical
624 inline bool IsEqualTo(const wxDateTime
& datetime
) const;
626 // returns TRUE if the two moments are identical
627 inline bool operator==(const wxDateTime
& datetime
) const;
629 // returns TRUE if the two moments are different
630 inline bool operator!=(const wxDateTime
& datetime
) const;
632 // returns TRUE if the date is strictly earlier than the given one
633 inline bool IsEarlierThan(const wxDateTime
& datetime
) const;
635 // returns TRUE if the date is strictly later than the given one
636 inline bool IsLaterThan(const wxDateTime
& datetime
) const;
638 // returns TRUE if the date is strictly in the given range
639 inline bool IsStrictlyBetween(const wxDateTime
& t1
,
640 const wxDateTime
& t2
) const;
642 // returns TRUE if the date is in the given range
643 inline bool IsBetween(const wxDateTime
& t1
, const wxDateTime
& t2
) const;
645 // date operations: for the non-const methods, the return value is this
646 // object itself (see also functions below for operator versions)
647 // ------------------------------------------------------------------------
649 // add a time span (positive or negative)
650 inline wxDateTime
& Add(const wxTimeSpan
& diff
);
651 // add a time span (positive or negative)
652 inline wxDateTime
& operator+=(const wxTimeSpan
& diff
);
654 // substract a time span (positive or negative)
655 inline wxDateTime
& Substract(const wxTimeSpan
& diff
);
656 // substract a time span (positive or negative)
657 inline wxDateTime
& operator-=(const wxTimeSpan
& diff
);
659 // add a date span (positive or negative)
660 wxDateTime
& Add(const wxDateSpan
& diff
);
661 // add a date span (positive or negative)
662 inline wxDateTime
& operator+=(const wxDateSpan
& diff
);
664 // substract a date span (positive or negative)
665 inline wxDateTime
& Substract(const wxDateSpan
& diff
);
666 // substract a date span (positive or negative)
667 inline wxDateTime
& operator-=(const wxDateSpan
& diff
);
669 // substract a date (may result in positive or negative time span)
670 inline wxTimeSpan
Substract(const wxDateTime
& datetime
) const;
671 // substract a date (may result in positive or negative time span)
672 inline wxTimeSpan
operator-(const wxDateTime
& datetime
) const;
674 // conversion to/from text: all conversions from text return TRUE on
675 // success or FALSE if the date is malformed/out of supported range
676 // ------------------------------------------------------------------------
678 // parse a string in RFC 822 format (found e.g. in mail headers and
679 // having the form "Wed, 10 Feb 1999 19:07:07 +0100")
680 bool ParseRfc822Date(const wxString
& date
);
681 // parse a date/time in the given format (see strptime(3))
682 bool ParseFormat(const wxString
& date
, const char *format
= "%c");
683 // parse a string containing the date/time in "free" format, this
684 // function will try to make an educated guess at the string contents
685 // (and return FALSE if it fails)
686 bool ParseDateTime(const wxString
& date
);
688 // this function accepts strftime()-like format string (default
689 // argument corresponds to the preferred date and time representation
690 // for the current locale) and returns the string containing the
691 // resulting text representation
692 wxString
Format(const wxChar
*format
= _T("%c")) const;
693 // preferred date representation for the current locale
694 wxString
FormatDate() const { return Format(_T("%x")); }
695 // preferred time representation for the current locale
696 wxString
FormatTime() const { return Format(_T("%X")); }
699 // ------------------------------------------------------------------------
701 // get the internal representation
702 inline wxLongLong
GetValue() const;
705 // a helper function to get the current time_t
706 static inline time_t GetTimeNow() { return time((time_t *)NULL
); }
708 // the current country - as it's the same for all program objects (unless
709 // it runs on a _really_ big cluster system :-), this is a static member:
710 // see SetCountry() and GetCountry()
711 static Country ms_country
;
713 // this constant is used to transform a time_t value to the internal
714 // representation, as time_t is in seconds and we use milliseconds it's
716 static const unsigned int TIME_T_FACTOR
;
718 // invalid wxDateTime object - returned by all functions which return
719 // "wxDateTime &" on failure
720 static wxDateTime ms_InvDateTime
;
722 // returns TRUE if we fall in range in which we can use standard ANSI C
724 inline IsInStdRange() const;
726 // the internal representation of the time is the amount of milliseconds
727 // elapsed since the origin which is set by convention to the UNIX/C epoch
728 // value: the midnight of January 1, 1970 (UTC)
732 // ----------------------------------------------------------------------------
733 // This class contains a difference between 2 wxDateTime values, so it makes
734 // sense to add it to wxDateTime and it is the result of substraction of 2
735 // objects of that class. See also wxDateSpan.
736 // ----------------------------------------------------------------------------
738 class WXDLLEXPORT wxTimeSpan
742 // ------------------------------------------------------------------------
744 // default ctor constructs the 0 time span
747 // from separate values for each component, date set to 0 (hours are
748 // not restricted to 0..24 range, neither are minutes, seconds or
750 wxTimeSpan(int hours
,
753 int milliseconds
= 0);
754 // from separate values for each component with explicit date (none of
755 // the parameters isn't restricted to any range)
756 wxTimeSpan(int years
,
762 int milliseconds
= 0);
764 // from internal representation
765 wxTimeSpan(wxLongLong diff
) : m_diff(diff
) { }
767 // default copy ctor is ok
771 // arithmetics with time spans
772 // ------------------------------------------------------------------------
774 // add two timespans together
775 inline wxTimeSpan
& Add(const wxTimeSpan
& diff
);
776 // add two timespans together
777 wxTimeSpan
& operator+=(const wxTimeSpan
& diff
) { return Add(diff
); }
779 // substract another timespan
780 inline wxTimeSpan
& Substract(const wxTimeSpan
& diff
);
781 // substract another timespan
782 wxTimeSpan
& operator-=(const wxTimeSpan
& diff
) { return Substract(diff
); }
784 // multiply timespan by a scalar
785 inline wxTimeSpan
& Multiply(int n
);
786 // multiply timespan by a scalar
787 wxTimeSpan
& operator*=(int n
) { return Multiply(n
); }
788 // multiply timespan by a scalar
789 inline wxTimeSpan
operator*(int n
) const;
791 // return this timespan with inversed sign
792 wxTimeSpan
Negate() const { return wxTimeSpan(-GetValue()); }
793 // negate the value of the timespan
794 wxTimeSpan
& Neg() { m_diff
= -GetValue(); return *this; }
795 // negate the value of the timespan
796 wxTimeSpan
& operator-() { return Neg(); }
798 // return the absolute value of the timespan: does _not_ modify the
800 inline wxTimeSpan
Abs() const;
802 // there is intentionally no division because we don't want to
803 // introduce rounding errors in time calculations
805 // comparaison (see also operator versions below)
806 // ------------------------------------------------------------------------
808 // is the timespan null?
809 bool IsNull() const { return m_diff
== 0l; }
810 // returns true if the timespan is null
811 bool operator!() const { return !IsNull(); }
813 // is the timespan positive?
814 bool IsPositive() const { return m_diff
> 0l; }
816 // is the timespan negative?
817 bool IsNegative() const { return m_diff
< 0l; }
819 // are two timespans equal?
820 inline bool IsEqualTo(const wxTimeSpan
& ts
) const;
821 // compare two timestamps: works with the absolute values, i.e. -2
822 // hours is longer than 1 hour. Also, it will return FALSE if the
823 // timespans are equal in absolute value.
824 inline bool IsLongerThan(const wxTimeSpan
& ts
) const;
825 // compare two timestamps: works with the absolute values, i.e. 1
826 // hour is shorter than -2 hours. Also, it will return FALSE if the
827 // timespans are equal in absolute value.
828 bool IsShorterThan(const wxTimeSpan
& t
) const { return !IsLongerThan(t
); }
830 // breaking into years, ..., days, ..., seconds: all these functions
831 // behave like GetYears() which returns 1 for the timespan of 1 year and 1
832 // day, but 0 (and not -1) for the negative timespan of 1 year without 1
833 // day. IOW, (ts - wxTimeSpan(ts.GetYears())).GetYears() is always 0.
834 // ------------------------------------------------------------------------
836 // get the max number of years in this timespan
837 inline int GetYears() const;
838 // get the max number of months in this timespan
839 inline int GetMonths() const;
840 // get the max number of weeks in this timespan
841 inline int GetWeeks() const;
842 // get the max number of days in this timespan
843 inline int GetDays() const;
844 // get the max number of hours in this timespan
845 inline int GetHours() const;
846 // get the max number of minutes in this timespan
847 inline int GetMinutes() const;
848 // get the max number of seconds in this timespan
849 inline int GetSeconds() const;
850 // get the number of milliseconds in this timespan
851 wxLongLong
GetMilliseconds() const { return m_diff
; }
853 // conversion to text
854 // ------------------------------------------------------------------------
856 // this function accepts strftime()-like format string (default
857 // argument corresponds to the preferred date and time representation
858 // for the current locale) and returns the string containing the
859 // resulting text representation. Notice that only some of format
860 // specifiers valid for wxDateTime are valid for wxTimeSpan: hours,
861 // minutes and seconds make sense, but not "PM/AM" string for example.
862 wxString
Format(const char *format
= "%c") const;
863 // preferred date representation for the current locale
864 wxString
FormatDate() const { return Format("%x"); }
865 // preferred time representation for the current locale
866 wxString
FormatTime() const { return Format("%X"); }
869 // ------------------------------------------------------------------------
871 // get the internal representation
872 wxLongLong
GetValue() const { return m_diff
; }
875 // the (signed) time span in milliseconds
879 // ----------------------------------------------------------------------------
880 // This class is a "logical time span" and is useful for implementing program
881 // logic for such things as "add one month to the date" which, in general,
882 // doesn't mean to add 60*60*24*31 seconds to it, but to take the same date
883 // the next month (to understand that this is indeed different consider adding
884 // one month to Feb, 15 - we want to get Mar, 15, of course).
886 // When adding a month to the date, all lesser components (days, hours, ...)
889 // wxDateSpan can be either positive or negative. They may be
890 // multiplied by scalars which multiply all deltas by the scalar: i.e. 2*(1
891 // month and 1 day) is 2 months and 2 days. They can be added together and
892 // with wxDateTime or wxTimeSpan, but the type of result is different for each
895 // Beware about weeks: if you specify both weeks and days, the total number of
896 // days added will be 7*weeks + days! See also GetTotalDays() function.
898 // Finally, notice that for adding hours, minutes &c you don't need this
899 // class: wxTimeSpan will do the job because there are no subtleties
900 // associated with those.
901 // ----------------------------------------------------------------------------
903 class WXDLLEXPORT wxDateSpan
907 // ------------------------------------------------------------------------
909 // this many years/months/weeks/days
910 wxDateSpan(int years
, int months
, int weeks
, int days
);
912 // default copy ctor is ok
916 // accessors (all SetXXX() return the (modified) wxDateSpan object)
917 // ------------------------------------------------------------------------
919 // set number of years
920 wxDateSpan
& SetYears(int n
) { m_years
= n
; return *this; }
921 // set number of months
922 wxDateSpan
& SetMonths(int n
) { m_months
= n
; return *this; }
923 // set number of weeks
924 wxDateSpan
& SetWeeks(int n
) { m_weeks
= n
; return *this; }
925 // set number of days
926 wxDateSpan
& SetDays(int n
) { m_days
= n
; return *this; }
928 // get number of years
929 int GetYears() const { return m_years
; }
930 // get number of months
931 int GetMonths() const { return m_months
; }
932 // get number of weeks
933 int GetWeeks() const { return m_weeks
; }
934 // get number of days
935 int GetDays() const { return m_days
; }
936 // returns 7*GetWeeks() + GetDays()
937 int GetTotalDays() const { return 7*m_weeks
+ m_days
; }
940 // ------------------------------------------------------------------------
942 // add another wxDateSpan to us
943 inline wxDateSpan
& Add(const wxDateSpan
& other
);
944 // add another wxDateSpan to us
945 inline wxDateSpan
& operator+=(const wxDateSpan
& other
);
947 // substract another wxDateSpan from us
948 inline wxDateSpan
& Substract(const wxDateSpan
& other
);
949 // substract another wxDateSpan from us
950 inline wxDateSpan
& operator-=(const wxDateSpan
& other
);
952 // return a copy of this time span with changed sign
953 inline wxDateSpan
Negate() const;
954 // inverse the sign of this timespan
955 inline wxDateSpan
& Neg();
956 // inverse the sign of this timespan
957 wxDateSpan
& operator-() { return Neg(); }
959 // multiply all components by a (signed) number
960 inline wxDateSpan
& operator*=(int factor
);
969 WXDLLEXPORT_DATA(extern wxDateSpan
) wxYear
;
970 WXDLLEXPORT_DATA(extern wxDateSpan
) wxMonth
;
971 WXDLLEXPORT_DATA(extern wxDateSpan
) wxWeek
;
972 WXDLLEXPORT_DATA(extern wxDateSpan
) wxDay
;
974 // ============================================================================
976 // ============================================================================
978 // ----------------------------------------------------------------------------
979 // wxDateTime operators
980 // ----------------------------------------------------------------------------
982 inline bool WXDLLEXPORT
operator<(const wxDateTime
&t1
, const wxDateTime
&t2
)
984 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
986 return t1
.GetValue() < t2
.GetValue();
989 inline bool WXDLLEXPORT
operator<=(const wxDateTime
&t1
, const wxDateTime
&t2
)
991 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
993 return t1
.GetValue() <= t2
.GetValue();
996 inline bool WXDLLEXPORT
operator>(const wxDateTime
&t1
, const wxDateTime
&t2
)
998 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1000 return t1
.GetValue() > t2
.GetValue();
1003 inline bool WXDLLEXPORT
operator>=(const wxDateTime
&t1
, const wxDateTime
&t2
)
1005 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1007 return t1
.GetValue() >= t2
.GetValue();
1010 inline bool WXDLLEXPORT
operator==(const wxDateTime
&t1
, const wxDateTime
&t2
)
1012 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1014 return t1
.GetValue() == t2
.GetValue();
1017 inline bool WXDLLEXPORT
operator!=(const wxDateTime
&t1
, const wxDateTime
&t2
)
1019 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1021 return t1
.GetValue() != t2
.GetValue();
1024 inline wxTimeSpan WXDLLEXPORT
operator-(const wxDateTime
&t1
,
1025 const wxDateTime
&t2
)
1027 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
1029 return wxTimeSpan(t1
.GetValue() - t2
.GetValue());
1032 // ----------------------------------------------------------------------------
1033 // wxTimeSpan operators
1034 // ----------------------------------------------------------------------------
1036 inline wxTimeSpan WXDLLEXPORT
operator+(const wxTimeSpan
& ts1
,
1037 const wxTimeSpan
& ts2
)
1039 return wxTimeSpan(ts1
.GetValue() + ts2
.GetValue());
1042 inline wxTimeSpan WXDLLEXPORT
operator-(const wxTimeSpan
& ts1
,
1043 const wxTimeSpan
& ts2
)
1045 return wxTimeSpan(ts1
.GetValue() - ts2
.GetValue());
1048 inline bool WXDLLEXPORT
operator<(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1050 return t1
.GetValue() < t2
.GetValue();
1053 inline bool WXDLLEXPORT
operator<=(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1055 return t1
.GetValue() <= t2
.GetValue();
1058 inline bool WXDLLEXPORT
operator>(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1060 return t1
.GetValue() > t2
.GetValue();
1063 inline bool WXDLLEXPORT
operator>=(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1065 return t1
.GetValue() >= t2
.GetValue();
1068 inline bool WXDLLEXPORT
operator==(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1070 return t1
.GetValue() == t2
.GetValue();
1073 inline bool WXDLLEXPORT
operator!=(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
1075 return t1
.GetValue() != t2
.GetValue();
1078 // ----------------------------------------------------------------------------
1080 // ----------------------------------------------------------------------------
1082 inline WXDLLEXPORT wxDateSpan
operator+(const wxDateSpan
& rt1
,
1083 const wxDateSpan
& rt2
)
1085 return wxDateSpan(rt1
.GetYears() + rt2
.GetYears(),
1086 rt1
.GetMonths() + rt2
.GetMonths(),
1087 rt1
.GetWeeks() + rt2
.GetWeeks(),
1088 rt1
.GetDays() + rt2
.GetDays());
1091 // ============================================================================
1092 // inline functions implementation
1093 // ============================================================================
1095 // ----------------------------------------------------------------------------
1096 // wxDateTime statics
1097 // ----------------------------------------------------------------------------
1100 wxDateTime::Country
wxDateTime::GetCountry()
1105 // ----------------------------------------------------------------------------
1106 // wxDateTime construction
1107 // ----------------------------------------------------------------------------
1109 // only define this once, when included from datetime.cpp
1110 #ifdef wxDEFINE_TIME_CONSTANTS
1111 const unsigned int wxDateTime::TIME_T_FACTOR
= 1000;
1112 #endif // wxDEFINE_TIME_CONSTANTS
1114 wxDateTime::IsInStdRange() const
1116 return m_time
>= 0l && (m_time
/ (long)TIME_T_FACTOR
) < LONG_MAX
;
1120 wxDateTime
wxDateTime::Now()
1122 return wxDateTime(GetTimeNow());
1125 wxDateTime
& wxDateTime::Set(time_t timet
)
1127 m_time
= timet
* TIME_T_FACTOR
;
1132 wxDateTime
& wxDateTime::SetToCurrent()
1134 return Set(GetTimeNow());
1137 wxDateTime::wxDateTime(time_t timet
)
1142 wxDateTime::wxDateTime(const struct tm
& tm
)
1147 wxDateTime::wxDateTime(const Tm
& tm
)
1152 wxDateTime
& wxDateTime::Set(const Tm
& tm
)
1154 wxASSERT_MSG( tm
.IsValid(), _T("invalid broken down date/time") );
1156 return Set(tm
.mday
, (Month
)tm
.mon
, tm
.year
, tm
.hour
, tm
.min
, tm
.sec
);
1159 wxDateTime::wxDateTime(wxDateTime_t hour
,
1160 wxDateTime_t minute
,
1161 wxDateTime_t second
,
1162 wxDateTime_t millisec
)
1164 Set(hour
, minute
, second
, millisec
);
1167 wxDateTime::wxDateTime(wxDateTime_t day
,
1171 wxDateTime_t minute
,
1172 wxDateTime_t second
,
1173 wxDateTime_t millisec
)
1175 Set(day
, month
, year
, hour
, minute
, second
, millisec
);
1178 // ----------------------------------------------------------------------------
1179 // wxDateTime accessors
1180 // ----------------------------------------------------------------------------
1182 wxLongLong
wxDateTime::GetValue() const
1184 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
1189 time_t wxDateTime::GetTicks() const
1191 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
1192 if ( !IsInStdRange() )
1197 return (time_t)((m_time
/ (long)TIME_T_FACTOR
).GetLo());
1200 bool wxDateTime::SetToLastWeekDay(WeekDay weekday
,
1204 SetToWeekDay(weekday
, -1, month
, year
);
1207 // ----------------------------------------------------------------------------
1208 // wxDateTime comparison
1209 // ----------------------------------------------------------------------------
1211 bool wxDateTime::IsEqualTo(const wxDateTime
& datetime
) const
1213 wxASSERT_MSG( IsValid() && datetime
.IsValid(), "invalid wxDateTime");
1215 return m_time
== datetime
.m_time
;
1218 bool wxDateTime::operator==(const wxDateTime
& datetime
) const
1220 return IsEqualTo(datetime
);
1223 bool wxDateTime::operator!=(const wxDateTime
& datetime
) const
1225 return !IsEqualTo(datetime
);
1228 bool wxDateTime::IsEarlierThan(const wxDateTime
& datetime
) const
1230 wxASSERT_MSG( IsValid() && datetime
.IsValid(), "invalid wxDateTime");
1232 return m_time
< datetime
.m_time
;
1235 bool wxDateTime::IsLaterThan(const wxDateTime
& datetime
) const
1237 wxASSERT_MSG( IsValid() && datetime
.IsValid(), "invalid wxDateTime");
1239 return m_time
> datetime
.m_time
;
1242 bool wxDateTime::IsStrictlyBetween(const wxDateTime
& t1
,
1243 const wxDateTime
& t2
) const
1245 // no need for assert, will be checked by the functions we call
1246 return IsLaterThan(t1
) && IsEarlierThan(t2
);
1249 bool wxDateTime::IsBetween(const wxDateTime
& t1
, const wxDateTime
& t2
) const
1251 // no need for assert, will be checked by the functions we call
1252 return IsEqualTo(t1
) || IsEqualTo(t2
) || IsStrictlyBetween(t1
, t2
);
1255 // ----------------------------------------------------------------------------
1256 // wxDateTime arithmetics
1257 // ----------------------------------------------------------------------------
1259 wxDateTime
& wxDateTime::Add(const wxTimeSpan
& diff
)
1261 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
1263 m_time
+= diff
.GetValue();
1268 wxDateTime
& wxDateTime::operator+=(const wxTimeSpan
& diff
)
1273 wxDateTime
& wxDateTime::Substract(const wxTimeSpan
& diff
)
1275 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
1277 m_time
-= diff
.GetValue();
1282 wxDateTime
& wxDateTime::operator-=(const wxTimeSpan
& diff
)
1284 return Substract(diff
);
1287 wxTimeSpan
wxDateTime::Substract(const wxDateTime
& datetime
) const
1289 wxASSERT_MSG( IsValid() && datetime
.IsValid(), "invalid wxDateTime");
1291 return wxTimeSpan(datetime
.GetValue() - GetValue());
1294 wxTimeSpan
wxDateTime::operator-(const wxDateTime
& datetime
) const
1296 return Substract(datetime
);
1299 wxDateTime
& wxDateTime::Substract(const wxDateSpan
& diff
)
1301 return Add(diff
.Negate());
1304 wxDateTime
& wxDateTime::operator-=(const wxDateSpan
& diff
)
1306 return Substract(diff
);
1309 wxDateTime
& wxDateTime::operator+=(const wxDateSpan
& diff
)
1314 // ----------------------------------------------------------------------------
1316 // ----------------------------------------------------------------------------
1318 wxTimeSpan
& wxTimeSpan::Add(const wxTimeSpan
& diff
)
1320 m_diff
+= diff
.GetValue();
1325 wxTimeSpan
& wxTimeSpan::Substract(const wxTimeSpan
& diff
)
1327 m_diff
-= diff
.GetValue();
1332 wxTimeSpan
& wxTimeSpan::Multiply(int n
)
1339 wxTimeSpan
wxTimeSpan::operator*(int n
) const
1341 wxTimeSpan
result(*this);
1347 wxTimeSpan
wxTimeSpan::Abs() const
1349 return wxTimeSpan(GetValue().Abs());
1352 bool wxTimeSpan::IsEqualTo(const wxTimeSpan
& ts
) const
1354 return GetValue() == ts
.GetValue();
1357 bool wxTimeSpan::IsLongerThan(const wxTimeSpan
& ts
) const
1359 return Abs() > ts
.Abs();
1362 // ----------------------------------------------------------------------------
1364 // ----------------------------------------------------------------------------
1367 wxDateSpan::operator+=(const wxDateSpan
& other
)
1369 m_years
+= other
.m_years
;
1370 m_months
+= other
.m_months
;
1371 m_weeks
+= other
.m_weeks
;
1372 m_days
+= other
.m_days
;
1377 wxDateSpan
& wxDateSpan::operator*=(int factor
)
1380 m_months
*= m_months
;
1387 wxDateSpan
wxDateSpan::Negate() const
1389 return wxDateSpan(-m_years
, -m_months
, -m_weeks
, -m_days
);
1392 wxDateSpan
& wxDateSpan::Neg()
1395 m_months
= -m_months
;
1402 #endif // _WX_TIME_H