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