1. wxCalendarCtrl
[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 // flags for GetWeekOfYear and GetWeekOfMonth
367 enum WeekFlags
368 {
369 Default_First, // Sunday_First for US, Monday_First for the rest
370 Monday_First, // week starts with a Monday
371 Sunday_First // week starts with a Sunday
372 };
373
374 // helper classes
375 // ------------------------------------------------------------------------
376
377 // a class representing a time zone: basicly, this is just an offset
378 // (in seconds) from GMT
379 class TimeZone
380 {
381 public:
382 TimeZone(TZ tz);
383 TimeZone(wxDateTime_t offset = 0) { m_offset = offset; }
384
385 long GetOffset() const { return m_offset; }
386
387 private:
388 // offset for this timezone from GMT in seconds
389 long m_offset;
390 };
391
392 // standard struct tm is limited to the years from 1900 (because
393 // tm_year field is the offset from 1900), so we use our own struct
394 // instead to represent broken down time
395 //
396 // NB: this struct should always be kept normalized (i.e. mon should
397 // be < 12, 1 <= day <= 31 &c), so use AddMonths(), AddDays()
398 // instead of modifying the member fields directly!
399 struct Tm
400 {
401 wxDateTime_t msec, sec, min, hour, mday;
402 Month mon;
403 int year;
404
405 // default ctor inits the object to an invalid value
406 Tm();
407
408 // ctor from struct tm and the timezone
409 Tm(const struct tm& tm, const TimeZone& tz);
410
411 // check that the given date/time is valid (in Gregorian calendar)
412 bool IsValid() const;
413
414 // get the week day
415 WeekDay GetWeekDay() // not const because wday may be changed
416 {
417 if ( wday == Inv_WeekDay )
418 ComputeWeekDay();
419
420 return (WeekDay)wday;
421 }
422
423 // add the given number of months to the date keeping it normalized
424 void AddMonths(int monDiff);
425
426 // add the given number of months to the date keeping it normalized
427 void AddDays(int dayDiff);
428
429 private:
430 // compute the weekday from other fields
431 void ComputeWeekDay();
432
433 // the timezone we correspond to
434 TimeZone m_tz;
435
436 // these values can't be accessed directly because they're not always
437 // computed and we calculate them on demand
438 wxDateTime_t wday, yday;
439 };
440
441 // static methods
442 // ------------------------------------------------------------------------
443
444 // set the current country
445 static void SetCountry(Country country);
446 // get the current country
447 static Country GetCountry();
448
449 // return TRUE if the country is a West European one (in practice,
450 // this means that the same DST rules as for EEC apply)
451 static bool IsWestEuropeanCountry(Country country = Country_Default);
452
453 // return the current year
454 static int GetCurrentYear(Calendar cal = Gregorian);
455
456 // convert the year as returned by wxDateTime::GetYear() to a year
457 // suitable for BC/AD notation. The difference is that BC year 1
458 // corresponds to the year 0 (while BC year 0 didn't exist) and AD
459 // year N is just year N.
460 static int ConvertYearToBC(int year);
461
462 // return the current month
463 static Month GetCurrentMonth(Calendar cal = Gregorian);
464
465 // returns TRUE if the given year is a leap year in the given calendar
466 static bool IsLeapYear(int year = Inv_Year, Calendar cal = Gregorian);
467
468 // get the century (19 for 1999, 20 for 2000 and -5 for 492 BC)
469 static int GetCentury(int year = Inv_Year);
470
471 // returns the number of days in this year (356 or 355 for Gregorian
472 // calendar usually :-)
473 static wxDateTime_t GetNumberOfDays(int year, Calendar cal = Gregorian);
474
475 // get the number of the days in the given month (default value for
476 // the year means the current one)
477 static wxDateTime_t GetNumberOfDays(Month month,
478 int year = Inv_Year,
479 Calendar cal = Gregorian);
480
481 // get the full (default) or abbreviated month name in the current
482 // locale, returns empty string on error
483 static wxString GetMonthName(Month month,
484 NameFlags flags = Name_Full);
485
486 // get the full (default) or abbreviated weekday name in the current
487 // locale, returns empty string on error
488 static wxString GetWeekDayName(WeekDay weekday,
489 NameFlags flags = Name_Full);
490
491 // get the AM and PM strings in the current locale (may be empty)
492 static void GetAmPmStrings(wxString *am, wxString *pm);
493
494 // return TRUE if the given country uses DST for this year
495 static bool IsDSTApplicable(int year = Inv_Year,
496 Country country = Country_Default);
497
498 // get the beginning of DST for this year, will return invalid object
499 // if no DST applicable in this year. The default value of the
500 // parameter means to take the current year.
501 static wxDateTime GetBeginDST(int year = Inv_Year,
502 Country country = Country_Default);
503 // get the end of DST for this year, will return invalid object
504 // if no DST applicable in this year. The default value of the
505 // parameter means to take the current year.
506 static wxDateTime GetEndDST(int year = Inv_Year,
507 Country country = Country_Default);
508
509 // return the wxDateTime object for the current time
510 static inline wxDateTime Now();
511
512 // return the wxDateTime object for today midnight: i.e. as Now() but
513 // with time set to 0
514 static inline wxDateTime Today();
515
516 // constructors: you should test whether the constructor succeeded with
517 // IsValid() function. The values Inv_Month and Inv_Year for the
518 // parameters mean take current month and/or year values.
519 // ------------------------------------------------------------------------
520
521 // default ctor does not initialize the object, use Set()!
522 wxDateTime() { }
523
524 // from time_t: seconds since the Epoch 00:00:00 UTC, Jan 1, 1970)
525 inline wxDateTime(time_t timet);
526 // from broken down time/date (only for standard Unix range)
527 inline wxDateTime(const struct tm& tm);
528 // from broken down time/date (any range)
529 inline wxDateTime(const Tm& tm);
530
531 // from JDN (beware of rounding errors)
532 inline wxDateTime(double jdn);
533
534 // from separate values for each component, date set to today
535 inline wxDateTime(wxDateTime_t hour,
536 wxDateTime_t minute = 0,
537 wxDateTime_t second = 0,
538 wxDateTime_t millisec = 0);
539 // from separate values for each component with explicit date
540 inline wxDateTime(wxDateTime_t day, // day of the month
541 Month month = Inv_Month,
542 int year = Inv_Year, // 1999, not 99 please!
543 wxDateTime_t hour = 0,
544 wxDateTime_t minute = 0,
545 wxDateTime_t second = 0,
546 wxDateTime_t millisec = 0);
547
548 // default copy ctor ok
549
550 // no dtor
551
552 // assignment operators and Set() functions: all non const methods return
553 // the reference to this object. IsValid() should be used to test whether
554 // the function succeeded.
555 // ------------------------------------------------------------------------
556
557 // set to the current time
558 inline wxDateTime& SetToCurrent();
559
560 // set to given time_t value
561 inline wxDateTime& Set(time_t timet);
562
563 // set to given broken down time/date
564 wxDateTime& Set(const struct tm& tm);
565
566 // set to given broken down time/date
567 inline wxDateTime& Set(const Tm& tm);
568
569 // set to given JDN (beware of rounding errors)
570 wxDateTime& Set(double jdn);
571
572 // set to given time, date = today
573 wxDateTime& Set(wxDateTime_t hour,
574 wxDateTime_t minute = 0,
575 wxDateTime_t second = 0,
576 wxDateTime_t millisec = 0);
577
578 // from separate values for each component with explicit date
579 // (defaults for month and year are the current values)
580 wxDateTime& Set(wxDateTime_t day,
581 Month month = Inv_Month,
582 int year = Inv_Year, // 1999, not 99 please!
583 wxDateTime_t hour = 0,
584 wxDateTime_t minute = 0,
585 wxDateTime_t second = 0,
586 wxDateTime_t millisec = 0);
587
588 // resets time to 00:00:00, doesn't change the date
589 wxDateTime& ResetTime();
590
591 // the following functions don't change the values of the other
592 // fields, i.e. SetMinute() won't change either hour or seconds value
593
594 // set the year
595 wxDateTime& SetYear(int year);
596 // set the month
597 wxDateTime& SetMonth(Month month);
598 // set the day of the month
599 wxDateTime& SetDay(wxDateTime_t day);
600 // set hour
601 wxDateTime& SetHour(wxDateTime_t hour);
602 // set minute
603 wxDateTime& SetMinute(wxDateTime_t minute);
604 // set second
605 wxDateTime& SetSecond(wxDateTime_t second);
606 // set millisecond
607 wxDateTime& SetMillisecond(wxDateTime_t millisecond);
608
609 // assignment operator from time_t
610 wxDateTime& operator=(time_t timet) { return Set(timet); }
611
612 // assignment operator from broken down time/date
613 wxDateTime& operator=(const struct tm& tm) { return Set(tm); }
614
615 // assignment operator from broken down time/date
616 wxDateTime& operator=(const Tm& tm) { return Set(tm); }
617
618 // default assignment operator is ok
619
620 // calendar calculations (functions which set the date only leave the time
621 // unchanged, e.g. don't explictly zero it)
622 // ------------------------------------------------------------------------
623
624 // set to the given week day in the same week as this one
625 wxDateTime& SetToWeekDayInSameWeek(WeekDay weekday);
626
627 // set to the next week day following this one
628 wxDateTime& SetToNextWeekDay(WeekDay weekday);
629
630 // set to the previous week day before this one
631 wxDateTime& SetToPrevWeekDay(WeekDay weekday);
632
633 // set to Nth occurence of given weekday in the given month of the
634 // given year (time is set to 0), return TRUE on success and FALSE on
635 // failure. n may be positive (1..5) or negative to count from the end
636 // of the month (see helper function SetToLastWeekDay())
637 bool SetToWeekDay(WeekDay weekday,
638 int n = 1,
639 Month month = Inv_Month,
640 int year = Inv_Year);
641
642 // sets to the last weekday in the given month, year
643 inline bool SetToLastWeekDay(WeekDay weekday,
644 Month month = Inv_Month,
645 int year = Inv_Year);
646
647 // sets the date to the given day of the given week in the year,
648 // returns TRUE on success and FALSE if given date doesn't exist (e.g.
649 // numWeek is > 53)
650 bool SetToTheWeek(wxDateTime_t numWeek, WeekDay weekday = Mon);
651
652 // sets the date to the last day of the given (or current) month or the
653 // given (or current) year
654 wxDateTime& SetToLastMonthDay(Month month = Inv_Month,
655 int year = Inv_Year);
656
657 // sets to the given year day (1..365 or 366)
658 wxDateTime& SetToYearDay(wxDateTime_t yday);
659
660 // The definitions below were taken verbatim from
661 //
662 // http://www.capecod.net/~pbaum/date/date0.htm
663 //
664 // (Peter Baum's home page)
665 //
666 // definition: The Julian Day Number, Julian Day, or JD of a
667 // particular instant of time is the number of days and fractions of a
668 // day since 12 hours Universal Time (Greenwich mean noon) on January
669 // 1 of the year -4712, where the year is given in the Julian
670 // proleptic calendar. The idea of using this reference date was
671 // originally proposed by Joseph Scalizer in 1582 to count years but
672 // it was modified by 19th century astronomers to count days. One
673 // could have equivalently defined the reference time to be noon of
674 // November 24, -4713 if were understood that Gregorian calendar rules
675 // were applied. Julian days are Julian Day Numbers and are not to be
676 // confused with Julian dates.
677 //
678 // definition: The Rata Die number is a date specified as the number
679 // of days relative to a base date of December 31 of the year 0. Thus
680 // January 1 of the year 1 is Rata Die day 1.
681
682 // get the Julian Day number (the fractional part specifies the time of
683 // the day, related to noon - beware of rounding errors!)
684 double GetJulianDayNumber() const;
685 double GetJDN() const { return GetJulianDayNumber(); }
686
687 // get the Modified Julian Day number: it is equal to JDN - 2400000.5
688 // and so integral MJDs correspond to the midnights (and not noons).
689 // MJD 0 is Nov 17, 1858
690 double GetModifiedJulianDayNumber() const { return GetJDN() - 2400000.5; }
691 double GetMJD() const { return GetModifiedJulianDayNumber(); }
692
693 // get the Rata Die number
694 double GetRataDie() const;
695
696 // TODO algorithms for calculating some important dates, such as
697 // religious holidays (Easter...) or moon/solar eclipses? Some
698 // algorithms can be found in the calendar FAQ
699
700 // timezone stuff: a wxDateTime object constructed using given
701 // day/month/year/hour/min/sec values correspond to this moment in local
702 // time. Using the functions below, it may be converted to another time
703 // zone (for example, the Unix epoch is wxDateTime(1, Jan, 1970).ToGMT())
704 //
705 // these functions try to handle DST internally, but there is no magical
706 // way to know all rules for it in all countries in the world, so if the
707 // program can handle it itself (or doesn't want to handle it at all for
708 // whatever reason), the DST handling can be disabled with noDST.
709 //
710 // Converting to the local time zone doesn't do anything.
711 // ------------------------------------------------------------------------
712
713 // transform to any given timezone
714 inline wxDateTime ToTimezone(const TimeZone& tz, bool noDST = FALSE) const;
715 wxDateTime& MakeTimezone(const TimeZone& tz, bool noDST = FALSE);
716
717 // transform to GMT/UTC
718 wxDateTime ToGMT(bool noDST = FALSE) const { return ToTimezone(GMT0, noDST); }
719 wxDateTime& MakeGMT(bool noDST = FALSE) { return MakeTimezone(GMT0, noDST); }
720
721 // is daylight savings time in effect at this moment according to the
722 // rules of the specified country?
723 //
724 // Return value is > 0 if DST is in effect, 0 if it is not and -1 if
725 // the information is not available (this is compatible with ANSI C)
726 int IsDST(Country country = Country_Default) const;
727
728 // accessors: many of them take the timezone parameter which indicates the
729 // timezone for which to make the calculations and the default value means
730 // to do it for the current timezone of this machine (even if the function
731 // only operates with the date it's necessary because a date may wrap as
732 // result of timezone shift)
733 // ------------------------------------------------------------------------
734
735 // is the date valid (TRUE even for non initialized objects)?
736 inline bool IsValid() const { return this != &wxInvalidDateTime; }
737
738 // get the broken down date/time representation in the given timezone
739 //
740 // If you wish to get several time components (day, month and year),
741 // consider getting the whole Tm strcuture first and retrieving the
742 // value from it - this is much more efficient
743 Tm GetTm(const TimeZone& tz = Local) const;
744
745 // get the number of seconds since the Unix epoch - returns (time_t)-1
746 // if the value is out of range
747 inline time_t GetTicks() const;
748
749 // get the year (returns Inv_Year if date is invalid)
750 int GetYear(const TimeZone& tz = Local) const
751 { return GetTm(tz).year; }
752 // get the month (Inv_Month if date is invalid)
753 Month GetMonth(const TimeZone& tz = Local) const
754 { return (Month)GetTm(tz).mon; }
755 // get the month day (in 1..31 range, 0 if date is invalid)
756 wxDateTime_t GetDay(const TimeZone& tz = Local) const
757 { return GetTm(tz).mday; }
758 // get the day of the week (Inv_WeekDay if date is invalid)
759 WeekDay GetWeekDay(const TimeZone& tz = Local) const
760 { return GetTm(tz).GetWeekDay(); }
761 // get the hour of the day
762 wxDateTime_t GetHour(const TimeZone& tz = Local) const
763 { return GetTm(tz).hour; }
764 // get the minute
765 wxDateTime_t GetMinute(const TimeZone& tz = Local) const
766 { return GetTm(tz).min; }
767 // get the second
768 wxDateTime_t GetSecond(const TimeZone& tz = Local) const
769 { return GetTm(tz).sec; }
770 // get milliseconds
771 wxDateTime_t GetMillisecond(const TimeZone& tz = Local) const
772 { return GetTm(tz).msec; }
773
774 // get the day since the year start (1..366, 0 if date is invalid)
775 wxDateTime_t GetDayOfYear(const TimeZone& tz = Local) const;
776 // get the week number since the year start (1..52 or 53, 0 if date is
777 // invalid)
778 wxDateTime_t GetWeekOfYear(WeekFlags flags = Monday_First,
779 const TimeZone& tz = Local) const;
780 // get the week number since the month start (1..5, 0 if date is
781 // invalid)
782 wxDateTime_t GetWeekOfMonth(WeekFlags flags = Monday_First,
783 const TimeZone& tz = Local) const;
784
785 // is this date a work day? This depends on a country, of course,
786 // because the holidays are different in different countries
787 bool IsWorkDay(Country country = Country_Default,
788 const TimeZone& tz = Local) const;
789
790 // is this date later than Gregorian calendar introduction for the
791 // given country (see enum GregorianAdoption)?
792 //
793 // NB: this function shouldn't be considered as absolute authoiruty in
794 // the matter. Besides, for some countries the exact date of
795 // adoption of the Gregorian calendar is simply unknown.
796 bool IsGregorianDate(GregorianAdoption country = Gr_Standard) const;
797
798 // comparison (see also functions below for operator versions)
799 // ------------------------------------------------------------------------
800
801 // returns TRUE if the two moments are strictly identical
802 inline bool IsEqualTo(const wxDateTime& datetime) const;
803
804 // returns TRUE if the date is strictly earlier than the given one
805 inline bool IsEarlierThan(const wxDateTime& datetime) const;
806
807 // returns TRUE if the date is strictly later than the given one
808 inline bool IsLaterThan(const wxDateTime& datetime) const;
809
810 // returns TRUE if the date is strictly in the given range
811 inline bool IsStrictlyBetween(const wxDateTime& t1,
812 const wxDateTime& t2) const;
813
814 // returns TRUE if the date is in the given range
815 inline bool IsBetween(const wxDateTime& t1, const wxDateTime& t2) const;
816
817 // do these two objects refer to the same date?
818 inline bool IsSameDate(const wxDateTime& dt) const;
819
820 // do these two objects have the same time?
821 inline bool IsSameTime(const wxDateTime& dt) const;
822
823 // are these two objects equal up to given timespan?
824 inline bool IsEqualUpTo(const wxDateTime& dt, const wxTimeSpan& ts) const;
825
826 // arithmetics with dates (see also below for more operators)
827 // ------------------------------------------------------------------------
828
829 // return the sum of the date with a time span (positive or negative)
830 inline wxDateTime Add(const wxTimeSpan& diff) const;
831 // add a time span (positive or negative)
832 inline wxDateTime& Add(const wxTimeSpan& diff);
833 // add a time span (positive or negative)
834 inline wxDateTime& operator+=(const wxTimeSpan& diff);
835
836 // return the difference of the date with a time span
837 inline wxDateTime Substract(const wxTimeSpan& diff) const;
838 // substract a time span (positive or negative)
839 inline wxDateTime& Substract(const wxTimeSpan& diff);
840 // substract a time span (positive or negative)
841 inline wxDateTime& operator-=(const wxTimeSpan& diff);
842
843 // return the sum of the date with a date span
844 inline wxDateTime Add(const wxDateSpan& diff) const;
845 // add a date span (positive or negative)
846 wxDateTime& Add(const wxDateSpan& diff);
847 // add a date span (positive or negative)
848 inline wxDateTime& operator+=(const wxDateSpan& diff);
849
850 // return the difference of the date with a date span
851 inline wxDateTime Substract(const wxDateSpan& diff) const;
852 // substract a date span (positive or negative)
853 inline wxDateTime& Substract(const wxDateSpan& diff);
854 // substract a date span (positive or negative)
855 inline wxDateTime& operator-=(const wxDateSpan& diff);
856
857 // return the difference between two dates
858 inline wxTimeSpan Substract(const wxDateTime& dt) const;
859
860 // conversion to/from text: all conversions from text return the pointer to
861 // the next character following the date specification (i.e. the one where
862 // the scan had to stop) or NULL on failure.
863 // ------------------------------------------------------------------------
864
865 // parse a string in RFC 822 format (found e.g. in mail headers and
866 // having the form "Wed, 10 Feb 1999 19:07:07 +0100")
867 const wxChar *ParseRfc822Date(const wxChar* date);
868 // parse a date/time in the given format (see strptime(3)), fill in
869 // the missing (in the string) fields with the values of dateDef (by
870 // default, they will not change if they had valid values or will
871 // default to Today() otherwise)
872 const wxChar *ParseFormat(const wxChar *date,
873 const wxChar *format = _T("%c"),
874 const wxDateTime& dateDef = wxDefaultDateTime);
875 // parse a string containing the date/time in "free" format, this
876 // function will try to make an educated guess at the string contents
877 const wxChar *ParseDateTime(const wxChar *datetime);
878 // parse a string containing the date only in "free" format (less
879 // flexible than ParseDateTime)
880 const wxChar *ParseDate(const wxChar *date);
881 // parse a string containing the time only in "free" format
882 const wxChar *ParseTime(const wxChar *time);
883
884 // this function accepts strftime()-like format string (default
885 // argument corresponds to the preferred date and time representation
886 // for the current locale) and returns the string containing the
887 // resulting text representation
888 wxString Format(const wxChar *format = _T("%c"),
889 const TimeZone& tz = Local) const;
890 // preferred date representation for the current locale
891 wxString FormatDate() const { return Format(_T("%x")); }
892 // preferred time representation for the current locale
893 wxString FormatTime() const { return Format(_T("%X")); }
894 // return the string representing the date in ISO 8601 format
895 // (YYYY-MM-DD)
896 wxString FormatISODate() const { return Format(_T("%Y-%m-%d")); }
897
898 // implementation
899 // ------------------------------------------------------------------------
900
901 // construct from internal representation
902 wxDateTime(const wxLongLong& time) { m_time = time; }
903
904 // get the internal representation
905 inline wxLongLong GetValue() const;
906
907 // a helper function to get the current time_t
908 static time_t GetTimeNow() { return time((time_t *)NULL); }
909
910 // another one to get the current time broken down
911 static struct tm *GetTmNow()
912 {
913 time_t t = GetTimeNow();
914 return localtime(&t);
915 }
916
917 private:
918 // the current country - as it's the same for all program objects (unless
919 // it runs on a _really_ big cluster system :-), this is a static member:
920 // see SetCountry() and GetCountry()
921 static Country ms_country;
922
923 // this constant is used to transform a time_t value to the internal
924 // representation, as time_t is in seconds and we use milliseconds it's
925 // fixed to 1000
926 static const long TIME_T_FACTOR;
927
928 // returns TRUE if we fall in range in which we can use standard ANSI C
929 // functions
930 inline bool IsInStdRange() const;
931
932 // the internal representation of the time is the amount of milliseconds
933 // elapsed since the origin which is set by convention to the UNIX/C epoch
934 // value: the midnight of January 1, 1970 (UTC)
935 wxLongLong m_time;
936 };
937
938 // ----------------------------------------------------------------------------
939 // This class contains a difference between 2 wxDateTime values, so it makes
940 // sense to add it to wxDateTime and it is the result of substraction of 2
941 // objects of that class. See also wxDateSpan.
942 // ----------------------------------------------------------------------------
943
944 class WXDLLEXPORT wxTimeSpan
945 {
946 public:
947 // constructors
948 // ------------------------------------------------------------------------
949
950 // return the timespan for the given number of seconds
951 static wxTimeSpan Seconds(int sec) { return wxTimeSpan(0, 0, sec); }
952 static wxTimeSpan Second() { return Seconds(1); }
953
954 // return the timespan for the given number of minutes
955 static wxTimeSpan Minutes(int min) { return wxTimeSpan(0, min, 0 ); }
956 static wxTimeSpan Minute() { return Minutes(1); }
957
958 // return the timespan for the given number of hours
959 static wxTimeSpan Hours(int hours) { return wxTimeSpan(hours, 0, 0); }
960 static wxTimeSpan Hour() { return Hours(1); }
961
962 // return the timespan for the given number of days
963 static wxTimeSpan Days(int days) { return Hours(24 * days); }
964 static wxTimeSpan Day() { return Days(1); }
965
966 // return the timespan for the given number of weeks
967 static wxTimeSpan Weeks(int days) { return Days(7 * days); }
968 static wxTimeSpan Week() { return Weeks(1); }
969
970 // default ctor constructs the 0 time span
971 wxTimeSpan() { }
972
973 // from separate values for each component, date set to 0 (hours are
974 // not restricted to 0..24 range, neither are minutes, seconds or
975 // milliseconds)
976 inline wxTimeSpan(int hours,
977 int minutes = 0,
978 int seconds = 0,
979 int milliseconds = 0);
980
981 // default copy ctor is ok
982
983 // no dtor
984
985 // arithmetics with time spans (see also below for more operators)
986 // ------------------------------------------------------------------------
987
988 // return the sum of two timespans
989 inline wxTimeSpan Add(const wxTimeSpan& diff) const;
990 // add two timespans together
991 inline wxTimeSpan& Add(const wxTimeSpan& diff);
992 // add two timespans together
993 wxTimeSpan& operator+=(const wxTimeSpan& diff) { return Add(diff); }
994
995 // return the difference of two timespans
996 inline wxTimeSpan Substract(const wxTimeSpan& diff) const;
997 // substract another timespan
998 inline wxTimeSpan& Substract(const wxTimeSpan& diff);
999 // substract another timespan
1000 wxTimeSpan& operator-=(const wxTimeSpan& diff) { return Substract(diff); }
1001
1002 // multiply timespan by a scalar
1003 inline wxTimeSpan Multiply(int n) const;
1004 // multiply timespan by a scalar
1005 inline wxTimeSpan& Multiply(int n);
1006 // multiply timespan by a scalar
1007 wxTimeSpan& operator*=(int n) { return Multiply(n); }
1008
1009 // return this timespan with inversed sign
1010 wxTimeSpan Negate() const { return wxTimeSpan(-GetValue()); }
1011 // negate the value of the timespan
1012 wxTimeSpan& Neg() { m_diff = -GetValue(); return *this; }
1013 // negate the value of the timespan
1014 wxTimeSpan& operator-() { return Neg(); }
1015
1016 // return the absolute value of the timespan: does _not_ modify the
1017 // object
1018 inline wxTimeSpan Abs() const;
1019
1020 // there is intentionally no division because we don't want to
1021 // introduce rounding errors in time calculations
1022
1023 // comparaison (see also operator versions below)
1024 // ------------------------------------------------------------------------
1025
1026 // is the timespan null?
1027 bool IsNull() const { return m_diff == 0l; }
1028 // returns true if the timespan is null
1029 bool operator!() const { return !IsNull(); }
1030
1031 // is the timespan positive?
1032 bool IsPositive() const { return m_diff > 0l; }
1033
1034 // is the timespan negative?
1035 bool IsNegative() const { return m_diff < 0l; }
1036
1037 // are two timespans equal?
1038 inline bool IsEqualTo(const wxTimeSpan& ts) const;
1039 // compare two timestamps: works with the absolute values, i.e. -2
1040 // hours is longer than 1 hour. Also, it will return FALSE if the
1041 // timespans are equal in absolute value.
1042 inline bool IsLongerThan(const wxTimeSpan& ts) const;
1043 // compare two timestamps: works with the absolute values, i.e. 1
1044 // hour is shorter than -2 hours. Also, it will return FALSE if the
1045 // timespans are equal in absolute value.
1046 bool IsShorterThan(const wxTimeSpan& t) const { return !IsLongerThan(t); }
1047
1048 // breaking into days, hours, minutes and seconds
1049 // ------------------------------------------------------------------------
1050
1051 // get the max number of weeks in this timespan
1052 inline int GetWeeks() const;
1053 // get the max number of days in this timespan
1054 inline int GetDays() const;
1055 // get the max number of hours in this timespan
1056 inline int GetHours() const;
1057 // get the max number of minutes in this timespan
1058 inline int GetMinutes() const;
1059 // get the max number of seconds in this timespan
1060 inline wxLongLong GetSeconds() const;
1061 // get the number of milliseconds in this timespan
1062 wxLongLong GetMilliseconds() const { return m_diff; }
1063
1064 // conversion to text
1065 // ------------------------------------------------------------------------
1066
1067 // this function accepts strftime()-like format string (default
1068 // argument corresponds to the preferred date and time representation
1069 // for the current locale) and returns the string containing the
1070 // resulting text representation. Notice that only some of format
1071 // specifiers valid for wxDateTime are valid for wxTimeSpan: hours,
1072 // minutes and seconds make sense, but not "PM/AM" string for example.
1073 wxString Format(const wxChar *format = _T("%c")) const;
1074 // preferred date representation for the current locale
1075 wxString FormatDate() const { return Format(_T("%x")); }
1076 // preferred time representation for the current locale
1077 wxString FormatTime() const { return Format(_T("%X")); }
1078
1079 // implementation
1080 // ------------------------------------------------------------------------
1081
1082 // construct from internal representation
1083 wxTimeSpan(const wxLongLong& diff) { m_diff = diff; }
1084
1085 // get the internal representation
1086 wxLongLong GetValue() const { return m_diff; }
1087
1088 private:
1089 // the (signed) time span in milliseconds
1090 wxLongLong m_diff;
1091 };
1092
1093 // ----------------------------------------------------------------------------
1094 // This class is a "logical time span" and is useful for implementing program
1095 // logic for such things as "add one month to the date" which, in general,
1096 // doesn't mean to add 60*60*24*31 seconds to it, but to take the same date
1097 // the next month (to understand that this is indeed different consider adding
1098 // one month to Feb, 15 - we want to get Mar, 15, of course).
1099 //
1100 // When adding a month to the date, all lesser components (days, hours, ...)
1101 // won't be changed.
1102 //
1103 // wxDateSpan can be either positive or negative. They may be
1104 // multiplied by scalars which multiply all deltas by the scalar: i.e. 2*(1
1105 // month and 1 day) is 2 months and 2 days. They can be added together and
1106 // with wxDateTime or wxTimeSpan, but the type of result is different for each
1107 // case.
1108 //
1109 // Beware about weeks: if you specify both weeks and days, the total number of
1110 // days added will be 7*weeks + days! See also GetTotalDays() function.
1111 //
1112 // Finally, notice that for adding hours, minutes &c you don't need this
1113 // class: wxTimeSpan will do the job because there are no subtleties
1114 // associated with those.
1115 // ----------------------------------------------------------------------------
1116
1117 class WXDLLEXPORT wxDateSpan
1118 {
1119 public:
1120 // constructors
1121 // ------------------------------------------------------------------------
1122
1123 // this many years/months/weeks/days
1124 wxDateSpan(int years = 0, int months = 0, int weeks = 0, int days = 0)
1125 {
1126 m_years = years;
1127 m_months = months;
1128 m_weeks = weeks;
1129 m_days = days;
1130 }
1131
1132 // get an object for the given number of days
1133 static wxDateSpan Days(int days) { return wxDateSpan(0, 0, 0, days); }
1134 static wxDateSpan Day() { return Days(1); }
1135
1136 // get an object for the given number of weeks
1137 static wxDateSpan Weeks(int weeks) { return wxDateSpan(0, 0, weeks, 0); }
1138 static wxDateSpan Week() { return Weeks(1); }
1139
1140 // get an object for the given number of months
1141 static wxDateSpan Months(int mon) { return wxDateSpan(0, mon, 0, 0); }
1142 static wxDateSpan Month() { return Months(1); }
1143
1144 // get an object for the given number of years
1145 static wxDateSpan Years(int years) { return wxDateSpan(years, 0, 0, 0); }
1146 static wxDateSpan Year() { return Years(1); }
1147
1148 // default copy ctor is ok
1149
1150 // no dtor
1151
1152 // accessors (all SetXXX() return the (modified) wxDateSpan object)
1153 // ------------------------------------------------------------------------
1154
1155 // set number of years
1156 wxDateSpan& SetYears(int n) { m_years = n; return *this; }
1157 // set number of months
1158 wxDateSpan& SetMonths(int n) { m_months = n; return *this; }
1159 // set number of weeks
1160 wxDateSpan& SetWeeks(int n) { m_weeks = n; return *this; }
1161 // set number of days
1162 wxDateSpan& SetDays(int n) { m_days = n; return *this; }
1163
1164 // get number of years
1165 int GetYears() const { return m_years; }
1166 // get number of months
1167 int GetMonths() const { return m_months; }
1168 // get number of weeks
1169 int GetWeeks() const { return m_weeks; }
1170 // get number of days
1171 int GetDays() const { return m_days; }
1172 // returns 7*GetWeeks() + GetDays()
1173 int GetTotalDays() const { return 7*m_weeks + m_days; }
1174
1175 // arithmetics with date spans (see also below for more operators)
1176 // ------------------------------------------------------------------------
1177
1178 // return sum of two date spans
1179 inline wxDateSpan Add(const wxDateSpan& other) const;
1180 // add another wxDateSpan to us
1181 inline wxDateSpan& Add(const wxDateSpan& other);
1182 // add another wxDateSpan to us
1183 inline wxDateSpan& operator+=(const wxDateSpan& other);
1184
1185 // return difference of two date spans
1186 inline wxDateSpan Substract(const wxDateSpan& other) const;
1187 // substract another wxDateSpan from us
1188 inline wxDateSpan& Substract(const wxDateSpan& other);
1189 // substract another wxDateSpan from us
1190 inline wxDateSpan& operator-=(const wxDateSpan& other);
1191
1192 // return a copy of this time span with changed sign
1193 inline wxDateSpan Negate() const;
1194 // inverse the sign of this timespan
1195 inline wxDateSpan& Neg();
1196 // inverse the sign of this timespan
1197 wxDateSpan& operator-() { return Neg(); }
1198
1199 // return the date span proportional to this one with given factor
1200 inline wxDateSpan Multiply(int factor) const;
1201 // multiply all components by a (signed) number
1202 inline wxDateSpan& Multiply(int factor);
1203 // multiply all components by a (signed) number
1204 inline wxDateSpan& operator*=(int factor) { return Multiply(factor); }
1205
1206 private:
1207 int m_years,
1208 m_months,
1209 m_weeks,
1210 m_days;
1211 };
1212
1213 WXDLLEXPORT_DATA(extern wxDateSpan) wxYear;
1214 WXDLLEXPORT_DATA(extern wxDateSpan) wxMonth;
1215 WXDLLEXPORT_DATA(extern wxDateSpan) wxWeek;
1216 WXDLLEXPORT_DATA(extern wxDateSpan) wxDay;
1217
1218 // ============================================================================
1219 // inline functions implementation
1220 // ============================================================================
1221
1222 // don't include inline functions definitions when we're included from anything
1223 // else than datetime.cpp in debug builds: this minimizes rebuilds if we change
1224 // some inline function and the performance doesn't matter in the debug builds.
1225
1226 #if !defined(__WXDEBUG__) || defined(wxDEFINE_TIME_CONSTANTS)
1227 #define INCLUDED_FROM_WX_DATETIME_H
1228 #include "wx/datetime.inl"
1229 #undef INCLUDED_FROM_WX_DATETIME_H
1230 #endif
1231
1232 // if we defined it to be empty above, restore it now
1233 #undef inline
1234
1235 // ============================================================================
1236 // binary operators
1237 // ============================================================================
1238
1239 // ----------------------------------------------------------------------------
1240 // wxDateTime operators
1241 // ----------------------------------------------------------------------------
1242
1243 // arithmetics
1244 // -----------
1245
1246 // no need to check for validity - the member functions we call will do it
1247
1248 inline wxDateTime WXDLLEXPORT operator+(const wxDateTime& dt,
1249 const wxTimeSpan& ts)
1250 {
1251 return dt.Add(ts);
1252 }
1253
1254 inline wxDateTime WXDLLEXPORT operator-(const wxDateTime& dt,
1255 const wxTimeSpan& ts)
1256 {
1257 return dt.Substract(ts);
1258 }
1259
1260 inline wxDateTime WXDLLEXPORT operator+(const wxDateTime& dt,
1261 const wxDateSpan& ds)
1262 {
1263 return dt.Add(ds);
1264 }
1265
1266 inline wxDateTime WXDLLEXPORT operator-(const wxDateTime& dt,
1267 const wxDateSpan& ds)
1268 {
1269 return dt.Substract(ds);
1270 }
1271
1272 inline wxTimeSpan WXDLLEXPORT operator-(const wxDateTime& dt1,
1273 const wxDateTime& dt2)
1274 {
1275 return dt1.Substract(dt2);
1276 }
1277
1278 // comparison
1279 // ----------
1280
1281 inline bool WXDLLEXPORT operator<(const wxDateTime& t1, const wxDateTime& t2)
1282 {
1283 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") );
1284
1285 return t1.GetValue() < t2.GetValue();
1286 }
1287
1288 inline bool WXDLLEXPORT operator<=(const wxDateTime& t1, const wxDateTime& t2)
1289 {
1290 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") );
1291
1292 return t1.GetValue() <= t2.GetValue();
1293 }
1294
1295 inline bool WXDLLEXPORT operator>(const wxDateTime& t1, const wxDateTime& t2)
1296 {
1297 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") );
1298
1299 return t1.GetValue() > t2.GetValue();
1300 }
1301
1302 inline bool WXDLLEXPORT operator>=(const wxDateTime& t1, const wxDateTime& t2)
1303 {
1304 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") );
1305
1306 return t1.GetValue() >= t2.GetValue();
1307 }
1308
1309 inline bool WXDLLEXPORT operator==(const wxDateTime& t1, const wxDateTime& t2)
1310 {
1311 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") );
1312
1313 return t1.GetValue() == t2.GetValue();
1314 }
1315
1316 inline bool WXDLLEXPORT operator!=(const wxDateTime& t1, const wxDateTime& t2)
1317 {
1318 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), _T("invalid wxDateTime") );
1319
1320 return t1.GetValue() != t2.GetValue();
1321 }
1322
1323 // ----------------------------------------------------------------------------
1324 // wxTimeSpan operators
1325 // ----------------------------------------------------------------------------
1326
1327 // arithmetics
1328 // -----------
1329
1330 inline wxTimeSpan WXDLLEXPORT operator+(const wxTimeSpan& ts1,
1331 const wxTimeSpan& ts2)
1332 {
1333 return wxTimeSpan(ts1.GetValue() + ts2.GetValue());
1334 }
1335
1336 inline wxTimeSpan WXDLLEXPORT operator-(const wxTimeSpan& ts1,
1337 const wxTimeSpan& ts2)
1338 {
1339 return wxTimeSpan(ts1.GetValue() - ts2.GetValue());
1340 }
1341
1342 inline wxTimeSpan WXDLLEXPORT operator*(const wxTimeSpan& ts, int n)
1343 {
1344 return wxTimeSpan(ts).Multiply(n);
1345 }
1346
1347 inline wxTimeSpan WXDLLEXPORT operator*(int n, const wxTimeSpan& ts)
1348 {
1349 return wxTimeSpan(ts).Multiply(n);
1350 }
1351
1352 // comparison
1353 // ----------
1354
1355 inline bool WXDLLEXPORT operator<(const wxTimeSpan &t1, const wxTimeSpan &t2)
1356 {
1357 return t1.GetValue() < t2.GetValue();
1358 }
1359
1360 inline bool WXDLLEXPORT operator<=(const wxTimeSpan &t1, const wxTimeSpan &t2)
1361 {
1362 return t1.GetValue() <= t2.GetValue();
1363 }
1364
1365 inline bool WXDLLEXPORT operator>(const wxTimeSpan &t1, const wxTimeSpan &t2)
1366 {
1367 return t1.GetValue() > t2.GetValue();
1368 }
1369
1370 inline bool WXDLLEXPORT operator>=(const wxTimeSpan &t1, const wxTimeSpan &t2)
1371 {
1372 return t1.GetValue() >= t2.GetValue();
1373 }
1374
1375 inline bool WXDLLEXPORT operator==(const wxTimeSpan &t1, const wxTimeSpan &t2)
1376 {
1377 return t1.GetValue() == t2.GetValue();
1378 }
1379
1380 inline bool WXDLLEXPORT operator!=(const wxTimeSpan &t1, const wxTimeSpan &t2)
1381 {
1382 return t1.GetValue() != t2.GetValue();
1383 }
1384
1385 // ----------------------------------------------------------------------------
1386 // wxDateSpan
1387 // ----------------------------------------------------------------------------
1388
1389 // arithmetics
1390 // -----------
1391
1392 inline WXDLLEXPORT wxDateSpan operator+(const wxDateSpan& ds1,
1393 const wxDateSpan& ds2)
1394 {
1395 return wxDateSpan(ds1.GetYears() + ds2.GetYears(),
1396 ds1.GetMonths() + ds2.GetMonths(),
1397 ds1.GetWeeks() + ds2.GetWeeks(),
1398 ds1.GetDays() + ds2.GetDays());
1399 }
1400
1401 inline WXDLLEXPORT wxDateSpan operator-(const wxDateSpan& ds1,
1402 const wxDateSpan& ds2)
1403 {
1404 return wxDateSpan(ds1.GetYears() - ds2.GetYears(),
1405 ds1.GetMonths() - ds2.GetMonths(),
1406 ds1.GetWeeks() - ds2.GetWeeks(),
1407 ds1.GetDays() - ds2.GetDays());
1408 }
1409
1410 inline WXDLLEXPORT wxDateSpan operator*(const wxDateSpan& ds, int n)
1411 {
1412 return wxDateSpan(ds).Multiply(n);
1413 }
1414
1415 inline WXDLLEXPORT wxDateSpan operator*(int n, const wxDateSpan& ds)
1416 {
1417 return wxDateSpan(ds).Multiply(n);
1418 }
1419
1420 // ============================================================================
1421 // other helper functions
1422 // ============================================================================
1423
1424 // ----------------------------------------------------------------------------
1425 // iteration helpers: can be used to write a for loop over enum variable like
1426 // this:
1427 // for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) )
1428 // ----------------------------------------------------------------------------
1429
1430 inline WXDLLEXPORT void wxNextMonth(wxDateTime::Month& m)
1431 {
1432 wxASSERT_MSG( m < wxDateTime::Inv_Month, _T("invalid month") );
1433
1434 // no wrapping or the for loop above would never end!
1435 m = (wxDateTime::Month)(m + 1);
1436 }
1437
1438 inline WXDLLEXPORT void wxPrevMonth(wxDateTime::Month& m)
1439 {
1440 wxASSERT_MSG( m < wxDateTime::Inv_Month, _T("invalid month") );
1441
1442 m = m == wxDateTime::Jan ? wxDateTime::Inv_Month
1443 : (wxDateTime::Month)(m - 1);
1444 }
1445
1446 inline WXDLLEXPORT void wxNextWDay(wxDateTime::WeekDay& wd)
1447 {
1448 wxASSERT_MSG( wd < wxDateTime::Inv_WeekDay, _T("invalid week day") );
1449
1450 // no wrapping or the for loop above would never end!
1451 wd = (wxDateTime::WeekDay)(wd + 1);
1452 }
1453
1454 inline WXDLLEXPORT void wxPrevWDay(wxDateTime::WeekDay& wd)
1455 {
1456 wxASSERT_MSG( wd < wxDateTime::Inv_WeekDay, _T("invalid week day") );
1457
1458 wd = wd == wxDateTime::Sun ? wxDateTime::Inv_WeekDay
1459 : (wxDateTime::WeekDay)(wd - 1);
1460 }
1461
1462 #endif // _WX_DATETIME_H