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