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