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