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"
27 // need this to be able to use CPPUNIT_ASSERT_EQUAL with wxDateTime objects
28 static std::ostream
& operator<<(std::ostream
& ostr
, const wxDateTime
& dt
)
30 ostr
<< dt
.FormatISOCombined(' ');
35 // to test Today() meaningfully we must be able to change the system date which
36 // is not usually the case, but if we're under Win32 we can try it -- define
37 // the macro below to do it
38 //#define CHANGE_SYSTEM_DATE
41 #undef CHANGE_SYSTEM_DATE
44 #ifdef CHANGE_SYSTEM_DATE
49 DateChanger(int year
, int month
, int day
, int hour
, int min
, int sec
)
60 ::GetSystemTime(&m_savedTime
);
61 ::GetTimeZoneInformation(&m_tzi
);
63 m_changed
= ::SetSystemTime(&st
) != 0;
70 ::SetSystemTime(&m_savedTime
);
71 ::SetTimeZoneInformation(&m_tzi
);
76 SYSTEMTIME m_savedTime
;
77 TIME_ZONE_INFORMATION m_tzi
;
81 #endif // CHANGE_SYSTEM_DATE
83 // ----------------------------------------------------------------------------
84 // broken down date representation used for testing
85 // ----------------------------------------------------------------------------
89 wxDateTime::wxDateTime_t day
;
90 wxDateTime::Month month
;
92 wxDateTime::wxDateTime_t hour
, min
, sec
;
94 wxDateTime::WeekDay wday
;
95 time_t gmticks
, ticks
;
97 void Init(const wxDateTime::Tm
& tm
)
106 gmticks
= ticks
= -1;
109 wxDateTime
DT() const
110 { return wxDateTime(day
, month
, year
, hour
, min
, sec
); }
112 bool SameDay(const wxDateTime::Tm
& tm
) const
114 return day
== tm
.mday
&& month
== tm
.mon
&& year
== tm
.year
;
117 wxString
Format() const
120 s
.Printf(_T("%02d:%02d:%02d %10s %02d, %4d%s"),
122 wxDateTime::GetMonthName(month
).c_str(),
124 abs(wxDateTime::ConvertYearToBC(year
)),
125 year
> 0 ? _T("AD") : _T("BC"));
129 wxString
FormatDate() const
132 s
.Printf(_T("%02d-%s-%4d%s"),
134 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
135 abs(wxDateTime::ConvertYearToBC(year
)),
136 year
> 0 ? _T("AD") : _T("BC"));
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
145 static const Date testDates
[] =
147 { 1, wxDateTime::Jan
, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu
, 0, -3600 },
148 { 7, wxDateTime::Feb
, 2036, 00, 00, 00, 2464730.5, wxDateTime::Thu
, -1, -1 },
149 { 8, wxDateTime::Feb
, 2036, 00, 00, 00, 2464731.5, wxDateTime::Fri
, -1, -1 },
150 { 1, wxDateTime::Jan
, 2037, 00, 00, 00, 2465059.5, wxDateTime::Thu
, -1, -1 },
151 { 1, wxDateTime::Jan
, 2038, 00, 00, 00, 2465424.5, wxDateTime::Fri
, -1, -1 },
152 { 21, wxDateTime::Jan
, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon
, -1, -1 },
153 { 29, wxDateTime::May
, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat
, 202219200, 202212000 },
154 { 29, wxDateTime::Feb
, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun
, 194400000, 194396400 },
155 { 1, wxDateTime::Jan
, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon
, -1, -1 },
156 { 1, wxDateTime::Jan
, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon
, -1, -1 },
157 { 15, wxDateTime::Oct
, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri
, -1, -1 },
158 { 4, wxDateTime::Oct
, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon
, -1, -1 },
159 { 1, wxDateTime::Mar
, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu
, -1, -1 },
160 { 1, wxDateTime::Jan
, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon
, -1, -1 },
161 { 31, wxDateTime::Dec
, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun
, -1, -1 },
162 { 1, wxDateTime::Jan
, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat
, -1, -1 },
163 { 12, wxDateTime::Aug
, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri
, -1, -1 },
164 { 12, wxDateTime::Aug
, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat
, -1, -1 },
165 { 24, wxDateTime::Nov
, -4713, 00, 00, 00, -0.5, wxDateTime::Mon
, -1, -1 },
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
173 class DateTimeTestCase
: public CppUnit::TestCase
176 DateTimeTestCase() { }
179 CPPUNIT_TEST_SUITE( DateTimeTestCase
);
180 CPPUNIT_TEST( TestLeapYears
);
181 CPPUNIT_TEST( TestTimeSet
);
182 CPPUNIT_TEST( TestTimeJDN
);
183 CPPUNIT_TEST( TestTimeWNumber
);
184 CPPUNIT_TEST( TestTimeWDays
);
185 CPPUNIT_TEST( TestTimeDST
);
186 CPPUNIT_TEST( TestTimeFormat
);
187 CPPUNIT_TEST( TestTimeSpanFormat
);
188 CPPUNIT_TEST( TestTimeTicks
);
189 CPPUNIT_TEST( TestParceRFC822
);
190 CPPUNIT_TEST( TestDateParse
);
191 CPPUNIT_TEST( TestDateParseISO
);
192 CPPUNIT_TEST( TestDateTimeParse
);
193 CPPUNIT_TEST( TestTimeArithmetics
);
194 CPPUNIT_TEST( TestDSTBug
);
195 CPPUNIT_TEST( TestDateOnly
);
196 CPPUNIT_TEST_SUITE_END();
198 void TestLeapYears();
201 void TestTimeWNumber();
202 void TestTimeWDays();
204 void TestTimeFormat();
205 void TestTimeSpanFormat();
206 void TestTimeTicks();
207 void TestParceRFC822();
208 void TestDateParse();
209 void TestDateParseISO();
210 void TestDateTimeParse();
211 void TestTimeArithmetics();
215 DECLARE_NO_COPY_CLASS(DateTimeTestCase
)
218 // register in the unnamed registry so that these tests are run by default
219 CPPUNIT_TEST_SUITE_REGISTRATION( DateTimeTestCase
);
221 // also include in it's own registry so that these tests can be run alone
222 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DateTimeTestCase
, "DateTimeTestCase" );
224 // ============================================================================
226 // ============================================================================
228 // test leap years detection
229 void DateTimeTestCase::TestLeapYears()
231 static const struct LeapYearTestData
247 for ( size_t n
= 0; n
< WXSIZEOF(years
); n
++ )
249 const LeapYearTestData
& y
= years
[n
];
251 CPPUNIT_ASSERT( wxDateTime::IsLeapYear(y
.year
) == y
.isLeap
);
255 // test constructing wxDateTime objects
256 void DateTimeTestCase::TestTimeSet()
258 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
260 const Date
& d1
= testDates
[n
];
261 wxDateTime dt
= d1
.DT();
266 wxString s1
= d1
.Format(),
269 CPPUNIT_ASSERT( s1
== s2
);
273 // test conversions to JDN &c
274 void DateTimeTestCase::TestTimeJDN()
276 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
278 const Date
& d
= testDates
[n
];
279 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
281 // JDNs must be computed for UTC times
282 double jdn
= dt
.FromUTC().GetJulianDayNumber();
284 CPPUNIT_ASSERT( jdn
== d
.jdn
);
287 CPPUNIT_ASSERT( dt
.GetJulianDayNumber() == jdn
);
291 // test week days computation
292 void DateTimeTestCase::TestTimeWDays()
296 for ( n
= 0; n
< WXSIZEOF(testDates
); n
++ )
298 const Date
& d
= testDates
[n
];
299 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
301 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
302 CPPUNIT_ASSERT( wday
== d
.wday
);
305 // test SetToWeekDay()
306 struct WeekDateTestData
308 Date date
; // the real date (precomputed)
309 int nWeek
; // its week index in the month
310 wxDateTime::WeekDay wday
; // the weekday
311 wxDateTime::Month month
; // the month
312 int year
; // and the year
314 wxString
Format() const
317 switch ( nWeek
< -1 ? -nWeek
: nWeek
)
319 case 1: which
= _T("first"); break;
320 case 2: which
= _T("second"); break;
321 case 3: which
= _T("third"); break;
322 case 4: which
= _T("fourth"); break;
323 case 5: which
= _T("fifth"); break;
325 case -1: which
= _T("last"); break;
330 which
+= _T(" from end");
333 s
.Printf(_T("The %s %s of %s in %d"),
335 wxDateTime::GetWeekDayName(wday
).c_str(),
336 wxDateTime::GetMonthName(month
).c_str(),
343 // the array data was generated by the following python program
345 from DateTime import *
346 from whrandom import *
349 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
350 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
352 week = DateTimeDelta(7)
355 year = randint(1900, 2100)
356 month = randint(1, 12)
358 dt = DateTime(year, month, day)
359 wday = dt.day_of_week
361 countFromEnd = choice([-1, 1])
364 while dt.month is month:
365 dt = dt - countFromEnd * week
366 weekNum = weekNum + countFromEnd
368 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
370 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
371 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
374 static const WeekDateTestData weekDatesTestData
[] =
376 { { 20, wxDateTime::Mar
, 2045, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 3, wxDateTime::Mon
, wxDateTime::Mar
, 2045 },
377 { { 5, wxDateTime::Jun
, 1985, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -4, wxDateTime::Wed
, wxDateTime::Jun
, 1985 },
378 { { 12, wxDateTime::Nov
, 1961, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -3, wxDateTime::Sun
, wxDateTime::Nov
, 1961 },
379 { { 27, wxDateTime::Feb
, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -1, wxDateTime::Fri
, wxDateTime::Feb
, 2093 },
380 { { 4, wxDateTime::Jul
, 2070, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -4, wxDateTime::Fri
, wxDateTime::Jul
, 2070 },
381 { { 2, wxDateTime::Apr
, 1906, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -5, wxDateTime::Mon
, wxDateTime::Apr
, 1906 },
382 { { 19, wxDateTime::Jul
, 2023, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -2, wxDateTime::Wed
, wxDateTime::Jul
, 2023 },
383 { { 5, wxDateTime::May
, 1958, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -4, wxDateTime::Mon
, wxDateTime::May
, 1958 },
384 { { 11, wxDateTime::Aug
, 1900, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 2, wxDateTime::Sat
, wxDateTime::Aug
, 1900 },
385 { { 14, wxDateTime::Feb
, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 2, wxDateTime::Wed
, wxDateTime::Feb
, 1945 },
386 { { 25, wxDateTime::Jul
, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -1, wxDateTime::Tue
, wxDateTime::Jul
, 1967 },
387 { { 9, wxDateTime::May
, 1916, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -4, wxDateTime::Tue
, wxDateTime::May
, 1916 },
388 { { 20, wxDateTime::Jun
, 1927, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 3, wxDateTime::Mon
, wxDateTime::Jun
, 1927 },
389 { { 2, wxDateTime::Aug
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, wxDateTime::Wed
, wxDateTime::Aug
, 2000 },
390 { { 20, wxDateTime::Apr
, 2044, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 3, wxDateTime::Wed
, wxDateTime::Apr
, 2044 },
391 { { 20, wxDateTime::Feb
, 1932, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -2, wxDateTime::Sat
, wxDateTime::Feb
, 1932 },
392 { { 25, wxDateTime::Jul
, 2069, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 4, wxDateTime::Thu
, wxDateTime::Jul
, 2069 },
393 { { 3, wxDateTime::Apr
, 1925, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, wxDateTime::Fri
, wxDateTime::Apr
, 1925 },
394 { { 21, wxDateTime::Mar
, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 3, wxDateTime::Sat
, wxDateTime::Mar
, 2093 },
395 { { 3, wxDateTime::Dec
, 2074, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -5, wxDateTime::Mon
, wxDateTime::Dec
, 2074 }
399 for ( n
= 0; n
< WXSIZEOF(weekDatesTestData
); n
++ )
401 const WeekDateTestData
& wd
= weekDatesTestData
[n
];
403 dt
.SetToWeekDay(wd
.wday
, wd
.nWeek
, wd
.month
, wd
.year
);
405 const Date
& d
= wd
.date
;
406 CPPUNIT_ASSERT( d
.SameDay(dt
.GetTm()) );
410 // test the computation of (ISO) week numbers
411 void DateTimeTestCase::TestTimeWNumber()
413 struct WeekNumberTestData
415 Date date
; // the date
416 wxDateTime::wxDateTime_t week
; // the week number in the year
417 wxDateTime::wxDateTime_t wmon
; // the week number in the month
418 wxDateTime::wxDateTime_t wmon2
; // same but week starts with Sun
419 wxDateTime::wxDateTime_t dnum
; // day number in the year
422 // data generated with the following python script:
424 from DateTime import *
425 from whrandom import *
428 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
429 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
431 def GetMonthWeek(dt):
432 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
434 weekNumMonth = weekNumMonth + 53
437 def GetLastSundayBefore(dt):
438 if dt.iso_week[2] == 7:
441 return dt - DateTimeDelta(dt.iso_week[2])
444 year = randint(1900, 2100)
445 month = randint(1, 12)
447 dt = DateTime(year, month, day)
448 dayNum = dt.day_of_year
449 weekNum = dt.iso_week[1]
450 weekNumMonth = GetMonthWeek(dt)
453 dtSunday = GetLastSundayBefore(dt)
455 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
456 weekNumMonth2 = weekNumMonth2 + 1
457 dtSunday = dtSunday - DateTimeDelta(7)
459 data = { 'day': rjust(`day`, 2), \
460 'month': monthNames[month - 1], \
462 'weekNum': rjust(`weekNum`, 2), \
463 'weekNumMonth': weekNumMonth, \
464 'weekNumMonth2': weekNumMonth2, \
465 'dayNum': rjust(`dayNum`, 3) }
467 print " { { %(day)s, "\
468 "wxDateTime::%(month)s, "\
471 "%(weekNumMonth)s, "\
472 "%(weekNumMonth2)s, "\
473 "%(dayNum)s }," % data
476 static const WeekNumberTestData weekNumberTestDates
[] =
478 { { 27, wxDateTime::Dec
, 1966, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 52, 5, 5, 361 },
479 { { 22, wxDateTime::Jul
, 1926, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 29, 4, 4, 203 },
480 { { 22, wxDateTime::Oct
, 2076, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 43, 4, 4, 296 },
481 { { 1, wxDateTime::Jul
, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 26, 1, 1, 182 },
482 { { 8, wxDateTime::Nov
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 46, 2, 2, 313 },
483 { { 21, wxDateTime::Mar
, 1920, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 12, 3, 4, 81 },
484 { { 7, wxDateTime::Jan
, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, 2, 2, 7 },
485 { { 19, wxDateTime::Oct
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 42, 4, 4, 292 },
486 { { 13, wxDateTime::Aug
, 1955, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 32, 2, 2, 225 },
487 { { 18, wxDateTime::Jul
, 2087, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 29, 3, 3, 199 },
488 { { 2, wxDateTime::Sep
, 2028, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 35, 1, 1, 246 },
489 { { 28, wxDateTime::Jul
, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 30, 5, 4, 209 },
490 { { 15, wxDateTime::Jun
, 1901, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 24, 3, 3, 166 },
491 { { 10, wxDateTime::Oct
, 1939, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 41, 3, 2, 283 },
492 { { 3, wxDateTime::Dec
, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 48, 1, 1, 337 },
493 { { 23, wxDateTime::Feb
, 1940, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 8, 4, 4, 54 },
494 { { 2, wxDateTime::Jan
, 1987, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, 1, 1, 2 },
495 { { 11, wxDateTime::Aug
, 2079, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 32, 2, 2, 223 },
496 { { 2, wxDateTime::Feb
, 2063, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 5, 1, 1, 33 },
497 { { 16, wxDateTime::Oct
, 1942, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 42, 3, 3, 289 },
498 { { 30, wxDateTime::Dec
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, 5, 5, 364 },
499 { { 2, wxDateTime::Jan
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, 1, 1, 2 },
502 for ( size_t n
= 0; n
< WXSIZEOF(weekNumberTestDates
); n
++ )
504 const WeekNumberTestData
& wn
= weekNumberTestDates
[n
];
505 const Date
& d
= wn
.date
;
507 wxDateTime dt
= d
.DT();
509 wxDateTime::wxDateTime_t
510 week
= dt
.GetWeekOfYear(wxDateTime::Monday_First
),
511 wmon
= dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
512 wmon2
= dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
513 dnum
= dt
.GetDayOfYear();
515 CPPUNIT_ASSERT( dnum
== wn
.dnum
);
516 CPPUNIT_ASSERT( wmon
== wn
.wmon
);
517 CPPUNIT_ASSERT( wmon2
== wn
.wmon2
);
518 CPPUNIT_ASSERT( week
== wn
.week
);
521 if ( week
== 1 && d
.month
!= wxDateTime::Jan
)
523 // this means we're in the first week of the next year
528 dt2
= wxDateTime::SetToWeekOfYear(year
, week
, dt
.GetWeekDay());
529 CPPUNIT_ASSERT( dt2
== dt
);
533 // test DST applicability
534 void DateTimeTestCase::TestTimeDST()
536 // taken from http://www.energy.ca.gov/daylightsaving.html
537 static const Date datesDST
[2][2004 - 1900 + 1] =
540 { 1, wxDateTime::Apr
, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
541 { 7, wxDateTime::Apr
, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
542 { 5, wxDateTime::Apr
, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
543 { 4, wxDateTime::Apr
, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
544 { 3, wxDateTime::Apr
, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
545 { 2, wxDateTime::Apr
, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
546 { 7, wxDateTime::Apr
, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
547 { 6, wxDateTime::Apr
, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
548 { 5, wxDateTime::Apr
, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
549 { 4, wxDateTime::Apr
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
550 { 2, wxDateTime::Apr
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
551 { 1, wxDateTime::Apr
, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
552 { 7, wxDateTime::Apr
, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
553 { 6, wxDateTime::Apr
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
554 { 4, wxDateTime::Apr
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
557 { 28, wxDateTime::Oct
, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
558 { 27, wxDateTime::Oct
, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
559 { 25, wxDateTime::Oct
, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
560 { 31, wxDateTime::Oct
, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
561 { 30, wxDateTime::Oct
, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
562 { 29, wxDateTime::Oct
, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
563 { 27, wxDateTime::Oct
, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
564 { 26, wxDateTime::Oct
, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
565 { 25, wxDateTime::Oct
, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
566 { 31, wxDateTime::Oct
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
567 { 29, wxDateTime::Oct
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
568 { 28, wxDateTime::Oct
, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
569 { 27, wxDateTime::Oct
, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
570 { 26, wxDateTime::Oct
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
571 { 31, wxDateTime::Oct
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
575 for ( int year
= 1990; year
< 2005; year
++ )
577 wxDateTime dtBegin
= wxDateTime::GetBeginDST(year
, wxDateTime::USA
),
578 dtEnd
= wxDateTime::GetEndDST(year
, wxDateTime::USA
);
580 size_t n
= year
- 1990;
581 const Date
& dBegin
= datesDST
[0][n
];
582 const Date
& dEnd
= datesDST
[1][n
];
584 CPPUNIT_ASSERT( dBegin
.SameDay(dtBegin
.GetTm()) );
585 CPPUNIT_ASSERT( dEnd
.SameDay(dtEnd
.GetTm()) );
589 // test wxDateTime -> text conversion
590 void DateTimeTestCase::TestTimeFormat()
592 // some information may be lost during conversion, so store what kind
593 // of info should we recover after a round trip
596 CompareNone
, // don't try comparing
597 CompareBoth
, // dates and times should be identical
598 CompareYear
, // don't compare centuries (fails for 2 digit years)
599 CompareDate
, // dates only
600 CompareTime
// time only
605 CompareKind compareKind
;
607 } formatTestFormats
[] =
609 { CompareYear
, "---> %c" }, // %c could use 2 digit years
610 { CompareDate
, "Date is %A, %d of %B, in year %Y" },
611 { CompareYear
, "Date is %x, time is %X" }, // %x could use 2 digits
612 { CompareTime
, "Time is %H:%M:%S or %I:%M:%S %p" },
613 { CompareNone
, "The day of year: %j, the week of year: %W" },
614 { CompareDate
, "ISO date without separators: %Y%m%d" },
617 static const Date formatTestDates
[] =
619 { 29, wxDateTime::May
, 1976, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
620 { 31, wxDateTime::Dec
, 1999, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
621 { 6, wxDateTime::Feb
, 1937, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
622 { 6, wxDateTime::Feb
, 1856, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
623 { 6, wxDateTime::Feb
, 1857, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
624 { 29, wxDateTime::May
, 2076, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
625 { 29, wxDateTime::Feb
, 2400, 02, 15, 25, 0.0, wxDateTime::Inv_WeekDay
},
627 // Need to add support for BCE dates.
628 { 01, wxDateTime::Jan
, -52, 03, 16, 47, 0.0, wxDateTime::Inv_WeekDay
},
632 for ( size_t d
= 0; d
< WXSIZEOF(formatTestDates
); d
++ )
634 wxDateTime dt
= formatTestDates
[d
].DT();
635 for ( size_t n
= 0; n
< WXSIZEOF(formatTestFormats
); n
++ )
637 const char *fmt
= formatTestFormats
[n
].format
;
638 wxString s
= dt
.Format(fmt
);
640 // what can we recover?
641 CompareKind kind
= formatTestFormats
[n
].compareKind
;
645 const char *result
= dt2
.ParseFormat(s
, fmt
);
648 // converion failed - should it have?
649 CPPUNIT_ASSERT( kind
== CompareNone
);
651 else // conversion succeeded
653 // should have parsed the entire string
654 CPPUNIT_ASSERT( !*result
);
659 if ( dt2
.GetCentury() != dt
.GetCentury() )
661 CPPUNIT_ASSERT_EQUAL(dt
.GetYear() % 100,
662 dt2
.GetYear() % 100);
664 dt2
.SetYear(dt
.GetYear());
666 // fall through and compare everything
669 CPPUNIT_ASSERT_EQUAL( dt
, dt2
);
673 CPPUNIT_ASSERT( dt
.IsSameDate(dt2
) );
677 CPPUNIT_ASSERT( dt
.IsSameTime(dt2
) );
681 wxFAIL_MSG( _T("unexpected") );
689 void DateTimeTestCase::TestTimeSpanFormat()
691 static const struct TimeSpanFormatTestData
693 long h
, min
, sec
, msec
;
698 { 12, 34, 56, 789, "%H:%M:%S.%l", "12:34:56.789" },
699 { 1, 2, 3, 0, "%H:%M:%S", "01:02:03" },
700 { 1, 2, 3, 0, "%S", "3723" },
701 { -1, -2, -3, 0, "%S", "-3723" },
702 { -1, -2, -3, 0, "%H:%M:%S", "-01:02:03" },
703 { 26, 0, 0, 0, "%H", "26" },
704 { 26, 0, 0, 0, "%D, %H", "1, 02" },
705 { -26, 0, 0, 0, "%H", "-26" },
706 { -26, 0, 0, 0, "%D, %H", "-1, 02" },
707 { 219, 0, 0, 0, "%H", "219" },
708 { 219, 0, 0, 0, "%D, %H", "9, 03" },
709 { 219, 0, 0, 0, "%E, %D, %H", "1, 2, 03" },
712 for ( size_t n
= 0; n
< WXSIZEOF(testSpans
); n
++ )
714 const TimeSpanFormatTestData
& td
= testSpans
[n
];
715 wxTimeSpan
ts(td
.h
, td
.min
, td
.sec
, td
.msec
);
716 CPPUNIT_ASSERT_EQUAL( wxString(td
.result
), ts
.Format(td
.fmt
) );
720 void DateTimeTestCase::TestTimeTicks()
722 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
724 const Date
& d
= testDates
[n
];
728 wxDateTime dt
= d
.DT();
729 //RN: Translate according to test's time zone
730 //2nd param is to ignore DST - it's already factored
732 dt
.MakeTimezone(wxDateTime::WEST
, true);
733 long ticks
= (dt
.GetValue() / 1000).ToLong();
734 CPPUNIT_ASSERT( ticks
== d
.ticks
);
736 dt
= d
.DT().FromTimezone(wxDateTime::GMT0
);
737 ticks
= (dt
.GetValue() / 1000).ToLong();
738 CPPUNIT_ASSERT( ticks
== d
.gmticks
);
742 // test parsing dates in RFC822 format
743 void DateTimeTestCase::TestParceRFC822()
745 static const struct ParseTestData
748 Date date
; // NB: this should be in UTC
753 "Sat, 18 Dec 1999 00:46:40 +0100",
754 { 17, wxDateTime::Dec
, 1999, 23, 46, 40 },
758 "Wed, 1 Dec 1999 05:17:20 +0300",
759 { 1, wxDateTime::Dec
, 1999, 2, 17, 20 },
763 "Sun, 28 Aug 2005 03:31:30 +0200",
764 { 28, wxDateTime::Aug
, 2005, 1, 31, 30 },
769 "Sat, 18 Dec 1999 10:48:30 -0500",
770 { 18, wxDateTime::Dec
, 1999, 15, 48, 30 },
775 for ( unsigned n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
777 const char * const datestr
= parseTestDates
[n
].rfc822
;
780 if ( dt
.ParseRfc822Date(datestr
) )
783 ("Erroneously parsed \"%s\"", datestr
),
784 parseTestDates
[n
].good
787 wxDateTime dtReal
= parseTestDates
[n
].date
.DT().FromUTC();
788 CPPUNIT_ASSERT_EQUAL( dtReal
, dt
);
790 else // failed to parse
793 ("Failed to parse \"%s\"", datestr
),
794 !parseTestDates
[n
].good
800 // test parsing dates in free format
801 void DateTimeTestCase::TestDateParse()
803 static const struct ParseTestData
806 Date date
; // NB: this should be in UTC
810 { "21 Mar 2006", { 21, wxDateTime::Mar
, 2006 }, true },
811 { "29 Feb 1976", { 29, wxDateTime::Feb
, 1976 }, true },
812 { "Feb 29 1976", { 29, wxDateTime::Feb
, 1976 }, true },
813 { "31/03/06", { 31, wxDateTime::Mar
, 6 }, true },
814 { "31/03/2006", { 31, wxDateTime::Mar
, 2006 }, true },
816 // some invalid ones too
824 CPPUNIT_ASSERT( dt
.ParseDate(_T("today")) );
825 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), dt
);
827 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
830 if ( dt
.ParseDate(parseTestDates
[n
].str
) )
832 CPPUNIT_ASSERT( parseTestDates
[n
].good
);
834 CPPUNIT_ASSERT_EQUAL( parseTestDates
[n
].date
.DT(), dt
);
836 else // failed to parse
838 CPPUNIT_ASSERT( !parseTestDates
[n
].good
);
843 void DateTimeTestCase::TestDateParseISO()
848 Date date
; // NB: this should be in UTC
852 { "2006-03-21", { 21, wxDateTime::Mar
, 2006 }, true },
853 { "1976-02-29", { 29, wxDateTime::Feb
, 1976 }, true },
854 { "0006-03-31", { 31, wxDateTime::Mar
, 6 }, true },
856 // some invalid ones too
866 wxDateTime::wxDateTime_t hour
, min
, sec
;
870 { "13:42:17", 13, 42, 17, true },
871 { "02:17:01", 2, 17, 1, true },
873 // some invalid ones too
880 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
883 if ( dt
.ParseISODate(parseTestDates
[n
].str
) )
885 CPPUNIT_ASSERT( parseTestDates
[n
].good
);
887 CPPUNIT_ASSERT_EQUAL( parseTestDates
[n
].date
.DT(), dt
);
889 for ( size_t m
= 0; m
< WXSIZEOF(parseTestTimes
); m
++ )
892 dtCombined
<< parseTestDates
[n
].str
894 << parseTestTimes
[m
].str
;
896 if ( dt
.ParseISOCombined(dtCombined
) )
898 CPPUNIT_ASSERT( parseTestTimes
[m
].good
);
900 CPPUNIT_ASSERT_EQUAL( parseTestTimes
[m
].hour
, dt
.GetHour()) ;
901 CPPUNIT_ASSERT_EQUAL( parseTestTimes
[m
].min
, dt
.GetMinute()) ;
902 CPPUNIT_ASSERT_EQUAL( parseTestTimes
[m
].sec
, dt
.GetSecond()) ;
904 else // failed to parse combined date/time
906 CPPUNIT_ASSERT( !parseTestTimes
[m
].good
);
910 else // failed to parse
912 CPPUNIT_ASSERT( !parseTestDates
[n
].good
);
917 void DateTimeTestCase::TestDateTimeParse()
919 static const struct ParseTestData
922 Date date
; // NB: this should be in UTC
926 { "Thu 22 Nov 2007 07:40:00 PM",
927 { 22, wxDateTime::Nov
, 2007, 19, 40, 0}, true },
932 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
935 if ( dt
.ParseDateTime(parseTestDates
[n
].str
) )
937 CPPUNIT_ASSERT( parseTestDates
[n
].good
);
939 CPPUNIT_ASSERT_EQUAL( parseTestDates
[n
].date
.DT(), dt
);
941 else // failed to parse
943 CPPUNIT_ASSERT( !parseTestDates
[n
].good
);
948 void DateTimeTestCase::TestTimeArithmetics()
950 static const wxDateSpan testArithmData
[] =
958 // the test will *not* work with arbitrary date!
959 wxDateTime
dt(2, wxDateTime::Dec
, 1999),
963 for ( size_t n
= 0; n
< WXSIZEOF(testArithmData
); n
++ )
965 const wxDateSpan
& span
= testArithmData
[n
];
969 CPPUNIT_ASSERT( dt1
- span
== dt
);
970 CPPUNIT_ASSERT( dt2
+ span
== dt
);
971 CPPUNIT_ASSERT( dt2
+ 2*span
== dt1
);
975 void DateTimeTestCase::TestDSTBug()
977 /////////////////////////
979 wxDateTime dt
= wxDateTime::GetEndDST(2004, wxDateTime::France
);
980 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
981 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
982 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
983 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
984 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
985 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
986 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
988 /////////////////////////
991 CPPUNIT_ASSERT_EQUAL(5, (int)dt
.GetHour());
993 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
994 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
995 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
996 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetHour());
997 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
998 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
999 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
1002 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
1004 /////////////////////////
1006 #ifdef CHANGE_SYSTEM_DATE
1008 DateChanger
change(2004, 10, 31, 5, 0, 0);
1009 dt
= wxDateTime::Today();
1012 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
1013 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
1014 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
1015 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetHour());
1016 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
1017 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
1018 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
1020 /////////////////////////
1021 // Test Set(hour, minute, second, milli)
1024 DateChanger
change(2004, 10, 31, 5, 0, 0);
1025 dt
.Set(1, 30, 0, 0);
1026 dt2
.Set(5, 30, 0, 0);
1029 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
1030 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
1031 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
1032 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
1033 CPPUNIT_ASSERT_EQUAL(30, (int)dt
.GetMinute());
1034 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
1035 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
1037 CPPUNIT_ASSERT_EQUAL(31, (int)dt2
.GetDay());
1038 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt2
.GetMonth());
1039 CPPUNIT_ASSERT_EQUAL(2004, (int)dt2
.GetYear());
1040 CPPUNIT_ASSERT_EQUAL(5, (int)dt2
.GetHour());
1041 CPPUNIT_ASSERT_EQUAL(30, (int)dt2
.GetMinute());
1042 CPPUNIT_ASSERT_EQUAL(0, (int)dt2
.GetSecond());
1043 CPPUNIT_ASSERT_EQUAL(0, (int)dt2
.GetMillisecond());
1044 #endif // CHANGE_SYSTEM_DATE
1047 void DateTimeTestCase::TestDateOnly()
1049 wxDateTime
dt(19, wxDateTime::Jan
, 2007, 15, 01, 00);
1051 static const wxDateTime::wxDateTime_t DATE_ZERO
= 0;
1052 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetHour() );
1053 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetMinute() );
1054 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetSecond() );
1055 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetMillisecond() );
1058 CPPUNIT_ASSERT_EQUAL( wxDateTime(19, wxDateTime::Jan
, 2007), dt
);
1060 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), wxDateTime::Now().GetDateOnly() );
1063 #endif // wxUSE_DATETIME