1. more wxDateTime work
[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 license
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_DATETIME_H
14 #define _WX_DATETIME_H
15
16 #ifdef __GNUG__
17 #pragma interface "datetime.h"
18 #endif
19
20 #include <time.h>
21 #include <limits.h> // for INT_MIN
22
23 #include "wx/longlong.h"
24
25 class WXDLLEXPORT wxDateTime;
26 class WXDLLEXPORT wxTimeSpan;
27 class WXDLLEXPORT wxDateSpan;
28
29 // don't use inline functions in debug builds - we don't care about
30 // performances and this only leads to increased rebuild time (because every
31 // time an inline method is changed, all files including the header must be
32 // rebuilt)
33 #ifdef __WXDEBUG__
34 #define inline
35 #endif // Debug
36
37 /*
38 * TODO Well, everything :-)
39 *
40 * 1. Time zones with minutes (make wxTimeZone a class)
41 * 2. getdate() function like under Solaris
42 * 3. text conversion for wxDateSpan
43 */
44
45 /*
46 The three classes declared in this header represent:
47
48 1. An absolute moment in the time (wxDateTime)
49 2. A difference between two moments in the time, positive or negative
50 (wxTimeSpan)
51 3. A logical difference between two dates expressed in
52 years/months/weeks/days (wxDateSpan)
53
54 The following arithmetic operations are permitted (all others are not):
55
56 addition
57 --------
58
59 wxDateTime + wxTimeSpan = wxDateTime
60 wxDateTime + wxDateSpan = wxDateTime
61 wxTimeSpan + wxTimeSpan = wxTimeSpan
62 wxDateSpan + wxDateSpan = wxDateSpan
63
64 substraction
65 ------------
66 wxDateTime - wxDateTime = wxTimeSpan
67 wxTimeSpan - wxTimeSpan = wxTimeSpan
68 wxDateSpan - wxDateSpan = wxDateSpan
69
70 multiplication
71 --------------
72 wxTimeSpan * number = wxTimeSpan
73 wxDateSpan * number = wxDateSpan
74
75 unitary minus
76 -------------
77 -wxTimeSpan = wxTimeSpan
78 -wxDateSpan = wxDateSpan
79 */
80
81 // ----------------------------------------------------------------------------
82 // This class represents an absolute moment in the time
83 // ----------------------------------------------------------------------------
84
85 class WXDLLEXPORT wxDateTime
86 {
87 public:
88 // types
89 // ------------------------------------------------------------------------
90
91 // a small unsigned integer type for storing things like seconds, days
92 // of the week, &c. It should be at least short (i.e. not char) to
93 // contain the number of milliseconds - it may also be 'int' because
94 // there is no size penalty associated with it in our code, we don't
95 // store any data in this format
96 typedef unsigned short wxDateTime_t;
97
98 // the timezones
99 enum TZ
100 {
101 // the time in the current time zone
102 Local,
103
104 // zones from GMT (= Greenwhich Mean Time): they're guaranteed to be
105 // consequent numbers, so writing something like `GMT0 + offset' is
106 // safe if abs(offset) <= 12
107
108 // underscore stands for minus
109 GMT_12, GMT_11, GMT_10, GMT_9, GMT_8, GMT_7,
110 GMT_6, GMT_5, GMT_4, GMT_3, GMT_2, GMT_1,
111 GMT0,
112 GMT1, GMT2, GMT3, GMT4, GMT5, GMT6,
113 GMT7, GMT8, GMT9, GMT10, GMT11, GMT12,
114 // Note that GMT12 and GMT_12 are not the same: there is a difference
115 // of exactly one day between them
116
117 // some symbolic names for TZ
118
119 // Europe
120 WET = GMT0, // Western Europe Time
121 WEST = GMT1, // Western Europe Summer Time
122 CET = GMT1, // Central Europe Time
123 CEST = GMT2, // Central Europe Summer Time
124 EET = GMT2, // Eastern Europe Time
125 EEST = GMT3, // Eastern Europe Summer Time
126 MSK = GMT3, // Moscow Time
127 MSD = GMT4, // Moscow Summer Time
128
129 // US and Canada
130 AST = GMT_4, // Atlantic Standard Time
131 ADT = GMT_3, // Atlantic Daylight Time
132 EST = GMT_5, // Eastern Standard Time
133 EDT = GMT_4, // Eastern Daylight Saving Time
134 CST = GMT_6, // Central Standard Time
135 CDT = GMT_5, // Central Daylight Saving Time
136 MST = GMT_7, // Mountain Standard Time
137 MDT = GMT_6, // Mountain Daylight Saving Time
138 PST = GMT_8, // Pacific Standard Time
139 PDT = GMT_7, // Pacific Daylight Saving Time
140 HST = GMT_10, // Hawaiian Standard Time
141 AKST = GMT_9, // Alaska Standard Time
142 AKDT = GMT_8, // Alaska Daylight Saving Time
143
144 // Australia
145
146 A_WST = GMT8, // Western Standard Time
147 A_CST = GMT12 + 1, // Central Standard Time (+9.5)
148 A_EST = GMT10, // Eastern Standard Time
149 A_ESST = GMT11, // Eastern Summer Time
150
151 // TODO add more symbolic timezone names here
152
153 // Universal Coordinated Time = the new and politically correct name
154 // for GMT
155 UTC = GMT0
156 };
157
158 // the calendar systems we know about: notice that it's valid (for
159 // this classes purpose anyhow) to work with any of these calendars
160 // even with the dates before the historical appearance of the
161 // calendar
162 enum Calendar
163 {
164 Gregorian, // current calendar
165 Julian // calendar in use since -45 until the 1582 (or later)
166
167 // TODO Hebrew, Chinese, Maya, ... (just kidding) (or then may be not?)
168 };
169
170 // these values only are used to identify the different dates of
171 // adoption of the Gregorian calendar (see IsGregorian())
172 //
173 // All data and comments taken verbatim from "The Calendar FAQ (v 2.0)"
174 // by Claus Tøndering, http://www.pip.dknet.dk/~c-t/calendar.html
175 // except for the comments "we take".
176 //
177 // Symbol "->" should be read as "was followed by" in the comments
178 // which follow.
179 enum GregorianAdoption
180 {
181 Gr_Unknown, // no data for this country or it's too uncertain to use
182 Gr_Standard, // on the day 0 of Gregorian calendar: 15 Oct 1582
183
184 Gr_Alaska, // Oct 1867 when Alaska became part of the USA
185 Gr_Albania, // Dec 1912
186
187 Gr_Austria = Gr_Unknown, // Different regions on different dates
188 Gr_Austria_Brixen, // 5 Oct 1583 -> 16 Oct 1583
189 Gr_Austria_Salzburg = Gr_Austria_Brixen,
190 Gr_Austria_Tyrol = Gr_Austria_Brixen,
191 Gr_Austria_Carinthia, // 14 Dec 1583 -> 25 Dec 1583
192 Gr_Austria_Styria = Gr_Austria_Carinthia,
193
194 Gr_Belgium, // Then part of the Netherlands
195
196 Gr_Bulgaria = Gr_Unknown, // Unknown precisely (from 1915 to 1920)
197 Gr_Bulgaria_1, // 18 Mar 1916 -> 1 Apr 1916
198 Gr_Bulgaria_2, // 31 Mar 1916 -> 14 Apr 1916
199 Gr_Bulgaria_3, // 3 Sep 1920 -> 17 Sep 1920
200
201 Gr_Canada = Gr_Unknown, // Different regions followed the changes in
202 // Great Britain or France
203
204 Gr_China = Gr_Unknown, // Different authorities say:
205 Gr_China_1, // 18 Dec 1911 -> 1 Jan 1912
206 Gr_China_2, // 18 Dec 1928 -> 1 Jan 1929
207
208 Gr_Czechoslovakia, // (Bohemia and Moravia) 6 Jan 1584 -> 17 Jan 1584
209 Gr_Denmark, // (including Norway) 18 Feb 1700 -> 1 Mar 1700
210 Gr_Egypt, // 1875
211 Gr_Estonia, // 1918
212 Gr_Finland, // Then part of Sweden
213
214 Gr_France, // 9 Dec 1582 -> 20 Dec 1582
215 Gr_France_Alsace, // 4 Feb 1682 -> 16 Feb 1682
216 Gr_France_Lorraine, // 16 Feb 1760 -> 28 Feb 1760
217 Gr_France_Strasbourg, // February 1682
218
219 Gr_Germany = Gr_Unknown, // Different states on different dates:
220 Gr_Germany_Catholic, // 1583-1585 (we take 1584)
221 Gr_Germany_Prussia, // 22 Aug 1610 -> 2 Sep 1610
222 Gr_Germany_Protestant, // 18 Feb 1700 -> 1 Mar 1700
223
224 Gr_GreatBritain, // 2 Sep 1752 -> 14 Sep 1752 (use 'cal(1)')
225
226 Gr_Greece, // 9 Mar 1924 -> 23 Mar 1924
227 Gr_Hungary, // 21 Oct 1587 -> 1 Nov 1587
228 Gr_Ireland = Gr_GreatBritain,
229 Gr_Italy = Gr_Standard,
230
231 Gr_Japan = Gr_Unknown, // Different authorities say:
232 Gr_Japan_1, // 19 Dec 1872 -> 1 Jan 1873
233 Gr_Japan_2, // 19 Dec 1892 -> 1 Jan 1893
234 Gr_Japan_3, // 18 Dec 1918 -> 1 Jan 1919
235
236 Gr_Latvia, // 1915-1918 (we take 1915)
237 Gr_Lithuania, // 1915
238 Gr_Luxemburg, // 14 Dec 1582 -> 25 Dec 1582
239 Gr_Netherlands = Gr_Belgium, // (including Belgium) 1 Jan 1583
240
241 // this is too weird to take into account: the Gregorian calendar was
242 // introduced twice in Groningen, first time 28 Feb 1583 was followed
243 // by 11 Mar 1583, then it has gone back to Julian in the summer of
244 // 1584 and then 13 Dec 1700 -> 12 Jan 1701 - which is
245 // the date we take here
246 Gr_Netherlands_Groningen, // 13 Dec 1700 -> 12 Jan 1701
247 Gr_Netherlands_Gelderland, // 30 Jun 1700 -> 12 Jul 1700
248 Gr_Netherlands_Utrecht, // (and Overijssel) 30 Nov 1700->12 Dec 1700
249 Gr_Netherlands_Friesland, // (and Drenthe) 31 Dec 1700 -> 12 Jan 1701
250
251 Gr_Norway = Gr_Denmark, // Then part of Denmark
252 Gr_Poland = Gr_Standard,
253 Gr_Portugal = Gr_Standard,
254 Gr_Romania, // 31 Mar 1919 -> 14 Apr 1919
255 Gr_Russia, // 31 Jan 1918 -> 14 Feb 1918
256 Gr_Scotland = Gr_GreatBritain,
257 Gr_Spain = Gr_Standard,
258
259 // Sweden has a curious history. Sweden decided to make a gradual
260 // change from the Julian to the Gregorian calendar. By dropping every
261 // leap year from 1700 through 1740 the eleven superfluous days would
262 // be omitted and from 1 Mar 1740 they would be in sync with the
263 // Gregorian calendar. (But in the meantime they would be in sync with
264 // nobody!)
265 //
266 // So 1700 (which should have been a leap year in the Julian calendar)
267 // was not a leap year in Sweden. However, by mistake 1704 and 1708
268 // became leap years. This left Sweden out of synchronisation with
269 // both the Julian and the Gregorian world, so they decided to go back
270 // to the Julian calendar. In order to do this, they inserted an extra
271 // day in 1712, making that year a double leap year! So in 1712,
272 // February had 30 days in Sweden.
273 //
274 // Later, in 1753, Sweden changed to the Gregorian calendar by
275 // dropping 11 days like everyone else.
276 Gr_Sweden = Gr_Finland, // 17 Feb 1753 -> 1 Mar 1753
277
278 Gr_Switzerland = Gr_Unknown,// Different cantons used different dates
279 Gr_Switzerland_Catholic, // 1583, 1584 or 1597 (we take 1584)
280 Gr_Switzerland_Protestant, // 31 Dec 1700 -> 12 Jan 1701
281
282 Gr_Turkey, // 1 Jan 1927
283 Gr_USA = Gr_GreatBritain,
284 Gr_Wales = Gr_GreatBritain,
285 Gr_Yugoslavia // 1919
286 };
287
288 // the country parameter is used so far for calculating the start and
289 // the end of DST period and for deciding whether the date is a work
290 // day or not
291 //
292 // TODO move this to intl.h
293 enum Country
294 {
295 Country_Unknown, // no special information for this country
296 Country_Default, // set the default country with SetCountry() method
297
298 // TODO add more countries (for this we must know about DST and/or
299 // holidays for this country)
300 France,
301 USA
302 };
303
304 // symbolic names for the months
305 enum Month
306 {
307 Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec, Inv_Month
308 };
309
310 // symbolic names for the weekdays
311 enum WeekDay
312 {
313 Sun, Mon, Tue, Wed, Thu, Fri, Sat, Inv_WeekDay
314 };
315
316 // invalid value for the year
317 enum Year
318 {
319 Inv_Year = SHRT_MIN // should hold in wxDateTime_t
320 };
321
322 // helper classes
323 // ------------------------------------------------------------------------
324
325 // standard struct tm is limited to the years from 1900 (because
326 // tm_year field is the offset from 1900), so we use our own struct
327 // instead to represent broken down time
328 //
329 // NB: this struct should always be kept normalized (i.e. mon should
330 // be < 12, 1 <= day <= 31 &c), so use AddMonths(), AddDays()
331 // instead of modifying the member fields directly!
332 struct Tm
333 {
334 wxDateTime_t sec, min, hour, mday;
335 Month mon;
336 int year;
337
338 // default ctor inits the object to an invalid value
339 Tm();
340
341 // ctor from struct tm
342 Tm(const struct tm& tm);
343
344 // check that the given date/time is valid (in Gregorian calendar)
345 bool IsValid() const;
346
347 // get the week day
348 WeekDay GetWeekDay() // not const because wday may be changed
349 {
350 if ( wday == Inv_WeekDay )
351 ComputeWeekDay();
352
353 return (WeekDay)wday;
354 }
355
356 // add the given number of months to the date keeping it normalized
357 void AddMonths(wxDateTime_t monDiff);
358
359 // add the given number of months to the date keeping it normalized
360 void AddDays(wxDateTime_t dayDiff);
361
362 private:
363 // compute the weekday from other fields
364 void ComputeWeekDay();
365
366 // these values can't be accessed directly because they're not always
367 // computed and we calculate them on demand
368 wxDateTime_t wday, yday;
369 };
370
371 // a class representing a time zone: basicly, this is just an offset
372 // (in minutes) from GMT
373 class TimeZone
374 {
375 public:
376 TimeZone(TZ tz);
377 TimeZone(wxDateTime_t offset) { m_offset = offset; }
378
379 int GetOffset() const { return m_offset; }
380
381 private:
382 // offset for this timezone from GMT in minutes
383 int m_offset;
384 };
385
386 // static methods
387 // ------------------------------------------------------------------------
388
389 // set the current country
390 static void SetCountry(Country country);
391 // get the current country
392 static inline Country GetCountry();
393
394 // return the current year
395 static int GetCurrentYear(Calendar cal = Gregorian);
396
397 // convert the year as returned by wxDateTime::GetYear() to a year
398 // suitable for BC/AD notation. The difference is that BC year 1
399 // corresponds to the year 0 (while BC year 0 didn't exist) and AD
400 // year N is just year N.
401 static int ConvertYearToBC(int year);
402
403 // return the current month
404 static Month GetCurrentMonth(Calendar cal = Gregorian);
405
406 // returns TRUE if the given year is a leap year in the given calendar
407 static bool IsLeapYear(int year = Inv_Year, Calendar cal = Gregorian);
408
409 // get the century (19 for 1999, 20 for 2000 and -5 for 492 BC)
410 static int GetCentury(int year = Inv_Year);
411
412 // returns the number of days in this year (356 or 355 for Gregorian
413 // calendar usually :-)
414 static wxDateTime_t GetNumberOfDays(int year, Calendar cal = Gregorian);
415
416 // get the number of the days in the given month (default value for
417 // the year means the current one)
418 static wxDateTime_t GetNumberOfDays(Month month,
419 int year = Inv_Year,
420 Calendar cal = Gregorian);
421
422 // get the full (default) or abbreviated month name in the current
423 // locale, returns empty string on error
424 static wxString GetMonthName(Month month, bool abbr = FALSE);
425
426 // get the full (default) or abbreviated weekday name in the current
427 // locale, returns empty string on error
428 static wxString GetWeekDayName(WeekDay weekday, bool abbr = FALSE);
429
430 // get the beginning of DST for this year, will return invalid object
431 // if no DST applicable in this year. The default value of the
432 // parameter means to take the current year.
433 static wxDateTime GetBeginDST(int year = Inv_Year);
434 // get the end of DST for this year, will return invalid object
435 // if no DST applicable in this year. The default value of the
436 // parameter means to take the current year.
437 static wxDateTime GetEndDST(int year = Inv_Year);
438
439 // return the wxDateTime object for the current time
440 static inline wxDateTime Now();
441
442 // constructors: you should test whether the constructor succeeded with
443 // IsValid() function. The values Inv_Month and Inv_Year for the
444 // parameters mean take current month and/or year values.
445 //
446 // All new wxDateTime correspond to the local time, use ToUTC() or
447 // MakeUTC() to get the time in UTC/GMT.
448 // ------------------------------------------------------------------------
449
450 // default ctor does not initialize the object, use Set()!
451 wxDateTime() { }
452
453 // from time_t: seconds since the Epoch 00:00:00 UTC, Jan 1, 1970)
454 inline wxDateTime(time_t timet);
455 // from broken down time/date (only for standard Unix range)
456 inline wxDateTime(const struct tm& tm);
457 // from broken down time/date (any range)
458 inline wxDateTime(const Tm& tm);
459
460 // from separate values for each component, date set to today
461 inline wxDateTime(wxDateTime_t hour,
462 wxDateTime_t minute = 0,
463 wxDateTime_t second = 0,
464 wxDateTime_t millisec = 0);
465 // from separate values for each component with explicit date
466 inline wxDateTime(wxDateTime_t day, // day of the month
467 Month month = Inv_Month,
468 int year = Inv_Year, // 1999, not 99 please!
469 wxDateTime_t hour = 0,
470 wxDateTime_t minute = 0,
471 wxDateTime_t second = 0,
472 wxDateTime_t millisec = 0);
473
474 // default copy ctor ok
475
476 // no dtor
477
478 // assignment operators and Set() functions: all non const methods return
479 // the reference to this object. IsValid() should be used to test whether
480 // the function succeeded.
481 // ------------------------------------------------------------------------
482
483 // set to the current time
484 inline wxDateTime& SetToCurrent();
485
486 // set to given time_t value
487 inline wxDateTime& Set(time_t timet);
488
489 // set to given broken down time/date
490 wxDateTime& Set(const struct tm& tm);
491
492 // set to given broken down time/date
493 inline wxDateTime& Set(const Tm& tm);
494
495 // set to given time, date = today
496 wxDateTime& Set(wxDateTime_t hour,
497 wxDateTime_t minute = 0,
498 wxDateTime_t second = 0,
499 wxDateTime_t millisec = 0);
500
501 // from separate values for each component with explicit date
502 // (defaults for month and year are the current values)
503 wxDateTime& Set(wxDateTime_t day,
504 Month month = Inv_Month,
505 int year = Inv_Year, // 1999, not 99 please!
506 wxDateTime_t hour = 0,
507 wxDateTime_t minute = 0,
508 wxDateTime_t second = 0,
509 wxDateTime_t millisec = 0);
510
511 // resets time to 00:00:00, doesn't change the date
512 wxDateTime& ResetTime();
513
514 // the following functions don't change the values of the other
515 // fields, i.e. SetMinute() won't change either hour or seconds value
516
517 // set the year
518 wxDateTime& SetYear(int year);
519 // set the month
520 wxDateTime& SetMonth(Month month);
521 // set the day of the month
522 wxDateTime& SetDay(wxDateTime_t day);
523 // set hour
524 wxDateTime& SetHour(wxDateTime_t hour);
525 // set minute
526 wxDateTime& SetMinute(wxDateTime_t minute);
527 // set second
528 wxDateTime& SetSecond(wxDateTime_t second);
529 // set millisecond
530 wxDateTime& SetMillisecond(wxDateTime_t millisecond);
531
532 // assignment operator from time_t
533 wxDateTime& operator=(time_t timet) { return Set(timet); }
534
535 // assignment operator from broken down time/date
536 wxDateTime& operator=(const struct tm& tm) { return Set(tm); }
537
538 // assignment operator from broken down time/date
539 wxDateTime& operator=(const Tm& tm) { return Set(tm); }
540
541 // default assignment operator is ok
542
543 // calendar calculations (functions which set the date only leave the time
544 // unchanged, e.g. don't explictly zero it)
545 // ------------------------------------------------------------------------
546
547 // set to Nth occurence of given weekday in the given month of the
548 // given year (time is set to 0), return TRUE on success and FALSE on
549 // failure. n may be positive (1..5) or negative to count from the end
550 // of the month (see helper function SetToLastWeekDay())
551 bool SetToWeekDay(WeekDay weekday,
552 int n = 1,
553 Month month = Inv_Month,
554 int year = Inv_Year);
555
556 // sets to the last weekday in the given month, year
557 inline bool SetToLastWeekDay(WeekDay weekday,
558 Month month = Inv_Month,
559 int year = Inv_Year);
560
561 // sets the date to the given day of the given week in the year,
562 // returns TRUE on success and FALSE if given date doesn't exist (e.g.
563 // numWeek is > 53)
564 bool SetToTheWeek(wxDateTime_t numWeek, WeekDay weekday = Mon);
565
566 // sets the date to the last day of the given (or current) month or the
567 // given (or current) year
568 wxDateTime& SetToLastMonthDay(Month month = Inv_Month,
569 int year = Inv_Year);
570
571 // The definitions below were taken verbatim from
572 //
573 // http://www.capecod.net/~pbaum/date/date0.htm
574 //
575 // (Peter Baum's home page)
576 //
577 // definition: The Julian Day Number, Julian Day, or JD of a
578 // particular instant of time is the number of days and fractions of a
579 // day since 12 hours Universal Time (Greenwich mean noon) on January
580 // 1 of the year -4712, where the year is given in the Julian
581 // proleptic calendar. The idea of using this reference date was
582 // originally proposed by Joseph Scalizer in 1582 to count years but
583 // it was modified by 19th century astronomers to count days. One
584 // could have equivalently defined the reference time to be noon of
585 // November 24, -4713 if were understood that Gregorian calendar rules
586 // were applied. Julian days are Julian Day Numbers and are not to be
587 // confused with Julian dates.
588 //
589 // definition: The Rata Die number is a date specified as the number
590 // of days relative to a base date of December 31 of the year 0. Thus
591 // January 1 of the year 1 is Rata Die day 1.
592
593 // get the Julian Day number
594 long GetJulianDay() const;
595
596 // get the Rata Die number
597 long GetRataDie() const;
598
599 // TODO algorithms for calculating some important dates, such as
600 // religious holidays (Easter...) or moon/solar eclipses? Some
601 // algorithms can be found in the calendar FAQ
602
603 // timezone stuff: by default, we always work with local times, to get
604 // anything else, it should be requested explicitly
605 // ------------------------------------------------------------------------
606
607 // transform this object to UTC/GMT
608 wxDateTime& MakeUTC();
609 wxDateTime& MakeGMT() { return MakeUTC(); }
610
611 // get the time corresponding to this one in UTC/GMT
612 inline wxDateTime ToUTC() const;
613 wxDateTime ToGMT() const { return ToUTC(); }
614
615 // generic versions of the above
616
617 // transform from local time to any given timezone
618 inline wxDateTime ToTimezone(const TimeZone& tz) const;
619 wxDateTime& MakeTimezone(const TimeZone& tz);
620
621 // transform time from any timezone to the local time
622 inline wxDateTime ToLocalTime(const TimeZone& tz) const;
623 wxDateTime& MakeLocalTime(const TimeZone& tz);
624
625 // accessors: many of them take the timezone parameter which indicates the
626 // timezone for which to make the calculations and the default value means
627 // to do it for the current timezone of this machine (even if the function
628 // only operates with the date it's necessary because a date may wrap as
629 // result of timezone shift)
630 // ------------------------------------------------------------------------
631
632 // is the date valid (FALSE for uninitialized objects as well as after
633 // the functions which failed to convert the date to supported range)
634 inline bool IsValid() const { return this != &ms_InvDateTime; }
635
636 // get the broken down date/time representation
637 Tm GetTm() const;
638
639 // get the number of seconds since the Unix epoch - returns (time_t)-1
640 // if the value is out of range
641 inline time_t GetTicks() const;
642
643 // get the year (returns Inv_Year if date is invalid)
644 int GetYear() const { return GetTm().year; }
645 // get the month (Inv_Month if date is invalid)
646 Month GetMonth() const { return (Month)GetTm().mon; }
647 // get the month day (in 1..31 range, 0 if date is invalid)
648 wxDateTime_t GetDay() const { return GetTm().mday; }
649 // get the day of the week (Inv_WeekDay if date is invalid)
650 WeekDay GetWeekDay() const { return GetTm().GetWeekDay(); }
651 // get the hour of the day
652 wxDateTime_t GetHour() const { return GetTm().hour; }
653 // get the minute
654 wxDateTime_t GetMinute() const { return GetTm().min; }
655 // get the second
656 wxDateTime_t GetSecond() const { return GetTm().sec; }
657 // get milliseconds
658 wxDateTime_t GetMillisecond() const { return m_time.GetLo() % 1000; }
659
660 // get the day since the year start (1..366, 0 if date is invalid)
661 wxDateTime_t GetDayOfYear() const;
662 // get the week number since the year start (1..52, 0 if date is
663 // invalid)
664 wxDateTime_t GetWeekOfYear() const;
665
666 // is this date a work day? This depends on a country, of course,
667 // because the holidays are different in different countries
668 bool IsWorkDay(Country country = Country_Default,
669 TimeZone zone = Local) const;
670
671 // is this date later than Gregorian calendar introduction for the
672 // given country (see enum GregorianAdoption)?
673 //
674 // NB: this function shouldn't be considered as absolute authoiruty in
675 // the matter. Besides, for some countries the exact date of
676 // adoption of the Gregorian calendar is simply unknown.
677 bool IsGregorianDate(GregorianAdoption country = Gr_Standard) const;
678
679 // is daylight savings time in effect at this moment?
680 //
681 // Return value is > 0 if DST is in effect, 0 if it is not and -1 if
682 // the information is not available (this is compatible with ANSI C)
683 int IsDST(Country country = Country_Default, TimeZone zone = Local) const;
684
685 // comparison (see also functions below for operator versions)
686 // ------------------------------------------------------------------------
687
688 // returns TRUE if the two moments are strictly identical
689 inline bool IsEqualTo(const wxDateTime& datetime) const;
690
691 // returns TRUE if the two moments are identical
692 inline bool operator==(const wxDateTime& datetime) const;
693
694 // returns TRUE if the two moments are different
695 inline bool operator!=(const wxDateTime& datetime) const;
696
697 // returns TRUE if the date is strictly earlier than the given one
698 inline bool IsEarlierThan(const wxDateTime& datetime) const;
699
700 // returns TRUE if the date is strictly later than the given one
701 inline bool IsLaterThan(const wxDateTime& datetime) const;
702
703 // returns TRUE if the date is strictly in the given range
704 inline bool IsStrictlyBetween(const wxDateTime& t1,
705 const wxDateTime& t2) const;
706
707 // returns TRUE if the date is in the given range
708 inline bool IsBetween(const wxDateTime& t1, const wxDateTime& t2) const;
709
710 // date operations: for the non-const methods, the return value is this
711 // object itself (see also functions below for operator versions)
712 // ------------------------------------------------------------------------
713
714 // add a time span (positive or negative)
715 inline wxDateTime& Add(const wxTimeSpan& diff);
716 // add a time span (positive or negative)
717 inline wxDateTime& operator+=(const wxTimeSpan& diff);
718
719 // substract a time span (positive or negative)
720 inline wxDateTime& Substract(const wxTimeSpan& diff);
721 // substract a time span (positive or negative)
722 inline wxDateTime& operator-=(const wxTimeSpan& diff);
723
724 // add a date span (positive or negative)
725 wxDateTime& Add(const wxDateSpan& diff);
726 // add a date span (positive or negative)
727 inline wxDateTime& operator+=(const wxDateSpan& diff);
728
729 // substract a date span (positive or negative)
730 inline wxDateTime& Substract(const wxDateSpan& diff);
731 // substract a date span (positive or negative)
732 inline wxDateTime& operator-=(const wxDateSpan& diff);
733
734 // substract a date (may result in positive or negative time span)
735 inline wxTimeSpan Substract(const wxDateTime& datetime) const;
736 // substract a date (may result in positive or negative time span)
737 inline wxTimeSpan operator-(const wxDateTime& datetime) const;
738
739 // conversion to/from text: all conversions from text return TRUE on
740 // success or FALSE if the date is malformed/out of supported range
741 // ------------------------------------------------------------------------
742
743 // parse a string in RFC 822 format (found e.g. in mail headers and
744 // having the form "Wed, 10 Feb 1999 19:07:07 +0100")
745 bool ParseRfc822Date(const wxString& date);
746 // parse a date/time in the given format (see strptime(3))
747 bool ParseFormat(const wxString& date, const char *format = "%c");
748 // parse a string containing the date/time in "free" format, this
749 // function will try to make an educated guess at the string contents
750 // (and return FALSE if it fails)
751 bool ParseDateTime(const wxString& date);
752
753 // this function accepts strftime()-like format string (default
754 // argument corresponds to the preferred date and time representation
755 // for the current locale) and returns the string containing the
756 // resulting text representation
757 wxString Format(const wxChar *format = _T("%c")) const;
758 // preferred date representation for the current locale
759 wxString FormatDate() const { return Format(_T("%x")); }
760 // preferred time representation for the current locale
761 wxString FormatTime() const { return Format(_T("%X")); }
762
763 // implementation
764 // ------------------------------------------------------------------------
765
766 // get the internal representation
767 inline wxLongLong GetValue() const;
768
769 // a helper function to get the current time_t
770 static time_t GetTimeNow() { return time((time_t *)NULL); }
771
772 // another one to get the current time broken down
773 static struct tm *GetTmNow()
774 {
775 time_t t = GetTimeNow();
776 return localtime(&t);
777 }
778
779 private:
780 // the current country - as it's the same for all program objects (unless
781 // it runs on a _really_ big cluster system :-), this is a static member:
782 // see SetCountry() and GetCountry()
783 static Country ms_country;
784
785 // this constant is used to transform a time_t value to the internal
786 // representation, as time_t is in seconds and we use milliseconds it's
787 // fixed to 1000
788 static const unsigned int TIME_T_FACTOR;
789
790 // invalid wxDateTime object - returned by all functions which return
791 // "wxDateTime &" on failure
792 static wxDateTime ms_InvDateTime;
793
794 // returns TRUE if we fall in range in which we can use standard ANSI C
795 // functions
796 inline bool IsInStdRange() const;
797
798 // the internal representation of the time is the amount of milliseconds
799 // elapsed since the origin which is set by convention to the UNIX/C epoch
800 // value: the midnight of January 1, 1970 (UTC)
801 wxLongLong m_time;
802 };
803
804 // ----------------------------------------------------------------------------
805 // This class contains a difference between 2 wxDateTime values, so it makes
806 // sense to add it to wxDateTime and it is the result of substraction of 2
807 // objects of that class. See also wxDateSpan.
808 // ----------------------------------------------------------------------------
809
810 class WXDLLEXPORT wxTimeSpan
811 {
812 public:
813 // constructors
814 // ------------------------------------------------------------------------
815
816 // return the timespan for the given number of seconds
817 static wxTimeSpan Seconds(int sec) { return wxTimeSpan(0, 0, sec); }
818
819 // return the timespan for the given number of minutes
820 static wxTimeSpan Minutes(int min) { return wxTimeSpan(0, min, 0 ); }
821
822 // return the timespan for the given number of hours
823 static wxTimeSpan Hours(int hours) { return wxTimeSpan(hours, 0, 0); }
824
825 // default ctor constructs the 0 time span
826 wxTimeSpan() { }
827
828 // from separate values for each component, date set to 0 (hours are
829 // not restricted to 0..24 range, neither are minutes, seconds or
830 // milliseconds)
831 inline wxTimeSpan(int hours,
832 int minutes = 0,
833 int seconds = 0,
834 int milliseconds = 0);
835
836 // from internal representation
837 wxTimeSpan(wxLongLong diff) : m_diff(diff) { }
838
839 // default copy ctor is ok
840
841 // no dtor
842
843 // arithmetics with time spans
844 // ------------------------------------------------------------------------
845
846 // add two timespans together
847 inline wxTimeSpan& Add(const wxTimeSpan& diff);
848 // add two timespans together
849 wxTimeSpan& operator+=(const wxTimeSpan& diff) { return Add(diff); }
850
851 // substract another timespan
852 inline wxTimeSpan& Substract(const wxTimeSpan& diff);
853 // substract another timespan
854 wxTimeSpan& operator-=(const wxTimeSpan& diff) { return Substract(diff); }
855
856 // multiply timespan by a scalar
857 inline wxTimeSpan& Multiply(int n);
858 // multiply timespan by a scalar
859 wxTimeSpan& operator*=(int n) { return Multiply(n); }
860 // multiply timespan by a scalar
861 inline wxTimeSpan operator*(int n) const;
862
863 // return this timespan with inversed sign
864 wxTimeSpan Negate() const { return wxTimeSpan(-GetValue()); }
865 // negate the value of the timespan
866 wxTimeSpan& Neg() { m_diff = -GetValue(); return *this; }
867 // negate the value of the timespan
868 wxTimeSpan& operator-() { return Neg(); }
869
870 // return the absolute value of the timespan: does _not_ modify the
871 // object
872 inline wxTimeSpan Abs() const;
873
874 // there is intentionally no division because we don't want to
875 // introduce rounding errors in time calculations
876
877 // comparaison (see also operator versions below)
878 // ------------------------------------------------------------------------
879
880 // is the timespan null?
881 bool IsNull() const { return m_diff == 0l; }
882 // returns true if the timespan is null
883 bool operator!() const { return !IsNull(); }
884
885 // is the timespan positive?
886 bool IsPositive() const { return m_diff > 0l; }
887
888 // is the timespan negative?
889 bool IsNegative() const { return m_diff < 0l; }
890
891 // are two timespans equal?
892 inline bool IsEqualTo(const wxTimeSpan& ts) const;
893 // compare two timestamps: works with the absolute values, i.e. -2
894 // hours is longer than 1 hour. Also, it will return FALSE if the
895 // timespans are equal in absolute value.
896 inline bool IsLongerThan(const wxTimeSpan& ts) const;
897 // compare two timestamps: works with the absolute values, i.e. 1
898 // hour is shorter than -2 hours. Also, it will return FALSE if the
899 // timespans are equal in absolute value.
900 bool IsShorterThan(const wxTimeSpan& t) const { return !IsLongerThan(t); }
901
902 // breaking into years, ..., days, ..., seconds: all these functions
903 // behave like GetYears() which returns 1 for the timespan of 1 year and 1
904 // day, but 0 (and not -1) for the negative timespan of 1 year without 1
905 // day. IOW, (ts - wxTimeSpan(ts.GetYears())).GetYears() is always 0.
906 // ------------------------------------------------------------------------
907
908 // get the max number of years in this timespan
909 inline int GetYears() const;
910 // get the max number of months in this timespan
911 inline int GetMonths() const;
912 // get the max number of weeks in this timespan
913 inline int GetWeeks() const;
914 // get the max number of days in this timespan
915 inline int GetDays() const;
916 // get the max number of hours in this timespan
917 inline int GetHours() const;
918 // get the max number of minutes in this timespan
919 inline int GetMinutes() const;
920 // get the max number of seconds in this timespan
921 inline int GetSeconds() const;
922 // get the number of milliseconds in this timespan
923 wxLongLong GetMilliseconds() const { return m_diff; }
924
925 // conversion to text
926 // ------------------------------------------------------------------------
927
928 // this function accepts strftime()-like format string (default
929 // argument corresponds to the preferred date and time representation
930 // for the current locale) and returns the string containing the
931 // resulting text representation. Notice that only some of format
932 // specifiers valid for wxDateTime are valid for wxTimeSpan: hours,
933 // minutes and seconds make sense, but not "PM/AM" string for example.
934 wxString Format(const wxChar *format = _T("%c")) const;
935 // preferred date representation for the current locale
936 wxString FormatDate() const { return Format(_T("%x")); }
937 // preferred time representation for the current locale
938 wxString FormatTime() const { return Format(_T("%X")); }
939
940 // implementation
941 // ------------------------------------------------------------------------
942
943 // get the internal representation
944 wxLongLong GetValue() const { return m_diff; }
945
946 private:
947 // the (signed) time span in milliseconds
948 wxLongLong m_diff;
949 };
950
951 // ----------------------------------------------------------------------------
952 // This class is a "logical time span" and is useful for implementing program
953 // logic for such things as "add one month to the date" which, in general,
954 // doesn't mean to add 60*60*24*31 seconds to it, but to take the same date
955 // the next month (to understand that this is indeed different consider adding
956 // one month to Feb, 15 - we want to get Mar, 15, of course).
957 //
958 // When adding a month to the date, all lesser components (days, hours, ...)
959 // won't be changed.
960 //
961 // wxDateSpan can be either positive or negative. They may be
962 // multiplied by scalars which multiply all deltas by the scalar: i.e. 2*(1
963 // month and 1 day) is 2 months and 2 days. They can be added together and
964 // with wxDateTime or wxTimeSpan, but the type of result is different for each
965 // case.
966 //
967 // Beware about weeks: if you specify both weeks and days, the total number of
968 // days added will be 7*weeks + days! See also GetTotalDays() function.
969 //
970 // Finally, notice that for adding hours, minutes &c you don't need this
971 // class: wxTimeSpan will do the job because there are no subtleties
972 // associated with those.
973 // ----------------------------------------------------------------------------
974
975 class WXDLLEXPORT wxDateSpan
976 {
977 public:
978 // constructors
979 // ------------------------------------------------------------------------
980
981 // this many years/months/weeks/days
982 wxDateSpan(int years = 0, int months = 0, int weeks = 0, int days = 0)
983 {
984 m_years = years;
985 m_months = months;
986 m_weeks = weeks;
987 m_days = days;
988 }
989
990 // get an object for the given number of days
991 static wxDateSpan Days(int days) { return wxDateSpan(0, 0, 0, days); }
992
993 // get an object for the given number of weeks
994 static wxDateSpan Weeks(int weeks) { return wxDateSpan(0, 0, weeks, 0); }
995
996 // get an object for the given number of months
997 static wxDateSpan Months(int mon) { return wxDateSpan(0, mon, 0, 0); }
998
999 // get an object for the given number of years
1000 static wxDateSpan Years(int years) { return wxDateSpan(years, 0, 0, 0); }
1001
1002 // default copy ctor is ok
1003
1004 // no dtor
1005
1006 // accessors (all SetXXX() return the (modified) wxDateSpan object)
1007 // ------------------------------------------------------------------------
1008
1009 // set number of years
1010 wxDateSpan& SetYears(int n) { m_years = n; return *this; }
1011 // set number of months
1012 wxDateSpan& SetMonths(int n) { m_months = n; return *this; }
1013 // set number of weeks
1014 wxDateSpan& SetWeeks(int n) { m_weeks = n; return *this; }
1015 // set number of days
1016 wxDateSpan& SetDays(int n) { m_days = n; return *this; }
1017
1018 // get number of years
1019 int GetYears() const { return m_years; }
1020 // get number of months
1021 int GetMonths() const { return m_months; }
1022 // get number of weeks
1023 int GetWeeks() const { return m_weeks; }
1024 // get number of days
1025 int GetDays() const { return m_days; }
1026 // returns 7*GetWeeks() + GetDays()
1027 int GetTotalDays() const { return 7*m_weeks + m_days; }
1028
1029 // arithmetics
1030 // ------------------------------------------------------------------------
1031
1032 // add another wxDateSpan to us
1033 inline wxDateSpan& Add(const wxDateSpan& other);
1034 // add another wxDateSpan to us
1035 inline wxDateSpan& operator+=(const wxDateSpan& other);
1036
1037 // substract another wxDateSpan from us
1038 inline wxDateSpan& Substract(const wxDateSpan& other);
1039 // substract another wxDateSpan from us
1040 inline wxDateSpan& operator-=(const wxDateSpan& other);
1041
1042 // return a copy of this time span with changed sign
1043 inline wxDateSpan Negate() const;
1044 // inverse the sign of this timespan
1045 inline wxDateSpan& Neg();
1046 // inverse the sign of this timespan
1047 wxDateSpan& operator-() { return Neg(); }
1048
1049 // multiply all components by a (signed) number
1050 inline wxDateSpan& operator*=(int factor);
1051
1052 private:
1053 int m_years,
1054 m_months,
1055 m_weeks,
1056 m_days;
1057 };
1058
1059 WXDLLEXPORT_DATA(extern wxDateSpan) wxYear;
1060 WXDLLEXPORT_DATA(extern wxDateSpan) wxMonth;
1061 WXDLLEXPORT_DATA(extern wxDateSpan) wxWeek;
1062 WXDLLEXPORT_DATA(extern wxDateSpan) wxDay;
1063
1064 // ============================================================================
1065 // inline functions implementation
1066 // ============================================================================
1067
1068 // don't include inline functions definitions when we're included from anything
1069 // else than datetime.cpp in debug builds: this minimizes rebuilds if we change
1070 // some inline function and the performance doesn't matter in the debug builds.
1071
1072 #if !defined(__WXDEBUG__) || defined(wxDEFINE_TIME_CONSTANTS)
1073 #define INCLUDED_FROM_WX_DATETIME_H
1074 #include "wx/datetime.inl"
1075 #undef INCLUDED_FROM_WX_DATETIME_H
1076 #endif
1077
1078 // if we defined it to be empty above, restore it now
1079 #undef inline
1080
1081 // ============================================================================
1082 // binary operators
1083 // ============================================================================
1084
1085 // ----------------------------------------------------------------------------
1086 // wxDateTime operators
1087 // ----------------------------------------------------------------------------
1088
1089 inline bool WXDLLEXPORT operator<(const wxDateTime &t1, const wxDateTime &t2)
1090 {
1091 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), "invalid wxDateTime" );
1092
1093 return t1.GetValue() < t2.GetValue();
1094 }
1095
1096 inline bool WXDLLEXPORT operator<=(const wxDateTime &t1, const wxDateTime &t2)
1097 {
1098 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), "invalid wxDateTime" );
1099
1100 return t1.GetValue() <= t2.GetValue();
1101 }
1102
1103 inline bool WXDLLEXPORT operator>(const wxDateTime &t1, const wxDateTime &t2)
1104 {
1105 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), "invalid wxDateTime" );
1106
1107 return t1.GetValue() > t2.GetValue();
1108 }
1109
1110 inline bool WXDLLEXPORT operator>=(const wxDateTime &t1, const wxDateTime &t2)
1111 {
1112 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), "invalid wxDateTime" );
1113
1114 return t1.GetValue() >= t2.GetValue();
1115 }
1116
1117 inline bool WXDLLEXPORT operator==(const wxDateTime &t1, const wxDateTime &t2)
1118 {
1119 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), "invalid wxDateTime" );
1120
1121 return t1.GetValue() == t2.GetValue();
1122 }
1123
1124 inline bool WXDLLEXPORT operator!=(const wxDateTime &t1, const wxDateTime &t2)
1125 {
1126 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), "invalid wxDateTime" );
1127
1128 return t1.GetValue() != t2.GetValue();
1129 }
1130
1131 inline wxTimeSpan WXDLLEXPORT operator-(const wxDateTime &t1,
1132 const wxDateTime &t2)
1133 {
1134 wxASSERT_MSG( t1.IsValid() && t2.IsValid(), "invalid wxDateTime" );
1135
1136 return wxTimeSpan(t1.GetValue() - t2.GetValue());
1137 }
1138
1139 // ----------------------------------------------------------------------------
1140 // wxTimeSpan operators
1141 // ----------------------------------------------------------------------------
1142
1143 inline wxTimeSpan WXDLLEXPORT operator+(const wxTimeSpan& ts1,
1144 const wxTimeSpan& ts2)
1145 {
1146 return wxTimeSpan(ts1.GetValue() + ts2.GetValue());
1147 }
1148
1149 inline wxTimeSpan WXDLLEXPORT operator-(const wxTimeSpan& ts1,
1150 const wxTimeSpan& ts2)
1151 {
1152 return wxTimeSpan(ts1.GetValue() - ts2.GetValue());
1153 }
1154
1155 inline bool WXDLLEXPORT operator<(const wxTimeSpan &t1, const wxTimeSpan &t2)
1156 {
1157 return t1.GetValue() < t2.GetValue();
1158 }
1159
1160 inline bool WXDLLEXPORT operator<=(const wxTimeSpan &t1, const wxTimeSpan &t2)
1161 {
1162 return t1.GetValue() <= t2.GetValue();
1163 }
1164
1165 inline bool WXDLLEXPORT operator>(const wxTimeSpan &t1, const wxTimeSpan &t2)
1166 {
1167 return t1.GetValue() > t2.GetValue();
1168 }
1169
1170 inline bool WXDLLEXPORT operator>=(const wxTimeSpan &t1, const wxTimeSpan &t2)
1171 {
1172 return t1.GetValue() >= t2.GetValue();
1173 }
1174
1175 inline bool WXDLLEXPORT operator==(const wxTimeSpan &t1, const wxTimeSpan &t2)
1176 {
1177 return t1.GetValue() == t2.GetValue();
1178 }
1179
1180 inline bool WXDLLEXPORT operator!=(const wxTimeSpan &t1, const wxTimeSpan &t2)
1181 {
1182 return t1.GetValue() != t2.GetValue();
1183 }
1184
1185 // ----------------------------------------------------------------------------
1186 // wxDateSpan
1187 // ----------------------------------------------------------------------------
1188
1189 inline WXDLLEXPORT wxDateSpan operator+(const wxDateSpan& rt1,
1190 const wxDateSpan& rt2)
1191 {
1192 return wxDateSpan(rt1.GetYears() + rt2.GetYears(),
1193 rt1.GetMonths() + rt2.GetMonths(),
1194 rt1.GetWeeks() + rt2.GetWeeks(),
1195 rt1.GetDays() + rt2.GetDays());
1196 }
1197
1198 #endif // _WX_DATETIME_H