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