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 // mktime() rather unintuitively fails for Jan 1, 1970 if the hour is
559 // less than timezone - try to make it work for this case
560 if ( tm2
.tm_year
== 70 && tm2
.tm_mon
== 0 && tm2
.tm_mday
== 1 )
562 // add timezone to make sure that date is in range
563 tm2
.tm_sec
-= GetTimeZone();
565 timet
= mktime(&tm2
);
566 if ( timet
!= (time_t)-1 )
568 timet
+= GetTimeZone();
574 wxFAIL_MSG( _T("mktime() failed") );
576 return ms_InvDateTime
;
584 wxDateTime
& wxDateTime::Set(wxDateTime_t hour
,
587 wxDateTime_t millisec
)
589 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
591 // we allow seconds to be 61 to account for the leap seconds, even if we
592 // don't use them really
593 wxCHECK_MSG( hour
< 24 && second
< 62 && minute
< 60 && millisec
< 1000,
595 _T("Invalid time in wxDateTime::Set()") );
597 // get the current date from system
598 time_t timet
= GetTimeNow();
599 struct tm
*tm
= localtime(&timet
);
601 wxCHECK_MSG( tm
, ms_InvDateTime
, _T("localtime() failed") );
610 // and finally adjust milliseconds
611 return SetMillisecond(millisec
);
614 wxDateTime
& wxDateTime::Set(wxDateTime_t day
,
620 wxDateTime_t millisec
)
622 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
624 wxCHECK_MSG( hour
< 24 && second
< 62 && minute
< 60 && millisec
< 1000,
626 _T("Invalid time in wxDateTime::Set()") );
628 ReplaceDefaultYearMonthWithCurrent(&year
, &month
);
630 wxCHECK_MSG( (0 < day
) && (day
<= GetNumberOfDays(month
, year
)),
632 _T("Invalid date in wxDateTime::Set()") );
634 // the range of time_t type (inclusive)
635 static const int yearMinInRange
= 1970;
636 static const int yearMaxInRange
= 2037;
638 // test only the year instead of testing for the exact end of the Unix
639 // time_t range - it doesn't bring anything to do more precise checks
640 if ( year
>= yearMinInRange
&& year
<= yearMaxInRange
)
642 // use the standard library version if the date is in range - this is
643 // probably more efficient than our code
645 tm
.tm_year
= year
- 1900;
651 tm
.tm_isdst
= -1; // mktime() will guess it
655 // and finally adjust milliseconds
656 return SetMillisecond(millisec
);
660 // do time calculations ourselves: we want to calculate the number of
661 // milliseconds between the given date and the epoch
663 // get the JDN for the midnight of this day
664 m_time
= GetTruncatedJDN(day
, month
, year
);
666 m_time
*= SECONDS_PER_DAY
* TIME_T_FACTOR
;
668 // JDN corresponds to GMT, we take localtime
669 Add(wxTimeSpan(hour
, minute
, second
+ GetTimeZone(), millisec
));
675 wxDateTime
& wxDateTime::Set(double jdn
)
677 // so that m_time will be 0 for the midnight of Jan 1, 1970 which is jdn
679 jdn
-= EPOCH_JDN
+ 0.5;
682 m_time
*= MILLISECONDS_PER_DAY
;
687 // ----------------------------------------------------------------------------
688 // time_t <-> broken down time conversions
689 // ----------------------------------------------------------------------------
691 wxDateTime::Tm
wxDateTime::GetTm(const TimeZone
& tz
) const
693 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
695 time_t time
= GetTicks();
696 if ( time
!= (time_t)-1 )
698 // use C RTL functions
700 if ( tz
.GetOffset() == -GetTimeZone() )
702 // we are working with local time
703 tm
= localtime(&time
);
707 time
+= tz
.GetOffset();
711 // should never happen
712 wxCHECK_MSG( tm
, Tm(), _T("gmtime() failed") );
718 // remember the time and do the calculations with the date only - this
719 // eliminates rounding errors of the floating point arithmetics
721 wxLongLong timeMidnight
= m_time
+ tz
.GetOffset() * 1000;
723 long timeOnly
= (timeMidnight
% MILLISECONDS_PER_DAY
).ToLong();
725 // we want to always have positive time and timeMidnight to be really
726 // the midnight before it
729 timeOnly
= MILLISECONDS_PER_DAY
+ timeOnly
;
732 timeMidnight
-= timeOnly
;
734 // calculate the Gregorian date from JDN for the midnight of our date:
735 // this will yield day, month (in 1..12 range) and year
737 // actually, this is the JDN for the noon of the previous day
738 long jdn
= (timeMidnight
/ MILLISECONDS_PER_DAY
).ToLong() + EPOCH_JDN
;
740 // CREDIT: code below is by Scott E. Lee (but bugs are mine)
742 wxASSERT_MSG( jdn
> -2, _T("JDN out of range") );
744 // calculate the century
745 int temp
= (jdn
+ JDN_OFFSET
) * 4 - 1;
746 int century
= temp
/ DAYS_PER_400_YEARS
;
748 // then the year and day of year (1 <= dayOfYear <= 366)
749 temp
= ((temp
% DAYS_PER_400_YEARS
) / 4) * 4 + 3;
750 int year
= (century
* 100) + (temp
/ DAYS_PER_4_YEARS
);
751 int dayOfYear
= (temp
% DAYS_PER_4_YEARS
) / 4 + 1;
753 // and finally the month and day of the month
754 temp
= dayOfYear
* 5 - 3;
755 int month
= temp
/ DAYS_PER_5_MONTHS
;
756 int day
= (temp
% DAYS_PER_5_MONTHS
) / 5 + 1;
758 // month is counted from March - convert to normal
769 // year is offset by 4800
772 // check that the algorithm gave us something reasonable
773 wxASSERT_MSG( (0 < month
) && (month
<= 12), _T("invalid month") );
774 wxASSERT_MSG( (1 <= day
) && (day
< 32), _T("invalid day") );
775 wxASSERT_MSG( (INT_MIN
<= year
) && (year
<= INT_MAX
),
776 _T("year range overflow") );
778 // construct Tm from these values
781 tm
.mon
= (Month
)(month
- 1); // algorithm yields 1 for January, not 0
782 tm
.mday
= (wxDateTime_t
)day
;
783 tm
.msec
= timeOnly
% 1000;
785 timeOnly
/= 1000; // now we have time in seconds
787 tm
.sec
= timeOnly
% 60;
789 timeOnly
/= 60; // now we have time in minutes
791 tm
.min
= timeOnly
% 60;
794 tm
.hour
= timeOnly
/ 60;
800 wxDateTime
& wxDateTime::SetYear(int year
)
802 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
811 wxDateTime
& wxDateTime::SetMonth(Month month
)
813 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
822 wxDateTime
& wxDateTime::SetDay(wxDateTime_t mday
)
824 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
833 wxDateTime
& wxDateTime::SetHour(wxDateTime_t hour
)
835 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
844 wxDateTime
& wxDateTime::SetMinute(wxDateTime_t min
)
846 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
855 wxDateTime
& wxDateTime::SetSecond(wxDateTime_t sec
)
857 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
866 wxDateTime
& wxDateTime::SetMillisecond(wxDateTime_t millisecond
)
868 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
870 // we don't need to use GetTm() for this one
871 m_time
-= m_time
% 1000l;
872 m_time
+= millisecond
;
877 // ----------------------------------------------------------------------------
878 // wxDateTime arithmetics
879 // ----------------------------------------------------------------------------
881 wxDateTime
& wxDateTime::Add(const wxDateSpan
& diff
)
885 tm
.year
+= diff
.GetYears();
886 tm
.AddMonths(diff
.GetMonths());
887 tm
.AddDays(diff
.GetTotalDays());
894 // ----------------------------------------------------------------------------
895 // Weekday and monthday stuff
896 // ----------------------------------------------------------------------------
898 wxDateTime
& wxDateTime::SetToLastMonthDay(Month month
,
901 // take the current month/year if none specified
902 ReplaceDefaultYearMonthWithCurrent(&year
, &month
);
904 return Set(GetNumOfDaysInMonth(year
, month
), month
, year
);
907 bool wxDateTime::SetToWeekDay(WeekDay weekday
,
912 wxCHECK_MSG( weekday
!= Inv_WeekDay
, FALSE
, _T("invalid weekday") );
914 // we don't check explicitly that -5 <= n <= 5 because we will return FALSE
915 // anyhow in such case - but may be should still give an assert for it?
917 // take the current month/year if none specified
918 ReplaceDefaultYearMonthWithCurrent(&year
, &month
);
922 // TODO this probably could be optimised somehow...
926 // get the first day of the month
927 dt
.Set(1, month
, year
);
930 WeekDay wdayFirst
= dt
.GetWeekDay();
932 // go to the first weekday of the month
933 int diff
= weekday
- wdayFirst
;
937 // add advance n-1 weeks more
940 dt
-= wxDateSpan::Days(diff
);
944 // get the last day of the month
945 dt
.SetToLastMonthDay(month
, year
);
948 WeekDay wdayLast
= dt
.GetWeekDay();
950 // go to the last weekday of the month
951 int diff
= wdayLast
- weekday
;
955 // and rewind n-1 weeks from there
958 dt
-= wxDateSpan::Days(diff
);
961 // check that it is still in the same month
962 if ( dt
.GetMonth() == month
)
970 // no such day in this month
975 // ----------------------------------------------------------------------------
976 // Julian day number conversion and related stuff
977 // ----------------------------------------------------------------------------
979 double wxDateTime::GetJulianDayNumber() const
981 // JDN are always expressed for the GMT dates
982 Tm
tm(ToTimezone(GMT0
).GetTm(GMT0
));
984 double result
= GetTruncatedJDN(tm
.mday
, tm
.mon
, tm
.year
);
986 // add the part GetTruncatedJDN() neglected
989 // and now add the time: 86400 sec = 1 JDN
990 return result
+ ((double)(60*(60*tm
.hour
+ tm
.min
) + tm
.sec
)) / 86400;
993 double wxDateTime::GetRataDie() const
995 // March 1 of the year 0 is Rata Die day -306 and JDN 1721119.5
996 return GetJulianDayNumber() - 1721119.5 - 306;
999 // ----------------------------------------------------------------------------
1000 // timezone and DST stuff
1001 // ----------------------------------------------------------------------------
1003 int wxDateTime::IsDST(wxDateTime::Country country
) const
1005 wxCHECK_MSG( country
== Country_Default
, -1,
1006 _T("country support not implemented") );
1008 // use the C RTL for the dates in the standard range
1009 time_t timet
= GetTicks();
1010 if ( timet
!= (time_t)-1 )
1012 tm
*tm
= localtime(&timet
);
1014 wxCHECK_MSG( tm
, -1, _T("localtime() failed") );
1016 return tm
->tm_isdst
;
1020 // wxFAIL_MSG( _T("TODO") );
1026 wxDateTime
& wxDateTime::MakeTimezone(const TimeZone
& tz
)
1028 int secDiff
= GetTimeZone() + tz
.GetOffset();
1030 // we need to know whether DST is or not in effect for this date
1033 // FIXME we assume that the DST is always shifted by 1 hour
1037 return Substract(wxTimeSpan::Seconds(secDiff
));
1040 // ----------------------------------------------------------------------------
1041 // wxDateTime to/from text representations
1042 // ----------------------------------------------------------------------------
1044 wxString
wxDateTime::Format(const wxChar
*format
, const TimeZone
& tz
) const
1046 wxCHECK_MSG( format
, _T(""), _T("NULL format in wxDateTime::Format") );
1048 time_t time
= GetTicks();
1049 if ( time
!= (time_t)-1 )
1053 if ( tz
.GetOffset() == -GetTimeZone() )
1055 // we are working with local time
1056 tm
= localtime(&time
);
1060 time
+= tz
.GetOffset();
1065 // should never happen
1066 wxCHECK_MSG( tm
, _T(""), _T("gmtime() failed") );
1068 return CallStrftime(format
, tm
);
1072 // use a hack and still use strftime(): make a copy of the format and
1073 // replace all occurences of YEAR in it with some unique string not
1074 // appearing anywhere else in it, then use strftime() to format the
1075 // date in year YEAR and then replace YEAR back by the real year and
1076 // the unique replacement string back with YEAR where YEAR is any year
1077 // in the range supported by strftime() (1970 - 2037) which is equal to
1078 // the real year modulo 28 (so the week days coincide for them)
1081 int yearReal
= GetYear(tz
);
1082 int year
= 1970 + yearReal
% 28;
1085 strYear
.Printf(_T("%d"), year
);
1087 // find a string not occuring in format (this is surely not optimal way
1088 // of doing it... improvements welcome!)
1089 wxString fmt
= format
;
1090 wxString replacement
= (wxChar
)-1;
1091 while ( fmt
.Find(replacement
) != wxNOT_FOUND
)
1093 replacement
<< (wxChar
)-1;
1096 // replace all occurences of year with it
1097 bool wasReplaced
= fmt
.Replace(strYear
, replacement
) > 0;
1099 // use strftime() to format the same date but in supported year
1100 wxDateTime
dt(*this);
1102 wxString str
= dt
.Format(format
, tz
);
1104 // now replace the occurence of 1999 with the real year
1105 wxString strYearReal
;
1106 strYearReal
.Printf(_T("%d"), yearReal
);
1107 str
.Replace(strYear
, strYearReal
);
1109 // and replace back all occurences of replacement string
1111 str
.Replace(replacement
, strYear
);
1117 // ============================================================================
1119 // ============================================================================
1121 // not all strftime(3) format specifiers make sense here because, for example,
1122 // a time span doesn't have a year nor a timezone
1124 // Here are the ones which are supported (all of them are supported by strftime
1126 // %H hour in 24 hour format
1127 // %M minute (00 - 59)
1128 // %S second (00 - 59)
1131 // Also, for MFC CTimeSpan compatibility, we support
1132 // %D number of days
1134 // And, to be better than MFC :-), we also have
1135 // %E number of wEeks
1136 // %l milliseconds (000 - 999)
1137 wxString
wxTimeSpan::Format(const wxChar
*format
) const
1139 wxCHECK_MSG( format
, _T(""), _T("NULL format in wxTimeSpan::Format") );
1142 str
.Alloc(strlen(format
));
1144 for ( const wxChar
*pch
= format
; pch
; pch
++ )
1156 wxFAIL_MSG( _T("invalid format character") );
1160 // will get to str << ch below
1164 tmp
.Printf(_T("%d"), GetDays());
1168 tmp
.Printf(_T("%d"), GetWeeks());
1172 tmp
.Printf(_T("%02d"), GetHours());
1176 tmp
.Printf(_T("%03d"), GetMilliseconds());
1180 tmp
.Printf(_T("%02d"), GetMinutes());
1184 tmp
.Printf(_T("%02d"), GetSeconds());
1192 // skip str += ch below