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