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