1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of time/date related classes
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // parts of code taken from sndcal library by Scott E. Lee:
11 // Copyright 1993-1995, Scott E. Lee, all rights reserved.
12 // Permission granted to use, copy, modify, distribute and sell
13 // so long as the above copyright and this permission statement
14 // are retained in all copies.
16 // Licence: wxWindows license
17 ///////////////////////////////////////////////////////////////////////////////
20 * Implementation notes:
22 * 1. the time is stored as a 64bit integer containing the signed number of
23 * milliseconds since Jan 1. 1970 (the Unix Epoch)
25 * 2. the range is thus something about 580 million years, but due to current
26 * algorithms limitations, only dates from Nov 24, 4714BC are handled
28 * 3. standard ANSI C functions are used to do time calculations whenever
29 * possible, i.e. when the date is in the range Jan 1, 1970 to 2038
31 * 4. otherwise, the calculations are done by converting the date to/from JDN
32 * first (the range limitation mentioned above comes from here: the
33 * algorithm used by Scott E. Lee's code only works for positive JDNs, more
38 // ============================================================================
40 // ============================================================================
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
47 #pragma implementation "datetime.h"
50 // For compilers that support precompilation, includes "wx.h".
51 #include "wx/wxprec.h"
58 #include "wx/string.h"
63 #include "wx/thread.h"
65 #define wxDEFINE_TIME_CONSTANTS
67 #include "wx/datetime.h"
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
74 static const int MONTHS_IN_YEAR
= 12;
76 static const int SECONDS_IN_MINUTE
= 60;
78 static const long SECONDS_PER_DAY
= 86400l;
80 static const long MILLISECONDS_PER_DAY
= 86400000l;
82 // this is the integral part of JDN of the midnight of Jan 1, 1970
83 // (i.e. JDN(Jan 1, 1970) = 2440587.5)
84 static const int EPOCH_JDN
= 2440587;
86 // the date of JDN -0.5 (as we don't work with fractional parts, this is the
87 // reference date for us) is Nov 24, 4714BC
88 static const int JDN_0_YEAR
= -4713;
89 static const int JDN_0_MONTH
= wxDateTime::Nov
;
90 static const int JDN_0_DAY
= 24;
92 // the constants used for JDN calculations
93 static const int JDN_OFFSET
= 32046;
94 static const int DAYS_PER_5_MONTHS
= 153;
95 static const int DAYS_PER_4_YEARS
= 1461;
96 static const int DAYS_PER_400_YEARS
= 146097;
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 // a critical section is needed to protect GetTimeZone() static
103 // variable in MT case
105 wxCriticalSection gs_critsectTimezone
;
106 #endif // wxUSE_THREADS
108 // ----------------------------------------------------------------------------
110 // ----------------------------------------------------------------------------
112 // get the number of days in the given month of the given year
114 wxDateTime::wxDateTime_t
GetNumOfDaysInMonth(int year
, wxDateTime::Month month
)
116 // the number of days in month in Julian/Gregorian calendar: the first line
117 // is for normal years, the second one is for the leap ones
118 static wxDateTime::wxDateTime_t daysInMonth
[2][MONTHS_IN_YEAR
] =
120 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
121 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
124 return daysInMonth
[wxDateTime::IsLeapYear(year
)][month
];
127 // ensure that the timezone variable is set by calling localtime
128 static int GetTimeZone()
130 // set to TRUE when the timezone is set
131 static bool s_timezoneSet
= FALSE
;
133 wxCRIT_SECT_LOCKER(lock
, gs_critsectTimezone
);
135 if ( !s_timezoneSet
)
137 // just call localtime() instead of figuring out whether this system
138 // supports tzset(), _tzset() or something else
142 s_timezoneSet
= TRUE
;
145 return (int)timezone
;
148 // return the integral part of the JDN for the midnight of the given date (to
149 // get the real JDN you need to add 0.5, this is, in fact, JDN of the
150 // noon of the previous day)
151 static long GetTruncatedJDN(wxDateTime::wxDateTime_t day
,
152 wxDateTime::Month mon
,
155 // CREDIT: code below is by Scott E. Lee (but bugs are mine)
157 // check the date validity
159 (year
> JDN_0_YEAR
) ||
160 ((year
== JDN_0_YEAR
) && (mon
> JDN_0_MONTH
)) ||
161 ((year
== JDN_0_YEAR
) && (mon
== JDN_0_MONTH
) && (day
>= JDN_0_DAY
)),
162 _T("date out of range - can't convert to JDN")
165 // make the year positive to avoid problems with negative numbers division
168 // months are counted from March here
170 if ( mon
>= wxDateTime::Mar
)
180 // now we can simply add all the contributions together
181 return ((year
/ 100) * DAYS_PER_400_YEARS
) / 4
182 + ((year
% 100) * DAYS_PER_4_YEARS
) / 4
183 + (month
* DAYS_PER_5_MONTHS
+ 2) / 5
188 // this function is a wrapper around strftime(3)
189 static wxString
CallStrftime(const wxChar
*format
, const tm
* tm
)
192 if ( !wxStrftime(buf
, WXSIZEOF(buf
), format
, tm
) )
194 // is ti really possible that 1024 is too short?
195 wxFAIL_MSG(_T("strftime() failed"));
198 return wxString(buf
);
201 // if year and/or month have invalid values, replace them with the current ones
202 static void ReplaceDefaultYearMonthWithCurrent(int *year
,
203 wxDateTime::Month
*month
)
205 struct tm
*tmNow
= NULL
;
207 if ( *year
== wxDateTime::Inv_Year
)
209 tmNow
= wxDateTime::GetTmNow();
211 *year
= 1900 + tmNow
->tm_year
;
214 if ( *month
== wxDateTime::Inv_Month
)
217 tmNow
= wxDateTime::GetTmNow();
219 *month
= (wxDateTime::Month
)tmNow
->tm_mon
;
223 // ============================================================================
224 // implementation of wxDateTime
225 // ============================================================================
227 // ----------------------------------------------------------------------------
229 // ----------------------------------------------------------------------------
231 wxDateTime::Country
wxDateTime::ms_country
= wxDateTime::Country_Unknown
;
232 wxDateTime
wxDateTime::ms_InvDateTime
;
234 // ----------------------------------------------------------------------------
236 // ----------------------------------------------------------------------------
240 year
= (wxDateTime_t
)wxDateTime::Inv_Year
;
241 mon
= wxDateTime::Inv_Month
;
243 hour
= min
= sec
= msec
= 0;
244 wday
= wxDateTime::Inv_WeekDay
;
247 wxDateTime::Tm::Tm(const struct tm
& tm
)
254 mon
= (wxDateTime::Month
)tm
.tm_mon
;
255 year
= 1900 + tm
.tm_year
;
260 bool wxDateTime::Tm::IsValid() const
262 // we allow for the leap seconds, although we don't use them (yet)
263 return (year
!= wxDateTime::Inv_Year
) && (mon
!= wxDateTime::Inv_Month
) &&
264 (mday
< GetNumOfDaysInMonth(year
, mon
)) &&
265 (hour
< 24) && (min
< 60) && (sec
< 62) && (msec
< 1000);
268 void wxDateTime::Tm::ComputeWeekDay()
270 wxFAIL_MSG(_T("TODO"));
273 void wxDateTime::Tm::AddMonths(int monDiff
)
275 // normalize the months field
276 while ( monDiff
< -mon
)
280 monDiff
+= MONTHS_IN_YEAR
;
283 while ( monDiff
+ mon
> MONTHS_IN_YEAR
)
288 mon
= (wxDateTime::Month
)(mon
+ monDiff
);
290 wxASSERT_MSG( mon
>= 0 && mon
< MONTHS_IN_YEAR
, _T("logic error") );
293 void wxDateTime::Tm::AddDays(int dayDiff
)
295 // normalize the days field
301 mday
+= GetNumOfDaysInMonth(year
, mon
);
304 while ( mday
> GetNumOfDaysInMonth(year
, mon
) )
306 mday
-= GetNumOfDaysInMonth(year
, mon
);
311 wxASSERT_MSG( mday
> 0 && mday
<= GetNumOfDaysInMonth(year
, mon
),
315 // ----------------------------------------------------------------------------
317 // ----------------------------------------------------------------------------
319 wxDateTime::TimeZone::TimeZone(wxDateTime::TZ tz
)
323 case wxDateTime::Local
:
324 // leave offset to be 0
327 case wxDateTime::GMT_12
:
328 case wxDateTime::GMT_11
:
329 case wxDateTime::GMT_10
:
330 case wxDateTime::GMT_9
:
331 case wxDateTime::GMT_8
:
332 case wxDateTime::GMT_7
:
333 case wxDateTime::GMT_6
:
334 case wxDateTime::GMT_5
:
335 case wxDateTime::GMT_4
:
336 case wxDateTime::GMT_3
:
337 case wxDateTime::GMT_2
:
338 case wxDateTime::GMT_1
:
339 m_offset
= -60*(wxDateTime::GMT0
- tz
);
342 case wxDateTime::GMT0
:
343 case wxDateTime::GMT1
:
344 case wxDateTime::GMT2
:
345 case wxDateTime::GMT3
:
346 case wxDateTime::GMT4
:
347 case wxDateTime::GMT5
:
348 case wxDateTime::GMT6
:
349 case wxDateTime::GMT7
:
350 case wxDateTime::GMT8
:
351 case wxDateTime::GMT9
:
352 case wxDateTime::GMT10
:
353 case wxDateTime::GMT11
:
354 case wxDateTime::GMT12
:
355 m_offset
= 60*(tz
- wxDateTime::GMT0
);
358 case wxDateTime::A_CST
:
359 // Central Standard Time in use in Australia = UTC + 9.5
360 m_offset
= 9*60 + 30;
364 wxFAIL_MSG( _T("unknown time zone") );
368 // ----------------------------------------------------------------------------
370 // ----------------------------------------------------------------------------
373 bool wxDateTime::IsLeapYear(int year
, wxDateTime::Calendar cal
)
375 if ( year
== Inv_Year
)
376 year
= GetCurrentYear();
378 if ( cal
== Gregorian
)
380 // in Gregorian calendar leap years are those divisible by 4 except
381 // those divisible by 100 unless they're also divisible by 400
382 // (in some countries, like Russia and Greece, additional corrections
383 // exist, but they won't manifest themselves until 2700)
384 return (year
% 4 == 0) && ((year
% 100 != 0) || (year
% 400 == 0));
386 else if ( cal
== Julian
)
388 // in Julian calendar the rule is simpler
389 return year
% 4 == 0;
393 wxFAIL_MSG(_T("unknown calendar"));
400 int wxDateTime::GetCentury(int year
)
402 return year
> 0 ? year
/ 100 : year
/ 100 - 1;
406 void wxDateTime::SetCountry(wxDateTime::Country country
)
408 ms_country
= country
;
412 int wxDateTime::ConvertYearToBC(int year
)
415 return year
> 0 ? year
: year
- 1;
419 int wxDateTime::GetCurrentYear(wxDateTime::Calendar cal
)
424 return Now().GetYear();
427 wxFAIL_MSG(_T("TODO"));
431 wxFAIL_MSG(_T("unsupported calendar"));
439 wxDateTime::Month
wxDateTime::GetCurrentMonth(wxDateTime::Calendar cal
)
444 return Now().GetMonth();
448 wxFAIL_MSG(_T("TODO"));
452 wxFAIL_MSG(_T("unsupported calendar"));
460 wxDateTime::wxDateTime_t
wxDateTime::GetNumberOfDays(int year
, Calendar cal
)
462 if ( year
== Inv_Year
)
464 // take the current year if none given
465 year
= GetCurrentYear();
472 return IsLeapYear(year
) ? 366 : 365;
476 wxFAIL_MSG(_T("unsupported calendar"));
484 wxDateTime::wxDateTime_t
wxDateTime::GetNumberOfDays(wxDateTime::Month month
,
486 wxDateTime::Calendar cal
)
488 wxCHECK_MSG( month
< MONTHS_IN_YEAR
, 0, _T("invalid month") );
490 if ( cal
== Gregorian
|| cal
== Julian
)
492 if ( year
== Inv_Year
)
494 // take the current year if none given
495 year
= GetCurrentYear();
498 return GetNumOfDaysInMonth(year
, month
);
502 wxFAIL_MSG(_T("unsupported calendar"));
509 wxString
wxDateTime::GetMonthName(wxDateTime::Month month
, bool abbr
)
511 wxCHECK_MSG( month
!= Inv_Month
, _T(""), _T("invalid month") );
513 tm tm
= { 0, 0, 0, 1, month
, 76 }; // any year will do
515 return CallStrftime(abbr
? _T("%b") : _T("%B"), &tm
);
519 wxString
wxDateTime::GetWeekDayName(wxDateTime::WeekDay wday
, bool abbr
)
521 wxCHECK_MSG( wday
!= Inv_WeekDay
, _T(""), _T("invalid weekday") );
523 // take some arbitrary Sunday
524 tm tm
= { 0, 0, 0, 28, Nov
, 99 };
526 // and offset it by the number of days needed to get the correct wday
529 return CallStrftime(abbr
? _T("%a") : _T("%A"), &tm
);
532 // ----------------------------------------------------------------------------
533 // constructors and assignment operators
534 // ----------------------------------------------------------------------------
536 wxDateTime
& wxDateTime::Set(const struct tm
& tm1
)
538 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
542 // we want the time in GMT, mktime() takes the local time, so use timegm()
545 time_t timet
= timegm(&tm2
);
546 #else // !HAVE_TIMEGM
547 // FIXME this almost surely doesn't work
548 tm2
.tm_sec
-= GetTimeZone();
550 time_t timet
= mktime(&tm2
);
556 timet
= mktime(&tm2
);
558 #endif // HAVE_TIMEGM/!HAVE_TIMEGM
560 if ( timet
== (time_t)(-1) )
562 wxFAIL_MSG(_T("Invalid time"));
564 return ms_InvDateTime
;
572 wxDateTime
& wxDateTime::Set(wxDateTime_t hour
,
575 wxDateTime_t millisec
)
577 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
579 // we allow seconds to be 61 to account for the leap seconds, even if we
580 // don't use them really
581 wxCHECK_MSG( hour
< 24 && second
< 62 && minute
< 60 && millisec
< 1000,
583 _T("Invalid time in wxDateTime::Set()") );
585 // get the current date from system
586 time_t timet
= GetTimeNow();
587 struct tm
*tm
= gmtime(&timet
);
596 // and finally adjust milliseconds
597 return SetMillisecond(millisec
);
600 wxDateTime
& wxDateTime::Set(wxDateTime_t day
,
606 wxDateTime_t millisec
)
608 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
610 wxCHECK_MSG( hour
< 24 && second
< 62 && minute
< 60 && millisec
< 1000,
612 _T("Invalid time in wxDateTime::Set()") );
614 ReplaceDefaultYearMonthWithCurrent(&year
, &month
);
616 wxCHECK_MSG( (0 < day
) && (day
<= GetNumberOfDays(month
, year
)),
618 _T("Invalid date in wxDateTime::Set()") );
620 // the range of time_t type (inclusive)
621 static const int yearMinInRange
= 1970;
622 static const int yearMaxInRange
= 2037;
624 // test only the year instead of testing for the exact end of the Unix
625 // time_t range - it doesn't bring anything to do more precise checks
626 if ( year
>= yearMinInRange
&& year
<= yearMaxInRange
)
628 // use the standard library version if the date is in range - this is
629 // probably more efficient than our code
631 tm
.tm_year
= year
- 1900;
637 tm
.tm_isdst
= -1; // mktime() will guess it (wrongly, probably)
641 // and finally adjust milliseconds
642 return SetMillisecond(millisec
);
646 // do time calculations ourselves: we want to calculate the number of
647 // milliseconds between the given date and the epoch
649 // get the JDN for the midnight of this day
650 m_time
= GetTruncatedJDN(day
, month
, year
);
652 m_time
*= SECONDS_PER_DAY
* TIME_T_FACTOR
;
654 Add(wxTimeSpan(hour
, minute
, second
, millisec
));
660 wxDateTime
& wxDateTime::Set(double jdn
)
662 // so that m_time will be 0 for the midnight of Jan 1, 1970 which is jdn
664 jdn
-= EPOCH_JDN
+ 0.5;
667 m_time
*= MILLISECONDS_PER_DAY
;
672 // ----------------------------------------------------------------------------
673 // time_t <-> broken down time conversions
674 // ----------------------------------------------------------------------------
676 wxDateTime::Tm
wxDateTime::GetTm() const
678 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
680 time_t time
= GetTicks();
681 if ( time
!= (time_t)-1 )
683 // use C RTL functions
684 tm
*tm
= gmtime(&time
);
686 // should never happen
687 wxCHECK_MSG( tm
, Tm(), _T("gmtime() failed") );
693 // remember the time and do the calculations with the date only - this
694 // eliminates rounding errors of the floating point arithmetics
696 wxLongLong timeMidnight
= m_time
;
698 long timeOnly
= (m_time
% MILLISECONDS_PER_DAY
).ToLong();
701 timeOnly
= MILLISECONDS_PER_DAY
- timeOnly
;
704 timeMidnight
-= timeOnly
;
706 // calculate the Gregorian date from JDN for the midnight of our date:
707 // this will yield day, month (in 1..12 range) and year
709 // actually, this is the JDN for the noon of the previous day
710 long jdn
= (timeMidnight
/ MILLISECONDS_PER_DAY
).ToLong() + EPOCH_JDN
;
712 // CREDIT: code below is by Scott E. Lee (but bugs are mine)
714 wxASSERT_MSG( jdn
> -2, _T("JDN out of range") );
716 // calculate the century
717 int temp
= (jdn
+ JDN_OFFSET
) * 4 - 1;
718 int century
= temp
/ DAYS_PER_400_YEARS
;
720 // then the year and day of year (1 <= dayOfYear <= 366)
721 temp
= ((temp
% DAYS_PER_400_YEARS
) / 4) * 4 + 3;
722 int year
= (century
* 100) + (temp
/ DAYS_PER_4_YEARS
);
723 int dayOfYear
= (temp
% DAYS_PER_4_YEARS
) / 4 + 1;
725 // and finally the month and day of the month
726 temp
= dayOfYear
* 5 - 3;
727 int month
= temp
/ DAYS_PER_5_MONTHS
;
728 int day
= (temp
% DAYS_PER_5_MONTHS
) / 5 + 1;
730 // month is counted from March - convert to normal
741 // year is offset by 4800
744 // check that the algorithm gave us something reasonable
745 wxASSERT_MSG( (0 < month
) && (month
<= 12), _T("invalid month") );
746 wxASSERT_MSG( (1 <= day
) && (day
< 32), _T("invalid day") );
747 wxASSERT_MSG( (INT_MIN
<= year
) && (year
<= INT_MAX
),
748 _T("year range overflow") );
750 // construct Tm from these values
753 tm
.mon
= (Month
)(month
- 1); // algorithm yields 1 for January, not 0
754 tm
.mday
= (wxDateTime_t
)day
;
755 tm
.msec
= timeOnly
% 1000;
757 timeOnly
/= 1000; // now we have time in seconds
759 tm
.sec
= timeOnly
% 60;
761 timeOnly
/= 60; // now we have time in minutes
763 tm
.min
= timeOnly
% 60;
766 tm
.hour
= timeOnly
/ 60;
772 wxDateTime
& wxDateTime::SetYear(int year
)
774 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
783 wxDateTime
& wxDateTime::SetMonth(Month month
)
785 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
794 wxDateTime
& wxDateTime::SetDay(wxDateTime_t mday
)
796 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
805 wxDateTime
& wxDateTime::SetHour(wxDateTime_t hour
)
807 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
816 wxDateTime
& wxDateTime::SetMinute(wxDateTime_t min
)
818 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
827 wxDateTime
& wxDateTime::SetSecond(wxDateTime_t sec
)
829 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
838 wxDateTime
& wxDateTime::SetMillisecond(wxDateTime_t millisecond
)
840 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
842 // we don't need to use GetTm() for this one
843 m_time
-= m_time
% 1000l;
844 m_time
+= millisecond
;
849 // ----------------------------------------------------------------------------
850 // wxDateTime arithmetics
851 // ----------------------------------------------------------------------------
853 wxDateTime
& wxDateTime::Add(const wxDateSpan
& diff
)
857 tm
.year
+= diff
.GetYears();
858 tm
.AddMonths(diff
.GetMonths());
859 tm
.AddDays(diff
.GetTotalDays());
866 // ----------------------------------------------------------------------------
867 // Weekday and monthday stuff
868 // ----------------------------------------------------------------------------
870 wxDateTime
& wxDateTime::SetToLastMonthDay(Month month
,
873 // take the current month/year if none specified
874 ReplaceDefaultYearMonthWithCurrent(&year
, &month
);
876 return Set(GetNumOfDaysInMonth(year
, month
), month
, year
);
879 bool wxDateTime::SetToWeekDay(WeekDay weekday
,
884 wxCHECK_MSG( weekday
!= Inv_WeekDay
, FALSE
, _T("invalid weekday") );
886 // we don't check explicitly that -5 <= n <= 5 because we will return FALSE
887 // anyhow in such case - but may be should still give an assert for it?
889 // take the current month/year if none specified
890 ReplaceDefaultYearMonthWithCurrent(&year
, &month
);
894 // TODO this probably could be optimised somehow...
898 // get the first day of the month
899 dt
.Set(1, month
, year
);
902 WeekDay wdayFirst
= dt
.GetWeekDay();
904 // go to the first weekday of the month
905 int diff
= weekday
- wdayFirst
;
909 // add advance n-1 weeks more
912 dt
-= wxDateSpan::Days(diff
);
916 // get the last day of the month
917 dt
.SetToLastMonthDay(month
, year
);
920 WeekDay wdayLast
= dt
.GetWeekDay();
922 // go to the last weekday of the month
923 int diff
= wdayLast
- weekday
;
927 // and rewind n-1 weeks from there
930 dt
-= wxDateSpan::Days(diff
);
933 // check that it is still in the same month
934 if ( dt
.GetMonth() == month
)
942 // no such day in this month
947 // ----------------------------------------------------------------------------
948 // Julian day number conversion and related stuff
949 // ----------------------------------------------------------------------------
951 double wxDateTime::GetJulianDayNumber() const
955 double result
= GetTruncatedJDN(tm
.mday
, tm
.mon
, tm
.year
);
957 // add the part GetTruncatedJDN() neglected
960 // and now add the time: 86400 sec = 1 JDN
961 return result
+ ((double)(60*(60*tm
.hour
+ tm
.min
) + tm
.sec
)) / 86400;
964 double wxDateTime::GetRataDie() const
966 // March 1 of the year 0 is Rata Die day -306 and JDN 1721119.5
967 return GetJulianDayNumber() - 1721119.5 - 306;
970 // ----------------------------------------------------------------------------
972 // ----------------------------------------------------------------------------
974 wxDateTime
& wxDateTime::MakeUTC()
976 return Add(wxTimeSpan::Seconds(GetTimeZone()));
979 wxDateTime
& wxDateTime::MakeTimezone(const TimeZone
& tz
)
981 int minDiff
= GetTimeZone() / SECONDS_IN_MINUTE
+ tz
.GetOffset();
982 return Add(wxTimeSpan::Minutes(minDiff
));
985 wxDateTime
& wxDateTime::MakeLocalTime(const TimeZone
& tz
)
987 int minDiff
= GetTimeZone() / SECONDS_IN_MINUTE
+ tz
.GetOffset();
988 return Substract(wxTimeSpan::Minutes(minDiff
));
991 // ----------------------------------------------------------------------------
992 // wxDateTime to/from text representations
993 // ----------------------------------------------------------------------------
995 wxString
wxDateTime::Format(const wxChar
*format
) const
997 wxCHECK_MSG( format
, _T(""), _T("NULL format in wxDateTime::Format") );
999 time_t time
= GetTicks();
1000 if ( time
!= (time_t)-1 )
1003 tm
*tm
= gmtime(&time
);
1005 // should never happen
1006 wxCHECK_MSG( tm
, _T(""), _T("gmtime() failed") );
1008 return CallStrftime(format
, tm
);
1012 // use a hack and still use strftime(): make a copy of the format and
1013 // replace all occurences of YEAR in it with some unique string not
1014 // appearing anywhere else in it, then use strftime() to format the
1015 // date in year YEAR and then replace YEAR back by the real year and
1016 // the unique replacement string back with YEAR where YEAR is any year
1017 // in the range supported by strftime() (1970 - 2037) which is equal to
1018 // the real year modulo 28 (so the week days coincide for them)
1021 int yearReal
= GetYear();
1022 int year
= 1970 + yearReal
% 28;
1025 strYear
.Printf(_T("%d"), year
);
1027 // find a string not occuring in format (this is surely not optimal way
1028 // of doing it... improvements welcome!)
1029 wxString fmt
= format
;
1030 wxString replacement
= (wxChar
)-1;
1031 while ( fmt
.Find(replacement
) != wxNOT_FOUND
)
1033 replacement
<< (wxChar
)-1;
1036 // replace all occurences of year with it
1037 bool wasReplaced
= fmt
.Replace(strYear
, replacement
) > 0;
1039 // use strftime() to format the same date but in supported year
1040 wxDateTime
dt(*this);
1042 wxString str
= dt
.Format(format
);
1044 // now replace the occurence of 1999 with the real year
1045 wxString strYearReal
;
1046 strYearReal
.Printf(_T("%d"), yearReal
);
1047 str
.Replace(strYear
, strYearReal
);
1049 // and replace back all occurences of replacement string
1051 str
.Replace(replacement
, strYear
);
1057 // ============================================================================
1059 // ============================================================================
1061 // not all strftime(3) format specifiers make sense here because, for example,
1062 // a time span doesn't have a year nor a timezone
1064 // Here are the ones which are supported (all of them are supported by strftime
1066 // %H hour in 24 hour format
1067 // %M minute (00 - 59)
1068 // %S second (00 - 59)
1071 // Also, for MFC CTimeSpan compatibility, we support
1072 // %D number of days
1074 // And, to be better than MFC :-), we also have
1075 // %E number of wEeks
1076 // %l milliseconds (000 - 999)
1077 wxString
wxTimeSpan::Format(const wxChar
*format
) const
1079 wxCHECK_MSG( format
, _T(""), _T("NULL format in wxTimeSpan::Format") );
1082 str
.Alloc(strlen(format
));
1084 for ( const wxChar
*pch
= format
; pch
; pch
++ )
1096 wxFAIL_MSG( _T("invalid format character") );
1100 // will get to str << ch below
1104 tmp
.Printf(_T("%d"), GetDays());
1108 tmp
.Printf(_T("%d"), GetWeeks());
1112 tmp
.Printf(_T("%02d"), GetHours());
1116 tmp
.Printf(_T("%03d"), GetMilliseconds());
1120 tmp
.Printf(_T("%02d"), GetMinutes());
1124 tmp
.Printf(_T("%02d"), GetSeconds());
1132 // skip str += ch below