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