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
)
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
;
97 long flags
; //Test specific flags - currently only used by TestTimeFormat.
99 void Init(const wxDateTime::Tm
& tm
)
108 gmticks
= ticks
= -1;
111 wxDateTime
DT() const
112 { return wxDateTime(day
, month
, year
, hour
, min
, sec
); }
114 bool SameDay(const wxDateTime::Tm
& tm
) const
116 return day
== tm
.mday
&& month
== tm
.mon
&& year
== tm
.year
;
119 wxString
Format() const
122 s
.Printf(_T("%02d:%02d:%02d %10s %02d, %4d%s"),
124 wxDateTime::GetMonthName(month
).c_str(),
126 abs(wxDateTime::ConvertYearToBC(year
)),
127 year
> 0 ? _T("AD") : _T("BC"));
131 wxString
FormatDate() const
134 s
.Printf(_T("%02d-%s-%4d%s"),
136 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
137 abs(wxDateTime::ConvertYearToBC(year
)),
138 year
> 0 ? _T("AD") : _T("BC"));
141 bool IsSet(long f
) const { return (f
&& flags
) ==f
; }
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
148 static const Date testDates
[] =
150 { 1, wxDateTime::Jan
, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu
, 0, -3600 },
151 { 7, wxDateTime::Feb
, 2036, 00, 00, 00, 2464730.5, wxDateTime::Thu
, -1, -1 },
152 { 8, wxDateTime::Feb
, 2036, 00, 00, 00, 2464731.5, wxDateTime::Fri
, -1, -1 },
153 { 1, wxDateTime::Jan
, 2037, 00, 00, 00, 2465059.5, wxDateTime::Thu
, -1, -1 },
154 { 1, wxDateTime::Jan
, 2038, 00, 00, 00, 2465424.5, wxDateTime::Fri
, -1, -1 },
155 { 21, wxDateTime::Jan
, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon
, -1, -1 },
156 { 29, wxDateTime::May
, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat
, 202219200, 202212000 },
157 { 29, wxDateTime::Feb
, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun
, 194400000, 194396400 },
158 { 1, wxDateTime::Jan
, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon
, -1, -1 },
159 { 1, wxDateTime::Jan
, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon
, -1, -1 },
160 { 15, wxDateTime::Oct
, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri
, -1, -1 },
161 { 4, wxDateTime::Oct
, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon
, -1, -1 },
162 { 1, wxDateTime::Mar
, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu
, -1, -1 },
163 { 1, wxDateTime::Jan
, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon
, -1, -1 },
164 { 31, wxDateTime::Dec
, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun
, -1, -1 },
165 { 1, wxDateTime::Jan
, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat
, -1, -1 },
166 { 12, wxDateTime::Aug
, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri
, -1, -1 },
167 { 12, wxDateTime::Aug
, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat
, -1, -1 },
168 { 24, wxDateTime::Nov
, -4713, 00, 00, 00, -0.5, wxDateTime::Mon
, -1, -1 },
172 // ----------------------------------------------------------------------------
174 // ----------------------------------------------------------------------------
176 class DateTimeTestCase
: public CppUnit::TestCase
179 DateTimeTestCase() { }
182 CPPUNIT_TEST_SUITE( DateTimeTestCase
);
183 CPPUNIT_TEST( TestLeapYears
);
184 CPPUNIT_TEST( TestTimeSet
);
185 CPPUNIT_TEST( TestTimeJDN
);
186 CPPUNIT_TEST( TestTimeWNumber
);
187 CPPUNIT_TEST( TestTimeWDays
);
188 CPPUNIT_TEST( TestTimeDST
);
189 CPPUNIT_TEST( TestTimeFormat
);
190 CPPUNIT_TEST( TestTimeTicks
);
191 CPPUNIT_TEST( TestParceRFC822
);
192 CPPUNIT_TEST( TestDateParse
);
193 CPPUNIT_TEST( TestTimeArithmetics
);
194 CPPUNIT_TEST( TestDSTBug
);
195 CPPUNIT_TEST_SUITE_END();
197 void TestLeapYears();
200 void TestTimeWNumber();
201 void TestTimeWDays();
203 void TestTimeFormat();
204 void TestTimeTicks();
205 void TestParceRFC822();
206 void TestDateParse();
207 void TestTimeArithmetics();
210 DECLARE_NO_COPY_CLASS(DateTimeTestCase
)
213 // register in the unnamed registry so that these tests are run by default
214 CPPUNIT_TEST_SUITE_REGISTRATION( DateTimeTestCase
);
216 // also include in it's own registry so that these tests can be run alone
217 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DateTimeTestCase
, "DateTimeTestCase" );
219 // ============================================================================
221 // ============================================================================
223 // test leap years detection
224 void DateTimeTestCase::TestLeapYears()
226 static const struct LeapYearTestData
242 for ( size_t n
= 0; n
< WXSIZEOF(years
); n
++ )
244 const LeapYearTestData
& y
= years
[n
];
246 CPPUNIT_ASSERT( wxDateTime::IsLeapYear(y
.year
) == y
.isLeap
);
250 // test constructing wxDateTime objects
251 void DateTimeTestCase::TestTimeSet()
253 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
255 const Date
& d1
= testDates
[n
];
256 wxDateTime dt
= d1
.DT();
261 wxString s1
= d1
.Format(),
264 CPPUNIT_ASSERT( s1
== s2
);
268 // test conversions to JDN &c
269 void DateTimeTestCase::TestTimeJDN()
271 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
273 const Date
& d
= testDates
[n
];
274 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
276 // JDNs must be computed for UTC times
277 double jdn
= dt
.FromUTC().GetJulianDayNumber();
279 CPPUNIT_ASSERT( jdn
== d
.jdn
);
282 CPPUNIT_ASSERT( dt
.GetJulianDayNumber() == jdn
);
286 // test week days computation
287 void DateTimeTestCase::TestTimeWDays()
291 for ( n
= 0; n
< WXSIZEOF(testDates
); n
++ )
293 const Date
& d
= testDates
[n
];
294 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
296 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
297 CPPUNIT_ASSERT( wday
== d
.wday
);
300 // test SetToWeekDay()
301 struct WeekDateTestData
303 Date date
; // the real date (precomputed)
304 int nWeek
; // its week index in the month
305 wxDateTime::WeekDay wday
; // the weekday
306 wxDateTime::Month month
; // the month
307 int year
; // and the year
309 wxString
Format() const
312 switch ( nWeek
< -1 ? -nWeek
: nWeek
)
314 case 1: which
= _T("first"); break;
315 case 2: which
= _T("second"); break;
316 case 3: which
= _T("third"); break;
317 case 4: which
= _T("fourth"); break;
318 case 5: which
= _T("fifth"); break;
320 case -1: which
= _T("last"); break;
325 which
+= _T(" from end");
328 s
.Printf(_T("The %s %s of %s in %d"),
330 wxDateTime::GetWeekDayName(wday
).c_str(),
331 wxDateTime::GetMonthName(month
).c_str(),
338 // the array data was generated by the following python program
340 from DateTime import *
341 from whrandom import *
344 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
345 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
347 week = DateTimeDelta(7)
350 year = randint(1900, 2100)
351 month = randint(1, 12)
353 dt = DateTime(year, month, day)
354 wday = dt.day_of_week
356 countFromEnd = choice([-1, 1])
359 while dt.month is month:
360 dt = dt - countFromEnd * week
361 weekNum = weekNum + countFromEnd
363 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
365 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
366 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
369 static const WeekDateTestData weekDatesTestData
[] =
371 { { 20, wxDateTime::Mar
, 2045, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 3, wxDateTime::Mon
, wxDateTime::Mar
, 2045 },
372 { { 5, wxDateTime::Jun
, 1985, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -4, wxDateTime::Wed
, wxDateTime::Jun
, 1985 },
373 { { 12, wxDateTime::Nov
, 1961, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -3, wxDateTime::Sun
, wxDateTime::Nov
, 1961 },
374 { { 27, wxDateTime::Feb
, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -1, wxDateTime::Fri
, wxDateTime::Feb
, 2093 },
375 { { 4, wxDateTime::Jul
, 2070, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -4, wxDateTime::Fri
, wxDateTime::Jul
, 2070 },
376 { { 2, wxDateTime::Apr
, 1906, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -5, wxDateTime::Mon
, wxDateTime::Apr
, 1906 },
377 { { 19, wxDateTime::Jul
, 2023, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -2, wxDateTime::Wed
, wxDateTime::Jul
, 2023 },
378 { { 5, wxDateTime::May
, 1958, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -4, wxDateTime::Mon
, wxDateTime::May
, 1958 },
379 { { 11, wxDateTime::Aug
, 1900, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 2, wxDateTime::Sat
, wxDateTime::Aug
, 1900 },
380 { { 14, wxDateTime::Feb
, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 2, wxDateTime::Wed
, wxDateTime::Feb
, 1945 },
381 { { 25, wxDateTime::Jul
, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -1, wxDateTime::Tue
, wxDateTime::Jul
, 1967 },
382 { { 9, wxDateTime::May
, 1916, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -4, wxDateTime::Tue
, wxDateTime::May
, 1916 },
383 { { 20, wxDateTime::Jun
, 1927, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 3, wxDateTime::Mon
, wxDateTime::Jun
, 1927 },
384 { { 2, wxDateTime::Aug
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, wxDateTime::Wed
, wxDateTime::Aug
, 2000 },
385 { { 20, wxDateTime::Apr
, 2044, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 3, wxDateTime::Wed
, wxDateTime::Apr
, 2044 },
386 { { 20, wxDateTime::Feb
, 1932, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -2, wxDateTime::Sat
, wxDateTime::Feb
, 1932 },
387 { { 25, wxDateTime::Jul
, 2069, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 4, wxDateTime::Thu
, wxDateTime::Jul
, 2069 },
388 { { 3, wxDateTime::Apr
, 1925, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, wxDateTime::Fri
, wxDateTime::Apr
, 1925 },
389 { { 21, wxDateTime::Mar
, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 3, wxDateTime::Sat
, wxDateTime::Mar
, 2093 },
390 { { 3, wxDateTime::Dec
, 2074, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -5, wxDateTime::Mon
, wxDateTime::Dec
, 2074 }
394 for ( n
= 0; n
< WXSIZEOF(weekDatesTestData
); n
++ )
396 const WeekDateTestData
& wd
= weekDatesTestData
[n
];
398 dt
.SetToWeekDay(wd
.wday
, wd
.nWeek
, wd
.month
, wd
.year
);
400 const Date
& d
= wd
.date
;
401 CPPUNIT_ASSERT( d
.SameDay(dt
.GetTm()) );
405 // test the computation of (ISO) week numbers
406 void DateTimeTestCase::TestTimeWNumber()
408 struct WeekNumberTestData
410 Date date
; // the date
411 wxDateTime::wxDateTime_t week
; // the week number in the year
412 wxDateTime::wxDateTime_t wmon
; // the week number in the month
413 wxDateTime::wxDateTime_t wmon2
; // same but week starts with Sun
414 wxDateTime::wxDateTime_t dnum
; // day number in the year
417 // data generated with the following python script:
419 from DateTime import *
420 from whrandom import *
423 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
424 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
426 def GetMonthWeek(dt):
427 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
429 weekNumMonth = weekNumMonth + 53
432 def GetLastSundayBefore(dt):
433 if dt.iso_week[2] == 7:
436 return dt - DateTimeDelta(dt.iso_week[2])
439 year = randint(1900, 2100)
440 month = randint(1, 12)
442 dt = DateTime(year, month, day)
443 dayNum = dt.day_of_year
444 weekNum = dt.iso_week[1]
445 weekNumMonth = GetMonthWeek(dt)
448 dtSunday = GetLastSundayBefore(dt)
450 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
451 weekNumMonth2 = weekNumMonth2 + 1
452 dtSunday = dtSunday - DateTimeDelta(7)
454 data = { 'day': rjust(`day`, 2), \
455 'month': monthNames[month - 1], \
457 'weekNum': rjust(`weekNum`, 2), \
458 'weekNumMonth': weekNumMonth, \
459 'weekNumMonth2': weekNumMonth2, \
460 'dayNum': rjust(`dayNum`, 3) }
462 print " { { %(day)s, "\
463 "wxDateTime::%(month)s, "\
466 "%(weekNumMonth)s, "\
467 "%(weekNumMonth2)s, "\
468 "%(dayNum)s }," % data
471 static const WeekNumberTestData weekNumberTestDates
[] =
473 { { 27, wxDateTime::Dec
, 1966, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 52, 5, 5, 361 },
474 { { 22, wxDateTime::Jul
, 1926, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 29, 4, 4, 203 },
475 { { 22, wxDateTime::Oct
, 2076, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 43, 4, 4, 296 },
476 { { 1, wxDateTime::Jul
, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 26, 1, 1, 182 },
477 { { 8, wxDateTime::Nov
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 46, 2, 2, 313 },
478 { { 21, wxDateTime::Mar
, 1920, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 12, 3, 4, 81 },
479 { { 7, wxDateTime::Jan
, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, 2, 2, 7 },
480 { { 19, wxDateTime::Oct
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 42, 4, 4, 292 },
481 { { 13, wxDateTime::Aug
, 1955, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 32, 2, 2, 225 },
482 { { 18, wxDateTime::Jul
, 2087, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 29, 3, 3, 199 },
483 { { 2, wxDateTime::Sep
, 2028, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 35, 1, 1, 246 },
484 { { 28, wxDateTime::Jul
, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 30, 5, 4, 209 },
485 { { 15, wxDateTime::Jun
, 1901, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 24, 3, 3, 166 },
486 { { 10, wxDateTime::Oct
, 1939, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 41, 3, 2, 283 },
487 { { 3, wxDateTime::Dec
, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 48, 1, 1, 337 },
488 { { 23, wxDateTime::Feb
, 1940, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 8, 4, 4, 54 },
489 { { 2, wxDateTime::Jan
, 1987, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, 1, 1, 2 },
490 { { 11, wxDateTime::Aug
, 2079, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 32, 2, 2, 223 },
491 { { 2, wxDateTime::Feb
, 2063, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 5, 1, 1, 33 },
492 { { 16, wxDateTime::Oct
, 1942, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 42, 3, 3, 289 },
493 { { 30, wxDateTime::Dec
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, 5, 5, 364 },
494 { { 2, wxDateTime::Jan
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, 1, 1, 2 },
497 for ( size_t n
= 0; n
< WXSIZEOF(weekNumberTestDates
); n
++ )
499 const WeekNumberTestData
& wn
= weekNumberTestDates
[n
];
500 const Date
& d
= wn
.date
;
502 wxDateTime dt
= d
.DT();
504 wxDateTime::wxDateTime_t
505 week
= dt
.GetWeekOfYear(wxDateTime::Monday_First
),
506 wmon
= dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
507 wmon2
= dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
508 dnum
= dt
.GetDayOfYear();
510 CPPUNIT_ASSERT( dnum
== wn
.dnum
);
511 CPPUNIT_ASSERT( wmon
== wn
.wmon
);
512 CPPUNIT_ASSERT( wmon2
== wn
.wmon2
);
513 CPPUNIT_ASSERT( week
== wn
.week
);
516 if ( week
== 1 && d
.month
!= wxDateTime::Jan
)
518 // this means we're in the first week of the next year
523 dt2
= wxDateTime::SetToWeekOfYear(year
, week
, dt
.GetWeekDay());
524 CPPUNIT_ASSERT( dt2
== dt
);
528 // test DST applicability
529 void DateTimeTestCase::TestTimeDST()
531 // taken from http://www.energy.ca.gov/daylightsaving.html
532 static const Date datesDST
[2][2004 - 1900 + 1] =
535 { 1, wxDateTime::Apr
, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
536 { 7, wxDateTime::Apr
, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
537 { 5, wxDateTime::Apr
, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
538 { 4, wxDateTime::Apr
, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
539 { 3, wxDateTime::Apr
, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
540 { 2, wxDateTime::Apr
, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
541 { 7, wxDateTime::Apr
, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
542 { 6, wxDateTime::Apr
, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
543 { 5, wxDateTime::Apr
, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
544 { 4, wxDateTime::Apr
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
545 { 2, wxDateTime::Apr
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
546 { 1, wxDateTime::Apr
, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
547 { 7, wxDateTime::Apr
, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
548 { 6, wxDateTime::Apr
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
549 { 4, wxDateTime::Apr
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
552 { 28, wxDateTime::Oct
, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
553 { 27, wxDateTime::Oct
, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
554 { 25, wxDateTime::Oct
, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
555 { 31, wxDateTime::Oct
, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
556 { 30, wxDateTime::Oct
, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
557 { 29, wxDateTime::Oct
, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
558 { 27, wxDateTime::Oct
, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
559 { 26, wxDateTime::Oct
, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
560 { 25, wxDateTime::Oct
, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
561 { 31, wxDateTime::Oct
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
562 { 29, wxDateTime::Oct
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
563 { 28, wxDateTime::Oct
, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
564 { 27, wxDateTime::Oct
, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
565 { 26, wxDateTime::Oct
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
566 { 31, wxDateTime::Oct
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
570 for ( int year
= 1990; year
< 2005; year
++ )
572 wxDateTime dtBegin
= wxDateTime::GetBeginDST(year
, wxDateTime::USA
),
573 dtEnd
= wxDateTime::GetEndDST(year
, wxDateTime::USA
);
575 size_t n
= year
- 1990;
576 const Date
& dBegin
= datesDST
[0][n
];
577 const Date
& dEnd
= datesDST
[1][n
];
579 CPPUNIT_ASSERT( dBegin
.SameDay(dtBegin
.GetTm()) );
580 CPPUNIT_ASSERT( dEnd
.SameDay(dtEnd
.GetTm()) );
584 // test wxDateTime -> text conversion
585 void DateTimeTestCase::TestTimeFormat()
587 // some information may be lost during conversion, so store what kind
588 // of info should we recover after a round trip
591 CompareNone
, // don't try comparing
592 CompareBoth
, // dates and times should be identical
593 CompareDate
, // dates only
594 CompareTime
// time only
597 const int WORKS_WITH_2DIGIT_YEAR(1);
600 CompareKind compareKind
;
602 const wxChar
*format
;
603 } formatTestFormats
[] =
605 { CompareBoth
,0 , _T("---> %c") }, //Assumes %c show a 4digit year.
606 { CompareDate
,0, _T("Date is %A, %d of %B, in year %Y") },
607 { CompareBoth
,WORKS_WITH_2DIGIT_YEAR
, _T("Date is %x, time is %X") },
608 { CompareTime
,0, _T("Time is %H:%M:%S or %I:%M:%S %p") },
609 { CompareNone
,0, _T("The day of year: %j, the week of year: %W") },
610 { CompareDate
,0, _T("ISO date without separators: %Y%m%d") },
613 static const Date formatTestDates
[] =
615 { 29, wxDateTime::May
, 1976, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 , WORKS_WITH_2DIGIT_YEAR
},
616 { 31, wxDateTime::Dec
, 1999, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 , WORKS_WITH_2DIGIT_YEAR
},
617 { 6, wxDateTime::Feb
, 1937, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 ,0 },
618 { 6, wxDateTime::Feb
, 1856, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 ,0 },
619 { 6, wxDateTime::Feb
, 1857, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 ,0 },
620 { 29, wxDateTime::May
, 2076, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 ,0 },
621 { 29, wxDateTime::Feb
, 2400, 02, 15, 25, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 ,0 },
623 // Need to add support for BCE dates.
624 { 01, wxDateTime::Jan
, -52, 03, 16, 47, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 ,0 },
628 for ( size_t d
= 0; d
< WXSIZEOF(formatTestDates
) + 1; d
++ )
630 wxDateTime dt
= d
== 0 ? wxDateTime::Now() : formatTestDates
[d
- 1].DT();
631 for ( size_t n
= 0; n
< WXSIZEOF(formatTestFormats
); n
++ )
633 //Skip test if date hasn't got the required flags.
634 if ((d
!=0) && !(formatTestDates
[d
- 1].IsSet(formatTestFormats
[n
].flagsneeded
))) continue;
636 wxString s
= dt
.Format(formatTestFormats
[n
].format
);
637 // what can we recover?
638 int kind
= formatTestFormats
[n
].compareKind
;
642 const wxChar
*result
= dt2
.ParseFormat(s
, formatTestFormats
[n
].format
);
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 CPPUNIT_ASSERT( dt2
== dt
);
660 CPPUNIT_ASSERT( dt
.IsSameDate(dt2
) );
664 CPPUNIT_ASSERT( dt
.IsSameTime(dt2
) );
672 void DateTimeTestCase::TestTimeTicks()
674 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
676 const Date
& d
= testDates
[n
];
680 wxDateTime dt
= d
.DT();
681 //RN: Translate according to test's time zone
682 //2nd param is to ignore DST - it's already factored
684 dt
.MakeTimezone(wxDateTime::WEST
, true);
685 long ticks
= (dt
.GetValue() / 1000).ToLong();
686 CPPUNIT_ASSERT( ticks
== d
.ticks
);
688 dt
= d
.DT().FromTimezone(wxDateTime::GMT0
);
689 ticks
= (dt
.GetValue() / 1000).ToLong();
690 CPPUNIT_ASSERT( ticks
== d
.gmticks
);
694 // test parsing dates in RFC822 format
695 void DateTimeTestCase::TestParceRFC822()
697 static const struct ParseTestData
699 const wxChar
*rfc822
;
700 Date date
; // NB: this should be in UTC
705 _T("Sat, 18 Dec 1999 00:46:40 +0100"),
706 { 17, wxDateTime::Dec
, 1999, 23, 46, 40, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
710 _T("Wed, 1 Dec 1999 05:17:20 +0300"),
711 { 1, wxDateTime::Dec
, 1999, 2, 17, 20, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
715 _T("Sun, 28 Aug 2005 03:31:30 +0200"),
716 { 28, wxDateTime::Aug
, 2005, 1, 31, 30, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
721 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
724 if ( dt
.ParseRfc822Date(parseTestDates
[n
].rfc822
) )
726 CPPUNIT_ASSERT( parseTestDates
[n
].good
);
728 wxDateTime dtReal
= parseTestDates
[n
].date
.DT().FromUTC();
729 CPPUNIT_ASSERT_EQUAL( dtReal
, dt
);
731 else // failed to parse
733 CPPUNIT_ASSERT( !parseTestDates
[n
].good
);
738 // test parsing dates in free format
739 void DateTimeTestCase::TestDateParse()
741 static const struct ParseTestData
744 Date date
; // NB: this should be in UTC
748 { _T("21 Mar 2006"), { 21, wxDateTime::Mar
, 2006 }, true },
749 { _T("29 Feb 1976"), { 29, wxDateTime::Feb
, 1976 }, true },
750 { _T("Feb 29 1976"), { 29, wxDateTime::Feb
, 1976 }, true },
751 { _T("31/03/06"), { 31, wxDateTime::Mar
, 6 }, true },
752 { _T("31/03/2006"), { 31, wxDateTime::Mar
, 2006 }, true },
754 // some invalid ones too
755 { _T("29 Feb 2006") },
757 { _T("bloordyblop") }
762 CPPUNIT_ASSERT( dt
.ParseDate(_T("today")) );
763 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), dt
);
765 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
768 if ( dt
.ParseDate(parseTestDates
[n
].str
) )
770 CPPUNIT_ASSERT( parseTestDates
[n
].good
);
772 CPPUNIT_ASSERT_EQUAL( parseTestDates
[n
].date
.DT(), dt
);
774 else // failed to parse
776 CPPUNIT_ASSERT( !parseTestDates
[n
].good
);
781 void DateTimeTestCase::TestTimeArithmetics()
783 static const wxDateSpan testArithmData
[] =
791 // the test will *not* work with arbitrary date!
792 wxDateTime
dt(2, wxDateTime::Dec
, 1999),
796 for ( size_t n
= 0; n
< WXSIZEOF(testArithmData
); n
++ )
798 const wxDateSpan
& span
= testArithmData
[n
];
802 CPPUNIT_ASSERT( dt1
- span
== dt
);
803 CPPUNIT_ASSERT( dt2
+ span
== dt
);
804 CPPUNIT_ASSERT( dt2
+ 2*span
== dt1
);
808 void DateTimeTestCase::TestDSTBug()
810 /////////////////////////
812 wxDateTime dt
= wxDateTime::GetEndDST(2004);
813 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
814 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
815 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
816 CPPUNIT_ASSERT_EQUAL(2, (int)dt
.GetHour());
817 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
818 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
819 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
821 /////////////////////////
824 CPPUNIT_ASSERT_EQUAL(5, (int)dt
.GetHour());
826 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
827 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
828 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
829 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetHour());
830 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
831 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
832 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
835 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
837 /////////////////////////
839 #ifdef CHANGE_SYSTEM_DATE
841 DateChanger
change(2004, 10, 31, 5, 0, 0);
842 dt
= wxDateTime::Today();
845 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
846 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
847 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
848 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetHour());
849 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
850 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
851 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
853 /////////////////////////
854 // Test Set(hour, minute, second, milli)
857 DateChanger
change(2004, 10, 31, 5, 0, 0);
859 dt2
.Set(5, 30, 0, 0);
862 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
863 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
864 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
865 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
866 CPPUNIT_ASSERT_EQUAL(30, (int)dt
.GetMinute());
867 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
868 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
870 CPPUNIT_ASSERT_EQUAL(31, (int)dt2
.GetDay());
871 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt2
.GetMonth());
872 CPPUNIT_ASSERT_EQUAL(2004, (int)dt2
.GetYear());
873 CPPUNIT_ASSERT_EQUAL(5, (int)dt2
.GetHour());
874 CPPUNIT_ASSERT_EQUAL(30, (int)dt2
.GetMinute());
875 CPPUNIT_ASSERT_EQUAL(0, (int)dt2
.GetSecond());
876 CPPUNIT_ASSERT_EQUAL(0, (int)dt2
.GetMillisecond());
877 #endif // CHANGE_SYSTEM_DATE
880 #endif // wxUSE_DATETIME