1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/datetime/datetime.cpp
3 // Purpose: wxDateTime unit test
4 // Author: Vadim Zeitlin
5 // Created: 2004-06-23 (extracted from samples/console/console.cpp)
7 // Copyright: (c) 2004 Vadim Zeitlin <vadim@wxwindows.org>
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
25 #include "wx/datetime.h"
26 #include "wx/ioswrap.h"
28 // need this to be able to use CPPUNIT_ASSERT_EQUAL with wxDateTime objects
29 static wxSTD ostream
& operator<<(wxSTD ostream
& ostr
, const wxDateTime
& dt
)
31 ostr
<< dt
.FormatISODate() << _T(" ") << dt
.FormatISOTime();
36 // to test Today() meaningfully we must be able to change the system date which
37 // is not usually the case, but if we're under Win32 we can try it -- define
38 // the macro below to do it
39 //#define CHANGE_SYSTEM_DATE
42 #undef CHANGE_SYSTEM_DATE
45 #ifdef CHANGE_SYSTEM_DATE
50 DateChanger(int year
, int month
, int day
, int hour
, int min
, int sec
)
61 ::GetSystemTime(&m_savedTime
);
62 ::GetTimeZoneInformation(&m_tzi
);
64 m_changed
= ::SetSystemTime(&st
) != 0;
71 ::SetSystemTime(&m_savedTime
);
72 ::SetTimeZoneInformation(&m_tzi
);
77 SYSTEMTIME m_savedTime
;
78 TIME_ZONE_INFORMATION m_tzi
;
82 #endif // CHANGE_SYSTEM_DATE
84 // ----------------------------------------------------------------------------
85 // broken down date representation used for testing
86 // ----------------------------------------------------------------------------
90 wxDateTime::wxDateTime_t day
;
91 wxDateTime::Month month
;
93 wxDateTime::wxDateTime_t hour
, min
, sec
;
95 wxDateTime::WeekDay wday
;
96 time_t gmticks
, ticks
;
98 void Init(const wxDateTime::Tm
& tm
)
107 gmticks
= ticks
= -1;
110 wxDateTime
DT() const
111 { return wxDateTime(day
, month
, year
, hour
, min
, sec
); }
113 bool SameDay(const wxDateTime::Tm
& tm
) const
115 return day
== tm
.mday
&& month
== tm
.mon
&& year
== tm
.year
;
118 wxString
Format() const
121 s
.Printf(_T("%02d:%02d:%02d %10s %02d, %4d%s"),
123 wxDateTime::GetMonthName(month
).c_str(),
125 abs(wxDateTime::ConvertYearToBC(year
)),
126 year
> 0 ? _T("AD") : _T("BC"));
130 wxString
FormatDate() const
133 s
.Printf(_T("%02d-%s-%4d%s"),
135 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
136 abs(wxDateTime::ConvertYearToBC(year
)),
137 year
> 0 ? _T("AD") : _T("BC"));
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 static const Date testDates
[] =
148 { 1, wxDateTime::Jan
, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu
, 0, -3600 },
149 { 7, wxDateTime::Feb
, 2036, 00, 00, 00, 2464730.5, wxDateTime::Thu
, -1, -1 },
150 { 8, wxDateTime::Feb
, 2036, 00, 00, 00, 2464731.5, wxDateTime::Fri
, -1, -1 },
151 { 1, wxDateTime::Jan
, 2037, 00, 00, 00, 2465059.5, wxDateTime::Thu
, -1, -1 },
152 { 1, wxDateTime::Jan
, 2038, 00, 00, 00, 2465424.5, wxDateTime::Fri
, -1, -1 },
153 { 21, wxDateTime::Jan
, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon
, -1, -1 },
154 { 29, wxDateTime::May
, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat
, 202219200, 202212000 },
155 { 29, wxDateTime::Feb
, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun
, 194400000, 194396400 },
156 { 1, wxDateTime::Jan
, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon
, -1, -1 },
157 { 1, wxDateTime::Jan
, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon
, -1, -1 },
158 { 15, wxDateTime::Oct
, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri
, -1, -1 },
159 { 4, wxDateTime::Oct
, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon
, -1, -1 },
160 { 1, wxDateTime::Mar
, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu
, -1, -1 },
161 { 1, wxDateTime::Jan
, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon
, -1, -1 },
162 { 31, wxDateTime::Dec
, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun
, -1, -1 },
163 { 1, wxDateTime::Jan
, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat
, -1, -1 },
164 { 12, wxDateTime::Aug
, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri
, -1, -1 },
165 { 12, wxDateTime::Aug
, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat
, -1, -1 },
166 { 24, wxDateTime::Nov
, -4713, 00, 00, 00, -0.5, wxDateTime::Mon
, -1, -1 },
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
174 class DateTimeTestCase
: public CppUnit::TestCase
177 DateTimeTestCase() { }
180 CPPUNIT_TEST_SUITE( DateTimeTestCase
);
181 CPPUNIT_TEST( TestLeapYears
);
182 CPPUNIT_TEST( TestTimeSet
);
183 CPPUNIT_TEST( TestTimeJDN
);
184 CPPUNIT_TEST( TestTimeWNumber
);
185 CPPUNIT_TEST( TestTimeWDays
);
186 CPPUNIT_TEST( TestTimeDST
);
187 CPPUNIT_TEST( TestTimeFormat
);
188 CPPUNIT_TEST( TestTimeSpanFormat
);
189 CPPUNIT_TEST( TestTimeTicks
);
190 CPPUNIT_TEST( TestParceRFC822
);
191 CPPUNIT_TEST( TestDateParse
);
192 CPPUNIT_TEST( TestTimeArithmetics
);
193 CPPUNIT_TEST( TestDSTBug
);
194 CPPUNIT_TEST( TestDateOnly
);
195 CPPUNIT_TEST_SUITE_END();
197 void TestLeapYears();
200 void TestTimeWNumber();
201 void TestTimeWDays();
203 void TestTimeFormat();
204 void TestTimeSpanFormat();
205 void TestTimeTicks();
206 void TestParceRFC822();
207 void TestDateParse();
208 void TestTimeArithmetics();
212 DECLARE_NO_COPY_CLASS(DateTimeTestCase
)
215 // register in the unnamed registry so that these tests are run by default
216 CPPUNIT_TEST_SUITE_REGISTRATION( DateTimeTestCase
);
218 // also include in it's own registry so that these tests can be run alone
219 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DateTimeTestCase
, "DateTimeTestCase" );
221 // ============================================================================
223 // ============================================================================
225 // test leap years detection
226 void DateTimeTestCase::TestLeapYears()
228 static const struct LeapYearTestData
244 for ( size_t n
= 0; n
< WXSIZEOF(years
); n
++ )
246 const LeapYearTestData
& y
= years
[n
];
248 CPPUNIT_ASSERT( wxDateTime::IsLeapYear(y
.year
) == y
.isLeap
);
252 // test constructing wxDateTime objects
253 void DateTimeTestCase::TestTimeSet()
255 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
257 const Date
& d1
= testDates
[n
];
258 wxDateTime dt
= d1
.DT();
263 wxString s1
= d1
.Format(),
266 CPPUNIT_ASSERT( s1
== s2
);
270 // test conversions to JDN &c
271 void DateTimeTestCase::TestTimeJDN()
273 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
275 const Date
& d
= testDates
[n
];
276 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
278 // JDNs must be computed for UTC times
279 double jdn
= dt
.FromUTC().GetJulianDayNumber();
281 CPPUNIT_ASSERT( jdn
== d
.jdn
);
284 CPPUNIT_ASSERT( dt
.GetJulianDayNumber() == jdn
);
288 // test week days computation
289 void DateTimeTestCase::TestTimeWDays()
293 for ( n
= 0; n
< WXSIZEOF(testDates
); n
++ )
295 const Date
& d
= testDates
[n
];
296 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
298 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
299 CPPUNIT_ASSERT( wday
== d
.wday
);
302 // test SetToWeekDay()
303 struct WeekDateTestData
305 Date date
; // the real date (precomputed)
306 int nWeek
; // its week index in the month
307 wxDateTime::WeekDay wday
; // the weekday
308 wxDateTime::Month month
; // the month
309 int year
; // and the year
311 wxString
Format() const
314 switch ( nWeek
< -1 ? -nWeek
: nWeek
)
316 case 1: which
= _T("first"); break;
317 case 2: which
= _T("second"); break;
318 case 3: which
= _T("third"); break;
319 case 4: which
= _T("fourth"); break;
320 case 5: which
= _T("fifth"); break;
322 case -1: which
= _T("last"); break;
327 which
+= _T(" from end");
330 s
.Printf(_T("The %s %s of %s in %d"),
332 wxDateTime::GetWeekDayName(wday
).c_str(),
333 wxDateTime::GetMonthName(month
).c_str(),
340 // the array data was generated by the following python program
342 from DateTime import *
343 from whrandom import *
346 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
347 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
349 week = DateTimeDelta(7)
352 year = randint(1900, 2100)
353 month = randint(1, 12)
355 dt = DateTime(year, month, day)
356 wday = dt.day_of_week
358 countFromEnd = choice([-1, 1])
361 while dt.month is month:
362 dt = dt - countFromEnd * week
363 weekNum = weekNum + countFromEnd
365 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
367 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
368 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
371 static const WeekDateTestData weekDatesTestData
[] =
373 { { 20, wxDateTime::Mar
, 2045, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 3, wxDateTime::Mon
, wxDateTime::Mar
, 2045 },
374 { { 5, wxDateTime::Jun
, 1985, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -4, wxDateTime::Wed
, wxDateTime::Jun
, 1985 },
375 { { 12, wxDateTime::Nov
, 1961, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -3, wxDateTime::Sun
, wxDateTime::Nov
, 1961 },
376 { { 27, wxDateTime::Feb
, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -1, wxDateTime::Fri
, wxDateTime::Feb
, 2093 },
377 { { 4, wxDateTime::Jul
, 2070, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -4, wxDateTime::Fri
, wxDateTime::Jul
, 2070 },
378 { { 2, wxDateTime::Apr
, 1906, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -5, wxDateTime::Mon
, wxDateTime::Apr
, 1906 },
379 { { 19, wxDateTime::Jul
, 2023, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -2, wxDateTime::Wed
, wxDateTime::Jul
, 2023 },
380 { { 5, wxDateTime::May
, 1958, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -4, wxDateTime::Mon
, wxDateTime::May
, 1958 },
381 { { 11, wxDateTime::Aug
, 1900, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 2, wxDateTime::Sat
, wxDateTime::Aug
, 1900 },
382 { { 14, wxDateTime::Feb
, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 2, wxDateTime::Wed
, wxDateTime::Feb
, 1945 },
383 { { 25, wxDateTime::Jul
, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -1, wxDateTime::Tue
, wxDateTime::Jul
, 1967 },
384 { { 9, wxDateTime::May
, 1916, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -4, wxDateTime::Tue
, wxDateTime::May
, 1916 },
385 { { 20, wxDateTime::Jun
, 1927, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 3, wxDateTime::Mon
, wxDateTime::Jun
, 1927 },
386 { { 2, wxDateTime::Aug
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, wxDateTime::Wed
, wxDateTime::Aug
, 2000 },
387 { { 20, wxDateTime::Apr
, 2044, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 3, wxDateTime::Wed
, wxDateTime::Apr
, 2044 },
388 { { 20, wxDateTime::Feb
, 1932, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -2, wxDateTime::Sat
, wxDateTime::Feb
, 1932 },
389 { { 25, wxDateTime::Jul
, 2069, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 4, wxDateTime::Thu
, wxDateTime::Jul
, 2069 },
390 { { 3, wxDateTime::Apr
, 1925, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, wxDateTime::Fri
, wxDateTime::Apr
, 1925 },
391 { { 21, wxDateTime::Mar
, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 3, wxDateTime::Sat
, wxDateTime::Mar
, 2093 },
392 { { 3, wxDateTime::Dec
, 2074, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -5, wxDateTime::Mon
, wxDateTime::Dec
, 2074 }
396 for ( n
= 0; n
< WXSIZEOF(weekDatesTestData
); n
++ )
398 const WeekDateTestData
& wd
= weekDatesTestData
[n
];
400 dt
.SetToWeekDay(wd
.wday
, wd
.nWeek
, wd
.month
, wd
.year
);
402 const Date
& d
= wd
.date
;
403 CPPUNIT_ASSERT( d
.SameDay(dt
.GetTm()) );
407 // test the computation of (ISO) week numbers
408 void DateTimeTestCase::TestTimeWNumber()
410 struct WeekNumberTestData
412 Date date
; // the date
413 wxDateTime::wxDateTime_t week
; // the week number in the year
414 wxDateTime::wxDateTime_t wmon
; // the week number in the month
415 wxDateTime::wxDateTime_t wmon2
; // same but week starts with Sun
416 wxDateTime::wxDateTime_t dnum
; // day number in the year
419 // data generated with the following python script:
421 from DateTime import *
422 from whrandom import *
425 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
426 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
428 def GetMonthWeek(dt):
429 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
431 weekNumMonth = weekNumMonth + 53
434 def GetLastSundayBefore(dt):
435 if dt.iso_week[2] == 7:
438 return dt - DateTimeDelta(dt.iso_week[2])
441 year = randint(1900, 2100)
442 month = randint(1, 12)
444 dt = DateTime(year, month, day)
445 dayNum = dt.day_of_year
446 weekNum = dt.iso_week[1]
447 weekNumMonth = GetMonthWeek(dt)
450 dtSunday = GetLastSundayBefore(dt)
452 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
453 weekNumMonth2 = weekNumMonth2 + 1
454 dtSunday = dtSunday - DateTimeDelta(7)
456 data = { 'day': rjust(`day`, 2), \
457 'month': monthNames[month - 1], \
459 'weekNum': rjust(`weekNum`, 2), \
460 'weekNumMonth': weekNumMonth, \
461 'weekNumMonth2': weekNumMonth2, \
462 'dayNum': rjust(`dayNum`, 3) }
464 print " { { %(day)s, "\
465 "wxDateTime::%(month)s, "\
468 "%(weekNumMonth)s, "\
469 "%(weekNumMonth2)s, "\
470 "%(dayNum)s }," % data
473 static const WeekNumberTestData weekNumberTestDates
[] =
475 { { 27, wxDateTime::Dec
, 1966, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 52, 5, 5, 361 },
476 { { 22, wxDateTime::Jul
, 1926, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 29, 4, 4, 203 },
477 { { 22, wxDateTime::Oct
, 2076, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 43, 4, 4, 296 },
478 { { 1, wxDateTime::Jul
, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 26, 1, 1, 182 },
479 { { 8, wxDateTime::Nov
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 46, 2, 2, 313 },
480 { { 21, wxDateTime::Mar
, 1920, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 12, 3, 4, 81 },
481 { { 7, wxDateTime::Jan
, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, 2, 2, 7 },
482 { { 19, wxDateTime::Oct
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 42, 4, 4, 292 },
483 { { 13, wxDateTime::Aug
, 1955, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 32, 2, 2, 225 },
484 { { 18, wxDateTime::Jul
, 2087, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 29, 3, 3, 199 },
485 { { 2, wxDateTime::Sep
, 2028, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 35, 1, 1, 246 },
486 { { 28, wxDateTime::Jul
, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 30, 5, 4, 209 },
487 { { 15, wxDateTime::Jun
, 1901, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 24, 3, 3, 166 },
488 { { 10, wxDateTime::Oct
, 1939, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 41, 3, 2, 283 },
489 { { 3, wxDateTime::Dec
, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 48, 1, 1, 337 },
490 { { 23, wxDateTime::Feb
, 1940, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 8, 4, 4, 54 },
491 { { 2, wxDateTime::Jan
, 1987, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, 1, 1, 2 },
492 { { 11, wxDateTime::Aug
, 2079, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 32, 2, 2, 223 },
493 { { 2, wxDateTime::Feb
, 2063, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 5, 1, 1, 33 },
494 { { 16, wxDateTime::Oct
, 1942, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 42, 3, 3, 289 },
495 { { 30, wxDateTime::Dec
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, 5, 5, 364 },
496 { { 2, wxDateTime::Jan
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, 1, 1, 2 },
499 for ( size_t n
= 0; n
< WXSIZEOF(weekNumberTestDates
); n
++ )
501 const WeekNumberTestData
& wn
= weekNumberTestDates
[n
];
502 const Date
& d
= wn
.date
;
504 wxDateTime dt
= d
.DT();
506 wxDateTime::wxDateTime_t
507 week
= dt
.GetWeekOfYear(wxDateTime::Monday_First
),
508 wmon
= dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
509 wmon2
= dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
510 dnum
= dt
.GetDayOfYear();
512 CPPUNIT_ASSERT( dnum
== wn
.dnum
);
513 CPPUNIT_ASSERT( wmon
== wn
.wmon
);
514 CPPUNIT_ASSERT( wmon2
== wn
.wmon2
);
515 CPPUNIT_ASSERT( week
== wn
.week
);
518 if ( week
== 1 && d
.month
!= wxDateTime::Jan
)
520 // this means we're in the first week of the next year
525 dt2
= wxDateTime::SetToWeekOfYear(year
, week
, dt
.GetWeekDay());
526 CPPUNIT_ASSERT( dt2
== dt
);
530 // test DST applicability
531 void DateTimeTestCase::TestTimeDST()
533 // taken from http://www.energy.ca.gov/daylightsaving.html
534 static const Date datesDST
[2][2004 - 1900 + 1] =
537 { 1, wxDateTime::Apr
, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
538 { 7, wxDateTime::Apr
, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
539 { 5, wxDateTime::Apr
, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
540 { 4, wxDateTime::Apr
, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
541 { 3, wxDateTime::Apr
, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
542 { 2, wxDateTime::Apr
, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
543 { 7, wxDateTime::Apr
, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
544 { 6, wxDateTime::Apr
, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
545 { 5, wxDateTime::Apr
, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
546 { 4, wxDateTime::Apr
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
547 { 2, wxDateTime::Apr
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
548 { 1, wxDateTime::Apr
, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
549 { 7, wxDateTime::Apr
, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
550 { 6, wxDateTime::Apr
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
551 { 4, wxDateTime::Apr
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
554 { 28, wxDateTime::Oct
, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
555 { 27, wxDateTime::Oct
, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
556 { 25, wxDateTime::Oct
, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
557 { 31, wxDateTime::Oct
, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
558 { 30, wxDateTime::Oct
, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
559 { 29, wxDateTime::Oct
, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
560 { 27, wxDateTime::Oct
, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
561 { 26, wxDateTime::Oct
, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
562 { 25, wxDateTime::Oct
, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
563 { 31, wxDateTime::Oct
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
564 { 29, wxDateTime::Oct
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
565 { 28, wxDateTime::Oct
, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
566 { 27, wxDateTime::Oct
, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
567 { 26, wxDateTime::Oct
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
568 { 31, wxDateTime::Oct
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
572 for ( int year
= 1990; year
< 2005; year
++ )
574 wxDateTime dtBegin
= wxDateTime::GetBeginDST(year
, wxDateTime::USA
),
575 dtEnd
= wxDateTime::GetEndDST(year
, wxDateTime::USA
);
577 size_t n
= year
- 1990;
578 const Date
& dBegin
= datesDST
[0][n
];
579 const Date
& dEnd
= datesDST
[1][n
];
581 CPPUNIT_ASSERT( dBegin
.SameDay(dtBegin
.GetTm()) );
582 CPPUNIT_ASSERT( dEnd
.SameDay(dtEnd
.GetTm()) );
586 // test wxDateTime -> text conversion
587 void DateTimeTestCase::TestTimeFormat()
589 // some information may be lost during conversion, so store what kind
590 // of info should we recover after a round trip
593 CompareNone
, // don't try comparing
594 CompareBoth
, // dates and times should be identical
595 CompareYear
, // don't compare centuries (fails for 2 digit years)
596 CompareDate
, // dates only
597 CompareTime
// time only
602 CompareKind compareKind
;
603 const wxChar
*format
;
604 } formatTestFormats
[] =
606 { CompareYear
, _T("---> %c") }, // %c could use 2 digit years
607 { CompareDate
, _T("Date is %A, %d of %B, in year %Y") },
608 { CompareYear
, _T("Date is %x, time is %X") }, // %x could use 2 digits
609 { CompareTime
, _T("Time is %H:%M:%S or %I:%M:%S %p") },
610 { CompareNone
, _T("The day of year: %j, the week of year: %W") },
611 { CompareDate
, _T("ISO date without separators: %Y%m%d") },
614 static const Date formatTestDates
[] =
616 { 29, wxDateTime::May
, 1976, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
617 { 31, wxDateTime::Dec
, 1999, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
618 { 6, wxDateTime::Feb
, 1937, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
619 { 6, wxDateTime::Feb
, 1856, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
620 { 6, wxDateTime::Feb
, 1857, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
621 { 29, wxDateTime::May
, 2076, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
622 { 29, wxDateTime::Feb
, 2400, 02, 15, 25, 0.0, wxDateTime::Inv_WeekDay
},
624 // Need to add support for BCE dates.
625 { 01, wxDateTime::Jan
, -52, 03, 16, 47, 0.0, wxDateTime::Inv_WeekDay
},
629 for ( size_t d
= 0; d
< WXSIZEOF(formatTestDates
); d
++ )
631 wxDateTime dt
= formatTestDates
[d
].DT();
632 for ( size_t n
= 0; n
< WXSIZEOF(formatTestFormats
); n
++ )
634 const wxChar
*fmt
= formatTestFormats
[n
].format
;
635 wxString s
= dt
.Format(fmt
);
637 // what can we recover?
638 CompareKind kind
= formatTestFormats
[n
].compareKind
;
642 const wxChar
*result
= dt2
.ParseFormat(s
.c_str(), fmt
);
645 // converion failed - should it have?
646 CPPUNIT_ASSERT( kind
== CompareNone
);
648 else // conversion succeeded
650 // should have parsed the entire string
651 CPPUNIT_ASSERT( !*result
);
656 if ( dt2
.GetCentury() != dt
.GetCentury() )
658 CPPUNIT_ASSERT_EQUAL(dt
.GetYear() % 100,
659 dt2
.GetYear() % 100);
661 dt2
.SetYear(dt
.GetYear());
663 // fall through and compare everything
666 CPPUNIT_ASSERT_EQUAL( dt
, dt2
);
670 CPPUNIT_ASSERT( dt
.IsSameDate(dt2
) );
674 CPPUNIT_ASSERT( dt
.IsSameTime(dt2
) );
678 wxFAIL_MSG( _T("unexpected") );
686 void DateTimeTestCase::TestTimeSpanFormat()
688 static const struct TimeSpanFormatTestData
690 long h
, min
, sec
, msec
;
692 const wxChar
*result
;
695 { 12, 34, 56, 789, _T("%H:%M:%S.%l"), _T("12:34:56.789") },
696 { 1, 2, 3, 0, _T("%H:%M:%S"), _T("01:02:03") },
697 { 1, 2, 3, 0, _T("%S"), _T("3723") },
698 { -1, -2, -3, 0, _T("%S"), _T("-3723") },
699 { -1, -2, -3, 0, _T("%H:%M:%S"), _T("-01:02:03") },
700 { 26, 0, 0, 0, _T("%H"), _T("26") },
701 { 26, 0, 0, 0, _T("%D, %H"), _T("1, 02") },
702 { -26, 0, 0, 0, _T("%H"), _T("-26") },
703 { -26, 0, 0, 0, _T("%D, %H"), _T("-1, 02") },
704 { 219, 0, 0, 0, _T("%H"), _T("219") },
705 { 219, 0, 0, 0, _T("%D, %H"), _T("9, 03") },
706 { 219, 0, 0, 0, _T("%E, %D, %H"), _T("1, 2, 03") },
709 for ( size_t n
= 0; n
< WXSIZEOF(testSpans
); n
++ )
711 const TimeSpanFormatTestData
& td
= testSpans
[n
];
712 wxTimeSpan
ts(td
.h
, td
.min
, td
.sec
, td
.msec
);
713 CPPUNIT_ASSERT_EQUAL( wxString(td
.result
), ts
.Format(td
.fmt
) );
717 void DateTimeTestCase::TestTimeTicks()
719 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
721 const Date
& d
= testDates
[n
];
725 wxDateTime dt
= d
.DT();
726 //RN: Translate according to test's time zone
727 //2nd param is to ignore DST - it's already factored
729 dt
.MakeTimezone(wxDateTime::WEST
, true);
730 long ticks
= (dt
.GetValue() / 1000).ToLong();
731 CPPUNIT_ASSERT( ticks
== d
.ticks
);
733 dt
= d
.DT().FromTimezone(wxDateTime::GMT0
);
734 ticks
= (dt
.GetValue() / 1000).ToLong();
735 CPPUNIT_ASSERT( ticks
== d
.gmticks
);
739 // test parsing dates in RFC822 format
740 void DateTimeTestCase::TestParceRFC822()
742 static const struct ParseTestData
744 const wxChar
*rfc822
;
745 Date date
; // NB: this should be in UTC
750 _T("Sat, 18 Dec 1999 00:46:40 +0100"),
751 { 17, wxDateTime::Dec
, 1999, 23, 46, 40, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
755 _T("Wed, 1 Dec 1999 05:17:20 +0300"),
756 { 1, wxDateTime::Dec
, 1999, 2, 17, 20, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
760 _T("Sun, 28 Aug 2005 03:31:30 +0200"),
761 { 28, wxDateTime::Aug
, 2005, 1, 31, 30, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
766 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
769 if ( dt
.ParseRfc822Date(parseTestDates
[n
].rfc822
) )
771 CPPUNIT_ASSERT( parseTestDates
[n
].good
);
773 wxDateTime dtReal
= parseTestDates
[n
].date
.DT().FromUTC();
774 CPPUNIT_ASSERT_EQUAL( dtReal
, dt
);
776 else // failed to parse
778 CPPUNIT_ASSERT( !parseTestDates
[n
].good
);
783 // test parsing dates in free format
784 void DateTimeTestCase::TestDateParse()
786 static const struct ParseTestData
789 Date date
; // NB: this should be in UTC
793 { _T("21 Mar 2006"), { 21, wxDateTime::Mar
, 2006 }, true },
794 { _T("29 Feb 1976"), { 29, wxDateTime::Feb
, 1976 }, true },
795 { _T("Feb 29 1976"), { 29, wxDateTime::Feb
, 1976 }, true },
796 { _T("31/03/06"), { 31, wxDateTime::Mar
, 6 }, true },
797 { _T("31/03/2006"), { 31, wxDateTime::Mar
, 2006 }, true },
799 // some invalid ones too
800 { _T("29 Feb 2006") },
802 { _T("bloordyblop") }
807 CPPUNIT_ASSERT( dt
.ParseDate(_T("today")) );
808 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), dt
);
810 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
813 if ( dt
.ParseDate(parseTestDates
[n
].str
) )
815 CPPUNIT_ASSERT( parseTestDates
[n
].good
);
817 CPPUNIT_ASSERT_EQUAL( parseTestDates
[n
].date
.DT(), dt
);
819 else // failed to parse
821 CPPUNIT_ASSERT( !parseTestDates
[n
].good
);
826 void DateTimeTestCase::TestTimeArithmetics()
828 static const wxDateSpan testArithmData
[] =
836 // the test will *not* work with arbitrary date!
837 wxDateTime
dt(2, wxDateTime::Dec
, 1999),
841 for ( size_t n
= 0; n
< WXSIZEOF(testArithmData
); n
++ )
843 const wxDateSpan
& span
= testArithmData
[n
];
847 CPPUNIT_ASSERT( dt1
- span
== dt
);
848 CPPUNIT_ASSERT( dt2
+ span
== dt
);
849 CPPUNIT_ASSERT( dt2
+ 2*span
== dt1
);
853 void DateTimeTestCase::TestDSTBug()
855 /////////////////////////
857 wxDateTime dt
= wxDateTime::GetEndDST(2004);
858 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
859 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
860 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
861 CPPUNIT_ASSERT_EQUAL(2, (int)dt
.GetHour());
862 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
863 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
864 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
866 /////////////////////////
869 CPPUNIT_ASSERT_EQUAL(5, (int)dt
.GetHour());
871 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
872 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
873 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
874 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetHour());
875 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
876 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
877 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
880 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
882 /////////////////////////
884 #ifdef CHANGE_SYSTEM_DATE
886 DateChanger
change(2004, 10, 31, 5, 0, 0);
887 dt
= wxDateTime::Today();
890 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
891 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
892 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
893 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetHour());
894 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
895 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
896 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
898 /////////////////////////
899 // Test Set(hour, minute, second, milli)
902 DateChanger
change(2004, 10, 31, 5, 0, 0);
904 dt2
.Set(5, 30, 0, 0);
907 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
908 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
909 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
910 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
911 CPPUNIT_ASSERT_EQUAL(30, (int)dt
.GetMinute());
912 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
913 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
915 CPPUNIT_ASSERT_EQUAL(31, (int)dt2
.GetDay());
916 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt2
.GetMonth());
917 CPPUNIT_ASSERT_EQUAL(2004, (int)dt2
.GetYear());
918 CPPUNIT_ASSERT_EQUAL(5, (int)dt2
.GetHour());
919 CPPUNIT_ASSERT_EQUAL(30, (int)dt2
.GetMinute());
920 CPPUNIT_ASSERT_EQUAL(0, (int)dt2
.GetSecond());
921 CPPUNIT_ASSERT_EQUAL(0, (int)dt2
.GetMillisecond());
922 #endif // CHANGE_SYSTEM_DATE
925 void DateTimeTestCase::TestDateOnly()
927 wxDateTime
dt(19, wxDateTime::Jan
, 2007, 15, 01, 00);
929 static const wxDateTime::wxDateTime_t DATE_ZERO
= 0;
930 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetHour() );
931 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetMinute() );
932 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetSecond() );
933 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetMillisecond() );
936 CPPUNIT_ASSERT_EQUAL( wxDateTime(19, wxDateTime::Jan
, 2007), dt
);
938 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), wxDateTime::Now().GetDateOnly() );
941 #endif // wxUSE_DATETIME