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"
79 #define WX_TIMEZONE timezone
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
87 static const int MONTHS_IN_YEAR
= 12;
89 static const int SECONDS_IN_MINUTE
= 60;
91 static const long SECONDS_PER_DAY
= 86400l;
93 static const long MILLISECONDS_PER_DAY
= 86400000l;
95 // this is the integral part of JDN of the midnight of Jan 1, 1970
96 // (i.e. JDN(Jan 1, 1970) = 2440587.5)
97 static const int EPOCH_JDN
= 2440587;
99 // the date of JDN -0.5 (as we don't work with fractional parts, this is the
100 // reference date for us) is Nov 24, 4714BC
101 static const int JDN_0_YEAR
= -4713;
102 static const int JDN_0_MONTH
= wxDateTime::Nov
;
103 static const int JDN_0_DAY
= 24;
105 // the constants used for JDN calculations
106 static const int JDN_OFFSET
= 32046;
107 static const int DAYS_PER_5_MONTHS
= 153;
108 static const int DAYS_PER_4_YEARS
= 1461;
109 static const int DAYS_PER_400_YEARS
= 146097;
111 // ----------------------------------------------------------------------------
113 // ----------------------------------------------------------------------------
115 // a critical section is needed to protect GetTimeZone() static
116 // variable in MT case
118 wxCriticalSection gs_critsectTimezone
;
119 #endif // wxUSE_THREADS
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 // get the number of days in the given month of the given year
127 wxDateTime::wxDateTime_t
GetNumOfDaysInMonth(int year
, wxDateTime::Month month
)
129 // the number of days in month in Julian/Gregorian calendar: the first line
130 // is for normal years, the second one is for the leap ones
131 static wxDateTime::wxDateTime_t daysInMonth
[2][MONTHS_IN_YEAR
] =
133 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
134 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
137 return daysInMonth
[wxDateTime::IsLeapYear(year
)][month
];
140 // ensure that the timezone variable is set by calling localtime
141 static int GetTimeZone()
143 // set to TRUE when the timezone is set
144 static bool s_timezoneSet
= FALSE
;
146 wxCRIT_SECT_LOCKER(lock
, gs_critsectTimezone
);
148 if ( !s_timezoneSet
)
150 // just call localtime() instead of figuring out whether this system
151 // supports tzset(), _tzset() or something else
155 s_timezoneSet
= TRUE
;
158 return (int)WX_TIMEZONE
;
161 // return the integral part of the JDN for the midnight of the given date (to
162 // get the real JDN you need to add 0.5, this is, in fact, JDN of the
163 // noon of the previous day)
164 static long GetTruncatedJDN(wxDateTime::wxDateTime_t day
,
165 wxDateTime::Month mon
,
168 // CREDIT: code below is by Scott E. Lee (but bugs are mine)
170 // check the date validity
172 (year
> JDN_0_YEAR
) ||
173 ((year
== JDN_0_YEAR
) && (mon
> JDN_0_MONTH
)) ||
174 ((year
== JDN_0_YEAR
) && (mon
== JDN_0_MONTH
) && (day
>= JDN_0_DAY
)),
175 _T("date out of range - can't convert to JDN")
178 // make the year positive to avoid problems with negative numbers division
181 // months are counted from March here
183 if ( mon
>= wxDateTime::Mar
)
193 // now we can simply add all the contributions together
194 return ((year
/ 100) * DAYS_PER_400_YEARS
) / 4
195 + ((year
% 100) * DAYS_PER_4_YEARS
) / 4
196 + (month
* DAYS_PER_5_MONTHS
+ 2) / 5
201 // this function is a wrapper around strftime(3)
202 static wxString
CallStrftime(const wxChar
*format
, const tm
* tm
)
205 if ( !wxStrftime(buf
, WXSIZEOF(buf
), format
, tm
) )
207 // is ti really possible that 1024 is too short?
208 wxFAIL_MSG(_T("strftime() failed"));
211 return wxString(buf
);
214 // if year and/or month have invalid values, replace them with the current ones
215 static void ReplaceDefaultYearMonthWithCurrent(int *year
,
216 wxDateTime::Month
*month
)
218 struct tm
*tmNow
= NULL
;
220 if ( *year
== wxDateTime::Inv_Year
)
222 tmNow
= wxDateTime::GetTmNow();
224 *year
= 1900 + tmNow
->tm_year
;
227 if ( *month
== wxDateTime::Inv_Month
)
230 tmNow
= wxDateTime::GetTmNow();
232 *month
= (wxDateTime::Month
)tmNow
->tm_mon
;
236 // ============================================================================
237 // implementation of wxDateTime
238 // ============================================================================
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
244 wxDateTime::Country
wxDateTime::ms_country
= wxDateTime::Country_Unknown
;
245 wxDateTime
wxDateTime::ms_InvDateTime
;
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
253 year
= (wxDateTime_t
)wxDateTime::Inv_Year
;
254 mon
= wxDateTime::Inv_Month
;
256 hour
= min
= sec
= msec
= 0;
257 wday
= wxDateTime::Inv_WeekDay
;
260 wxDateTime::Tm::Tm(const struct tm
& tm
, const TimeZone
& tz
)
268 mon
= (wxDateTime::Month
)tm
.tm_mon
;
269 year
= 1900 + tm
.tm_year
;
274 bool wxDateTime::Tm::IsValid() const
276 // we allow for the leap seconds, although we don't use them (yet)
277 return (year
!= wxDateTime::Inv_Year
) && (mon
!= wxDateTime::Inv_Month
) &&
278 (mday
< GetNumOfDaysInMonth(year
, mon
)) &&
279 (hour
< 24) && (min
< 60) && (sec
< 62) && (msec
< 1000);
282 void wxDateTime::Tm::ComputeWeekDay()
284 wxFAIL_MSG(_T("TODO"));
287 void wxDateTime::Tm::AddMonths(int monDiff
)
289 // normalize the months field
290 while ( monDiff
< -mon
)
294 monDiff
+= MONTHS_IN_YEAR
;
297 while ( monDiff
+ mon
> MONTHS_IN_YEAR
)
302 mon
= (wxDateTime::Month
)(mon
+ monDiff
);
304 wxASSERT_MSG( mon
>= 0 && mon
< MONTHS_IN_YEAR
, _T("logic error") );
307 void wxDateTime::Tm::AddDays(int dayDiff
)
309 // normalize the days field
315 mday
+= GetNumOfDaysInMonth(year
, mon
);
318 while ( mday
> GetNumOfDaysInMonth(year
, mon
) )
320 mday
-= GetNumOfDaysInMonth(year
, mon
);
325 wxASSERT_MSG( mday
> 0 && mday
<= GetNumOfDaysInMonth(year
, mon
),
329 // ----------------------------------------------------------------------------
331 // ----------------------------------------------------------------------------
333 wxDateTime::TimeZone::TimeZone(wxDateTime::TZ tz
)
337 case wxDateTime::Local
:
338 // get the offset from C RTL: it returns the difference GMT-local
339 // while we want to have the offset _from_ GMT, hence the '-'
340 m_offset
= -GetTimeZone();
343 case wxDateTime::GMT_12
:
344 case wxDateTime::GMT_11
:
345 case wxDateTime::GMT_10
:
346 case wxDateTime::GMT_9
:
347 case wxDateTime::GMT_8
:
348 case wxDateTime::GMT_7
:
349 case wxDateTime::GMT_6
:
350 case wxDateTime::GMT_5
:
351 case wxDateTime::GMT_4
:
352 case wxDateTime::GMT_3
:
353 case wxDateTime::GMT_2
:
354 case wxDateTime::GMT_1
:
355 m_offset
= -3600*(wxDateTime::GMT0
- tz
);
358 case wxDateTime::GMT0
:
359 case wxDateTime::GMT1
:
360 case wxDateTime::GMT2
:
361 case wxDateTime::GMT3
:
362 case wxDateTime::GMT4
:
363 case wxDateTime::GMT5
:
364 case wxDateTime::GMT6
:
365 case wxDateTime::GMT7
:
366 case wxDateTime::GMT8
:
367 case wxDateTime::GMT9
:
368 case wxDateTime::GMT10
:
369 case wxDateTime::GMT11
:
370 case wxDateTime::GMT12
:
371 m_offset
= 3600*(tz
- wxDateTime::GMT0
);
374 case wxDateTime::A_CST
:
375 // Central Standard Time in use in Australia = UTC + 9.5
376 m_offset
= 60*(9*60 + 30);
380 wxFAIL_MSG( _T("unknown time zone") );
384 // ----------------------------------------------------------------------------
386 // ----------------------------------------------------------------------------
389 bool wxDateTime::IsLeapYear(int year
, wxDateTime::Calendar cal
)
391 if ( year
== Inv_Year
)
392 year
= GetCurrentYear();
394 if ( cal
== Gregorian
)
396 // in Gregorian calendar leap years are those divisible by 4 except
397 // those divisible by 100 unless they're also divisible by 400
398 // (in some countries, like Russia and Greece, additional corrections
399 // exist, but they won't manifest themselves until 2700)
400 return (year
% 4 == 0) && ((year
% 100 != 0) || (year
% 400 == 0));
402 else if ( cal
== Julian
)
404 // in Julian calendar the rule is simpler
405 return year
% 4 == 0;
409 wxFAIL_MSG(_T("unknown calendar"));
416 int wxDateTime::GetCentury(int year
)
418 return year
> 0 ? year
/ 100 : year
/ 100 - 1;
422 void wxDateTime::SetCountry(wxDateTime::Country country
)
424 ms_country
= country
;
428 int wxDateTime::ConvertYearToBC(int year
)
431 return year
> 0 ? year
: year
- 1;
435 int wxDateTime::GetCurrentYear(wxDateTime::Calendar cal
)
440 return Now().GetYear();
443 wxFAIL_MSG(_T("TODO"));
447 wxFAIL_MSG(_T("unsupported calendar"));
455 wxDateTime::Month
wxDateTime::GetCurrentMonth(wxDateTime::Calendar cal
)
460 return Now().GetMonth();
464 wxFAIL_MSG(_T("TODO"));
468 wxFAIL_MSG(_T("unsupported calendar"));
476 wxDateTime::wxDateTime_t
wxDateTime::GetNumberOfDays(int year
, Calendar cal
)
478 if ( year
== Inv_Year
)
480 // take the current year if none given
481 year
= GetCurrentYear();
488 return IsLeapYear(year
) ? 366 : 365;
492 wxFAIL_MSG(_T("unsupported calendar"));
500 wxDateTime::wxDateTime_t
wxDateTime::GetNumberOfDays(wxDateTime::Month month
,
502 wxDateTime::Calendar cal
)
504 wxCHECK_MSG( month
< MONTHS_IN_YEAR
, 0, _T("invalid month") );
506 if ( cal
== Gregorian
|| cal
== Julian
)
508 if ( year
== Inv_Year
)
510 // take the current year if none given
511 year
= GetCurrentYear();
514 return GetNumOfDaysInMonth(year
, month
);
518 wxFAIL_MSG(_T("unsupported calendar"));
525 wxString
wxDateTime::GetMonthName(wxDateTime::Month month
, bool abbr
)
527 wxCHECK_MSG( month
!= Inv_Month
, _T(""), _T("invalid month") );
529 tm tm
= { 0, 0, 0, 1, month
, 76 }; // any year will do
531 return CallStrftime(abbr
? _T("%b") : _T("%B"), &tm
);
535 wxString
wxDateTime::GetWeekDayName(wxDateTime::WeekDay wday
, bool abbr
)
537 wxCHECK_MSG( wday
!= Inv_WeekDay
, _T(""), _T("invalid weekday") );
539 // take some arbitrary Sunday
540 tm tm
= { 0, 0, 0, 28, Nov
, 99 };
542 // and offset it by the number of days needed to get the correct wday
545 return CallStrftime(abbr
? _T("%a") : _T("%A"), &tm
);
548 // ----------------------------------------------------------------------------
549 // constructors and assignment operators
550 // ----------------------------------------------------------------------------
552 // the values in the tm structure contain the local time
553 wxDateTime
& wxDateTime::Set(const struct tm
& tm
)
555 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
558 time_t timet
= mktime(&tm2
);
560 if ( timet
== (time_t)-1 )
562 // mktime() rather unintuitively fails for Jan 1, 1970 if the hour is
563 // less than timezone - try to make it work for this case
564 if ( tm2
.tm_year
== 70 && tm2
.tm_mon
== 0 && tm2
.tm_mday
== 1 )
566 // add timezone to make sure that date is in range
567 tm2
.tm_sec
-= GetTimeZone();
569 timet
= mktime(&tm2
);
570 if ( timet
!= (time_t)-1 )
572 timet
+= GetTimeZone();
578 wxFAIL_MSG( _T("mktime() failed") );
580 return ms_InvDateTime
;
588 wxDateTime
& wxDateTime::Set(wxDateTime_t hour
,
591 wxDateTime_t millisec
)
593 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
595 // we allow seconds to be 61 to account for the leap seconds, even if we
596 // don't use them really
597 wxCHECK_MSG( hour
< 24 && second
< 62 && minute
< 60 && millisec
< 1000,
599 _T("Invalid time in wxDateTime::Set()") );
601 // get the current date from system
602 time_t timet
= GetTimeNow();
603 struct tm
*tm
= localtime(&timet
);
605 wxCHECK_MSG( tm
, ms_InvDateTime
, _T("localtime() failed") );
614 // and finally adjust milliseconds
615 return SetMillisecond(millisec
);
618 wxDateTime
& wxDateTime::Set(wxDateTime_t day
,
624 wxDateTime_t millisec
)
626 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
628 wxCHECK_MSG( hour
< 24 && second
< 62 && minute
< 60 && millisec
< 1000,
630 _T("Invalid time in wxDateTime::Set()") );
632 ReplaceDefaultYearMonthWithCurrent(&year
, &month
);
634 wxCHECK_MSG( (0 < day
) && (day
<= GetNumberOfDays(month
, year
)),
636 _T("Invalid date in wxDateTime::Set()") );
638 // the range of time_t type (inclusive)
639 static const int yearMinInRange
= 1970;
640 static const int yearMaxInRange
= 2037;
642 // test only the year instead of testing for the exact end of the Unix
643 // time_t range - it doesn't bring anything to do more precise checks
644 if ( year
>= yearMinInRange
&& year
<= yearMaxInRange
)
646 // use the standard library version if the date is in range - this is
647 // probably more efficient than our code
649 tm
.tm_year
= year
- 1900;
655 tm
.tm_isdst
= -1; // mktime() will guess it
659 // and finally adjust milliseconds
660 return SetMillisecond(millisec
);
664 // do time calculations ourselves: we want to calculate the number of
665 // milliseconds between the given date and the epoch
667 // get the JDN for the midnight of this day
668 m_time
= GetTruncatedJDN(day
, month
, year
);
670 m_time
*= SECONDS_PER_DAY
* TIME_T_FACTOR
;
672 // JDN corresponds to GMT, we take localtime
673 Add(wxTimeSpan(hour
, minute
, second
+ GetTimeZone(), millisec
));
679 wxDateTime
& wxDateTime::Set(double jdn
)
681 // so that m_time will be 0 for the midnight of Jan 1, 1970 which is jdn
683 jdn
-= EPOCH_JDN
+ 0.5;
686 m_time
*= MILLISECONDS_PER_DAY
;
691 // ----------------------------------------------------------------------------
692 // time_t <-> broken down time conversions
693 // ----------------------------------------------------------------------------
695 wxDateTime::Tm
wxDateTime::GetTm(const TimeZone
& tz
) const
697 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
699 time_t time
= GetTicks();
700 if ( time
!= (time_t)-1 )
702 // use C RTL functions
704 if ( tz
.GetOffset() == -GetTimeZone() )
706 // we are working with local time
707 tm
= localtime(&time
);
711 time
+= tz
.GetOffset();
715 // should never happen
716 wxCHECK_MSG( tm
, Tm(), _T("gmtime() failed") );
722 // remember the time and do the calculations with the date only - this
723 // eliminates rounding errors of the floating point arithmetics
725 wxLongLong timeMidnight
= m_time
+ tz
.GetOffset() * 1000;
727 long timeOnly
= (timeMidnight
% MILLISECONDS_PER_DAY
).ToLong();
729 // we want to always have positive time and timeMidnight to be really
730 // the midnight before it
733 timeOnly
= MILLISECONDS_PER_DAY
+ timeOnly
;
736 timeMidnight
-= timeOnly
;
738 // calculate the Gregorian date from JDN for the midnight of our date:
739 // this will yield day, month (in 1..12 range) and year
741 // actually, this is the JDN for the noon of the previous day
742 long jdn
= (timeMidnight
/ MILLISECONDS_PER_DAY
).ToLong() + EPOCH_JDN
;
744 // CREDIT: code below is by Scott E. Lee (but bugs are mine)
746 wxASSERT_MSG( jdn
> -2, _T("JDN out of range") );
748 // calculate the century
749 int temp
= (jdn
+ JDN_OFFSET
) * 4 - 1;
750 int century
= temp
/ DAYS_PER_400_YEARS
;
752 // then the year and day of year (1 <= dayOfYear <= 366)
753 temp
= ((temp
% DAYS_PER_400_YEARS
) / 4) * 4 + 3;
754 int year
= (century
* 100) + (temp
/ DAYS_PER_4_YEARS
);
755 int dayOfYear
= (temp
% DAYS_PER_4_YEARS
) / 4 + 1;
757 // and finally the month and day of the month
758 temp
= dayOfYear
* 5 - 3;
759 int month
= temp
/ DAYS_PER_5_MONTHS
;
760 int day
= (temp
% DAYS_PER_5_MONTHS
) / 5 + 1;
762 // month is counted from March - convert to normal
773 // year is offset by 4800
776 // check that the algorithm gave us something reasonable
777 wxASSERT_MSG( (0 < month
) && (month
<= 12), _T("invalid month") );
778 wxASSERT_MSG( (1 <= day
) && (day
< 32), _T("invalid day") );
779 wxASSERT_MSG( (INT_MIN
<= year
) && (year
<= INT_MAX
),
780 _T("year range overflow") );
782 // construct Tm from these values
785 tm
.mon
= (Month
)(month
- 1); // algorithm yields 1 for January, not 0
786 tm
.mday
= (wxDateTime_t
)day
;
787 tm
.msec
= timeOnly
% 1000;
789 timeOnly
/= 1000; // now we have time in seconds
791 tm
.sec
= timeOnly
% 60;
793 timeOnly
/= 60; // now we have time in minutes
795 tm
.min
= timeOnly
% 60;
798 tm
.hour
= timeOnly
/ 60;
804 wxDateTime
& wxDateTime::SetYear(int year
)
806 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
815 wxDateTime
& wxDateTime::SetMonth(Month month
)
817 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
826 wxDateTime
& wxDateTime::SetDay(wxDateTime_t mday
)
828 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
837 wxDateTime
& wxDateTime::SetHour(wxDateTime_t hour
)
839 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
848 wxDateTime
& wxDateTime::SetMinute(wxDateTime_t min
)
850 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
859 wxDateTime
& wxDateTime::SetSecond(wxDateTime_t sec
)
861 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
870 wxDateTime
& wxDateTime::SetMillisecond(wxDateTime_t millisecond
)
872 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
874 // we don't need to use GetTm() for this one
875 m_time
-= m_time
% 1000l;
876 m_time
+= millisecond
;
881 // ----------------------------------------------------------------------------
882 // wxDateTime arithmetics
883 // ----------------------------------------------------------------------------
885 wxDateTime
& wxDateTime::Add(const wxDateSpan
& diff
)
889 tm
.year
+= diff
.GetYears();
890 tm
.AddMonths(diff
.GetMonths());
891 tm
.AddDays(diff
.GetTotalDays());
898 // ----------------------------------------------------------------------------
899 // Weekday and monthday stuff
900 // ----------------------------------------------------------------------------
902 wxDateTime
& wxDateTime::SetToLastMonthDay(Month month
,
905 // take the current month/year if none specified
906 ReplaceDefaultYearMonthWithCurrent(&year
, &month
);
908 return Set(GetNumOfDaysInMonth(year
, month
), month
, year
);
911 bool wxDateTime::SetToWeekDay(WeekDay weekday
,
916 wxCHECK_MSG( weekday
!= Inv_WeekDay
, FALSE
, _T("invalid weekday") );
918 // we don't check explicitly that -5 <= n <= 5 because we will return FALSE
919 // anyhow in such case - but may be should still give an assert for it?
921 // take the current month/year if none specified
922 ReplaceDefaultYearMonthWithCurrent(&year
, &month
);
926 // TODO this probably could be optimised somehow...
930 // get the first day of the month
931 dt
.Set(1, month
, year
);
934 WeekDay wdayFirst
= dt
.GetWeekDay();
936 // go to the first weekday of the month
937 int diff
= weekday
- wdayFirst
;
941 // add advance n-1 weeks more
944 dt
-= wxDateSpan::Days(diff
);
948 // get the last day of the month
949 dt
.SetToLastMonthDay(month
, year
);
952 WeekDay wdayLast
= dt
.GetWeekDay();
954 // go to the last weekday of the month
955 int diff
= wdayLast
- weekday
;
959 // and rewind n-1 weeks from there
962 dt
-= wxDateSpan::Days(diff
);
965 // check that it is still in the same month
966 if ( dt
.GetMonth() == month
)
974 // no such day in this month
979 // ----------------------------------------------------------------------------
980 // Julian day number conversion and related stuff
981 // ----------------------------------------------------------------------------
983 double wxDateTime::GetJulianDayNumber() const
985 // JDN are always expressed for the GMT dates
986 Tm
tm(ToTimezone(GMT0
).GetTm(GMT0
));
988 double result
= GetTruncatedJDN(tm
.mday
, tm
.mon
, tm
.year
);
990 // add the part GetTruncatedJDN() neglected
993 // and now add the time: 86400 sec = 1 JDN
994 return result
+ ((double)(60*(60*tm
.hour
+ tm
.min
) + tm
.sec
)) / 86400;
997 double wxDateTime::GetRataDie() const
999 // March 1 of the year 0 is Rata Die day -306 and JDN 1721119.5
1000 return GetJulianDayNumber() - 1721119.5 - 306;
1003 // ----------------------------------------------------------------------------
1004 // timezone and DST stuff
1005 // ----------------------------------------------------------------------------
1007 int wxDateTime::IsDST(wxDateTime::Country country
) const
1009 wxCHECK_MSG( country
== Country_Default
, -1,
1010 _T("country support not implemented") );
1012 // use the C RTL for the dates in the standard range
1013 time_t timet
= GetTicks();
1014 if ( timet
!= (time_t)-1 )
1016 tm
*tm
= localtime(&timet
);
1018 wxCHECK_MSG( tm
, -1, _T("localtime() failed") );
1020 return tm
->tm_isdst
;
1024 // wxFAIL_MSG( _T("TODO") );
1030 wxDateTime
& wxDateTime::MakeTimezone(const TimeZone
& tz
)
1032 int secDiff
= GetTimeZone() + tz
.GetOffset();
1034 // we need to know whether DST is or not in effect for this date
1037 // FIXME we assume that the DST is always shifted by 1 hour
1041 return Substract(wxTimeSpan::Seconds(secDiff
));
1044 // ----------------------------------------------------------------------------
1045 // wxDateTime to/from text representations
1046 // ----------------------------------------------------------------------------
1048 wxString
wxDateTime::Format(const wxChar
*format
, const TimeZone
& tz
) const
1050 wxCHECK_MSG( format
, _T(""), _T("NULL format in wxDateTime::Format") );
1052 time_t time
= GetTicks();
1053 if ( time
!= (time_t)-1 )
1057 if ( tz
.GetOffset() == -GetTimeZone() )
1059 // we are working with local time
1060 tm
= localtime(&time
);
1064 time
+= tz
.GetOffset();
1069 // should never happen
1070 wxCHECK_MSG( tm
, _T(""), _T("gmtime() failed") );
1072 return CallStrftime(format
, tm
);
1076 // use a hack and still use strftime(): make a copy of the format and
1077 // replace all occurences of YEAR in it with some unique string not
1078 // appearing anywhere else in it, then use strftime() to format the
1079 // date in year YEAR and then replace YEAR back by the real year and
1080 // the unique replacement string back with YEAR where YEAR is any year
1081 // in the range supported by strftime() (1970 - 2037) which is equal to
1082 // the real year modulo 28 (so the week days coincide for them)
1085 int yearReal
= GetYear(tz
);
1086 int year
= 1970 + yearReal
% 28;
1089 strYear
.Printf(_T("%d"), year
);
1091 // find a string not occuring in format (this is surely not optimal way
1092 // of doing it... improvements welcome!)
1093 wxString fmt
= format
;
1094 wxString replacement
= (wxChar
)-1;
1095 while ( fmt
.Find(replacement
) != wxNOT_FOUND
)
1097 replacement
<< (wxChar
)-1;
1100 // replace all occurences of year with it
1101 bool wasReplaced
= fmt
.Replace(strYear
, replacement
) > 0;
1103 // use strftime() to format the same date but in supported year
1104 wxDateTime
dt(*this);
1106 wxString str
= dt
.Format(format
, tz
);
1108 // now replace the occurence of 1999 with the real year
1109 wxString strYearReal
;
1110 strYearReal
.Printf(_T("%d"), yearReal
);
1111 str
.Replace(strYear
, strYearReal
);
1113 // and replace back all occurences of replacement string
1115 str
.Replace(replacement
, strYear
);
1121 // ============================================================================
1123 // ============================================================================
1125 // not all strftime(3) format specifiers make sense here because, for example,
1126 // a time span doesn't have a year nor a timezone
1128 // Here are the ones which are supported (all of them are supported by strftime
1130 // %H hour in 24 hour format
1131 // %M minute (00 - 59)
1132 // %S second (00 - 59)
1135 // Also, for MFC CTimeSpan compatibility, we support
1136 // %D number of days
1138 // And, to be better than MFC :-), we also have
1139 // %E number of wEeks
1140 // %l milliseconds (000 - 999)
1141 wxString
wxTimeSpan::Format(const wxChar
*format
) const
1143 wxCHECK_MSG( format
, _T(""), _T("NULL format in wxTimeSpan::Format") );
1146 str
.Alloc(strlen(format
));
1148 for ( const wxChar
*pch
= format
; pch
; pch
++ )
1160 wxFAIL_MSG( _T("invalid format character") );
1164 // will get to str << ch below
1168 tmp
.Printf(_T("%d"), GetDays());
1172 tmp
.Printf(_T("%d"), GetWeeks());
1176 tmp
.Printf(_T("%02d"), GetHours());
1180 tmp
.Printf(_T("%03d"), GetMilliseconds());
1184 tmp
.Printf(_T("%02d"), GetMinutes());
1188 tmp
.Printf(_T("%02d"), GetSeconds());
1196 // skip str += ch below