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) - so it is always
26 * 2. the range is thus something about 580 million years, but due to current
27 * algorithms limitations, only dates from Nov 24, 4714BC are handled
29 * 3. standard ANSI C functions are used to do time calculations whenever
30 * possible, i.e. when the date is in the range Jan 1, 1970 to 2038
32 * 4. otherwise, the calculations are done by converting the date to/from JDN
33 * first (the range limitation mentioned above comes from here: the
34 * algorithm used by Scott E. Lee's code only works for positive JDNs, more
37 * 5. the object constructed for the given DD-MM-YYYY HH:MM:SS corresponds to
38 * this moment in local time and may be converted to the object
39 * corresponding to the same date/time in another time zone by using
42 * 6. the conversions to the current (or any other) timezone are done when the
43 * internal time representation is converted to the broken-down one in
47 // ============================================================================
49 // ============================================================================
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
56 #pragma implementation "datetime.h"
59 // For compilers that support precompilation, includes "wx.h".
60 #include "wx/wxprec.h"
67 #include "wx/string.h"
72 #include "wx/thread.h"
74 #define wxDEFINE_TIME_CONSTANTS
76 #include "wx/datetime.h"
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
83 static const int MONTHS_IN_YEAR
= 12;
85 static const int SECONDS_IN_MINUTE
= 60;
87 static const long SECONDS_PER_DAY
= 86400l;
89 static const long MILLISECONDS_PER_DAY
= 86400000l;
91 // this is the integral part of JDN of the midnight of Jan 1, 1970
92 // (i.e. JDN(Jan 1, 1970) = 2440587.5)
93 static const int EPOCH_JDN
= 2440587;
95 // the date of JDN -0.5 (as we don't work with fractional parts, this is the
96 // reference date for us) is Nov 24, 4714BC
97 static const int JDN_0_YEAR
= -4713;
98 static const int JDN_0_MONTH
= wxDateTime::Nov
;
99 static const int JDN_0_DAY
= 24;
101 // the constants used for JDN calculations
102 static const int JDN_OFFSET
= 32046;
103 static const int DAYS_PER_5_MONTHS
= 153;
104 static const int DAYS_PER_4_YEARS
= 1461;
105 static const int DAYS_PER_400_YEARS
= 146097;
107 // ----------------------------------------------------------------------------
109 // ----------------------------------------------------------------------------
111 // a critical section is needed to protect GetTimeZone() static
112 // variable in MT case
114 wxCriticalSection gs_critsectTimezone
;
115 #endif // wxUSE_THREADS
117 // ----------------------------------------------------------------------------
119 // ----------------------------------------------------------------------------
121 // get the number of days in the given month of the given year
123 wxDateTime::wxDateTime_t
GetNumOfDaysInMonth(int year
, wxDateTime::Month month
)
125 // the number of days in month in Julian/Gregorian calendar: the first line
126 // is for normal years, the second one is for the leap ones
127 static wxDateTime::wxDateTime_t daysInMonth
[2][MONTHS_IN_YEAR
] =
129 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
130 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
133 return daysInMonth
[wxDateTime::IsLeapYear(year
)][month
];
136 // ensure that the timezone variable is set by calling localtime
137 static int GetTimeZone()
139 // set to TRUE when the timezone is set
140 static bool s_timezoneSet
= FALSE
;
142 wxCRIT_SECT_LOCKER(lock
, gs_critsectTimezone
);
144 if ( !s_timezoneSet
)
146 // just call localtime() instead of figuring out whether this system
147 // supports tzset(), _tzset() or something else
151 s_timezoneSet
= TRUE
;
154 return (int)timezone
;
157 // return the integral part of the JDN for the midnight of the given date (to
158 // get the real JDN you need to add 0.5, this is, in fact, JDN of the
159 // noon of the previous day)
160 static long GetTruncatedJDN(wxDateTime::wxDateTime_t day
,
161 wxDateTime::Month mon
,
164 // CREDIT: code below is by Scott E. Lee (but bugs are mine)
166 // check the date validity
168 (year
> JDN_0_YEAR
) ||
169 ((year
== JDN_0_YEAR
) && (mon
> JDN_0_MONTH
)) ||
170 ((year
== JDN_0_YEAR
) && (mon
== JDN_0_MONTH
) && (day
>= JDN_0_DAY
)),
171 _T("date out of range - can't convert to JDN")
174 // make the year positive to avoid problems with negative numbers division
177 // months are counted from March here
179 if ( mon
>= wxDateTime::Mar
)
189 // now we can simply add all the contributions together
190 return ((year
/ 100) * DAYS_PER_400_YEARS
) / 4
191 + ((year
% 100) * DAYS_PER_4_YEARS
) / 4
192 + (month
* DAYS_PER_5_MONTHS
+ 2) / 5
197 // this function is a wrapper around strftime(3)
198 static wxString
CallStrftime(const wxChar
*format
, const tm
* tm
)
201 if ( !wxStrftime(buf
, WXSIZEOF(buf
), format
, tm
) )
203 // is ti really possible that 1024 is too short?
204 wxFAIL_MSG(_T("strftime() failed"));
207 return wxString(buf
);
210 // if year and/or month have invalid values, replace them with the current ones
211 static void ReplaceDefaultYearMonthWithCurrent(int *year
,
212 wxDateTime::Month
*month
)
214 struct tm
*tmNow
= NULL
;
216 if ( *year
== wxDateTime::Inv_Year
)
218 tmNow
= wxDateTime::GetTmNow();
220 *year
= 1900 + tmNow
->tm_year
;
223 if ( *month
== wxDateTime::Inv_Month
)
226 tmNow
= wxDateTime::GetTmNow();
228 *month
= (wxDateTime::Month
)tmNow
->tm_mon
;
232 // ============================================================================
233 // implementation of wxDateTime
234 // ============================================================================
236 // ----------------------------------------------------------------------------
238 // ----------------------------------------------------------------------------
240 wxDateTime::Country
wxDateTime::ms_country
= wxDateTime::Country_Unknown
;
241 wxDateTime
wxDateTime::ms_InvDateTime
;
243 // ----------------------------------------------------------------------------
245 // ----------------------------------------------------------------------------
249 year
= (wxDateTime_t
)wxDateTime::Inv_Year
;
250 mon
= wxDateTime::Inv_Month
;
252 hour
= min
= sec
= msec
= 0;
253 wday
= wxDateTime::Inv_WeekDay
;
256 wxDateTime::Tm::Tm(const struct tm
& tm
, const TimeZone
& tz
)
264 mon
= (wxDateTime::Month
)tm
.tm_mon
;
265 year
= 1900 + tm
.tm_year
;
270 bool wxDateTime::Tm::IsValid() const
272 // we allow for the leap seconds, although we don't use them (yet)
273 return (year
!= wxDateTime::Inv_Year
) && (mon
!= wxDateTime::Inv_Month
) &&
274 (mday
< GetNumOfDaysInMonth(year
, mon
)) &&
275 (hour
< 24) && (min
< 60) && (sec
< 62) && (msec
< 1000);
278 void wxDateTime::Tm::ComputeWeekDay()
280 wxFAIL_MSG(_T("TODO"));
283 void wxDateTime::Tm::AddMonths(int monDiff
)
285 // normalize the months field
286 while ( monDiff
< -mon
)
290 monDiff
+= MONTHS_IN_YEAR
;
293 while ( monDiff
+ mon
> MONTHS_IN_YEAR
)
298 mon
= (wxDateTime::Month
)(mon
+ monDiff
);
300 wxASSERT_MSG( mon
>= 0 && mon
< MONTHS_IN_YEAR
, _T("logic error") );
303 void wxDateTime::Tm::AddDays(int dayDiff
)
305 // normalize the days field
311 mday
+= GetNumOfDaysInMonth(year
, mon
);
314 while ( mday
> GetNumOfDaysInMonth(year
, mon
) )
316 mday
-= GetNumOfDaysInMonth(year
, mon
);
321 wxASSERT_MSG( mday
> 0 && mday
<= GetNumOfDaysInMonth(year
, mon
),
325 // ----------------------------------------------------------------------------
327 // ----------------------------------------------------------------------------
329 wxDateTime::TimeZone::TimeZone(wxDateTime::TZ tz
)
333 case wxDateTime::Local
:
334 // get the offset from C RTL: it returns the difference GMT-local
335 // while we want to have the offset _from_ GMT, hence the '-'
336 m_offset
= -GetTimeZone();
339 case wxDateTime::GMT_12
:
340 case wxDateTime::GMT_11
:
341 case wxDateTime::GMT_10
:
342 case wxDateTime::GMT_9
:
343 case wxDateTime::GMT_8
:
344 case wxDateTime::GMT_7
:
345 case wxDateTime::GMT_6
:
346 case wxDateTime::GMT_5
:
347 case wxDateTime::GMT_4
:
348 case wxDateTime::GMT_3
:
349 case wxDateTime::GMT_2
:
350 case wxDateTime::GMT_1
:
351 m_offset
= -3600*(wxDateTime::GMT0
- tz
);
354 case wxDateTime::GMT0
:
355 case wxDateTime::GMT1
:
356 case wxDateTime::GMT2
:
357 case wxDateTime::GMT3
:
358 case wxDateTime::GMT4
:
359 case wxDateTime::GMT5
:
360 case wxDateTime::GMT6
:
361 case wxDateTime::GMT7
:
362 case wxDateTime::GMT8
:
363 case wxDateTime::GMT9
:
364 case wxDateTime::GMT10
:
365 case wxDateTime::GMT11
:
366 case wxDateTime::GMT12
:
367 m_offset
= 3600*(tz
- wxDateTime::GMT0
);
370 case wxDateTime::A_CST
:
371 // Central Standard Time in use in Australia = UTC + 9.5
372 m_offset
= 60*(9*60 + 30);
376 wxFAIL_MSG( _T("unknown time zone") );
380 // ----------------------------------------------------------------------------
382 // ----------------------------------------------------------------------------
385 bool wxDateTime::IsLeapYear(int year
, wxDateTime::Calendar cal
)
387 if ( year
== Inv_Year
)
388 year
= GetCurrentYear();
390 if ( cal
== Gregorian
)
392 // in Gregorian calendar leap years are those divisible by 4 except
393 // those divisible by 100 unless they're also divisible by 400
394 // (in some countries, like Russia and Greece, additional corrections
395 // exist, but they won't manifest themselves until 2700)
396 return (year
% 4 == 0) && ((year
% 100 != 0) || (year
% 400 == 0));
398 else if ( cal
== Julian
)
400 // in Julian calendar the rule is simpler
401 return year
% 4 == 0;
405 wxFAIL_MSG(_T("unknown calendar"));
412 int wxDateTime::GetCentury(int year
)
414 return year
> 0 ? year
/ 100 : year
/ 100 - 1;
418 void wxDateTime::SetCountry(wxDateTime::Country country
)
420 ms_country
= country
;
424 int wxDateTime::ConvertYearToBC(int year
)
427 return year
> 0 ? year
: year
- 1;
431 int wxDateTime::GetCurrentYear(wxDateTime::Calendar cal
)
436 return Now().GetYear();
439 wxFAIL_MSG(_T("TODO"));
443 wxFAIL_MSG(_T("unsupported calendar"));
451 wxDateTime::Month
wxDateTime::GetCurrentMonth(wxDateTime::Calendar cal
)
456 return Now().GetMonth();
460 wxFAIL_MSG(_T("TODO"));
464 wxFAIL_MSG(_T("unsupported calendar"));
472 wxDateTime::wxDateTime_t
wxDateTime::GetNumberOfDays(int year
, Calendar cal
)
474 if ( year
== Inv_Year
)
476 // take the current year if none given
477 year
= GetCurrentYear();
484 return IsLeapYear(year
) ? 366 : 365;
488 wxFAIL_MSG(_T("unsupported calendar"));
496 wxDateTime::wxDateTime_t
wxDateTime::GetNumberOfDays(wxDateTime::Month month
,
498 wxDateTime::Calendar cal
)
500 wxCHECK_MSG( month
< MONTHS_IN_YEAR
, 0, _T("invalid month") );
502 if ( cal
== Gregorian
|| cal
== Julian
)
504 if ( year
== Inv_Year
)
506 // take the current year if none given
507 year
= GetCurrentYear();
510 return GetNumOfDaysInMonth(year
, month
);
514 wxFAIL_MSG(_T("unsupported calendar"));
521 wxString
wxDateTime::GetMonthName(wxDateTime::Month month
, bool abbr
)
523 wxCHECK_MSG( month
!= Inv_Month
, _T(""), _T("invalid month") );
525 tm tm
= { 0, 0, 0, 1, month
, 76 }; // any year will do
527 return CallStrftime(abbr
? _T("%b") : _T("%B"), &tm
);
531 wxString
wxDateTime::GetWeekDayName(wxDateTime::WeekDay wday
, bool abbr
)
533 wxCHECK_MSG( wday
!= Inv_WeekDay
, _T(""), _T("invalid weekday") );
535 // take some arbitrary Sunday
536 tm tm
= { 0, 0, 0, 28, Nov
, 99 };
538 // and offset it by the number of days needed to get the correct wday
541 return CallStrftime(abbr
? _T("%a") : _T("%A"), &tm
);
544 // ----------------------------------------------------------------------------
545 // constructors and assignment operators
546 // ----------------------------------------------------------------------------
548 // the values in the tm structure contain the local time
549 wxDateTime
& wxDateTime::Set(const struct tm
& tm
)
551 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
554 time_t timet
= mktime(&tm2
);
556 if ( timet
== (time_t)(-1) )
558 wxFAIL_MSG(_T("Invalid time"));
560 return ms_InvDateTime
;
568 wxDateTime
& wxDateTime::Set(wxDateTime_t hour
,
571 wxDateTime_t millisec
)
573 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
575 // we allow seconds to be 61 to account for the leap seconds, even if we
576 // don't use them really
577 wxCHECK_MSG( hour
< 24 && second
< 62 && minute
< 60 && millisec
< 1000,
579 _T("Invalid time in wxDateTime::Set()") );
581 // get the current date from system
582 time_t timet
= GetTimeNow();
583 struct tm
*tm
= localtime(&timet
);
585 wxCHECK_MSG( tm
, ms_InvDateTime
, _T("localtime() failed") );
594 // and finally adjust milliseconds
595 return SetMillisecond(millisec
);
598 wxDateTime
& wxDateTime::Set(wxDateTime_t day
,
604 wxDateTime_t millisec
)
606 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
608 wxCHECK_MSG( hour
< 24 && second
< 62 && minute
< 60 && millisec
< 1000,
610 _T("Invalid time in wxDateTime::Set()") );
612 ReplaceDefaultYearMonthWithCurrent(&year
, &month
);
614 wxCHECK_MSG( (0 < day
) && (day
<= GetNumberOfDays(month
, year
)),
616 _T("Invalid date in wxDateTime::Set()") );
618 // the range of time_t type (inclusive)
619 static const int yearMinInRange
= 1970;
620 static const int yearMaxInRange
= 2037;
622 // test only the year instead of testing for the exact end of the Unix
623 // time_t range - it doesn't bring anything to do more precise checks
624 if ( year
>= yearMinInRange
&& year
<= yearMaxInRange
)
626 // use the standard library version if the date is in range - this is
627 // probably more efficient than our code
629 tm
.tm_year
= year
- 1900;
635 tm
.tm_isdst
= -1; // mktime() will guess it
639 // and finally adjust milliseconds
640 return SetMillisecond(millisec
);
644 // do time calculations ourselves: we want to calculate the number of
645 // milliseconds between the given date and the epoch
647 // get the JDN for the midnight of this day
648 m_time
= GetTruncatedJDN(day
, month
, year
);
650 m_time
*= SECONDS_PER_DAY
* TIME_T_FACTOR
;
652 // JDN corresponds to GMT, we take localtime
653 Add(wxTimeSpan(hour
, minute
, second
+ GetTimeZone(), millisec
));
659 wxDateTime
& wxDateTime::Set(double jdn
)
661 // so that m_time will be 0 for the midnight of Jan 1, 1970 which is jdn
663 jdn
-= EPOCH_JDN
+ 0.5;
666 m_time
*= MILLISECONDS_PER_DAY
;
671 // ----------------------------------------------------------------------------
672 // time_t <-> broken down time conversions
673 // ----------------------------------------------------------------------------
675 wxDateTime::Tm
wxDateTime::GetTm(const TimeZone
& tz
) const
677 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
679 time_t time
= GetTicks();
680 if ( time
!= (time_t)-1 )
682 // use C RTL functions
684 if ( tz
.GetOffset() == -GetTimeZone() )
686 // we are working with local time
687 tm
= localtime(&time
);
694 // should never happen
695 wxCHECK_MSG( tm
, Tm(), _T("gmtime() failed") );
701 // remember the time and do the calculations with the date only - this
702 // eliminates rounding errors of the floating point arithmetics
704 wxLongLong timeMidnight
= m_time
- GetTimeZone() * 1000;
706 long timeOnly
= (timeMidnight
% MILLISECONDS_PER_DAY
).ToLong();
708 // we want to always have positive time and timeMidnight to be really
709 // the midnight before it
712 timeOnly
= MILLISECONDS_PER_DAY
+ timeOnly
;
715 timeMidnight
-= timeOnly
;
717 // calculate the Gregorian date from JDN for the midnight of our date:
718 // this will yield day, month (in 1..12 range) and year
720 // actually, this is the JDN for the noon of the previous day
721 long jdn
= (timeMidnight
/ MILLISECONDS_PER_DAY
).ToLong() + EPOCH_JDN
;
723 // CREDIT: code below is by Scott E. Lee (but bugs are mine)
725 wxASSERT_MSG( jdn
> -2, _T("JDN out of range") );
727 // calculate the century
728 int temp
= (jdn
+ JDN_OFFSET
) * 4 - 1;
729 int century
= temp
/ DAYS_PER_400_YEARS
;
731 // then the year and day of year (1 <= dayOfYear <= 366)
732 temp
= ((temp
% DAYS_PER_400_YEARS
) / 4) * 4 + 3;
733 int year
= (century
* 100) + (temp
/ DAYS_PER_4_YEARS
);
734 int dayOfYear
= (temp
% DAYS_PER_4_YEARS
) / 4 + 1;
736 // and finally the month and day of the month
737 temp
= dayOfYear
* 5 - 3;
738 int month
= temp
/ DAYS_PER_5_MONTHS
;
739 int day
= (temp
% DAYS_PER_5_MONTHS
) / 5 + 1;
741 // month is counted from March - convert to normal
752 // year is offset by 4800
755 // check that the algorithm gave us something reasonable
756 wxASSERT_MSG( (0 < month
) && (month
<= 12), _T("invalid month") );
757 wxASSERT_MSG( (1 <= day
) && (day
< 32), _T("invalid day") );
758 wxASSERT_MSG( (INT_MIN
<= year
) && (year
<= INT_MAX
),
759 _T("year range overflow") );
761 // construct Tm from these values
764 tm
.mon
= (Month
)(month
- 1); // algorithm yields 1 for January, not 0
765 tm
.mday
= (wxDateTime_t
)day
;
766 tm
.msec
= timeOnly
% 1000;
768 timeOnly
/= 1000; // now we have time in seconds
770 tm
.sec
= timeOnly
% 60;
772 timeOnly
/= 60; // now we have time in minutes
774 tm
.min
= timeOnly
% 60;
777 tm
.hour
= timeOnly
/ 60;
783 wxDateTime
& wxDateTime::SetYear(int year
)
785 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
794 wxDateTime
& wxDateTime::SetMonth(Month month
)
796 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
805 wxDateTime
& wxDateTime::SetDay(wxDateTime_t mday
)
807 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
816 wxDateTime
& wxDateTime::SetHour(wxDateTime_t hour
)
818 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
827 wxDateTime
& wxDateTime::SetMinute(wxDateTime_t min
)
829 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
838 wxDateTime
& wxDateTime::SetSecond(wxDateTime_t sec
)
840 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
849 wxDateTime
& wxDateTime::SetMillisecond(wxDateTime_t millisecond
)
851 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
853 // we don't need to use GetTm() for this one
854 m_time
-= m_time
% 1000l;
855 m_time
+= millisecond
;
860 // ----------------------------------------------------------------------------
861 // wxDateTime arithmetics
862 // ----------------------------------------------------------------------------
864 wxDateTime
& wxDateTime::Add(const wxDateSpan
& diff
)
868 tm
.year
+= diff
.GetYears();
869 tm
.AddMonths(diff
.GetMonths());
870 tm
.AddDays(diff
.GetTotalDays());
877 // ----------------------------------------------------------------------------
878 // Weekday and monthday stuff
879 // ----------------------------------------------------------------------------
881 wxDateTime
& wxDateTime::SetToLastMonthDay(Month month
,
884 // take the current month/year if none specified
885 ReplaceDefaultYearMonthWithCurrent(&year
, &month
);
887 return Set(GetNumOfDaysInMonth(year
, month
), month
, year
);
890 bool wxDateTime::SetToWeekDay(WeekDay weekday
,
895 wxCHECK_MSG( weekday
!= Inv_WeekDay
, FALSE
, _T("invalid weekday") );
897 // we don't check explicitly that -5 <= n <= 5 because we will return FALSE
898 // anyhow in such case - but may be should still give an assert for it?
900 // take the current month/year if none specified
901 ReplaceDefaultYearMonthWithCurrent(&year
, &month
);
905 // TODO this probably could be optimised somehow...
909 // get the first day of the month
910 dt
.Set(1, month
, year
);
913 WeekDay wdayFirst
= dt
.GetWeekDay();
915 // go to the first weekday of the month
916 int diff
= weekday
- wdayFirst
;
920 // add advance n-1 weeks more
923 dt
-= wxDateSpan::Days(diff
);
927 // get the last day of the month
928 dt
.SetToLastMonthDay(month
, year
);
931 WeekDay wdayLast
= dt
.GetWeekDay();
933 // go to the last weekday of the month
934 int diff
= wdayLast
- weekday
;
938 // and rewind n-1 weeks from there
941 dt
-= wxDateSpan::Days(diff
);
944 // check that it is still in the same month
945 if ( dt
.GetMonth() == month
)
953 // no such day in this month
958 // ----------------------------------------------------------------------------
959 // Julian day number conversion and related stuff
960 // ----------------------------------------------------------------------------
962 double wxDateTime::GetJulianDayNumber() const
964 // JDN are always expressed for the GMT dates
965 Tm
tm(ToTimezone(GMT0
).GetTm(GMT0
));
967 double result
= GetTruncatedJDN(tm
.mday
, tm
.mon
, tm
.year
);
969 // add the part GetTruncatedJDN() neglected
972 // and now add the time: 86400 sec = 1 JDN
973 return result
+ ((double)(60*(60*tm
.hour
+ tm
.min
) + tm
.sec
)) / 86400;
976 double wxDateTime::GetRataDie() const
978 // March 1 of the year 0 is Rata Die day -306 and JDN 1721119.5
979 return GetJulianDayNumber() - 1721119.5 - 306;
982 // ----------------------------------------------------------------------------
983 // timezone and DST stuff
984 // ----------------------------------------------------------------------------
986 int wxDateTime::IsDST(wxDateTime::Country country
) const
988 wxCHECK_MSG( country
== Country_Default
, -1,
989 _T("country support not implemented") );
991 // use the C RTL for the dates in the standard range
992 time_t timet
= GetTicks();
993 if ( timet
!= (time_t)-1 )
995 tm
*tm
= localtime(&timet
);
997 wxCHECK_MSG( tm
, -1, _T("localtime() failed") );
1003 // wxFAIL_MSG( _T("TODO") );
1009 wxDateTime
& wxDateTime::MakeTimezone(const TimeZone
& tz
)
1011 int secDiff
= GetTimeZone() + tz
.GetOffset();
1013 // we need to know whether DST is or not in effect for this date
1016 // FIXME we assume that the DST is always shifted by 1 hour
1020 return Substract(wxTimeSpan::Seconds(secDiff
));
1023 // ----------------------------------------------------------------------------
1024 // wxDateTime to/from text representations
1025 // ----------------------------------------------------------------------------
1027 wxString
wxDateTime::Format(const wxChar
*format
, const TimeZone
& tz
) const
1029 wxCHECK_MSG( format
, _T(""), _T("NULL format in wxDateTime::Format") );
1031 time_t time
= GetTicks();
1032 if ( time
!= (time_t)-1 )
1036 if ( tz
.GetOffset() == -GetTimeZone() )
1038 // we are working with local time
1039 tm
= localtime(&time
);
1043 time
+= tz
.GetOffset();
1048 // should never happen
1049 wxCHECK_MSG( tm
, _T(""), _T("gmtime() failed") );
1051 return CallStrftime(format
, tm
);
1055 // use a hack and still use strftime(): make a copy of the format and
1056 // replace all occurences of YEAR in it with some unique string not
1057 // appearing anywhere else in it, then use strftime() to format the
1058 // date in year YEAR and then replace YEAR back by the real year and
1059 // the unique replacement string back with YEAR where YEAR is any year
1060 // in the range supported by strftime() (1970 - 2037) which is equal to
1061 // the real year modulo 28 (so the week days coincide for them)
1064 int yearReal
= GetYear(tz
);
1065 int year
= 1970 + yearReal
% 28;
1068 strYear
.Printf(_T("%d"), year
);
1070 // find a string not occuring in format (this is surely not optimal way
1071 // of doing it... improvements welcome!)
1072 wxString fmt
= format
;
1073 wxString replacement
= (wxChar
)-1;
1074 while ( fmt
.Find(replacement
) != wxNOT_FOUND
)
1076 replacement
<< (wxChar
)-1;
1079 // replace all occurences of year with it
1080 bool wasReplaced
= fmt
.Replace(strYear
, replacement
) > 0;
1082 // use strftime() to format the same date but in supported year
1083 wxDateTime
dt(*this);
1085 wxString str
= dt
.Format(format
, tz
);
1087 // now replace the occurence of 1999 with the real year
1088 wxString strYearReal
;
1089 strYearReal
.Printf(_T("%d"), yearReal
);
1090 str
.Replace(strYear
, strYearReal
);
1092 // and replace back all occurences of replacement string
1094 str
.Replace(replacement
, strYear
);
1100 // ============================================================================
1102 // ============================================================================
1104 // not all strftime(3) format specifiers make sense here because, for example,
1105 // a time span doesn't have a year nor a timezone
1107 // Here are the ones which are supported (all of them are supported by strftime
1109 // %H hour in 24 hour format
1110 // %M minute (00 - 59)
1111 // %S second (00 - 59)
1114 // Also, for MFC CTimeSpan compatibility, we support
1115 // %D number of days
1117 // And, to be better than MFC :-), we also have
1118 // %E number of wEeks
1119 // %l milliseconds (000 - 999)
1120 wxString
wxTimeSpan::Format(const wxChar
*format
) const
1122 wxCHECK_MSG( format
, _T(""), _T("NULL format in wxTimeSpan::Format") );
1125 str
.Alloc(strlen(format
));
1127 for ( const wxChar
*pch
= format
; pch
; pch
++ )
1139 wxFAIL_MSG( _T("invalid format character") );
1143 // will get to str << ch below
1147 tmp
.Printf(_T("%d"), GetDays());
1151 tmp
.Printf(_T("%d"), GetWeeks());
1155 tmp
.Printf(_T("%02d"), GetHours());
1159 tmp
.Printf(_T("%03d"), GetMilliseconds());
1163 tmp
.Printf(_T("%02d"), GetMinutes());
1167 tmp
.Printf(_T("%02d"), GetSeconds());
1175 // skip str += ch below