]> git.saurik.com Git - wxWidgets.git/blob - include/wx/datetime.h
fixed wxBITMAP for Motif
[wxWidgets.git] / include / wx / datetime.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/datetime.h
3 // Purpose: declarations of time/date related classes (wxDateTime,
4 // wxTimeSpan)
5 // Author: Vadim Zeitlin
6 // Modified by:
7 // Created: 10.02.99
8 // RCS-ID: $Id$
9 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_TIME_H
14 #define _WX_TIME_H
15
16 #ifdef __GNUG__
17 #pragma interface "datetime.h"
18 #endif
19
20 #include <time.h>
21 #include <limits.h> // for INT_MIN
22
23 #include "wx/longlong.h"
24
25 class WXDLLEXPORT wxDateTime;
26 class WXDLLEXPORT wxTimeSpan;
27 class WXDLLEXPORT wxDateSpan;
28
29 /*
30 * TODO Well, everything :-)
31 *
32 * 1. Time zones with minutes (make wxTimeZone a class)
33 * 2. getdate() function like under Solaris
34 * 3. text conversion for wxDateSpan
35 */
36
37 /*
38 The three classes declared in this header represent:
39
40 1. An absolute moment in the time (wxDateTime)
41 2. A difference between two moments in the time, positive or negative
42 (wxTimeSpan)
43 3. A logical difference between two dates expressed in
44 years/months/weeks/days (wxDateSpan)
45
46 The following arithmetic operations are permitted (all others are not):
47
48 addition
49 --------
50
51 wxDateTime + wxTimeSpan = wxDateTime
52 wxDateTime + wxDateSpan = wxDateTime
53 wxTimeSpan + wxTimeSpan = wxTimeSpan
54 wxDateSpan + wxDateSpan = wxDateSpan
55
56 substraction
57 ------------
58 wxDateTime - wxDateTime = wxTimeSpan
59 wxTimeSpan - wxTimeSpan = wxTimeSpan
60 wxDateSpan - wxDateSpan = wxDateSpan
61
62 multiplication
63 --------------
64 wxTimeSpan * number = wxTimeSpan
65 wxDateSpan * number = wxDateSpan
66
67 unitary minus
68 -------------
69 -wxTimeSpan = wxTimeSpan
70 -wxDateSpan = wxDateSpan
71 */
72
73 // ----------------------------------------------------------------------------
74 // This class represents an absolute moment in the time
75 // ----------------------------------------------------------------------------
76
77 class WXDLLEXPORT wxDateTime
78 {
79 public:
80 // types
81 // ------------------------------------------------------------------------
82
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;
89
90 // the timezones
91 enum TZ
92 {
93 // the time in the current time zone
94 Local,
95
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
99
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,
103 GMT0,
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
108
109 // Universal Coordinated Time
110 UTC = GMT0
111
112 // TODO add symbolic names for TZ (EST, MET, ...)?
113 };
114
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
118 // calendar
119 enum Calendar
120 {
121 Gregorian, // current calendar
122 Julian // calendar in use since -45 until the 1582 (or later)
123
124 // TODO Hebrew, Chinese, Maya, ... (just kidding) (or then may be not?)
125 };
126
127 // these values only are used to identify the different dates of
128 // adoption of the Gregorian calendar (see IsGregorian())
129 //
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".
133 //
134 // Symbol "->" should be read as "was followed by" in the comments
135 // which follow.
136 enum GregorianAdoption
137 {
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
140
141 Gr_Alaska, // Oct 1867 when Alaska became part of the USA
142 Gr_Albania, // Dec 1912
143
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,
150
151 Gr_Belgium, // Then part of the Netherlands
152
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
157
158 Gr_Canada = Gr_Unknown, // Different regions followed the changes in
159 // Great Britain or France
160
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
164
165 Gr_Czechoslovakia, // (Bohemia and Moravia) 6 Jan 1584 -> 17 Jan 1584
166 Gr_Denmark, // (including Norway) 18 Feb 1700 -> 1 Mar 1700
167 Gr_Egypt, // 1875
168 Gr_Estonia, // 1918
169 Gr_Finland, // Then part of Sweden
170
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
175
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
180
181 Gr_GreatBritain, // 2 Sep 1752 -> 14 Sep 1752 (use 'cal(1)')
182
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,
187
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
192
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
197
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
207
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,
215
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
221 // nobody!)
222 //
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.
230 //
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
234
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
238
239 Gr_Turkey, // 1 Jan 1927
240 Gr_USA = Gr_GreatBritain,
241 Gr_Wales = Gr_GreatBritain,
242 Gr_Yugoslavia // 1919
243 };
244
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
247 // day or not
248 //
249 // TODO move this to intl.h
250 enum Country
251 {
252 Country_Unknown, // no special information for this country
253 Country_Default, // set the default country with SetCountry() method
254
255 // TODO add more countries (for this we must know about DST and/or
256 // holidays for this country)
257 France,
258 USA
259 };
260
261 // symbolic names for the months
262 enum Month
263 {
264 Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec, Inv_Month
265 };
266
267 // symbolic names for the weekdays
268 enum WeekDay
269 {
270 Sun, Mon, Tue, Wed, Thu, Fri, Sat, Inv_WeekDay
271 };
272
273 // invalid value for the year
274 enum Year
275 {
276 Inv_Year = SHRT_MIN // should hold in wxDateTime_t
277 };
278
279 // helper classes
280 // ------------------------------------------------------------------------
281
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
285 struct Tm
286 {
287 wxDateTime_t sec, min, hour,
288 mday, mon, year;
289
290 // default ctor inits the object to an invalid value
291 Tm();
292
293 // ctor from struct tm
294 Tm(const struct tm& tm);
295
296 // check that the given date/time is valid (in Gregorian calendar)
297 bool IsValid() const;
298
299 // get the week day
300 WeekDay GetWeekDay() // not const because wday may be changed
301 {
302 if ( wday == Inv_WeekDay )
303 ComputeWeekDay();
304
305 return (WeekDay)wday;
306 }
307
308 private:
309 // compute the weekday from other fields
310 void ComputeWeekDay();
311
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;
315 };
316
317 // a class representing a time zone: basicly, this is just an offset
318 // (in minutes) from GMT
319 class TimeZone
320 {
321 public:
322 TimeZone(TZ tz);
323 TimeZone(wxDateTime_t offset) { m_offset = offset; }
324
325 wxDateTime_t GetOffset() const { return m_offset; }
326
327 private:
328 // offset for this timezone from GMT in minutes
329 wxDateTime_t m_offset;
330 };
331
332 // static methods
333 // ------------------------------------------------------------------------
334
335 // set the current country
336 static void SetCountry(Country country);
337 // get the current country
338 static inline Country GetCountry();
339
340 // return the current year
341 static int GetCurrentYear(Calendar cal = Gregorian);
342
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);
348
349 // return the current month
350 static Month GetCurrentMonth(Calendar cal = Gregorian);
351
352 // returns TRUE if the given year is a leap year in the given calendar
353 static bool IsLeapYear(int year, Calendar cal = Gregorian);
354
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);
358
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,
362 int year = Inv_Year,
363 Calendar cal = Gregorian);
364
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);
368
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);
372
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);
381
382 // return the wxDateTime object for the current time
383 static inline wxDateTime Now();
384
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.
388 //
389 // All new wxDateTime correspond to the local time, use ToUTC() or
390 // MakeUTC() to get the time in UTC/GMT.
391 // ------------------------------------------------------------------------
392
393 // default ctor does not initialize the object, use Set()!
394 wxDateTime() { }
395
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);
402
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);
416
417 // default copy ctor ok
418
419 // no dtor
420
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 // ------------------------------------------------------------------------
425
426 // set to the current time
427 inline wxDateTime& SetToCurrent();
428
429 // set to given time_t value
430 inline wxDateTime& Set(time_t timet);
431
432 // set to given broken down time/date
433 wxDateTime& Set(const struct tm& tm);
434
435 // set to given broken down time/date
436 inline wxDateTime& Set(const Tm& tm);
437
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);
443
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);
453
454 // resets time to 00:00:00, doesn't change the date
455 wxDateTime& ResetTime();
456
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
459
460 // set the year
461 wxDateTime& SetYear(int year);
462 // set the month
463 wxDateTime& SetMonth(Month month);
464 // set the day of the month
465 wxDateTime& SetDay(wxDateTime_t day);
466 // set hour
467 wxDateTime& SetHour(wxDateTime_t hour);
468 // set minute
469 wxDateTime& SetMinute(wxDateTime_t minute);
470 // set second
471 wxDateTime& SetSecond(wxDateTime_t second);
472 // set millisecond
473 wxDateTime& SetMillisecond(wxDateTime_t millisecond);
474
475 // assignment operator from time_t
476 wxDateTime& operator=(time_t timet) { return Set(timet); }
477
478 // assignment operator from broken down time/date
479 wxDateTime& operator=(const struct tm& tm) { return Set(tm); }
480
481 // assignment operator from broken down time/date
482 wxDateTime& operator=(const Tm& tm) { return Set(tm); }
483
484 // default assignment operator is ok
485
486 // calendar calculations (functions which set the date only leave the time
487 // unchanged, e.g. don't explictly zero it)
488 // ------------------------------------------------------------------------
489
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,
495 int n = 1,
496 wxDateTime_t month = Inv_Month,
497 int year = Inv_Year);
498
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);
503
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.
506 // numWeek is > 53)
507 bool SetToTheWeek(wxDateTime_t numWeek, WeekDay weekday = Mon);
508
509 // get the century (19 for 1999, 20 for 2000 and -5 for 492 BC)
510 int GetCentury() const;
511
512 // The definitions below were taken verbatim from
513 //
514 // http://www.capecod.net/~pbaum/date/date0.htm
515 //
516 // (Peter Baum's home page)
517 //
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.
529 //
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.
533
534 // get the Julian Day number
535 long GetJulianDay() const;
536
537 // get the Rata Die number
538 long GetRataDie() const;
539
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
543
544 // timezone stuff: by default, we always work with local times, to get
545 // anything else, it should be requested explicitly
546 // ------------------------------------------------------------------------
547
548 // get the time corresponding to this one in UTC/GMT
549 wxDateTime ToUTC() const;
550 wxDateTime ToGMT() const { return ToUTC(); }
551
552 // transform this object to UTC/GMT
553 wxDateTime& MakeUTC();
554 wxDateTime& MakeGMT() { return MakeUTC(); }
555
556 // generic version: transform time to any given timezone
557 wxDateTime ToTimezone(const TimeZone& tz);
558 wxDateTime& MakeTimezone(const TimeZone& tz);
559
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 // ------------------------------------------------------------------------
566
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; }
570
571 // get the broken down date/time representation
572 Tm GetTm() const;
573
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;
577
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; }
588 // get the minute
589 wxDateTime_t GetMinute() const { return GetTm().min; }
590 // get the second
591 wxDateTime_t GetSecond() const { return GetTm().sec; }
592 // get milliseconds
593 wxDateTime_t GetMillisecond() const { return m_time.GetLo() % 1000; }
594
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
598 // invalid)
599 wxDateTime_t GetWeekOfYear() const;
600
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;
605
606 // is this date later than Gregorian calendar introduction for the
607 // given country (see enum GregorianAdoption)?
608 //
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;
613
614 // is daylight savings time in effect at this moment?
615 //
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;
619
620 // comparison (see also functions below for operator versions)
621 // ------------------------------------------------------------------------
622
623 // returns TRUE if the two moments are strictly identical
624 inline bool IsEqualTo(const wxDateTime& datetime) const;
625
626 // returns TRUE if the two moments are identical
627 inline bool operator==(const wxDateTime& datetime) const;
628
629 // returns TRUE if the two moments are different
630 inline bool operator!=(const wxDateTime& datetime) const;
631
632 // returns TRUE if the date is strictly earlier than the given one
633 inline bool IsEarlierThan(const wxDateTime& datetime) const;
634
635 // returns TRUE if the date is strictly later than the given one
636 inline bool IsLaterThan(const wxDateTime& datetime) const;
637
638 // returns TRUE if the date is strictly in the given range
639 inline bool IsStrictlyBetween(const wxDateTime& t1,
640 const wxDateTime& t2) const;
641
642 // returns TRUE if the date is in the given range
643 inline bool IsBetween(const wxDateTime& t1, const wxDateTime& t2) const;
644
645 // date operations: for the non-const methods, the return value is this
646 // object itself (see also functions below for operator versions)
647 // ------------------------------------------------------------------------
648
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);
653
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);
658
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);
663
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);
668
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;
673
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 // ------------------------------------------------------------------------
677
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);
687
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")); }
697
698 // implementation
699 // ------------------------------------------------------------------------
700
701 // get the internal representation
702 inline wxLongLong GetValue() const;
703
704 private:
705 // a helper function to get the current time_t
706 static inline time_t GetTimeNow() { return time((time_t *)NULL); }
707
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;
712
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
715 // fixed to 1000
716 static const unsigned int TIME_T_FACTOR;
717
718 // invalid wxDateTime object - returned by all functions which return
719 // "wxDateTime &" on failure
720 static wxDateTime ms_InvDateTime;
721
722 // returns TRUE if we fall in range in which we can use standard ANSI C
723 // functions
724 inline IsInStdRange() const;
725
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)
729 wxLongLong m_time;
730 };
731
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 // ----------------------------------------------------------------------------
737
738 class WXDLLEXPORT wxTimeSpan
739 {
740 public:
741 // constructors
742 // ------------------------------------------------------------------------
743
744 // default ctor constructs the 0 time span
745 wxTimeSpan() { }
746
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
749 // milliseconds)
750 wxTimeSpan(int hours,
751 int minutes = 0,
752 int seconds = 0,
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,
757 int months,
758 int days,
759 int hours = 0,
760 int minutes = 0,
761 int seconds = 0,
762 int milliseconds = 0);
763
764 // from internal representation
765 wxTimeSpan(wxLongLong diff) : m_diff(diff) { }
766
767 // default copy ctor is ok
768
769 // no dtor
770
771 // arithmetics with time spans
772 // ------------------------------------------------------------------------
773
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); }
778
779 // substract another timespan
780 inline wxTimeSpan& Substract(const wxTimeSpan& diff);
781 // substract another timespan
782 wxTimeSpan& operator-=(const wxTimeSpan& diff) { return Substract(diff); }
783
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;
790
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(); }
797
798 // return the absolute value of the timespan: does _not_ modify the
799 // object
800 inline wxTimeSpan Abs() const;
801
802 // there is intentionally no division because we don't want to
803 // introduce rounding errors in time calculations
804
805 // comparaison (see also operator versions below)
806 // ------------------------------------------------------------------------
807
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(); }
812
813 // is the timespan positive?
814 bool IsPositive() const { return m_diff > 0l; }
815
816 // is the timespan negative?
817 bool IsNegative() const { return m_diff < 0l; }
818
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); }
829
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 // ------------------------------------------------------------------------
835
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; }
852
853 // conversion to text
854 // ------------------------------------------------------------------------
855
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"); }
867
868 // implementation
869 // ------------------------------------------------------------------------
870
871 // get the internal representation
872 wxLongLong GetValue() const { return m_diff; }
873
874 private:
875 // the (signed) time span in milliseconds
876 wxLongLong m_diff;
877 };
878
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).
885 //
886 // When adding a month to the date, all lesser components (days, hours, ...)
887 // won't be changed.
888 //
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
893 // case.
894 //
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.
897 //
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 // ----------------------------------------------------------------------------
902
903 class WXDLLEXPORT wxDateSpan
904 {
905 public:
906 // constructors
907 // ------------------------------------------------------------------------
908
909 // this many years/months/weeks/days
910 wxDateSpan(int years, int months, int weeks, int days);
911
912 // default copy ctor is ok
913
914 // no dtor
915
916 // accessors (all SetXXX() return the (modified) wxDateSpan object)
917 // ------------------------------------------------------------------------
918
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; }
927
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; }
938
939 // arithmetics
940 // ------------------------------------------------------------------------
941
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);
946
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);
951
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(); }
958
959 // multiply all components by a (signed) number
960 inline wxDateSpan& operator*=(int factor);
961
962 private:
963 int m_years,
964 m_months,
965 m_weeks,
966 m_days;
967 };
968
969 WXDLLEXPORT_DATA(extern wxDateSpan) wxYear;
970 WXDLLEXPORT_DATA(extern wxDateSpan) wxMonth;
971 WXDLLEXPORT_DATA(extern wxDateSpan) wxWeek;
972 WXDLLEXPORT_DATA(extern wxDateSpan) wxDay;
973
974 // ============================================================================
975 // binary operators
976 // ============================================================================
977
978 // ----------------------------------------------------------------------------
979 // wxDateTime operators
980 // ----------------------------------------------------------------------------
981
982 inline bool WXDLLEXPORT operator<(const wxDateTime &t1, const wxDateTime &t2)
983 {
984 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), "invalid wxDateTime" );
985
986 return t1.GetValue() < t2.GetValue();
987 }
988
989 inline bool WXDLLEXPORT operator<=(const wxDateTime &t1, const wxDateTime &t2)
990 {
991 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), "invalid wxDateTime" );
992
993 return t1.GetValue() <= t2.GetValue();
994 }
995
996 inline bool WXDLLEXPORT operator>(const wxDateTime &t1, const wxDateTime &t2)
997 {
998 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), "invalid wxDateTime" );
999
1000 return t1.GetValue() > t2.GetValue();
1001 }
1002
1003 inline bool WXDLLEXPORT operator>=(const wxDateTime &t1, const wxDateTime &t2)
1004 {
1005 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), "invalid wxDateTime" );
1006
1007 return t1.GetValue() >= t2.GetValue();
1008 }
1009
1010 inline bool WXDLLEXPORT operator==(const wxDateTime &t1, const wxDateTime &t2)
1011 {
1012 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), "invalid wxDateTime" );
1013
1014 return t1.GetValue() == t2.GetValue();
1015 }
1016
1017 inline bool WXDLLEXPORT operator!=(const wxDateTime &t1, const wxDateTime &t2)
1018 {
1019 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), "invalid wxDateTime" );
1020
1021 return t1.GetValue() != t2.GetValue();
1022 }
1023
1024 inline wxTimeSpan WXDLLEXPORT operator-(const wxDateTime &t1,
1025 const wxDateTime &t2)
1026 {
1027 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), "invalid wxDateTime" );
1028
1029 return wxTimeSpan(t1.GetValue() - t2.GetValue());
1030 }
1031
1032 // ----------------------------------------------------------------------------
1033 // wxTimeSpan operators
1034 // ----------------------------------------------------------------------------
1035
1036 inline wxTimeSpan WXDLLEXPORT operator+(const wxTimeSpan& ts1,
1037 const wxTimeSpan& ts2)
1038 {
1039 return wxTimeSpan(ts1.GetValue() + ts2.GetValue());
1040 }
1041
1042 inline wxTimeSpan WXDLLEXPORT operator-(const wxTimeSpan& ts1,
1043 const wxTimeSpan& ts2)
1044 {
1045 return wxTimeSpan(ts1.GetValue() - ts2.GetValue());
1046 }
1047
1048 inline bool WXDLLEXPORT operator<(const wxTimeSpan &t1, const wxTimeSpan &t2)
1049 {
1050 return t1.GetValue() < t2.GetValue();
1051 }
1052
1053 inline bool WXDLLEXPORT operator<=(const wxTimeSpan &t1, const wxTimeSpan &t2)
1054 {
1055 return t1.GetValue() <= t2.GetValue();
1056 }
1057
1058 inline bool WXDLLEXPORT operator>(const wxTimeSpan &t1, const wxTimeSpan &t2)
1059 {
1060 return t1.GetValue() > t2.GetValue();
1061 }
1062
1063 inline bool WXDLLEXPORT operator>=(const wxTimeSpan &t1, const wxTimeSpan &t2)
1064 {
1065 return t1.GetValue() >= t2.GetValue();
1066 }
1067
1068 inline bool WXDLLEXPORT operator==(const wxTimeSpan &t1, const wxTimeSpan &t2)
1069 {
1070 return t1.GetValue() == t2.GetValue();
1071 }
1072
1073 inline bool WXDLLEXPORT operator!=(const wxTimeSpan &t1, const wxTimeSpan &t2)
1074 {
1075 return t1.GetValue() != t2.GetValue();
1076 }
1077
1078 // ----------------------------------------------------------------------------
1079 // wxDateSpan
1080 // ----------------------------------------------------------------------------
1081
1082 inline WXDLLEXPORT wxDateSpan operator+(const wxDateSpan& rt1,
1083 const wxDateSpan& rt2)
1084 {
1085 return wxDateSpan(rt1.GetYears() + rt2.GetYears(),
1086 rt1.GetMonths() + rt2.GetMonths(),
1087 rt1.GetWeeks() + rt2.GetWeeks(),
1088 rt1.GetDays() + rt2.GetDays());
1089 }
1090
1091 // ============================================================================
1092 // inline functions implementation
1093 // ============================================================================
1094
1095 // ----------------------------------------------------------------------------
1096 // wxDateTime statics
1097 // ----------------------------------------------------------------------------
1098
1099 /* static */
1100 wxDateTime::Country wxDateTime::GetCountry()
1101 {
1102 return ms_country;
1103 }
1104
1105 // ----------------------------------------------------------------------------
1106 // wxDateTime construction
1107 // ----------------------------------------------------------------------------
1108
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
1113
1114 wxDateTime::IsInStdRange() const
1115 {
1116 return m_time >= 0l && (m_time / (long)TIME_T_FACTOR) < LONG_MAX;
1117 }
1118
1119 /* static */
1120 wxDateTime wxDateTime::Now()
1121 {
1122 return wxDateTime(GetTimeNow());
1123 }
1124
1125 wxDateTime& wxDateTime::Set(time_t timet)
1126 {
1127 m_time = timet * TIME_T_FACTOR;
1128
1129 return *this;
1130 }
1131
1132 wxDateTime& wxDateTime::SetToCurrent()
1133 {
1134 return Set(GetTimeNow());
1135 }
1136
1137 wxDateTime::wxDateTime(time_t timet)
1138 {
1139 Set(timet);
1140 }
1141
1142 wxDateTime::wxDateTime(const struct tm& tm)
1143 {
1144 Set(tm);
1145 }
1146
1147 wxDateTime::wxDateTime(const Tm& tm)
1148 {
1149 Set(tm);
1150 }
1151
1152 wxDateTime& wxDateTime::Set(const Tm& tm)
1153 {
1154 wxASSERT_MSG( tm.IsValid(), _T("invalid broken down date/time") );
1155
1156 return Set(tm.mday, (Month)tm.mon, tm.year, tm.hour, tm.min, tm.sec);
1157 }
1158
1159 wxDateTime::wxDateTime(wxDateTime_t hour,
1160 wxDateTime_t minute,
1161 wxDateTime_t second,
1162 wxDateTime_t millisec)
1163 {
1164 Set(hour, minute, second, millisec);
1165 }
1166
1167 wxDateTime::wxDateTime(wxDateTime_t day,
1168 Month month,
1169 int year,
1170 wxDateTime_t hour,
1171 wxDateTime_t minute,
1172 wxDateTime_t second,
1173 wxDateTime_t millisec)
1174 {
1175 Set(day, month, year, hour, minute, second, millisec);
1176 }
1177
1178 // ----------------------------------------------------------------------------
1179 // wxDateTime accessors
1180 // ----------------------------------------------------------------------------
1181
1182 wxLongLong wxDateTime::GetValue() const
1183 {
1184 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
1185
1186 return m_time;
1187 }
1188
1189 time_t wxDateTime::GetTicks() const
1190 {
1191 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
1192 if ( !IsInStdRange() )
1193 {
1194 return (time_t)-1;
1195 }
1196
1197 return (time_t)((m_time / (long)TIME_T_FACTOR).GetLo());
1198 }
1199
1200 bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
1201 wxDateTime_t month,
1202 int year)
1203 {
1204 SetToWeekDay(weekday, -1, month, year);
1205 }
1206
1207 // ----------------------------------------------------------------------------
1208 // wxDateTime comparison
1209 // ----------------------------------------------------------------------------
1210
1211 bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
1212 {
1213 wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime");
1214
1215 return m_time == datetime.m_time;
1216 }
1217
1218 bool wxDateTime::operator==(const wxDateTime& datetime) const
1219 {
1220 return IsEqualTo(datetime);
1221 }
1222
1223 bool wxDateTime::operator!=(const wxDateTime& datetime) const
1224 {
1225 return !IsEqualTo(datetime);
1226 }
1227
1228 bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
1229 {
1230 wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime");
1231
1232 return m_time < datetime.m_time;
1233 }
1234
1235 bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
1236 {
1237 wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime");
1238
1239 return m_time > datetime.m_time;
1240 }
1241
1242 bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1,
1243 const wxDateTime& t2) const
1244 {
1245 // no need for assert, will be checked by the functions we call
1246 return IsLaterThan(t1) && IsEarlierThan(t2);
1247 }
1248
1249 bool wxDateTime::IsBetween(const wxDateTime& t1, const wxDateTime& t2) const
1250 {
1251 // no need for assert, will be checked by the functions we call
1252 return IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2);
1253 }
1254
1255 // ----------------------------------------------------------------------------
1256 // wxDateTime arithmetics
1257 // ----------------------------------------------------------------------------
1258
1259 wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
1260 {
1261 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
1262
1263 m_time += diff.GetValue();
1264
1265 return *this;
1266 }
1267
1268 wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
1269 {
1270 return Add(diff);
1271 }
1272
1273 wxDateTime& wxDateTime::Substract(const wxTimeSpan& diff)
1274 {
1275 wxASSERT_MSG( IsValid(), "invalid wxDateTime");
1276
1277 m_time -= diff.GetValue();
1278
1279 return *this;
1280 }
1281
1282 wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
1283 {
1284 return Substract(diff);
1285 }
1286
1287 wxTimeSpan wxDateTime::Substract(const wxDateTime& datetime) const
1288 {
1289 wxASSERT_MSG( IsValid() && datetime.IsValid(), "invalid wxDateTime");
1290
1291 return wxTimeSpan(datetime.GetValue() - GetValue());
1292 }
1293
1294 wxTimeSpan wxDateTime::operator-(const wxDateTime& datetime) const
1295 {
1296 return Substract(datetime);
1297 }
1298
1299 wxDateTime& wxDateTime::Substract(const wxDateSpan& diff)
1300 {
1301 return Add(diff.Negate());
1302 }
1303
1304 wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
1305 {
1306 return Substract(diff);
1307 }
1308
1309 wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
1310 {
1311 return Add(diff);
1312 }
1313
1314 // ----------------------------------------------------------------------------
1315 // wxTimeSpan
1316 // ----------------------------------------------------------------------------
1317
1318 wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
1319 {
1320 m_diff += diff.GetValue();
1321
1322 return *this;
1323 }
1324
1325 wxTimeSpan& wxTimeSpan::Substract(const wxTimeSpan& diff)
1326 {
1327 m_diff -= diff.GetValue();
1328
1329 return *this;
1330 }
1331
1332 wxTimeSpan& wxTimeSpan::Multiply(int n)
1333 {
1334 m_diff *= n;
1335
1336 return *this;
1337 }
1338
1339 wxTimeSpan wxTimeSpan::operator*(int n) const
1340 {
1341 wxTimeSpan result(*this);
1342 result.Multiply(n);
1343
1344 return result;
1345 }
1346
1347 wxTimeSpan wxTimeSpan::Abs() const
1348 {
1349 return wxTimeSpan(GetValue().Abs());
1350 }
1351
1352 bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
1353 {
1354 return GetValue() == ts.GetValue();
1355 }
1356
1357 bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
1358 {
1359 return Abs() > ts.Abs();
1360 }
1361
1362 // ----------------------------------------------------------------------------
1363 // wxDateSpan
1364 // ----------------------------------------------------------------------------
1365
1366 wxDateSpan&
1367 wxDateSpan::operator+=(const wxDateSpan& other)
1368 {
1369 m_years += other.m_years;
1370 m_months += other.m_months;
1371 m_weeks += other.m_weeks;
1372 m_days += other.m_days;
1373
1374 return *this;
1375 }
1376
1377 wxDateSpan& wxDateSpan::operator*=(int factor)
1378 {
1379 m_years *= m_years;
1380 m_months *= m_months;
1381 m_weeks *= m_weeks;
1382 m_days *= m_days;
1383
1384 return *this;
1385 }
1386
1387 wxDateSpan wxDateSpan::Negate() const
1388 {
1389 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
1390 }
1391
1392 wxDateSpan& wxDateSpan::Neg()
1393 {
1394 m_years = -m_years;
1395 m_months = -m_months;
1396 m_weeks = -m_weeks;
1397 m_days = -m_days;
1398
1399 return *this;
1400 }
1401
1402 #endif // _WX_TIME_H