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