]> git.saurik.com Git - wxWidgets.git/blob - include/wx/datetime.h
more extra semicolons removed (patch 1303724)
[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 licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_DATETIME_H
14 #define _WX_DATETIME_H
15
16 #include "wx/defs.h"
17
18 #if wxUSE_DATETIME
19
20 #ifndef __WXWINCE__
21 #include <time.h>
22 #else
23 #include "wx/msw/wince/time.h"
24 #endif
25
26 #include <limits.h> // for INT_MIN
27
28 #include "wx/longlong.h"
29
30 class WXDLLIMPEXP_BASE wxDateTime;
31 class WXDLLIMPEXP_BASE wxTimeSpan;
32 class WXDLLIMPEXP_BASE wxDateSpan;
33
34 #include "wx/dynarray.h"
35
36 // not all c-runtimes are based on 1/1/1970 being (time_t) 0
37 // set this to the corresponding value in seconds 1/1/1970 has on your
38 // systems c-runtime
39
40 #if defined(__WXMAC__) && !defined(__DARWIN__) && __MSL__ < 0x6000
41 #define WX_TIME_BASE_OFFSET ( 2082844800L + 126144000L )
42 #else
43 #define WX_TIME_BASE_OFFSET 0
44 #endif
45 /*
46 * TODO
47 *
48 * + 1. Time zones with minutes (make TimeZone a class)
49 * ? 2. getdate() function like under Solaris
50 * + 3. text conversion for wxDateSpan
51 * + 4. pluggable modules for the workdays calculations
52 * 5. wxDateTimeHolidayAuthority for Easter and other christian feasts
53 */
54
55 /*
56 The three (main) classes declared in this header represent:
57
58 1. An absolute moment in the time (wxDateTime)
59 2. A difference between two moments in the time, positive or negative
60 (wxTimeSpan)
61 3. A logical difference between two dates expressed in
62 years/months/weeks/days (wxDateSpan)
63
64 The following arithmetic operations are permitted (all others are not):
65
66 addition
67 --------
68
69 wxDateTime + wxTimeSpan = wxDateTime
70 wxDateTime + wxDateSpan = wxDateTime
71 wxTimeSpan + wxTimeSpan = wxTimeSpan
72 wxDateSpan + wxDateSpan = wxDateSpan
73
74 subtraction
75 ------------
76 wxDateTime - wxDateTime = wxTimeSpan
77 wxDateTime - wxTimeSpan = wxDateTime
78 wxDateTime - wxDateSpan = wxDateTime
79 wxTimeSpan - wxTimeSpan = wxTimeSpan
80 wxDateSpan - wxDateSpan = wxDateSpan
81
82 multiplication
83 --------------
84 wxTimeSpan * number = wxTimeSpan
85 number * wxTimeSpan = wxTimeSpan
86 wxDateSpan * number = wxDateSpan
87 number * wxDateSpan = wxDateSpan
88
89 unitary minus
90 -------------
91 -wxTimeSpan = wxTimeSpan
92 -wxDateSpan = wxDateSpan
93
94 For each binary operation OP (+, -, *) we have the following operatorOP=() as
95 a method and the method with a symbolic name OPER (Add, Subtract, Multiply)
96 as a synonym for it and another const method with the same name which returns
97 the changed copy of the object and operatorOP() as a global function which is
98 implemented in terms of the const version of OPEN. For the unary - we have
99 operator-() as a method, Neg() as synonym for it and Negate() which returns
100 the copy of the object with the changed sign.
101 */
102
103 // an invalid/default date time object which may be used as the default
104 // argument for arguments of type wxDateTime; it is also returned by all
105 // functions returning wxDateTime on failure (this is why it is also called
106 // wxInvalidDateTime)
107 class WXDLLIMPEXP_BASE wxDateTime;
108
109 extern WXDLLIMPEXP_DATA_BASE(const wxChar*) wxDefaultDateTimeFormat;
110 extern WXDLLIMPEXP_DATA_BASE(const wxChar*) wxDefaultTimeSpanFormat;
111 extern WXDLLIMPEXP_DATA_BASE(const wxDateTime) wxDefaultDateTime;
112
113 #define wxInvalidDateTime wxDefaultDateTime
114
115 // ----------------------------------------------------------------------------
116 // wxDateTime represents an absolute moment in the time
117 // ----------------------------------------------------------------------------
118
119 class WXDLLIMPEXP_BASE wxDateTime
120 {
121 public:
122 // types
123 // ------------------------------------------------------------------------
124
125 // a small unsigned integer type for storing things like minutes,
126 // seconds &c. It should be at least short (i.e. not char) to contain
127 // the number of milliseconds - it may also be 'int' because there is
128 // no size penalty associated with it in our code, we don't store any
129 // data in this format
130 typedef unsigned short wxDateTime_t;
131
132 // constants
133 // ------------------------------------------------------------------------
134
135 // the timezones
136 enum TZ
137 {
138 // the time in the current time zone
139 Local,
140
141 // zones from GMT (= Greenwhich Mean Time): they're guaranteed to be
142 // consequent numbers, so writing something like `GMT0 + offset' is
143 // safe if abs(offset) <= 12
144
145 // underscore stands for minus
146 GMT_12, GMT_11, GMT_10, GMT_9, GMT_8, GMT_7,
147 GMT_6, GMT_5, GMT_4, GMT_3, GMT_2, GMT_1,
148 GMT0,
149 GMT1, GMT2, GMT3, GMT4, GMT5, GMT6,
150 GMT7, GMT8, GMT9, GMT10, GMT11, GMT12,
151 // Note that GMT12 and GMT_12 are not the same: there is a difference
152 // of exactly one day between them
153
154 // some symbolic names for TZ
155
156 // Europe
157 WET = GMT0, // Western Europe Time
158 WEST = GMT1, // Western Europe Summer Time
159 CET = GMT1, // Central Europe Time
160 CEST = GMT2, // Central Europe Summer Time
161 EET = GMT2, // Eastern Europe Time
162 EEST = GMT3, // Eastern Europe Summer Time
163 MSK = GMT3, // Moscow Time
164 MSD = GMT4, // Moscow Summer Time
165
166 // US and Canada
167 AST = GMT_4, // Atlantic Standard Time
168 ADT = GMT_3, // Atlantic Daylight Time
169 EST = GMT_5, // Eastern Standard Time
170 EDT = GMT_4, // Eastern Daylight Saving Time
171 CST = GMT_6, // Central Standard Time
172 CDT = GMT_5, // Central Daylight Saving Time
173 MST = GMT_7, // Mountain Standard Time
174 MDT = GMT_6, // Mountain Daylight Saving Time
175 PST = GMT_8, // Pacific Standard Time
176 PDT = GMT_7, // Pacific Daylight Saving Time
177 HST = GMT_10, // Hawaiian Standard Time
178 AKST = GMT_9, // Alaska Standard Time
179 AKDT = GMT_8, // Alaska Daylight Saving Time
180
181 // Australia
182
183 A_WST = GMT8, // Western Standard Time
184 A_CST = GMT12 + 1, // Central Standard Time (+9.5)
185 A_EST = GMT10, // Eastern Standard Time
186 A_ESST = GMT11, // Eastern Summer Time
187
188 // TODO add more symbolic timezone names here
189
190 // Universal Coordinated Time = the new and politically correct name
191 // for GMT
192 UTC = GMT0
193 };
194
195 // the calendar systems we know about: notice that it's valid (for
196 // this classes purpose anyhow) to work with any of these calendars
197 // even with the dates before the historical appearance of the
198 // calendar
199 enum Calendar
200 {
201 Gregorian, // current calendar
202 Julian // calendar in use since -45 until the 1582 (or later)
203
204 // TODO Hebrew, Chinese, Maya, ... (just kidding) (or then may be not?)
205 };
206
207 // these values only are used to identify the different dates of
208 // adoption of the Gregorian calendar (see IsGregorian())
209 //
210 // All data and comments taken verbatim from "The Calendar FAQ (v 2.0)"
211 // by Claus Tøndering, http://www.pip.dknet.dk/~c-t/calendar.html
212 // except for the comments "we take".
213 //
214 // Symbol "->" should be read as "was followed by" in the comments
215 // which follow.
216 enum GregorianAdoption
217 {
218 Gr_Unknown, // no data for this country or it's too uncertain to use
219 Gr_Standard, // on the day 0 of Gregorian calendar: 15 Oct 1582
220
221 Gr_Alaska, // Oct 1867 when Alaska became part of the USA
222 Gr_Albania, // Dec 1912
223
224 Gr_Austria = Gr_Unknown, // Different regions on different dates
225 Gr_Austria_Brixen, // 5 Oct 1583 -> 16 Oct 1583
226 Gr_Austria_Salzburg = Gr_Austria_Brixen,
227 Gr_Austria_Tyrol = Gr_Austria_Brixen,
228 Gr_Austria_Carinthia, // 14 Dec 1583 -> 25 Dec 1583
229 Gr_Austria_Styria = Gr_Austria_Carinthia,
230
231 Gr_Belgium, // Then part of the Netherlands
232
233 Gr_Bulgaria = Gr_Unknown, // Unknown precisely (from 1915 to 1920)
234 Gr_Bulgaria_1, // 18 Mar 1916 -> 1 Apr 1916
235 Gr_Bulgaria_2, // 31 Mar 1916 -> 14 Apr 1916
236 Gr_Bulgaria_3, // 3 Sep 1920 -> 17 Sep 1920
237
238 Gr_Canada = Gr_Unknown, // Different regions followed the changes in
239 // Great Britain or France
240
241 Gr_China = Gr_Unknown, // Different authorities say:
242 Gr_China_1, // 18 Dec 1911 -> 1 Jan 1912
243 Gr_China_2, // 18 Dec 1928 -> 1 Jan 1929
244
245 Gr_Czechoslovakia, // (Bohemia and Moravia) 6 Jan 1584 -> 17 Jan 1584
246 Gr_Denmark, // (including Norway) 18 Feb 1700 -> 1 Mar 1700
247 Gr_Egypt, // 1875
248 Gr_Estonia, // 1918
249 Gr_Finland, // Then part of Sweden
250
251 Gr_France, // 9 Dec 1582 -> 20 Dec 1582
252 Gr_France_Alsace, // 4 Feb 1682 -> 16 Feb 1682
253 Gr_France_Lorraine, // 16 Feb 1760 -> 28 Feb 1760
254 Gr_France_Strasbourg, // February 1682
255
256 Gr_Germany = Gr_Unknown, // Different states on different dates:
257 Gr_Germany_Catholic, // 1583-1585 (we take 1584)
258 Gr_Germany_Prussia, // 22 Aug 1610 -> 2 Sep 1610
259 Gr_Germany_Protestant, // 18 Feb 1700 -> 1 Mar 1700
260
261 Gr_GreatBritain, // 2 Sep 1752 -> 14 Sep 1752 (use 'cal(1)')
262
263 Gr_Greece, // 9 Mar 1924 -> 23 Mar 1924
264 Gr_Hungary, // 21 Oct 1587 -> 1 Nov 1587
265 Gr_Ireland = Gr_GreatBritain,
266 Gr_Italy = Gr_Standard,
267
268 Gr_Japan = Gr_Unknown, // Different authorities say:
269 Gr_Japan_1, // 19 Dec 1872 -> 1 Jan 1873
270 Gr_Japan_2, // 19 Dec 1892 -> 1 Jan 1893
271 Gr_Japan_3, // 18 Dec 1918 -> 1 Jan 1919
272
273 Gr_Latvia, // 1915-1918 (we take 1915)
274 Gr_Lithuania, // 1915
275 Gr_Luxemburg, // 14 Dec 1582 -> 25 Dec 1582
276 Gr_Netherlands = Gr_Belgium, // (including Belgium) 1 Jan 1583
277
278 // this is too weird to take into account: the Gregorian calendar was
279 // introduced twice in Groningen, first time 28 Feb 1583 was followed
280 // by 11 Mar 1583, then it has gone back to Julian in the summer of
281 // 1584 and then 13 Dec 1700 -> 12 Jan 1701 - which is
282 // the date we take here
283 Gr_Netherlands_Groningen, // 13 Dec 1700 -> 12 Jan 1701
284 Gr_Netherlands_Gelderland, // 30 Jun 1700 -> 12 Jul 1700
285 Gr_Netherlands_Utrecht, // (and Overijssel) 30 Nov 1700->12 Dec 1700
286 Gr_Netherlands_Friesland, // (and Drenthe) 31 Dec 1700 -> 12 Jan 1701
287
288 Gr_Norway = Gr_Denmark, // Then part of Denmark
289 Gr_Poland = Gr_Standard,
290 Gr_Portugal = Gr_Standard,
291 Gr_Romania, // 31 Mar 1919 -> 14 Apr 1919
292 Gr_Russia, // 31 Jan 1918 -> 14 Feb 1918
293 Gr_Scotland = Gr_GreatBritain,
294 Gr_Spain = Gr_Standard,
295
296 // Sweden has a curious history. Sweden decided to make a gradual
297 // change from the Julian to the Gregorian calendar. By dropping every
298 // leap year from 1700 through 1740 the eleven superfluous days would
299 // be omitted and from 1 Mar 1740 they would be in sync with the
300 // Gregorian calendar. (But in the meantime they would be in sync with
301 // nobody!)
302 //
303 // So 1700 (which should have been a leap year in the Julian calendar)
304 // was not a leap year in Sweden. However, by mistake 1704 and 1708
305 // became leap years. This left Sweden out of synchronisation with
306 // both the Julian and the Gregorian world, so they decided to go back
307 // to the Julian calendar. In order to do this, they inserted an extra
308 // day in 1712, making that year a double leap year! So in 1712,
309 // February had 30 days in Sweden.
310 //
311 // Later, in 1753, Sweden changed to the Gregorian calendar by
312 // dropping 11 days like everyone else.
313 Gr_Sweden = Gr_Finland, // 17 Feb 1753 -> 1 Mar 1753
314
315 Gr_Switzerland = Gr_Unknown,// Different cantons used different dates
316 Gr_Switzerland_Catholic, // 1583, 1584 or 1597 (we take 1584)
317 Gr_Switzerland_Protestant, // 31 Dec 1700 -> 12 Jan 1701
318
319 Gr_Turkey, // 1 Jan 1927
320 Gr_USA = Gr_GreatBritain,
321 Gr_Wales = Gr_GreatBritain,
322 Gr_Yugoslavia // 1919
323 };
324
325 // the country parameter is used so far for calculating the start and
326 // the end of DST period and for deciding whether the date is a work
327 // day or not
328 //
329 // TODO move this to intl.h
330
331 // Required for WinCE
332 #ifdef USA
333 #undef USA
334 #endif
335
336 enum Country
337 {
338 Country_Unknown, // no special information for this country
339 Country_Default, // set the default country with SetCountry() method
340 // or use the default country with any other
341
342 // TODO add more countries (for this we must know about DST and/or
343 // holidays for this country)
344
345 // Western European countries: we assume that they all follow the same
346 // DST rules (true or false?)
347 Country_WesternEurope_Start,
348 Country_EEC = Country_WesternEurope_Start,
349 France,
350 Germany,
351 UK,
352 Country_WesternEurope_End = UK,
353
354 Russia,
355 USA
356 };
357 // symbolic names for the months
358 enum Month
359 {
360 Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec, Inv_Month
361 };
362
363 // symbolic names for the weekdays
364 enum WeekDay
365 {
366 Sun, Mon, Tue, Wed, Thu, Fri, Sat, Inv_WeekDay
367 };
368
369 // invalid value for the year
370 enum Year
371 {
372 Inv_Year = SHRT_MIN // should hold in wxDateTime_t
373 };
374
375 // flags for GetWeekDayName and GetMonthName
376 enum NameFlags
377 {
378 Name_Full = 0x01, // return full name
379 Name_Abbr = 0x02 // return abbreviated name
380 };
381
382 // flags for GetWeekOfYear and GetWeekOfMonth
383 enum WeekFlags
384 {
385 Default_First, // Sunday_First for US, Monday_First for the rest
386 Monday_First, // week starts with a Monday
387 Sunday_First // week starts with a Sunday
388 };
389
390 // helper classes
391 // ------------------------------------------------------------------------
392
393 // a class representing a time zone: basicly, this is just an offset
394 // (in seconds) from GMT
395 class WXDLLIMPEXP_BASE TimeZone
396 {
397 public:
398 TimeZone(TZ tz);
399 TimeZone(wxDateTime_t offset = 0) { m_offset = offset; }
400
401 long GetOffset() const { return m_offset; }
402
403 private:
404 // offset for this timezone from GMT in seconds
405 long m_offset;
406 };
407
408 // standard struct tm is limited to the years from 1900 (because
409 // tm_year field is the offset from 1900), so we use our own struct
410 // instead to represent broken down time
411 //
412 // NB: this struct should always be kept normalized (i.e. mon should
413 // be < 12, 1 <= day <= 31 &c), so use AddMonths(), AddDays()
414 // instead of modifying the member fields directly!
415 struct WXDLLIMPEXP_BASE Tm
416 {
417 wxDateTime_t msec, sec, min, hour, mday;
418 Month mon;
419 int year;
420
421 // default ctor inits the object to an invalid value
422 Tm();
423
424 // ctor from struct tm and the timezone
425 Tm(const struct tm& tm, const TimeZone& tz);
426
427 // check that the given date/time is valid (in Gregorian calendar)
428 bool IsValid() const;
429
430 // get the week day
431 WeekDay GetWeekDay() // not const because wday may be changed
432 {
433 if ( wday == Inv_WeekDay )
434 ComputeWeekDay();
435
436 return (WeekDay)wday;
437 }
438
439 // add the given number of months to the date keeping it normalized
440 void AddMonths(int monDiff);
441
442 // add the given number of months to the date keeping it normalized
443 void AddDays(int dayDiff);
444
445 private:
446 // compute the weekday from other fields
447 void ComputeWeekDay();
448
449 // the timezone we correspond to
450 TimeZone m_tz;
451
452 // these values can't be accessed directly because they're not always
453 // computed and we calculate them on demand
454 wxDateTime_t wday, yday;
455 };
456
457 // static methods
458 // ------------------------------------------------------------------------
459
460 // set the current country
461 static void SetCountry(Country country);
462 // get the current country
463 static Country GetCountry();
464
465 // return true if the country is a West European one (in practice,
466 // this means that the same DST rules as for EEC apply)
467 static bool IsWestEuropeanCountry(Country country = Country_Default);
468
469 // return the current year
470 static int GetCurrentYear(Calendar cal = Gregorian);
471
472 // convert the year as returned by wxDateTime::GetYear() to a year
473 // suitable for BC/AD notation. The difference is that BC year 1
474 // corresponds to the year 0 (while BC year 0 didn't exist) and AD
475 // year N is just year N.
476 static int ConvertYearToBC(int year);
477
478 // return the current month
479 static Month GetCurrentMonth(Calendar cal = Gregorian);
480
481 // returns true if the given year is a leap year in the given calendar
482 static bool IsLeapYear(int year = Inv_Year, Calendar cal = Gregorian);
483
484 // get the century (19 for 1999, 20 for 2000 and -5 for 492 BC)
485 static int GetCentury(int year = Inv_Year);
486
487 // returns the number of days in this year (356 or 355 for Gregorian
488 // calendar usually :-)
489 static wxDateTime_t GetNumberOfDays(int year, Calendar cal = Gregorian);
490
491 // get the number of the days in the given month (default value for
492 // the year means the current one)
493 static wxDateTime_t GetNumberOfDays(Month month,
494 int year = Inv_Year,
495 Calendar cal = Gregorian);
496
497 // get the full (default) or abbreviated month name in the current
498 // locale, returns empty string on error
499 static wxString GetMonthName(Month month,
500 NameFlags flags = Name_Full);
501
502 // get the full (default) or abbreviated weekday name in the current
503 // locale, returns empty string on error
504 static wxString GetWeekDayName(WeekDay weekday,
505 NameFlags flags = Name_Full);
506
507 // get the AM and PM strings in the current locale (may be empty)
508 static void GetAmPmStrings(wxString *am, wxString *pm);
509
510 // return true if the given country uses DST for this year
511 static bool IsDSTApplicable(int year = Inv_Year,
512 Country country = Country_Default);
513
514 // get the beginning of DST for this year, will return invalid object
515 // if no DST applicable in this year. The default value of the
516 // parameter means to take the current year.
517 static wxDateTime GetBeginDST(int year = Inv_Year,
518 Country country = Country_Default);
519 // get the end of DST for this year, will return invalid object
520 // if no DST applicable in this year. The default value of the
521 // parameter means to take the current year.
522 static wxDateTime GetEndDST(int year = Inv_Year,
523 Country country = Country_Default);
524
525 // return the wxDateTime object for the current time
526 static inline wxDateTime Now();
527
528 // return the wxDateTime object for the current time with millisecond
529 // precision (if available on this platform)
530 static wxDateTime UNow();
531
532 // return the wxDateTime object for today midnight: i.e. as Now() but
533 // with time set to 0
534 static inline wxDateTime Today();
535
536 // constructors: you should test whether the constructor succeeded with
537 // IsValid() function. The values Inv_Month and Inv_Year for the
538 // parameters mean take current month and/or year values.
539 // ------------------------------------------------------------------------
540
541 // default ctor does not initialize the object, use Set()!
542 wxDateTime() { m_time = wxLongLong((long)ULONG_MAX, ULONG_MAX); }
543
544 // from time_t: seconds since the Epoch 00:00:00 UTC, Jan 1, 1970)
545 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
546 // VA C++ confuses this with wxDateTime(double jdn) thinking it is a duplicate declaration
547 inline wxDateTime(time_t timet);
548 #endif
549 // from broken down time/date (only for standard Unix range)
550 inline wxDateTime(const struct tm& tm);
551 // from broken down time/date (any range)
552 inline wxDateTime(const Tm& tm);
553
554 // from JDN (beware of rounding errors)
555 inline wxDateTime(double jdn);
556
557 // from separate values for each component, date set to today
558 inline wxDateTime(wxDateTime_t hour,
559 wxDateTime_t minute = 0,
560 wxDateTime_t second = 0,
561 wxDateTime_t millisec = 0);
562 // from separate values for each component with explicit date
563 inline wxDateTime(wxDateTime_t day, // day of the month
564 Month month,
565 int year = Inv_Year, // 1999, not 99 please!
566 wxDateTime_t hour = 0,
567 wxDateTime_t minute = 0,
568 wxDateTime_t second = 0,
569 wxDateTime_t millisec = 0);
570
571 // default copy ctor ok
572
573 // no dtor
574
575 // assignment operators and Set() functions: all non const methods return
576 // the reference to this object. IsValid() should be used to test whether
577 // the function succeeded.
578 // ------------------------------------------------------------------------
579
580 // set to the current time
581 inline wxDateTime& SetToCurrent();
582
583 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
584 // VA C++ confuses this with wxDateTime(double jdn) thinking it is a duplicate declaration
585 // set to given time_t value
586 inline wxDateTime& Set(time_t timet);
587 #endif
588
589 // set to given broken down time/date
590 wxDateTime& Set(const struct tm& tm);
591
592 // set to given broken down time/date
593 inline wxDateTime& Set(const Tm& tm);
594
595 // set to given JDN (beware of rounding errors)
596 wxDateTime& Set(double jdn);
597
598 // set to given time, date = today
599 wxDateTime& Set(wxDateTime_t hour,
600 wxDateTime_t minute = 0,
601 wxDateTime_t second = 0,
602 wxDateTime_t millisec = 0);
603
604 // from separate values for each component with explicit date
605 // (defaults for month and year are the current values)
606 wxDateTime& Set(wxDateTime_t day,
607 Month month,
608 int year = Inv_Year, // 1999, not 99 please!
609 wxDateTime_t hour = 0,
610 wxDateTime_t minute = 0,
611 wxDateTime_t second = 0,
612 wxDateTime_t millisec = 0);
613
614 // resets time to 00:00:00, doesn't change the date
615 wxDateTime& ResetTime();
616
617 // the following functions don't change the values of the other
618 // fields, i.e. SetMinute() won't change either hour or seconds value
619
620 // set the year
621 wxDateTime& SetYear(int year);
622 // set the month
623 wxDateTime& SetMonth(Month month);
624 // set the day of the month
625 wxDateTime& SetDay(wxDateTime_t day);
626 // set hour
627 wxDateTime& SetHour(wxDateTime_t hour);
628 // set minute
629 wxDateTime& SetMinute(wxDateTime_t minute);
630 // set second
631 wxDateTime& SetSecond(wxDateTime_t second);
632 // set millisecond
633 wxDateTime& SetMillisecond(wxDateTime_t millisecond);
634
635 // assignment operator from time_t
636 wxDateTime& operator=(time_t timet) { return Set(timet); }
637
638 // assignment operator from broken down time/date
639 wxDateTime& operator=(const struct tm& tm) { return Set(tm); }
640
641 // assignment operator from broken down time/date
642 wxDateTime& operator=(const Tm& tm) { return Set(tm); }
643
644 // default assignment operator is ok
645
646 // calendar calculations (functions which set the date only leave the time
647 // unchanged, e.g. don't explictly zero it): SetXXX() functions modify the
648 // object itself, GetXXX() ones return a new object.
649 // ------------------------------------------------------------------------
650
651 // set to the given week day in the same week as this one
652 wxDateTime& SetToWeekDayInSameWeek(WeekDay weekday,
653 WeekFlags flags = Monday_First);
654 inline wxDateTime GetWeekDayInSameWeek(WeekDay weekday,
655 WeekFlags flags = Monday_First) const;
656
657 // set to the next week day following this one
658 wxDateTime& SetToNextWeekDay(WeekDay weekday);
659 inline wxDateTime GetNextWeekDay(WeekDay weekday) const;
660
661 // set to the previous week day before this one
662 wxDateTime& SetToPrevWeekDay(WeekDay weekday);
663 inline wxDateTime GetPrevWeekDay(WeekDay weekday) const;
664
665 // set to Nth occurence of given weekday in the given month of the
666 // given year (time is set to 0), return true on success and false on
667 // failure. n may be positive (1..5) or negative to count from the end
668 // of the month (see helper function SetToLastWeekDay())
669 bool SetToWeekDay(WeekDay weekday,
670 int n = 1,
671 Month month = Inv_Month,
672 int year = Inv_Year);
673 inline wxDateTime GetWeekDay(WeekDay weekday,
674 int n = 1,
675 Month month = Inv_Month,
676 int year = Inv_Year) const;
677
678 // sets to the last weekday in the given month, year
679 inline bool SetToLastWeekDay(WeekDay weekday,
680 Month month = Inv_Month,
681 int year = Inv_Year);
682 inline wxDateTime GetLastWeekDay(WeekDay weekday,
683 Month month = Inv_Month,
684 int year = Inv_Year);
685
686 // sets the date to the given day of the given week in the year,
687 // returns true on success and false if given date doesn't exist (e.g.
688 // numWeek is > 53)
689 //
690 // these functions are badly defined as they're not the reverse of
691 // GetWeekOfYear(), use SetToTheWeekOfYear() instead
692 wxDEPRECATED( bool SetToTheWeek(wxDateTime_t numWeek,
693 WeekDay weekday = Mon,
694 WeekFlags flags = Monday_First) );
695 wxDEPRECATED( wxDateTime GetWeek(wxDateTime_t numWeek,
696 WeekDay weekday = Mon,
697 WeekFlags flags = Monday_First) const );
698
699 // returns the date corresponding to the given week day of the given
700 // week (in ISO notation) of the specified year
701 static wxDateTime SetToWeekOfYear(int year,
702 wxDateTime_t numWeek,
703 WeekDay weekday = Mon);
704
705 // sets the date to the last day of the given (or current) month or the
706 // given (or current) year
707 wxDateTime& SetToLastMonthDay(Month month = Inv_Month,
708 int year = Inv_Year);
709 inline wxDateTime GetLastMonthDay(Month month = Inv_Month,
710 int year = Inv_Year) const;
711
712 // sets to the given year day (1..365 or 366)
713 wxDateTime& SetToYearDay(wxDateTime_t yday);
714 inline wxDateTime GetYearDay(wxDateTime_t yday) const;
715
716 // The definitions below were taken verbatim from
717 //
718 // http://www.capecod.net/~pbaum/date/date0.htm
719 //
720 // (Peter Baum's home page)
721 //
722 // definition: The Julian Day Number, Julian Day, or JD of a
723 // particular instant of time is the number of days and fractions of a
724 // day since 12 hours Universal Time (Greenwich mean noon) on January
725 // 1 of the year -4712, where the year is given in the Julian
726 // proleptic calendar. The idea of using this reference date was
727 // originally proposed by Joseph Scalizer in 1582 to count years but
728 // it was modified by 19th century astronomers to count days. One
729 // could have equivalently defined the reference time to be noon of
730 // November 24, -4713 if were understood that Gregorian calendar rules
731 // were applied. Julian days are Julian Day Numbers and are not to be
732 // confused with Julian dates.
733 //
734 // definition: The Rata Die number is a date specified as the number
735 // of days relative to a base date of December 31 of the year 0. Thus
736 // January 1 of the year 1 is Rata Die day 1.
737
738 // get the Julian Day number (the fractional part specifies the time of
739 // the day, related to noon - beware of rounding errors!)
740 double GetJulianDayNumber() const;
741 double GetJDN() const { return GetJulianDayNumber(); }
742
743 // get the Modified Julian Day number: it is equal to JDN - 2400000.5
744 // and so integral MJDs correspond to the midnights (and not noons).
745 // MJD 0 is Nov 17, 1858
746 double GetModifiedJulianDayNumber() const { return GetJDN() - 2400000.5; }
747 double GetMJD() const { return GetModifiedJulianDayNumber(); }
748
749 // get the Rata Die number
750 double GetRataDie() const;
751
752 // TODO algorithms for calculating some important dates, such as
753 // religious holidays (Easter...) or moon/solar eclipses? Some
754 // algorithms can be found in the calendar FAQ
755
756
757 // Timezone stuff: a wxDateTime object constructed using given
758 // day/month/year/hour/min/sec values is interpreted as this moment in
759 // local time. Using the functions below, it may be converted to another
760 // time zone (e.g., the Unix epoch is wxDateTime(1, Jan, 1970).ToGMT()).
761 //
762 // These functions try to handle DST internally, but there is no magical
763 // way to know all rules for it in all countries in the world, so if the
764 // program can handle it itself (or doesn't want to handle it at all for
765 // whatever reason), the DST handling can be disabled with noDST.
766 // ------------------------------------------------------------------------
767
768 // transform to any given timezone
769 inline wxDateTime ToTimezone(const TimeZone& tz, bool noDST = false) const;
770 wxDateTime& MakeTimezone(const TimeZone& tz, bool noDST = false);
771
772 #if wxABI_VERSION >= 20602
773 // interpret current value as being in another timezone and transform
774 // it to local one
775 inline wxDateTime FromTimezone(const TimeZone& tz, bool noDST = false) const;
776 wxDateTime& MakeFromTimezone(const TimeZone& tz, bool noDST = false);
777 #endif // ABI >= 2.6.2
778
779 // transform to/from GMT/UTC
780 wxDateTime ToUTC(bool noDST = false) const { return ToTimezone(UTC, noDST); }
781 wxDateTime& MakeUTC(bool noDST = false) { return MakeTimezone(UTC, noDST); }
782
783 wxDateTime ToGMT(bool noDST = false) const { return ToUTC(noDST); }
784 wxDateTime& MakeGMT(bool noDST = false) { return MakeUTC(noDST); }
785
786 #if wxABI_VERSION >= 20602
787 wxDateTime FromUTC(bool noDST = false) const
788 { return FromTimezone(UTC, noDST); }
789 wxDateTime& MakeFromUTC(bool noDST = false)
790 { return MakeFromTimezone(UTC, noDST); }
791 #endif // ABI >= 2.6.2
792
793 // is daylight savings time in effect at this moment according to the
794 // rules of the specified country?
795 //
796 // Return value is > 0 if DST is in effect, 0 if it is not and -1 if
797 // the information is not available (this is compatible with ANSI C)
798 int IsDST(Country country = Country_Default) const;
799
800
801 // accessors: many of them take the timezone parameter which indicates the
802 // timezone for which to make the calculations and the default value means
803 // to do it for the current timezone of this machine (even if the function
804 // only operates with the date it's necessary because a date may wrap as
805 // result of timezone shift)
806 // ------------------------------------------------------------------------
807
808 // is the date valid?
809 inline bool IsValid() const { return m_time != wxInvalidDateTime.m_time; }
810
811 // get the broken down date/time representation in the given timezone
812 //
813 // If you wish to get several time components (day, month and year),
814 // consider getting the whole Tm strcuture first and retrieving the
815 // value from it - this is much more efficient
816 Tm GetTm(const TimeZone& tz = Local) const;
817
818 // get the number of seconds since the Unix epoch - returns (time_t)-1
819 // if the value is out of range
820 inline time_t GetTicks() const;
821
822 // get the year (returns Inv_Year if date is invalid)
823 int GetYear(const TimeZone& tz = Local) const
824 { return GetTm(tz).year; }
825 // get the month (Inv_Month if date is invalid)
826 Month GetMonth(const TimeZone& tz = Local) const
827 { return (Month)GetTm(tz).mon; }
828 // get the month day (in 1..31 range, 0 if date is invalid)
829 wxDateTime_t GetDay(const TimeZone& tz = Local) const
830 { return GetTm(tz).mday; }
831 // get the day of the week (Inv_WeekDay if date is invalid)
832 WeekDay GetWeekDay(const TimeZone& tz = Local) const
833 { return GetTm(tz).GetWeekDay(); }
834 // get the hour of the day
835 wxDateTime_t GetHour(const TimeZone& tz = Local) const
836 { return GetTm(tz).hour; }
837 // get the minute
838 wxDateTime_t GetMinute(const TimeZone& tz = Local) const
839 { return GetTm(tz).min; }
840 // get the second
841 wxDateTime_t GetSecond(const TimeZone& tz = Local) const
842 { return GetTm(tz).sec; }
843 // get milliseconds
844 wxDateTime_t GetMillisecond(const TimeZone& tz = Local) const
845 { return GetTm(tz).msec; }
846
847 // get the day since the year start (1..366, 0 if date is invalid)
848 wxDateTime_t GetDayOfYear(const TimeZone& tz = Local) const;
849 // get the week number since the year start (1..52 or 53, 0 if date is
850 // invalid)
851 wxDateTime_t GetWeekOfYear(WeekFlags flags = Monday_First,
852 const TimeZone& tz = Local) const;
853 // get the week number since the month start (1..5, 0 if date is
854 // invalid)
855 wxDateTime_t GetWeekOfMonth(WeekFlags flags = Monday_First,
856 const TimeZone& tz = Local) const;
857
858 // is this date a work day? This depends on a country, of course,
859 // because the holidays are different in different countries
860 bool IsWorkDay(Country country = Country_Default) const;
861
862 // is this date later than Gregorian calendar introduction for the
863 // given country (see enum GregorianAdoption)?
864 //
865 // NB: this function shouldn't be considered as absolute authority in
866 // the matter. Besides, for some countries the exact date of
867 // adoption of the Gregorian calendar is simply unknown.
868 bool IsGregorianDate(GregorianAdoption country = Gr_Standard) const;
869
870 // dos date and time format
871 // ------------------------------------------------------------------------
872
873 // set from the DOS packed format
874 wxDateTime& SetFromDOS(unsigned long ddt);
875
876 // pack the date in DOS format
877 unsigned long GetAsDOS() const;
878
879 // comparison (see also functions below for operator versions)
880 // ------------------------------------------------------------------------
881
882 // returns true if the two moments are strictly identical
883 inline bool IsEqualTo(const wxDateTime& datetime) const;
884
885 // returns true if the date is strictly earlier than the given one
886 inline bool IsEarlierThan(const wxDateTime& datetime) const;
887
888 // returns true if the date is strictly later than the given one
889 inline bool IsLaterThan(const wxDateTime& datetime) const;
890
891 // returns true if the date is strictly in the given range
892 inline bool IsStrictlyBetween(const wxDateTime& t1,
893 const wxDateTime& t2) const;
894
895 // returns true if the date is in the given range
896 inline bool IsBetween(const wxDateTime& t1, const wxDateTime& t2) const;
897
898 // do these two objects refer to the same date?
899 inline bool IsSameDate(const wxDateTime& dt) const;
900
901 // do these two objects have the same time?
902 inline bool IsSameTime(const wxDateTime& dt) const;
903
904 // are these two objects equal up to given timespan?
905 inline bool IsEqualUpTo(const wxDateTime& dt, const wxTimeSpan& ts) const;
906
907 inline bool operator<(const wxDateTime& dt) const
908 {
909 wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") );
910 return GetValue() < dt.GetValue();
911 }
912
913 inline bool operator<=(const wxDateTime& dt) const
914 {
915 wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") );
916 return GetValue() <= dt.GetValue();
917 }
918
919 inline bool operator>(const wxDateTime& dt) const
920 {
921 wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") );
922 return GetValue() > dt.GetValue();
923 }
924
925 inline bool operator>=(const wxDateTime& dt) const
926 {
927 wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") );
928 return GetValue() >= dt.GetValue();
929 }
930
931 inline bool operator==(const wxDateTime& dt) const
932 {
933 wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") );
934 return GetValue() == dt.GetValue();
935 }
936
937 inline bool operator!=(const wxDateTime& dt) const
938 {
939 wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") );
940 return GetValue() != dt.GetValue();
941 }
942
943 // arithmetics with dates (see also below for more operators)
944 // ------------------------------------------------------------------------
945
946 // return the sum of the date with a time span (positive or negative)
947 inline wxDateTime Add(const wxTimeSpan& diff) const;
948 // add a time span (positive or negative)
949 inline wxDateTime& Add(const wxTimeSpan& diff);
950 // add a time span (positive or negative)
951 inline wxDateTime& operator+=(const wxTimeSpan& diff);
952 inline wxDateTime operator+(const wxTimeSpan& ts) const
953 {
954 wxDateTime dt(*this);
955 dt.Add(ts);
956 return dt;
957 }
958
959 // return the difference of the date with a time span
960 inline wxDateTime Subtract(const wxTimeSpan& diff) const;
961 // subtract a time span (positive or negative)
962 inline wxDateTime& Subtract(const wxTimeSpan& diff);
963 // subtract a time span (positive or negative)
964 inline wxDateTime& operator-=(const wxTimeSpan& diff);
965 inline wxDateTime operator-(const wxTimeSpan& ts) const
966 {
967 wxDateTime dt(*this);
968 dt.Subtract(ts);
969 return dt;
970 }
971
972 // return the sum of the date with a date span
973 inline wxDateTime Add(const wxDateSpan& diff) const;
974 // add a date span (positive or negative)
975 wxDateTime& Add(const wxDateSpan& diff);
976 // add a date span (positive or negative)
977 inline wxDateTime& operator+=(const wxDateSpan& diff);
978 inline wxDateTime operator+(const wxDateSpan& ds) const
979 {
980 wxDateTime dt(*this);
981 dt.Add(ds);
982 return dt;
983 }
984
985 // return the difference of the date with a date span
986 inline wxDateTime Subtract(const wxDateSpan& diff) const;
987 // subtract a date span (positive or negative)
988 inline wxDateTime& Subtract(const wxDateSpan& diff);
989 // subtract a date span (positive or negative)
990 inline wxDateTime& operator-=(const wxDateSpan& diff);
991 inline wxDateTime operator-(const wxDateSpan& ds) const
992 {
993 wxDateTime dt(*this);
994 dt.Subtract(ds);
995 return dt;
996 }
997
998 // return the difference between two dates
999 inline wxTimeSpan Subtract(const wxDateTime& dt) const;
1000 inline wxTimeSpan operator-(const wxDateTime& dt2) const;
1001
1002 // conversion to/from text: all conversions from text return the pointer to
1003 // the next character following the date specification (i.e. the one where
1004 // the scan had to stop) or NULL on failure.
1005 // ------------------------------------------------------------------------
1006
1007 // parse a string in RFC 822 format (found e.g. in mail headers and
1008 // having the form "Wed, 10 Feb 1999 19:07:07 +0100")
1009 const wxChar *ParseRfc822Date(const wxChar* date);
1010 // parse a date/time in the given format (see strptime(3)), fill in
1011 // the missing (in the string) fields with the values of dateDef (by
1012 // default, they will not change if they had valid values or will
1013 // default to Today() otherwise)
1014 const wxChar *ParseFormat(const wxChar *date,
1015 const wxChar *format = wxDefaultDateTimeFormat,
1016 const wxDateTime& dateDef = wxDefaultDateTime);
1017 // parse a string containing the date/time in "free" format, this
1018 // function will try to make an educated guess at the string contents
1019 const wxChar *ParseDateTime(const wxChar *datetime);
1020 // parse a string containing the date only in "free" format (less
1021 // flexible than ParseDateTime)
1022 const wxChar *ParseDate(const wxChar *date);
1023 // parse a string containing the time only in "free" format
1024 const wxChar *ParseTime(const wxChar *time);
1025
1026 // this function accepts strftime()-like format string (default
1027 // argument corresponds to the preferred date and time representation
1028 // for the current locale) and returns the string containing the
1029 // resulting text representation
1030 wxString Format(const wxChar *format = wxDefaultDateTimeFormat,
1031 const TimeZone& tz = Local) const;
1032 // preferred date representation for the current locale
1033 wxString FormatDate() const { return Format(_T("%x")); }
1034 // preferred time representation for the current locale
1035 wxString FormatTime() const { return Format(_T("%X")); }
1036 // returns the string representing the date in ISO 8601 format
1037 // (YYYY-MM-DD)
1038 wxString FormatISODate() const { return Format(_T("%Y-%m-%d")); }
1039 // returns the string representing the time in ISO 8601 format
1040 // (HH:MM:SS)
1041 wxString FormatISOTime() const { return Format(_T("%H:%M:%S")); }
1042
1043 // implementation
1044 // ------------------------------------------------------------------------
1045
1046 // construct from internal representation
1047 wxDateTime(const wxLongLong& time) { m_time = time; }
1048
1049 // get the internal representation
1050 inline wxLongLong GetValue() const;
1051
1052 // a helper function to get the current time_t
1053 static time_t GetTimeNow() { return time((time_t *)NULL); }
1054
1055 // another one to get the current time broken down
1056 static struct tm *GetTmNow()
1057 {
1058 time_t t = GetTimeNow();
1059 return localtime(&t);
1060 }
1061
1062 private:
1063 // the current country - as it's the same for all program objects (unless
1064 // it runs on a _really_ big cluster system :-), this is a static member:
1065 // see SetCountry() and GetCountry()
1066 static Country ms_country;
1067
1068 // this constant is used to transform a time_t value to the internal
1069 // representation, as time_t is in seconds and we use milliseconds it's
1070 // fixed to 1000
1071 static const long TIME_T_FACTOR;
1072
1073 // returns true if we fall in range in which we can use standard ANSI C
1074 // functions
1075 inline bool IsInStdRange() const;
1076
1077 // the internal representation of the time is the amount of milliseconds
1078 // elapsed since the origin which is set by convention to the UNIX/C epoch
1079 // value: the midnight of January 1, 1970 (UTC)
1080 wxLongLong m_time;
1081 };
1082
1083 // ----------------------------------------------------------------------------
1084 // This class contains a difference between 2 wxDateTime values, so it makes
1085 // sense to add it to wxDateTime and it is the result of subtraction of 2
1086 // objects of that class. See also wxDateSpan.
1087 // ----------------------------------------------------------------------------
1088
1089 class WXDLLIMPEXP_BASE wxTimeSpan
1090 {
1091 public:
1092 // constructors
1093 // ------------------------------------------------------------------------
1094
1095 // return the timespan for the given number of seconds
1096 static wxTimeSpan Seconds(long sec) { return wxTimeSpan(0, 0, sec); }
1097 static wxTimeSpan Second() { return Seconds(1); }
1098
1099 // return the timespan for the given number of minutes
1100 static wxTimeSpan Minutes(long min) { return wxTimeSpan(0, min, 0 ); }
1101 static wxTimeSpan Minute() { return Minutes(1); }
1102
1103 // return the timespan for the given number of hours
1104 static wxTimeSpan Hours(long hours) { return wxTimeSpan(hours, 0, 0); }
1105 static wxTimeSpan Hour() { return Hours(1); }
1106
1107 // return the timespan for the given number of days
1108 static wxTimeSpan Days(long days) { return Hours(24 * days); }
1109 static wxTimeSpan Day() { return Days(1); }
1110
1111 // return the timespan for the given number of weeks
1112 static wxTimeSpan Weeks(long days) { return Days(7 * days); }
1113 static wxTimeSpan Week() { return Weeks(1); }
1114
1115 // default ctor constructs the 0 time span
1116 wxTimeSpan() { }
1117
1118 // from separate values for each component, date set to 0 (hours are
1119 // not restricted to 0..24 range, neither are minutes, seconds or
1120 // milliseconds)
1121 inline wxTimeSpan(long hours,
1122 long minutes = 0,
1123 long seconds = 0,
1124 long milliseconds = 0);
1125
1126 // default copy ctor is ok
1127
1128 // no dtor
1129
1130 // arithmetics with time spans (see also below for more operators)
1131 // ------------------------------------------------------------------------
1132
1133 // return the sum of two timespans
1134 inline wxTimeSpan Add(const wxTimeSpan& diff) const;
1135 // add two timespans together
1136 inline wxTimeSpan& Add(const wxTimeSpan& diff);
1137 // add two timespans together
1138 wxTimeSpan& operator+=(const wxTimeSpan& diff) { return Add(diff); }
1139 inline wxTimeSpan operator+(const wxTimeSpan& ts) const
1140 {
1141 return wxTimeSpan(GetValue() + ts.GetValue());
1142 }
1143
1144 // return the difference of two timespans
1145 inline wxTimeSpan Subtract(const wxTimeSpan& diff) const;
1146 // subtract another timespan
1147 inline wxTimeSpan& Subtract(const wxTimeSpan& diff);
1148 // subtract another timespan
1149 wxTimeSpan& operator-=(const wxTimeSpan& diff) { return Subtract(diff); }
1150 inline wxTimeSpan operator-(const wxTimeSpan& ts)
1151 {
1152 return wxTimeSpan(GetValue() - ts.GetValue());
1153 }
1154
1155 // multiply timespan by a scalar
1156 inline wxTimeSpan Multiply(int n) const;
1157 // multiply timespan by a scalar
1158 inline wxTimeSpan& Multiply(int n);
1159 // multiply timespan by a scalar
1160 wxTimeSpan& operator*=(int n) { return Multiply(n); }
1161 inline wxTimeSpan operator*(int n) const
1162 {
1163 return wxTimeSpan(*this).Multiply(n);
1164 }
1165
1166 // return this timespan with opposite sign
1167 wxTimeSpan Negate() const { return wxTimeSpan(-GetValue()); }
1168 // negate the value of the timespan
1169 wxTimeSpan& Neg() { m_diff = -GetValue(); return *this; }
1170 // negate the value of the timespan
1171 wxTimeSpan& operator-() { return Neg(); }
1172
1173 // return the absolute value of the timespan: does _not_ modify the
1174 // object
1175 inline wxTimeSpan Abs() const;
1176
1177 // there is intentionally no division because we don't want to
1178 // introduce rounding errors in time calculations
1179
1180 // comparaison (see also operator versions below)
1181 // ------------------------------------------------------------------------
1182
1183 // is the timespan null?
1184 bool IsNull() const { return m_diff == 0l; }
1185 // returns true if the timespan is null
1186 bool operator!() const { return !IsNull(); }
1187
1188 // is the timespan positive?
1189 bool IsPositive() const { return m_diff > 0l; }
1190
1191 // is the timespan negative?
1192 bool IsNegative() const { return m_diff < 0l; }
1193
1194 // are two timespans equal?
1195 inline bool IsEqualTo(const wxTimeSpan& ts) const;
1196 // compare two timestamps: works with the absolute values, i.e. -2
1197 // hours is longer than 1 hour. Also, it will return false if the
1198 // timespans are equal in absolute value.
1199 inline bool IsLongerThan(const wxTimeSpan& ts) const;
1200 // compare two timestamps: works with the absolute values, i.e. 1
1201 // hour is shorter than -2 hours. Also, it will return false if the
1202 // timespans are equal in absolute value.
1203 bool IsShorterThan(const wxTimeSpan& t) const { return !IsLongerThan(t); }
1204
1205 inline bool operator<(const wxTimeSpan &ts) const
1206 {
1207 return GetValue() < ts.GetValue();
1208 }
1209
1210 inline bool operator<=(const wxTimeSpan &ts) const
1211 {
1212 return GetValue() <= ts.GetValue();
1213 }
1214
1215 inline bool operator>(const wxTimeSpan &ts) const
1216 {
1217 return GetValue() > ts.GetValue();
1218 }
1219
1220 inline bool operator>=(const wxTimeSpan &ts) const
1221 {
1222 return GetValue() >= ts.GetValue();
1223 }
1224
1225 inline bool operator==(const wxTimeSpan &ts) const
1226 {
1227 return GetValue() == ts.GetValue();
1228 }
1229
1230 inline bool operator!=(const wxTimeSpan &ts) const
1231 {
1232 return GetValue() != ts.GetValue();
1233 }
1234
1235 // breaking into days, hours, minutes and seconds
1236 // ------------------------------------------------------------------------
1237
1238 // get the max number of weeks in this timespan
1239 inline int GetWeeks() const;
1240 // get the max number of days in this timespan
1241 inline int GetDays() const;
1242 // get the max number of hours in this timespan
1243 inline int GetHours() const;
1244 // get the max number of minutes in this timespan
1245 inline int GetMinutes() const;
1246 // get the max number of seconds in this timespan
1247 inline wxLongLong GetSeconds() const;
1248 // get the number of milliseconds in this timespan
1249 wxLongLong GetMilliseconds() const { return m_diff; }
1250
1251 // conversion to text
1252 // ------------------------------------------------------------------------
1253
1254 // this function accepts strftime()-like format string (default
1255 // argument corresponds to the preferred date and time representation
1256 // for the current locale) and returns the string containing the
1257 // resulting text representation. Notice that only some of format
1258 // specifiers valid for wxDateTime are valid for wxTimeSpan: hours,
1259 // minutes and seconds make sense, but not "PM/AM" string for example.
1260 wxString Format(const wxChar *format = wxDefaultTimeSpanFormat) const;
1261
1262 // implementation
1263 // ------------------------------------------------------------------------
1264
1265 // construct from internal representation
1266 wxTimeSpan(const wxLongLong& diff) { m_diff = diff; }
1267
1268 // get the internal representation
1269 wxLongLong GetValue() const { return m_diff; }
1270
1271 private:
1272 // the (signed) time span in milliseconds
1273 wxLongLong m_diff;
1274 };
1275
1276 // ----------------------------------------------------------------------------
1277 // This class is a "logical time span" and is useful for implementing program
1278 // logic for such things as "add one month to the date" which, in general,
1279 // doesn't mean to add 60*60*24*31 seconds to it, but to take the same date
1280 // the next month (to understand that this is indeed different consider adding
1281 // one month to Feb, 15 - we want to get Mar, 15, of course).
1282 //
1283 // When adding a month to the date, all lesser components (days, hours, ...)
1284 // won't be changed unless the resulting date would be invalid: for example,
1285 // Jan 31 + 1 month will be Feb 28, not (non existing) Feb 31.
1286 //
1287 // Because of this feature, adding and subtracting back again the same
1288 // wxDateSpan will *not*, in general give back the original date: Feb 28 - 1
1289 // month will be Jan 28, not Jan 31!
1290 //
1291 // wxDateSpan can be either positive or negative. They may be
1292 // multiplied by scalars which multiply all deltas by the scalar: i.e. 2*(1
1293 // month and 1 day) is 2 months and 2 days. They can be added together and
1294 // with wxDateTime or wxTimeSpan, but the type of result is different for each
1295 // case.
1296 //
1297 // Beware about weeks: if you specify both weeks and days, the total number of
1298 // days added will be 7*weeks + days! See also GetTotalDays() function.
1299 //
1300 // Equality operators are defined for wxDateSpans. Two datespans are equal if
1301 // they both give the same target date when added to *every* source date.
1302 // Thus wxDateSpan::Months(1) is not equal to wxDateSpan::Days(30), because
1303 // they not give the same date when added to 1 Feb. But wxDateSpan::Days(14) is
1304 // equal to wxDateSpan::Weeks(2)
1305 //
1306 // Finally, notice that for adding hours, minutes &c you don't need this
1307 // class: wxTimeSpan will do the job because there are no subtleties
1308 // associated with those.
1309 // ----------------------------------------------------------------------------
1310
1311 class WXDLLIMPEXP_BASE wxDateSpan
1312 {
1313 public:
1314 // constructors
1315 // ------------------------------------------------------------------------
1316
1317 // this many years/months/weeks/days
1318 wxDateSpan(int years = 0, int months = 0, int weeks = 0, int days = 0)
1319 {
1320 m_years = years;
1321 m_months = months;
1322 m_weeks = weeks;
1323 m_days = days;
1324 }
1325
1326 // get an object for the given number of days
1327 static wxDateSpan Days(int days) { return wxDateSpan(0, 0, 0, days); }
1328 static wxDateSpan Day() { return Days(1); }
1329
1330 // get an object for the given number of weeks
1331 static wxDateSpan Weeks(int weeks) { return wxDateSpan(0, 0, weeks, 0); }
1332 static wxDateSpan Week() { return Weeks(1); }
1333
1334 // get an object for the given number of months
1335 static wxDateSpan Months(int mon) { return wxDateSpan(0, mon, 0, 0); }
1336 static wxDateSpan Month() { return Months(1); }
1337
1338 // get an object for the given number of years
1339 static wxDateSpan Years(int years) { return wxDateSpan(years, 0, 0, 0); }
1340 static wxDateSpan Year() { return Years(1); }
1341
1342 // default copy ctor is ok
1343
1344 // no dtor
1345
1346 // accessors (all SetXXX() return the (modified) wxDateSpan object)
1347 // ------------------------------------------------------------------------
1348
1349 // set number of years
1350 wxDateSpan& SetYears(int n) { m_years = n; return *this; }
1351 // set number of months
1352 wxDateSpan& SetMonths(int n) { m_months = n; return *this; }
1353 // set number of weeks
1354 wxDateSpan& SetWeeks(int n) { m_weeks = n; return *this; }
1355 // set number of days
1356 wxDateSpan& SetDays(int n) { m_days = n; return *this; }
1357
1358 // get number of years
1359 int GetYears() const { return m_years; }
1360 // get number of months
1361 int GetMonths() const { return m_months; }
1362 // get number of weeks
1363 int GetWeeks() const { return m_weeks; }
1364 // get number of days
1365 int GetDays() const { return m_days; }
1366 // returns 7*GetWeeks() + GetDays()
1367 int GetTotalDays() const { return 7*m_weeks + m_days; }
1368
1369 // arithmetics with date spans (see also below for more operators)
1370 // ------------------------------------------------------------------------
1371
1372 // return sum of two date spans
1373 inline wxDateSpan Add(const wxDateSpan& other) const;
1374 // add another wxDateSpan to us
1375 inline wxDateSpan& Add(const wxDateSpan& other);
1376 // add another wxDateSpan to us
1377 inline wxDateSpan& operator+=(const wxDateSpan& other);
1378 inline wxDateSpan operator+(const wxDateSpan& ds) const
1379 {
1380 return wxDateSpan(GetYears() + ds.GetYears(),
1381 GetMonths() + ds.GetMonths(),
1382 GetWeeks() + ds.GetWeeks(),
1383 GetDays() + ds.GetDays());
1384 }
1385
1386 // return difference of two date spans
1387 inline wxDateSpan Subtract(const wxDateSpan& other) const;
1388 // subtract another wxDateSpan from us
1389 inline wxDateSpan& Subtract(const wxDateSpan& other);
1390 // subtract another wxDateSpan from us
1391 inline wxDateSpan& operator-=(const wxDateSpan& other);
1392 inline wxDateSpan operator-(const wxDateSpan& ds) const
1393 {
1394 return wxDateSpan(GetYears() - ds.GetYears(),
1395 GetMonths() - ds.GetMonths(),
1396 GetWeeks() - ds.GetWeeks(),
1397 GetDays() - ds.GetDays());
1398 }
1399
1400 // return a copy of this time span with changed sign
1401 inline wxDateSpan Negate() const;
1402 // inverse the sign of this timespan
1403 inline wxDateSpan& Neg();
1404 // inverse the sign of this timespan
1405 wxDateSpan& operator-() { return Neg(); }
1406
1407 // return the date span proportional to this one with given factor
1408 inline wxDateSpan Multiply(int factor) const;
1409 // multiply all components by a (signed) number
1410 inline wxDateSpan& Multiply(int factor);
1411 // multiply all components by a (signed) number
1412 inline wxDateSpan& operator*=(int factor) { return Multiply(factor); }
1413 inline wxDateSpan operator*(int n) const
1414 {
1415 return wxDateSpan(*this).Multiply(n);
1416 }
1417
1418 // ds1 == d2 if and only if for every wxDateTime t t + ds1 == t + ds2
1419 inline bool operator==(const wxDateSpan& ds) const
1420 {
1421 return GetYears() == ds.GetYears() &&
1422 GetMonths() == ds.GetMonths() &&
1423 GetTotalDays() == ds.GetTotalDays();
1424 }
1425
1426 inline bool operator!=(const wxDateSpan& ds) const
1427 {
1428 return !(*this == ds);
1429 }
1430
1431 private:
1432 int m_years,
1433 m_months,
1434 m_weeks,
1435 m_days;
1436 };
1437
1438 // ----------------------------------------------------------------------------
1439 // wxDateTimeArray: array of dates.
1440 // ----------------------------------------------------------------------------
1441
1442 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxDateTime, wxDateTimeArray, WXDLLIMPEXP_BASE);
1443
1444 // ----------------------------------------------------------------------------
1445 // wxDateTimeHolidayAuthority: an object of this class will decide whether a
1446 // given date is a holiday and is used by all functions working with "work
1447 // days".
1448 //
1449 // NB: the base class is an ABC, derived classes must implement the pure
1450 // virtual methods to work with the holidays they correspond to.
1451 // ----------------------------------------------------------------------------
1452
1453 class WXDLLIMPEXP_BASE wxDateTimeHolidayAuthority;
1454 WX_DEFINE_USER_EXPORTED_ARRAY_PTR(wxDateTimeHolidayAuthority *,
1455 wxHolidayAuthoritiesArray,
1456 class WXDLLIMPEXP_BASE);
1457
1458 class wxDateTimeHolidaysModule;
1459 class WXDLLIMPEXP_BASE wxDateTimeHolidayAuthority
1460 {
1461 friend class wxDateTimeHolidaysModule;
1462 public:
1463 // returns true if the given date is a holiday
1464 static bool IsHoliday(const wxDateTime& dt);
1465
1466 // fills the provided array with all holidays in the given range, returns
1467 // the number of them
1468 static size_t GetHolidaysInRange(const wxDateTime& dtStart,
1469 const wxDateTime& dtEnd,
1470 wxDateTimeArray& holidays);
1471
1472 // clear the list of holiday authorities
1473 static void ClearAllAuthorities();
1474
1475 // add a new holiday authority (the pointer will be deleted by
1476 // wxDateTimeHolidayAuthority)
1477 static void AddAuthority(wxDateTimeHolidayAuthority *auth);
1478
1479 // the base class must have a virtual dtor
1480 virtual ~wxDateTimeHolidayAuthority();
1481
1482 protected:
1483 // this function is called to determine whether a given day is a holiday
1484 virtual bool DoIsHoliday(const wxDateTime& dt) const = 0;
1485
1486 // this function should fill the array with all holidays between the two
1487 // given dates - it is implemented in the base class, but in a very
1488 // inefficient way (it just iterates over all days and uses IsHoliday() for
1489 // each of them), so it must be overridden in the derived class where the
1490 // base class version may be explicitly used if needed
1491 //
1492 // returns the number of holidays in the given range and fills holidays
1493 // array
1494 virtual size_t DoGetHolidaysInRange(const wxDateTime& dtStart,
1495 const wxDateTime& dtEnd,
1496 wxDateTimeArray& holidays) const = 0;
1497
1498 private:
1499 // all holiday authorities
1500 static wxHolidayAuthoritiesArray ms_authorities;
1501 };
1502
1503 // the holidays for this class are all Saturdays and Sundays
1504 class WXDLLIMPEXP_BASE wxDateTimeWorkDays : public wxDateTimeHolidayAuthority
1505 {
1506 protected:
1507 virtual bool DoIsHoliday(const wxDateTime& dt) const;
1508 virtual size_t DoGetHolidaysInRange(const wxDateTime& dtStart,
1509 const wxDateTime& dtEnd,
1510 wxDateTimeArray& holidays) const;
1511 };
1512
1513 // ============================================================================
1514 // inline functions implementation
1515 // ============================================================================
1516
1517 // ----------------------------------------------------------------------------
1518 // private macros
1519 // ----------------------------------------------------------------------------
1520
1521 #define MILLISECONDS_PER_DAY 86400000l
1522
1523 // some broken compilers (HP-UX CC) refuse to compile the "normal" version, but
1524 // using a temp variable always might prevent other compilers from optimising
1525 // it away - hence use of this ugly macro
1526 #ifndef __HPUX__
1527 #define MODIFY_AND_RETURN(op) return wxDateTime(*this).op
1528 #else
1529 #define MODIFY_AND_RETURN(op) wxDateTime dt(*this); dt.op; return dt
1530 #endif
1531
1532 // ----------------------------------------------------------------------------
1533 // wxDateTime construction
1534 // ----------------------------------------------------------------------------
1535
1536 inline bool wxDateTime::IsInStdRange() const
1537 {
1538 return m_time >= 0l && (m_time / TIME_T_FACTOR) < LONG_MAX;
1539 }
1540
1541 /* static */
1542 inline wxDateTime wxDateTime::Now()
1543 {
1544 return wxDateTime(*GetTmNow());
1545 }
1546
1547 /* static */
1548 inline wxDateTime wxDateTime::Today()
1549 {
1550 wxDateTime dt(Now());
1551 dt.ResetTime();
1552
1553 return dt;
1554 }
1555
1556 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
1557 inline wxDateTime& wxDateTime::Set(time_t timet)
1558 {
1559 // assign first to avoid long multiplication overflow!
1560 m_time = timet - WX_TIME_BASE_OFFSET ;
1561 m_time *= TIME_T_FACTOR;
1562
1563 return *this;
1564 }
1565 #endif
1566
1567 inline wxDateTime& wxDateTime::SetToCurrent()
1568 {
1569 *this = Now();
1570 return *this;
1571 }
1572
1573 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
1574 inline wxDateTime::wxDateTime(time_t timet)
1575 {
1576 Set(timet);
1577 }
1578 #endif
1579
1580 inline wxDateTime::wxDateTime(const struct tm& tm)
1581 {
1582 Set(tm);
1583 }
1584
1585 inline wxDateTime::wxDateTime(const Tm& tm)
1586 {
1587 Set(tm);
1588 }
1589
1590 inline wxDateTime::wxDateTime(double jdn)
1591 {
1592 Set(jdn);
1593 }
1594
1595 inline wxDateTime& wxDateTime::Set(const Tm& tm)
1596 {
1597 wxASSERT_MSG( tm.IsValid(), _T("invalid broken down date/time") );
1598
1599 return Set(tm.mday, (Month)tm.mon, tm.year,
1600 tm.hour, tm.min, tm.sec, tm.msec);
1601 }
1602
1603 inline wxDateTime::wxDateTime(wxDateTime_t hour,
1604 wxDateTime_t minute,
1605 wxDateTime_t second,
1606 wxDateTime_t millisec)
1607 {
1608 Set(hour, minute, second, millisec);
1609 }
1610
1611 inline wxDateTime::wxDateTime(wxDateTime_t day,
1612 Month month,
1613 int year,
1614 wxDateTime_t hour,
1615 wxDateTime_t minute,
1616 wxDateTime_t second,
1617 wxDateTime_t millisec)
1618 {
1619 Set(day, month, year, hour, minute, second, millisec);
1620 }
1621
1622 // ----------------------------------------------------------------------------
1623 // wxDateTime accessors
1624 // ----------------------------------------------------------------------------
1625
1626 inline wxLongLong wxDateTime::GetValue() const
1627 {
1628 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1629
1630 return m_time;
1631 }
1632
1633 inline time_t wxDateTime::GetTicks() const
1634 {
1635 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1636 if ( !IsInStdRange() )
1637 {
1638 return (time_t)-1;
1639 }
1640
1641 return (time_t)((m_time / (long)TIME_T_FACTOR).GetLo())+WX_TIME_BASE_OFFSET ;
1642 }
1643
1644 inline bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
1645 Month month,
1646 int year)
1647 {
1648 return SetToWeekDay(weekday, -1, month, year);
1649 }
1650
1651 inline wxDateTime
1652 wxDateTime::GetWeekDayInSameWeek(WeekDay weekday,
1653 WeekFlags WXUNUSED(flags)) const
1654 {
1655 MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday) );
1656 }
1657
1658 inline wxDateTime wxDateTime::GetNextWeekDay(WeekDay weekday) const
1659 {
1660 MODIFY_AND_RETURN( SetToNextWeekDay(weekday) );
1661 }
1662
1663 inline wxDateTime wxDateTime::GetPrevWeekDay(WeekDay weekday) const
1664 {
1665 MODIFY_AND_RETURN( SetToPrevWeekDay(weekday) );
1666 }
1667
1668 inline wxDateTime wxDateTime::GetWeekDay(WeekDay weekday,
1669 int n,
1670 Month month,
1671 int year) const
1672 {
1673 wxDateTime dt(*this);
1674
1675 return dt.SetToWeekDay(weekday, n, month, year) ? dt : wxInvalidDateTime;
1676 }
1677
1678 inline wxDateTime wxDateTime::GetLastWeekDay(WeekDay weekday,
1679 Month month,
1680 int year)
1681 {
1682 wxDateTime dt(*this);
1683
1684 return dt.SetToLastWeekDay(weekday, month, year) ? dt : wxInvalidDateTime;
1685 }
1686
1687 inline wxDateTime wxDateTime::GetLastMonthDay(Month month, int year) const
1688 {
1689 MODIFY_AND_RETURN( SetToLastMonthDay(month, year) );
1690 }
1691
1692 inline wxDateTime wxDateTime::GetYearDay(wxDateTime_t yday) const
1693 {
1694 MODIFY_AND_RETURN( SetToYearDay(yday) );
1695 }
1696
1697 // ----------------------------------------------------------------------------
1698 // wxDateTime comparison
1699 // ----------------------------------------------------------------------------
1700
1701 inline bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
1702 {
1703 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
1704
1705 return m_time == datetime.m_time;
1706 }
1707
1708 inline bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
1709 {
1710 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
1711
1712 return m_time < datetime.m_time;
1713 }
1714
1715 inline bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
1716 {
1717 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
1718
1719 return m_time > datetime.m_time;
1720 }
1721
1722 inline bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1,
1723 const wxDateTime& t2) const
1724 {
1725 // no need for assert, will be checked by the functions we call
1726 return IsLaterThan(t1) && IsEarlierThan(t2);
1727 }
1728
1729 inline bool wxDateTime::IsBetween(const wxDateTime& t1,
1730 const wxDateTime& t2) const
1731 {
1732 // no need for assert, will be checked by the functions we call
1733 return IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2);
1734 }
1735
1736 inline bool wxDateTime::IsSameDate(const wxDateTime& dt) const
1737 {
1738 Tm tm1 = GetTm(),
1739 tm2 = dt.GetTm();
1740
1741 return tm1.year == tm2.year &&
1742 tm1.mon == tm2.mon &&
1743 tm1.mday == tm2.mday;
1744 }
1745
1746 inline bool wxDateTime::IsSameTime(const wxDateTime& dt) const
1747 {
1748 // notice that we can't do something like this:
1749 //
1750 // m_time % MILLISECONDS_PER_DAY == dt.m_time % MILLISECONDS_PER_DAY
1751 //
1752 // because we have also to deal with (possibly) different DST settings!
1753 Tm tm1 = GetTm(),
1754 tm2 = dt.GetTm();
1755
1756 return tm1.hour == tm2.hour &&
1757 tm1.min == tm2.min &&
1758 tm1.sec == tm2.sec &&
1759 tm1.msec == tm2.msec;
1760 }
1761
1762 inline bool wxDateTime::IsEqualUpTo(const wxDateTime& dt,
1763 const wxTimeSpan& ts) const
1764 {
1765 return IsBetween(dt.Subtract(ts), dt.Add(ts));
1766 }
1767
1768 // ----------------------------------------------------------------------------
1769 // wxDateTime arithmetics
1770 // ----------------------------------------------------------------------------
1771
1772 inline wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const
1773 {
1774 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1775
1776 return wxDateTime(m_time + diff.GetValue());
1777 }
1778
1779 inline wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
1780 {
1781 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1782
1783 m_time += diff.GetValue();
1784
1785 return *this;
1786 }
1787
1788 inline wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
1789 {
1790 return Add(diff);
1791 }
1792
1793 inline wxDateTime wxDateTime::Subtract(const wxTimeSpan& diff) const
1794 {
1795 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1796
1797 return wxDateTime(m_time - diff.GetValue());
1798 }
1799
1800 inline wxDateTime& wxDateTime::Subtract(const wxTimeSpan& diff)
1801 {
1802 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1803
1804 m_time -= diff.GetValue();
1805
1806 return *this;
1807 }
1808
1809 inline wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
1810 {
1811 return Subtract(diff);
1812 }
1813
1814 inline wxTimeSpan wxDateTime::Subtract(const wxDateTime& datetime) const
1815 {
1816 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
1817
1818 return wxTimeSpan(GetValue() - datetime.GetValue());
1819 }
1820
1821 inline wxTimeSpan wxDateTime::operator-(const wxDateTime& dt2) const
1822 {
1823 return this->Subtract(dt2);
1824 }
1825
1826 inline wxDateTime wxDateTime::Add(const wxDateSpan& diff) const
1827 {
1828 return wxDateTime(*this).Add(diff);
1829 }
1830
1831 inline wxDateTime& wxDateTime::Subtract(const wxDateSpan& diff)
1832 {
1833 return Add(diff.Negate());
1834 }
1835
1836 inline wxDateTime wxDateTime::Subtract(const wxDateSpan& diff) const
1837 {
1838 return wxDateTime(*this).Subtract(diff);
1839 }
1840
1841 inline wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
1842 {
1843 return Subtract(diff);
1844 }
1845
1846 inline wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
1847 {
1848 return Add(diff);
1849 }
1850
1851 // ----------------------------------------------------------------------------
1852 // wxDateTime and timezones
1853 // ----------------------------------------------------------------------------
1854
1855 inline wxDateTime
1856 wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz, bool noDST) const
1857 {
1858 MODIFY_AND_RETURN( MakeTimezone(tz, noDST) );
1859 }
1860
1861 #if wxABI_VERSION >= 20602
1862
1863 inline wxDateTime
1864 wxDateTime::FromTimezone(const wxDateTime::TimeZone& tz, bool noDST) const
1865 {
1866 MODIFY_AND_RETURN( MakeFromTimezone(tz, noDST) );
1867 }
1868
1869 #endif // ABI >= 2.6.2
1870
1871 // ----------------------------------------------------------------------------
1872 // wxTimeSpan construction
1873 // ----------------------------------------------------------------------------
1874
1875 inline wxTimeSpan::wxTimeSpan(long hours,
1876 long minutes,
1877 long seconds,
1878 long milliseconds)
1879 {
1880 // assign first to avoid precision loss
1881 m_diff = hours;
1882 m_diff *= 60l;
1883 m_diff += minutes;
1884 m_diff *= 60l;
1885 m_diff += seconds;
1886 m_diff *= 1000l;
1887 m_diff += milliseconds;
1888 }
1889
1890 // ----------------------------------------------------------------------------
1891 // wxTimeSpan accessors
1892 // ----------------------------------------------------------------------------
1893
1894 inline wxLongLong wxTimeSpan::GetSeconds() const
1895 {
1896 return m_diff / 1000l;
1897 }
1898
1899 inline int wxTimeSpan::GetMinutes() const
1900 {
1901 return (GetSeconds() / 60l).GetLo();
1902 }
1903
1904 inline int wxTimeSpan::GetHours() const
1905 {
1906 return GetMinutes() / 60;
1907 }
1908
1909 inline int wxTimeSpan::GetDays() const
1910 {
1911 return GetHours() / 24;
1912 }
1913
1914 inline int wxTimeSpan::GetWeeks() const
1915 {
1916 return GetDays() / 7;
1917 }
1918
1919 // ----------------------------------------------------------------------------
1920 // wxTimeSpan arithmetics
1921 // ----------------------------------------------------------------------------
1922
1923 inline wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const
1924 {
1925 return wxTimeSpan(m_diff + diff.GetValue());
1926 }
1927
1928 inline wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
1929 {
1930 m_diff += diff.GetValue();
1931
1932 return *this;
1933 }
1934
1935 inline wxTimeSpan wxTimeSpan::Subtract(const wxTimeSpan& diff) const
1936 {
1937 return wxTimeSpan(m_diff - diff.GetValue());
1938 }
1939
1940 inline wxTimeSpan& wxTimeSpan::Subtract(const wxTimeSpan& diff)
1941 {
1942 m_diff -= diff.GetValue();
1943
1944 return *this;
1945 }
1946
1947 inline wxTimeSpan& wxTimeSpan::Multiply(int n)
1948 {
1949 m_diff *= (long)n;
1950
1951 return *this;
1952 }
1953
1954 inline wxTimeSpan wxTimeSpan::Multiply(int n) const
1955 {
1956 return wxTimeSpan(m_diff * (long)n);
1957 }
1958
1959 inline wxTimeSpan wxTimeSpan::Abs() const
1960 {
1961 return wxTimeSpan(GetValue().Abs());
1962 }
1963
1964 inline bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
1965 {
1966 return GetValue() == ts.GetValue();
1967 }
1968
1969 inline bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
1970 {
1971 return GetValue().Abs() > ts.GetValue().Abs();
1972 }
1973
1974 // ----------------------------------------------------------------------------
1975 // wxDateSpan
1976 // ----------------------------------------------------------------------------
1977
1978 inline wxDateSpan& wxDateSpan::operator+=(const wxDateSpan& other)
1979 {
1980 m_years += other.m_years;
1981 m_months += other.m_months;
1982 m_weeks += other.m_weeks;
1983 m_days += other.m_days;
1984
1985 return *this;
1986 }
1987
1988 inline wxDateSpan& wxDateSpan::Add(const wxDateSpan& other)
1989 {
1990 return *this += other;
1991 }
1992
1993 inline wxDateSpan wxDateSpan::Add(const wxDateSpan& other) const
1994 {
1995 wxDateSpan ds(*this);
1996 ds.Add(other);
1997 return ds;
1998 }
1999
2000 inline wxDateSpan& wxDateSpan::Multiply(int factor)
2001 {
2002 m_years *= factor;
2003 m_months *= factor;
2004 m_weeks *= factor;
2005 m_days *= factor;
2006
2007 return *this;
2008 }
2009
2010 inline wxDateSpan wxDateSpan::Multiply(int factor) const
2011 {
2012 wxDateSpan ds(*this);
2013 ds.Multiply(factor);
2014 return ds;
2015 }
2016
2017 inline wxDateSpan wxDateSpan::Negate() const
2018 {
2019 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
2020 }
2021
2022 inline wxDateSpan& wxDateSpan::Neg()
2023 {
2024 m_years = -m_years;
2025 m_months = -m_months;
2026 m_weeks = -m_weeks;
2027 m_days = -m_days;
2028
2029 return *this;
2030 }
2031
2032 inline wxDateSpan& wxDateSpan::operator-=(const wxDateSpan& other)
2033 {
2034 return *this += other.Negate();
2035 }
2036
2037 inline wxDateSpan& wxDateSpan::Subtract(const wxDateSpan& other)
2038 {
2039 return *this -= other;
2040 }
2041
2042 inline wxDateSpan wxDateSpan::Subtract(const wxDateSpan& other) const
2043 {
2044 wxDateSpan ds(*this);
2045 ds.Subtract(other);
2046 return ds;
2047 }
2048
2049 #undef MILLISECONDS_PER_DAY
2050
2051 #undef MODIFY_AND_RETURN
2052
2053 // ============================================================================
2054 // binary operators
2055 // ============================================================================
2056
2057 // ----------------------------------------------------------------------------
2058 // wxTimeSpan operators
2059 // ----------------------------------------------------------------------------
2060
2061 wxTimeSpan WXDLLIMPEXP_BASE operator*(int n, const wxTimeSpan& ts);
2062
2063 // ----------------------------------------------------------------------------
2064 // wxDateSpan
2065 // ----------------------------------------------------------------------------
2066
2067 wxDateSpan WXDLLIMPEXP_BASE operator*(int n, const wxDateSpan& ds);
2068
2069 // ============================================================================
2070 // other helper functions
2071 // ============================================================================
2072
2073 // ----------------------------------------------------------------------------
2074 // iteration helpers: can be used to write a for loop over enum variable like
2075 // this:
2076 // for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) )
2077 // ----------------------------------------------------------------------------
2078
2079 WXDLLIMPEXP_BASE void wxNextMonth(wxDateTime::Month& m);
2080 WXDLLIMPEXP_BASE void wxPrevMonth(wxDateTime::Month& m);
2081 WXDLLIMPEXP_BASE void wxNextWDay(wxDateTime::WeekDay& wd);
2082 WXDLLIMPEXP_BASE void wxPrevWDay(wxDateTime::WeekDay& wd);
2083
2084 #endif // wxUSE_DATETIME
2085
2086 #endif // _WX_DATETIME_H