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"
22 #include "wx/longlong.h"
24 class WXDLLEXPORT wxDateTime
;
25 class WXDLLEXPORT wxTimeSpan
;
26 class WXDLLEXPORT wxDateSpan
;
29 * TODO Well, everything :-)
31 * 1. Time zones with minutes (make wxTimeZone a class)
32 * 2. getdate() function like under Solaris
33 * 3. text conversion for wxDateSpan
37 The three classes declared in this header represent:
39 1. An absolute moment in the time (wxDateTime)
40 2. A difference between two moments in the time, positive or negative
42 3. A logical difference between two dates expressed in
43 years/months/weeks/days (wxDateSpan)
45 The following arithmetic operations are permitted (all others are not):
50 wxDateTime + wxTimeSpan = wxDateTime
51 wxDateTime + wxDateSpan = wxDateTime
52 wxTimeSpan + wxTimeSpan = wxTimeSpan
53 wxDateSpan + wxDateSpan = wxDateSpan
57 wxDateTime - wxDateTime = wxTimeSpan
58 wxTimeSpan - wxTimeSpan = wxTimeSpan
59 wxDateSpan - wxDateSpan = wxDateSpan
63 wxTimeSpan * number = wxTimeSpan
64 wxDateSpan * number = wxDateSpan
68 -wxTimeSpan = wxTimeSpan
69 -wxDateSpan = wxDateSpan
72 // ----------------------------------------------------------------------------
73 // This class represents an absolute moment in the time
74 // ----------------------------------------------------------------------------
75 class WXDLLEXPORT wxDateTime
79 // ------------------------------------------------------------------------
81 // a small unsigned integer type for storing things like seconds, days
82 // of the week, &c. It should be at least short (i.e. not char) to
83 // contain the number of milliseconds - it may also be 'int' because
84 // there is no size penalty associated with it in our code, we don't
85 // store any data in this format
86 typedef unsigned short wxDateTime_t
;
91 // the time in the current time zone
94 // zones from GMT (= Greenwhich Mean Time): they're guaranteed to be
95 // consequent numbers, so writing something like `GMT0 + offset' is
96 // safe if abs(offset) <= 12
98 // underscore stands for minus
99 GMT_12
, GMT_11
, GMT_10
, GMT_9
, GMT_8
, GMT_7
,
100 GMT_6
, GMT_5
, GMT_4
, GMT_3
, GMT_2
, GMT_1
,
102 GMT1
, GMT2
, GMT3
, GMT4
, GMT5
, GMT6
,
103 GMT7
, GMT8
, GMT9
, GMT10
, GMT11
, GMT12
,
104 // Note that GMT12 and GMT_12 are not the same: there is a difference
105 // of exactly one day between them
107 // Universal Coordinated Time
110 // TODO add symbolic names for TZ (EST, MET, ...)?
113 // the calendar systems we know about: notice that it's valid (for
114 // this classes purpose anyhow) to work with any of these calendars
115 // even with the dates before the historical appearance of the
119 Gregorian
, // current calendar
120 Julian
// calendar in use since -45 until the 1582 (or later)
122 // TODO Hebrew, Chinese, Maya, ... (just kidding) (or then may be not?)
125 // these values only are used to identify the different dates of
126 // adoption of the Gregorian calendar (see IsGregorian())
128 // All data and comments taken verbatim from "The Calendar FAQ (v 2.0)"
129 // by Claus Tøndering, http://www.pip.dknet.dk/~c-t/calendar.html
130 // except for the comments "we take".
132 // Symbol "->" should be read as "was followed by" in the comments
134 enum GregorianAdoption
136 Gr_Unknown
, // no data for this country or it's too uncertain to use
137 Gr_Standard
, // on the day 0 of Gregorian calendar: 15 Oct 1582
139 Gr_Alaska
, // Oct 1867 when Alaska became part of the USA
140 Gr_Albania
, // Dec 1912
142 Gr_Austria
= Gr_Unknown
, // Different regions on different dates
143 Gr_Austria_Brixen
, // 5 Oct 1583 -> 16 Oct 1583
144 Gr_Austria_Salzburg
= Gr_Austria_Brixen
,
145 Gr_Austria_Tyrol
= Gr_Austria_Brixen
,
146 Gr_Austria_Carinthia
, // 14 Dec 1583 -> 25 Dec 1583
147 Gr_Austria_Styria
= Gr_Austria_Carinthia
,
149 Gr_Belgium
, // Then part of the Netherlands
151 Gr_Bulgaria
= Gr_Unknown
, // Unknown precisely (from 1915 to 1920)
152 Gr_Bulgaria_1
, // 18 Mar 1916 -> 1 Apr 1916
153 Gr_Bulgaria_2
, // 31 Mar 1916 -> 14 Apr 1916
154 Gr_Bulgaria_3
, // 3 Sep 1920 -> 17 Sep 1920
156 Gr_Canada
= Gr_Unknown
, // Different regions followed the changes in
157 // Great Britain or France
159 Gr_China
= Gr_Unknown
, // Different authorities say:
160 Gr_China_1
, // 18 Dec 1911 -> 1 Jan 1912
161 Gr_China_2
, // 18 Dec 1928 -> 1 Jan 1929
163 Gr_Czechoslovakia
, // (Bohemia and Moravia) 6 Jan 1584 -> 17 Jan 1584
164 Gr_Denmark
, // (including Norway) 18 Feb 1700 -> 1 Mar 1700
167 Gr_Finland
, // Then part of Sweden
169 Gr_France
, // 9 Dec 1582 -> 20 Dec 1582
170 Gr_France_Alsace
, // 4 Feb 1682 -> 16 Feb 1682
171 Gr_France_Lorraine
, // 16 Feb 1760 -> 28 Feb 1760
172 Gr_France_Strasbourg
, // February 1682
174 Gr_Germany
= Gr_Unknown
, // Different states on different dates:
175 Gr_Germany_Catholic
, // 1583-1585 (we take 1584)
176 Gr_Germany_Prussia
, // 22 Aug 1610 -> 2 Sep 1610
177 Gr_Germany_Protestant
, // 18 Feb 1700 -> 1 Mar 1700
179 Gr_GreatBritain
, // 2 Sep 1752 -> 14 Sep 1752 (use 'cal(1)')
181 Gr_Greece
, // 9 Mar 1924 -> 23 Mar 1924
182 Gr_Hungary
, // 21 Oct 1587 -> 1 Nov 1587
183 Gr_Ireland
= Gr_GreatBritain
,
184 Gr_Italy
= Gr_Standard
,
186 Gr_Japan
= Gr_Unknown
, // Different authorities say:
187 Gr_Japan_1
, // 19 Dec 1872 -> 1 Jan 1873
188 Gr_Japan_2
, // 19 Dec 1892 -> 1 Jan 1893
189 Gr_Japan_3
, // 18 Dec 1918 -> 1 Jan 1919
191 Gr_Latvia
, // 1915-1918 (we take 1915)
192 Gr_Lithuania
, // 1915
193 Gr_Luxemburg
, // 14 Dec 1582 -> 25 Dec 1582
194 Gr_Netherlands
= Gr_Belgium
, // (including Belgium) 1 Jan 1583
196 // this is too weird to take into account: the Gregorian calendar was
197 // introduced twice in Groningen, first time 28 Feb 1583 was followed
198 // by 11 Mar 1583, then it has gone back to Julian in the summer of
199 // 1584 and then 13 Dec 1700 -> 12 Jan 1701 - which is
200 // the date we take here
201 Gr_Netherlands_Groningen
, // 13 Dec 1700 -> 12 Jan 1701
202 Gr_Netherlands_Gelderland
, // 30 Jun 1700 -> 12 Jul 1700
203 Gr_Netherlands_Utrecht
, // (and Overijssel) 30 Nov 1700->12 Dec 1700
204 Gr_Netherlands_Friesland
, // (and Drenthe) 31 Dec 1700 -> 12 Jan 1701
206 Gr_Norway
= Gr_Denmark
, // Then part of Denmark
207 Gr_Poland
= Gr_Standard
,
208 Gr_Portugal
= Gr_Standard
,
209 Gr_Romania
, // 31 Mar 1919 -> 14 Apr 1919
210 Gr_Russia
, // 31 Jan 1918 -> 14 Feb 1918
211 Gr_Scotland
= Gr_GreatBritain
,
212 Gr_Spain
= Gr_Standard
,
214 // Sweden has a curious history. Sweden decided to make a gradual
215 // change from the Julian to the Gregorian calendar. By dropping every
216 // leap year from 1700 through 1740 the eleven superfluous days would
217 // be omitted and from 1 Mar 1740 they would be in sync with the
218 // Gregorian calendar. (But in the meantime they would be in sync with
221 // So 1700 (which should have been a leap year in the Julian calendar)
222 // was not a leap year in Sweden. However, by mistake 1704 and 1708
223 // became leap years. This left Sweden out of synchronisation with
224 // both the Julian and the Gregorian world, so they decided to go back
225 // to the Julian calendar. In order to do this, they inserted an extra
226 // day in 1712, making that year a double leap year! So in 1712,
227 // February had 30 days in Sweden.
229 // Later, in 1753, Sweden changed to the Gregorian calendar by
230 // dropping 11 days like everyone else.
231 Gr_Sweden
= Gr_Finland
, // 17 Feb 1753 -> 1 Mar 1753
233 Gr_Switzerland
= Gr_Unknown
,// Different cantons used different dates
234 Gr_Switzerland_Catholic
, // 1583, 1584 or 1597 (we take 1584)
235 Gr_Switzerland_Protestant
, // 31 Dec 1700 -> 12 Jan 1701
237 Gr_Turkey
, // 1 Jan 1927
238 Gr_USA
= Gr_GreatBritain
,
239 Gr_Wales
= Gr_GreatBritain
,
240 Gr_Yugoslavia
// 1919
243 // the country parameter is used so far for calculating the start and
244 // the end of DST period and for deciding whether the date is a work
247 // TODO move this to intl.h
250 Country_Unknown
, // no special information for this country
251 Country_Default
, // set the default country with SetCountry() method
253 // TODO add more countries (for this we must know about DST and/or
254 // holidays for this country)
259 // symbolic names for the months
262 Jan
, Feb
, Mar
, Apr
, May
, Jun
, Jul
, Aug
, Sep
, Oct
, Nov
, Dec
, Inv_Month
265 // symbolic names for the weekdays
268 Sun
, Mon
, Tue
, Wed
, Thu
, Fri
, Sat
, Inv_WeekDay
271 // invalid value for the year
278 // ------------------------------------------------------------------------
280 // set the current country
281 static void SetCountry(Country country
);
282 // get the current country
283 static Country
GetCountry();
285 // return the current year
286 static int GetCurrentYear(Calendar cal
= Gregorian
);
288 // convert the year as returned by wxDateTime::GetYear() to a year
289 // suitable for BC/AD notation. The difference is that BC year 1
290 // corresponds to the year 0 (while BC year 0 didn't exist) and AD
291 // year N is just year N.
292 static int ConvertYearToBC(int year
);
294 // return the current month
295 static Month
GetCurrentMonth(Calendar cal
= Gregorian
);
297 // returns TRUE if the given year is a leap year in the given calendar
298 static bool IsLeapYear(int year
, Calendar cal
= Gregorian
);
300 // returns the number of days in this year (356 or 355 typically :-)
301 static wxDateTime_t
GetNumberOfDays(int year
, Calendar cal
= Gregorian
);
303 // get the number of the days in the given month (default value for
304 // the year means the current one)
305 static wxDateTime_t
GetNumberOfDays(Month month
,
307 Calendar cal
= Gregorian
);
309 // get the month name in the current locale
310 static wxString
GetMonthName(Month month
);
312 // get the weekday name in the current locale
313 static wxString
GetDayOfWeekName(WeekDay weekday
);
315 // get the beginning of DST for this year, will return invalid object
316 // if no DST applicable in this year. The default value of the
317 // parameter means to take the current year.
318 static wxDateTime
GetBeginDST(int year
= Inv_Year
);
319 // get the end of DST for this year, will return invalid object
320 // if no DST applicable in this year. The default value of the
321 // parameter means to take the current year.
322 static wxDateTime
GetEndDST(int year
= Inv_Year
);
324 // constructors: you should test whether the constructor succeeded with
325 // IsValid() function. The values Inv_Month and Inv_Year for the
326 // parameters mean take current month and/or year values.
327 // ------------------------------------------------------------------------
329 // default ctor does not initialize the object, use Set()!
332 // from time_t: seconds since the Epoch 00:00:00 UTC, Jan 1, 1970)
333 inline wxDateTime(time_t timet
);
334 // from broken down time/date
335 inline wxDateTime(const struct tm
& tm
);
337 // from separate values for each component, date set to today
338 inline wxDateTime(wxDateTime_t hour
,
339 wxDateTime_t minute
= 0,
340 wxDateTime_t second
= 0,
341 wxDateTime_t millisec
= 0);
342 // from separate values for each component with explicit date
343 inline wxDateTime(wxDateTime_t day
,
344 Month month
= Inv_Month
,
345 int year
= Inv_Year
, // 1999, not 99 please!
346 wxDateTime_t hour
= 0,
347 wxDateTime_t minute
= 0,
348 wxDateTime_t second
= 0,
349 wxDateTime_t millisec
= 0);
351 // default copy ctor ok
355 // assignment operators and Set() functions: all non const methods return
356 // the reference to this object. IsValid() should be used to test whether
357 // the function succeeded.
358 // ------------------------------------------------------------------------
360 // set to the current time
361 inline wxDateTime
& SetToCurrent();
363 // set to given time_t value
364 inline wxDateTime
& Set(time_t timet
);
366 // set to given broken down time/date
367 wxDateTime
& Set(const struct tm
& tm
);
369 // set to given time, date = today
370 wxDateTime
& Set(wxDateTime_t hour
,
371 wxDateTime_t minute
= 0,
372 wxDateTime_t second
= 0,
373 wxDateTime_t millisec
= 0);
375 // from separate values for each component with explicit date
376 // (defaults for month and year are the current values)
377 wxDateTime
& Set(wxDateTime_t day
,
378 Month month
= Inv_Month
,
379 int year
= Inv_Year
, // 1999, not 99 please!
380 wxDateTime_t hour
= 0,
381 wxDateTime_t minute
= 0,
382 wxDateTime_t second
= 0,
383 wxDateTime_t millisec
= 0);
385 // resets time to 00:00:00, doesn't change the date
386 wxDateTime
& ResetTime();
388 // the following functions don't change the values of the other
389 // fields, i.e. SetMinute() won't change either hour or seconds value
392 wxDateTime
& SetYear(int year
);
394 wxDateTime
& SetMonth(Month month
);
395 // set the day of the month
396 wxDateTime
& SetDay(wxDateTime_t day
);
398 wxDateTime
& SetHour(wxDateTime_t hour
);
400 wxDateTime
& SetMinute(wxDateTime_t minute
);
402 wxDateTime
& SetSecond(wxDateTime_t second
);
404 wxDateTime
& SetMillisecond(wxDateTime_t millisecond
);
406 // assignment operator from time_t
407 wxDateTime
& operator=(time_t timet
) { return Set(timet
); }
409 // assignment operator from broken down time/date
410 wxDateTime
& operator=(const struct tm
& tm
) { return Set(tm
); }
412 // default assignment operator is ok
414 // calendar calculations (functions which set the date only leave the time
415 // unchanged, e.g. don't explictly zero it)
416 // ------------------------------------------------------------------------
418 // set to Nth occurence of given weekday in the given month of the
419 // given year (time is set to 0), return TRUE on success and FALSE on
420 // failure. n may be positive (1..5) or negative to count from the end
421 // of the month (see helper function SetToLastWeekDay())
422 bool SetToWeekDay(WeekDay weekday
,
424 wxDateTime_t month
= Inv_Month
,
425 int year
= Inv_Year
);
427 // sets to the last weekday in the given month, year
428 inline bool SetToLastWeekDay(WeekDay weekday
,
429 wxDateTime_t month
= Inv_Month
,
430 int year
= Inv_Year
);
432 // sets the date to the given day of the given week in the year,
433 // returns TRUE on success and FALSE if given date doesn't exist (e.g.
435 bool SetToTheWeek(wxDateTime_t numWeek
, WeekDay weekday
= Mon
);
437 // get the century (19 for 1999, 20 for 2000 and -5 for 492 BC)
438 int GetCentury() const;
440 // The definitions below were taken verbatim from
442 // http://www.capecod.net/~pbaum/date/date0.htm
444 // (Peter Baum's home page)
446 // definition: The Julian Day Number, Julian Day, or JD of a
447 // particular instant of time is the number of days and fractions of a
448 // day since 12 hours Universal Time (Greenwich mean noon) on January
449 // 1 of the year -4712, where the year is given in the Julian
450 // proleptic calendar. The idea of using this reference date was
451 // originally proposed by Joseph Scalizer in 1582 to count years but
452 // it was modified by 19th century astronomers to count days. One
453 // could have equivalently defined the reference time to be noon of
454 // November 24, -4713 if were understood that Gregorian calendar rules
455 // were applied. Julian days are Julian Day Numbers and are not to be
456 // confused with Julian dates.
458 // definition: The Rata Die number is a date specified as the number
459 // of days relative to a base date of December 31 of the year 0. Thus
460 // January 1 of the year 1 is Rata Die day 1.
462 // get the Julian Day number
463 long GetJulianDay() const;
465 // get the Rata Die number
466 long GetRataDie() const;
468 // TODO algorithms for calculating some important dates, such as
469 // religious holidays (Easter...) or moon/solar eclipses? Some
470 // algorithms can be found in the calendar FAQ
472 // accessors: many of them take the timezone parameter which indicates the
473 // timezone for which to make the calculations and the default value means
474 // to do it for the current timezone of this machine (even if the function
475 // only operates with the date it's necessary because a date may wrap as
476 // result of timezone shift)
477 // ------------------------------------------------------------------------
479 // is the date valid (FALSE for uninitialized objects as well as after
480 // the functions which failed to convert the date to supported range)
481 inline bool IsValid() const { return this != &ms_InvDateTime
; }
483 // get the year (returns Inv_Year if date is invalid)
484 int GetYear(TimeZone zone
= Local
) const;
485 // get the month (Inv_Month if date is invalid)
486 Month
GetMonth(TimeZone zone
= Local
) const;
487 // get the month day (in 1..31 range, 0 if date is invalid)
488 wxDateTime_t
GetDay(TimeZone zone
= Local
) const;
489 // get the day of the week (Inv_WeekDay if date is invalid)
490 WeekDay
GetDayOfWeek(TimeZone zone
= Local
) const;
491 // get the hour of the day
492 wxDateTime_t
GetHour(TimeZone zone
= Local
) const;
494 wxDateTime_t
GetMinute(TimeZone zone
= Local
) const;
496 wxDateTime_t
GetSecond(TimeZone zone
= Local
) const;
498 wxDateTime_t
GetMillisecond(TimeZone zone
= Local
) const;
500 // get the day since the year start (1..366, 0 if date is invalid)
501 wxDateTime_t
GetDayOfYear() const;
502 // get the week number since the year start (1..52, 0 if date is
504 wxDateTime_t
GetWeekOfYear() const;
506 // is this date a work day? This depends on a country, of course,
507 // because the holidays are different in different countries
508 bool IsWorkDay(Country country
= Current
, TimeZone zone
= local
) const;
510 // is this date later than Gregorian calendar introduction for the
511 // given country (see enum GregorianAdoption)?
513 // NB: this function shouldn't be considered as absolute authoiruty in
514 // the matter. Besides, for some countries the exact date of
515 // adoption of the Gregorian calendar is simply unknown.
516 bool IsGregorianDate(GregorianAdoption country
= Gregorian_Standard
) const;
518 // is daylight savings time in effect at this moment?
520 // Return value is > 0 if DST is in effect, 0 if it is not and -1 if
521 // the information is not available (this is compatible with ANSI C)
522 int IsDST(Country country
= Country_Default
, TimeZone zone
= local
) const;
524 // comparison (see also functions below for operator versions)
525 // ------------------------------------------------------------------------
527 // returns TRUE if the date is strictly earlier than the given one
528 bool IsEarlierThan(const wxDateTime
& datetime
) const;
530 // returns TRUE if the date is strictly later than the given one
531 bool IsLaterThan(const wxDateTime
& datetime
) const;
533 // returns TRUE if the date is strictly in the given range
534 bool IsStrictlyBetween(const wxDateTime
& t1
, const wxDateTime
& t2
) const;
535 // returns TRUE if the date is in the given range
536 bool IsBetween(const wxDateTime
& t1
, const wxDateTime
& t2
) const;
538 // date operations: for the non-const methods, the return value is this
539 // object itself (see also functions below for operator versions)
540 // ------------------------------------------------------------------------
542 // add a time span (positive or negative)
543 wxDateTime
& Add(const wxTimeSpan
& diff
);
544 // add a time span (positive or negative)
545 wxDateTime
& operator+=(const wxTimeSpan
& diff
) { return Add(diff
); }
547 // substract a time span (positive or negative)
548 inline wxDateTime
& Substract(const wxTimeSpan
& diff
);
549 // substract a time span (positive or negative)
550 inline wxDateTime
& operator-=(const wxTimeSpan
& diff
);
552 // add a date span (positive or negative)
553 wxDateTime
& Add(const wxDateSpan
& diff
);
554 // add a date span (positive or negative)
555 wxDateTime
& operator+=(const wxDateSpan
& diff
) { return Add(diff
); }
557 // substract a date span (positive or negative)
558 inline wxDateTime
& Substract(const wxDateSpan
& diff
);
559 // substract a date span (positive or negative)
560 inline wxDateTime
& operator-=(const wxDateSpan
& diff
);
562 // substract a date (may result in positive or negative time span)
563 wxTimeSpan
Substract(const wxDateTime
& datetime
) const;
564 // substract a date (may result in positive or negative time span)
565 wxTimeSpan
operator-(const wxDateTime
& datetime
) const;
567 // conversion to/from text: all conversions from text return TRUE on
568 // success or FALSE if the date is malformed/out of supported range
569 // ------------------------------------------------------------------------
571 // parse a string in RFC 822 format (found e.g. in mail headers and
572 // having the form "Wed, 10 Feb 1999 19:07:07 +0100")
573 bool ParseRfc822Date(const wxString
& date
);
574 // parse a date/time in the given format (see strptime(3))
575 bool ParseFormat(const wxString
& date
, const char *format
= "%c");
576 // parse a string containing the date/time in "free" format, this
577 // function will try to make an educated guess at the string contents
578 // (and return FALSE if it fails)
579 bool ParseDateTime(const wxString
& date
);
581 // this function accepts strftime()-like format string (default
582 // argument corresponds to the preferred date and time representation
583 // for the current locale) and returns the string containing the
584 // resulting text representation
585 wxString
Format(const char *format
= "%c") const;
586 // preferred date representation for the current locale
587 wxString
FormatDate() const { return Format("%x"); }
588 // preferred time representation for the current locale
589 wxString
FormatTime() const { return Format("%X"); }
592 // ------------------------------------------------------------------------
594 // get the internal representation
595 inline wxLongLong
GetValue() const;
598 // a helper function to get the current time_t
599 static inline time_t GetTimeNow() { return time((time_t *)NULL
); }
601 // the current country - as it's the same for all program objects (unless
602 // it runs on a _really_ big load balancing system), this is a static
603 // member: see SetCountry() and GetCountry()
604 static Country ms_country
;
606 // the internal representation of the time is the amount of milliseconds
607 // elapsed since the origin which is set by convention to the UNIX/C epoch
608 // value: the midnight of on January 1, 1970 (UTC)
611 // this constant is used to transform a time_t value to the internal
612 // representation, as time_t is in seconds and we use milliseconds it's
614 static const int TIME_T_FACTOR
;
616 // invalid wxDateTime object - returned by all functions which return
617 // "wxDateTime &" on failure
618 static wxDateTime ms_InvDateTime
;
621 // ----------------------------------------------------------------------------
622 // This class contains a difference between 2 wxDateTime values, so it makes
623 // sense to add it to wxDateTime and it is the result of substraction of 2
624 // objects of that class. See also wxDateSpan.
625 // ----------------------------------------------------------------------------
626 class WXDLLEXPORT wxTimeSpan
630 // ------------------------------------------------------------------------
632 // default ctor constructs the 0 time span
635 // from separate values for each component, date set to 0 (hours are
636 // not restricted to 0..24 range, neither are minutes, seconds or
638 wxTimeSpan(int hours
,
641 int milliseconds
= 0);
642 // from separate values for each component with explicit date (none of
643 // the parameters isn't restricted to any range)
644 wxTimeSpan(int years
,
650 int milliseconds
= 0);
652 // from internal representation
653 wxTimeSpan(wxLongLong diff
) : m_diff(diff
) { }
655 // default copy ctor is ok
659 // arithmetics with time spans
660 // ------------------------------------------------------------------------
662 // add two timespans together
663 inline wxTimeSpan
& Add(const wxTimeSpan
& diff
);
664 // add two timespans together
665 wxTimeSpan
& operator+=(const wxTimeSpan
& diff
) { return Add(diff
); }
666 // add two timespans together
667 inline wxTimeSpan
operator+(const wxTimeSpan
& ts1
, const wxTimeSpan
& ts2
);
669 // substract another timespan
670 inline wxTimeSpan
& Substract(const wxTimeSpan
& diff
);
671 // substract another timespan
672 wxTimeSpan
& operator-=(const wxTimeSpan
& diff
) { return Substract(diff
); }
673 // substract two timespans
674 inline wxTimeSpan
operator-(const wxTimeSpan
& ts1
, const wxTimeSpan
& ts2
);
676 // multiply timespan by a scalar
677 inline wxTimeSpan
& Multiply(int n
);
678 // multiply timespan by a scalar
679 wxTimeSpan
& operator*=(int n
) { return Multiply(n
); }
680 // multiply timespan by a scalar
681 inline wxTimeSpan
operator*(int n
) const;
683 // return this timespan with inversed sign
684 wxTimeSpan
Negate() { return wxTimeSpan(-GetValue()); }
685 // negate the value of the timespan
686 wxTimeSpan
& Neg() { m_diff
= -GetValue(); return *this; }
687 // negate the value of the timespan
688 wxTimeSpan
& operator-() { return Neg(); }
690 // return the absolute value of the timespan: does _not_ modify the
692 inline wxTimeSpan
Abs() const;
694 // there is intentionally no division because we don't want to
695 // introduce rounding errors in time calculations
697 // comparaison (see also operator versions below)
698 // ------------------------------------------------------------------------
700 // is the timespan null?
701 bool IsNull() const { return m_diff
== 0; }
702 // returns true if the timespan is null
703 bool operator!() const { return !IsNull(); }
705 // is the timespan positive?
706 bool IsPositive() const { return m_diff
> 0; }
708 // is the timespan negative?
709 bool IsNegative() const { return m_diff
< 0; }
711 // are two timespans equal?
712 inline bool IsEqualTo(const wxTimeSpan
& ts
) const;
713 // compare two timestamps: works with the absolute values, i.e. -2
714 // hours is longer than 1 hour. Also, it will return FALSE if the
715 // timespans are equal in absolute value.
716 inline bool IsLongerThan(const wxTimeSpan
& ts
) const;
717 // compare two timestamps: works with the absolute values, i.e. 1
718 // hour is shorter than -2 hours. Also, it will return FALSE if the
719 // timespans are equal in absolute value.
720 bool IsShorterThan(const wxTimeSpan
& t
) const { return !IsLongerThan(t
); }
722 // breaking into years, ..., days, ..., seconds: all these functions
723 // behave like GetYears() which returns 1 for the timespan of 1 year and 1
724 // day, but 0 (and not -1) for the negative timespan of 1 year without 1
725 // day. IOW, (ts - wxTimeSpan(ts.GetYears())).GetYears() is always 0.
726 // ------------------------------------------------------------------------
728 // get the max number of years in this timespan
729 inline int GetYears() const;
730 // get the max number of months in this timespan
731 inline int GetMonths() const;
732 // get the max number of weeks in this timespan
733 inline int GetWeeks() const;
734 // get the max number of days in this timespan
735 inline int GetDays() const;
736 // get the max number of hours in this timespan
737 inline int GetHours() const;
738 // get the max number of minutes in this timespan
739 inline int GetMinutes() const;
740 // get the max number of seconds in this timespan
741 inline int GetSeconds() const;
742 // get the number of milliseconds in this timespan
743 int GetMilliseconds() const { return m_diff
; }
745 // conversion to text
746 // ------------------------------------------------------------------------
748 // this function accepts strftime()-like format string (default
749 // argument corresponds to the preferred date and time representation
750 // for the current locale) and returns the string containing the
751 // resulting text representation. Notice that only some of format
752 // specifiers valid for wxDateTime are valid for wxTimeSpan: hours,
753 // minutes and seconds make sense, but not "PM/AM" string for example.
754 wxString
Format(const char *format
= "%c") const;
755 // preferred date representation for the current locale
756 wxString
FormatDate() const { return Format("%x"); }
757 // preferred time representation for the current locale
758 wxString
FormatTime() const { return Format("%X"); }
761 // ------------------------------------------------------------------------
763 // get the internal representation
764 wxLongLong
GetValue() const { return m_diff
; }
767 // the (signed) time span in milliseconds
771 // ----------------------------------------------------------------------------
772 // This class is a "logical time span" and is useful for implementing program
773 // logic for such things as "add one month to the date" which, in general,
774 // doesn't mean to add 60*60*24*31 seconds to it, but to take the same date
775 // the next month (to understand that this is indeed different consider adding
776 // one month to Feb, 15 - we want to get Mar, 15, of course).
778 // When adding a month to the date, all lesser components (days, hours, ...)
781 // wxDateSpan can be either positive or negative. They may be
782 // multiplied by scalars which multiply all deltas by the scalar: i.e. 2*(1
783 // month and 1 day) is 2 months and 2 days. They can be added together and
784 // with wxDateTime or wxTimeSpan, but the type of result is different for each
787 // Beware about weeks: if you specify both weeks and days, the total number of
788 // days added will be 7*weeks + days! See also GetTotalDays() function.
790 // Finally, notice that for adding hours, minutes &c you don't need this
791 // class: wxTimeSpan will do the job because there are no subtleties
792 // associated with those.
793 // ----------------------------------------------------------------------------
794 class WXDLLEXPORT wxDateSpan
798 // ------------------------------------------------------------------------
800 // this many years/months/weeks/days
801 wxDateSpan(int years
, int months
, int weeks
, int days
);
803 // default copy ctor is ok
807 // accessors (all SetXXX() return the (modified) wxDateSpan object)
808 // ------------------------------------------------------------------------
810 // set number of years
811 wxDateSpan
& SetYears(int n
) { m_years
= n
; return this; }
812 // set number of months
813 wxDateSpan
& SetMonths(int n
) { m_months
= n
; return this; }
814 // set number of weeks
815 wxDateSpan
& SetWeeks(int n
) { m_weeks
= n
; return this; }
816 // set number of days
817 wxDateSpan
& SetDays(int n
) { m_days
= n
; return this; }
819 // get number of years
820 int GetYears() const { return m_years
; }
821 // get number of months
822 int GetMonths() const { return m_months
; }
823 // get number of weeks
824 int GetWeeks() const { return m_weeks
; }
825 // get number of days
826 int GetDays() const { return m_days
; }
827 // returns 7*GetWeeks() + GetDays()
828 int GetTotalDays() const { return 7*m_weeks
+ m_days
; }
831 // ------------------------------------------------------------------------
833 // add another wxDateSpan to us
834 inline wxDateSpan
& Add(const wxDateSpan
& other
);
835 // add another wxDateSpan to us
836 inline wxDateSpan
& operator+=(const wxDateSpan
& other
);
838 // substract another wxDateSpan from us
839 inline wxDateSpan
& Substract(const wxDateSpan
& other
);
840 // substract another wxDateSpan from us
841 inline wxDateSpan
& operator-=(const wxDateSpan
& other
);
843 // return a copy of this time span with changed sign
844 inline wxDateSpan
Negate() const;
845 // inverse the sign of this timespan
846 inline wxDateSpan
& Neg();
847 // inverse the sign of this timespan
848 wxDateSpan
& operator-() { return Neg(); }
850 // multiply all components by a (signed) number
851 inline wxDateSpan
& operator*=(int factor
);
860 WXDLLEXPORT_DATA(extern wxDateSpan
) wxYear
;
861 WXDLLEXPORT_DATA(extern wxDateSpan
) wxMonth
;
862 WXDLLEXPORT_DATA(extern wxDateSpan
) wxWeek
;
863 WXDLLEXPORT_DATA(extern wxDateSpan
) wxDay
;
865 // ============================================================================
867 // ============================================================================
869 // ----------------------------------------------------------------------------
870 // wxDateTime operators
871 // ----------------------------------------------------------------------------
873 inline bool WXDLLEXPORT
operator<(const wxDateTime
&t1
, const wxDateTime
&t2
)
875 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
877 return t1
.GetValue() < t2
.GetValue()
880 inline bool WXDLLEXPORT
operator<=(const wxDateTime
&t1
, const wxDateTime
&t2
)
882 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
884 return t1
.GetValue() <= t2
.GetValue()
887 inline bool WXDLLEXPORT
operator>(const wxDateTime
&t1
, const wxDateTime
&t2
)
889 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
891 return t1
.GetValue() > t2
.GetValue()
894 inline bool WXDLLEXPORT
operator>=(const wxDateTime
&t1
, const wxDateTime
&t2
)
896 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
898 return t1
.GetValue() >= t2
.GetValue()
901 inline bool WXDLLEXPORT
operator==(const wxDateTime
&t1
, const wxDateTime
&t2
)
903 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
905 return t1
.GetValue() == t2
.GetValue()
908 inline bool WXDLLEXPORT
operator!=(const wxDateTime
&t1
, const wxDateTime
&t2
)
910 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
912 return t1
.GetValue() != t2
.GetValue()
915 inline wxTimeSpan WXDLLEXPORT
operator-(const wxDateTime
&t1
,
916 const wxDateTime
&t2
)
918 wxASSERT_MSG( t1
.IsValid() && t2
.IsValid(), "invalid wxDateTime" );
920 return wxTimeSpan(t1
.GetValue() - t2
.GetValue());
923 // ----------------------------------------------------------------------------
924 // wxTimeSpan operators
925 // ----------------------------------------------------------------------------
927 inline bool WXDLLEXPORT
operator<(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
929 return t1
.GetValue() < t2
.GetValue()
932 inline bool WXDLLEXPORT
operator<=(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
934 return t1
.GetValue() <= t2
.GetValue()
937 inline bool WXDLLEXPORT
operator>(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
939 return t1
.GetValue() > t2
.GetValue()
942 inline bool WXDLLEXPORT
operator>=(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
944 return t1
.GetValue() >= t2
.GetValue()
947 inline bool WXDLLEXPORT
operator==(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
949 return t1
.GetValue() == t2
.GetValue()
952 inline bool WXDLLEXPORT
operator!=(const wxTimeSpan
&t1
, const wxTimeSpan
&t2
)
954 return t1
.GetValue() != t2
.GetValue();
957 // ----------------------------------------------------------------------------
959 // ----------------------------------------------------------------------------
961 inline WXDLLEXPORT wxDateSpan
operator+(const wxDateSpan
& rt1
,
962 const wxDateSpan
& rt2
)
964 return wxDateSpan(rt1
.m_years
+ rt2
.m_years
,
965 rt1
.m_months
+ rt2
.m_months
,
966 rt1
.m_weeks
+ rt2
.m_weeks
,
967 rt1
.m_days
+ rt2
.m_days
);
970 // ============================================================================
971 // inline functions implementation
972 // ============================================================================
974 // ----------------------------------------------------------------------------
976 // ----------------------------------------------------------------------------
978 const int wxDateTime::TIME_T_FACTOR
= 1000;
980 wxDateTime
& Set(time_t timet
)
982 m_time
= timet
* wxDateTime::TIME_T_FACTOR
;
987 wxDateTime
& SetToCurrent()
989 return Set(GetTimeNow());
992 wxDateTime::wxDateTime(time_t timet
)
997 wxDateTime::wxDateTime(const struct tm
& tm
)
1002 wxDateTime::wxDateTime(wxDateTime_t hour
,
1003 wxDateTime_t minute
,
1004 wxDateTime_t second
,
1005 wxDateTime_t millisec
)
1007 Set(hour
, minute
, second
, millisec
);
1010 wxDateTime::wxDateTime(wxDateTime_t day
,
1014 wxDateTime_t minute
,
1015 wxDateTime_t second
,
1016 wxDateTime_t millisec
)
1018 Set(day
, month
, year
, hour
, minute
, second
, millisec
);
1021 wxLongLong
wxDateTime::GetValue() const
1023 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
1028 bool wxDateTime::SetToLastWeekDay(WeekDay weekday
,
1032 SetToWeekDay(weekday
, -1, month
, year
);
1035 wxDateTime
& wxDateTime::Substract(const wxTimeSpan
& diff
)
1037 return Add(diff
.Negate());
1040 wxDateTime
& wxDateTime::operator-=(const wxTimeSpan
& diff
)
1042 return Add(diff
.Negate());
1045 wxDateTime
& wxDateTime::Substract(const wxDateSpan
& diff
)
1047 return Add(diff
.Negate());
1050 wxDateTime
& wxDateTime::operator-=(const wxDateSpan
& diff
)
1052 return Add(diff
.Negate());
1055 wxTimeSpan
wxDateTime::operator-(const wxDateTime
& datetime
) const
1057 return Substract(datetime
);
1060 // ----------------------------------------------------------------------------
1062 // ----------------------------------------------------------------------------
1064 wxTimeSpan
& wxTimeSpan::Add(const wxTimeSpan
& diff
)
1066 m_diff
+= diff
.GetValue();
1071 wxTimeSpan
& wxTimeSpan::Substract(const wxTimeSpan
& diff
)
1073 m_diff
-= diff
.GetValue();
1078 wxTimeSpan
& wxTimeSpan::Multiply(int n
)
1085 wxTimeSpan
wxTimeSpan::operator*(int n
) const
1087 wxTimeSpan
result(*this);
1093 wxTimeSpan
wxTimeSpan::Abs() const
1095 return wxTimeSpan(GetValue().Abs());
1098 bool wxTimeSpan::IsEqualTo(const wxTimeSpan
& ts
) const
1100 return GetValue() == ts
.GetValue();
1103 bool wxTimeSpan::IsLongerThan(const wxTimeSpan
& ts
) const
1105 return Abs() > ts
.Abs();
1108 // ----------------------------------------------------------------------------
1110 // ----------------------------------------------------------------------------
1113 wxDateSpan::operator+=(const wxDateSpan
& other
)
1115 m_years
+= other
.m_years
;
1116 m_months
+= other
.m_months
;
1117 m_weeks
+= other
.m_weeks
;
1118 m_days
+= other
.m_days
;
1123 wxDateSpan
& wxDateSpan::operator*=(int factor
)
1126 m_months
*= m_months
;
1133 wxDateSpan
Negate() const
1135 return wxDateSpan(-m_years
, -m_months
, -m_weeks
, -m_days
);
1141 m_months
= -m_months
;
1148 #endif // _WX_TIME_H