]> git.saurik.com Git - wxWidgets.git/blob - tests/datetime/datetimetest.cpp
don't return items which are not visible from DoHitTest() (patch 1448508)
[wxWidgets.git] / tests / datetime / datetimetest.cpp
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)
6 // RCS-ID: $Id$
7 // Copyright: (c) 2004 Vadim Zeitlin <vadim@wxwindows.org>
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #endif // WX_PRECOMP
22
23 #if wxUSE_DATETIME
24
25 #include "wx/datetime.h"
26 #include "wx/ioswrap.h"
27
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)
30 {
31 ostr << dt.Format();
32
33 return ostr;
34 }
35
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
40
41 #ifndef __WINDOWS__
42 #undef CHANGE_SYSTEM_DATE
43 #endif
44
45 #ifdef CHANGE_SYSTEM_DATE
46
47 class DateChanger
48 {
49 public:
50 DateChanger(int year, int month, int day, int hour, int min, int sec)
51 {
52 SYSTEMTIME st;
53 st.wDay = day;
54 st.wMonth = month;
55 st.wYear = year;
56 st.wHour = hour;
57 st.wMinute = min;
58 st.wSecond = sec;
59 st.wMilliseconds = 0;
60
61 ::GetSystemTime(&m_savedTime);
62 ::GetTimeZoneInformation(&m_tzi);
63
64 m_changed = ::SetSystemTime(&st) != 0;
65 }
66
67 ~DateChanger()
68 {
69 if ( m_changed )
70 {
71 ::SetSystemTime(&m_savedTime);
72 ::SetTimeZoneInformation(&m_tzi);
73 }
74 }
75
76 private:
77 SYSTEMTIME m_savedTime;
78 TIME_ZONE_INFORMATION m_tzi;
79 bool m_changed;
80 };
81
82 #endif // CHANGE_SYSTEM_DATE
83
84 // ----------------------------------------------------------------------------
85 // broken down date representation used for testing
86 // ----------------------------------------------------------------------------
87
88 struct Date
89 {
90 wxDateTime::wxDateTime_t day;
91 wxDateTime::Month month;
92 int year;
93 wxDateTime::wxDateTime_t hour, min, sec;
94 double jdn;
95 wxDateTime::WeekDay wday;
96 time_t gmticks, ticks;
97 long flags; //Test specific flags - currently only used by TestTimeFormat.
98
99 void Init(const wxDateTime::Tm& tm)
100 {
101 day = tm.mday;
102 month = tm.mon;
103 year = tm.year;
104 hour = tm.hour;
105 min = tm.min;
106 sec = tm.sec;
107 jdn = 0.0;
108 gmticks = ticks = -1;
109 }
110
111 wxDateTime DT() const
112 { return wxDateTime(day, month, year, hour, min, sec); }
113
114 bool SameDay(const wxDateTime::Tm& tm) const
115 {
116 return day == tm.mday && month == tm.mon && year == tm.year;
117 }
118
119 wxString Format() const
120 {
121 wxString s;
122 s.Printf(_T("%02d:%02d:%02d %10s %02d, %4d%s"),
123 hour, min, sec,
124 wxDateTime::GetMonthName(month).c_str(),
125 day,
126 abs(wxDateTime::ConvertYearToBC(year)),
127 year > 0 ? _T("AD") : _T("BC"));
128 return s;
129 }
130
131 wxString FormatDate() const
132 {
133 wxString s;
134 s.Printf(_T("%02d-%s-%4d%s"),
135 day,
136 wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(),
137 abs(wxDateTime::ConvertYearToBC(year)),
138 year > 0 ? _T("AD") : _T("BC"));
139 return s;
140 }
141 bool IsSet(long f) const { return (f && flags) ==f; }
142 };
143
144 // ----------------------------------------------------------------------------
145 // test data
146 // ----------------------------------------------------------------------------
147
148 static const Date testDates[] =
149 {
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 },
169 };
170
171
172 // ----------------------------------------------------------------------------
173 // test class
174 // ----------------------------------------------------------------------------
175
176 class DateTimeTestCase : public CppUnit::TestCase
177 {
178 public:
179 DateTimeTestCase() { }
180
181 private:
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();
196
197 void TestLeapYears();
198 void TestTimeSet();
199 void TestTimeJDN();
200 void TestTimeWNumber();
201 void TestTimeWDays();
202 void TestTimeDST();
203 void TestTimeFormat();
204 void TestTimeTicks();
205 void TestParceRFC822();
206 void TestDateParse();
207 void TestTimeArithmetics();
208 void TestDSTBug();
209
210 DECLARE_NO_COPY_CLASS(DateTimeTestCase)
211 };
212
213 // register in the unnamed registry so that these tests are run by default
214 CPPUNIT_TEST_SUITE_REGISTRATION( DateTimeTestCase );
215
216 // also include in it's own registry so that these tests can be run alone
217 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DateTimeTestCase, "DateTimeTestCase" );
218
219 // ============================================================================
220 // implementation
221 // ============================================================================
222
223 // test leap years detection
224 void DateTimeTestCase::TestLeapYears()
225 {
226 static const struct LeapYearTestData
227 {
228 int year;
229 bool isLeap;
230 } years[] =
231 {
232 { 1900, false },
233 { 1990, false },
234 { 1976, true },
235 { 2000, true },
236 { 2030, false },
237 { 1984, true },
238 { 2100, false },
239 { 2400, true },
240 };
241
242 for ( size_t n = 0; n < WXSIZEOF(years); n++ )
243 {
244 const LeapYearTestData& y = years[n];
245
246 CPPUNIT_ASSERT( wxDateTime::IsLeapYear(y.year) == y.isLeap );
247 }
248 }
249
250 // test constructing wxDateTime objects
251 void DateTimeTestCase::TestTimeSet()
252 {
253 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
254 {
255 const Date& d1 = testDates[n];
256 wxDateTime dt = d1.DT();
257
258 Date d2;
259 d2.Init(dt.GetTm());
260
261 wxString s1 = d1.Format(),
262 s2 = d2.Format();
263
264 CPPUNIT_ASSERT( s1 == s2 );
265 }
266 }
267
268 // test conversions to JDN &c
269 void DateTimeTestCase::TestTimeJDN()
270 {
271 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
272 {
273 const Date& d = testDates[n];
274 wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
275
276 // JDNs must be computed for UTC times
277 double jdn = dt.FromUTC().GetJulianDayNumber();
278
279 CPPUNIT_ASSERT( jdn == d.jdn );
280
281 dt.Set(jdn);
282 CPPUNIT_ASSERT( dt.GetJulianDayNumber() == jdn );
283 }
284 }
285
286 // test week days computation
287 void DateTimeTestCase::TestTimeWDays()
288 {
289 // test GetWeekDay()
290 size_t n;
291 for ( n = 0; n < WXSIZEOF(testDates); n++ )
292 {
293 const Date& d = testDates[n];
294 wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
295
296 wxDateTime::WeekDay wday = dt.GetWeekDay();
297 CPPUNIT_ASSERT( wday == d.wday );
298 }
299
300 // test SetToWeekDay()
301 struct WeekDateTestData
302 {
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
308
309 wxString Format() const
310 {
311 wxString s, which;
312 switch ( nWeek < -1 ? -nWeek : nWeek )
313 {
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;
319
320 case -1: which = _T("last"); break;
321 }
322
323 if ( nWeek < -1 )
324 {
325 which += _T(" from end");
326 }
327
328 s.Printf(_T("The %s %s of %s in %d"),
329 which.c_str(),
330 wxDateTime::GetWeekDayName(wday).c_str(),
331 wxDateTime::GetMonthName(month).c_str(),
332 year);
333
334 return s;
335 }
336 };
337
338 // the array data was generated by the following python program
339 /*
340 from DateTime import *
341 from whrandom import *
342 from string import *
343
344 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
345 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
346
347 week = DateTimeDelta(7)
348
349 for n in range(20):
350 year = randint(1900, 2100)
351 month = randint(1, 12)
352 day = randint(1, 28)
353 dt = DateTime(year, month, day)
354 wday = dt.day_of_week
355
356 countFromEnd = choice([-1, 1])
357 weekNum = 0;
358
359 while dt.month is month:
360 dt = dt - countFromEnd * week
361 weekNum = weekNum + countFromEnd
362
363 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
364
365 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
366 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
367 */
368
369 static const WeekDateTestData weekDatesTestData[] =
370 {
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 }
391 };
392
393 wxDateTime dt;
394 for ( n = 0; n < WXSIZEOF(weekDatesTestData); n++ )
395 {
396 const WeekDateTestData& wd = weekDatesTestData[n];
397
398 dt.SetToWeekDay(wd.wday, wd.nWeek, wd.month, wd.year);
399
400 const Date& d = wd.date;
401 CPPUNIT_ASSERT( d.SameDay(dt.GetTm()) );
402 }
403 }
404
405 // test the computation of (ISO) week numbers
406 void DateTimeTestCase::TestTimeWNumber()
407 {
408 struct WeekNumberTestData
409 {
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
415 };
416
417 // data generated with the following python script:
418 /*
419 from DateTime import *
420 from whrandom import *
421 from string import *
422
423 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
424 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
425
426 def GetMonthWeek(dt):
427 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
428 if weekNumMonth < 0:
429 weekNumMonth = weekNumMonth + 53
430 return weekNumMonth
431
432 def GetLastSundayBefore(dt):
433 if dt.iso_week[2] == 7:
434 return dt
435 else:
436 return dt - DateTimeDelta(dt.iso_week[2])
437
438 for n in range(20):
439 year = randint(1900, 2100)
440 month = randint(1, 12)
441 day = randint(1, 28)
442 dt = DateTime(year, month, day)
443 dayNum = dt.day_of_year
444 weekNum = dt.iso_week[1]
445 weekNumMonth = GetMonthWeek(dt)
446
447 weekNumMonth2 = 0
448 dtSunday = GetLastSundayBefore(dt)
449
450 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
451 weekNumMonth2 = weekNumMonth2 + 1
452 dtSunday = dtSunday - DateTimeDelta(7)
453
454 data = { 'day': rjust(`day`, 2), \
455 'month': monthNames[month - 1], \
456 'year': year, \
457 'weekNum': rjust(`weekNum`, 2), \
458 'weekNumMonth': weekNumMonth, \
459 'weekNumMonth2': weekNumMonth2, \
460 'dayNum': rjust(`dayNum`, 3) }
461
462 print " { { %(day)s, "\
463 "wxDateTime::%(month)s, "\
464 "%(year)d }, "\
465 "%(weekNum)s, "\
466 "%(weekNumMonth)s, "\
467 "%(weekNumMonth2)s, "\
468 "%(dayNum)s }," % data
469
470 */
471 static const WeekNumberTestData weekNumberTestDates[] =
472 {
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 },
495 };
496
497 for ( size_t n = 0; n < WXSIZEOF(weekNumberTestDates); n++ )
498 {
499 const WeekNumberTestData& wn = weekNumberTestDates[n];
500 const Date& d = wn.date;
501
502 wxDateTime dt = d.DT();
503
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();
509
510 CPPUNIT_ASSERT( dnum == wn.dnum );
511 CPPUNIT_ASSERT( wmon == wn.wmon );
512 CPPUNIT_ASSERT( wmon2 == wn.wmon2 );
513 CPPUNIT_ASSERT( week == wn.week );
514
515 int year = d.year;
516 if ( week == 1 && d.month != wxDateTime::Jan )
517 {
518 // this means we're in the first week of the next year
519 year++;
520 }
521
522 wxDateTime
523 dt2 = wxDateTime::SetToWeekOfYear(year, week, dt.GetWeekDay());
524 CPPUNIT_ASSERT( dt2 == dt );
525 }
526 }
527
528 // test DST applicability
529 void DateTimeTestCase::TestTimeDST()
530 {
531 // taken from http://www.energy.ca.gov/daylightsaving.html
532 static const Date datesDST[2][2004 - 1900 + 1] =
533 {
534 {
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 },
550 },
551 {
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 },
567 }
568 };
569
570 for ( int year = 1990; year < 2005; year++ )
571 {
572 wxDateTime dtBegin = wxDateTime::GetBeginDST(year, wxDateTime::USA),
573 dtEnd = wxDateTime::GetEndDST(year, wxDateTime::USA);
574
575 size_t n = year - 1990;
576 const Date& dBegin = datesDST[0][n];
577 const Date& dEnd = datesDST[1][n];
578
579 CPPUNIT_ASSERT( dBegin.SameDay(dtBegin.GetTm()) );
580 CPPUNIT_ASSERT( dEnd.SameDay(dtEnd.GetTm()) );
581 }
582 }
583
584 // test wxDateTime -> text conversion
585 void DateTimeTestCase::TestTimeFormat()
586 {
587 // some information may be lost during conversion, so store what kind
588 // of info should we recover after a round trip
589 enum CompareKind
590 {
591 CompareNone, // don't try comparing
592 CompareBoth, // dates and times should be identical
593 CompareDate, // dates only
594 CompareTime // time only
595 };
596
597 const int WORKS_WITH_2DIGIT_YEAR(1);
598 static const struct
599 {
600 CompareKind compareKind;
601 long flagsneeded;
602 const wxChar *format;
603 } formatTestFormats[] =
604 {
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") },
611 };
612
613 static const Date formatTestDates[] =
614 {
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 },
622 #if 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 },
625 #endif
626 };
627
628 for ( size_t d = 0; d < WXSIZEOF(formatTestDates) + 1; d++ )
629 {
630 wxDateTime dt = d == 0 ? wxDateTime::Now() : formatTestDates[d - 1].DT();
631 for ( size_t n = 0; n < WXSIZEOF(formatTestFormats); n++ )
632 {
633 //Skip test if date hasn't got the required flags.
634 if ((d!=0) && !(formatTestDates[d - 1].IsSet(formatTestFormats[n].flagsneeded))) continue;
635
636 wxString s = dt.Format(formatTestFormats[n].format);
637 // what can we recover?
638 int kind = formatTestFormats[n].compareKind;
639
640 // convert back
641 wxDateTime dt2;
642 const wxChar *result = dt2.ParseFormat(s, formatTestFormats[n].format);
643 if ( !result )
644 {
645 // converion failed - should it have?
646 CPPUNIT_ASSERT( kind == CompareNone );
647 }
648 else // conversion succeeded
649 {
650 // should have parsed the entire string
651 CPPUNIT_ASSERT( !*result );
652
653 switch ( kind )
654 {
655 case CompareBoth:
656 CPPUNIT_ASSERT( dt2 == dt );
657 break;
658
659 case CompareDate:
660 CPPUNIT_ASSERT( dt.IsSameDate(dt2) );
661 break;
662
663 case CompareTime:
664 CPPUNIT_ASSERT( dt.IsSameTime(dt2) );
665 break;
666 }
667 }
668 }
669 }
670 }
671
672 void DateTimeTestCase::TestTimeTicks()
673 {
674 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
675 {
676 const Date& d = testDates[n];
677 if ( d.ticks == -1 )
678 continue;
679
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
683 //into Vadim's tests
684 dt.MakeTimezone(wxDateTime::WEST, true);
685 long ticks = (dt.GetValue() / 1000).ToLong();
686 CPPUNIT_ASSERT( ticks == d.ticks );
687
688 dt = d.DT().FromTimezone(wxDateTime::GMT0);
689 ticks = (dt.GetValue() / 1000).ToLong();
690 CPPUNIT_ASSERT( ticks == d.gmticks );
691 }
692 }
693
694 // test parsing dates in RFC822 format
695 void DateTimeTestCase::TestParceRFC822()
696 {
697 static const struct ParseTestData
698 {
699 const wxChar *rfc822;
700 Date date; // NB: this should be in UTC
701 bool good;
702 } parseTestDates[] =
703 {
704 {
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 },
707 true
708 },
709 {
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 },
712 true
713 },
714 {
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 },
717 true
718 },
719 };
720
721 for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
722 {
723 wxDateTime dt;
724 if ( dt.ParseRfc822Date(parseTestDates[n].rfc822) )
725 {
726 CPPUNIT_ASSERT( parseTestDates[n].good );
727
728 wxDateTime dtReal = parseTestDates[n].date.DT().FromUTC();
729 CPPUNIT_ASSERT_EQUAL( dtReal, dt );
730 }
731 else // failed to parse
732 {
733 CPPUNIT_ASSERT( !parseTestDates[n].good );
734 }
735 }
736 }
737
738 // test parsing dates in free format
739 void DateTimeTestCase::TestDateParse()
740 {
741 static const struct ParseTestData
742 {
743 const wxChar *str;
744 Date date; // NB: this should be in UTC
745 bool good;
746 } parseTestDates[] =
747 {
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 },
753
754 // some invalid ones too
755 { _T("29 Feb 2006") },
756 { _T("31/04/06") },
757 { _T("bloordyblop") }
758 };
759
760 // special cases
761 wxDateTime dt;
762 CPPUNIT_ASSERT( dt.ParseDate(_T("today")) );
763 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), dt );
764
765 for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
766 {
767 wxDateTime dt;
768 if ( dt.ParseDate(parseTestDates[n].str) )
769 {
770 CPPUNIT_ASSERT( parseTestDates[n].good );
771
772 CPPUNIT_ASSERT_EQUAL( parseTestDates[n].date.DT(), dt );
773 }
774 else // failed to parse
775 {
776 CPPUNIT_ASSERT( !parseTestDates[n].good );
777 }
778 }
779 }
780
781 void DateTimeTestCase::TestTimeArithmetics()
782 {
783 static const wxDateSpan testArithmData[] =
784 {
785 wxDateSpan::Day(),
786 wxDateSpan::Week(),
787 wxDateSpan::Month(),
788 wxDateSpan::Year(),
789 };
790
791 // the test will *not* work with arbitrary date!
792 wxDateTime dt(2, wxDateTime::Dec, 1999),
793 dt1,
794 dt2;
795
796 for ( size_t n = 0; n < WXSIZEOF(testArithmData); n++ )
797 {
798 const wxDateSpan& span = testArithmData[n];
799 dt1 = dt + span;
800 dt2 = dt - span;
801
802 CPPUNIT_ASSERT( dt1 - span == dt );
803 CPPUNIT_ASSERT( dt2 + span == dt );
804 CPPUNIT_ASSERT( dt2 + 2*span == dt1 );
805 }
806 }
807
808 void DateTimeTestCase::TestDSTBug()
809 {
810 /////////////////////////
811 // Test GetEndDST()
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());
820
821 /////////////////////////
822 // Test ResetTime()
823 dt.SetHour(5);
824 CPPUNIT_ASSERT_EQUAL(5, (int)dt.GetHour());
825 dt.ResetTime();
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());
833
834 dt.Set(1, 0, 0, 0);
835 CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
836
837 /////////////////////////
838 // Test Today()
839 #ifdef CHANGE_SYSTEM_DATE
840 {
841 DateChanger change(2004, 10, 31, 5, 0, 0);
842 dt = wxDateTime::Today();
843 }
844
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());
852
853 /////////////////////////
854 // Test Set(hour, minute, second, milli)
855 wxDateTime dt2;
856 {
857 DateChanger change(2004, 10, 31, 5, 0, 0);
858 dt.Set(1, 30, 0, 0);
859 dt2.Set(5, 30, 0, 0);
860 }
861
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());
869
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
878 }
879
880 #endif // wxUSE_DATETIME