]> git.saurik.com Git - wxWidgets.git/blob - include/wx/datetime.h
detect horizontal overflow in wxHtmlPrintout and warn the user about it
[wxWidgets.git] / include / wx / datetime.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/datetime.h
3 // Purpose: declarations of time/date related classes (wxDateTime,
4 // wxTimeSpan)
5 // Author: Vadim Zeitlin
6 // Modified by:
7 // Created: 10.02.99
8 // RCS-ID: $Id$
9 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_DATETIME_H
14 #define _WX_DATETIME_H
15
16 #include "wx/defs.h"
17
18 #if wxUSE_DATETIME
19
20 #ifdef __WXWINCE__
21 #include "wx/msw/wince/time.h"
22 #elif !defined(__WXPALMOS5__)
23 #include <time.h>
24 #endif // OS
25
26 #include <limits.h> // for INT_MIN
27
28 #include "wx/longlong.h"
29 #include "wx/anystr.h"
30
31 class WXDLLIMPEXP_FWD_BASE wxDateTime;
32 class WXDLLIMPEXP_FWD_BASE wxTimeSpan;
33 class WXDLLIMPEXP_FWD_BASE wxDateSpan;
34 #ifdef __WXMSW__
35 struct _SYSTEMTIME;
36 #endif
37
38 #include "wx/dynarray.h"
39
40 // not all c-runtimes are based on 1/1/1970 being (time_t) 0
41 // set this to the corresponding value in seconds 1/1/1970 has on your
42 // systems c-runtime
43
44 #define WX_TIME_BASE_OFFSET 0
45
46 /*
47 * TODO
48 *
49 * + 1. Time zones with minutes (make TimeZone a class)
50 * ? 2. getdate() function like under Solaris
51 * + 3. text conversion for wxDateSpan
52 * + 4. pluggable modules for the workdays calculations
53 * 5. wxDateTimeHolidayAuthority for Easter and other christian feasts
54 */
55
56 /* Two wrapper functions for thread safety */
57 #ifdef HAVE_LOCALTIME_R
58 #define wxLocaltime_r localtime_r
59 #else
60 WXDLLIMPEXP_BASE struct tm *wxLocaltime_r(const time_t*, struct tm*);
61 #if wxUSE_THREADS && !defined(__WINDOWS__) && !defined(__WATCOMC__)
62 // On Windows, localtime _is_ threadsafe!
63 #warning using pseudo thread-safe wrapper for localtime to emulate localtime_r
64 #endif
65 #endif
66
67 #ifdef HAVE_GMTIME_R
68 #define wxGmtime_r gmtime_r
69 #else
70 WXDLLIMPEXP_BASE struct tm *wxGmtime_r(const time_t*, struct tm*);
71 #if wxUSE_THREADS && !defined(__WINDOWS__) && !defined(__WATCOMC__)
72 // On Windows, gmtime _is_ threadsafe!
73 #warning using pseudo thread-safe wrapper for gmtime to emulate gmtime_r
74 #endif
75 #endif
76
77 /*
78 The three (main) classes declared in this header represent:
79
80 1. An absolute moment in the time (wxDateTime)
81 2. A difference between two moments in the time, positive or negative
82 (wxTimeSpan)
83 3. A logical difference between two dates expressed in
84 years/months/weeks/days (wxDateSpan)
85
86 The following arithmetic operations are permitted (all others are not):
87
88 addition
89 --------
90
91 wxDateTime + wxTimeSpan = wxDateTime
92 wxDateTime + wxDateSpan = wxDateTime
93 wxTimeSpan + wxTimeSpan = wxTimeSpan
94 wxDateSpan + wxDateSpan = wxDateSpan
95
96 subtraction
97 ------------
98 wxDateTime - wxDateTime = wxTimeSpan
99 wxDateTime - wxTimeSpan = wxDateTime
100 wxDateTime - wxDateSpan = wxDateTime
101 wxTimeSpan - wxTimeSpan = wxTimeSpan
102 wxDateSpan - wxDateSpan = wxDateSpan
103
104 multiplication
105 --------------
106 wxTimeSpan * number = wxTimeSpan
107 number * wxTimeSpan = wxTimeSpan
108 wxDateSpan * number = wxDateSpan
109 number * wxDateSpan = wxDateSpan
110
111 unitary minus
112 -------------
113 -wxTimeSpan = wxTimeSpan
114 -wxDateSpan = wxDateSpan
115
116 For each binary operation OP (+, -, *) we have the following operatorOP=() as
117 a method and the method with a symbolic name OPER (Add, Subtract, Multiply)
118 as a synonym for it and another const method with the same name which returns
119 the changed copy of the object and operatorOP() as a global function which is
120 implemented in terms of the const version of OPEN. For the unary - we have
121 operator-() as a method, Neg() as synonym for it and Negate() which returns
122 the copy of the object with the changed sign.
123 */
124
125 // an invalid/default date time object which may be used as the default
126 // argument for arguments of type wxDateTime; it is also returned by all
127 // functions returning wxDateTime on failure (this is why it is also called
128 // wxInvalidDateTime)
129 class WXDLLIMPEXP_FWD_BASE wxDateTime;
130
131 extern WXDLLIMPEXP_DATA_BASE(const char *) wxDefaultDateTimeFormat;
132 extern WXDLLIMPEXP_DATA_BASE(const char *) wxDefaultTimeSpanFormat;
133 extern WXDLLIMPEXP_DATA_BASE(const wxDateTime) wxDefaultDateTime;
134
135 #define wxInvalidDateTime wxDefaultDateTime
136
137
138 // ----------------------------------------------------------------------------
139 // conditional compilation
140 // ----------------------------------------------------------------------------
141
142 #if defined(HAVE_STRPTIME) && defined(__GLIBC__) && \
143 ((__GLIBC__ == 2) && (__GLIBC_MINOR__ == 0))
144 // glibc 2.0.7 strptime() is broken - the following snippet causes it to
145 // crash (instead of just failing):
146 //
147 // strncpy(buf, "Tue Dec 21 20:25:40 1999", 128);
148 // strptime(buf, "%x", &tm);
149 //
150 // so don't use it
151 #undef HAVE_STRPTIME
152 #endif // broken strptime()
153
154 #if defined(HAVE_STRPTIME) && defined(__DARWIN__) && defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS
155 // configure detects strptime as linkable because it's in the OS X
156 // System library but MSL headers don't declare it.
157
158 // char *strptime(const char *, const char *, struct tm *);
159 // However, we DON'T want to just provide it here because we would
160 // crash and/or overwrite data when strptime from OS X tries
161 // to fill in MW's struct tm which is two fields shorter (no TZ stuff)
162 // So for now let's just say we don't have strptime
163 #undef HAVE_STRPTIME
164 #endif
165
166 // everyone has strftime except Win CE unless VC8 is used
167 #if !defined(__WXWINCE__) || defined(__VISUALC8__)
168 #define HAVE_STRFTIME
169 #endif
170
171 // ----------------------------------------------------------------------------
172 // wxDateTime represents an absolute moment in the time
173 // ----------------------------------------------------------------------------
174
175 class WXDLLIMPEXP_BASE wxDateTime
176 {
177 public:
178 // types
179 // ------------------------------------------------------------------------
180
181 // a small unsigned integer type for storing things like minutes,
182 // seconds &c. It should be at least short (i.e. not char) to contain
183 // the number of milliseconds - it may also be 'int' because there is
184 // no size penalty associated with it in our code, we don't store any
185 // data in this format
186 typedef unsigned short wxDateTime_t;
187
188 // constants
189 // ------------------------------------------------------------------------
190
191 // the timezones
192 enum TZ
193 {
194 // the time in the current time zone
195 Local,
196
197 // zones from GMT (= Greenwhich Mean Time): they're guaranteed to be
198 // consequent numbers, so writing something like `GMT0 + offset' is
199 // safe if abs(offset) <= 12
200
201 // underscore stands for minus
202 GMT_12, GMT_11, GMT_10, GMT_9, GMT_8, GMT_7,
203 GMT_6, GMT_5, GMT_4, GMT_3, GMT_2, GMT_1,
204 GMT0,
205 GMT1, GMT2, GMT3, GMT4, GMT5, GMT6,
206 GMT7, GMT8, GMT9, GMT10, GMT11, GMT12, GMT13,
207 // Note that GMT12 and GMT_12 are not the same: there is a difference
208 // of exactly one day between them
209
210 // some symbolic names for TZ
211
212 // Europe
213 WET = GMT0, // Western Europe Time
214 WEST = GMT1, // Western Europe Summer Time
215 CET = GMT1, // Central Europe Time
216 CEST = GMT2, // Central Europe Summer Time
217 EET = GMT2, // Eastern Europe Time
218 EEST = GMT3, // Eastern Europe Summer Time
219 MSK = GMT3, // Moscow Time
220 MSD = GMT4, // Moscow Summer Time
221
222 // US and Canada
223 AST = GMT_4, // Atlantic Standard Time
224 ADT = GMT_3, // Atlantic Daylight Time
225 EST = GMT_5, // Eastern Standard Time
226 EDT = GMT_4, // Eastern Daylight Saving Time
227 CST = GMT_6, // Central Standard Time
228 CDT = GMT_5, // Central Daylight Saving Time
229 MST = GMT_7, // Mountain Standard Time
230 MDT = GMT_6, // Mountain Daylight Saving Time
231 PST = GMT_8, // Pacific Standard Time
232 PDT = GMT_7, // Pacific Daylight Saving Time
233 HST = GMT_10, // Hawaiian Standard Time
234 AKST = GMT_9, // Alaska Standard Time
235 AKDT = GMT_8, // Alaska Daylight Saving Time
236
237 // Australia
238
239 A_WST = GMT8, // Western Standard Time
240 A_CST = GMT13 + 1, // Central Standard Time (+9.5)
241 A_EST = GMT10, // Eastern Standard Time
242 A_ESST = GMT11, // Eastern Summer Time
243
244 // New Zealand
245 NZST = GMT12, // Standard Time
246 NZDT = GMT13, // Daylight Saving Time
247
248 // TODO add more symbolic timezone names here
249
250 // Universal Coordinated Time = the new and politically correct name
251 // for GMT
252 UTC = GMT0
253 };
254
255 // the calendar systems we know about: notice that it's valid (for
256 // this classes purpose anyhow) to work with any of these calendars
257 // even with the dates before the historical appearance of the
258 // calendar
259 enum Calendar
260 {
261 Gregorian, // current calendar
262 Julian // calendar in use since -45 until the 1582 (or later)
263
264 // TODO Hebrew, Chinese, Maya, ... (just kidding) (or then may be not?)
265 };
266
267 // these values only are used to identify the different dates of
268 // adoption of the Gregorian calendar (see IsGregorian())
269 //
270 // All data and comments taken verbatim from "The Calendar FAQ (v 2.0)"
271 // by Claus T�ndering, http://www.pip.dknet.dk/~c-t/calendar.html
272 // except for the comments "we take".
273 //
274 // Symbol "->" should be read as "was followed by" in the comments
275 // which follow.
276 enum GregorianAdoption
277 {
278 Gr_Unknown, // no data for this country or it's too uncertain to use
279 Gr_Standard, // on the day 0 of Gregorian calendar: 15 Oct 1582
280
281 Gr_Alaska, // Oct 1867 when Alaska became part of the USA
282 Gr_Albania, // Dec 1912
283
284 Gr_Austria = Gr_Unknown, // Different regions on different dates
285 Gr_Austria_Brixen, // 5 Oct 1583 -> 16 Oct 1583
286 Gr_Austria_Salzburg = Gr_Austria_Brixen,
287 Gr_Austria_Tyrol = Gr_Austria_Brixen,
288 Gr_Austria_Carinthia, // 14 Dec 1583 -> 25 Dec 1583
289 Gr_Austria_Styria = Gr_Austria_Carinthia,
290
291 Gr_Belgium, // Then part of the Netherlands
292
293 Gr_Bulgaria = Gr_Unknown, // Unknown precisely (from 1915 to 1920)
294 Gr_Bulgaria_1, // 18 Mar 1916 -> 1 Apr 1916
295 Gr_Bulgaria_2, // 31 Mar 1916 -> 14 Apr 1916
296 Gr_Bulgaria_3, // 3 Sep 1920 -> 17 Sep 1920
297
298 Gr_Canada = Gr_Unknown, // Different regions followed the changes in
299 // Great Britain or France
300
301 Gr_China = Gr_Unknown, // Different authorities say:
302 Gr_China_1, // 18 Dec 1911 -> 1 Jan 1912
303 Gr_China_2, // 18 Dec 1928 -> 1 Jan 1929
304
305 Gr_Czechoslovakia, // (Bohemia and Moravia) 6 Jan 1584 -> 17 Jan 1584
306 Gr_Denmark, // (including Norway) 18 Feb 1700 -> 1 Mar 1700
307 Gr_Egypt, // 1875
308 Gr_Estonia, // 1918
309 Gr_Finland, // Then part of Sweden
310
311 Gr_France, // 9 Dec 1582 -> 20 Dec 1582
312 Gr_France_Alsace, // 4 Feb 1682 -> 16 Feb 1682
313 Gr_France_Lorraine, // 16 Feb 1760 -> 28 Feb 1760
314 Gr_France_Strasbourg, // February 1682
315
316 Gr_Germany = Gr_Unknown, // Different states on different dates:
317 Gr_Germany_Catholic, // 1583-1585 (we take 1584)
318 Gr_Germany_Prussia, // 22 Aug 1610 -> 2 Sep 1610
319 Gr_Germany_Protestant, // 18 Feb 1700 -> 1 Mar 1700
320
321 Gr_GreatBritain, // 2 Sep 1752 -> 14 Sep 1752 (use 'cal(1)')
322
323 Gr_Greece, // 9 Mar 1924 -> 23 Mar 1924
324 Gr_Hungary, // 21 Oct 1587 -> 1 Nov 1587
325 Gr_Ireland = Gr_GreatBritain,
326 Gr_Italy = Gr_Standard,
327
328 Gr_Japan = Gr_Unknown, // Different authorities say:
329 Gr_Japan_1, // 19 Dec 1872 -> 1 Jan 1873
330 Gr_Japan_2, // 19 Dec 1892 -> 1 Jan 1893
331 Gr_Japan_3, // 18 Dec 1918 -> 1 Jan 1919
332
333 Gr_Latvia, // 1915-1918 (we take 1915)
334 Gr_Lithuania, // 1915
335 Gr_Luxemburg, // 14 Dec 1582 -> 25 Dec 1582
336 Gr_Netherlands = Gr_Belgium, // (including Belgium) 1 Jan 1583
337
338 // this is too weird to take into account: the Gregorian calendar was
339 // introduced twice in Groningen, first time 28 Feb 1583 was followed
340 // by 11 Mar 1583, then it has gone back to Julian in the summer of
341 // 1584 and then 13 Dec 1700 -> 12 Jan 1701 - which is
342 // the date we take here
343 Gr_Netherlands_Groningen, // 13 Dec 1700 -> 12 Jan 1701
344 Gr_Netherlands_Gelderland, // 30 Jun 1700 -> 12 Jul 1700
345 Gr_Netherlands_Utrecht, // (and Overijssel) 30 Nov 1700->12 Dec 1700
346 Gr_Netherlands_Friesland, // (and Drenthe) 31 Dec 1700 -> 12 Jan 1701
347
348 Gr_Norway = Gr_Denmark, // Then part of Denmark
349 Gr_Poland = Gr_Standard,
350 Gr_Portugal = Gr_Standard,
351 Gr_Romania, // 31 Mar 1919 -> 14 Apr 1919
352 Gr_Russia, // 31 Jan 1918 -> 14 Feb 1918
353 Gr_Scotland = Gr_GreatBritain,
354 Gr_Spain = Gr_Standard,
355
356 // Sweden has a curious history. Sweden decided to make a gradual
357 // change from the Julian to the Gregorian calendar. By dropping every
358 // leap year from 1700 through 1740 the eleven superfluous days would
359 // be omitted and from 1 Mar 1740 they would be in sync with the
360 // Gregorian calendar. (But in the meantime they would be in sync with
361 // nobody!)
362 //
363 // So 1700 (which should have been a leap year in the Julian calendar)
364 // was not a leap year in Sweden. However, by mistake 1704 and 1708
365 // became leap years. This left Sweden out of synchronisation with
366 // both the Julian and the Gregorian world, so they decided to go back
367 // to the Julian calendar. In order to do this, they inserted an extra
368 // day in 1712, making that year a double leap year! So in 1712,
369 // February had 30 days in Sweden.
370 //
371 // Later, in 1753, Sweden changed to the Gregorian calendar by
372 // dropping 11 days like everyone else.
373 Gr_Sweden = Gr_Finland, // 17 Feb 1753 -> 1 Mar 1753
374
375 Gr_Switzerland = Gr_Unknown,// Different cantons used different dates
376 Gr_Switzerland_Catholic, // 1583, 1584 or 1597 (we take 1584)
377 Gr_Switzerland_Protestant, // 31 Dec 1700 -> 12 Jan 1701
378
379 Gr_Turkey, // 1 Jan 1927
380 Gr_USA = Gr_GreatBritain,
381 Gr_Wales = Gr_GreatBritain,
382 Gr_Yugoslavia // 1919
383 };
384
385 // the country parameter is used so far for calculating the start and
386 // the end of DST period and for deciding whether the date is a work
387 // day or not
388 //
389 // TODO move this to intl.h
390
391 // Required for WinCE
392 #ifdef USA
393 #undef USA
394 #endif
395
396 enum Country
397 {
398 Country_Unknown, // no special information for this country
399 Country_Default, // set the default country with SetCountry() method
400 // or use the default country with any other
401
402 // TODO add more countries (for this we must know about DST and/or
403 // holidays for this country)
404
405 // Western European countries: we assume that they all follow the same
406 // DST rules (true or false?)
407 Country_WesternEurope_Start,
408 Country_EEC = Country_WesternEurope_Start,
409 France,
410 Germany,
411 UK,
412 Country_WesternEurope_End = UK,
413
414 Russia,
415 USA
416 };
417 // symbolic names for the months
418 enum Month
419 {
420 Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec, Inv_Month
421 };
422
423 // symbolic names for the weekdays
424 enum WeekDay
425 {
426 Sun, Mon, Tue, Wed, Thu, Fri, Sat, Inv_WeekDay
427 };
428
429 // invalid value for the year
430 enum Year
431 {
432 Inv_Year = SHRT_MIN // should hold in wxDateTime_t
433 };
434
435 // flags for GetWeekDayName and GetMonthName
436 enum NameFlags
437 {
438 Name_Full = 0x01, // return full name
439 Name_Abbr = 0x02 // return abbreviated name
440 };
441
442 // flags for GetWeekOfYear and GetWeekOfMonth
443 enum WeekFlags
444 {
445 Default_First, // Sunday_First for US, Monday_First for the rest
446 Monday_First, // week starts with a Monday
447 Sunday_First // week starts with a Sunday
448 };
449
450 // helper classes
451 // ------------------------------------------------------------------------
452
453 // a class representing a time zone: basicly, this is just an offset
454 // (in seconds) from GMT
455 class WXDLLIMPEXP_BASE TimeZone
456 {
457 public:
458 TimeZone(TZ tz);
459
460 // create time zone object with the given offset
461 TimeZone(long offset = 0) { m_offset = offset; }
462
463 static TimeZone Make(long offset)
464 {
465 TimeZone tz;
466 tz.m_offset = offset;
467 return tz;
468 }
469
470 long GetOffset() const { return m_offset; }
471
472 private:
473 // offset for this timezone from GMT in seconds
474 long m_offset;
475 };
476
477 // standard struct tm is limited to the years from 1900 (because
478 // tm_year field is the offset from 1900), so we use our own struct
479 // instead to represent broken down time
480 //
481 // NB: this struct should always be kept normalized (i.e. mon should
482 // be < 12, 1 <= day <= 31 &c), so use AddMonths(), AddDays()
483 // instead of modifying the member fields directly!
484 struct WXDLLIMPEXP_BASE Tm
485 {
486 wxDateTime_t msec, sec, min, hour, mday;
487 Month mon;
488 int year;
489
490 // default ctor inits the object to an invalid value
491 Tm();
492
493 // ctor from struct tm and the timezone
494 Tm(const struct tm& tm, const TimeZone& tz);
495
496 // check that the given date/time is valid (in Gregorian calendar)
497 bool IsValid() const;
498
499 // get the week day
500 WeekDay GetWeekDay() // not const because wday may be changed
501 {
502 if ( wday == Inv_WeekDay )
503 ComputeWeekDay();
504
505 return (WeekDay)wday;
506 }
507
508 // add the given number of months to the date keeping it normalized
509 void AddMonths(int monDiff);
510
511 // add the given number of months to the date keeping it normalized
512 void AddDays(int dayDiff);
513
514 private:
515 // compute the weekday from other fields
516 void ComputeWeekDay();
517
518 // the timezone we correspond to
519 TimeZone m_tz;
520
521 // these values can't be accessed directly because they're not always
522 // computed and we calculate them on demand
523 wxDateTime_t wday, yday;
524 };
525
526 // static methods
527 // ------------------------------------------------------------------------
528
529 // set the current country
530 static void SetCountry(Country country);
531 // get the current country
532 static Country GetCountry();
533
534 // return true if the country is a West European one (in practice,
535 // this means that the same DST rules as for EEC apply)
536 static bool IsWestEuropeanCountry(Country country = Country_Default);
537
538 // return the current year
539 static int GetCurrentYear(Calendar cal = Gregorian);
540
541 // convert the year as returned by wxDateTime::GetYear() to a year
542 // suitable for BC/AD notation. The difference is that BC year 1
543 // corresponds to the year 0 (while BC year 0 didn't exist) and AD
544 // year N is just year N.
545 static int ConvertYearToBC(int year);
546
547 // return the current month
548 static Month GetCurrentMonth(Calendar cal = Gregorian);
549
550 // returns true if the given year is a leap year in the given calendar
551 static bool IsLeapYear(int year = Inv_Year, Calendar cal = Gregorian);
552
553 // get the century (19 for 1999, 20 for 2000 and -5 for 492 BC)
554 static int GetCentury(int year);
555
556 // returns the number of days in this year (356 or 355 for Gregorian
557 // calendar usually :-)
558 static wxDateTime_t GetNumberOfDays(int year, Calendar cal = Gregorian);
559
560 // get the number of the days in the given month (default value for
561 // the year means the current one)
562 static wxDateTime_t GetNumberOfDays(Month month,
563 int year = Inv_Year,
564 Calendar cal = Gregorian);
565
566
567 // get the full (default) or abbreviated month name in the current
568 // locale, returns empty string on error
569 static wxString GetMonthName(Month month,
570 NameFlags flags = Name_Full);
571
572 // get the standard English full (default) or abbreviated month name
573 static wxString GetEnglishMonthName(Month month,
574 NameFlags flags = Name_Full);
575
576 // get the full (default) or abbreviated weekday name in the current
577 // locale, returns empty string on error
578 static wxString GetWeekDayName(WeekDay weekday,
579 NameFlags flags = Name_Full);
580
581 // get the standard English full (default) or abbreviated weekday name
582 static wxString GetEnglishWeekDayName(WeekDay weekday,
583 NameFlags flags = Name_Full);
584
585 // get the AM and PM strings in the current locale (may be empty)
586 static void GetAmPmStrings(wxString *am, wxString *pm);
587
588 // return true if the given country uses DST for this year
589 static bool IsDSTApplicable(int year = Inv_Year,
590 Country country = Country_Default);
591
592 // get the beginning of DST for this year, will return invalid object
593 // if no DST applicable in this year. The default value of the
594 // parameter means to take the current year.
595 static wxDateTime GetBeginDST(int year = Inv_Year,
596 Country country = Country_Default);
597 // get the end of DST for this year, will return invalid object
598 // if no DST applicable in this year. The default value of the
599 // parameter means to take the current year.
600 static wxDateTime GetEndDST(int year = Inv_Year,
601 Country country = Country_Default);
602
603 // return the wxDateTime object for the current time
604 static inline wxDateTime Now();
605
606 // return the wxDateTime object for the current time with millisecond
607 // precision (if available on this platform)
608 static wxDateTime UNow();
609
610 // return the wxDateTime object for today midnight: i.e. as Now() but
611 // with time set to 0
612 static inline wxDateTime Today();
613
614 // constructors: you should test whether the constructor succeeded with
615 // IsValid() function. The values Inv_Month and Inv_Year for the
616 // parameters mean take current month and/or year values.
617 // ------------------------------------------------------------------------
618
619 // default ctor does not initialize the object, use Set()!
620 wxDateTime() { m_time = wxLongLong(wxINT32_MIN, 0); }
621
622 // from time_t: seconds since the Epoch 00:00:00 UTC, Jan 1, 1970)
623 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
624 // VA C++ confuses this with wxDateTime(double jdn) thinking it is a duplicate declaration
625 inline wxDateTime(time_t timet);
626 #endif
627 // from broken down time/date (only for standard Unix range)
628 inline wxDateTime(const struct tm& tm);
629 // from broken down time/date (any range)
630 inline wxDateTime(const Tm& tm);
631
632 // from JDN (beware of rounding errors)
633 inline wxDateTime(double jdn);
634
635 // from separate values for each component, date set to today
636 inline wxDateTime(wxDateTime_t hour,
637 wxDateTime_t minute = 0,
638 wxDateTime_t second = 0,
639 wxDateTime_t millisec = 0);
640 // from separate values for each component with explicit date
641 inline wxDateTime(wxDateTime_t day, // day of the month
642 Month month,
643 int year = Inv_Year, // 1999, not 99 please!
644 wxDateTime_t hour = 0,
645 wxDateTime_t minute = 0,
646 wxDateTime_t second = 0,
647 wxDateTime_t millisec = 0);
648 #ifdef __WXMSW__
649 wxDateTime(const struct _SYSTEMTIME& st)
650 {
651 SetFromMSWSysTime(st);
652 }
653 #endif
654
655 // default copy ctor ok
656
657 // no dtor
658
659 // assignment operators and Set() functions: all non const methods return
660 // the reference to this object. IsValid() should be used to test whether
661 // the function succeeded.
662 // ------------------------------------------------------------------------
663
664 // set to the current time
665 inline wxDateTime& SetToCurrent();
666
667 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
668 // VA C++ confuses this with wxDateTime(double jdn) thinking it is a duplicate declaration
669 // set to given time_t value
670 inline wxDateTime& Set(time_t timet);
671 #endif
672
673 // set to given broken down time/date
674 wxDateTime& Set(const struct tm& tm);
675
676 // set to given broken down time/date
677 inline wxDateTime& Set(const Tm& tm);
678
679 // set to given JDN (beware of rounding errors)
680 wxDateTime& Set(double jdn);
681
682 // set to given time, date = today
683 wxDateTime& Set(wxDateTime_t hour,
684 wxDateTime_t minute = 0,
685 wxDateTime_t second = 0,
686 wxDateTime_t millisec = 0);
687
688 // from separate values for each component with explicit date
689 // (defaults for month and year are the current values)
690 wxDateTime& Set(wxDateTime_t day,
691 Month month,
692 int year = Inv_Year, // 1999, not 99 please!
693 wxDateTime_t hour = 0,
694 wxDateTime_t minute = 0,
695 wxDateTime_t second = 0,
696 wxDateTime_t millisec = 0);
697
698 // resets time to 00:00:00, doesn't change the date
699 wxDateTime& ResetTime();
700
701 // get the date part of this object only, i.e. the object which has the
702 // same date as this one but time of 00:00:00
703 wxDateTime GetDateOnly() const;
704
705 // the following functions don't change the values of the other
706 // fields, i.e. SetMinute() won't change either hour or seconds value
707
708 // set the year
709 wxDateTime& SetYear(int year);
710 // set the month
711 wxDateTime& SetMonth(Month month);
712 // set the day of the month
713 wxDateTime& SetDay(wxDateTime_t day);
714 // set hour
715 wxDateTime& SetHour(wxDateTime_t hour);
716 // set minute
717 wxDateTime& SetMinute(wxDateTime_t minute);
718 // set second
719 wxDateTime& SetSecond(wxDateTime_t second);
720 // set millisecond
721 wxDateTime& SetMillisecond(wxDateTime_t millisecond);
722
723 // assignment operator from time_t
724 wxDateTime& operator=(time_t timet) { return Set(timet); }
725
726 // assignment operator from broken down time/date
727 wxDateTime& operator=(const struct tm& tm) { return Set(tm); }
728
729 // assignment operator from broken down time/date
730 wxDateTime& operator=(const Tm& tm) { return Set(tm); }
731
732 // default assignment operator is ok
733
734 // calendar calculations (functions which set the date only leave the time
735 // unchanged, e.g. don't explictly zero it): SetXXX() functions modify the
736 // object itself, GetXXX() ones return a new object.
737 // ------------------------------------------------------------------------
738
739 // set to the given week day in the same week as this one
740 wxDateTime& SetToWeekDayInSameWeek(WeekDay weekday,
741 WeekFlags flags = Monday_First);
742 inline wxDateTime GetWeekDayInSameWeek(WeekDay weekday,
743 WeekFlags flags = Monday_First) const;
744
745 // set to the next week day following this one
746 wxDateTime& SetToNextWeekDay(WeekDay weekday);
747 inline wxDateTime GetNextWeekDay(WeekDay weekday) const;
748
749 // set to the previous week day before this one
750 wxDateTime& SetToPrevWeekDay(WeekDay weekday);
751 inline wxDateTime GetPrevWeekDay(WeekDay weekday) const;
752
753 // set to Nth occurrence of given weekday in the given month of the
754 // given year (time is set to 0), return true on success and false on
755 // failure. n may be positive (1..5) or negative to count from the end
756 // of the month (see helper function SetToLastWeekDay())
757 bool SetToWeekDay(WeekDay weekday,
758 int n = 1,
759 Month month = Inv_Month,
760 int year = Inv_Year);
761 inline wxDateTime GetWeekDay(WeekDay weekday,
762 int n = 1,
763 Month month = Inv_Month,
764 int year = Inv_Year) const;
765
766 // sets to the last weekday in the given month, year
767 inline bool SetToLastWeekDay(WeekDay weekday,
768 Month month = Inv_Month,
769 int year = Inv_Year);
770 inline wxDateTime GetLastWeekDay(WeekDay weekday,
771 Month month = Inv_Month,
772 int year = Inv_Year);
773
774 #if WXWIN_COMPATIBILITY_2_6
775 // sets the date to the given day of the given week in the year,
776 // returns true on success and false if given date doesn't exist (e.g.
777 // numWeek is > 53)
778 //
779 // these functions are badly defined as they're not the reverse of
780 // GetWeekOfYear(), use SetToTheWeekOfYear() instead
781 wxDEPRECATED( bool SetToTheWeek(wxDateTime_t numWeek,
782 WeekDay weekday = Mon,
783 WeekFlags flags = Monday_First) );
784 wxDEPRECATED( wxDateTime GetWeek(wxDateTime_t numWeek,
785 WeekDay weekday = Mon,
786 WeekFlags flags = Monday_First) const );
787 #endif // WXWIN_COMPATIBILITY_2_6
788
789 // returns the date corresponding to the given week day of the given
790 // week (in ISO notation) of the specified year
791 static wxDateTime SetToWeekOfYear(int year,
792 wxDateTime_t numWeek,
793 WeekDay weekday = Mon);
794
795 // sets the date to the last day of the given (or current) month or the
796 // given (or current) year
797 wxDateTime& SetToLastMonthDay(Month month = Inv_Month,
798 int year = Inv_Year);
799 inline wxDateTime GetLastMonthDay(Month month = Inv_Month,
800 int year = Inv_Year) const;
801
802 // sets to the given year day (1..365 or 366)
803 wxDateTime& SetToYearDay(wxDateTime_t yday);
804 inline wxDateTime GetYearDay(wxDateTime_t yday) const;
805
806 // The definitions below were taken verbatim from
807 //
808 // http://www.capecod.net/~pbaum/date/date0.htm
809 //
810 // (Peter Baum's home page)
811 //
812 // definition: The Julian Day Number, Julian Day, or JD of a
813 // particular instant of time is the number of days and fractions of a
814 // day since 12 hours Universal Time (Greenwich mean noon) on January
815 // 1 of the year -4712, where the year is given in the Julian
816 // proleptic calendar. The idea of using this reference date was
817 // originally proposed by Joseph Scalizer in 1582 to count years but
818 // it was modified by 19th century astronomers to count days. One
819 // could have equivalently defined the reference time to be noon of
820 // November 24, -4713 if were understood that Gregorian calendar rules
821 // were applied. Julian days are Julian Day Numbers and are not to be
822 // confused with Julian dates.
823 //
824 // definition: The Rata Die number is a date specified as the number
825 // of days relative to a base date of December 31 of the year 0. Thus
826 // January 1 of the year 1 is Rata Die day 1.
827
828 // get the Julian Day number (the fractional part specifies the time of
829 // the day, related to noon - beware of rounding errors!)
830 double GetJulianDayNumber() const;
831 double GetJDN() const { return GetJulianDayNumber(); }
832
833 // get the Modified Julian Day number: it is equal to JDN - 2400000.5
834 // and so integral MJDs correspond to the midnights (and not noons).
835 // MJD 0 is Nov 17, 1858
836 double GetModifiedJulianDayNumber() const { return GetJDN() - 2400000.5; }
837 double GetMJD() const { return GetModifiedJulianDayNumber(); }
838
839 // get the Rata Die number
840 double GetRataDie() const;
841
842 // TODO algorithms for calculating some important dates, such as
843 // religious holidays (Easter...) or moon/solar eclipses? Some
844 // algorithms can be found in the calendar FAQ
845
846
847 // Timezone stuff: a wxDateTime object constructed using given
848 // day/month/year/hour/min/sec values is interpreted as this moment in
849 // local time. Using the functions below, it may be converted to another
850 // time zone (e.g., the Unix epoch is wxDateTime(1, Jan, 1970).ToGMT()).
851 //
852 // These functions try to handle DST internally, but there is no magical
853 // way to know all rules for it in all countries in the world, so if the
854 // program can handle it itself (or doesn't want to handle it at all for
855 // whatever reason), the DST handling can be disabled with noDST.
856 // ------------------------------------------------------------------------
857
858 // transform to any given timezone
859 inline wxDateTime ToTimezone(const TimeZone& tz, bool noDST = false) const;
860 wxDateTime& MakeTimezone(const TimeZone& tz, bool noDST = false);
861
862 // interpret current value as being in another timezone and transform
863 // it to local one
864 inline wxDateTime FromTimezone(const TimeZone& tz, bool noDST = false) const;
865 wxDateTime& MakeFromTimezone(const TimeZone& tz, bool noDST = false);
866
867 // transform to/from GMT/UTC
868 wxDateTime ToUTC(bool noDST = false) const { return ToTimezone(UTC, noDST); }
869 wxDateTime& MakeUTC(bool noDST = false) { return MakeTimezone(UTC, noDST); }
870
871 wxDateTime ToGMT(bool noDST = false) const { return ToUTC(noDST); }
872 wxDateTime& MakeGMT(bool noDST = false) { return MakeUTC(noDST); }
873
874 wxDateTime FromUTC(bool noDST = false) const
875 { return FromTimezone(UTC, noDST); }
876 wxDateTime& MakeFromUTC(bool noDST = false)
877 { return MakeFromTimezone(UTC, noDST); }
878
879 // is daylight savings time in effect at this moment according to the
880 // rules of the specified country?
881 //
882 // Return value is > 0 if DST is in effect, 0 if it is not and -1 if
883 // the information is not available (this is compatible with ANSI C)
884 int IsDST(Country country = Country_Default) const;
885
886
887 // accessors: many of them take the timezone parameter which indicates the
888 // timezone for which to make the calculations and the default value means
889 // to do it for the current timezone of this machine (even if the function
890 // only operates with the date it's necessary because a date may wrap as
891 // result of timezone shift)
892 // ------------------------------------------------------------------------
893
894 // is the date valid?
895 inline bool IsValid() const { return m_time != wxInvalidDateTime.m_time; }
896
897 // get the broken down date/time representation in the given timezone
898 //
899 // If you wish to get several time components (day, month and year),
900 // consider getting the whole Tm strcuture first and retrieving the
901 // value from it - this is much more efficient
902 Tm GetTm(const TimeZone& tz = Local) const;
903
904 // get the number of seconds since the Unix epoch - returns (time_t)-1
905 // if the value is out of range
906 inline time_t GetTicks() const;
907
908 // get the century, same as GetCentury(GetYear())
909 int GetCentury(const TimeZone& tz = Local) const
910 { return GetCentury(GetYear(tz)); }
911 // get the year (returns Inv_Year if date is invalid)
912 int GetYear(const TimeZone& tz = Local) const
913 { return GetTm(tz).year; }
914 // get the month (Inv_Month if date is invalid)
915 Month GetMonth(const TimeZone& tz = Local) const
916 { return (Month)GetTm(tz).mon; }
917 // get the month day (in 1..31 range, 0 if date is invalid)
918 wxDateTime_t GetDay(const TimeZone& tz = Local) const
919 { return GetTm(tz).mday; }
920 // get the day of the week (Inv_WeekDay if date is invalid)
921 WeekDay GetWeekDay(const TimeZone& tz = Local) const
922 { return GetTm(tz).GetWeekDay(); }
923 // get the hour of the day
924 wxDateTime_t GetHour(const TimeZone& tz = Local) const
925 { return GetTm(tz).hour; }
926 // get the minute
927 wxDateTime_t GetMinute(const TimeZone& tz = Local) const
928 { return GetTm(tz).min; }
929 // get the second
930 wxDateTime_t GetSecond(const TimeZone& tz = Local) const
931 { return GetTm(tz).sec; }
932 // get milliseconds
933 wxDateTime_t GetMillisecond(const TimeZone& tz = Local) const
934 { return GetTm(tz).msec; }
935
936 // get the day since the year start (1..366, 0 if date is invalid)
937 wxDateTime_t GetDayOfYear(const TimeZone& tz = Local) const;
938 // get the week number since the year start (1..52 or 53, 0 if date is
939 // invalid)
940 wxDateTime_t GetWeekOfYear(WeekFlags flags = Monday_First,
941 const TimeZone& tz = Local) const;
942 // get the week number since the month start (1..5, 0 if date is
943 // invalid)
944 wxDateTime_t GetWeekOfMonth(WeekFlags flags = Monday_First,
945 const TimeZone& tz = Local) const;
946
947 // is this date a work day? This depends on a country, of course,
948 // because the holidays are different in different countries
949 bool IsWorkDay(Country country = Country_Default) const;
950
951 // is this date later than Gregorian calendar introduction for the
952 // given country (see enum GregorianAdoption)?
953 //
954 // NB: this function shouldn't be considered as absolute authority in
955 // the matter. Besides, for some countries the exact date of
956 // adoption of the Gregorian calendar is simply unknown.
957 bool IsGregorianDate(GregorianAdoption country = Gr_Standard) const;
958
959 // dos date and time format
960 // ------------------------------------------------------------------------
961
962 // set from the DOS packed format
963 wxDateTime& SetFromDOS(unsigned long ddt);
964
965 // pack the date in DOS format
966 unsigned long GetAsDOS() const;
967
968 // SYSTEMTIME format
969 // ------------------------------------------------------------------------
970 #ifdef __WXMSW__
971
972 // convert SYSTEMTIME to wxDateTime
973 wxDateTime& SetFromMSWSysTime(const struct _SYSTEMTIME&);
974
975 // convert wxDateTime to SYSTEMTIME
976 void GetAsMSWSysTime(struct _SYSTEMTIME*) const;
977 #endif // __WXMSW__
978
979 // comparison (see also functions below for operator versions)
980 // ------------------------------------------------------------------------
981
982 // returns true if the two moments are strictly identical
983 inline bool IsEqualTo(const wxDateTime& datetime) const;
984
985 // returns true if the date is strictly earlier than the given one
986 inline bool IsEarlierThan(const wxDateTime& datetime) const;
987
988 // returns true if the date is strictly later than the given one
989 inline bool IsLaterThan(const wxDateTime& datetime) const;
990
991 // returns true if the date is strictly in the given range
992 inline bool IsStrictlyBetween(const wxDateTime& t1,
993 const wxDateTime& t2) const;
994
995 // returns true if the date is in the given range
996 inline bool IsBetween(const wxDateTime& t1, const wxDateTime& t2) const;
997
998 // do these two objects refer to the same date?
999 inline bool IsSameDate(const wxDateTime& dt) const;
1000
1001 // do these two objects have the same time?
1002 inline bool IsSameTime(const wxDateTime& dt) const;
1003
1004 // are these two objects equal up to given timespan?
1005 inline bool IsEqualUpTo(const wxDateTime& dt, const wxTimeSpan& ts) const;
1006
1007 inline bool operator<(const wxDateTime& dt) const
1008 {
1009 wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") );
1010 return GetValue() < dt.GetValue();
1011 }
1012
1013 inline bool operator<=(const wxDateTime& dt) const
1014 {
1015 wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") );
1016 return GetValue() <= dt.GetValue();
1017 }
1018
1019 inline bool operator>(const wxDateTime& dt) const
1020 {
1021 wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") );
1022 return GetValue() > dt.GetValue();
1023 }
1024
1025 inline bool operator>=(const wxDateTime& dt) const
1026 {
1027 wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") );
1028 return GetValue() >= dt.GetValue();
1029 }
1030
1031 inline bool operator==(const wxDateTime& dt) const
1032 {
1033 wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") );
1034 return GetValue() == dt.GetValue();
1035 }
1036
1037 inline bool operator!=(const wxDateTime& dt) const
1038 {
1039 wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") );
1040 return GetValue() != dt.GetValue();
1041 }
1042
1043 // arithmetics with dates (see also below for more operators)
1044 // ------------------------------------------------------------------------
1045
1046 // return the sum of the date with a time span (positive or negative)
1047 inline wxDateTime Add(const wxTimeSpan& diff) const;
1048 // add a time span (positive or negative)
1049 inline wxDateTime& Add(const wxTimeSpan& diff);
1050 // add a time span (positive or negative)
1051 inline wxDateTime& operator+=(const wxTimeSpan& diff);
1052 inline wxDateTime operator+(const wxTimeSpan& ts) const
1053 {
1054 wxDateTime dt(*this);
1055 dt.Add(ts);
1056 return dt;
1057 }
1058
1059 // return the difference of the date with a time span
1060 inline wxDateTime Subtract(const wxTimeSpan& diff) const;
1061 // subtract a time span (positive or negative)
1062 inline wxDateTime& Subtract(const wxTimeSpan& diff);
1063 // subtract a time span (positive or negative)
1064 inline wxDateTime& operator-=(const wxTimeSpan& diff);
1065 inline wxDateTime operator-(const wxTimeSpan& ts) const
1066 {
1067 wxDateTime dt(*this);
1068 dt.Subtract(ts);
1069 return dt;
1070 }
1071
1072 // return the sum of the date with a date span
1073 inline wxDateTime Add(const wxDateSpan& diff) const;
1074 // add a date span (positive or negative)
1075 wxDateTime& Add(const wxDateSpan& diff);
1076 // add a date span (positive or negative)
1077 inline wxDateTime& operator+=(const wxDateSpan& diff);
1078 inline wxDateTime operator+(const wxDateSpan& ds) const
1079 {
1080 wxDateTime dt(*this);
1081 dt.Add(ds);
1082 return dt;
1083 }
1084
1085 // return the difference of the date with a date span
1086 inline wxDateTime Subtract(const wxDateSpan& diff) const;
1087 // subtract a date span (positive or negative)
1088 inline wxDateTime& Subtract(const wxDateSpan& diff);
1089 // subtract a date span (positive or negative)
1090 inline wxDateTime& operator-=(const wxDateSpan& diff);
1091 inline wxDateTime operator-(const wxDateSpan& ds) const
1092 {
1093 wxDateTime dt(*this);
1094 dt.Subtract(ds);
1095 return dt;
1096 }
1097
1098 // return the difference between two dates
1099 inline wxTimeSpan Subtract(const wxDateTime& dt) const;
1100 inline wxTimeSpan operator-(const wxDateTime& dt2) const;
1101
1102 // conversion to/from text
1103 // ------------------------------------------------------------------------
1104
1105 // all conversions functions return true to indicate whether parsing
1106 // succeeded or failed and fill in the provided end iterator, which must
1107 // not be NULL, with the location of the character where the parsing
1108 // stopped (this will be end() of the passed string if everything was
1109 // parsed)
1110
1111 // parse a string in RFC 822 format (found e.g. in mail headers and
1112 // having the form "Wed, 10 Feb 1999 19:07:07 +0100")
1113 bool ParseRfc822Date(const wxString& date,
1114 wxString::const_iterator *end);
1115
1116 // parse a date/time in the given format (see strptime(3)), fill in
1117 // the missing (in the string) fields with the values of dateDef (by
1118 // default, they will not change if they had valid values or will
1119 // default to Today() otherwise)
1120 bool ParseFormat(const wxString& date,
1121 const wxString& format,
1122 const wxDateTime& dateDef,
1123 wxString::const_iterator *end);
1124
1125 bool ParseFormat(const wxString& date,
1126 const wxString& format,
1127 wxString::const_iterator *end)
1128 {
1129 return ParseFormat(date, format, wxDefaultDateTime, end);
1130 }
1131
1132 bool ParseFormat(const wxString& date,
1133 wxString::const_iterator *end)
1134 {
1135 return ParseFormat(date, wxDefaultDateTimeFormat, wxDefaultDateTime, end);
1136 }
1137
1138 // parse a string containing date, time or both in ISO 8601 format
1139 //
1140 // notice that these functions are new in wx 3.0 and so we don't
1141 // provide compatibility overloads for them
1142 bool ParseISODate(const wxString& date)
1143 {
1144 wxString::const_iterator end;
1145 return ParseFormat(date, wxS("%Y-%m-%d"), &end) && end == date.end();
1146 }
1147
1148 bool ParseISOTime(const wxString& time)
1149 {
1150 wxString::const_iterator end;
1151 return ParseFormat(time, wxS("%H:%M:%S"), &end) && end == time.end();
1152 }
1153
1154 bool ParseISOCombined(const wxString& datetime, char sep = 'T')
1155 {
1156 wxString::const_iterator end;
1157 const wxString fmt = wxS("%Y-%m-%d") + wxString(sep) + wxS("%H:%M:%S");
1158 return ParseFormat(datetime, fmt, &end) && end == datetime.end();
1159 }
1160
1161 // parse a string containing the date/time in "free" format, this
1162 // function will try to make an educated guess at the string contents
1163 bool ParseDateTime(const wxString& datetime,
1164 wxString::const_iterator *end);
1165
1166 // parse a string containing the date only in "free" format (less
1167 // flexible than ParseDateTime)
1168 bool ParseDate(const wxString& date,
1169 wxString::const_iterator *end);
1170
1171 // parse a string containing the time only in "free" format
1172 bool ParseTime(const wxString& time,
1173 wxString::const_iterator *end);
1174
1175
1176 // this function accepts strftime()-like format string (default
1177 // argument corresponds to the preferred date and time representation
1178 // for the current locale) and returns the string containing the
1179 // resulting text representation
1180 wxString Format(const wxString& format = wxDefaultDateTimeFormat,
1181 const TimeZone& tz = Local) const;
1182 // preferred date representation for the current locale
1183 wxString FormatDate() const { return Format(wxS("%x")); }
1184 // preferred time representation for the current locale
1185 wxString FormatTime() const { return Format(wxS("%X")); }
1186 // returns the string representing the date in ISO 8601 format
1187 // (YYYY-MM-DD)
1188 wxString FormatISODate() const { return Format(wxS("%Y-%m-%d")); }
1189 // returns the string representing the time in ISO 8601 format
1190 // (HH:MM:SS)
1191 wxString FormatISOTime() const { return Format(wxS("%H:%M:%S")); }
1192 // return the combined date time representation in ISO 8601 format; the
1193 // separator character should be 'T' according to the standard but it
1194 // can also be useful to set it to ' '
1195 wxString FormatISOCombined(char sep = 'T') const
1196 { return FormatISODate() + sep + FormatISOTime(); }
1197
1198
1199 // backwards compatible versions of the parsing functions: they return an
1200 // object representing the next character following the date specification
1201 // (i.e. the one where the scan had to stop) or a special NULL-like object
1202 // on failure
1203 //
1204 // they're not deprecated because a lot of existing code uses them and
1205 // there is no particular harm in keeping them but you should still prefer
1206 // the versions above in the new code
1207 wxAnyStrPtr ParseRfc822Date(const wxString& date)
1208 {
1209 wxString::const_iterator end;
1210 return ParseRfc822Date(date, &end) ? wxAnyStrPtr(date, end)
1211 : wxAnyStrPtr();
1212 }
1213
1214 wxAnyStrPtr ParseFormat(const wxString& date,
1215 const wxString& format = wxDefaultDateTimeFormat,
1216 const wxDateTime& dateDef = wxDefaultDateTime)
1217 {
1218 wxString::const_iterator end;
1219 return ParseFormat(date, format, dateDef, &end) ? wxAnyStrPtr(date, end)
1220 : wxAnyStrPtr();
1221 }
1222
1223 wxAnyStrPtr ParseDateTime(const wxString& datetime)
1224 {
1225 wxString::const_iterator end;
1226 return ParseDateTime(datetime, &end) ? wxAnyStrPtr(datetime, end)
1227 : wxAnyStrPtr();
1228 }
1229
1230 wxAnyStrPtr ParseDate(const wxString& date)
1231 {
1232 wxString::const_iterator end;
1233 return ParseDate(date, &end) ? wxAnyStrPtr(date, end)
1234 : wxAnyStrPtr();
1235 }
1236
1237 wxAnyStrPtr ParseTime(const wxString& time)
1238 {
1239 wxString::const_iterator end;
1240 return ParseTime(time, &end) ? wxAnyStrPtr(time, end)
1241 : wxAnyStrPtr();
1242 }
1243
1244 // implementation
1245 // ------------------------------------------------------------------------
1246
1247 // construct from internal representation
1248 wxDateTime(const wxLongLong& time) { m_time = time; }
1249
1250 // get the internal representation
1251 inline wxLongLong GetValue() const;
1252
1253 // a helper function to get the current time_t
1254 static time_t GetTimeNow() { return time(NULL); }
1255
1256 // another one to get the current time broken down
1257 static struct tm *GetTmNow()
1258 {
1259 static struct tm l_CurrentTime;
1260 return GetTmNow(&l_CurrentTime);
1261 }
1262
1263 // get current time using thread-safe function
1264 static struct tm *GetTmNow(struct tm *tmstruct);
1265
1266 private:
1267 // the current country - as it's the same for all program objects (unless
1268 // it runs on a _really_ big cluster system :-), this is a static member:
1269 // see SetCountry() and GetCountry()
1270 static Country ms_country;
1271
1272 // this constant is used to transform a time_t value to the internal
1273 // representation, as time_t is in seconds and we use milliseconds it's
1274 // fixed to 1000
1275 static const long TIME_T_FACTOR;
1276
1277 // returns true if we fall in range in which we can use standard ANSI C
1278 // functions
1279 inline bool IsInStdRange() const;
1280
1281 // the internal representation of the time is the amount of milliseconds
1282 // elapsed since the origin which is set by convention to the UNIX/C epoch
1283 // value: the midnight of January 1, 1970 (UTC)
1284 wxLongLong m_time;
1285 };
1286
1287 // ----------------------------------------------------------------------------
1288 // This class contains a difference between 2 wxDateTime values, so it makes
1289 // sense to add it to wxDateTime and it is the result of subtraction of 2
1290 // objects of that class. See also wxDateSpan.
1291 // ----------------------------------------------------------------------------
1292
1293 class WXDLLIMPEXP_BASE wxTimeSpan
1294 {
1295 public:
1296 // constructors
1297 // ------------------------------------------------------------------------
1298
1299 // return the timespan for the given number of milliseconds
1300 static wxTimeSpan Milliseconds(wxLongLong ms) { return wxTimeSpan(0, 0, 0, ms); }
1301 static wxTimeSpan Millisecond() { return Milliseconds(1); }
1302
1303 // return the timespan for the given number of seconds
1304 static wxTimeSpan Seconds(wxLongLong sec) { return wxTimeSpan(0, 0, sec); }
1305 static wxTimeSpan Second() { return Seconds(1); }
1306
1307 // return the timespan for the given number of minutes
1308 static wxTimeSpan Minutes(long min) { return wxTimeSpan(0, min, 0 ); }
1309 static wxTimeSpan Minute() { return Minutes(1); }
1310
1311 // return the timespan for the given number of hours
1312 static wxTimeSpan Hours(long hours) { return wxTimeSpan(hours, 0, 0); }
1313 static wxTimeSpan Hour() { return Hours(1); }
1314
1315 // return the timespan for the given number of days
1316 static wxTimeSpan Days(long days) { return Hours(24 * days); }
1317 static wxTimeSpan Day() { return Days(1); }
1318
1319 // return the timespan for the given number of weeks
1320 static wxTimeSpan Weeks(long days) { return Days(7 * days); }
1321 static wxTimeSpan Week() { return Weeks(1); }
1322
1323 // default ctor constructs the 0 time span
1324 wxTimeSpan() { }
1325
1326 // from separate values for each component, date set to 0 (hours are
1327 // not restricted to 0..24 range, neither are minutes, seconds or
1328 // milliseconds)
1329 inline wxTimeSpan(long hours,
1330 long minutes = 0,
1331 wxLongLong seconds = 0,
1332 wxLongLong milliseconds = 0);
1333
1334 // default copy ctor is ok
1335
1336 // no dtor
1337
1338 // arithmetics with time spans (see also below for more operators)
1339 // ------------------------------------------------------------------------
1340
1341 // return the sum of two timespans
1342 inline wxTimeSpan Add(const wxTimeSpan& diff) const;
1343 // add two timespans together
1344 inline wxTimeSpan& Add(const wxTimeSpan& diff);
1345 // add two timespans together
1346 wxTimeSpan& operator+=(const wxTimeSpan& diff) { return Add(diff); }
1347 inline wxTimeSpan operator+(const wxTimeSpan& ts) const
1348 {
1349 return wxTimeSpan(GetValue() + ts.GetValue());
1350 }
1351
1352 // return the difference of two timespans
1353 inline wxTimeSpan Subtract(const wxTimeSpan& diff) const;
1354 // subtract another timespan
1355 inline wxTimeSpan& Subtract(const wxTimeSpan& diff);
1356 // subtract another timespan
1357 wxTimeSpan& operator-=(const wxTimeSpan& diff) { return Subtract(diff); }
1358 inline wxTimeSpan operator-(const wxTimeSpan& ts)
1359 {
1360 return wxTimeSpan(GetValue() - ts.GetValue());
1361 }
1362
1363 // multiply timespan by a scalar
1364 inline wxTimeSpan Multiply(int n) const;
1365 // multiply timespan by a scalar
1366 inline wxTimeSpan& Multiply(int n);
1367 // multiply timespan by a scalar
1368 wxTimeSpan& operator*=(int n) { return Multiply(n); }
1369 inline wxTimeSpan operator*(int n) const
1370 {
1371 return wxTimeSpan(*this).Multiply(n);
1372 }
1373
1374 // return this timespan with opposite sign
1375 wxTimeSpan Negate() const { return wxTimeSpan(-GetValue()); }
1376 // negate the value of the timespan
1377 wxTimeSpan& Neg() { m_diff = -GetValue(); return *this; }
1378 // negate the value of the timespan
1379 wxTimeSpan& operator-() { return Neg(); }
1380
1381 // return the absolute value of the timespan: does _not_ modify the
1382 // object
1383 inline wxTimeSpan Abs() const;
1384
1385 // there is intentionally no division because we don't want to
1386 // introduce rounding errors in time calculations
1387
1388 // comparaison (see also operator versions below)
1389 // ------------------------------------------------------------------------
1390
1391 // is the timespan null?
1392 bool IsNull() const { return m_diff == 0l; }
1393 // returns true if the timespan is null
1394 bool operator!() const { return !IsNull(); }
1395
1396 // is the timespan positive?
1397 bool IsPositive() const { return m_diff > 0l; }
1398
1399 // is the timespan negative?
1400 bool IsNegative() const { return m_diff < 0l; }
1401
1402 // are two timespans equal?
1403 inline bool IsEqualTo(const wxTimeSpan& ts) const;
1404 // compare two timestamps: works with the absolute values, i.e. -2
1405 // hours is longer than 1 hour. Also, it will return false if the
1406 // timespans are equal in absolute value.
1407 inline bool IsLongerThan(const wxTimeSpan& ts) const;
1408 // compare two timestamps: works with the absolute values, i.e. 1
1409 // hour is shorter than -2 hours. Also, it will return false if the
1410 // timespans are equal in absolute value.
1411 bool IsShorterThan(const wxTimeSpan& t) const;
1412
1413 inline bool operator<(const wxTimeSpan &ts) const
1414 {
1415 return GetValue() < ts.GetValue();
1416 }
1417
1418 inline bool operator<=(const wxTimeSpan &ts) const
1419 {
1420 return GetValue() <= ts.GetValue();
1421 }
1422
1423 inline bool operator>(const wxTimeSpan &ts) const
1424 {
1425 return GetValue() > ts.GetValue();
1426 }
1427
1428 inline bool operator>=(const wxTimeSpan &ts) const
1429 {
1430 return GetValue() >= ts.GetValue();
1431 }
1432
1433 inline bool operator==(const wxTimeSpan &ts) const
1434 {
1435 return GetValue() == ts.GetValue();
1436 }
1437
1438 inline bool operator!=(const wxTimeSpan &ts) const
1439 {
1440 return GetValue() != ts.GetValue();
1441 }
1442
1443 // breaking into days, hours, minutes and seconds
1444 // ------------------------------------------------------------------------
1445
1446 // get the max number of weeks in this timespan
1447 inline int GetWeeks() const;
1448 // get the max number of days in this timespan
1449 inline int GetDays() const;
1450 // get the max number of hours in this timespan
1451 inline int GetHours() const;
1452 // get the max number of minutes in this timespan
1453 inline int GetMinutes() const;
1454 // get the max number of seconds in this timespan
1455 inline wxLongLong GetSeconds() const;
1456 // get the number of milliseconds in this timespan
1457 wxLongLong GetMilliseconds() const { return m_diff; }
1458
1459 // conversion to text
1460 // ------------------------------------------------------------------------
1461
1462 // this function accepts strftime()-like format string (default
1463 // argument corresponds to the preferred date and time representation
1464 // for the current locale) and returns the string containing the
1465 // resulting text representation. Notice that only some of format
1466 // specifiers valid for wxDateTime are valid for wxTimeSpan: hours,
1467 // minutes and seconds make sense, but not "PM/AM" string for example.
1468 wxString Format(const wxString& format = wxDefaultTimeSpanFormat) const;
1469
1470 // implementation
1471 // ------------------------------------------------------------------------
1472
1473 // construct from internal representation
1474 wxTimeSpan(const wxLongLong& diff) { m_diff = diff; }
1475
1476 // get the internal representation
1477 wxLongLong GetValue() const { return m_diff; }
1478
1479 private:
1480 // the (signed) time span in milliseconds
1481 wxLongLong m_diff;
1482 };
1483
1484 // ----------------------------------------------------------------------------
1485 // This class is a "logical time span" and is useful for implementing program
1486 // logic for such things as "add one month to the date" which, in general,
1487 // doesn't mean to add 60*60*24*31 seconds to it, but to take the same date
1488 // the next month (to understand that this is indeed different consider adding
1489 // one month to Feb, 15 - we want to get Mar, 15, of course).
1490 //
1491 // When adding a month to the date, all lesser components (days, hours, ...)
1492 // won't be changed unless the resulting date would be invalid: for example,
1493 // Jan 31 + 1 month will be Feb 28, not (non existing) Feb 31.
1494 //
1495 // Because of this feature, adding and subtracting back again the same
1496 // wxDateSpan will *not*, in general give back the original date: Feb 28 - 1
1497 // month will be Jan 28, not Jan 31!
1498 //
1499 // wxDateSpan can be either positive or negative. They may be
1500 // multiplied by scalars which multiply all deltas by the scalar: i.e. 2*(1
1501 // month and 1 day) is 2 months and 2 days. They can be added together and
1502 // with wxDateTime or wxTimeSpan, but the type of result is different for each
1503 // case.
1504 //
1505 // Beware about weeks: if you specify both weeks and days, the total number of
1506 // days added will be 7*weeks + days! See also GetTotalDays() function.
1507 //
1508 // Equality operators are defined for wxDateSpans. Two datespans are equal if
1509 // they both give the same target date when added to *every* source date.
1510 // Thus wxDateSpan::Months(1) is not equal to wxDateSpan::Days(30), because
1511 // they not give the same date when added to 1 Feb. But wxDateSpan::Days(14) is
1512 // equal to wxDateSpan::Weeks(2)
1513 //
1514 // Finally, notice that for adding hours, minutes &c you don't need this
1515 // class: wxTimeSpan will do the job because there are no subtleties
1516 // associated with those.
1517 // ----------------------------------------------------------------------------
1518
1519 class WXDLLIMPEXP_BASE wxDateSpan
1520 {
1521 public:
1522 // constructors
1523 // ------------------------------------------------------------------------
1524
1525 // this many years/months/weeks/days
1526 wxDateSpan(int years = 0, int months = 0, int weeks = 0, int days = 0)
1527 {
1528 m_years = years;
1529 m_months = months;
1530 m_weeks = weeks;
1531 m_days = days;
1532 }
1533
1534 // get an object for the given number of days
1535 static wxDateSpan Days(int days) { return wxDateSpan(0, 0, 0, days); }
1536 static wxDateSpan Day() { return Days(1); }
1537
1538 // get an object for the given number of weeks
1539 static wxDateSpan Weeks(int weeks) { return wxDateSpan(0, 0, weeks, 0); }
1540 static wxDateSpan Week() { return Weeks(1); }
1541
1542 // get an object for the given number of months
1543 static wxDateSpan Months(int mon) { return wxDateSpan(0, mon, 0, 0); }
1544 static wxDateSpan Month() { return Months(1); }
1545
1546 // get an object for the given number of years
1547 static wxDateSpan Years(int years) { return wxDateSpan(years, 0, 0, 0); }
1548 static wxDateSpan Year() { return Years(1); }
1549
1550 // default copy ctor is ok
1551
1552 // no dtor
1553
1554 // accessors (all SetXXX() return the (modified) wxDateSpan object)
1555 // ------------------------------------------------------------------------
1556
1557 // set number of years
1558 wxDateSpan& SetYears(int n) { m_years = n; return *this; }
1559 // set number of months
1560 wxDateSpan& SetMonths(int n) { m_months = n; return *this; }
1561 // set number of weeks
1562 wxDateSpan& SetWeeks(int n) { m_weeks = n; return *this; }
1563 // set number of days
1564 wxDateSpan& SetDays(int n) { m_days = n; return *this; }
1565
1566 // get number of years
1567 int GetYears() const { return m_years; }
1568 // get number of months
1569 int GetMonths() const { return m_months; }
1570 // get number of weeks
1571 int GetWeeks() const { return m_weeks; }
1572 // get number of days
1573 int GetDays() const { return m_days; }
1574 // returns 7*GetWeeks() + GetDays()
1575 int GetTotalDays() const { return 7*m_weeks + m_days; }
1576
1577 // arithmetics with date spans (see also below for more operators)
1578 // ------------------------------------------------------------------------
1579
1580 // return sum of two date spans
1581 inline wxDateSpan Add(const wxDateSpan& other) const;
1582 // add another wxDateSpan to us
1583 inline wxDateSpan& Add(const wxDateSpan& other);
1584 // add another wxDateSpan to us
1585 inline wxDateSpan& operator+=(const wxDateSpan& other);
1586 inline wxDateSpan operator+(const wxDateSpan& ds) const
1587 {
1588 return wxDateSpan(GetYears() + ds.GetYears(),
1589 GetMonths() + ds.GetMonths(),
1590 GetWeeks() + ds.GetWeeks(),
1591 GetDays() + ds.GetDays());
1592 }
1593
1594 // return difference of two date spans
1595 inline wxDateSpan Subtract(const wxDateSpan& other) const;
1596 // subtract another wxDateSpan from us
1597 inline wxDateSpan& Subtract(const wxDateSpan& other);
1598 // subtract another wxDateSpan from us
1599 inline wxDateSpan& operator-=(const wxDateSpan& other);
1600 inline wxDateSpan operator-(const wxDateSpan& ds) const
1601 {
1602 return wxDateSpan(GetYears() - ds.GetYears(),
1603 GetMonths() - ds.GetMonths(),
1604 GetWeeks() - ds.GetWeeks(),
1605 GetDays() - ds.GetDays());
1606 }
1607
1608 // return a copy of this time span with changed sign
1609 inline wxDateSpan Negate() const;
1610 // inverse the sign of this timespan
1611 inline wxDateSpan& Neg();
1612 // inverse the sign of this timespan
1613 wxDateSpan& operator-() { return Neg(); }
1614
1615 // return the date span proportional to this one with given factor
1616 inline wxDateSpan Multiply(int factor) const;
1617 // multiply all components by a (signed) number
1618 inline wxDateSpan& Multiply(int factor);
1619 // multiply all components by a (signed) number
1620 inline wxDateSpan& operator*=(int factor) { return Multiply(factor); }
1621 inline wxDateSpan operator*(int n) const
1622 {
1623 return wxDateSpan(*this).Multiply(n);
1624 }
1625
1626 // ds1 == d2 if and only if for every wxDateTime t t + ds1 == t + ds2
1627 inline bool operator==(const wxDateSpan& ds) const
1628 {
1629 return GetYears() == ds.GetYears() &&
1630 GetMonths() == ds.GetMonths() &&
1631 GetTotalDays() == ds.GetTotalDays();
1632 }
1633
1634 inline bool operator!=(const wxDateSpan& ds) const
1635 {
1636 return !(*this == ds);
1637 }
1638
1639 private:
1640 int m_years,
1641 m_months,
1642 m_weeks,
1643 m_days;
1644 };
1645
1646 // ----------------------------------------------------------------------------
1647 // wxDateTimeArray: array of dates.
1648 // ----------------------------------------------------------------------------
1649
1650 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxDateTime, wxDateTimeArray, WXDLLIMPEXP_BASE);
1651
1652 // ----------------------------------------------------------------------------
1653 // wxDateTimeHolidayAuthority: an object of this class will decide whether a
1654 // given date is a holiday and is used by all functions working with "work
1655 // days".
1656 //
1657 // NB: the base class is an ABC, derived classes must implement the pure
1658 // virtual methods to work with the holidays they correspond to.
1659 // ----------------------------------------------------------------------------
1660
1661 class WXDLLIMPEXP_FWD_BASE wxDateTimeHolidayAuthority;
1662 WX_DEFINE_USER_EXPORTED_ARRAY_PTR(wxDateTimeHolidayAuthority *,
1663 wxHolidayAuthoritiesArray,
1664 class WXDLLIMPEXP_BASE);
1665
1666 class wxDateTimeHolidaysModule;
1667 class WXDLLIMPEXP_BASE wxDateTimeHolidayAuthority
1668 {
1669 friend class wxDateTimeHolidaysModule;
1670 public:
1671 // returns true if the given date is a holiday
1672 static bool IsHoliday(const wxDateTime& dt);
1673
1674 // fills the provided array with all holidays in the given range, returns
1675 // the number of them
1676 static size_t GetHolidaysInRange(const wxDateTime& dtStart,
1677 const wxDateTime& dtEnd,
1678 wxDateTimeArray& holidays);
1679
1680 // clear the list of holiday authorities
1681 static void ClearAllAuthorities();
1682
1683 // add a new holiday authority (the pointer will be deleted by
1684 // wxDateTimeHolidayAuthority)
1685 static void AddAuthority(wxDateTimeHolidayAuthority *auth);
1686
1687 // the base class must have a virtual dtor
1688 virtual ~wxDateTimeHolidayAuthority();
1689
1690 protected:
1691 // this function is called to determine whether a given day is a holiday
1692 virtual bool DoIsHoliday(const wxDateTime& dt) const = 0;
1693
1694 // this function should fill the array with all holidays between the two
1695 // given dates - it is implemented in the base class, but in a very
1696 // inefficient way (it just iterates over all days and uses IsHoliday() for
1697 // each of them), so it must be overridden in the derived class where the
1698 // base class version may be explicitly used if needed
1699 //
1700 // returns the number of holidays in the given range and fills holidays
1701 // array
1702 virtual size_t DoGetHolidaysInRange(const wxDateTime& dtStart,
1703 const wxDateTime& dtEnd,
1704 wxDateTimeArray& holidays) const = 0;
1705
1706 private:
1707 // all holiday authorities
1708 static wxHolidayAuthoritiesArray ms_authorities;
1709 };
1710
1711 // the holidays for this class are all Saturdays and Sundays
1712 class WXDLLIMPEXP_BASE wxDateTimeWorkDays : public wxDateTimeHolidayAuthority
1713 {
1714 protected:
1715 virtual bool DoIsHoliday(const wxDateTime& dt) const;
1716 virtual size_t DoGetHolidaysInRange(const wxDateTime& dtStart,
1717 const wxDateTime& dtEnd,
1718 wxDateTimeArray& holidays) const;
1719 };
1720
1721 // ============================================================================
1722 // inline functions implementation
1723 // ============================================================================
1724
1725 // ----------------------------------------------------------------------------
1726 // private macros
1727 // ----------------------------------------------------------------------------
1728
1729 #define MILLISECONDS_PER_DAY 86400000l
1730
1731 // some broken compilers (HP-UX CC) refuse to compile the "normal" version, but
1732 // using a temp variable always might prevent other compilers from optimising
1733 // it away - hence use of this ugly macro
1734 #ifndef __HPUX__
1735 #define MODIFY_AND_RETURN(op) return wxDateTime(*this).op
1736 #else
1737 #define MODIFY_AND_RETURN(op) wxDateTime dt(*this); dt.op; return dt
1738 #endif
1739
1740 // ----------------------------------------------------------------------------
1741 // wxDateTime construction
1742 // ----------------------------------------------------------------------------
1743
1744 inline bool wxDateTime::IsInStdRange() const
1745 {
1746 return m_time >= 0l && (m_time / TIME_T_FACTOR) < LONG_MAX;
1747 }
1748
1749 /* static */
1750 inline wxDateTime wxDateTime::Now()
1751 {
1752 struct tm tmstruct;
1753 return wxDateTime(*GetTmNow(&tmstruct));
1754 }
1755
1756 /* static */
1757 inline wxDateTime wxDateTime::Today()
1758 {
1759 wxDateTime dt(Now());
1760 dt.ResetTime();
1761
1762 return dt;
1763 }
1764
1765 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
1766 inline wxDateTime& wxDateTime::Set(time_t timet)
1767 {
1768 // assign first to avoid long multiplication overflow!
1769 m_time = timet - WX_TIME_BASE_OFFSET ;
1770 m_time *= TIME_T_FACTOR;
1771
1772 return *this;
1773 }
1774 #endif
1775
1776 inline wxDateTime& wxDateTime::SetToCurrent()
1777 {
1778 *this = Now();
1779 return *this;
1780 }
1781
1782 #if (!(defined(__VISAGECPP__) && __IBMCPP__ >= 400))
1783 inline wxDateTime::wxDateTime(time_t timet)
1784 {
1785 Set(timet);
1786 }
1787 #endif
1788
1789 inline wxDateTime::wxDateTime(const struct tm& tm)
1790 {
1791 Set(tm);
1792 }
1793
1794 inline wxDateTime::wxDateTime(const Tm& tm)
1795 {
1796 Set(tm);
1797 }
1798
1799 inline wxDateTime::wxDateTime(double jdn)
1800 {
1801 Set(jdn);
1802 }
1803
1804 inline wxDateTime& wxDateTime::Set(const Tm& tm)
1805 {
1806 wxASSERT_MSG( tm.IsValid(), _T("invalid broken down date/time") );
1807
1808 return Set(tm.mday, (Month)tm.mon, tm.year,
1809 tm.hour, tm.min, tm.sec, tm.msec);
1810 }
1811
1812 inline wxDateTime::wxDateTime(wxDateTime_t hour,
1813 wxDateTime_t minute,
1814 wxDateTime_t second,
1815 wxDateTime_t millisec)
1816 {
1817 Set(hour, minute, second, millisec);
1818 }
1819
1820 inline wxDateTime::wxDateTime(wxDateTime_t day,
1821 Month month,
1822 int year,
1823 wxDateTime_t hour,
1824 wxDateTime_t minute,
1825 wxDateTime_t second,
1826 wxDateTime_t millisec)
1827 {
1828 Set(day, month, year, hour, minute, second, millisec);
1829 }
1830
1831 // ----------------------------------------------------------------------------
1832 // wxDateTime accessors
1833 // ----------------------------------------------------------------------------
1834
1835 inline wxLongLong wxDateTime::GetValue() const
1836 {
1837 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1838
1839 return m_time;
1840 }
1841
1842 inline time_t wxDateTime::GetTicks() const
1843 {
1844 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1845 if ( !IsInStdRange() )
1846 {
1847 return (time_t)-1;
1848 }
1849
1850 return (time_t)((m_time / (long)TIME_T_FACTOR).ToLong()) + WX_TIME_BASE_OFFSET;
1851 }
1852
1853 inline bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
1854 Month month,
1855 int year)
1856 {
1857 return SetToWeekDay(weekday, -1, month, year);
1858 }
1859
1860 inline wxDateTime
1861 wxDateTime::GetWeekDayInSameWeek(WeekDay weekday,
1862 WeekFlags WXUNUSED(flags)) const
1863 {
1864 MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday) );
1865 }
1866
1867 inline wxDateTime wxDateTime::GetNextWeekDay(WeekDay weekday) const
1868 {
1869 MODIFY_AND_RETURN( SetToNextWeekDay(weekday) );
1870 }
1871
1872 inline wxDateTime wxDateTime::GetPrevWeekDay(WeekDay weekday) const
1873 {
1874 MODIFY_AND_RETURN( SetToPrevWeekDay(weekday) );
1875 }
1876
1877 inline wxDateTime wxDateTime::GetWeekDay(WeekDay weekday,
1878 int n,
1879 Month month,
1880 int year) const
1881 {
1882 wxDateTime dt(*this);
1883
1884 return dt.SetToWeekDay(weekday, n, month, year) ? dt : wxInvalidDateTime;
1885 }
1886
1887 inline wxDateTime wxDateTime::GetLastWeekDay(WeekDay weekday,
1888 Month month,
1889 int year)
1890 {
1891 wxDateTime dt(*this);
1892
1893 return dt.SetToLastWeekDay(weekday, month, year) ? dt : wxInvalidDateTime;
1894 }
1895
1896 inline wxDateTime wxDateTime::GetLastMonthDay(Month month, int year) const
1897 {
1898 MODIFY_AND_RETURN( SetToLastMonthDay(month, year) );
1899 }
1900
1901 inline wxDateTime wxDateTime::GetYearDay(wxDateTime_t yday) const
1902 {
1903 MODIFY_AND_RETURN( SetToYearDay(yday) );
1904 }
1905
1906 // ----------------------------------------------------------------------------
1907 // wxDateTime comparison
1908 // ----------------------------------------------------------------------------
1909
1910 inline bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
1911 {
1912 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
1913
1914 return m_time == datetime.m_time;
1915 }
1916
1917 inline bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
1918 {
1919 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
1920
1921 return m_time < datetime.m_time;
1922 }
1923
1924 inline bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
1925 {
1926 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
1927
1928 return m_time > datetime.m_time;
1929 }
1930
1931 inline bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1,
1932 const wxDateTime& t2) const
1933 {
1934 // no need for assert, will be checked by the functions we call
1935 return IsLaterThan(t1) && IsEarlierThan(t2);
1936 }
1937
1938 inline bool wxDateTime::IsBetween(const wxDateTime& t1,
1939 const wxDateTime& t2) const
1940 {
1941 // no need for assert, will be checked by the functions we call
1942 return IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2);
1943 }
1944
1945 inline bool wxDateTime::IsSameDate(const wxDateTime& dt) const
1946 {
1947 Tm tm1 = GetTm(),
1948 tm2 = dt.GetTm();
1949
1950 return tm1.year == tm2.year &&
1951 tm1.mon == tm2.mon &&
1952 tm1.mday == tm2.mday;
1953 }
1954
1955 inline bool wxDateTime::IsSameTime(const wxDateTime& dt) const
1956 {
1957 // notice that we can't do something like this:
1958 //
1959 // m_time % MILLISECONDS_PER_DAY == dt.m_time % MILLISECONDS_PER_DAY
1960 //
1961 // because we have also to deal with (possibly) different DST settings!
1962 Tm tm1 = GetTm(),
1963 tm2 = dt.GetTm();
1964
1965 return tm1.hour == tm2.hour &&
1966 tm1.min == tm2.min &&
1967 tm1.sec == tm2.sec &&
1968 tm1.msec == tm2.msec;
1969 }
1970
1971 inline bool wxDateTime::IsEqualUpTo(const wxDateTime& dt,
1972 const wxTimeSpan& ts) const
1973 {
1974 return IsBetween(dt.Subtract(ts), dt.Add(ts));
1975 }
1976
1977 // ----------------------------------------------------------------------------
1978 // wxDateTime arithmetics
1979 // ----------------------------------------------------------------------------
1980
1981 inline wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const
1982 {
1983 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1984
1985 return wxDateTime(m_time + diff.GetValue());
1986 }
1987
1988 inline wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
1989 {
1990 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
1991
1992 m_time += diff.GetValue();
1993
1994 return *this;
1995 }
1996
1997 inline wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
1998 {
1999 return Add(diff);
2000 }
2001
2002 inline wxDateTime wxDateTime::Subtract(const wxTimeSpan& diff) const
2003 {
2004 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
2005
2006 return wxDateTime(m_time - diff.GetValue());
2007 }
2008
2009 inline wxDateTime& wxDateTime::Subtract(const wxTimeSpan& diff)
2010 {
2011 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
2012
2013 m_time -= diff.GetValue();
2014
2015 return *this;
2016 }
2017
2018 inline wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
2019 {
2020 return Subtract(diff);
2021 }
2022
2023 inline wxTimeSpan wxDateTime::Subtract(const wxDateTime& datetime) const
2024 {
2025 wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
2026
2027 return wxTimeSpan(GetValue() - datetime.GetValue());
2028 }
2029
2030 inline wxTimeSpan wxDateTime::operator-(const wxDateTime& dt2) const
2031 {
2032 return this->Subtract(dt2);
2033 }
2034
2035 inline wxDateTime wxDateTime::Add(const wxDateSpan& diff) const
2036 {
2037 return wxDateTime(*this).Add(diff);
2038 }
2039
2040 inline wxDateTime& wxDateTime::Subtract(const wxDateSpan& diff)
2041 {
2042 return Add(diff.Negate());
2043 }
2044
2045 inline wxDateTime wxDateTime::Subtract(const wxDateSpan& diff) const
2046 {
2047 return wxDateTime(*this).Subtract(diff);
2048 }
2049
2050 inline wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
2051 {
2052 return Subtract(diff);
2053 }
2054
2055 inline wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
2056 {
2057 return Add(diff);
2058 }
2059
2060 // ----------------------------------------------------------------------------
2061 // wxDateTime and timezones
2062 // ----------------------------------------------------------------------------
2063
2064 inline wxDateTime
2065 wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz, bool noDST) const
2066 {
2067 MODIFY_AND_RETURN( MakeTimezone(tz, noDST) );
2068 }
2069
2070 inline wxDateTime
2071 wxDateTime::FromTimezone(const wxDateTime::TimeZone& tz, bool noDST) const
2072 {
2073 MODIFY_AND_RETURN( MakeFromTimezone(tz, noDST) );
2074 }
2075
2076 // ----------------------------------------------------------------------------
2077 // wxTimeSpan construction
2078 // ----------------------------------------------------------------------------
2079
2080 inline wxTimeSpan::wxTimeSpan(long hours,
2081 long minutes,
2082 wxLongLong seconds,
2083 wxLongLong milliseconds)
2084 {
2085 // assign first to avoid precision loss
2086 m_diff = hours;
2087 m_diff *= 60l;
2088 m_diff += minutes;
2089 m_diff *= 60l;
2090 m_diff += seconds;
2091 m_diff *= 1000l;
2092 m_diff += milliseconds;
2093 }
2094
2095 // ----------------------------------------------------------------------------
2096 // wxTimeSpan accessors
2097 // ----------------------------------------------------------------------------
2098
2099 inline wxLongLong wxTimeSpan::GetSeconds() const
2100 {
2101 return m_diff / 1000l;
2102 }
2103
2104 inline int wxTimeSpan::GetMinutes() const
2105 {
2106 // explicit cast to int suppresses a warning with CodeWarrior and possibly
2107 // others (changing the return type to long from int is impossible in 2.8)
2108 return (int)((GetSeconds() / 60l).GetLo());
2109 }
2110
2111 inline int wxTimeSpan::GetHours() const
2112 {
2113 return GetMinutes() / 60;
2114 }
2115
2116 inline int wxTimeSpan::GetDays() const
2117 {
2118 return GetHours() / 24;
2119 }
2120
2121 inline int wxTimeSpan::GetWeeks() const
2122 {
2123 return GetDays() / 7;
2124 }
2125
2126 // ----------------------------------------------------------------------------
2127 // wxTimeSpan arithmetics
2128 // ----------------------------------------------------------------------------
2129
2130 inline wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const
2131 {
2132 return wxTimeSpan(m_diff + diff.GetValue());
2133 }
2134
2135 inline wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
2136 {
2137 m_diff += diff.GetValue();
2138
2139 return *this;
2140 }
2141
2142 inline wxTimeSpan wxTimeSpan::Subtract(const wxTimeSpan& diff) const
2143 {
2144 return wxTimeSpan(m_diff - diff.GetValue());
2145 }
2146
2147 inline wxTimeSpan& wxTimeSpan::Subtract(const wxTimeSpan& diff)
2148 {
2149 m_diff -= diff.GetValue();
2150
2151 return *this;
2152 }
2153
2154 inline wxTimeSpan& wxTimeSpan::Multiply(int n)
2155 {
2156 m_diff *= (long)n;
2157
2158 return *this;
2159 }
2160
2161 inline wxTimeSpan wxTimeSpan::Multiply(int n) const
2162 {
2163 return wxTimeSpan(m_diff * (long)n);
2164 }
2165
2166 inline wxTimeSpan wxTimeSpan::Abs() const
2167 {
2168 return wxTimeSpan(GetValue().Abs());
2169 }
2170
2171 inline bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
2172 {
2173 return GetValue() == ts.GetValue();
2174 }
2175
2176 inline bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
2177 {
2178 return GetValue().Abs() > ts.GetValue().Abs();
2179 }
2180
2181 inline bool wxTimeSpan::IsShorterThan(const wxTimeSpan& ts) const
2182 {
2183 return GetValue().Abs() < ts.GetValue().Abs();
2184 }
2185
2186 // ----------------------------------------------------------------------------
2187 // wxDateSpan
2188 // ----------------------------------------------------------------------------
2189
2190 inline wxDateSpan& wxDateSpan::operator+=(const wxDateSpan& other)
2191 {
2192 m_years += other.m_years;
2193 m_months += other.m_months;
2194 m_weeks += other.m_weeks;
2195 m_days += other.m_days;
2196
2197 return *this;
2198 }
2199
2200 inline wxDateSpan& wxDateSpan::Add(const wxDateSpan& other)
2201 {
2202 return *this += other;
2203 }
2204
2205 inline wxDateSpan wxDateSpan::Add(const wxDateSpan& other) const
2206 {
2207 wxDateSpan ds(*this);
2208 ds.Add(other);
2209 return ds;
2210 }
2211
2212 inline wxDateSpan& wxDateSpan::Multiply(int factor)
2213 {
2214 m_years *= factor;
2215 m_months *= factor;
2216 m_weeks *= factor;
2217 m_days *= factor;
2218
2219 return *this;
2220 }
2221
2222 inline wxDateSpan wxDateSpan::Multiply(int factor) const
2223 {
2224 wxDateSpan ds(*this);
2225 ds.Multiply(factor);
2226 return ds;
2227 }
2228
2229 inline wxDateSpan wxDateSpan::Negate() const
2230 {
2231 return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
2232 }
2233
2234 inline wxDateSpan& wxDateSpan::Neg()
2235 {
2236 m_years = -m_years;
2237 m_months = -m_months;
2238 m_weeks = -m_weeks;
2239 m_days = -m_days;
2240
2241 return *this;
2242 }
2243
2244 inline wxDateSpan& wxDateSpan::operator-=(const wxDateSpan& other)
2245 {
2246 return *this += other.Negate();
2247 }
2248
2249 inline wxDateSpan& wxDateSpan::Subtract(const wxDateSpan& other)
2250 {
2251 return *this -= other;
2252 }
2253
2254 inline wxDateSpan wxDateSpan::Subtract(const wxDateSpan& other) const
2255 {
2256 wxDateSpan ds(*this);
2257 ds.Subtract(other);
2258 return ds;
2259 }
2260
2261 #undef MILLISECONDS_PER_DAY
2262
2263 #undef MODIFY_AND_RETURN
2264
2265 // ============================================================================
2266 // binary operators
2267 // ============================================================================
2268
2269 // ----------------------------------------------------------------------------
2270 // wxTimeSpan operators
2271 // ----------------------------------------------------------------------------
2272
2273 wxTimeSpan WXDLLIMPEXP_BASE operator*(int n, const wxTimeSpan& ts);
2274
2275 // ----------------------------------------------------------------------------
2276 // wxDateSpan
2277 // ----------------------------------------------------------------------------
2278
2279 wxDateSpan WXDLLIMPEXP_BASE operator*(int n, const wxDateSpan& ds);
2280
2281 // ============================================================================
2282 // other helper functions
2283 // ============================================================================
2284
2285 // ----------------------------------------------------------------------------
2286 // iteration helpers: can be used to write a for loop over enum variable like
2287 // this:
2288 // for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) )
2289 // ----------------------------------------------------------------------------
2290
2291 WXDLLIMPEXP_BASE void wxNextMonth(wxDateTime::Month& m);
2292 WXDLLIMPEXP_BASE void wxPrevMonth(wxDateTime::Month& m);
2293 WXDLLIMPEXP_BASE void wxNextWDay(wxDateTime::WeekDay& wd);
2294 WXDLLIMPEXP_BASE void wxPrevWDay(wxDateTime::WeekDay& wd);
2295
2296 #endif // wxUSE_DATETIME
2297
2298 #endif // _WX_DATETIME_H