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 // ----------------------------------------------------------------------------
26 // broken down date representation used for testing
27 // ----------------------------------------------------------------------------
31 wxDateTime::wxDateTime_t day
;
32 wxDateTime::Month month
;
34 wxDateTime::wxDateTime_t hour
, min
, sec
;
36 wxDateTime::WeekDay wday
;
37 time_t gmticks
, ticks
;
39 void Init(const wxDateTime::Tm
& tm
)
52 { return wxDateTime(day
, month
, year
, hour
, min
, sec
); }
54 bool SameDay(const wxDateTime::Tm
& tm
) const
56 return day
== tm
.mday
&& month
== tm
.mon
&& year
== tm
.year
;
59 wxString
Format() const
62 s
.Printf(_T("%02d:%02d:%02d %10s %02d, %4d%s"),
64 wxDateTime::GetMonthName(month
).c_str(),
66 abs(wxDateTime::ConvertYearToBC(year
)),
67 year
> 0 ? _T("AD") : _T("BC"));
71 wxString
FormatDate() const
74 s
.Printf(_T("%02d-%s-%4d%s"),
76 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
77 abs(wxDateTime::ConvertYearToBC(year
)),
78 year
> 0 ? _T("AD") : _T("BC"));
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 static const Date testDates
[] =
89 { 1, wxDateTime::Jan
, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu
, 0, -3600 },
90 { 7, wxDateTime::Feb
, 2036, 00, 00, 00, 2464730.5, wxDateTime::Thu
, -1, -1 },
91 { 8, wxDateTime::Feb
, 2036, 00, 00, 00, 2464731.5, wxDateTime::Fri
, -1, -1 },
92 { 1, wxDateTime::Jan
, 2037, 00, 00, 00, 2465059.5, wxDateTime::Thu
, -1, -1 },
93 { 1, wxDateTime::Jan
, 2038, 00, 00, 00, 2465424.5, wxDateTime::Fri
, -1, -1 },
94 { 21, wxDateTime::Jan
, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon
, -1, -1 },
95 { 29, wxDateTime::May
, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat
, 202219200, 202212000 },
96 { 29, wxDateTime::Feb
, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun
, 194400000, 194396400 },
97 { 1, wxDateTime::Jan
, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon
, -1, -1 },
98 { 1, wxDateTime::Jan
, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon
, -1, -1 },
99 { 15, wxDateTime::Oct
, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri
, -1, -1 },
100 { 4, wxDateTime::Oct
, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon
, -1, -1 },
101 { 1, wxDateTime::Mar
, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu
, -1, -1 },
102 { 1, wxDateTime::Jan
, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon
, -1, -1 },
103 { 31, wxDateTime::Dec
, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun
, -1, -1 },
104 { 1, wxDateTime::Jan
, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat
, -1, -1 },
105 { 12, wxDateTime::Aug
, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri
, -1, -1 },
106 { 12, wxDateTime::Aug
, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat
, -1, -1 },
107 { 24, wxDateTime::Nov
, -4713, 00, 00, 00, -0.5, wxDateTime::Mon
, -1, -1 },
111 // ----------------------------------------------------------------------------
113 // ----------------------------------------------------------------------------
115 class DateTimeTestCase
: public CppUnit::TestCase
118 DateTimeTestCase() { }
121 CPPUNIT_TEST_SUITE( DateTimeTestCase
);
122 CPPUNIT_TEST( TestLeapYears
);
123 CPPUNIT_TEST( TestTimeSet
);
124 CPPUNIT_TEST( TestTimeJDN
);
125 CPPUNIT_TEST( TestTimeWNumber
);
126 CPPUNIT_TEST( TestTimeWDays
);
127 CPPUNIT_TEST( TestTimeDST
);
128 CPPUNIT_TEST( TestTimeFormat
);
129 CPPUNIT_TEST( TestTimeTicks
);
130 CPPUNIT_TEST( TestTimeParse
);
131 CPPUNIT_TEST( TestTimeArithmetics
);
132 CPPUNIT_TEST_SUITE_END();
134 void TestLeapYears();
137 void TestTimeWNumber();
138 void TestTimeWDays();
140 void TestTimeFormat();
141 void TestTimeTicks();
142 void TestTimeParse();
143 void TestTimeArithmetics();
145 DECLARE_NO_COPY_CLASS(DateTimeTestCase
)
148 // register in the unnamed registry so that these tests are run by default
149 CPPUNIT_TEST_SUITE_REGISTRATION( DateTimeTestCase
);
151 // also include in it's own registry so that these tests can be run alone
152 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DateTimeTestCase
, "DateTimeTestCase" );
154 // ============================================================================
156 // ============================================================================
158 // test leap years detection
159 void DateTimeTestCase::TestLeapYears()
161 static const struct LeapYearTestData
177 for ( size_t n
= 0; n
< WXSIZEOF(years
); n
++ )
179 const LeapYearTestData
& y
= years
[n
];
181 CPPUNIT_ASSERT( wxDateTime::IsLeapYear(y
.year
) == y
.isLeap
);
185 // test constructing wxDateTime objects
186 void DateTimeTestCase::TestTimeSet()
188 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
190 const Date
& d1
= testDates
[n
];
191 wxDateTime dt
= d1
.DT();
196 wxString s1
= d1
.Format(),
199 CPPUNIT_ASSERT( s1
== s2
);
203 // test conversions to JDN &c
204 void DateTimeTestCase::TestTimeJDN()
206 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
208 const Date
& d
= testDates
[n
];
209 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
210 double jdn
= dt
.GetJulianDayNumber();
212 CPPUNIT_ASSERT( jdn
== d
.jdn
);
215 CPPUNIT_ASSERT( dt
.GetJulianDayNumber() == jdn
);
219 // test week days computation
220 void DateTimeTestCase::TestTimeWDays()
224 for ( n
= 0; n
< WXSIZEOF(testDates
); n
++ )
226 const Date
& d
= testDates
[n
];
227 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
229 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
230 CPPUNIT_ASSERT( wday
== d
.wday
);
233 // test SetToWeekDay()
234 struct WeekDateTestData
236 Date date
; // the real date (precomputed)
237 int nWeek
; // its week index in the month
238 wxDateTime::WeekDay wday
; // the weekday
239 wxDateTime::Month month
; // the month
240 int year
; // and the year
242 wxString
Format() const
245 switch ( nWeek
< -1 ? -nWeek
: nWeek
)
247 case 1: which
= _T("first"); break;
248 case 2: which
= _T("second"); break;
249 case 3: which
= _T("third"); break;
250 case 4: which
= _T("fourth"); break;
251 case 5: which
= _T("fifth"); break;
253 case -1: which
= _T("last"); break;
258 which
+= _T(" from end");
261 s
.Printf(_T("The %s %s of %s in %d"),
263 wxDateTime::GetWeekDayName(wday
).c_str(),
264 wxDateTime::GetMonthName(month
).c_str(),
271 // the array data was generated by the following python program
273 from DateTime import *
274 from whrandom import *
277 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
278 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
280 week = DateTimeDelta(7)
283 year = randint(1900, 2100)
284 month = randint(1, 12)
286 dt = DateTime(year, month, day)
287 wday = dt.day_of_week
289 countFromEnd = choice([-1, 1])
292 while dt.month is month:
293 dt = dt - countFromEnd * week
294 weekNum = weekNum + countFromEnd
296 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
298 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
299 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
302 static const WeekDateTestData weekDatesTestData
[] =
304 { { 20, wxDateTime::Mar
, 2045, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 3, wxDateTime::Mon
, wxDateTime::Mar
, 2045 },
305 { { 5, wxDateTime::Jun
, 1985, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -4, wxDateTime::Wed
, wxDateTime::Jun
, 1985 },
306 { { 12, wxDateTime::Nov
, 1961, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -3, wxDateTime::Sun
, wxDateTime::Nov
, 1961 },
307 { { 27, wxDateTime::Feb
, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -1, wxDateTime::Fri
, wxDateTime::Feb
, 2093 },
308 { { 4, wxDateTime::Jul
, 2070, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -4, wxDateTime::Fri
, wxDateTime::Jul
, 2070 },
309 { { 2, wxDateTime::Apr
, 1906, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -5, wxDateTime::Mon
, wxDateTime::Apr
, 1906 },
310 { { 19, wxDateTime::Jul
, 2023, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -2, wxDateTime::Wed
, wxDateTime::Jul
, 2023 },
311 { { 5, wxDateTime::May
, 1958, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -4, wxDateTime::Mon
, wxDateTime::May
, 1958 },
312 { { 11, wxDateTime::Aug
, 1900, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 2, wxDateTime::Sat
, wxDateTime::Aug
, 1900 },
313 { { 14, wxDateTime::Feb
, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 2, wxDateTime::Wed
, wxDateTime::Feb
, 1945 },
314 { { 25, wxDateTime::Jul
, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -1, wxDateTime::Tue
, wxDateTime::Jul
, 1967 },
315 { { 9, wxDateTime::May
, 1916, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -4, wxDateTime::Tue
, wxDateTime::May
, 1916 },
316 { { 20, wxDateTime::Jun
, 1927, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 3, wxDateTime::Mon
, wxDateTime::Jun
, 1927 },
317 { { 2, wxDateTime::Aug
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, wxDateTime::Wed
, wxDateTime::Aug
, 2000 },
318 { { 20, wxDateTime::Apr
, 2044, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 3, wxDateTime::Wed
, wxDateTime::Apr
, 2044 },
319 { { 20, wxDateTime::Feb
, 1932, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -2, wxDateTime::Sat
, wxDateTime::Feb
, 1932 },
320 { { 25, wxDateTime::Jul
, 2069, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 4, wxDateTime::Thu
, wxDateTime::Jul
, 2069 },
321 { { 3, wxDateTime::Apr
, 1925, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, wxDateTime::Fri
, wxDateTime::Apr
, 1925 },
322 { { 21, wxDateTime::Mar
, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 3, wxDateTime::Sat
, wxDateTime::Mar
, 2093 },
323 { { 3, wxDateTime::Dec
, 2074, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -5, wxDateTime::Mon
, wxDateTime::Dec
, 2074 }
327 for ( n
= 0; n
< WXSIZEOF(weekDatesTestData
); n
++ )
329 const WeekDateTestData
& wd
= weekDatesTestData
[n
];
331 dt
.SetToWeekDay(wd
.wday
, wd
.nWeek
, wd
.month
, wd
.year
);
333 const Date
& d
= wd
.date
;
334 CPPUNIT_ASSERT( d
.SameDay(dt
.GetTm()) );
338 // test the computation of (ISO) week numbers
339 void DateTimeTestCase::TestTimeWNumber()
341 struct WeekNumberTestData
343 Date date
; // the date
344 wxDateTime::wxDateTime_t week
; // the week number in the year
345 wxDateTime::wxDateTime_t wmon
; // the week number in the month
346 wxDateTime::wxDateTime_t wmon2
; // same but week starts with Sun
347 wxDateTime::wxDateTime_t dnum
; // day number in the year
350 // data generated with the following python script:
352 from DateTime import *
353 from whrandom import *
356 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
357 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
359 def GetMonthWeek(dt):
360 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
362 weekNumMonth = weekNumMonth + 53
365 def GetLastSundayBefore(dt):
366 if dt.iso_week[2] == 7:
369 return dt - DateTimeDelta(dt.iso_week[2])
372 year = randint(1900, 2100)
373 month = randint(1, 12)
375 dt = DateTime(year, month, day)
376 dayNum = dt.day_of_year
377 weekNum = dt.iso_week[1]
378 weekNumMonth = GetMonthWeek(dt)
381 dtSunday = GetLastSundayBefore(dt)
383 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
384 weekNumMonth2 = weekNumMonth2 + 1
385 dtSunday = dtSunday - DateTimeDelta(7)
387 data = { 'day': rjust(`day`, 2), \
388 'month': monthNames[month - 1], \
390 'weekNum': rjust(`weekNum`, 2), \
391 'weekNumMonth': weekNumMonth, \
392 'weekNumMonth2': weekNumMonth2, \
393 'dayNum': rjust(`dayNum`, 3) }
395 print " { { %(day)s, "\
396 "wxDateTime::%(month)s, "\
399 "%(weekNumMonth)s, "\
400 "%(weekNumMonth2)s, "\
401 "%(dayNum)s }," % data
404 static const WeekNumberTestData weekNumberTestDates
[] =
406 { { 27, wxDateTime::Dec
, 1966, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 52, 5, 5, 361 },
407 { { 22, wxDateTime::Jul
, 1926, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 29, 4, 4, 203 },
408 { { 22, wxDateTime::Oct
, 2076, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 43, 4, 4, 296 },
409 { { 1, wxDateTime::Jul
, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 26, 1, 1, 182 },
410 { { 8, wxDateTime::Nov
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 46, 2, 2, 313 },
411 { { 21, wxDateTime::Mar
, 1920, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 12, 3, 4, 81 },
412 { { 7, wxDateTime::Jan
, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, 2, 2, 7 },
413 { { 19, wxDateTime::Oct
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 42, 4, 4, 292 },
414 { { 13, wxDateTime::Aug
, 1955, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 32, 2, 2, 225 },
415 { { 18, wxDateTime::Jul
, 2087, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 29, 3, 3, 199 },
416 { { 2, wxDateTime::Sep
, 2028, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 35, 1, 1, 246 },
417 { { 28, wxDateTime::Jul
, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 30, 5, 4, 209 },
418 { { 15, wxDateTime::Jun
, 1901, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 24, 3, 3, 166 },
419 { { 10, wxDateTime::Oct
, 1939, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 41, 3, 2, 283 },
420 { { 3, wxDateTime::Dec
, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 48, 1, 1, 337 },
421 { { 23, wxDateTime::Feb
, 1940, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 8, 4, 4, 54 },
422 { { 2, wxDateTime::Jan
, 1987, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, 1, 1, 2 },
423 { { 11, wxDateTime::Aug
, 2079, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 32, 2, 2, 223 },
424 { { 2, wxDateTime::Feb
, 2063, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 5, 1, 1, 33 },
425 { { 16, wxDateTime::Oct
, 1942, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 42, 3, 3, 289 },
426 { { 30, wxDateTime::Dec
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, 5, 5, 364 },
427 { { 2, wxDateTime::Jan
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, 1, 1, 2 },
430 for ( size_t n
= 0; n
< WXSIZEOF(weekNumberTestDates
); n
++ )
432 const WeekNumberTestData
& wn
= weekNumberTestDates
[n
];
433 const Date
& d
= wn
.date
;
435 wxDateTime dt
= d
.DT();
437 wxDateTime::wxDateTime_t
438 week
= dt
.GetWeekOfYear(wxDateTime::Monday_First
),
439 wmon
= dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
440 wmon2
= dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
441 dnum
= dt
.GetDayOfYear();
443 CPPUNIT_ASSERT( dnum
== wn
.dnum
);
444 CPPUNIT_ASSERT( wmon
== wn
.wmon
);
445 CPPUNIT_ASSERT( wmon2
== wn
.wmon2
);
446 CPPUNIT_ASSERT( week
== wn
.week
);
449 if ( week
== 1 && d
.month
!= wxDateTime::Jan
)
451 // this means we're in the first week of the next year
456 dt2
= wxDateTime::SetToWeekOfYear(year
, week
, dt
.GetWeekDay());
457 CPPUNIT_ASSERT( dt2
== dt
);
461 // test DST applicability
462 void DateTimeTestCase::TestTimeDST()
464 // taken from http://www.energy.ca.gov/daylightsaving.html
465 static const Date datesDST
[2][2004 - 1900 + 1] =
468 { 1, wxDateTime::Apr
, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
469 { 7, wxDateTime::Apr
, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
470 { 5, wxDateTime::Apr
, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
471 { 4, wxDateTime::Apr
, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
472 { 3, wxDateTime::Apr
, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
473 { 2, wxDateTime::Apr
, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
474 { 7, wxDateTime::Apr
, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
475 { 6, wxDateTime::Apr
, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
476 { 5, wxDateTime::Apr
, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
477 { 4, wxDateTime::Apr
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
478 { 2, wxDateTime::Apr
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
479 { 1, wxDateTime::Apr
, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
480 { 7, wxDateTime::Apr
, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
481 { 6, wxDateTime::Apr
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
482 { 4, wxDateTime::Apr
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
485 { 28, wxDateTime::Oct
, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
486 { 27, wxDateTime::Oct
, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
487 { 25, wxDateTime::Oct
, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
488 { 31, wxDateTime::Oct
, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
489 { 30, wxDateTime::Oct
, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
490 { 29, wxDateTime::Oct
, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
491 { 27, wxDateTime::Oct
, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
492 { 26, wxDateTime::Oct
, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
493 { 25, wxDateTime::Oct
, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
494 { 31, wxDateTime::Oct
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
495 { 29, wxDateTime::Oct
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
496 { 28, wxDateTime::Oct
, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
497 { 27, wxDateTime::Oct
, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
498 { 26, wxDateTime::Oct
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
499 { 31, wxDateTime::Oct
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
503 for ( int year
= 1990; year
< 2005; year
++ )
505 wxDateTime dtBegin
= wxDateTime::GetBeginDST(year
, wxDateTime::USA
),
506 dtEnd
= wxDateTime::GetEndDST(year
, wxDateTime::USA
);
508 size_t n
= year
- 1990;
509 const Date
& dBegin
= datesDST
[0][n
];
510 const Date
& dEnd
= datesDST
[1][n
];
512 CPPUNIT_ASSERT( dBegin
.SameDay(dtBegin
.GetTm()) );
513 CPPUNIT_ASSERT( dEnd
.SameDay(dtEnd
.GetTm()) );
517 // test wxDateTime -> text conversion
518 void DateTimeTestCase::TestTimeFormat()
520 // some information may be lost during conversion, so store what kind
521 // of info should we recover after a round trip
524 CompareNone
, // don't try comparing
525 CompareBoth
, // dates and times should be identical
526 CompareDate
, // dates only
527 CompareTime
// time only
532 CompareKind compareKind
;
533 const wxChar
*format
;
534 } formatTestFormats
[] =
536 { CompareBoth
, _T("---> %c") },
537 { CompareDate
, _T("Date is %A, %d of %B, in year %Y") },
538 { CompareBoth
, _T("Date is %x, time is %X") },
539 { CompareTime
, _T("Time is %H:%M:%S or %I:%M:%S %p") },
540 { CompareNone
, _T("The day of year: %j, the week of year: %W") },
541 { CompareDate
, _T("ISO date without separators: %Y%m%d") },
544 static const Date formatTestDates
[] =
546 { 29, wxDateTime::May
, 1976, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
547 { 31, wxDateTime::Dec
, 1999, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
549 // this test can't work for other centuries because it uses two digit
550 // years in formats, so don't even try it
551 { 29, wxDateTime::May
, 2076, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
552 { 29, wxDateTime::Feb
, 2400, 02, 15, 25, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
553 { 01, wxDateTime::Jan
, -52, 03, 16, 47, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
557 for ( size_t d
= 0; d
< WXSIZEOF(formatTestDates
) + 1; d
++ )
559 wxDateTime dt
= d
== 0 ? wxDateTime::Now() : formatTestDates
[d
- 1].DT();
560 for ( size_t n
= 0; n
< WXSIZEOF(formatTestFormats
); n
++ )
562 wxString s
= dt
.Format(formatTestFormats
[n
].format
);
564 // what can we recover?
565 int kind
= formatTestFormats
[n
].compareKind
;
569 const wxChar
*result
= dt2
.ParseFormat(s
, formatTestFormats
[n
].format
);
572 // converion failed - should it have?
573 CPPUNIT_ASSERT( kind
== CompareNone
);
575 else // conversion succeeded
577 // should have parsed the entire string
578 CPPUNIT_ASSERT( !*result
);
583 CPPUNIT_ASSERT( dt2
== dt
);
587 CPPUNIT_ASSERT( dt
.IsSameDate(dt2
) );
591 CPPUNIT_ASSERT( dt
.IsSameTime(dt2
) );
599 void DateTimeTestCase::TestTimeTicks()
601 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
603 const Date
& d
= testDates
[n
];
607 wxDateTime dt
= d
.DT();
608 //RN: Translate according to test's time zone
609 //2nd param is to ignore DST - it's already factored
611 dt
.MakeTimezone(wxDateTime::WEST
, true);
612 long ticks
= (dt
.GetValue() / 1000).ToLong();
613 CPPUNIT_ASSERT( ticks
== d
.ticks
);
615 dt
= d
.DT().ToTimezone(wxDateTime::GMT0
);
616 ticks
= (dt
.GetValue() / 1000).ToLong();
617 CPPUNIT_ASSERT( ticks
== d
.gmticks
);
621 // test text -> wxDateTime conversion
622 void DateTimeTestCase::TestTimeParse()
624 static const struct ParseTestData
626 const wxChar
*format
;
631 { _T("Sat, 18 Dec 1999 00:46:40 +0100"), { 18, wxDateTime::Dec
, 1999, 00, 46, 40, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, true },
632 { _T("Wed, 1 Dec 1999 05:17:20 +0300"), { 1, wxDateTime::Dec
, 1999, 03, 17, 20, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, true },
635 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
637 const wxChar
*format
= parseTestDates
[n
].format
;
640 if ( dt
.ParseRfc822Date(format
) )
642 CPPUNIT_ASSERT( parseTestDates
[n
].good
);
644 wxDateTime dtReal
= parseTestDates
[n
].date
.DT();
645 //RN: We need this because the tests are based on
646 //a non-GMT time zone
647 dtReal
.MakeTimezone(wxDateTime::WEST
, true);
648 CPPUNIT_ASSERT( dt
== dtReal
);
650 else // failed to parse
652 CPPUNIT_ASSERT( !parseTestDates
[n
].good
);
657 void DateTimeTestCase::TestTimeArithmetics()
659 static const wxDateSpan testArithmData
[] =
667 // the test will *not* work with arbitrary date!
668 wxDateTime
dt(2, wxDateTime::Dec
, 1999),
672 for ( size_t n
= 0; n
< WXSIZEOF(testArithmData
); n
++ )
674 const wxDateSpan
& span
= testArithmData
[n
];
678 CPPUNIT_ASSERT( dt1
- span
== dt
);
679 CPPUNIT_ASSERT( dt2
+ span
== dt
);
680 CPPUNIT_ASSERT( dt2
+ 2*span
== dt1
);