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