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