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