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"
66 #if !defined(wxUSE_DATETIME) || wxUSE_DATETIME
69 #include "wx/string.h"
74 #include "wx/thread.h"
75 #include "wx/tokenzr.h"
76 #include "wx/module.h"
78 #define wxDEFINE_TIME_CONSTANTS // before including datetime.h
82 #include "wx/datetime.h"
83 #include "wx/timer.h" // for wxGetLocalTimeMillis()
85 // ----------------------------------------------------------------------------
86 // conditional compilation
87 // ----------------------------------------------------------------------------
89 #if defined(HAVE_STRPTIME) && defined(__LINUX__)
90 // glibc 2.0.7 strptime() is broken - the following snippet causes it to
91 // crash (instead of just failing):
93 // strncpy(buf, "Tue Dec 21 20:25:40 1999", 128);
94 // strptime(buf, "%x", &tm);
98 #endif // broken strptime()
101 #if defined(__BORLANDC__) || defined(__MINGW32__) || defined(__VISAGECPP__)
102 #define WX_TIMEZONE _timezone
103 #elif defined(__MWERKS__)
104 long wxmw_timezone
= 28800;
105 #define WX_TIMEZONE wxmw_timezone;
106 #else // unknown platform - try timezone
107 #define WX_TIMEZONE timezone
109 #endif // !WX_TIMEZONE
111 // ----------------------------------------------------------------------------
113 // ----------------------------------------------------------------------------
115 // debugging helper: just a convenient replacement of wxCHECK()
116 #define wxDATETIME_CHECK(expr, msg) \
120 *this = wxInvalidDateTime; \
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 class wxDateTimeHolidaysModule
: public wxModule
131 virtual bool OnInit()
133 wxDateTimeHolidayAuthority::AddAuthority(new wxDateTimeWorkDays
);
138 virtual void OnExit()
140 wxDateTimeHolidayAuthority::ClearAllAuthorities();
141 wxDateTimeHolidayAuthority::ms_authorities
.Clear();
145 DECLARE_DYNAMIC_CLASS(wxDateTimeHolidaysModule
)
148 IMPLEMENT_DYNAMIC_CLASS(wxDateTimeHolidaysModule
, wxModule
)
150 // ----------------------------------------------------------------------------
152 // ----------------------------------------------------------------------------
155 static const int MONTHS_IN_YEAR
= 12;
157 static const int SEC_PER_MIN
= 60;
159 static const int MIN_PER_HOUR
= 60;
161 static const int HOURS_PER_DAY
= 24;
163 static const long SECONDS_PER_DAY
= 86400l;
165 static const int DAYS_PER_WEEK
= 7;
167 static const long MILLISECONDS_PER_DAY
= 86400000l;
169 // this is the integral part of JDN of the midnight of Jan 1, 1970
170 // (i.e. JDN(Jan 1, 1970) = 2440587.5)
171 static const long EPOCH_JDN
= 2440587l;
173 // the date of JDN -0.5 (as we don't work with fractional parts, this is the
174 // reference date for us) is Nov 24, 4714BC
175 static const int JDN_0_YEAR
= -4713;
176 static const int JDN_0_MONTH
= wxDateTime::Nov
;
177 static const int JDN_0_DAY
= 24;
179 // the constants used for JDN calculations
180 static const long JDN_OFFSET
= 32046l;
181 static const long DAYS_PER_5_MONTHS
= 153l;
182 static const long DAYS_PER_4_YEARS
= 1461l;
183 static const long DAYS_PER_400_YEARS
= 146097l;
185 // this array contains the cumulated number of days in all previous months for
186 // normal and leap years
187 static const wxDateTime::wxDateTime_t gs_cumulatedDays
[2][MONTHS_IN_YEAR
] =
189 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 },
190 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 }
193 // ----------------------------------------------------------------------------
195 // ----------------------------------------------------------------------------
197 // in the fine tradition of ANSI C we use our equivalent of (time_t)-1 to
198 // indicate an invalid wxDateTime object
200 static const wxDateTime gs_dtDefault
;
202 const wxDateTime
& wxDefaultDateTime
= gs_dtDefault
;
204 wxDateTime::Country
wxDateTime::ms_country
= wxDateTime::Country_Unknown
;
206 // ----------------------------------------------------------------------------
208 // ----------------------------------------------------------------------------
210 // a critical section is needed to protect GetTimeZone() static
211 // variable in MT case
213 static wxCriticalSection gs_critsectTimezone
;
214 #endif // wxUSE_THREADS
216 // ----------------------------------------------------------------------------
218 // ----------------------------------------------------------------------------
220 // debugger helper: shows what the date really is
222 extern const wxChar
*wxDumpDate(const wxDateTime
* dt
)
224 static wxChar buf
[128];
226 wxStrcpy(buf
, dt
->Format(_T("%Y-%m-%d (%a) %H:%M:%S")));
232 // get the number of days in the given month of the given year
234 wxDateTime::wxDateTime_t
GetNumOfDaysInMonth(int year
, wxDateTime::Month month
)
236 // the number of days in month in Julian/Gregorian calendar: the first line
237 // is for normal years, the second one is for the leap ones
238 static wxDateTime::wxDateTime_t daysInMonth
[2][MONTHS_IN_YEAR
] =
240 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
241 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
244 return daysInMonth
[wxDateTime::IsLeapYear(year
)][month
];
247 // ensure that the timezone variable is set by calling localtime
248 static int GetTimeZone()
250 // set to TRUE when the timezone is set
251 static bool s_timezoneSet
= FALSE
;
253 wxCRIT_SECT_LOCKER(lock
, gs_critsectTimezone
);
255 if ( !s_timezoneSet
)
257 // just call localtime() instead of figuring out whether this system
258 // supports tzset(), _tzset() or something else
262 s_timezoneSet
= TRUE
;
265 return (int)WX_TIMEZONE
;
268 // return the integral part of the JDN for the midnight of the given date (to
269 // get the real JDN you need to add 0.5, this is, in fact, JDN of the
270 // noon of the previous day)
271 static long GetTruncatedJDN(wxDateTime::wxDateTime_t day
,
272 wxDateTime::Month mon
,
275 // CREDIT: code below is by Scott E. Lee (but bugs are mine)
277 // check the date validity
279 (year
> JDN_0_YEAR
) ||
280 ((year
== JDN_0_YEAR
) && (mon
> JDN_0_MONTH
)) ||
281 ((year
== JDN_0_YEAR
) && (mon
== JDN_0_MONTH
) && (day
>= JDN_0_DAY
)),
282 _T("date out of range - can't convert to JDN")
285 // make the year positive to avoid problems with negative numbers division
288 // months are counted from March here
290 if ( mon
>= wxDateTime::Mar
)
300 // now we can simply add all the contributions together
301 return ((year
/ 100) * DAYS_PER_400_YEARS
) / 4
302 + ((year
% 100) * DAYS_PER_4_YEARS
) / 4
303 + (month
* DAYS_PER_5_MONTHS
+ 2) / 5
308 // this function is a wrapper around strftime(3)
309 static wxString
CallStrftime(const wxChar
*format
, const tm
* tm
)
312 if ( !wxStrftime(buf
, WXSIZEOF(buf
), format
, tm
) )
314 // buffer is too small?
315 wxFAIL_MSG(_T("strftime() failed"));
318 return wxString(buf
);
321 // if year and/or month have invalid values, replace them with the current ones
322 static void ReplaceDefaultYearMonthWithCurrent(int *year
,
323 wxDateTime::Month
*month
)
325 struct tm
*tmNow
= NULL
;
327 if ( *year
== wxDateTime::Inv_Year
)
329 tmNow
= wxDateTime::GetTmNow();
331 *year
= 1900 + tmNow
->tm_year
;
334 if ( *month
== wxDateTime::Inv_Month
)
337 tmNow
= wxDateTime::GetTmNow();
339 *month
= (wxDateTime::Month
)tmNow
->tm_mon
;
343 // fll the struct tm with default values
344 static void InitTm(struct tm
& tm
)
346 // struct tm may have etxra fields (undocumented and with unportable
347 // names) which, nevertheless, must be set to 0
348 memset(&tm
, 0, sizeof(struct tm
));
350 tm
.tm_mday
= 1; // mday 0 is invalid
351 tm
.tm_year
= 76; // any valid year
352 tm
.tm_isdst
= -1; // auto determine
358 // return the month if the string is a month name or Inv_Month otherwise
359 static wxDateTime::Month
GetMonthFromName(const wxString
& name
, int flags
)
361 wxDateTime::Month mon
;
362 for ( mon
= wxDateTime::Jan
; mon
< wxDateTime::Inv_Month
; wxNextMonth(mon
) )
364 // case-insensitive comparison either one of or with both abbreviated
366 if ( flags
& wxDateTime::Name_Full
)
368 if ( name
.CmpNoCase(wxDateTime::
369 GetMonthName(mon
, wxDateTime::Name_Full
)) == 0 )
375 if ( flags
& wxDateTime::Name_Abbr
)
377 if ( name
.CmpNoCase(wxDateTime::
378 GetMonthName(mon
, wxDateTime::Name_Abbr
)) == 0 )
388 // return the weekday if the string is a weekday name or Inv_WeekDay otherwise
389 static wxDateTime::WeekDay
GetWeekDayFromName(const wxString
& name
, int flags
)
391 wxDateTime::WeekDay wd
;
392 for ( wd
= wxDateTime::Sun
; wd
< wxDateTime::Inv_WeekDay
; wxNextWDay(wd
) )
394 // case-insensitive comparison either one of or with both abbreviated
396 if ( flags
& wxDateTime::Name_Full
)
398 if ( name
.CmpNoCase(wxDateTime::
399 GetWeekDayName(wd
, wxDateTime::Name_Full
)) == 0 )
405 if ( flags
& wxDateTime::Name_Abbr
)
407 if ( name
.CmpNoCase(wxDateTime::
408 GetWeekDayName(wd
, wxDateTime::Name_Abbr
)) == 0 )
418 // scans all digits (but no more than len) and returns the resulting number
419 static bool GetNumericToken(size_t len
, const wxChar
*& p
, unsigned long *number
)
423 while ( wxIsdigit(*p
) )
427 if ( len
&& ++n
> len
)
431 return !!s
&& s
.ToULong(number
);
434 // scans all alphabetic characters and returns the resulting string
435 static wxString
GetAlphaToken(const wxChar
*& p
)
438 while ( wxIsalpha(*p
) )
446 // ============================================================================
447 // implementation of wxDateTime
448 // ============================================================================
450 // ----------------------------------------------------------------------------
452 // ----------------------------------------------------------------------------
456 year
= (wxDateTime_t
)wxDateTime::Inv_Year
;
457 mon
= wxDateTime::Inv_Month
;
459 hour
= min
= sec
= msec
= 0;
460 wday
= wxDateTime::Inv_WeekDay
;
463 wxDateTime::Tm::Tm(const struct tm
& tm
, const TimeZone
& tz
)
471 mon
= (wxDateTime::Month
)tm
.tm_mon
;
472 year
= 1900 + tm
.tm_year
;
477 bool wxDateTime::Tm::IsValid() const
479 // we allow for the leap seconds, although we don't use them (yet)
480 return (year
!= wxDateTime::Inv_Year
) && (mon
!= wxDateTime::Inv_Month
) &&
481 (mday
<= GetNumOfDaysInMonth(year
, mon
)) &&
482 (hour
< 24) && (min
< 60) && (sec
< 62) && (msec
< 1000);
485 void wxDateTime::Tm::ComputeWeekDay()
487 // compute the week day from day/month/year: we use the dumbest algorithm
488 // possible: just compute our JDN and then use the (simple to derive)
489 // formula: weekday = (JDN + 1.5) % 7
490 wday
= (wxDateTime::WeekDay
)(GetTruncatedJDN(mday
, mon
, year
) + 2) % 7;
493 void wxDateTime::Tm::AddMonths(int monDiff
)
495 // normalize the months field
496 while ( monDiff
< -mon
)
500 monDiff
+= MONTHS_IN_YEAR
;
503 while ( monDiff
+ mon
>= MONTHS_IN_YEAR
)
507 monDiff
-= MONTHS_IN_YEAR
;
510 mon
= (wxDateTime::Month
)(mon
+ monDiff
);
512 wxASSERT_MSG( mon
>= 0 && mon
< MONTHS_IN_YEAR
, _T("logic error") );
514 // NB: we don't check here that the resulting date is valid, this function
515 // is private and the caller must check it if needed
518 void wxDateTime::Tm::AddDays(int dayDiff
)
520 // normalize the days field
521 while ( dayDiff
+ mday
< 1 )
525 dayDiff
+= GetNumOfDaysInMonth(year
, mon
);
529 while ( mday
> GetNumOfDaysInMonth(year
, mon
) )
531 mday
-= GetNumOfDaysInMonth(year
, mon
);
536 wxASSERT_MSG( mday
> 0 && mday
<= GetNumOfDaysInMonth(year
, mon
),
540 // ----------------------------------------------------------------------------
542 // ----------------------------------------------------------------------------
544 wxDateTime::TimeZone::TimeZone(wxDateTime::TZ tz
)
548 case wxDateTime::Local
:
549 // get the offset from C RTL: it returns the difference GMT-local
550 // while we want to have the offset _from_ GMT, hence the '-'
551 m_offset
= -GetTimeZone();
554 case wxDateTime::GMT_12
:
555 case wxDateTime::GMT_11
:
556 case wxDateTime::GMT_10
:
557 case wxDateTime::GMT_9
:
558 case wxDateTime::GMT_8
:
559 case wxDateTime::GMT_7
:
560 case wxDateTime::GMT_6
:
561 case wxDateTime::GMT_5
:
562 case wxDateTime::GMT_4
:
563 case wxDateTime::GMT_3
:
564 case wxDateTime::GMT_2
:
565 case wxDateTime::GMT_1
:
566 m_offset
= -3600*(wxDateTime::GMT0
- tz
);
569 case wxDateTime::GMT0
:
570 case wxDateTime::GMT1
:
571 case wxDateTime::GMT2
:
572 case wxDateTime::GMT3
:
573 case wxDateTime::GMT4
:
574 case wxDateTime::GMT5
:
575 case wxDateTime::GMT6
:
576 case wxDateTime::GMT7
:
577 case wxDateTime::GMT8
:
578 case wxDateTime::GMT9
:
579 case wxDateTime::GMT10
:
580 case wxDateTime::GMT11
:
581 case wxDateTime::GMT12
:
582 m_offset
= 3600*(tz
- wxDateTime::GMT0
);
585 case wxDateTime::A_CST
:
586 // Central Standard Time in use in Australia = UTC + 9.5
587 m_offset
= 60l*(9*60 + 30);
591 wxFAIL_MSG( _T("unknown time zone") );
595 // ----------------------------------------------------------------------------
597 // ----------------------------------------------------------------------------
600 bool wxDateTime::IsLeapYear(int year
, wxDateTime::Calendar cal
)
602 if ( year
== Inv_Year
)
603 year
= GetCurrentYear();
605 if ( cal
== Gregorian
)
607 // in Gregorian calendar leap years are those divisible by 4 except
608 // those divisible by 100 unless they're also divisible by 400
609 // (in some countries, like Russia and Greece, additional corrections
610 // exist, but they won't manifest themselves until 2700)
611 return (year
% 4 == 0) && ((year
% 100 != 0) || (year
% 400 == 0));
613 else if ( cal
== Julian
)
615 // in Julian calendar the rule is simpler
616 return year
% 4 == 0;
620 wxFAIL_MSG(_T("unknown calendar"));
627 int wxDateTime::GetCentury(int year
)
629 return year
> 0 ? year
/ 100 : year
/ 100 - 1;
633 int wxDateTime::ConvertYearToBC(int year
)
636 return year
> 0 ? year
: year
- 1;
640 int wxDateTime::GetCurrentYear(wxDateTime::Calendar cal
)
645 return Now().GetYear();
648 wxFAIL_MSG(_T("TODO"));
652 wxFAIL_MSG(_T("unsupported calendar"));
660 wxDateTime::Month
wxDateTime::GetCurrentMonth(wxDateTime::Calendar cal
)
665 return Now().GetMonth();
668 wxFAIL_MSG(_T("TODO"));
672 wxFAIL_MSG(_T("unsupported calendar"));
680 wxDateTime::wxDateTime_t
wxDateTime::GetNumberOfDays(int year
, Calendar cal
)
682 if ( year
== Inv_Year
)
684 // take the current year if none given
685 year
= GetCurrentYear();
692 return IsLeapYear(year
) ? 366 : 365;
695 wxFAIL_MSG(_T("unsupported calendar"));
703 wxDateTime::wxDateTime_t
wxDateTime::GetNumberOfDays(wxDateTime::Month month
,
705 wxDateTime::Calendar cal
)
707 wxCHECK_MSG( month
< MONTHS_IN_YEAR
, 0, _T("invalid month") );
709 if ( cal
== Gregorian
|| cal
== Julian
)
711 if ( year
== Inv_Year
)
713 // take the current year if none given
714 year
= GetCurrentYear();
717 return GetNumOfDaysInMonth(year
, month
);
721 wxFAIL_MSG(_T("unsupported calendar"));
728 wxString
wxDateTime::GetMonthName(wxDateTime::Month month
,
729 wxDateTime::NameFlags flags
)
731 wxCHECK_MSG( month
!= Inv_Month
, _T(""), _T("invalid month") );
733 // notice that we must set all the fields to avoid confusing libc (GNU one
734 // gets confused to a crash if we don't do this)
739 return CallStrftime(flags
== Name_Abbr
? _T("%b") : _T("%B"), &tm
);
743 wxString
wxDateTime::GetWeekDayName(wxDateTime::WeekDay wday
,
744 wxDateTime::NameFlags flags
)
746 wxCHECK_MSG( wday
!= Inv_WeekDay
, _T(""), _T("invalid weekday") );
748 // take some arbitrary Sunday
755 // and offset it by the number of days needed to get the correct wday
758 // call mktime() to normalize it...
761 // ... and call strftime()
762 return CallStrftime(flags
== Name_Abbr
? _T("%a") : _T("%A"), &tm
);
766 void wxDateTime::GetAmPmStrings(wxString
*am
, wxString
*pm
)
772 *am
= CallStrftime(_T("%p"), &tm
);
777 *pm
= CallStrftime(_T("%p"), &tm
);
781 // ----------------------------------------------------------------------------
782 // Country stuff: date calculations depend on the country (DST, work days,
783 // ...), so we need to know which rules to follow.
784 // ----------------------------------------------------------------------------
787 wxDateTime::Country
wxDateTime::GetCountry()
789 // TODO use LOCALE_ICOUNTRY setting under Win32
791 if ( ms_country
== Country_Unknown
)
793 // try to guess from the time zone name
794 time_t t
= time(NULL
);
795 struct tm
*tm
= localtime(&t
);
797 wxString tz
= CallStrftime(_T("%Z"), tm
);
798 if ( tz
== _T("WET") || tz
== _T("WEST") )
802 else if ( tz
== _T("CET") || tz
== _T("CEST") )
804 ms_country
= Country_EEC
;
806 else if ( tz
== _T("MSK") || tz
== _T("MSD") )
810 else if ( tz
== _T("AST") || tz
== _T("ADT") ||
811 tz
== _T("EST") || tz
== _T("EDT") ||
812 tz
== _T("CST") || tz
== _T("CDT") ||
813 tz
== _T("MST") || tz
== _T("MDT") ||
814 tz
== _T("PST") || tz
== _T("PDT") )
820 // well, choose a default one
829 void wxDateTime::SetCountry(wxDateTime::Country country
)
831 ms_country
= country
;
835 bool wxDateTime::IsWestEuropeanCountry(Country country
)
837 if ( country
== Country_Default
)
839 country
= GetCountry();
842 return (Country_WesternEurope_Start
<= country
) &&
843 (country
<= Country_WesternEurope_End
);
846 // ----------------------------------------------------------------------------
847 // DST calculations: we use 3 different rules for the West European countries,
848 // USA and for the rest of the world. This is undoubtedly false for many
849 // countries, but I lack the necessary info (and the time to gather it),
850 // please add the other rules here!
851 // ----------------------------------------------------------------------------
854 bool wxDateTime::IsDSTApplicable(int year
, Country country
)
856 if ( year
== Inv_Year
)
858 // take the current year if none given
859 year
= GetCurrentYear();
862 if ( country
== Country_Default
)
864 country
= GetCountry();
871 // DST was first observed in the US and UK during WWI, reused
872 // during WWII and used again since 1966
873 return year
>= 1966 ||
874 (year
>= 1942 && year
<= 1945) ||
875 (year
== 1918 || year
== 1919);
878 // assume that it started after WWII
884 wxDateTime
wxDateTime::GetBeginDST(int year
, Country country
)
886 if ( year
== Inv_Year
)
888 // take the current year if none given
889 year
= GetCurrentYear();
892 if ( country
== Country_Default
)
894 country
= GetCountry();
897 if ( !IsDSTApplicable(year
, country
) )
899 return wxInvalidDateTime
;
904 if ( IsWestEuropeanCountry(country
) || (country
== Russia
) )
906 // DST begins at 1 a.m. GMT on the last Sunday of March
907 if ( !dt
.SetToLastWeekDay(Sun
, Mar
, year
) )
910 wxFAIL_MSG( _T("no last Sunday in March?") );
913 dt
+= wxTimeSpan::Hours(1);
915 // disable DST tests because it could result in an infinite recursion!
918 else switch ( country
)
925 // don't know for sure - assume it was in effect all year
930 dt
.Set(1, Jan
, year
);
934 // DST was installed Feb 2, 1942 by the Congress
935 dt
.Set(2, Feb
, year
);
938 // Oil embargo changed the DST period in the US
940 dt
.Set(6, Jan
, 1974);
944 dt
.Set(23, Feb
, 1975);
948 // before 1986, DST begun on the last Sunday of April, but
949 // in 1986 Reagan changed it to begin at 2 a.m. of the
950 // first Sunday in April
953 if ( !dt
.SetToLastWeekDay(Sun
, Apr
, year
) )
956 wxFAIL_MSG( _T("no first Sunday in April?") );
961 if ( !dt
.SetToWeekDay(Sun
, 1, Apr
, year
) )
964 wxFAIL_MSG( _T("no first Sunday in April?") );
968 dt
+= wxTimeSpan::Hours(2);
970 // TODO what about timezone??
976 // assume Mar 30 as the start of the DST for the rest of the world
977 // - totally bogus, of course
978 dt
.Set(30, Mar
, year
);
985 wxDateTime
wxDateTime::GetEndDST(int year
, Country country
)
987 if ( year
== Inv_Year
)
989 // take the current year if none given
990 year
= GetCurrentYear();
993 if ( country
== Country_Default
)
995 country
= GetCountry();
998 if ( !IsDSTApplicable(year
, country
) )
1000 return wxInvalidDateTime
;
1005 if ( IsWestEuropeanCountry(country
) || (country
== Russia
) )
1007 // DST ends at 1 a.m. GMT on the last Sunday of October
1008 if ( !dt
.SetToLastWeekDay(Sun
, Oct
, year
) )
1010 // weirder and weirder...
1011 wxFAIL_MSG( _T("no last Sunday in October?") );
1014 dt
+= wxTimeSpan::Hours(1);
1016 // disable DST tests because it could result in an infinite recursion!
1019 else switch ( country
)
1026 // don't know for sure - assume it was in effect all year
1030 dt
.Set(31, Dec
, year
);
1034 // the time was reset after the end of the WWII
1035 dt
.Set(30, Sep
, year
);
1039 // DST ends at 2 a.m. on the last Sunday of October
1040 if ( !dt
.SetToLastWeekDay(Sun
, Oct
, year
) )
1042 // weirder and weirder...
1043 wxFAIL_MSG( _T("no last Sunday in October?") );
1046 dt
+= wxTimeSpan::Hours(2);
1048 // TODO what about timezone??
1053 // assume October 26th as the end of the DST - totally bogus too
1054 dt
.Set(26, Oct
, year
);
1060 // ----------------------------------------------------------------------------
1061 // constructors and assignment operators
1062 // ----------------------------------------------------------------------------
1064 // return the current time with ms precision
1065 /* static */ wxDateTime
wxDateTime::UNow()
1067 return wxDateTime(wxGetLocalTimeMillis());
1070 // the values in the tm structure contain the local time
1071 wxDateTime
& wxDateTime::Set(const struct tm
& tm
)
1074 time_t timet
= mktime(&tm2
);
1076 if ( timet
== (time_t)-1 )
1078 // mktime() rather unintuitively fails for Jan 1, 1970 if the hour is
1079 // less than timezone - try to make it work for this case
1080 if ( tm2
.tm_year
== 70 && tm2
.tm_mon
== 0 && tm2
.tm_mday
== 1 )
1082 // add timezone to make sure that date is in range
1083 tm2
.tm_sec
-= GetTimeZone();
1085 timet
= mktime(&tm2
);
1086 if ( timet
!= (time_t)-1 )
1088 timet
+= GetTimeZone();
1094 wxFAIL_MSG( _T("mktime() failed") );
1096 *this = wxInvalidDateTime
;
1106 wxDateTime
& wxDateTime::Set(wxDateTime_t hour
,
1107 wxDateTime_t minute
,
1108 wxDateTime_t second
,
1109 wxDateTime_t millisec
)
1111 // we allow seconds to be 61 to account for the leap seconds, even if we
1112 // don't use them really
1113 wxDATETIME_CHECK( hour
< 24 &&
1117 _T("Invalid time in wxDateTime::Set()") );
1119 // get the current date from system
1120 struct tm
*tm
= GetTmNow();
1122 wxDATETIME_CHECK( tm
, _T("localtime() failed") );
1126 tm
->tm_min
= minute
;
1127 tm
->tm_sec
= second
;
1131 // and finally adjust milliseconds
1132 return SetMillisecond(millisec
);
1135 wxDateTime
& wxDateTime::Set(wxDateTime_t day
,
1139 wxDateTime_t minute
,
1140 wxDateTime_t second
,
1141 wxDateTime_t millisec
)
1143 wxDATETIME_CHECK( hour
< 24 &&
1147 _T("Invalid time in wxDateTime::Set()") );
1149 ReplaceDefaultYearMonthWithCurrent(&year
, &month
);
1151 wxDATETIME_CHECK( (0 < day
) && (day
<= GetNumberOfDays(month
, year
)),
1152 _T("Invalid date in wxDateTime::Set()") );
1154 // the range of time_t type (inclusive)
1155 static const int yearMinInRange
= 1970;
1156 static const int yearMaxInRange
= 2037;
1158 // test only the year instead of testing for the exact end of the Unix
1159 // time_t range - it doesn't bring anything to do more precise checks
1160 if ( year
>= yearMinInRange
&& year
<= yearMaxInRange
)
1162 // use the standard library version if the date is in range - this is
1163 // probably more efficient than our code
1165 tm
.tm_year
= year
- 1900;
1171 tm
.tm_isdst
= -1; // mktime() will guess it
1175 // and finally adjust milliseconds
1176 return SetMillisecond(millisec
);
1180 // do time calculations ourselves: we want to calculate the number of
1181 // milliseconds between the given date and the epoch
1183 // get the JDN for the midnight of this day
1184 m_time
= GetTruncatedJDN(day
, month
, year
);
1185 m_time
-= EPOCH_JDN
;
1186 m_time
*= SECONDS_PER_DAY
* TIME_T_FACTOR
;
1188 // JDN corresponds to GMT, we take localtime
1189 Add(wxTimeSpan(hour
, minute
, second
+ GetTimeZone(), millisec
));
1195 wxDateTime
& wxDateTime::Set(double jdn
)
1197 // so that m_time will be 0 for the midnight of Jan 1, 1970 which is jdn
1199 jdn
-= EPOCH_JDN
+ 0.5;
1201 jdn
*= MILLISECONDS_PER_DAY
;
1208 wxDateTime
& wxDateTime::ResetTime()
1212 if ( tm
.hour
|| tm
.min
|| tm
.sec
|| tm
.msec
)
1225 // ----------------------------------------------------------------------------
1226 // time_t <-> broken down time conversions
1227 // ----------------------------------------------------------------------------
1229 wxDateTime::Tm
wxDateTime::GetTm(const TimeZone
& tz
) const
1231 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
1233 time_t time
= GetTicks();
1234 if ( time
!= (time_t)-1 )
1236 // use C RTL functions
1238 if ( tz
.GetOffset() == -GetTimeZone() )
1240 // we are working with local time
1241 tm
= localtime(&time
);
1243 // should never happen
1244 wxCHECK_MSG( tm
, Tm(), _T("localtime() failed") );
1248 time
+= (time_t)tz
.GetOffset();
1249 #if defined(__VMS__) || defined(__WATCOMC__) // time is unsigned so avoid warning
1250 int time2
= (int) time
;
1258 // should never happen
1259 wxCHECK_MSG( tm
, Tm(), _T("gmtime() failed") );
1263 tm
= (struct tm
*)NULL
;
1269 // adjust the milliseconds
1271 long timeOnly
= (m_time
% MILLISECONDS_PER_DAY
).ToLong();
1272 tm2
.msec
= (wxDateTime_t
)(timeOnly
% 1000);
1275 //else: use generic code below
1278 // remember the time and do the calculations with the date only - this
1279 // eliminates rounding errors of the floating point arithmetics
1281 wxLongLong timeMidnight
= m_time
+ tz
.GetOffset() * 1000;
1283 long timeOnly
= (timeMidnight
% MILLISECONDS_PER_DAY
).ToLong();
1285 // we want to always have positive time and timeMidnight to be really
1286 // the midnight before it
1289 timeOnly
= MILLISECONDS_PER_DAY
+ timeOnly
;
1292 timeMidnight
-= timeOnly
;
1294 // calculate the Gregorian date from JDN for the midnight of our date:
1295 // this will yield day, month (in 1..12 range) and year
1297 // actually, this is the JDN for the noon of the previous day
1298 long jdn
= (timeMidnight
/ MILLISECONDS_PER_DAY
).ToLong() + EPOCH_JDN
;
1300 // CREDIT: code below is by Scott E. Lee (but bugs are mine)
1302 wxASSERT_MSG( jdn
> -2, _T("JDN out of range") );
1304 // calculate the century
1305 long temp
= (jdn
+ JDN_OFFSET
) * 4 - 1;
1306 long century
= temp
/ DAYS_PER_400_YEARS
;
1308 // then the year and day of year (1 <= dayOfYear <= 366)
1309 temp
= ((temp
% DAYS_PER_400_YEARS
) / 4) * 4 + 3;
1310 long year
= (century
* 100) + (temp
/ DAYS_PER_4_YEARS
);
1311 long dayOfYear
= (temp
% DAYS_PER_4_YEARS
) / 4 + 1;
1313 // and finally the month and day of the month
1314 temp
= dayOfYear
* 5 - 3;
1315 long month
= temp
/ DAYS_PER_5_MONTHS
;
1316 long day
= (temp
% DAYS_PER_5_MONTHS
) / 5 + 1;
1318 // month is counted from March - convert to normal
1329 // year is offset by 4800
1332 // check that the algorithm gave us something reasonable
1333 wxASSERT_MSG( (0 < month
) && (month
<= 12), _T("invalid month") );
1334 wxASSERT_MSG( (1 <= day
) && (day
< 32), _T("invalid day") );
1336 // construct Tm from these values
1338 tm
.year
= (int)year
;
1339 tm
.mon
= (Month
)(month
- 1); // algorithm yields 1 for January, not 0
1340 tm
.mday
= (wxDateTime_t
)day
;
1341 tm
.msec
= (wxDateTime_t
)(timeOnly
% 1000);
1342 timeOnly
-= tm
.msec
;
1343 timeOnly
/= 1000; // now we have time in seconds
1345 tm
.sec
= (wxDateTime_t
)(timeOnly
% 60);
1347 timeOnly
/= 60; // now we have time in minutes
1349 tm
.min
= (wxDateTime_t
)(timeOnly
% 60);
1352 tm
.hour
= (wxDateTime_t
)(timeOnly
/ 60);
1357 wxDateTime
& wxDateTime::SetYear(int year
)
1359 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
1368 wxDateTime
& wxDateTime::SetMonth(Month month
)
1370 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
1379 wxDateTime
& wxDateTime::SetDay(wxDateTime_t mday
)
1381 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
1390 wxDateTime
& wxDateTime::SetHour(wxDateTime_t hour
)
1392 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
1401 wxDateTime
& wxDateTime::SetMinute(wxDateTime_t min
)
1403 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
1412 wxDateTime
& wxDateTime::SetSecond(wxDateTime_t sec
)
1414 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
1423 wxDateTime
& wxDateTime::SetMillisecond(wxDateTime_t millisecond
)
1425 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
1427 // we don't need to use GetTm() for this one
1428 m_time
-= m_time
% 1000l;
1429 m_time
+= millisecond
;
1434 // ----------------------------------------------------------------------------
1435 // wxDateTime arithmetics
1436 // ----------------------------------------------------------------------------
1438 wxDateTime
& wxDateTime::Add(const wxDateSpan
& diff
)
1442 tm
.year
+= diff
.GetYears();
1443 tm
.AddMonths(diff
.GetMonths());
1445 // check that the resulting date is valid
1446 if ( tm
.mday
> GetNumOfDaysInMonth(tm
.year
, tm
.mon
) )
1448 // We suppose that when adding one month to Jan 31 we want to get Feb
1449 // 28 (or 29), i.e. adding a month to the last day of the month should
1450 // give the last day of the next month which is quite logical.
1452 // Unfortunately, there is no logic way to understand what should
1453 // Jan 30 + 1 month be - Feb 28 too or Feb 27 (assuming non leap year)?
1454 // We make it Feb 28 (last day too), but it is highly questionable.
1455 tm
.mday
= GetNumOfDaysInMonth(tm
.year
, tm
.mon
);
1458 tm
.AddDays(diff
.GetTotalDays());
1462 wxASSERT_MSG( IsSameTime(tm
),
1463 _T("Add(wxDateSpan) shouldn't modify time") );
1468 // ----------------------------------------------------------------------------
1469 // Weekday and monthday stuff
1470 // ----------------------------------------------------------------------------
1472 bool wxDateTime::SetToTheWeek(wxDateTime_t numWeek
, WeekDay weekday
)
1474 int year
= GetYear();
1476 // Jan 4 always lies in the 1st week of the year
1478 SetToWeekDayInSameWeek(weekday
) += wxDateSpan::Weeks(numWeek
);
1480 if ( GetYear() != year
)
1482 // oops... numWeek was too big
1489 wxDateTime
& wxDateTime::SetToLastMonthDay(Month month
,
1492 // take the current month/year if none specified
1493 if ( year
== Inv_Year
)
1495 if ( month
== Inv_Month
)
1498 return Set(GetNumOfDaysInMonth(year
, month
), month
, year
);
1501 wxDateTime
& wxDateTime::SetToWeekDayInSameWeek(WeekDay weekday
)
1503 wxDATETIME_CHECK( weekday
!= Inv_WeekDay
, _T("invalid weekday") );
1505 WeekDay wdayThis
= GetWeekDay();
1506 if ( weekday
== wdayThis
)
1511 else if ( weekday
< wdayThis
)
1513 return Subtract(wxDateSpan::Days(wdayThis
- weekday
));
1515 else // weekday > wdayThis
1517 return Add(wxDateSpan::Days(weekday
- wdayThis
));
1521 wxDateTime
& wxDateTime::SetToNextWeekDay(WeekDay weekday
)
1523 wxDATETIME_CHECK( weekday
!= Inv_WeekDay
, _T("invalid weekday") );
1526 WeekDay wdayThis
= GetWeekDay();
1527 if ( weekday
== wdayThis
)
1532 else if ( weekday
< wdayThis
)
1534 // need to advance a week
1535 diff
= 7 - (wdayThis
- weekday
);
1537 else // weekday > wdayThis
1539 diff
= weekday
- wdayThis
;
1542 return Add(wxDateSpan::Days(diff
));
1545 wxDateTime
& wxDateTime::SetToPrevWeekDay(WeekDay weekday
)
1547 wxDATETIME_CHECK( weekday
!= Inv_WeekDay
, _T("invalid weekday") );
1550 WeekDay wdayThis
= GetWeekDay();
1551 if ( weekday
== wdayThis
)
1556 else if ( weekday
> wdayThis
)
1558 // need to go to previous week
1559 diff
= 7 - (weekday
- wdayThis
);
1561 else // weekday < wdayThis
1563 diff
= wdayThis
- weekday
;
1566 return Subtract(wxDateSpan::Days(diff
));
1569 bool wxDateTime::SetToWeekDay(WeekDay weekday
,
1574 wxCHECK_MSG( weekday
!= Inv_WeekDay
, FALSE
, _T("invalid weekday") );
1576 // we don't check explicitly that -5 <= n <= 5 because we will return FALSE
1577 // anyhow in such case - but may be should still give an assert for it?
1579 // take the current month/year if none specified
1580 ReplaceDefaultYearMonthWithCurrent(&year
, &month
);
1584 // TODO this probably could be optimised somehow...
1588 // get the first day of the month
1589 dt
.Set(1, month
, year
);
1592 WeekDay wdayFirst
= dt
.GetWeekDay();
1594 // go to the first weekday of the month
1595 int diff
= weekday
- wdayFirst
;
1599 // add advance n-1 weeks more
1602 dt
+= wxDateSpan::Days(diff
);
1604 else // count from the end of the month
1606 // get the last day of the month
1607 dt
.SetToLastMonthDay(month
, year
);
1610 WeekDay wdayLast
= dt
.GetWeekDay();
1612 // go to the last weekday of the month
1613 int diff
= wdayLast
- weekday
;
1617 // and rewind n-1 weeks from there
1620 dt
-= wxDateSpan::Days(diff
);
1623 // check that it is still in the same month
1624 if ( dt
.GetMonth() == month
)
1632 // no such day in this month
1637 wxDateTime::wxDateTime_t
wxDateTime::GetDayOfYear(const TimeZone
& tz
) const
1641 return gs_cumulatedDays
[IsLeapYear(tm
.year
)][tm
.mon
] + tm
.mday
;
1644 wxDateTime::wxDateTime_t
wxDateTime::GetWeekOfYear(wxDateTime::WeekFlags flags
,
1645 const TimeZone
& tz
) const
1647 if ( flags
== Default_First
)
1649 flags
= GetCountry() == USA
? Sunday_First
: Monday_First
;
1652 wxDateTime_t nDayInYear
= GetDayOfYear(tz
);
1655 WeekDay wd
= GetWeekDay(tz
);
1656 if ( flags
== Sunday_First
)
1658 week
= (nDayInYear
- wd
+ 7) / 7;
1662 // have to shift the week days values
1663 week
= (nDayInYear
- (wd
- 1 + 7) % 7 + 7) / 7;
1666 // FIXME some more elegant way??
1667 WeekDay wdYearStart
= wxDateTime(1, Jan
, GetYear()).GetWeekDay();
1668 if ( wdYearStart
== Wed
|| wdYearStart
== Thu
)
1676 wxDateTime::wxDateTime_t
wxDateTime::GetWeekOfMonth(wxDateTime::WeekFlags flags
,
1677 const TimeZone
& tz
) const
1680 wxDateTime dtMonthStart
= wxDateTime(1, tm
.mon
, tm
.year
);
1681 int nWeek
= GetWeekOfYear(flags
) - dtMonthStart
.GetWeekOfYear(flags
) + 1;
1684 // this may happen for January when Jan, 1 is the last week of the
1686 nWeek
+= IsLeapYear(tm
.year
- 1) ? 53 : 52;
1689 return (wxDateTime::wxDateTime_t
)nWeek
;
1692 wxDateTime
& wxDateTime::SetToYearDay(wxDateTime::wxDateTime_t yday
)
1694 int year
= GetYear();
1695 wxDATETIME_CHECK( (0 < yday
) && (yday
<= GetNumberOfDays(year
)),
1696 _T("invalid year day") );
1698 bool isLeap
= IsLeapYear(year
);
1699 for ( Month mon
= Jan
; mon
< Inv_Month
; wxNextMonth(mon
) )
1701 // for Dec, we can't compare with gs_cumulatedDays[mon + 1], but we
1702 // don't need it neither - because of the CHECK above we know that
1703 // yday lies in December then
1704 if ( (mon
== Dec
) || (yday
< gs_cumulatedDays
[isLeap
][mon
+ 1]) )
1706 Set(yday
- gs_cumulatedDays
[isLeap
][mon
], mon
, year
);
1715 // ----------------------------------------------------------------------------
1716 // Julian day number conversion and related stuff
1717 // ----------------------------------------------------------------------------
1719 double wxDateTime::GetJulianDayNumber() const
1721 // JDN are always expressed for the GMT dates
1722 Tm
tm(ToTimezone(GMT0
).GetTm(GMT0
));
1724 double result
= GetTruncatedJDN(tm
.mday
, tm
.mon
, tm
.year
);
1726 // add the part GetTruncatedJDN() neglected
1729 // and now add the time: 86400 sec = 1 JDN
1730 return result
+ ((double)(60*(60*tm
.hour
+ tm
.min
) + tm
.sec
)) / 86400;
1733 double wxDateTime::GetRataDie() const
1735 // March 1 of the year 0 is Rata Die day -306 and JDN 1721119.5
1736 return GetJulianDayNumber() - 1721119.5 - 306;
1739 // ----------------------------------------------------------------------------
1740 // timezone and DST stuff
1741 // ----------------------------------------------------------------------------
1743 int wxDateTime::IsDST(wxDateTime::Country country
) const
1745 wxCHECK_MSG( country
== Country_Default
, -1,
1746 _T("country support not implemented") );
1748 // use the C RTL for the dates in the standard range
1749 time_t timet
= GetTicks();
1750 if ( timet
!= (time_t)-1 )
1752 tm
*tm
= localtime(&timet
);
1754 wxCHECK_MSG( tm
, -1, _T("localtime() failed") );
1756 return tm
->tm_isdst
;
1760 int year
= GetYear();
1762 if ( !IsDSTApplicable(year
, country
) )
1764 // no DST time in this year in this country
1768 return IsBetween(GetBeginDST(year
, country
), GetEndDST(year
, country
));
1772 wxDateTime
& wxDateTime::MakeTimezone(const TimeZone
& tz
, bool noDST
)
1774 long secDiff
= GetTimeZone() + tz
.GetOffset();
1776 // we need to know whether DST is or not in effect for this date unless
1777 // the test disabled by the caller
1778 if ( !noDST
&& (IsDST() == 1) )
1780 // FIXME we assume that the DST is always shifted by 1 hour
1784 return Subtract(wxTimeSpan::Seconds(secDiff
));
1787 // ----------------------------------------------------------------------------
1788 // wxDateTime to/from text representations
1789 // ----------------------------------------------------------------------------
1791 wxString
wxDateTime::Format(const wxChar
*format
, const TimeZone
& tz
) const
1793 wxCHECK_MSG( format
, _T(""), _T("NULL format in wxDateTime::Format") );
1795 // we have to use our own implementation if the date is out of range of
1796 // strftime() or if we use non standard specificators
1797 time_t time
= GetTicks();
1798 if ( (time
!= (time_t)-1) && !wxStrstr(format
, _T("%l")) )
1802 if ( tz
.GetOffset() == -GetTimeZone() )
1804 // we are working with local time
1805 tm
= localtime(&time
);
1807 // should never happen
1808 wxCHECK_MSG( tm
, wxEmptyString
, _T("localtime() failed") );
1812 time
+= (int)tz
.GetOffset();
1814 #if defined(__VMS__) || defined(__WATCOMC__) // time is unsigned so avoid warning
1815 int time2
= (int) time
;
1823 // should never happen
1824 wxCHECK_MSG( tm
, wxEmptyString
, _T("gmtime() failed") );
1828 tm
= (struct tm
*)NULL
;
1834 return CallStrftime(format
, tm
);
1836 //else: use generic code below
1839 // we only parse ANSI C format specifications here, no POSIX 2
1840 // complications, no GNU extensions but we do add support for a "%l" format
1841 // specifier allowing to get the number of milliseconds
1844 // used for calls to strftime() when we only deal with time
1845 struct tm tmTimeOnly
;
1846 tmTimeOnly
.tm_hour
= tm
.hour
;
1847 tmTimeOnly
.tm_min
= tm
.min
;
1848 tmTimeOnly
.tm_sec
= tm
.sec
;
1849 tmTimeOnly
.tm_wday
= 0;
1850 tmTimeOnly
.tm_yday
= 0;
1851 tmTimeOnly
.tm_mday
= 1; // any date will do
1852 tmTimeOnly
.tm_mon
= 0;
1853 tmTimeOnly
.tm_year
= 76;
1854 tmTimeOnly
.tm_isdst
= 0; // no DST, we adjust for tz ourselves
1856 wxString tmp
, res
, fmt
;
1857 for ( const wxChar
*p
= format
; *p
; p
++ )
1859 if ( *p
!= _T('%') )
1867 // set the default format
1870 case _T('Y'): // year has 4 digits
1874 case _T('j'): // day of year has 3 digits
1875 case _T('l'): // milliseconds have 3 digits
1880 // it's either another valid format specifier in which case
1881 // the format is "%02d" (for all the rest) or we have the
1882 // field width preceding the format in which case it will
1883 // override the default format anyhow
1887 bool restart
= TRUE
;
1892 // start of the format specification
1895 case _T('a'): // a weekday name
1897 // second parameter should be TRUE for abbreviated names
1898 res
+= GetWeekDayName(tm
.GetWeekDay(),
1899 *p
== _T('a') ? Name_Abbr
: Name_Full
);
1902 case _T('b'): // a month name
1904 res
+= GetMonthName(tm
.mon
,
1905 *p
== _T('b') ? Name_Abbr
: Name_Full
);
1908 case _T('c'): // locale default date and time representation
1909 case _T('x'): // locale default date representation
1911 // the problem: there is no way to know what do these format
1912 // specifications correspond to for the current locale.
1914 // the solution: use a hack and still use strftime(): first
1915 // find the YEAR which is a year in the strftime() range (1970
1916 // - 2038) whose Jan 1 falls on the same week day as the Jan 1
1917 // of the real year. Then make a copy of the format and
1918 // replace all occurences of YEAR in it with some unique
1919 // string not appearing anywhere else in it, then use
1920 // strftime() to format the date in year YEAR and then replace
1921 // YEAR back by the real year and the unique replacement
1922 // string back with YEAR. Notice that "all occurences of YEAR"
1923 // means all occurences of 4 digit as well as 2 digit form!
1925 // the bugs: we assume that neither of %c nor %x contains any
1926 // fields which may change between the YEAR and real year. For
1927 // example, the week number (%U, %W) and the day number (%j)
1928 // will change if one of these years is leap and the other one
1931 // find the YEAR: normally, for any year X, Jan 1 or the
1932 // year X + 28 is the same weekday as Jan 1 of X (because
1933 // the weekday advances by 1 for each normal X and by 2
1934 // for each leap X, hence by 5 every 4 years or by 35
1935 // which is 0 mod 7 every 28 years) but this rule breaks
1936 // down if there are years between X and Y which are
1937 // divisible by 4 but not leap (i.e. divisible by 100 but
1938 // not 400), hence the correction.
1940 int yearReal
= GetYear(tz
);
1941 int mod28
= yearReal
% 28;
1943 // be careful to not go too far - we risk to leave the
1948 year
= 1988 + mod28
; // 1988 == 0 (mod 28)
1952 year
= 1970 + mod28
- 10; // 1970 == 10 (mod 28)
1955 int nCentury
= year
/ 100,
1956 nCenturyReal
= yearReal
/ 100;
1958 // need to adjust for the years divisble by 400 which are
1959 // not leap but are counted like leap ones if we just take
1960 // the number of centuries in between for nLostWeekDays
1961 int nLostWeekDays
= (nCentury
- nCenturyReal
) -
1962 (nCentury
/ 4 - nCenturyReal
/ 4);
1964 // we have to gain back the "lost" weekdays: note that the
1965 // effect of this loop is to not do anything to
1966 // nLostWeekDays (which we won't use any more), but to
1967 // (indirectly) set the year correctly
1968 while ( (nLostWeekDays
% 7) != 0 )
1970 nLostWeekDays
+= year
++ % 4 ? 1 : 2;
1973 // at any rate, we couldn't go further than 1988 + 9 + 28!
1974 wxASSERT_MSG( year
< 2030,
1975 _T("logic error in wxDateTime::Format") );
1977 wxString strYear
, strYear2
;
1978 strYear
.Printf(_T("%d"), year
);
1979 strYear2
.Printf(_T("%d"), year
% 100);
1981 // find two strings not occuring in format (this is surely
1982 // not optimal way of doing it... improvements welcome!)
1983 wxString fmt
= format
;
1984 wxString replacement
= (wxChar
)-1;
1985 while ( fmt
.Find(replacement
) != wxNOT_FOUND
)
1987 replacement
<< (wxChar
)-1;
1990 wxString replacement2
= (wxChar
)-2;
1991 while ( fmt
.Find(replacement
) != wxNOT_FOUND
)
1993 replacement
<< (wxChar
)-2;
1996 // replace all occurences of year with it
1997 bool wasReplaced
= fmt
.Replace(strYear
, replacement
) > 0;
1999 wasReplaced
= fmt
.Replace(strYear2
, replacement2
) > 0;
2001 // use strftime() to format the same date but in supported
2004 // NB: we assume that strftime() doesn't check for the
2005 // date validity and will happily format the date
2006 // corresponding to Feb 29 of a non leap year (which
2007 // may happen if yearReal was leap and year is not)
2008 struct tm tmAdjusted
;
2010 tmAdjusted
.tm_hour
= tm
.hour
;
2011 tmAdjusted
.tm_min
= tm
.min
;
2012 tmAdjusted
.tm_sec
= tm
.sec
;
2013 tmAdjusted
.tm_wday
= tm
.GetWeekDay();
2014 tmAdjusted
.tm_yday
= GetDayOfYear();
2015 tmAdjusted
.tm_mday
= tm
.mday
;
2016 tmAdjusted
.tm_mon
= tm
.mon
;
2017 tmAdjusted
.tm_year
= year
- 1900;
2018 tmAdjusted
.tm_isdst
= 0; // no DST, already adjusted
2019 wxString str
= CallStrftime(*p
== _T('c') ? _T("%c")
2023 // now replace the occurence of 1999 with the real year
2024 wxString strYearReal
, strYearReal2
;
2025 strYearReal
.Printf(_T("%04d"), yearReal
);
2026 strYearReal2
.Printf(_T("%02d"), yearReal
% 100);
2027 str
.Replace(strYear
, strYearReal
);
2028 str
.Replace(strYear2
, strYearReal2
);
2030 // and replace back all occurences of replacement string
2033 str
.Replace(replacement2
, strYear2
);
2034 str
.Replace(replacement
, strYear
);
2041 case _T('d'): // day of a month (01-31)
2042 res
+= wxString::Format(fmt
, tm
.mday
);
2045 case _T('H'): // hour in 24h format (00-23)
2046 res
+= wxString::Format(fmt
, tm
.hour
);
2049 case _T('I'): // hour in 12h format (01-12)
2051 // 24h -> 12h, 0h -> 12h too
2052 int hour12
= tm
.hour
> 12 ? tm
.hour
- 12
2053 : tm
.hour
? tm
.hour
: 12;
2054 res
+= wxString::Format(fmt
, hour12
);
2058 case _T('j'): // day of the year
2059 res
+= wxString::Format(fmt
, GetDayOfYear(tz
));
2062 case _T('l'): // milliseconds (NOT STANDARD)
2063 res
+= wxString::Format(fmt
, GetMillisecond(tz
));
2066 case _T('m'): // month as a number (01-12)
2067 res
+= wxString::Format(fmt
, tm
.mon
+ 1);
2070 case _T('M'): // minute as a decimal number (00-59)
2071 res
+= wxString::Format(fmt
, tm
.min
);
2074 case _T('p'): // AM or PM string
2075 res
+= CallStrftime(_T("%p"), &tmTimeOnly
);
2078 case _T('S'): // second as a decimal number (00-61)
2079 res
+= wxString::Format(fmt
, tm
.sec
);
2082 case _T('U'): // week number in the year (Sunday 1st week day)
2083 res
+= wxString::Format(fmt
, GetWeekOfYear(Sunday_First
, tz
));
2086 case _T('W'): // week number in the year (Monday 1st week day)
2087 res
+= wxString::Format(fmt
, GetWeekOfYear(Monday_First
, tz
));
2090 case _T('w'): // weekday as a number (0-6), Sunday = 0
2091 res
+= wxString::Format(fmt
, tm
.GetWeekDay());
2094 // case _T('x'): -- handled with "%c"
2096 case _T('X'): // locale default time representation
2097 // just use strftime() to format the time for us
2098 res
+= CallStrftime(_T("%X"), &tmTimeOnly
);
2101 case _T('y'): // year without century (00-99)
2102 res
+= wxString::Format(fmt
, tm
.year
% 100);
2105 case _T('Y'): // year with century
2106 res
+= wxString::Format(fmt
, tm
.year
);
2109 case _T('Z'): // timezone name
2110 res
+= CallStrftime(_T("%Z"), &tmTimeOnly
);
2114 // is it the format width?
2116 while ( *p
== _T('-') || *p
== _T('+') ||
2117 *p
== _T(' ') || wxIsdigit(*p
) )
2122 if ( !fmt
.IsEmpty() )
2124 // we've only got the flags and width so far in fmt
2125 fmt
.Prepend(_T('%'));
2126 fmt
.Append(_T('d'));
2133 // no, it wasn't the width
2134 wxFAIL_MSG(_T("unknown format specificator"));
2136 // fall through and just copy it nevertheless
2138 case _T('%'): // a percent sign
2142 case 0: // the end of string
2143 wxFAIL_MSG(_T("missing format at the end of string"));
2145 // just put the '%' which was the last char in format
2155 // this function parses a string in (strict) RFC 822 format: see the section 5
2156 // of the RFC for the detailed description, but briefly it's something of the
2157 // form "Sat, 18 Dec 1999 00:48:30 +0100"
2159 // this function is "strict" by design - it must reject anything except true
2160 // RFC822 time specs.
2162 // TODO a great candidate for using reg exps
2163 const wxChar
*wxDateTime::ParseRfc822Date(const wxChar
* date
)
2165 wxCHECK_MSG( date
, (wxChar
*)NULL
, _T("NULL pointer in wxDateTime::Parse") );
2167 const wxChar
*p
= date
;
2168 const wxChar
*comma
= wxStrchr(p
, _T(','));
2171 // the part before comma is the weekday
2173 // skip it for now - we don't use but might check that it really
2174 // corresponds to the specfied date
2177 if ( *p
!= _T(' ') )
2179 wxLogDebug(_T("no space after weekday in RFC822 time spec"));
2181 return (wxChar
*)NULL
;
2187 // the following 1 or 2 digits are the day number
2188 if ( !wxIsdigit(*p
) )
2190 wxLogDebug(_T("day number expected in RFC822 time spec, none found"));
2192 return (wxChar
*)NULL
;
2195 wxDateTime_t day
= *p
++ - _T('0');
2196 if ( wxIsdigit(*p
) )
2199 day
+= *p
++ - _T('0');
2202 if ( *p
++ != _T(' ') )
2204 return (wxChar
*)NULL
;
2207 // the following 3 letters specify the month
2208 wxString
monName(p
, 3);
2210 if ( monName
== _T("Jan") )
2212 else if ( monName
== _T("Feb") )
2214 else if ( monName
== _T("Mar") )
2216 else if ( monName
== _T("Apr") )
2218 else if ( monName
== _T("May") )
2220 else if ( monName
== _T("Jun") )
2222 else if ( monName
== _T("Jul") )
2224 else if ( monName
== _T("Aug") )
2226 else if ( monName
== _T("Sep") )
2228 else if ( monName
== _T("Oct") )
2230 else if ( monName
== _T("Nov") )
2232 else if ( monName
== _T("Dec") )
2236 wxLogDebug(_T("Invalid RFC 822 month name '%s'"), monName
.c_str());
2238 return (wxChar
*)NULL
;
2243 if ( *p
++ != _T(' ') )
2245 return (wxChar
*)NULL
;
2249 if ( !wxIsdigit(*p
) )
2252 return (wxChar
*)NULL
;
2255 int year
= *p
++ - _T('0');
2257 if ( !wxIsdigit(*p
) )
2259 // should have at least 2 digits in the year
2260 return (wxChar
*)NULL
;
2264 year
+= *p
++ - _T('0');
2266 // is it a 2 digit year (as per original RFC 822) or a 4 digit one?
2267 if ( wxIsdigit(*p
) )
2270 year
+= *p
++ - _T('0');
2272 if ( !wxIsdigit(*p
) )
2274 // no 3 digit years please
2275 return (wxChar
*)NULL
;
2279 year
+= *p
++ - _T('0');
2282 if ( *p
++ != _T(' ') )
2284 return (wxChar
*)NULL
;
2287 // time is in the format hh:mm:ss and seconds are optional
2288 if ( !wxIsdigit(*p
) )
2290 return (wxChar
*)NULL
;
2293 wxDateTime_t hour
= *p
++ - _T('0');
2295 if ( !wxIsdigit(*p
) )
2297 return (wxChar
*)NULL
;
2301 hour
+= *p
++ - _T('0');
2303 if ( *p
++ != _T(':') )
2305 return (wxChar
*)NULL
;
2308 if ( !wxIsdigit(*p
) )
2310 return (wxChar
*)NULL
;
2313 wxDateTime_t min
= *p
++ - _T('0');
2315 if ( !wxIsdigit(*p
) )
2317 return (wxChar
*)NULL
;
2321 min
+= *p
++ - _T('0');
2323 wxDateTime_t sec
= 0;
2324 if ( *p
++ == _T(':') )
2326 if ( !wxIsdigit(*p
) )
2328 return (wxChar
*)NULL
;
2331 sec
= *p
++ - _T('0');
2333 if ( !wxIsdigit(*p
) )
2335 return (wxChar
*)NULL
;
2339 sec
+= *p
++ - _T('0');
2342 if ( *p
++ != _T(' ') )
2344 return (wxChar
*)NULL
;
2347 // and now the interesting part: the timezone
2349 if ( *p
== _T('-') || *p
== _T('+') )
2351 // the explicit offset given: it has the form of hhmm
2352 bool plus
= *p
++ == _T('+');
2354 if ( !wxIsdigit(*p
) || !wxIsdigit(*(p
+ 1)) )
2356 return (wxChar
*)NULL
;
2360 offset
= 60*(10*(*p
- _T('0')) + (*(p
+ 1) - _T('0')));
2364 if ( !wxIsdigit(*p
) || !wxIsdigit(*(p
+ 1)) )
2366 return (wxChar
*)NULL
;
2370 offset
+= 10*(*p
- _T('0')) + (*(p
+ 1) - _T('0'));
2381 // the symbolic timezone given: may be either military timezone or one
2382 // of standard abbreviations
2385 // military: Z = UTC, J unused, A = -1, ..., Y = +12
2386 static const int offsets
[26] =
2388 //A B C D E F G H I J K L M
2389 -1, -2, -3, -4, -5, -6, -7, -8, -9, 0, -10, -11, -12,
2390 //N O P R Q S T U V W Z Y Z
2391 +1, +2, +3, +4, +5, +6, +7, +8, +9, +10, +11, +12, 0
2394 if ( *p
< _T('A') || *p
> _T('Z') || *p
== _T('J') )
2396 wxLogDebug(_T("Invalid militaty timezone '%c'"), *p
);
2398 return (wxChar
*)NULL
;
2401 offset
= offsets
[*p
++ - _T('A')];
2407 if ( tz
== _T("UT") || tz
== _T("UTC") || tz
== _T("GMT") )
2409 else if ( tz
== _T("AST") )
2410 offset
= AST
- GMT0
;
2411 else if ( tz
== _T("ADT") )
2412 offset
= ADT
- GMT0
;
2413 else if ( tz
== _T("EST") )
2414 offset
= EST
- GMT0
;
2415 else if ( tz
== _T("EDT") )
2416 offset
= EDT
- GMT0
;
2417 else if ( tz
== _T("CST") )
2418 offset
= CST
- GMT0
;
2419 else if ( tz
== _T("CDT") )
2420 offset
= CDT
- GMT0
;
2421 else if ( tz
== _T("MST") )
2422 offset
= MST
- GMT0
;
2423 else if ( tz
== _T("MDT") )
2424 offset
= MDT
- GMT0
;
2425 else if ( tz
== _T("PST") )
2426 offset
= PST
- GMT0
;
2427 else if ( tz
== _T("PDT") )
2428 offset
= PDT
- GMT0
;
2431 wxLogDebug(_T("Unknown RFC 822 timezone '%s'"), p
);
2433 return (wxChar
*)NULL
;
2443 // the spec was correct
2444 Set(day
, mon
, year
, hour
, min
, sec
);
2445 MakeTimezone((wxDateTime_t
)(60*offset
));
2450 const wxChar
*wxDateTime::ParseFormat(const wxChar
*date
,
2451 const wxChar
*format
,
2452 const wxDateTime
& dateDef
)
2454 wxCHECK_MSG( date
&& format
, (wxChar
*)NULL
,
2455 _T("NULL pointer in wxDateTime::ParseFormat()") );
2460 // what fields have we found?
2461 bool haveWDay
= FALSE
,
2470 bool hourIsIn12hFormat
= FALSE
, // or in 24h one?
2471 isPM
= FALSE
; // AM by default
2473 // and the value of the items we have (init them to get rid of warnings)
2474 wxDateTime_t sec
= 0,
2477 WeekDay wday
= Inv_WeekDay
;
2478 wxDateTime_t yday
= 0,
2480 wxDateTime::Month mon
= Inv_Month
;
2483 const wxChar
*input
= date
;
2484 for ( const wxChar
*fmt
= format
; *fmt
; fmt
++ )
2486 if ( *fmt
!= _T('%') )
2488 if ( wxIsspace(*fmt
) )
2490 // a white space in the format string matches 0 or more white
2491 // spaces in the input
2492 while ( wxIsspace(*input
) )
2499 // any other character (not whitespace, not '%') must be
2500 // matched by itself in the input
2501 if ( *input
++ != *fmt
)
2504 return (wxChar
*)NULL
;
2508 // done with this format char
2512 // start of a format specification
2514 // parse the optional width
2516 while ( isdigit(*++fmt
) )
2519 width
+= *fmt
- _T('0');
2522 // then the format itself
2525 case _T('a'): // a weekday name
2528 int flag
= *fmt
== _T('a') ? Name_Abbr
: Name_Full
;
2529 wday
= GetWeekDayFromName(GetAlphaToken(input
), flag
);
2530 if ( wday
== Inv_WeekDay
)
2533 return (wxChar
*)NULL
;
2539 case _T('b'): // a month name
2542 int flag
= *fmt
== _T('b') ? Name_Abbr
: Name_Full
;
2543 mon
= GetMonthFromName(GetAlphaToken(input
), flag
);
2544 if ( mon
== Inv_Month
)
2547 return (wxChar
*)NULL
;
2553 case _T('c'): // locale default date and time representation
2557 // this is the format which corresponds to ctime() output
2558 // and strptime("%c") should parse it, so try it first
2559 static const wxChar
*fmtCtime
= _T("%a %b %d %H:%M:%S %Y");
2561 const wxChar
*result
= dt
.ParseFormat(input
, fmtCtime
);
2564 result
= dt
.ParseFormat(input
, _T("%x %X"));
2569 result
= dt
.ParseFormat(input
, _T("%X %x"));
2574 // we've tried everything and still no match
2575 return (wxChar
*)NULL
;
2580 haveDay
= haveMon
= haveYear
=
2581 haveHour
= haveMin
= haveSec
= TRUE
;
2595 case _T('d'): // day of a month (01-31)
2596 if ( !GetNumericToken(width
, input
, &num
) ||
2597 (num
> 31) || (num
< 1) )
2600 return (wxChar
*)NULL
;
2603 // we can't check whether the day range is correct yet, will
2604 // do it later - assume ok for now
2606 mday
= (wxDateTime_t
)num
;
2609 case _T('H'): // hour in 24h format (00-23)
2610 if ( !GetNumericToken(width
, input
, &num
) || (num
> 23) )
2613 return (wxChar
*)NULL
;
2617 hour
= (wxDateTime_t
)num
;
2620 case _T('I'): // hour in 12h format (01-12)
2621 if ( !GetNumericToken(width
, input
, &num
) || !num
|| (num
> 12) )
2624 return (wxChar
*)NULL
;
2628 hourIsIn12hFormat
= TRUE
;
2629 hour
= (wxDateTime_t
)(num
% 12); // 12 should be 0
2632 case _T('j'): // day of the year
2633 if ( !GetNumericToken(width
, input
, &num
) || !num
|| (num
> 366) )
2636 return (wxChar
*)NULL
;
2640 yday
= (wxDateTime_t
)num
;
2643 case _T('m'): // month as a number (01-12)
2644 if ( !GetNumericToken(width
, input
, &num
) || !num
|| (num
> 12) )
2647 return (wxChar
*)NULL
;
2651 mon
= (Month
)(num
- 1);
2654 case _T('M'): // minute as a decimal number (00-59)
2655 if ( !GetNumericToken(width
, input
, &num
) || (num
> 59) )
2658 return (wxChar
*)NULL
;
2662 min
= (wxDateTime_t
)num
;
2665 case _T('p'): // AM or PM string
2667 wxString am
, pm
, token
= GetAlphaToken(input
);
2669 GetAmPmStrings(&am
, &pm
);
2670 if ( token
.CmpNoCase(pm
) == 0 )
2674 else if ( token
.CmpNoCase(am
) != 0 )
2677 return (wxChar
*)NULL
;
2682 case _T('r'): // time as %I:%M:%S %p
2685 input
= dt
.ParseFormat(input
, _T("%I:%M:%S %p"));
2689 return (wxChar
*)NULL
;
2692 haveHour
= haveMin
= haveSec
= TRUE
;
2701 case _T('R'): // time as %H:%M
2704 input
= dt
.ParseFormat(input
, _T("%H:%M"));
2708 return (wxChar
*)NULL
;
2711 haveHour
= haveMin
= TRUE
;
2718 case _T('S'): // second as a decimal number (00-61)
2719 if ( !GetNumericToken(width
, input
, &num
) || (num
> 61) )
2722 return (wxChar
*)NULL
;
2726 sec
= (wxDateTime_t
)num
;
2729 case _T('T'): // time as %H:%M:%S
2732 input
= dt
.ParseFormat(input
, _T("%H:%M:%S"));
2736 return (wxChar
*)NULL
;
2739 haveHour
= haveMin
= haveSec
= TRUE
;
2748 case _T('w'): // weekday as a number (0-6), Sunday = 0
2749 if ( !GetNumericToken(width
, input
, &num
) || (wday
> 6) )
2752 return (wxChar
*)NULL
;
2756 wday
= (WeekDay
)num
;
2759 case _T('x'): // locale default date representation
2760 #ifdef HAVE_STRPTIME
2761 // try using strptime() - it may fail even if the input is
2762 // correct but the date is out of range, so we will fall back
2763 // to our generic code anyhow (FIXME !Unicode friendly)
2766 const wxChar
*result
= strptime(input
, "%x", &tm
);
2771 haveDay
= haveMon
= haveYear
= TRUE
;
2773 year
= 1900 + tm
.tm_year
;
2774 mon
= (Month
)tm
.tm_mon
;
2780 #endif // HAVE_STRPTIME
2782 // TODO query the LOCALE_IDATE setting under Win32
2786 wxString fmtDate
, fmtDateAlt
;
2787 if ( IsWestEuropeanCountry(GetCountry()) ||
2788 GetCountry() == Russia
)
2790 fmtDate
= _T("%d/%m/%y");
2791 fmtDateAlt
= _T("%m/%d/%y");
2795 fmtDate
= _T("%m/%d/%y");
2796 fmtDateAlt
= _T("%d/%m/%y");
2799 const wxChar
*result
= dt
.ParseFormat(input
, fmtDate
);
2803 // ok, be nice and try another one
2804 result
= dt
.ParseFormat(input
, fmtDateAlt
);
2810 return (wxChar
*)NULL
;
2815 haveDay
= haveMon
= haveYear
= TRUE
;
2826 case _T('X'): // locale default time representation
2827 #ifdef HAVE_STRPTIME
2829 // use strptime() to do it for us (FIXME !Unicode friendly)
2831 input
= strptime(input
, "%X", &tm
);
2834 return (wxChar
*)NULL
;
2837 haveHour
= haveMin
= haveSec
= TRUE
;
2843 #else // !HAVE_STRPTIME
2844 // TODO under Win32 we can query the LOCALE_ITIME system
2845 // setting which says whether the default time format is
2848 // try to parse what follows as "%H:%M:%S" and, if this
2849 // fails, as "%I:%M:%S %p" - this should catch the most
2853 const wxChar
*result
= dt
.ParseFormat(input
, _T("%T"));
2856 result
= dt
.ParseFormat(input
, _T("%r"));
2862 return (wxChar
*)NULL
;
2865 haveHour
= haveMin
= haveSec
= TRUE
;
2874 #endif // HAVE_STRPTIME/!HAVE_STRPTIME
2877 case _T('y'): // year without century (00-99)
2878 if ( !GetNumericToken(width
, input
, &num
) || (num
> 99) )
2881 return (wxChar
*)NULL
;
2886 // TODO should have an option for roll over date instead of
2887 // hard coding it here
2888 year
= (num
> 30 ? 1900 : 2000) + (wxDateTime_t
)num
;
2891 case _T('Y'): // year with century
2892 if ( !GetNumericToken(width
, input
, &num
) )
2895 return (wxChar
*)NULL
;
2899 year
= (wxDateTime_t
)num
;
2902 case _T('Z'): // timezone name
2903 wxFAIL_MSG(_T("TODO"));
2906 case _T('%'): // a percent sign
2907 if ( *input
++ != _T('%') )
2910 return (wxChar
*)NULL
;
2914 case 0: // the end of string
2915 wxFAIL_MSG(_T("unexpected format end"));
2919 default: // not a known format spec
2920 return (wxChar
*)NULL
;
2924 // format matched, try to construct a date from what we have now
2926 if ( dateDef
.IsValid() )
2928 // take this date as default
2929 tmDef
= dateDef
.GetTm();
2931 else if ( IsValid() )
2933 // if this date is valid, don't change it
2938 // no default and this date is invalid - fall back to Today()
2939 tmDef
= Today().GetTm();
2950 // TODO we don't check here that the values are consistent, if both year
2951 // day and month/day were found, we just ignore the year day and we
2952 // also always ignore the week day
2953 if ( haveMon
&& haveDay
)
2955 if ( mday
> GetNumOfDaysInMonth(tm
.year
, mon
) )
2957 wxLogDebug(_T("bad month day in wxDateTime::ParseFormat"));
2959 return (wxChar
*)NULL
;
2965 else if ( haveYDay
)
2967 if ( yday
> GetNumberOfDays(tm
.year
) )
2969 wxLogDebug(_T("bad year day in wxDateTime::ParseFormat"));
2971 return (wxChar
*)NULL
;
2974 Tm tm2
= wxDateTime(1, Jan
, tm
.year
).SetToYearDay(yday
).GetTm();
2981 if ( haveHour
&& hourIsIn12hFormat
&& isPM
)
2983 // translate to 24hour format
2986 //else: either already in 24h format or no translation needed
3009 const wxChar
*wxDateTime::ParseDateTime(const wxChar
*date
)
3011 wxCHECK_MSG( date
, (wxChar
*)NULL
, _T("NULL pointer in wxDateTime::Parse") );
3013 // there is a public domain version of getdate.y, but it only works for
3015 wxFAIL_MSG(_T("TODO"));
3017 return (wxChar
*)NULL
;
3020 const wxChar
*wxDateTime::ParseDate(const wxChar
*date
)
3022 // this is a simplified version of ParseDateTime() which understands only
3023 // "today" (for wxDate compatibility) and digits only otherwise (and not
3024 // all esoteric constructions ParseDateTime() knows about)
3026 wxCHECK_MSG( date
, (wxChar
*)NULL
, _T("NULL pointer in wxDateTime::Parse") );
3028 const wxChar
*p
= date
;
3029 while ( wxIsspace(*p
) )
3032 // some special cases
3036 int dayDiffFromToday
;
3039 { wxTRANSLATE("today"), 0 },
3040 { wxTRANSLATE("yesterday"), -1 },
3041 { wxTRANSLATE("tomorrow"), 1 },
3044 for ( size_t n
= 0; n
< WXSIZEOF(literalDates
); n
++ )
3046 wxString date
= wxGetTranslation(literalDates
[n
].str
);
3047 size_t len
= date
.length();
3048 if ( wxStrlen(p
) >= len
&& (wxString(p
, len
).CmpNoCase(date
) == 0) )
3050 // nothing can follow this, so stop here
3053 int dayDiffFromToday
= literalDates
[n
].dayDiffFromToday
;
3055 if ( dayDiffFromToday
)
3057 *this += wxDateSpan::Days(dayDiffFromToday
);
3064 // We try to guess what we have here: for each new (numeric) token, we
3065 // determine if it can be a month, day or a year. Of course, there is an
3066 // ambiguity as some numbers may be days as well as months, so we also
3067 // have the ability to back track.
3070 bool haveDay
= FALSE
, // the months day?
3071 haveWDay
= FALSE
, // the day of week?
3072 haveMon
= FALSE
, // the month?
3073 haveYear
= FALSE
; // the year?
3075 // and the value of the items we have (init them to get rid of warnings)
3076 WeekDay wday
= Inv_WeekDay
;
3077 wxDateTime_t day
= 0;
3078 wxDateTime::Month mon
= Inv_Month
;
3081 // tokenize the string
3083 static const wxChar
*dateDelimiters
= _T(".,/-\t\n ");
3084 wxStringTokenizer
tok(p
, dateDelimiters
);
3085 while ( tok
.HasMoreTokens() )
3087 wxString token
= tok
.GetNextToken();
3093 if ( token
.ToULong(&val
) )
3095 // guess what this number is
3101 if ( !haveMon
&& val
> 0 && val
<= 12 )
3103 // assume it is month
3106 else // not the month
3108 wxDateTime_t maxDays
= haveMon
3109 ? GetNumOfDaysInMonth(haveYear
? year
: Inv_Year
, mon
)
3113 if ( (val
== 0) || (val
> (unsigned long)maxDays
) ) // cast to shut up compiler warning in BCC
3130 year
= (wxDateTime_t
)val
;
3139 day
= (wxDateTime_t
)val
;
3145 mon
= (Month
)(val
- 1);
3148 else // not a number
3150 // be careful not to overwrite the current mon value
3151 Month mon2
= GetMonthFromName(token
, Name_Full
| Name_Abbr
);
3152 if ( mon2
!= Inv_Month
)
3157 // but we already have a month - maybe we guessed wrong?
3160 // no need to check in month range as always < 12, but
3161 // the days are counted from 1 unlike the months
3162 day
= (wxDateTime_t
)mon
+ 1;
3167 // could possible be the year (doesn't the year come
3168 // before the month in the japanese format?) (FIXME)
3177 else // not a valid month name
3179 wday
= GetWeekDayFromName(token
, Name_Full
| Name_Abbr
);
3180 if ( wday
!= Inv_WeekDay
)
3190 else // not a valid weekday name
3193 static const wxChar
*ordinals
[] =
3195 wxTRANSLATE("first"),
3196 wxTRANSLATE("second"),
3197 wxTRANSLATE("third"),
3198 wxTRANSLATE("fourth"),
3199 wxTRANSLATE("fifth"),
3200 wxTRANSLATE("sixth"),
3201 wxTRANSLATE("seventh"),
3202 wxTRANSLATE("eighth"),
3203 wxTRANSLATE("ninth"),
3204 wxTRANSLATE("tenth"),
3205 wxTRANSLATE("eleventh"),
3206 wxTRANSLATE("twelfth"),
3207 wxTRANSLATE("thirteenth"),
3208 wxTRANSLATE("fourteenth"),
3209 wxTRANSLATE("fifteenth"),
3210 wxTRANSLATE("sixteenth"),
3211 wxTRANSLATE("seventeenth"),
3212 wxTRANSLATE("eighteenth"),
3213 wxTRANSLATE("nineteenth"),
3214 wxTRANSLATE("twentieth"),
3215 // that's enough - otherwise we'd have problems with
3216 // composite (or not) ordinals
3220 for ( n
= 0; n
< WXSIZEOF(ordinals
); n
++ )
3222 if ( token
.CmpNoCase(ordinals
[n
]) == 0 )
3228 if ( n
== WXSIZEOF(ordinals
) )
3230 // stop here - something unknown
3237 // don't try anything here (as in case of numeric day
3238 // above) - the symbolic day spec should always
3239 // precede the month/year
3245 day
= (wxDateTime_t
)(n
+ 1);
3250 nPosCur
= tok
.GetPosition();
3253 // either no more tokens or the scan was stopped by something we couldn't
3254 // parse - in any case, see if we can construct a date from what we have
3255 if ( !haveDay
&& !haveWDay
)
3257 wxLogDebug(_T("ParseDate: no day, no weekday hence no date."));
3259 return (wxChar
*)NULL
;
3262 if ( haveWDay
&& (haveMon
|| haveYear
|| haveDay
) &&
3263 !(haveDay
&& haveMon
&& haveYear
) )
3265 // without adjectives (which we don't support here) the week day only
3266 // makes sense completely separately or with the full date
3267 // specification (what would "Wed 1999" mean?)
3268 return (wxChar
*)NULL
;
3271 if ( !haveWDay
&& haveYear
&& !(haveDay
&& haveMon
) )
3273 // may be we have month and day instead of day and year?
3274 if ( haveDay
&& !haveMon
)
3278 // exchange day and month
3279 mon
= (wxDateTime::Month
)(day
- 1);
3281 // we're in the current year then
3283 (unsigned)year
<= GetNumOfDaysInMonth(Inv_Year
, mon
) )
3290 //else: no, can't exchange, leave haveMon == FALSE
3296 // if we give the year, month and day must be given too
3297 wxLogDebug(_T("ParseDate: day and month should be specified if year is."));
3299 return (wxChar
*)NULL
;
3305 mon
= GetCurrentMonth();
3310 year
= GetCurrentYear();
3315 Set(day
, mon
, year
);
3319 // check that it is really the same
3320 if ( GetWeekDay() != wday
)
3322 // inconsistency detected
3323 wxLogDebug(_T("ParseDate: inconsistent day/weekday."));
3325 return (wxChar
*)NULL
;
3333 SetToWeekDayInSameWeek(wday
);
3336 // return the pointer to the first unparsed char
3338 if ( nPosCur
&& wxStrchr(dateDelimiters
, *(p
- 1)) )
3340 // if we couldn't parse the token after the delimiter, put back the
3341 // delimiter as well
3348 const wxChar
*wxDateTime::ParseTime(const wxChar
*time
)
3350 wxCHECK_MSG( time
, (wxChar
*)NULL
, _T("NULL pointer in wxDateTime::Parse") );
3352 // first try some extra things
3359 { wxTRANSLATE("noon"), 12 },
3360 { wxTRANSLATE("midnight"), 00 },
3364 for ( size_t n
= 0; n
< WXSIZEOF(stdTimes
); n
++ )
3366 wxString timeString
= wxGetTranslation(stdTimes
[n
].name
);
3367 size_t len
= timeString
.length();
3368 if ( timeString
.CmpNoCase(wxString(time
, len
)) == 0 )
3370 Set(stdTimes
[n
].hour
, 0, 0);
3376 // try all time formats we may think about in the order from longest to
3379 // 12hour with AM/PM?
3380 const wxChar
*result
= ParseFormat(time
, _T("%I:%M:%S %p"));
3384 // normally, it's the same, but why not try it?
3385 result
= ParseFormat(time
, _T("%H:%M:%S"));
3390 // 12hour with AM/PM but without seconds?
3391 result
= ParseFormat(time
, _T("%I:%M %p"));
3397 result
= ParseFormat(time
, _T("%H:%M"));
3402 // just the hour and AM/PM?
3403 result
= ParseFormat(time
, _T("%I %p"));
3409 result
= ParseFormat(time
, _T("%H"));
3414 // parse the standard format: normally it is one of the formats above
3415 // but it may be set to something completely different by the user
3416 result
= ParseFormat(time
, _T("%X"));
3419 // TODO: parse timezones
3424 // ----------------------------------------------------------------------------
3425 // Workdays and holidays support
3426 // ----------------------------------------------------------------------------
3428 bool wxDateTime::IsWorkDay(Country
WXUNUSED(country
)) const
3430 return !wxDateTimeHolidayAuthority::IsHoliday(*this);
3433 // ============================================================================
3435 // ============================================================================
3437 // this enum is only used in wxTimeSpan::Format() below but we can't declare
3438 // it locally to the method as it provokes an internal compiler error in egcs
3439 // 2.91.60 when building with -O2
3450 // not all strftime(3) format specifiers make sense here because, for example,
3451 // a time span doesn't have a year nor a timezone
3453 // Here are the ones which are supported (all of them are supported by strftime
3455 // %H hour in 24 hour format
3456 // %M minute (00 - 59)
3457 // %S second (00 - 59)
3460 // Also, for MFC CTimeSpan compatibility, we support
3461 // %D number of days
3463 // And, to be better than MFC :-), we also have
3464 // %E number of wEeks
3465 // %l milliseconds (000 - 999)
3466 wxString
wxTimeSpan::Format(const wxChar
*format
) const
3468 wxCHECK_MSG( format
, _T(""), _T("NULL format in wxTimeSpan::Format") );
3471 str
.Alloc(wxStrlen(format
));
3473 // Suppose we have wxTimeSpan ts(1 /* hour */, 2 /* min */, 3 /* sec */)
3475 // Then, of course, ts.Format("%H:%M:%S") must return "01:02:03", but the
3476 // question is what should ts.Format("%S") do? The code here returns "3273"
3477 // in this case (i.e. the total number of seconds, not just seconds % 60)
3478 // because, for me, this call means "give me entire time interval in
3479 // seconds" and not "give me the seconds part of the time interval"
3481 // If we agree that it should behave like this, it is clear that the
3482 // interpretation of each format specifier depends on the presence of the
3483 // other format specs in the string: if there was "%H" before "%M", we
3484 // should use GetMinutes() % 60, otherwise just GetMinutes() &c
3486 // we remember the most important unit found so far
3487 TimeSpanPart partBiggest
= Part_MSec
;
3489 for ( const wxChar
*pch
= format
; *pch
; pch
++ )
3493 if ( ch
== _T('%') )
3495 // the start of the format specification of the printf() below
3496 wxString fmtPrefix
= _T('%');
3501 ch
= *++pch
; // get the format spec char
3505 wxFAIL_MSG( _T("invalid format character") );
3511 // skip the part below switch
3516 if ( partBiggest
< Part_Day
)
3522 partBiggest
= Part_Day
;
3527 partBiggest
= Part_Week
;
3533 if ( partBiggest
< Part_Hour
)
3539 partBiggest
= Part_Hour
;
3542 fmtPrefix
+= _T("02");
3546 n
= GetMilliseconds().ToLong();
3547 if ( partBiggest
< Part_MSec
)
3551 //else: no need to reset partBiggest to Part_MSec, it is
3552 // the least significant one anyhow
3554 fmtPrefix
+= _T("03");
3559 if ( partBiggest
< Part_Min
)
3565 partBiggest
= Part_Min
;
3568 fmtPrefix
+= _T("02");
3572 n
= GetSeconds().ToLong();
3573 if ( partBiggest
< Part_Sec
)
3579 partBiggest
= Part_Sec
;
3582 fmtPrefix
+= _T("02");
3586 str
+= wxString::Format(fmtPrefix
+ _T("ld"), n
);
3590 // normal character, just copy
3598 // ============================================================================
3599 // wxDateTimeHolidayAuthority and related classes
3600 // ============================================================================
3602 #include "wx/arrimpl.cpp"
3604 WX_DEFINE_OBJARRAY(wxDateTimeArray
);
3606 static int wxCMPFUNC_CONV
3607 wxDateTimeCompareFunc(wxDateTime
**first
, wxDateTime
**second
)
3609 wxDateTime dt1
= **first
,
3612 return dt1
== dt2
? 0 : dt1
< dt2
? -1 : +1;
3615 // ----------------------------------------------------------------------------
3616 // wxDateTimeHolidayAuthority
3617 // ----------------------------------------------------------------------------
3619 wxHolidayAuthoritiesArray
wxDateTimeHolidayAuthority::ms_authorities
;
3622 bool wxDateTimeHolidayAuthority::IsHoliday(const wxDateTime
& dt
)
3624 size_t count
= ms_authorities
.GetCount();
3625 for ( size_t n
= 0; n
< count
; n
++ )
3627 if ( ms_authorities
[n
]->DoIsHoliday(dt
) )
3638 wxDateTimeHolidayAuthority::GetHolidaysInRange(const wxDateTime
& dtStart
,
3639 const wxDateTime
& dtEnd
,
3640 wxDateTimeArray
& holidays
)
3642 wxDateTimeArray hol
;
3646 size_t count
= ms_authorities
.GetCount();
3647 for ( size_t nAuth
= 0; nAuth
< count
; nAuth
++ )
3649 ms_authorities
[nAuth
]->DoGetHolidaysInRange(dtStart
, dtEnd
, hol
);
3651 WX_APPEND_ARRAY(holidays
, hol
);
3654 holidays
.Sort(wxDateTimeCompareFunc
);
3656 return holidays
.GetCount();
3660 void wxDateTimeHolidayAuthority::ClearAllAuthorities()
3662 WX_CLEAR_ARRAY(ms_authorities
);
3666 void wxDateTimeHolidayAuthority::AddAuthority(wxDateTimeHolidayAuthority
*auth
)
3668 ms_authorities
.Add(auth
);
3671 // ----------------------------------------------------------------------------
3672 // wxDateTimeWorkDays
3673 // ----------------------------------------------------------------------------
3675 bool wxDateTimeWorkDays::DoIsHoliday(const wxDateTime
& dt
) const
3677 wxDateTime::WeekDay wd
= dt
.GetWeekDay();
3679 return (wd
== wxDateTime::Sun
) || (wd
== wxDateTime::Sat
);
3682 size_t wxDateTimeWorkDays::DoGetHolidaysInRange(const wxDateTime
& dtStart
,
3683 const wxDateTime
& dtEnd
,
3684 wxDateTimeArray
& holidays
) const
3686 if ( dtStart
> dtEnd
)
3688 wxFAIL_MSG( _T("invalid date range in GetHolidaysInRange") );
3695 // instead of checking all days, start with the first Sat after dtStart and
3696 // end with the last Sun before dtEnd
3697 wxDateTime dtSatFirst
= dtStart
.GetNextWeekDay(wxDateTime::Sat
),
3698 dtSatLast
= dtEnd
.GetPrevWeekDay(wxDateTime::Sat
),
3699 dtSunFirst
= dtStart
.GetNextWeekDay(wxDateTime::Sun
),
3700 dtSunLast
= dtEnd
.GetPrevWeekDay(wxDateTime::Sun
),
3703 for ( dt
= dtSatFirst
; dt
<= dtSatLast
; dt
+= wxDateSpan::Week() )
3708 for ( dt
= dtSunFirst
; dt
<= dtSunLast
; dt
+= wxDateSpan::Week() )
3713 return holidays
.GetCount();
3716 #endif // wxUSE_DATETIME