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 // ----------------------------------------------------------------------------
23 #include "wx/datetime.h"
25 // to test Today() meaningfully we must be able to change the system date which
26 // is not usually the case, but if we're under Win32 we can try it -- define
27 // the macro below to do it
28 //#define CHANGE_SYSTEM_DATE
31 #undef CHANGE_SYSTEM_DATE
34 #ifdef CHANGE_SYSTEM_DATE
39 DateChanger(int year
, int month
, int day
, int hour
, int min
, int sec
)
50 ::GetSystemTime(&m_savedTime
);
51 ::GetTimeZoneInformation(&m_tzi
);
53 m_changed
= ::SetSystemTime(&st
) != 0;
60 ::SetSystemTime(&m_savedTime
);
61 ::SetTimeZoneInformation(&m_tzi
);
66 SYSTEMTIME m_savedTime
;
67 TIME_ZONE_INFORMATION m_tzi
;
71 #endif // CHANGE_SYSTEM_DATE
73 // ----------------------------------------------------------------------------
74 // broken down date representation used for testing
75 // ----------------------------------------------------------------------------
79 wxDateTime::wxDateTime_t day
;
80 wxDateTime::Month month
;
82 wxDateTime::wxDateTime_t hour
, min
, sec
;
84 wxDateTime::WeekDay wday
;
85 time_t gmticks
, ticks
;
87 void Init(const wxDateTime::Tm
& tm
)
100 { return wxDateTime(day
, month
, year
, hour
, min
, sec
); }
102 bool SameDay(const wxDateTime::Tm
& tm
) const
104 return day
== tm
.mday
&& month
== tm
.mon
&& year
== tm
.year
;
107 wxString
Format() const
110 s
.Printf(_T("%02d:%02d:%02d %10s %02d, %4d%s"),
112 wxDateTime::GetMonthName(month
).c_str(),
114 abs(wxDateTime::ConvertYearToBC(year
)),
115 year
> 0 ? _T("AD") : _T("BC"));
119 wxString
FormatDate() const
122 s
.Printf(_T("%02d-%s-%4d%s"),
124 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
125 abs(wxDateTime::ConvertYearToBC(year
)),
126 year
> 0 ? _T("AD") : _T("BC"));
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 static const Date testDates
[] =
137 { 1, wxDateTime::Jan
, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu
, 0, -3600 },
138 { 7, wxDateTime::Feb
, 2036, 00, 00, 00, 2464730.5, wxDateTime::Thu
, -1, -1 },
139 { 8, wxDateTime::Feb
, 2036, 00, 00, 00, 2464731.5, wxDateTime::Fri
, -1, -1 },
140 { 1, wxDateTime::Jan
, 2037, 00, 00, 00, 2465059.5, wxDateTime::Thu
, -1, -1 },
141 { 1, wxDateTime::Jan
, 2038, 00, 00, 00, 2465424.5, wxDateTime::Fri
, -1, -1 },
142 { 21, wxDateTime::Jan
, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon
, -1, -1 },
143 { 29, wxDateTime::May
, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat
, 202219200, 202212000 },
144 { 29, wxDateTime::Feb
, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun
, 194400000, 194396400 },
145 { 1, wxDateTime::Jan
, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon
, -1, -1 },
146 { 1, wxDateTime::Jan
, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon
, -1, -1 },
147 { 15, wxDateTime::Oct
, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri
, -1, -1 },
148 { 4, wxDateTime::Oct
, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon
, -1, -1 },
149 { 1, wxDateTime::Mar
, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu
, -1, -1 },
150 { 1, wxDateTime::Jan
, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon
, -1, -1 },
151 { 31, wxDateTime::Dec
, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun
, -1, -1 },
152 { 1, wxDateTime::Jan
, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat
, -1, -1 },
153 { 12, wxDateTime::Aug
, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri
, -1, -1 },
154 { 12, wxDateTime::Aug
, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat
, -1, -1 },
155 { 24, wxDateTime::Nov
, -4713, 00, 00, 00, -0.5, wxDateTime::Mon
, -1, -1 },
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
163 class DateTimeTestCase
: public CppUnit::TestCase
166 DateTimeTestCase() { }
169 CPPUNIT_TEST_SUITE( DateTimeTestCase
);
170 CPPUNIT_TEST( TestLeapYears
);
171 CPPUNIT_TEST( TestTimeSet
);
172 CPPUNIT_TEST( TestTimeJDN
);
173 CPPUNIT_TEST( TestTimeWNumber
);
174 CPPUNIT_TEST( TestTimeWDays
);
175 CPPUNIT_TEST( TestTimeDST
);
176 CPPUNIT_TEST( TestTimeFormat
);
177 CPPUNIT_TEST( TestTimeTicks
);
178 CPPUNIT_TEST( TestTimeParse
);
179 CPPUNIT_TEST( TestTimeArithmetics
);
180 CPPUNIT_TEST( TestDSTBug
);
181 CPPUNIT_TEST_SUITE_END();
183 void TestLeapYears();
186 void TestTimeWNumber();
187 void TestTimeWDays();
189 void TestTimeFormat();
190 void TestTimeTicks();
191 void TestTimeParse();
192 void TestTimeArithmetics();
195 DECLARE_NO_COPY_CLASS(DateTimeTestCase
)
198 // register in the unnamed registry so that these tests are run by default
199 CPPUNIT_TEST_SUITE_REGISTRATION( DateTimeTestCase
);
201 // also include in it's own registry so that these tests can be run alone
202 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DateTimeTestCase
, "DateTimeTestCase" );
204 // ============================================================================
206 // ============================================================================
208 // test leap years detection
209 void DateTimeTestCase::TestLeapYears()
211 static const struct LeapYearTestData
227 for ( size_t n
= 0; n
< WXSIZEOF(years
); n
++ )
229 const LeapYearTestData
& y
= years
[n
];
231 CPPUNIT_ASSERT( wxDateTime::IsLeapYear(y
.year
) == y
.isLeap
);
235 // test constructing wxDateTime objects
236 void DateTimeTestCase::TestTimeSet()
238 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
240 const Date
& d1
= testDates
[n
];
241 wxDateTime dt
= d1
.DT();
246 wxString s1
= d1
.Format(),
249 CPPUNIT_ASSERT( s1
== s2
);
253 // test conversions to JDN &c
254 void DateTimeTestCase::TestTimeJDN()
256 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
258 const Date
& d
= testDates
[n
];
259 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
260 double jdn
= dt
.GetJulianDayNumber();
262 CPPUNIT_ASSERT( jdn
== d
.jdn
);
265 CPPUNIT_ASSERT( dt
.GetJulianDayNumber() == jdn
);
269 // test week days computation
270 void DateTimeTestCase::TestTimeWDays()
274 for ( n
= 0; n
< WXSIZEOF(testDates
); n
++ )
276 const Date
& d
= testDates
[n
];
277 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
279 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
280 CPPUNIT_ASSERT( wday
== d
.wday
);
283 // test SetToWeekDay()
284 struct WeekDateTestData
286 Date date
; // the real date (precomputed)
287 int nWeek
; // its week index in the month
288 wxDateTime::WeekDay wday
; // the weekday
289 wxDateTime::Month month
; // the month
290 int year
; // and the year
292 wxString
Format() const
295 switch ( nWeek
< -1 ? -nWeek
: nWeek
)
297 case 1: which
= _T("first"); break;
298 case 2: which
= _T("second"); break;
299 case 3: which
= _T("third"); break;
300 case 4: which
= _T("fourth"); break;
301 case 5: which
= _T("fifth"); break;
303 case -1: which
= _T("last"); break;
308 which
+= _T(" from end");
311 s
.Printf(_T("The %s %s of %s in %d"),
313 wxDateTime::GetWeekDayName(wday
).c_str(),
314 wxDateTime::GetMonthName(month
).c_str(),
321 // the array data was generated by the following python program
323 from DateTime import *
324 from whrandom import *
327 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
328 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
330 week = DateTimeDelta(7)
333 year = randint(1900, 2100)
334 month = randint(1, 12)
336 dt = DateTime(year, month, day)
337 wday = dt.day_of_week
339 countFromEnd = choice([-1, 1])
342 while dt.month is month:
343 dt = dt - countFromEnd * week
344 weekNum = weekNum + countFromEnd
346 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
348 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
349 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
352 static const WeekDateTestData weekDatesTestData
[] =
354 { { 20, wxDateTime::Mar
, 2045, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 3, wxDateTime::Mon
, wxDateTime::Mar
, 2045 },
355 { { 5, wxDateTime::Jun
, 1985, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -4, wxDateTime::Wed
, wxDateTime::Jun
, 1985 },
356 { { 12, wxDateTime::Nov
, 1961, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -3, wxDateTime::Sun
, wxDateTime::Nov
, 1961 },
357 { { 27, wxDateTime::Feb
, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -1, wxDateTime::Fri
, wxDateTime::Feb
, 2093 },
358 { { 4, wxDateTime::Jul
, 2070, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -4, wxDateTime::Fri
, wxDateTime::Jul
, 2070 },
359 { { 2, wxDateTime::Apr
, 1906, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -5, wxDateTime::Mon
, wxDateTime::Apr
, 1906 },
360 { { 19, wxDateTime::Jul
, 2023, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -2, wxDateTime::Wed
, wxDateTime::Jul
, 2023 },
361 { { 5, wxDateTime::May
, 1958, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -4, wxDateTime::Mon
, wxDateTime::May
, 1958 },
362 { { 11, wxDateTime::Aug
, 1900, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 2, wxDateTime::Sat
, wxDateTime::Aug
, 1900 },
363 { { 14, wxDateTime::Feb
, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 2, wxDateTime::Wed
, wxDateTime::Feb
, 1945 },
364 { { 25, wxDateTime::Jul
, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -1, wxDateTime::Tue
, wxDateTime::Jul
, 1967 },
365 { { 9, wxDateTime::May
, 1916, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -4, wxDateTime::Tue
, wxDateTime::May
, 1916 },
366 { { 20, wxDateTime::Jun
, 1927, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 3, wxDateTime::Mon
, wxDateTime::Jun
, 1927 },
367 { { 2, wxDateTime::Aug
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, wxDateTime::Wed
, wxDateTime::Aug
, 2000 },
368 { { 20, wxDateTime::Apr
, 2044, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 3, wxDateTime::Wed
, wxDateTime::Apr
, 2044 },
369 { { 20, wxDateTime::Feb
, 1932, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -2, wxDateTime::Sat
, wxDateTime::Feb
, 1932 },
370 { { 25, wxDateTime::Jul
, 2069, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 4, wxDateTime::Thu
, wxDateTime::Jul
, 2069 },
371 { { 3, wxDateTime::Apr
, 1925, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, wxDateTime::Fri
, wxDateTime::Apr
, 1925 },
372 { { 21, wxDateTime::Mar
, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 3, wxDateTime::Sat
, wxDateTime::Mar
, 2093 },
373 { { 3, wxDateTime::Dec
, 2074, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -5, wxDateTime::Mon
, wxDateTime::Dec
, 2074 }
377 for ( n
= 0; n
< WXSIZEOF(weekDatesTestData
); n
++ )
379 const WeekDateTestData
& wd
= weekDatesTestData
[n
];
381 dt
.SetToWeekDay(wd
.wday
, wd
.nWeek
, wd
.month
, wd
.year
);
383 const Date
& d
= wd
.date
;
384 CPPUNIT_ASSERT( d
.SameDay(dt
.GetTm()) );
388 // test the computation of (ISO) week numbers
389 void DateTimeTestCase::TestTimeWNumber()
391 struct WeekNumberTestData
393 Date date
; // the date
394 wxDateTime::wxDateTime_t week
; // the week number in the year
395 wxDateTime::wxDateTime_t wmon
; // the week number in the month
396 wxDateTime::wxDateTime_t wmon2
; // same but week starts with Sun
397 wxDateTime::wxDateTime_t dnum
; // day number in the year
400 // data generated with the following python script:
402 from DateTime import *
403 from whrandom import *
406 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
407 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
409 def GetMonthWeek(dt):
410 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
412 weekNumMonth = weekNumMonth + 53
415 def GetLastSundayBefore(dt):
416 if dt.iso_week[2] == 7:
419 return dt - DateTimeDelta(dt.iso_week[2])
422 year = randint(1900, 2100)
423 month = randint(1, 12)
425 dt = DateTime(year, month, day)
426 dayNum = dt.day_of_year
427 weekNum = dt.iso_week[1]
428 weekNumMonth = GetMonthWeek(dt)
431 dtSunday = GetLastSundayBefore(dt)
433 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
434 weekNumMonth2 = weekNumMonth2 + 1
435 dtSunday = dtSunday - DateTimeDelta(7)
437 data = { 'day': rjust(`day`, 2), \
438 'month': monthNames[month - 1], \
440 'weekNum': rjust(`weekNum`, 2), \
441 'weekNumMonth': weekNumMonth, \
442 'weekNumMonth2': weekNumMonth2, \
443 'dayNum': rjust(`dayNum`, 3) }
445 print " { { %(day)s, "\
446 "wxDateTime::%(month)s, "\
449 "%(weekNumMonth)s, "\
450 "%(weekNumMonth2)s, "\
451 "%(dayNum)s }," % data
454 static const WeekNumberTestData weekNumberTestDates
[] =
456 { { 27, wxDateTime::Dec
, 1966, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 52, 5, 5, 361 },
457 { { 22, wxDateTime::Jul
, 1926, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 29, 4, 4, 203 },
458 { { 22, wxDateTime::Oct
, 2076, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 43, 4, 4, 296 },
459 { { 1, wxDateTime::Jul
, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 26, 1, 1, 182 },
460 { { 8, wxDateTime::Nov
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 46, 2, 2, 313 },
461 { { 21, wxDateTime::Mar
, 1920, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 12, 3, 4, 81 },
462 { { 7, wxDateTime::Jan
, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, 2, 2, 7 },
463 { { 19, wxDateTime::Oct
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 42, 4, 4, 292 },
464 { { 13, wxDateTime::Aug
, 1955, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 32, 2, 2, 225 },
465 { { 18, wxDateTime::Jul
, 2087, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 29, 3, 3, 199 },
466 { { 2, wxDateTime::Sep
, 2028, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 35, 1, 1, 246 },
467 { { 28, wxDateTime::Jul
, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 30, 5, 4, 209 },
468 { { 15, wxDateTime::Jun
, 1901, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 24, 3, 3, 166 },
469 { { 10, wxDateTime::Oct
, 1939, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 41, 3, 2, 283 },
470 { { 3, wxDateTime::Dec
, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 48, 1, 1, 337 },
471 { { 23, wxDateTime::Feb
, 1940, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 8, 4, 4, 54 },
472 { { 2, wxDateTime::Jan
, 1987, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, 1, 1, 2 },
473 { { 11, wxDateTime::Aug
, 2079, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 32, 2, 2, 223 },
474 { { 2, wxDateTime::Feb
, 2063, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 5, 1, 1, 33 },
475 { { 16, wxDateTime::Oct
, 1942, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 42, 3, 3, 289 },
476 { { 30, wxDateTime::Dec
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, 5, 5, 364 },
477 { { 2, wxDateTime::Jan
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, 1, 1, 2 },
480 for ( size_t n
= 0; n
< WXSIZEOF(weekNumberTestDates
); n
++ )
482 const WeekNumberTestData
& wn
= weekNumberTestDates
[n
];
483 const Date
& d
= wn
.date
;
485 wxDateTime dt
= d
.DT();
487 wxDateTime::wxDateTime_t
488 week
= dt
.GetWeekOfYear(wxDateTime::Monday_First
),
489 wmon
= dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
490 wmon2
= dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
491 dnum
= dt
.GetDayOfYear();
493 CPPUNIT_ASSERT( dnum
== wn
.dnum
);
494 CPPUNIT_ASSERT( wmon
== wn
.wmon
);
495 CPPUNIT_ASSERT( wmon2
== wn
.wmon2
);
496 CPPUNIT_ASSERT( week
== wn
.week
);
499 if ( week
== 1 && d
.month
!= wxDateTime::Jan
)
501 // this means we're in the first week of the next year
506 dt2
= wxDateTime::SetToWeekOfYear(year
, week
, dt
.GetWeekDay());
507 CPPUNIT_ASSERT( dt2
== dt
);
511 // test DST applicability
512 void DateTimeTestCase::TestTimeDST()
514 // taken from http://www.energy.ca.gov/daylightsaving.html
515 static const Date datesDST
[2][2004 - 1900 + 1] =
518 { 1, wxDateTime::Apr
, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
519 { 7, wxDateTime::Apr
, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
520 { 5, wxDateTime::Apr
, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
521 { 4, wxDateTime::Apr
, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
522 { 3, wxDateTime::Apr
, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
523 { 2, wxDateTime::Apr
, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
524 { 7, wxDateTime::Apr
, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
525 { 6, wxDateTime::Apr
, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
526 { 5, wxDateTime::Apr
, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
527 { 4, wxDateTime::Apr
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
528 { 2, wxDateTime::Apr
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
529 { 1, wxDateTime::Apr
, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
530 { 7, wxDateTime::Apr
, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
531 { 6, wxDateTime::Apr
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
532 { 4, wxDateTime::Apr
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
535 { 28, wxDateTime::Oct
, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
536 { 27, wxDateTime::Oct
, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
537 { 25, wxDateTime::Oct
, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
538 { 31, wxDateTime::Oct
, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
539 { 30, wxDateTime::Oct
, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
540 { 29, wxDateTime::Oct
, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
541 { 27, wxDateTime::Oct
, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
542 { 26, wxDateTime::Oct
, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
543 { 25, wxDateTime::Oct
, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
544 { 31, wxDateTime::Oct
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
545 { 29, wxDateTime::Oct
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
546 { 28, wxDateTime::Oct
, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
547 { 27, wxDateTime::Oct
, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
548 { 26, wxDateTime::Oct
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
549 { 31, wxDateTime::Oct
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
553 for ( int year
= 1990; year
< 2005; year
++ )
555 wxDateTime dtBegin
= wxDateTime::GetBeginDST(year
, wxDateTime::USA
),
556 dtEnd
= wxDateTime::GetEndDST(year
, wxDateTime::USA
);
558 size_t n
= year
- 1990;
559 const Date
& dBegin
= datesDST
[0][n
];
560 const Date
& dEnd
= datesDST
[1][n
];
562 CPPUNIT_ASSERT( dBegin
.SameDay(dtBegin
.GetTm()) );
563 CPPUNIT_ASSERT( dEnd
.SameDay(dtEnd
.GetTm()) );
567 // test wxDateTime -> text conversion
568 void DateTimeTestCase::TestTimeFormat()
570 // some information may be lost during conversion, so store what kind
571 // of info should we recover after a round trip
574 CompareNone
, // don't try comparing
575 CompareBoth
, // dates and times should be identical
576 CompareDate
, // dates only
577 CompareTime
// time only
582 CompareKind compareKind
;
583 const wxChar
*format
;
584 } formatTestFormats
[] =
586 { CompareBoth
, _T("---> %c") },
587 { CompareDate
, _T("Date is %A, %d of %B, in year %Y") },
588 { CompareBoth
, _T("Date is %x, time is %X") },
589 { CompareTime
, _T("Time is %H:%M:%S or %I:%M:%S %p") },
590 { CompareNone
, _T("The day of year: %j, the week of year: %W") },
591 { CompareDate
, _T("ISO date without separators: %Y%m%d") },
594 static const Date formatTestDates
[] =
596 { 29, wxDateTime::May
, 1976, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
597 { 31, wxDateTime::Dec
, 1999, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
599 // this test can't work for other centuries because it uses two digit
600 // years in formats, so don't even try it
601 { 29, wxDateTime::May
, 2076, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
602 { 29, wxDateTime::Feb
, 2400, 02, 15, 25, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
603 { 01, wxDateTime::Jan
, -52, 03, 16, 47, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
607 for ( size_t d
= 0; d
< WXSIZEOF(formatTestDates
) + 1; d
++ )
609 wxDateTime dt
= d
== 0 ? wxDateTime::Now() : formatTestDates
[d
- 1].DT();
610 for ( size_t n
= 0; n
< WXSIZEOF(formatTestFormats
); n
++ )
612 wxString s
= dt
.Format(formatTestFormats
[n
].format
);
614 // what can we recover?
615 int kind
= formatTestFormats
[n
].compareKind
;
619 const wxChar
*result
= dt2
.ParseFormat(s
, formatTestFormats
[n
].format
);
622 // converion failed - should it have?
623 CPPUNIT_ASSERT( kind
== CompareNone
);
625 else // conversion succeeded
627 // should have parsed the entire string
628 CPPUNIT_ASSERT( !*result
);
633 CPPUNIT_ASSERT( dt2
== dt
);
637 CPPUNIT_ASSERT( dt
.IsSameDate(dt2
) );
641 CPPUNIT_ASSERT( dt
.IsSameTime(dt2
) );
649 void DateTimeTestCase::TestTimeTicks()
651 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
653 const Date
& d
= testDates
[n
];
657 wxDateTime dt
= d
.DT();
658 //RN: Translate according to test's time zone
659 //2nd param is to ignore DST - it's already factored
661 dt
.MakeTimezone(wxDateTime::WEST
, true);
662 long ticks
= (dt
.GetValue() / 1000).ToLong();
663 CPPUNIT_ASSERT( ticks
== d
.ticks
);
665 dt
= d
.DT().ToTimezone(wxDateTime::GMT0
);
666 ticks
= (dt
.GetValue() / 1000).ToLong();
667 CPPUNIT_ASSERT( ticks
== d
.gmticks
);
671 // test text -> wxDateTime conversion
672 void DateTimeTestCase::TestTimeParse()
674 static const struct ParseTestData
676 const wxChar
*format
;
681 { _T("Sat, 18 Dec 1999 00:46:40 +0100"), { 18, wxDateTime::Dec
, 1999, 00, 46, 40, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, true },
682 { _T("Wed, 1 Dec 1999 05:17:20 +0300"), { 1, wxDateTime::Dec
, 1999, 03, 17, 20, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, true },
685 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
687 const wxChar
*format
= parseTestDates
[n
].format
;
690 if ( dt
.ParseRfc822Date(format
) )
692 CPPUNIT_ASSERT( parseTestDates
[n
].good
);
694 wxDateTime dtReal
= parseTestDates
[n
].date
.DT();
695 //RN: We need this because the tests are based on
696 //a non-GMT time zone
697 dtReal
.MakeTimezone(wxDateTime::WEST
, true);
698 CPPUNIT_ASSERT( dt
== dtReal
);
700 else // failed to parse
702 CPPUNIT_ASSERT( !parseTestDates
[n
].good
);
707 void DateTimeTestCase::TestTimeArithmetics()
709 static const wxDateSpan testArithmData
[] =
717 // the test will *not* work with arbitrary date!
718 wxDateTime
dt(2, wxDateTime::Dec
, 1999),
722 for ( size_t n
= 0; n
< WXSIZEOF(testArithmData
); n
++ )
724 const wxDateSpan
& span
= testArithmData
[n
];
728 CPPUNIT_ASSERT( dt1
- span
== dt
);
729 CPPUNIT_ASSERT( dt2
+ span
== dt
);
730 CPPUNIT_ASSERT( dt2
+ 2*span
== dt1
);
734 void DateTimeTestCase::TestDSTBug()
736 /////////////////////////
738 wxDateTime dt
= wxDateTime::GetEndDST(2004);
739 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
740 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
741 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
742 CPPUNIT_ASSERT_EQUAL(2, (int)dt
.GetHour());
743 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
744 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
745 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
747 /////////////////////////
750 CPPUNIT_ASSERT_EQUAL(5, (int)dt
.GetHour());
752 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
753 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
754 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
755 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetHour());
756 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
757 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
758 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
761 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
763 /////////////////////////
765 #ifdef CHANGE_SYSTEM_DATE
767 DateChanger
change(2004, 10, 31, 5, 0, 0);
768 dt
= wxDateTime::Today();
771 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
772 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
773 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
774 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetHour());
775 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
776 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
777 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
779 /////////////////////////
780 // Test Set(hour, minute, second, milli)
783 DateChanger
change(2004, 10, 31, 5, 0, 0);
785 dt2
.Set(5, 30, 0, 0);
788 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
789 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
790 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
791 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
792 CPPUNIT_ASSERT_EQUAL(30, (int)dt
.GetMinute());
793 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
794 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
796 CPPUNIT_ASSERT_EQUAL(31, (int)dt2
.GetDay());
797 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt2
.GetMonth());
798 CPPUNIT_ASSERT_EQUAL(2004, (int)dt2
.GetYear());
799 CPPUNIT_ASSERT_EQUAL(5, (int)dt2
.GetHour());
800 CPPUNIT_ASSERT_EQUAL(30, (int)dt2
.GetMinute());
801 CPPUNIT_ASSERT_EQUAL(0, (int)dt2
.GetSecond());
802 CPPUNIT_ASSERT_EQUAL(0, (int)dt2
.GetMillisecond());
803 #endif // CHANGE_SYSTEM_DATE