1. wxLongLongWx::Assign(double) added (half implemented)
[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_DATETIME_H
14 #define _WX_DATETIME_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 // don't use inline functions in debug builds - we don't care about
30 // performances and this only leads to increased rebuild time (because every
31 // time an inline method is changed, all files including the header must be
32 // rebuilt)
33 #ifdef __WXDEBUG__
34 #define inline
35 #endif // Debug
36
37 /*
38 * TODO Well, everything :-)
39 *
40 * + 1. Time zones with minutes (make TimeZone a class)
41 * 2. getdate() function like under Solaris
42 * + 3. text conversion for wxDateSpan
43 * 4. pluggable modules for the workdays calculations
44 */
45
46 /*
47 The three (main) classes declared in this header represent:
48
49 1. An absolute moment in the time (wxDateTime)
50 2. A difference between two moments in the time, positive or negative
51 (wxTimeSpan)
52 3. A logical difference between two dates expressed in
53 years/months/weeks/days (wxDateSpan)
54
55 The following arithmetic operations are permitted (all others are not):
56
57 addition
58 --------
59
60 wxDateTime + wxTimeSpan = wxDateTime
61 wxDateTime + wxDateSpan = wxDateTime
62 wxTimeSpan + wxTimeSpan = wxTimeSpan
63 wxDateSpan + wxDateSpan = wxDateSpan
64
65 substraction
66 ------------
67 wxDateTime - wxDateTime = wxTimeSpan
68 wxDateTime - wxTimeSpan = wxDateTime
69 wxDateTime - wxDateSpan = wxDateTime
70 wxTimeSpan - wxTimeSpan = wxTimeSpan
71 wxDateSpan - wxDateSpan = wxDateSpan
72
73 multiplication
74 --------------
75 wxTimeSpan * number = wxTimeSpan
76 number * wxTimeSpan = wxTimeSpan
77 wxDateSpan * number = wxDateSpan
78 number * wxDateSpan = wxDateSpan
79
80 unitary minus
81 -------------
82 -wxTimeSpan = wxTimeSpan
83 -wxDateSpan = wxDateSpan
84
85 For each binary operation OP (+, -, *) we have the following operatorOP=() as
86 a method and the method with a symbolic name OPER (Add, Substract, Multiply)
87 as a synonym for it and another const method with the same name which returns
88 the changed copy of the object and operatorOP() as a global function which is
89 implemented in terms of the const version of OPEN. For the unary - we have
90 operator-() as a method, Neg() as synonym for it and Negate() which returns
91 the copy of the object with the changed sign.
92 */
93
94 // ----------------------------------------------------------------------------
95 // wxDateTime represents an absolute moment in the time
96 // ----------------------------------------------------------------------------
97
98 class WXDLLEXPORT wxDateTime
99 {
100 public:
101 // types
102 // ------------------------------------------------------------------------
103
104 // a small unsigned integer type for storing things like minutes,
105 // seconds &c. It should be at least short (i.e. not char) to contain
106 // the number of milliseconds - it may also be 'int' because there is
107 // no size penalty associated with it in our code, we don't store any
108 // data in this format
109 typedef unsigned short wxDateTime_t;
110
111 // constants
112 // ------------------------------------------------------------------------
113
114 // the timezones
115 enum TZ
116 {
117 // the time in the current time zone
118 Local,
119
120 // zones from GMT (= Greenwhich Mean Time): they're guaranteed to be
121 // consequent numbers, so writing something like `GMT0 + offset' is
122 // safe if abs(offset) <= 12
123
124 // underscore stands for minus
125 GMT_12, GMT_11, GMT_10, GMT_9, GMT_8, GMT_7,
126 GMT_6, GMT_5, GMT_4, GMT_3, GMT_2, GMT_1,
127 GMT0,
128 GMT1, GMT2, GMT3, GMT4, GMT5, GMT6,
129 GMT7, GMT8, GMT9, GMT10, GMT11, GMT12,
130 // Note that GMT12 and GMT_12 are not the same: there is a difference
131 // of exactly one day between them
132
133 // some symbolic names for TZ
134
135 // Europe
136 WET = GMT0, // Western Europe Time
137 WEST = GMT1, // Western Europe Summer Time
138 CET = GMT1, // Central Europe Time
139 CEST = GMT2, // Central Europe Summer Time
140 EET = GMT2, // Eastern Europe Time
141 EEST = GMT3, // Eastern Europe Summer Time
142 MSK = GMT3, // Moscow Time
143 MSD = GMT4, // Moscow Summer Time
144
145 // US and Canada
146 AST = GMT_4, // Atlantic Standard Time
147 ADT = GMT_3, // Atlantic Daylight Time
148 EST = GMT_5, // Eastern Standard Time
149 EDT = GMT_4, // Eastern Daylight Saving Time
150 CST = GMT_6, // Central Standard Time
151 CDT = GMT_5, // Central Daylight Saving Time
152 MST = GMT_7, // Mountain Standard Time
153 MDT = GMT_6, // Mountain Daylight Saving Time
154 PST = GMT_8, // Pacific Standard Time
155 PDT = GMT_7, // Pacific Daylight Saving Time
156 HST = GMT_10, // Hawaiian Standard Time
157 AKST = GMT_9, // Alaska Standard Time
158 AKDT = GMT_8, // Alaska Daylight Saving Time
159
160 // Australia
161
162 A_WST = GMT8, // Western Standard Time
163 A_CST = GMT12 + 1, // Central Standard Time (+9.5)
164 A_EST = GMT10, // Eastern Standard Time
165 A_ESST = GMT11, // Eastern Summer Time
166
167 // TODO add more symbolic timezone names here
168
169 // Universal Coordinated Time = the new and politically correct name
170 // for GMT
171 UTC = GMT0
172 };
173
174 // the calendar systems we know about: notice that it's valid (for
175 // this classes purpose anyhow) to work with any of these calendars
176 // even with the dates before the historical appearance of the
177 // calendar
178 enum Calendar
179 {
180 Gregorian, // current calendar
181 Julian // calendar in use since -45 until the 1582 (or later)
182
183 // TODO Hebrew, Chinese, Maya, ... (just kidding) (or then may be not?)
184 };
185
186 // these values only are used to identify the different dates of
187 // adoption of the Gregorian calendar (see IsGregorian())
188 //
189 // All data and comments taken verbatim from "The Calendar FAQ (v 2.0)"
190 // by Claus Tøndering, http://www.pip.dknet.dk/~c-t/calendar.html
191 // except for the comments "we take".
192 //
193 // Symbol "->" should be read as "was followed by" in the comments
194 // which follow.
195 enum GregorianAdoption
196 {
197 Gr_Unknown, // no data for this country or it's too uncertain to use
198 Gr_Standard, // on the day 0 of Gregorian calendar: 15 Oct 1582
199
200 Gr_Alaska, // Oct 1867 when Alaska became part of the USA
201 Gr_Albania, // Dec 1912
202
203 Gr_Austria = Gr_Unknown, // Different regions on different dates
204 Gr_Austria_Brixen, // 5 Oct 1583 -> 16 Oct 1583
205 Gr_Austria_Salzburg = Gr_Austria_Brixen,
206 Gr_Austria_Tyrol = Gr_Austria_Brixen,
207 Gr_Austria_Carinthia, // 14 Dec 1583 -> 25 Dec 1583
208 Gr_Austria_Styria = Gr_Austria_Carinthia,
209
210 Gr_Belgium, // Then part of the Netherlands
211
212 Gr_Bulgaria = Gr_Unknown, // Unknown precisely (from 1915 to 1920)
213 Gr_Bulgaria_1, // 18 Mar 1916 -> 1 Apr 1916
214 Gr_Bulgaria_2, // 31 Mar 1916 -> 14 Apr 1916
215 Gr_Bulgaria_3, // 3 Sep 1920 -> 17 Sep 1920
216
217 Gr_Canada = Gr_Unknown, // Different regions followed the changes in
218 // Great Britain or France
219
220 Gr_China = Gr_Unknown, // Different authorities say:
221 Gr_China_1, // 18 Dec 1911 -> 1 Jan 1912
222 Gr_China_2, // 18 Dec 1928 -> 1 Jan 1929
223
224 Gr_Czechoslovakia, // (Bohemia and Moravia) 6 Jan 1584 -> 17 Jan 1584
225 Gr_Denmark, // (including Norway) 18 Feb 1700 -> 1 Mar 1700
226 Gr_Egypt, // 1875
227 Gr_Estonia, // 1918
228 Gr_Finland, // Then part of Sweden
229
230 Gr_France, // 9 Dec 1582 -> 20 Dec 1582
231 Gr_France_Alsace, // 4 Feb 1682 -> 16 Feb 1682
232 Gr_France_Lorraine, // 16 Feb 1760 -> 28 Feb 1760
233 Gr_France_Strasbourg, // February 1682
234
235 Gr_Germany = Gr_Unknown, // Different states on different dates:
236 Gr_Germany_Catholic, // 1583-1585 (we take 1584)
237 Gr_Germany_Prussia, // 22 Aug 1610 -> 2 Sep 1610
238 Gr_Germany_Protestant, // 18 Feb 1700 -> 1 Mar 1700
239
240 Gr_GreatBritain, // 2 Sep 1752 -> 14 Sep 1752 (use 'cal(1)')
241
242 Gr_Greece, // 9 Mar 1924 -> 23 Mar 1924
243 Gr_Hungary, // 21 Oct 1587 -> 1 Nov 1587
244 Gr_Ireland = Gr_GreatBritain,
245 Gr_Italy = Gr_Standard,
246
247 Gr_Japan = Gr_Unknown, // Different authorities say:
248 Gr_Japan_1, // 19 Dec 1872 -> 1 Jan 1873
249 Gr_Japan_2, // 19 Dec 1892 -> 1 Jan 1893
250 Gr_Japan_3, // 18 Dec 1918 -> 1 Jan 1919
251
252 Gr_Latvia, // 1915-1918 (we take 1915)
253 Gr_Lithuania, // 1915
254 Gr_Luxemburg, // 14 Dec 1582 -> 25 Dec 1582
255 Gr_Netherlands = Gr_Belgium, // (including Belgium) 1 Jan 1583
256
257 // this is too weird to take into account: the Gregorian calendar was
258 // introduced twice in Groningen, first time 28 Feb 1583 was followed
259 // by 11 Mar 1583, then it has gone back to Julian in the summer of
260 // 1584 and then 13 Dec 1700 -> 12 Jan 1701 - which is
261 // the date we take here
262 Gr_Netherlands_Groningen, // 13 Dec 1700 -> 12 Jan 1701
263 Gr_Netherlands_Gelderland, // 30 Jun 1700 -> 12 Jul 1700
264 Gr_Netherlands_Utrecht, // (and Overijssel) 30 Nov 1700->12 Dec 1700
265 Gr_Netherlands_Friesland, // (and Drenthe) 31 Dec 1700 -> 12 Jan 1701
266
267 Gr_Norway = Gr_Denmark, // Then part of Denmark
268 Gr_Poland = Gr_Standard,
269 Gr_Portugal = Gr_Standard,
270 Gr_Romania, // 31 Mar 1919 -> 14 Apr 1919
271 Gr_Russia, // 31 Jan 1918 -> 14 Feb 1918
272 Gr_Scotland = Gr_GreatBritain,
273 Gr_Spain = Gr_Standard,
274
275 // Sweden has a curious history. Sweden decided to make a gradual
276 // change from the Julian to the Gregorian calendar. By dropping every
277 // leap year from 1700 through 1740 the eleven superfluous days would
278 // be omitted and from 1 Mar 1740 they would be in sync with the
279 // Gregorian calendar. (But in the meantime they would be in sync with
280 // nobody!)
281 //
282 // So 1700 (which should have been a leap year in the Julian calendar)
283 // was not a leap year in Sweden. However, by mistake 1704 and 1708
284 // became leap years. This left Sweden out of synchronisation with
285 // both the Julian and the Gregorian world, so they decided to go back
286 // to the Julian calendar. In order to do this, they inserted an extra
287 // day in 1712, making that year a double leap year! So in 1712,
288 // February had 30 days in Sweden.
289 //
290 // Later, in 1753, Sweden changed to the Gregorian calendar by
291 // dropping 11 days like everyone else.
292 Gr_Sweden = Gr_Finland, // 17 Feb 1753 -> 1 Mar 1753
293
294 Gr_Switzerland = Gr_Unknown,// Different cantons used different dates
295 Gr_Switzerland_Catholic, // 1583, 1584 or 1597 (we take 1584)
296 Gr_Switzerland_Protestant, // 31 Dec 1700 -> 12 Jan 1701
297
298 Gr_Turkey, // 1 Jan 1927
299 Gr_USA = Gr_GreatBritain,
300 Gr_Wales = Gr_GreatBritain,
301 Gr_Yugoslavia // 1919
302 };
303
304 // the country parameter is used so far for calculating the start and
305 // the end of DST period and for deciding whether the date is a work
306 // day or not
307 //
308 // TODO move this to intl.h
309 enum Country
310 {
311 Country_Unknown, // no special information for this country
312 Country_Default, // set the default country with SetCountry() method
313 // or use the default country with any other
314
315 // TODO add more countries (for this we must know about DST and/or
316 // holidays for this country)
317
318 // Western European countries: we assume that they all follow the same
319 // DST rules (true or false?)
320 Country_WesternEurope_Start,
321 Country_EEC = Country_WesternEurope_Start,
322 France,
323 Germany,
324 UK,
325 Country_WesternEurope_End = UK,
326
327 Russia,
328
329 USA
330 };
331
332 // symbolic names for the months
333 enum Month
334 {
335 Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec, Inv_Month
336 };
337
338 // symbolic names for the weekdays
339 enum WeekDay
340 {
341 Sun, Mon, Tue, Wed, Thu, Fri, Sat, Inv_WeekDay
342 };
343
344 // invalid value for the year
345 enum Year
346 {
347 Inv_Year = SHRT_MIN // should hold in wxDateTime_t
348 };
349
350 // flags for GetWeekDayName and GetMonthName
351 enum NameFlags
352 {
353 Name_Full = 0x01, // return full name
354 Name_Abbr = 0x02 // return abbreviated name
355 };
356
357 // helper classes
358 // ------------------------------------------------------------------------
359
360 // a class representing a time zone: basicly, this is just an offset
361 // (in seconds) from GMT
362 class TimeZone
363 {
364 public:
365 TimeZone(TZ tz);
366 TimeZone(wxDateTime_t offset = 0) { m_offset = offset; }
367
368 long GetOffset() const { return m_offset; }
369
370 private:
371 // offset for this timezone from GMT in seconds
372 long m_offset;
373 };
374
375 // standard struct tm is limited to the years from 1900 (because
376 // tm_year field is the offset from 1900), so we use our own struct
377 // instead to represent broken down time
378 //
379 // NB: this struct should always be kept normalized (i.e. mon should
380 // be < 12, 1 <= day <= 31 &c), so use AddMonths(), AddDays()
381 // instead of modifying the member fields directly!
382 struct Tm
383 {
384 wxDateTime_t msec, sec, min, hour, mday;
385 Month mon;
386 int year;
387
388 // default ctor inits the object to an invalid value
389 Tm();
390
391 // ctor from struct tm and the timezone
392 Tm(const struct tm& tm, const TimeZone& tz);
393
394 // check that the given date/time is valid (in Gregorian calendar)
395 bool IsValid() const;
396
397 // get the week day
398 WeekDay GetWeekDay() // not const because wday may be changed
399 {
400 if ( wday == Inv_WeekDay )
401 ComputeWeekDay();
402
403 return (WeekDay)wday;
404 }
405
406 // add the given number of months to the date keeping it normalized
407 void AddMonths(int monDiff);
408
409 // add the given number of months to the date keeping it normalized
410 void AddDays(int dayDiff);
411
412 private:
413 // compute the weekday from other fields
414 void ComputeWeekDay();
415
416 // the timezone we correspond to
417 TimeZone m_tz;
418
419 // these values can't be accessed directly because they're not always
420 // computed and we calculate them on demand
421 wxDateTime_t wday, yday;
422 };
423
424 // static methods
425 // ------------------------------------------------------------------------
426
427 // set the current country
428 static void SetCountry(Country country);
429 // get the current country
430 static Country GetCountry();
431
432 // return TRUE if the country is a West European one (in practice,
433 // this means that the same DST rules as for EEC apply)
434 static bool IsWestEuropeanCountry(Country country = Country_Default);
435
436 // return the current year
437 static int GetCurrentYear(Calendar cal = Gregorian);
438
439 // convert the year as returned by wxDateTime::GetYear() to a year
440 // suitable for BC/AD notation. The difference is that BC year 1
441 // corresponds to the year 0 (while BC year 0 didn't exist) and AD
442 // year N is just year N.
443 static int ConvertYearToBC(int year);
444
445 // return the current month
446 static Month GetCurrentMonth(Calendar cal = Gregorian);
447
448 // returns TRUE if the given year is a leap year in the given calendar
449 static bool IsLeapYear(int year = Inv_Year, Calendar cal = Gregorian);
450
451 // get the century (19 for 1999, 20 for 2000 and -5 for 492 BC)
452 static int GetCentury(int year = Inv_Year);
453
454 // returns the number of days in this year (356 or 355 for Gregorian
455 // calendar usually :-)
456 static wxDateTime_t GetNumberOfDays(int year, Calendar cal = Gregorian);
457
458 // get the number of the days in the given month (default value for
459 // the year means the current one)
460 static wxDateTime_t GetNumberOfDays(Month month,
461 int year = Inv_Year,
462 Calendar cal = Gregorian);
463
464 // get the full (default) or abbreviated month name in the current
465 // locale, returns empty string on error
466 static wxString GetMonthName(Month month,
467 NameFlags flags = Name_Full);
468
469 // get the full (default) or abbreviated weekday name in the current
470 // locale, returns empty string on error
471 static wxString GetWeekDayName(WeekDay weekday,
472 NameFlags flags = Name_Full);
473
474 // get the AM and PM strings in the current locale (may be empty)
475 static void GetAmPmStrings(wxString *am, wxString *pm);
476
477 // return TRUE if the given country uses DST for this year
478 static bool IsDSTApplicable(int year = Inv_Year,
479 Country country = Country_Default);
480
481 // get the beginning of DST for this year, will return invalid object
482 // if no DST applicable in this year. The default value of the
483 // parameter means to take the current year.
484 static wxDateTime GetBeginDST(int year = Inv_Year,
485 Country country = Country_Default);
486 // get the end of DST for this year, will return invalid object
487 // if no DST applicable in this year. The default value of the
488 // parameter means to take the current year.
489 static wxDateTime GetEndDST(int year = Inv_Year,
490 Country country = Country_Default);
491
492 // return the wxDateTime object for the current time
493 static inline wxDateTime Now();
494
495 // return the wxDateTime object for today midnight: i.e. as Now() but
496 // with time set to 0
497 static inline wxDateTime Today();
498
499 // constructors: you should test whether the constructor succeeded with
500 // IsValid() function. The values Inv_Month and Inv_Year for the
501 // parameters mean take current month and/or year values.
502 // ------------------------------------------------------------------------
503
504 // default ctor does not initialize the object, use Set()!
505 wxDateTime() { }
506
507 // from time_t: seconds since the Epoch 00:00:00 UTC, Jan 1, 1970)
508 inline wxDateTime(time_t timet);
509 // from broken down time/date (only for standard Unix range)
510 inline wxDateTime(const struct tm& tm);
511 // from broken down time/date (any range)
512 inline wxDateTime(const Tm& tm);
513
514 // from JDN (beware of rounding errors)
515 inline wxDateTime(double jdn);
516
517 // from separate values for each component, date set to today
518 inline wxDateTime(wxDateTime_t hour,
519 wxDateTime_t minute = 0,
520 wxDateTime_t second = 0,
521 wxDateTime_t millisec = 0);
522 // from separate values for each component with explicit date
523 inline wxDateTime(wxDateTime_t day, // day of the month
524 Month month = Inv_Month,
525 int year = Inv_Year, // 1999, not 99 please!
526 wxDateTime_t hour = 0,
527 wxDateTime_t minute = 0,
528 wxDateTime_t second = 0,
529 wxDateTime_t millisec = 0);
530
531 // default copy ctor ok
532
533 // no dtor
534
535 // assignment operators and Set() functions: all non const methods return
536 // the reference to this object. IsValid() should be used to test whether
537 // the function succeeded.
538 // ------------------------------------------------------------------------
539
540 // set to the current time
541 inline wxDateTime& SetToCurrent();
542
543 // set to given time_t value
544 inline wxDateTime& Set(time_t timet);
545
546 // set to given broken down time/date
547 wxDateTime& Set(const struct tm& tm);
548
549 // set to given broken down time/date
550 inline wxDateTime& Set(const Tm& tm);
551
552 // set to given JDN (beware of rounding errors)
553 wxDateTime& Set(double jdn);
554
555 // set to given time, date = today
556 wxDateTime& Set(wxDateTime_t hour,
557 wxDateTime_t minute = 0,
558 wxDateTime_t second = 0,
559 wxDateTime_t millisec = 0);
560
561 // from separate values for each component with explicit date
562 // (defaults for month and year are the current values)
563 wxDateTime& Set(wxDateTime_t day,
564 Month month = Inv_Month,
565 int year = Inv_Year, // 1999, not 99 please!
566 wxDateTime_t hour = 0,
567 wxDateTime_t minute = 0,
568 wxDateTime_t second = 0,
569 wxDateTime_t millisec = 0);
570
571 // resets time to 00:00:00, doesn't change the date
572 wxDateTime& ResetTime();
573
574 // the following functions don't change the values of the other
575 // fields, i.e. SetMinute() won't change either hour or seconds value
576
577 // set the year
578 wxDateTime& SetYear(int year);
579 // set the month
580 wxDateTime& SetMonth(Month month);
581 // set the day of the month
582 wxDateTime& SetDay(wxDateTime_t day);
583 // set hour
584 wxDateTime& SetHour(wxDateTime_t hour);
585 // set minute
586 wxDateTime& SetMinute(wxDateTime_t minute);
587 // set second
588 wxDateTime& SetSecond(wxDateTime_t second);
589 // set millisecond
590 wxDateTime& SetMillisecond(wxDateTime_t millisecond);
591
592 // assignment operator from time_t
593 wxDateTime& operator=(time_t timet) { return Set(timet); }
594
595 // assignment operator from broken down time/date
596 wxDateTime& operator=(const struct tm& tm) { return Set(tm); }
597
598 // assignment operator from broken down time/date
599 wxDateTime& operator=(const Tm& tm) { return Set(tm); }
600
601 // default assignment operator is ok
602
603 // calendar calculations (functions which set the date only leave the time
604 // unchanged, e.g. don't explictly zero it)
605 // ------------------------------------------------------------------------
606
607 // set to the given week day in the same week as this one
608 wxDateTime& SetToWeekDayInSameWeek(WeekDay weekday);
609
610 // set to the next week day following this one
611 wxDateTime& SetToNextWeekDay(WeekDay weekday);
612
613 // set to the previous week day following this one
614 wxDateTime& SetToPrevWeekDay(WeekDay weekday);
615
616 // set to Nth occurence of given weekday in the given month of the
617 // given year (time is set to 0), return TRUE on success and FALSE on
618 // failure. n may be positive (1..5) or negative to count from the end
619 // of the month (see helper function SetToLastWeekDay())
620 bool SetToWeekDay(WeekDay weekday,
621 int n = 1,
622 Month month = Inv_Month,
623 int year = Inv_Year);
624
625 // sets to the last weekday in the given month, year
626 inline bool SetToLastWeekDay(WeekDay weekday,
627 Month month = Inv_Month,
628 int year = Inv_Year);
629
630 // sets the date to the given day of the given week in the year,
631 // returns TRUE on success and FALSE if given date doesn't exist (e.g.
632 // numWeek is > 53)
633 bool SetToTheWeek(wxDateTime_t numWeek, WeekDay weekday = Mon);
634
635 // sets the date to the last day of the given (or current) month or the
636 // given (or current) year
637 wxDateTime& SetToLastMonthDay(Month month = Inv_Month,
638 int year = Inv_Year);
639
640 // sets to the given year day (1..365 or 366)
641 wxDateTime& SetToYearDay(wxDateTime_t yday);
642
643 // The definitions below were taken verbatim from
644 //
645 // http://www.capecod.net/~pbaum/date/date0.htm
646 //
647 // (Peter Baum's home page)
648 //
649 // definition: The Julian Day Number, Julian Day, or JD of a
650 // particular instant of time is the number of days and fractions of a
651 // day since 12 hours Universal Time (Greenwich mean noon) on January
652 // 1 of the year -4712, where the year is given in the Julian
653 // proleptic calendar. The idea of using this reference date was
654 // originally proposed by Joseph Scalizer in 1582 to count years but
655 // it was modified by 19th century astronomers to count days. One
656 // could have equivalently defined the reference time to be noon of
657 // November 24, -4713 if were understood that Gregorian calendar rules
658 // were applied. Julian days are Julian Day Numbers and are not to be
659 // confused with Julian dates.
660 //
661 // definition: The Rata Die number is a date specified as the number
662 // of days relative to a base date of December 31 of the year 0. Thus
663 // January 1 of the year 1 is Rata Die day 1.
664
665 // get the Julian Day number (the fractional part specifies the time of
666 // the day, related to noon - beware of rounding errors!)
667 double GetJulianDayNumber() const;
668 double GetJDN() const { return GetJulianDayNumber(); }
669
670 // get the Modified Julian Day number: it is equal to JDN - 2400000.5
671 // and so integral MJDs correspond to the midnights (and not noons).
672 // MJD 0 is Nov 17, 1858
673 double GetModifiedJulianDayNumber() const { return GetJDN() - 2400000.5; }
674 double GetMJD() const { return GetModifiedJulianDayNumber(); }
675
676 // get the Rata Die number
677 double GetRataDie() const;
678
679 // TODO algorithms for calculating some important dates, such as
680 // religious holidays (Easter...) or moon/solar eclipses? Some
681 // algorithms can be found in the calendar FAQ
682
683 // timezone stuff: a wxDateTime object constructed using given
684 // day/month/year/hour/min/sec values correspond to this moment in local
685 // time. Using the functions below, it may be converted to another time
686 // zone (for example, the Unix epoch is wxDateTime(1, Jan, 1970).ToGMT())
687 //
688 // these functions try to handle DST internally, but there is no magical
689 // way to know all rules for it in all countries in the world, so if the
690 // program can handle it itself (or doesn't want to handle it at all for
691 // whatever reason), the DST handling can be disabled with noDST.
692 //
693 // Converting to the local time zone doesn't do anything.
694 // ------------------------------------------------------------------------
695
696 // transform to any given timezone
697 inline wxDateTime ToTimezone(const TimeZone& tz, bool noDST = FALSE) const;
698 wxDateTime& MakeTimezone(const TimeZone& tz, bool noDST = FALSE);
699
700 // transform to GMT/UTC
701 wxDateTime ToGMT(bool noDST = FALSE) const { return ToTimezone(GMT0, noDST); }
702 wxDateTime& MakeGMT(bool noDST = FALSE) { return MakeTimezone(GMT0, noDST); }
703
704 // is daylight savings time in effect at this moment according to the
705 // rules of the specified country?
706 //
707 // Return value is > 0 if DST is in effect, 0 if it is not and -1 if
708 // the information is not available (this is compatible with ANSI C)
709 int IsDST(Country country = Country_Default) const;
710
711 // accessors: many of them take the timezone parameter which indicates the
712 // timezone for which to make the calculations and the default value means
713 // to do it for the current timezone of this machine (even if the function
714 // only operates with the date it's necessary because a date may wrap as
715 // result of timezone shift)
716 // ------------------------------------------------------------------------
717
718 // is the date valid (FALSE for uninitialized objects as well as after
719 // the functions which failed to convert the date to supported range)
720 inline bool IsValid() const { return this != &ms_InvDateTime; }
721
722 // get the broken down date/time representation in the given timezone
723 //
724 // If you wish to get several time components (day, month and year),
725 // consider getting the whole Tm strcuture first and retrieving the
726 // value from it - this is much more efficient
727 Tm GetTm(const TimeZone& tz = Local) const;
728
729 // get the number of seconds since the Unix epoch - returns (time_t)-1
730 // if the value is out of range
731 inline time_t GetTicks() const;
732
733 // get the year (returns Inv_Year if date is invalid)
734 int GetYear(const TimeZone& tz = Local) const
735 { return GetTm(tz).year; }
736 // get the month (Inv_Month if date is invalid)
737 Month GetMonth(const TimeZone& tz = Local) const
738 { return (Month)GetTm(tz).mon; }
739 // get the month day (in 1..31 range, 0 if date is invalid)
740 wxDateTime_t GetDay(const TimeZone& tz = Local) const
741 { return GetTm(tz).mday; }
742 // get the day of the week (Inv_WeekDay if date is invalid)
743 WeekDay GetWeekDay(const TimeZone& tz = Local) const
744 { return GetTm(tz).GetWeekDay(); }
745 // get the hour of the day
746 wxDateTime_t GetHour(const TimeZone& tz = Local) const
747 { return GetTm(tz).hour; }
748 // get the minute
749 wxDateTime_t GetMinute(const TimeZone& tz = Local) const
750 { return GetTm(tz).min; }
751 // get the second
752 wxDateTime_t GetSecond(const TimeZone& tz = Local) const
753 { return GetTm(tz).sec; }
754 // get milliseconds
755 wxDateTime_t GetMillisecond(const TimeZone& tz = Local) const
756 { return GetTm(tz).msec; }
757
758 // get the day since the year start (1..366, 0 if date is invalid)
759 wxDateTime_t GetDayOfYear(const TimeZone& tz = Local) const;
760 // get the week number since the year start (1..52 or 53, 0 if date is
761 // invalid)
762 wxDateTime_t GetWeekOfYear(const TimeZone& tz = Local) const;
763 // get the week number since the month start (1..5, 0 if date is
764 // invalid)
765 wxDateTime_t GetWeekOfMonth(const TimeZone& tz = Local) const;
766
767 // is this date a work day? This depends on a country, of course,
768 // because the holidays are different in different countries
769 bool IsWorkDay(Country country = Country_Default,
770 const TimeZone& tz = Local) const;
771
772 // is this date later than Gregorian calendar introduction for the
773 // given country (see enum GregorianAdoption)?
774 //
775 // NB: this function shouldn't be considered as absolute authoiruty in
776 // the matter. Besides, for some countries the exact date of
777 // adoption of the Gregorian calendar is simply unknown.
778 bool IsGregorianDate(GregorianAdoption country = Gr_Standard) const;
779
780 // comparison (see also functions below for operator versions)
781 // ------------------------------------------------------------------------
782
783 // returns TRUE if the two moments are strictly identical
784 inline bool IsEqualTo(const wxDateTime& datetime) const;
785
786 // returns TRUE if the date is strictly earlier than the given one
787 inline bool IsEarlierThan(const wxDateTime& datetime) const;
788
789 // returns TRUE if the date is strictly later than the given one
790 inline bool IsLaterThan(const wxDateTime& datetime) const;
791
792 // returns TRUE if the date is strictly in the given range
793 inline bool IsStrictlyBetween(const wxDateTime& t1,
794 const wxDateTime& t2) const;
795
796 // returns TRUE if the date is in the given range
797 inline bool IsBetween(const wxDateTime& t1, const wxDateTime& t2) const;
798
799 // do these two objects refer to the same date?
800 inline bool IsSameDate(const wxDateTime& dt) const;
801
802 // do these two objects have the same time?
803 inline bool IsSameTime(const wxDateTime& dt) const;
804
805 // are these two objects equal up to given timespan?
806 inline bool IsEqualUpTo(const wxDateTime& dt, const wxTimeSpan& ts) const;
807
808 // arithmetics with dates (see also below for more operators)
809 // ------------------------------------------------------------------------
810
811 // return the sum of the date with a time span (positive or negative)
812 inline wxDateTime Add(const wxTimeSpan& diff) const;
813 // add a time span (positive or negative)
814 inline wxDateTime& Add(const wxTimeSpan& diff);
815 // add a time span (positive or negative)
816 inline wxDateTime& operator+=(const wxTimeSpan& diff);
817
818 // return the difference of the date with a time span
819 inline wxDateTime Substract(const wxTimeSpan& diff) const;
820 // substract a time span (positive or negative)
821 inline wxDateTime& Substract(const wxTimeSpan& diff);
822 // substract a time span (positive or negative)
823 inline wxDateTime& operator-=(const wxTimeSpan& diff);
824
825 // return the sum of the date with a date span
826 inline wxDateTime Add(const wxDateSpan& diff) const;
827 // add a date span (positive or negative)
828 wxDateTime& Add(const wxDateSpan& diff);
829 // add a date span (positive or negative)
830 inline wxDateTime& operator+=(const wxDateSpan& diff);
831
832 // return the difference of the date with a date span
833 inline wxDateTime Substract(const wxDateSpan& diff) const;
834 // substract a date span (positive or negative)
835 inline wxDateTime& Substract(const wxDateSpan& diff);
836 // substract a date span (positive or negative)
837 inline wxDateTime& operator-=(const wxDateSpan& diff);
838
839 // return the difference between two dates
840 inline wxTimeSpan Substract(const wxDateTime& dt) const;
841
842 // conversion to/from text: all conversions from text return the pointer to
843 // the next character following the date specification (i.e. the one where
844 // the scan had to stop) or NULL on failure.
845 // ------------------------------------------------------------------------
846
847 // parse a string in RFC 822 format (found e.g. in mail headers and
848 // having the form "Wed, 10 Feb 1999 19:07:07 +0100")
849 const wxChar *ParseRfc822Date(const wxChar* date);
850 // parse a date/time in the given format (see strptime(3)), fill in
851 // the missing (in the string) fields with the values of dateDef (by
852 // default, they will not change if they had valid values or will
853 // default to Today() otherwise)
854 const wxChar *ParseFormat(const wxChar *date,
855 const wxChar *format = _T("%c"),
856 const wxDateTime& dateDef = wxDateTime::ms_InvDateTime);
857 // parse a string containing the date/time in "free" format, this
858 // function will try to make an educated guess at the string contents
859 const wxChar *ParseDateTime(const wxChar *datetime);
860 // parse a string containing the date only in "free" format (less
861 // flexible than ParseDateTime)
862 const wxChar *ParseDate(const wxChar *date);
863 // parse a string containing the time only in "free" format
864 const wxChar *ParseTime(const wxChar *time);
865
866 // this function accepts strftime()-like format string (default
867 // argument corresponds to the preferred date and time representation
868 // for the current locale) and returns the string containing the
869 // resulting text representation
870 wxString Format(const wxChar *format = _T("%c"),
871 const TimeZone& tz = Local) const;
872 // preferred date representation for the current locale
873 wxString FormatDate() const { return Format(_T("%x")); }
874 // preferred time representation for the current locale
875 wxString FormatTime() const { return Format(_T("%X")); }
876
877 // implementation
878 // ------------------------------------------------------------------------
879
880 // construct from internal representation
881 wxDateTime(const wxLongLong& time) { m_time = time; }
882
883 // get the internal representation
884 inline wxLongLong GetValue() const;
885
886 // a helper function to get the current time_t
887 static time_t GetTimeNow() { return time((time_t *)NULL); }
888
889 // another one to get the current time broken down
890 static struct tm *GetTmNow()
891 {
892 time_t t = GetTimeNow();
893 return localtime(&t);
894 }
895
896 private:
897 // the current country - as it's the same for all program objects (unless
898 // it runs on a _really_ big cluster system :-), this is a static member:
899 // see SetCountry() and GetCountry()
900 static Country ms_country;
901
902 // this constant is used to transform a time_t value to the internal
903 // representation, as time_t is in seconds and we use milliseconds it's
904 // fixed to 1000
905 static const long TIME_T_FACTOR;
906
907 // invalid wxDateTime object - returned by all functions which return
908 // "wxDateTime &" on failure
909 static wxDateTime ms_InvDateTime;
910
911 // returns TRUE if we fall in range in which we can use standard ANSI C
912 // functions
913 inline bool IsInStdRange() const;
914
915 // the internal representation of the time is the amount of milliseconds
916 // elapsed since the origin which is set by convention to the UNIX/C epoch
917 // value: the midnight of January 1, 1970 (UTC)
918 wxLongLong m_time;
919 };
920
921 // ----------------------------------------------------------------------------
922 // This class contains a difference between 2 wxDateTime values, so it makes
923 // sense to add it to wxDateTime and it is the result of substraction of 2
924 // objects of that class. See also wxDateSpan.
925 // ----------------------------------------------------------------------------
926
927 class WXDLLEXPORT wxTimeSpan
928 {
929 public:
930 // constructors
931 // ------------------------------------------------------------------------
932
933 // return the timespan for the given number of seconds
934 static wxTimeSpan Seconds(int sec) { return wxTimeSpan(0, 0, sec); }
935 static wxTimeSpan Second() { return Seconds(1); }
936
937 // return the timespan for the given number of minutes
938 static wxTimeSpan Minutes(int min) { return wxTimeSpan(0, min, 0 ); }
939 static wxTimeSpan Minute() { return Minutes(1); }
940
941 // return the timespan for the given number of hours
942 static wxTimeSpan Hours(int hours) { return wxTimeSpan(hours, 0, 0); }
943 static wxTimeSpan Hour() { return Hours(1); }
944
945 // return the timespan for the given number of days
946 static wxTimeSpan Days(int days) { return Hours(24 * days); }
947 static wxTimeSpan Day() { return Days(1); }
948
949 // return the timespan for the given number of weeks
950 static wxTimeSpan Weeks(int days) { return Days(7 * days); }
951 static wxTimeSpan Week() { return Weeks(1); }
952
953 // default ctor constructs the 0 time span
954 wxTimeSpan() { }
955
956 // from separate values for each component, date set to 0 (hours are
957 // not restricted to 0..24 range, neither are minutes, seconds or
958 // milliseconds)
959 inline wxTimeSpan(int hours,
960 int minutes = 0,
961 int seconds = 0,
962 int milliseconds = 0);
963
964 // default copy ctor is ok
965
966 // no dtor
967
968 // arithmetics with time spans (see also below for more operators)
969 // ------------------------------------------------------------------------
970
971 // return the sum of two timespans
972 inline wxTimeSpan Add(const wxTimeSpan& diff) const;
973 // add two timespans together
974 inline wxTimeSpan& Add(const wxTimeSpan& diff);
975 // add two timespans together
976 wxTimeSpan& operator+=(const wxTimeSpan& diff) { return Add(diff); }
977
978 // return the difference of two timespans
979 inline wxTimeSpan Substract(const wxTimeSpan& diff) const;
980 // substract another timespan
981 inline wxTimeSpan& Substract(const wxTimeSpan& diff);
982 // substract another timespan
983 wxTimeSpan& operator-=(const wxTimeSpan& diff) { return Substract(diff); }
984
985 // multiply timespan by a scalar
986 inline wxTimeSpan Multiply(int n) const;
987 // multiply timespan by a scalar
988 inline wxTimeSpan& Multiply(int n);
989 // multiply timespan by a scalar
990 wxTimeSpan& operator*=(int n) { return Multiply(n); }
991
992 // return this timespan with inversed sign
993 wxTimeSpan Negate() const { return wxTimeSpan(-GetValue()); }
994 // negate the value of the timespan
995 wxTimeSpan& Neg() { m_diff = -GetValue(); return *this; }
996 // negate the value of the timespan
997 wxTimeSpan& operator-() { return Neg(); }
998
999 // return the absolute value of the timespan: does _not_ modify the
1000 // object
1001 inline wxTimeSpan Abs() const;
1002
1003 // there is intentionally no division because we don't want to
1004 // introduce rounding errors in time calculations
1005
1006 // comparaison (see also operator versions below)
1007 // ------------------------------------------------------------------------
1008
1009 // is the timespan null?
1010 bool IsNull() const { return m_diff == 0l; }
1011 // returns true if the timespan is null
1012 bool operator!() const { return !IsNull(); }
1013
1014 // is the timespan positive?
1015 bool IsPositive() const { return m_diff > 0l; }
1016
1017 // is the timespan negative?
1018 bool IsNegative() const { return m_diff < 0l; }
1019
1020 // are two timespans equal?
1021 inline bool IsEqualTo(const wxTimeSpan& ts) const;
1022 // compare two timestamps: works with the absolute values, i.e. -2
1023 // hours is longer than 1 hour. Also, it will return FALSE if the
1024 // timespans are equal in absolute value.
1025 inline bool IsLongerThan(const wxTimeSpan& ts) const;
1026 // compare two timestamps: works with the absolute values, i.e. 1
1027 // hour is shorter than -2 hours. Also, it will return FALSE if the
1028 // timespans are equal in absolute value.
1029 bool IsShorterThan(const wxTimeSpan& t) const { return !IsLongerThan(t); }
1030
1031 // breaking into days, hours, minutes and seconds
1032 // ------------------------------------------------------------------------
1033
1034 // get the max number of weeks in this timespan
1035 inline int GetWeeks() const;
1036 // get the max number of days in this timespan
1037 inline int GetDays() const;
1038 // get the max number of hours in this timespan
1039 inline int GetHours() const;
1040 // get the max number of minutes in this timespan
1041 inline int GetMinutes() const;
1042 // get the max number of seconds in this timespan
1043 inline wxLongLong GetSeconds() const;
1044 // get the number of milliseconds in this timespan
1045 wxLongLong GetMilliseconds() const { return m_diff; }
1046
1047 // conversion to text
1048 // ------------------------------------------------------------------------
1049
1050 // this function accepts strftime()-like format string (default
1051 // argument corresponds to the preferred date and time representation
1052 // for the current locale) and returns the string containing the
1053 // resulting text representation. Notice that only some of format
1054 // specifiers valid for wxDateTime are valid for wxTimeSpan: hours,
1055 // minutes and seconds make sense, but not "PM/AM" string for example.
1056 wxString Format(const wxChar *format = _T("%c")) const;
1057 // preferred date representation for the current locale
1058 wxString FormatDate() const { return Format(_T("%x")); }
1059 // preferred time representation for the current locale
1060 wxString FormatTime() const { return Format(_T("%X")); }
1061
1062 // implementation
1063 // ------------------------------------------------------------------------
1064
1065 // construct from internal representation
1066 wxTimeSpan(const wxLongLong& diff) { m_diff = diff; }
1067
1068 // get the internal representation
1069 wxLongLong GetValue() const { return m_diff; }
1070
1071 private:
1072 // the (signed) time span in milliseconds
1073 wxLongLong m_diff;
1074 };
1075
1076 // ----------------------------------------------------------------------------
1077 // This class is a "logical time span" and is useful for implementing program
1078 // logic for such things as "add one month to the date" which, in general,
1079 // doesn't mean to add 60*60*24*31 seconds to it, but to take the same date
1080 // the next month (to understand that this is indeed different consider adding
1081 // one month to Feb, 15 - we want to get Mar, 15, of course).
1082 //
1083 // When adding a month to the date, all lesser components (days, hours, ...)
1084 // won't be changed.
1085 //
1086 // wxDateSpan can be either positive or negative. They may be
1087 // multiplied by scalars which multiply all deltas by the scalar: i.e. 2*(1
1088 // month and 1 day) is 2 months and 2 days. They can be added together and
1089 // with wxDateTime or wxTimeSpan, but the type of result is different for each
1090 // case.
1091 //
1092 // Beware about weeks: if you specify both weeks and days, the total number of
1093 // days added will be 7*weeks + days! See also GetTotalDays() function.
1094 //
1095 // Finally, notice that for adding hours, minutes &c you don't need this
1096 // class: wxTimeSpan will do the job because there are no subtleties
1097 // associated with those.
1098 // ----------------------------------------------------------------------------
1099
1100 class WXDLLEXPORT wxDateSpan
1101 {
1102 public:
1103 // constructors
1104 // ------------------------------------------------------------------------
1105
1106 // this many years/months/weeks/days
1107 wxDateSpan(int years = 0, int months = 0, int weeks = 0, int days = 0)
1108 {
1109 m_years = years;
1110 m_months = months;
1111 m_weeks = weeks;
1112 m_days = days;
1113 }
1114
1115 // get an object for the given number of days
1116 static wxDateSpan Days(int days) { return wxDateSpan(0, 0, 0, days); }
1117 static wxDateSpan Day() { return Days(1); }
1118
1119 // get an object for the given number of weeks
1120 static wxDateSpan Weeks(int weeks) { return wxDateSpan(0, 0, weeks, 0); }
1121 static wxDateSpan Week() { return Weeks(1); }
1122
1123 // get an object for the given number of months
1124 static wxDateSpan Months(int mon) { return wxDateSpan(0, mon, 0, 0); }
1125 static wxDateSpan Month() { return Months(1); }
1126
1127 // get an object for the given number of years
1128 static wxDateSpan Years(int years) { return wxDateSpan(years, 0, 0, 0); }
1129 static wxDateSpan Year() { return Years(1); }
1130
1131 // default copy ctor is ok
1132
1133 // no dtor
1134
1135 // accessors (all SetXXX() return the (modified) wxDateSpan object)
1136 // ------------------------------------------------------------------------
1137
1138 // set number of years
1139 wxDateSpan& SetYears(int n) { m_years = n; return *this; }
1140 // set number of months
1141 wxDateSpan& SetMonths(int n) { m_months = n; return *this; }
1142 // set number of weeks
1143 wxDateSpan& SetWeeks(int n) { m_weeks = n; return *this; }
1144 // set number of days
1145 wxDateSpan& SetDays(int n) { m_days = n; return *this; }
1146
1147 // get number of years
1148 int GetYears() const { return m_years; }
1149 // get number of months
1150 int GetMonths() const { return m_months; }
1151 // get number of weeks
1152 int GetWeeks() const { return m_weeks; }
1153 // get number of days
1154 int GetDays() const { return m_days; }
1155 // returns 7*GetWeeks() + GetDays()
1156 int GetTotalDays() const { return 7*m_weeks + m_days; }
1157
1158 // arithmetics with date spans (see also below for more operators)
1159 // ------------------------------------------------------------------------
1160
1161 // return sum of two date spans
1162 inline wxDateSpan Add(const wxDateSpan& other) const;
1163 // add another wxDateSpan to us
1164 inline wxDateSpan& Add(const wxDateSpan& other);
1165 // add another wxDateSpan to us
1166 inline wxDateSpan& operator+=(const wxDateSpan& other);
1167
1168 // return difference of two date spans
1169 inline wxDateSpan Substract(const wxDateSpan& other) const;
1170 // substract another wxDateSpan from us
1171 inline wxDateSpan& Substract(const wxDateSpan& other);
1172 // substract another wxDateSpan from us
1173 inline wxDateSpan& operator-=(const wxDateSpan& other);
1174
1175 // return a copy of this time span with changed sign
1176 inline wxDateSpan Negate() const;
1177 // inverse the sign of this timespan
1178 inline wxDateSpan& Neg();
1179 // inverse the sign of this timespan
1180 wxDateSpan& operator-() { return Neg(); }
1181
1182 // return the date span proportional to this one with given factor
1183 inline wxDateSpan Multiply(int factor) const;
1184 // multiply all components by a (signed) number
1185 inline wxDateSpan& Multiply(int factor);
1186 // multiply all components by a (signed) number
1187 inline wxDateSpan& operator*=(int factor) { return Multiply(factor); }
1188
1189 private:
1190 int m_years,
1191 m_months,
1192 m_weeks,
1193 m_days;
1194 };
1195
1196 WXDLLEXPORT_DATA(extern wxDateSpan) wxYear;
1197 WXDLLEXPORT_DATA(extern wxDateSpan) wxMonth;
1198 WXDLLEXPORT_DATA(extern wxDateSpan) wxWeek;
1199 WXDLLEXPORT_DATA(extern wxDateSpan) wxDay;
1200
1201 // ============================================================================
1202 // inline functions implementation
1203 // ============================================================================
1204
1205 // don't include inline functions definitions when we're included from anything
1206 // else than datetime.cpp in debug builds: this minimizes rebuilds if we change
1207 // some inline function and the performance doesn't matter in the debug builds.
1208
1209 #if !defined(__WXDEBUG__) || defined(wxDEFINE_TIME_CONSTANTS)
1210 #define INCLUDED_FROM_WX_DATETIME_H
1211 #include "wx/datetime.inl"
1212 #undef INCLUDED_FROM_WX_DATETIME_H
1213 #endif
1214
1215 // if we defined it to be empty above, restore it now
1216 #undef inline
1217
1218 // ============================================================================
1219 // binary operators
1220 // ============================================================================
1221
1222 // ----------------------------------------------------------------------------
1223 // wxDateTime operators
1224 // ----------------------------------------------------------------------------
1225
1226 // arithmetics
1227 // -----------
1228
1229 // no need to check for validity - the member functions we call will do it
1230
1231 inline wxDateTime WXDLLEXPORT operator+(const wxDateTime& dt,
1232 const wxTimeSpan& ts)
1233 {
1234 return dt.Add(ts);
1235 }
1236
1237 inline wxDateTime WXDLLEXPORT operator-(const wxDateTime& dt,
1238 const wxTimeSpan& ts)
1239 {
1240 return dt.Substract(ts);
1241 }
1242
1243 inline wxDateTime WXDLLEXPORT operator+(const wxDateTime& dt,
1244 const wxDateSpan& ds)
1245 {
1246 return dt.Add(ds);
1247 }
1248
1249 inline wxDateTime WXDLLEXPORT operator-(const wxDateTime& dt,
1250 const wxDateSpan& ds)
1251 {
1252 return dt.Substract(ds);
1253 }
1254
1255 inline wxTimeSpan WXDLLEXPORT operator-(const wxDateTime& dt1,
1256 const wxDateTime& dt2)
1257 {
1258 return dt1.Substract(dt2);
1259 }
1260
1261 // comparison
1262 // ----------
1263
1264 inline bool WXDLLEXPORT operator<(const wxDateTime& t1, const wxDateTime& t2)
1265 {
1266 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") );
1267
1268 return t1.GetValue() < t2.GetValue();
1269 }
1270
1271 inline bool WXDLLEXPORT operator<=(const wxDateTime& t1, const wxDateTime& t2)
1272 {
1273 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") );
1274
1275 return t1.GetValue() <= t2.GetValue();
1276 }
1277
1278 inline bool WXDLLEXPORT operator>(const wxDateTime& t1, const wxDateTime& t2)
1279 {
1280 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") );
1281
1282 return t1.GetValue() > t2.GetValue();
1283 }
1284
1285 inline bool WXDLLEXPORT operator>=(const wxDateTime& t1, const wxDateTime& t2)
1286 {
1287 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") );
1288
1289 return t1.GetValue() >= t2.GetValue();
1290 }
1291
1292 inline bool WXDLLEXPORT operator==(const wxDateTime& t1, const wxDateTime& t2)
1293 {
1294 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") );
1295
1296 return t1.GetValue() == t2.GetValue();
1297 }
1298
1299 inline bool WXDLLEXPORT operator!=(const wxDateTime& t1, const wxDateTime& t2)
1300 {
1301 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") );
1302
1303 return t1.GetValue() != t2.GetValue();
1304 }
1305
1306 // ----------------------------------------------------------------------------
1307 // wxTimeSpan operators
1308 // ----------------------------------------------------------------------------
1309
1310 // arithmetics
1311 // -----------
1312
1313 inline wxTimeSpan WXDLLEXPORT operator+(const wxTimeSpan& ts1,
1314 const wxTimeSpan& ts2)
1315 {
1316 return wxTimeSpan(ts1.GetValue() + ts2.GetValue());
1317 }
1318
1319 inline wxTimeSpan WXDLLEXPORT operator-(const wxTimeSpan& ts1,
1320 const wxTimeSpan& ts2)
1321 {
1322 return wxTimeSpan(ts1.GetValue() - ts2.GetValue());
1323 }
1324
1325 inline wxTimeSpan WXDLLEXPORT operator*(const wxTimeSpan& ts, int n)
1326 {
1327 return wxTimeSpan(ts).Multiply(n);
1328 }
1329
1330 inline wxTimeSpan WXDLLEXPORT operator*(int n, const wxTimeSpan& ts)
1331 {
1332 return wxTimeSpan(ts).Multiply(n);
1333 }
1334
1335 // comparison
1336 // ----------
1337
1338 inline bool WXDLLEXPORT operator<(const wxTimeSpan &t1, const wxTimeSpan &t2)
1339 {
1340 return t1.GetValue() < t2.GetValue();
1341 }
1342
1343 inline bool WXDLLEXPORT operator<=(const wxTimeSpan &t1, const wxTimeSpan &t2)
1344 {
1345 return t1.GetValue() <= t2.GetValue();
1346 }
1347
1348 inline bool WXDLLEXPORT operator>(const wxTimeSpan &t1, const wxTimeSpan &t2)
1349 {
1350 return t1.GetValue() > t2.GetValue();
1351 }
1352
1353 inline bool WXDLLEXPORT operator>=(const wxTimeSpan &t1, const wxTimeSpan &t2)
1354 {
1355 return t1.GetValue() >= t2.GetValue();
1356 }
1357
1358 inline bool WXDLLEXPORT operator==(const wxTimeSpan &t1, const wxTimeSpan &t2)
1359 {
1360 return t1.GetValue() == t2.GetValue();
1361 }
1362
1363 inline bool WXDLLEXPORT operator!=(const wxTimeSpan &t1, const wxTimeSpan &t2)
1364 {
1365 return t1.GetValue() != t2.GetValue();
1366 }
1367
1368 // ----------------------------------------------------------------------------
1369 // wxDateSpan
1370 // ----------------------------------------------------------------------------
1371
1372 // arithmetics
1373 // -----------
1374
1375 inline WXDLLEXPORT wxDateSpan operator+(const wxDateSpan& ds1,
1376 const wxDateSpan& ds2)
1377 {
1378 return wxDateSpan(ds1.GetYears() + ds2.GetYears(),
1379 ds1.GetMonths() + ds2.GetMonths(),
1380 ds1.GetWeeks() + ds2.GetWeeks(),
1381 ds1.GetDays() + ds2.GetDays());
1382 }
1383
1384 inline WXDLLEXPORT wxDateSpan operator-(const wxDateSpan& ds1,
1385 const wxDateSpan& ds2)
1386 {
1387 return wxDateSpan(ds1.GetYears() - ds2.GetYears(),
1388 ds1.GetMonths() - ds2.GetMonths(),
1389 ds1.GetWeeks() - ds2.GetWeeks(),
1390 ds1.GetDays() - ds2.GetDays());
1391 }
1392
1393 inline WXDLLEXPORT wxDateSpan operator*(const wxDateSpan& ds, int n)
1394 {
1395 return wxDateSpan(ds).Multiply(n);
1396 }
1397
1398 inline WXDLLEXPORT wxDateSpan operator*(int n, const wxDateSpan& ds)
1399 {
1400 return wxDateSpan(ds).Multiply(n);
1401 }
1402
1403 // ============================================================================
1404 // other helper functions
1405 // ============================================================================
1406
1407 // ----------------------------------------------------------------------------
1408 // iteration helpers: can be used to write a for loop over enum variable like
1409 // this:
1410 // for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) )
1411 // ----------------------------------------------------------------------------
1412
1413 inline WXDLLEXPORT void wxNextMonth(wxDateTime::Month& m)
1414 {
1415 wxASSERT_MSG( m < wxDateTime::Inv_Month, _T("invalid month") );
1416
1417 // no wrapping or the for loop above would never end!
1418 m = (wxDateTime::Month)(m + 1);
1419 }
1420
1421 inline WXDLLEXPORT void wxPrevMonth(wxDateTime::Month& m)
1422 {
1423 wxASSERT_MSG( m < wxDateTime::Inv_Month, _T("invalid month") );
1424
1425 m = m == wxDateTime::Jan ? wxDateTime::Inv_Month
1426 : (wxDateTime::Month)(m - 1);
1427 }
1428
1429 inline WXDLLEXPORT void wxNextWDay(wxDateTime::WeekDay& wd)
1430 {
1431 wxASSERT_MSG( wd < wxDateTime::Inv_WeekDay, _T("invalid week day") );
1432
1433 // no wrapping or the for loop above would never end!
1434 wd = (wxDateTime::WeekDay)(wd + 1);
1435 }
1436
1437 inline WXDLLEXPORT void wxPrevWDay(wxDateTime::WeekDay& wd)
1438 {
1439 wxASSERT_MSG( wd < wxDateTime::Inv_WeekDay, _T("invalid week day") );
1440
1441 wd = wd == wxDateTime::Sun ? wxDateTime::Inv_WeekDay
1442 : (wxDateTime::WeekDay)(wd - 1);
1443 }
1444
1445 #endif // _WX_DATETIME_H