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