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