wxDateTime...
[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 msec, 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(int monDiff);
358
359 // add the given number of months to the date keeping it normalized
360 void AddDays(int 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 JDN (beware of rounding errors)
461 inline wxDateTime(double jdn);
462
463 // from separate values for each component, date set to today
464 inline wxDateTime(wxDateTime_t hour,
465 wxDateTime_t minute = 0,
466 wxDateTime_t second = 0,
467 wxDateTime_t millisec = 0);
468 // from separate values for each component with explicit date
469 inline wxDateTime(wxDateTime_t day, // day of the month
470 Month month = Inv_Month,
471 int year = Inv_Year, // 1999, not 99 please!
472 wxDateTime_t hour = 0,
473 wxDateTime_t minute = 0,
474 wxDateTime_t second = 0,
475 wxDateTime_t millisec = 0);
476
477 // default copy ctor ok
478
479 // no dtor
480
481 // assignment operators and Set() functions: all non const methods return
482 // the reference to this object. IsValid() should be used to test whether
483 // the function succeeded.
484 // ------------------------------------------------------------------------
485
486 // set to the current time
487 inline wxDateTime& SetToCurrent();
488
489 // set to given time_t value
490 inline wxDateTime& Set(time_t timet);
491
492 // set to given broken down time/date
493 wxDateTime& Set(const struct tm& tm);
494
495 // set to given broken down time/date
496 inline wxDateTime& Set(const Tm& tm);
497
498 // set to given JDN (beware of rounding errors)
499 wxDateTime& Set(double jdn);
500
501 // set to given time, date = today
502 wxDateTime& Set(wxDateTime_t hour,
503 wxDateTime_t minute = 0,
504 wxDateTime_t second = 0,
505 wxDateTime_t millisec = 0);
506
507 // from separate values for each component with explicit date
508 // (defaults for month and year are the current values)
509 wxDateTime& Set(wxDateTime_t day,
510 Month month = Inv_Month,
511 int year = Inv_Year, // 1999, not 99 please!
512 wxDateTime_t hour = 0,
513 wxDateTime_t minute = 0,
514 wxDateTime_t second = 0,
515 wxDateTime_t millisec = 0);
516
517 // resets time to 00:00:00, doesn't change the date
518 wxDateTime& ResetTime();
519
520 // the following functions don't change the values of the other
521 // fields, i.e. SetMinute() won't change either hour or seconds value
522
523 // set the year
524 wxDateTime& SetYear(int year);
525 // set the month
526 wxDateTime& SetMonth(Month month);
527 // set the day of the month
528 wxDateTime& SetDay(wxDateTime_t day);
529 // set hour
530 wxDateTime& SetHour(wxDateTime_t hour);
531 // set minute
532 wxDateTime& SetMinute(wxDateTime_t minute);
533 // set second
534 wxDateTime& SetSecond(wxDateTime_t second);
535 // set millisecond
536 wxDateTime& SetMillisecond(wxDateTime_t millisecond);
537
538 // assignment operator from time_t
539 wxDateTime& operator=(time_t timet) { return Set(timet); }
540
541 // assignment operator from broken down time/date
542 wxDateTime& operator=(const struct tm& tm) { return Set(tm); }
543
544 // assignment operator from broken down time/date
545 wxDateTime& operator=(const Tm& tm) { return Set(tm); }
546
547 // default assignment operator is ok
548
549 // calendar calculations (functions which set the date only leave the time
550 // unchanged, e.g. don't explictly zero it)
551 // ------------------------------------------------------------------------
552
553 // set to Nth occurence of given weekday in the given month of the
554 // given year (time is set to 0), return TRUE on success and FALSE on
555 // failure. n may be positive (1..5) or negative to count from the end
556 // of the month (see helper function SetToLastWeekDay())
557 bool SetToWeekDay(WeekDay weekday,
558 int n = 1,
559 Month month = Inv_Month,
560 int year = Inv_Year);
561
562 // sets to the last weekday in the given month, year
563 inline bool SetToLastWeekDay(WeekDay weekday,
564 Month month = Inv_Month,
565 int year = Inv_Year);
566
567 // sets the date to the given day of the given week in the year,
568 // returns TRUE on success and FALSE if given date doesn't exist (e.g.
569 // numWeek is > 53)
570 bool SetToTheWeek(wxDateTime_t numWeek, WeekDay weekday = Mon);
571
572 // sets the date to the last day of the given (or current) month or the
573 // given (or current) year
574 wxDateTime& SetToLastMonthDay(Month month = Inv_Month,
575 int year = Inv_Year);
576
577 // The definitions below were taken verbatim from
578 //
579 // http://www.capecod.net/~pbaum/date/date0.htm
580 //
581 // (Peter Baum's home page)
582 //
583 // definition: The Julian Day Number, Julian Day, or JD of a
584 // particular instant of time is the number of days and fractions of a
585 // day since 12 hours Universal Time (Greenwich mean noon) on January
586 // 1 of the year -4712, where the year is given in the Julian
587 // proleptic calendar. The idea of using this reference date was
588 // originally proposed by Joseph Scalizer in 1582 to count years but
589 // it was modified by 19th century astronomers to count days. One
590 // could have equivalently defined the reference time to be noon of
591 // November 24, -4713 if were understood that Gregorian calendar rules
592 // were applied. Julian days are Julian Day Numbers and are not to be
593 // confused with Julian dates.
594 //
595 // definition: The Rata Die number is a date specified as the number
596 // of days relative to a base date of December 31 of the year 0. Thus
597 // January 1 of the year 1 is Rata Die day 1.
598
599 // get the Julian Day number (the fractional part specifies the time of
600 // the day, related to noon - beware of rounding errors!)
601 double GetJulianDayNumber() const;
602
603 // get the Rata Die number
604 double GetRataDie() const;
605
606 // TODO algorithms for calculating some important dates, such as
607 // religious holidays (Easter...) or moon/solar eclipses? Some
608 // algorithms can be found in the calendar FAQ
609
610 // timezone stuff: by default, we always work with local times, to get
611 // anything else, it should be requested explicitly
612 // ------------------------------------------------------------------------
613
614 // transform this object to UTC/GMT
615 wxDateTime& MakeUTC();
616 wxDateTime& MakeGMT() { return MakeUTC(); }
617
618 // get the time corresponding to this one in UTC/GMT
619 inline wxDateTime ToUTC() const;
620 wxDateTime ToGMT() const { return ToUTC(); }
621
622 // generic versions of the above
623
624 // transform from local time to any given timezone
625 inline wxDateTime ToTimezone(const TimeZone& tz) const;
626 wxDateTime& MakeTimezone(const TimeZone& tz);
627
628 // transform time from any timezone to the local time
629 inline wxDateTime ToLocalTime(const TimeZone& tz) const;
630 wxDateTime& MakeLocalTime(const TimeZone& tz);
631
632 // accessors: many of them take the timezone parameter which indicates the
633 // timezone for which to make the calculations and the default value means
634 // to do it for the current timezone of this machine (even if the function
635 // only operates with the date it's necessary because a date may wrap as
636 // result of timezone shift)
637 // ------------------------------------------------------------------------
638
639 // is the date valid (FALSE for uninitialized objects as well as after
640 // the functions which failed to convert the date to supported range)
641 inline bool IsValid() const { return this != &ms_InvDateTime; }
642
643 // get the broken down date/time representation
644 Tm GetTm() const;
645
646 // get the number of seconds since the Unix epoch - returns (time_t)-1
647 // if the value is out of range
648 inline time_t GetTicks() const;
649
650 // get the year (returns Inv_Year if date is invalid)
651 int GetYear() const { return GetTm().year; }
652 // get the month (Inv_Month if date is invalid)
653 Month GetMonth() const { return (Month)GetTm().mon; }
654 // get the month day (in 1..31 range, 0 if date is invalid)
655 wxDateTime_t GetDay() const { return GetTm().mday; }
656 // get the day of the week (Inv_WeekDay if date is invalid)
657 WeekDay GetWeekDay() const { return GetTm().GetWeekDay(); }
658 // get the hour of the day
659 wxDateTime_t GetHour() const { return GetTm().hour; }
660 // get the minute
661 wxDateTime_t GetMinute() const { return GetTm().min; }
662 // get the second
663 wxDateTime_t GetSecond() const { return GetTm().sec; }
664 // get milliseconds
665 wxDateTime_t GetMillisecond() const { return m_time.GetLo() % 1000; }
666
667 // get the day since the year start (1..366, 0 if date is invalid)
668 wxDateTime_t GetDayOfYear() const;
669 // get the week number since the year start (1..52, 0 if date is
670 // invalid)
671 wxDateTime_t GetWeekOfYear() const;
672
673 // is this date a work day? This depends on a country, of course,
674 // because the holidays are different in different countries
675 bool IsWorkDay(Country country = Country_Default,
676 TimeZone zone = Local) const;
677
678 // is this date later than Gregorian calendar introduction for the
679 // given country (see enum GregorianAdoption)?
680 //
681 // NB: this function shouldn't be considered as absolute authoiruty in
682 // the matter. Besides, for some countries the exact date of
683 // adoption of the Gregorian calendar is simply unknown.
684 bool IsGregorianDate(GregorianAdoption country = Gr_Standard) const;
685
686 // is daylight savings time in effect at this moment?
687 //
688 // Return value is > 0 if DST is in effect, 0 if it is not and -1 if
689 // the information is not available (this is compatible with ANSI C)
690 int IsDST(Country country = Country_Default, TimeZone zone = Local) const;
691
692 // comparison (see also functions below for operator versions)
693 // ------------------------------------------------------------------------
694
695 // returns TRUE if the two moments are strictly identical
696 inline bool IsEqualTo(const wxDateTime& datetime) const;
697
698 // returns TRUE if the two moments are identical
699 inline bool operator==(const wxDateTime& datetime) const;
700
701 // returns TRUE if the two moments are different
702 inline bool operator!=(const wxDateTime& datetime) const;
703
704 // returns TRUE if the date is strictly earlier than the given one
705 inline bool IsEarlierThan(const wxDateTime& datetime) const;
706
707 // returns TRUE if the date is strictly later than the given one
708 inline bool IsLaterThan(const wxDateTime& datetime) const;
709
710 // returns TRUE if the date is strictly in the given range
711 inline bool IsStrictlyBetween(const wxDateTime& t1,
712 const wxDateTime& t2) const;
713
714 // returns TRUE if the date is in the given range
715 inline bool IsBetween(const wxDateTime& t1, const wxDateTime& t2) const;
716
717 // date operations: for the non-const methods, the return value is this
718 // object itself (see also functions below for operator versions)
719 // ------------------------------------------------------------------------
720
721 // add a time span (positive or negative)
722 inline wxDateTime& Add(const wxTimeSpan& diff);
723 // add a time span (positive or negative)
724 inline wxDateTime& operator+=(const wxTimeSpan& diff);
725
726 // substract a time span (positive or negative)
727 inline wxDateTime& Substract(const wxTimeSpan& diff);
728 // substract a time span (positive or negative)
729 inline wxDateTime& operator-=(const wxTimeSpan& diff);
730
731 // add a date span (positive or negative)
732 wxDateTime& Add(const wxDateSpan& diff);
733 // add a date span (positive or negative)
734 inline wxDateTime& operator+=(const wxDateSpan& diff);
735
736 // substract a date span (positive or negative)
737 inline wxDateTime& Substract(const wxDateSpan& diff);
738 // substract a date span (positive or negative)
739 inline wxDateTime& operator-=(const wxDateSpan& diff);
740
741 // substract a date (may result in positive or negative time span)
742 inline wxTimeSpan Substract(const wxDateTime& datetime) const;
743 // substract a date (may result in positive or negative time span)
744 inline wxTimeSpan operator-(const wxDateTime& datetime) const;
745
746 // conversion to/from text: all conversions from text return TRUE on
747 // success or FALSE if the date is malformed/out of supported range
748 // ------------------------------------------------------------------------
749
750 // parse a string in RFC 822 format (found e.g. in mail headers and
751 // having the form "Wed, 10 Feb 1999 19:07:07 +0100")
752 bool ParseRfc822Date(const wxString& date);
753 // parse a date/time in the given format (see strptime(3))
754 bool ParseFormat(const wxString& date, const char *format = "%c");
755 // parse a string containing the date/time in "free" format, this
756 // function will try to make an educated guess at the string contents
757 // (and return FALSE if it fails)
758 bool ParseDateTime(const wxString& date);
759
760 // this function accepts strftime()-like format string (default
761 // argument corresponds to the preferred date and time representation
762 // for the current locale) and returns the string containing the
763 // resulting text representation
764 wxString Format(const wxChar *format = _T("%c")) const;
765 // preferred date representation for the current locale
766 wxString FormatDate() const { return Format(_T("%x")); }
767 // preferred time representation for the current locale
768 wxString FormatTime() const { return Format(_T("%X")); }
769
770 // implementation
771 // ------------------------------------------------------------------------
772
773 // get the internal representation
774 inline wxLongLong GetValue() const;
775
776 // a helper function to get the current time_t
777 static time_t GetTimeNow() { return time((time_t *)NULL); }
778
779 // another one to get the current time broken down
780 static struct tm *GetTmNow()
781 {
782 time_t t = GetTimeNow();
783 return gmtime(&t);
784 }
785
786 private:
787 // the current country - as it's the same for all program objects (unless
788 // it runs on a _really_ big cluster system :-), this is a static member:
789 // see SetCountry() and GetCountry()
790 static Country ms_country;
791
792 // this constant is used to transform a time_t value to the internal
793 // representation, as time_t is in seconds and we use milliseconds it's
794 // fixed to 1000
795 static const unsigned int TIME_T_FACTOR;
796
797 // invalid wxDateTime object - returned by all functions which return
798 // "wxDateTime &" on failure
799 static wxDateTime ms_InvDateTime;
800
801 // returns TRUE if we fall in range in which we can use standard ANSI C
802 // functions
803 inline bool IsInStdRange() const;
804
805 // the internal representation of the time is the amount of milliseconds
806 // elapsed since the origin which is set by convention to the UNIX/C epoch
807 // value: the midnight of January 1, 1970 (UTC)
808 wxLongLong m_time;
809 };
810
811 // ----------------------------------------------------------------------------
812 // This class contains a difference between 2 wxDateTime values, so it makes
813 // sense to add it to wxDateTime and it is the result of substraction of 2
814 // objects of that class. See also wxDateSpan.
815 // ----------------------------------------------------------------------------
816
817 class WXDLLEXPORT wxTimeSpan
818 {
819 public:
820 // constructors
821 // ------------------------------------------------------------------------
822
823 // return the timespan for the given number of seconds
824 static wxTimeSpan Seconds(int sec) { return wxTimeSpan(0, 0, sec); }
825
826 // return the timespan for the given number of minutes
827 static wxTimeSpan Minutes(int min) { return wxTimeSpan(0, min, 0 ); }
828
829 // return the timespan for the given number of hours
830 static wxTimeSpan Hours(int hours) { return wxTimeSpan(hours, 0, 0); }
831
832 // default ctor constructs the 0 time span
833 wxTimeSpan() { }
834
835 // from separate values for each component, date set to 0 (hours are
836 // not restricted to 0..24 range, neither are minutes, seconds or
837 // milliseconds)
838 inline wxTimeSpan(int hours,
839 int minutes = 0,
840 int seconds = 0,
841 int milliseconds = 0);
842
843 // from internal representation
844 wxTimeSpan(wxLongLong diff) : m_diff(diff) { }
845
846 // default copy ctor is ok
847
848 // no dtor
849
850 // arithmetics with time spans
851 // ------------------------------------------------------------------------
852
853 // add two timespans together
854 inline wxTimeSpan& Add(const wxTimeSpan& diff);
855 // add two timespans together
856 wxTimeSpan& operator+=(const wxTimeSpan& diff) { return Add(diff); }
857
858 // substract another timespan
859 inline wxTimeSpan& Substract(const wxTimeSpan& diff);
860 // substract another timespan
861 wxTimeSpan& operator-=(const wxTimeSpan& diff) { return Substract(diff); }
862
863 // multiply timespan by a scalar
864 inline wxTimeSpan& Multiply(int n);
865 // multiply timespan by a scalar
866 wxTimeSpan& operator*=(int n) { return Multiply(n); }
867 // multiply timespan by a scalar
868 inline wxTimeSpan operator*(int n) const;
869
870 // return this timespan with inversed sign
871 wxTimeSpan Negate() const { return wxTimeSpan(-GetValue()); }
872 // negate the value of the timespan
873 wxTimeSpan& Neg() { m_diff = -GetValue(); return *this; }
874 // negate the value of the timespan
875 wxTimeSpan& operator-() { return Neg(); }
876
877 // return the absolute value of the timespan: does _not_ modify the
878 // object
879 inline wxTimeSpan Abs() const;
880
881 // there is intentionally no division because we don't want to
882 // introduce rounding errors in time calculations
883
884 // comparaison (see also operator versions below)
885 // ------------------------------------------------------------------------
886
887 // is the timespan null?
888 bool IsNull() const { return m_diff == 0l; }
889 // returns true if the timespan is null
890 bool operator!() const { return !IsNull(); }
891
892 // is the timespan positive?
893 bool IsPositive() const { return m_diff > 0l; }
894
895 // is the timespan negative?
896 bool IsNegative() const { return m_diff < 0l; }
897
898 // are two timespans equal?
899 inline bool IsEqualTo(const wxTimeSpan& ts) const;
900 // compare two timestamps: works with the absolute values, i.e. -2
901 // hours is longer than 1 hour. Also, it will return FALSE if the
902 // timespans are equal in absolute value.
903 inline bool IsLongerThan(const wxTimeSpan& ts) const;
904 // compare two timestamps: works with the absolute values, i.e. 1
905 // hour is shorter than -2 hours. Also, it will return FALSE if the
906 // timespans are equal in absolute value.
907 bool IsShorterThan(const wxTimeSpan& t) const { return !IsLongerThan(t); }
908
909 // breaking into days, hours, minutes and seconds
910 // ------------------------------------------------------------------------
911
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 wxLongLong 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