]> git.saurik.com Git - wxWidgets.git/blob - tests/datetime/datetimetest.cpp
Added the wx.HVScrolledWindow class.
[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 #include "wx/datetime.h"
24
25 // to test Today() meaningfully we must be able to change the system date which
26 // is not usually the case, but if we're under Win32 we can try it -- define
27 // the macro below to do it
28 //#define CHANGE_SYSTEM_DATE
29
30 #if wxUSE_DATETIME
31
32 #ifndef __WINDOWS__
33 #undef CHANGE_SYSTEM_DATE
34 #endif
35
36 #ifdef CHANGE_SYSTEM_DATE
37
38 class DateChanger
39 {
40 public:
41 DateChanger(int year, int month, int day, int hour, int min, int sec)
42 {
43 SYSTEMTIME st;
44 st.wDay = day;
45 st.wMonth = month;
46 st.wYear = year;
47 st.wHour = hour;
48 st.wMinute = min;
49 st.wSecond = sec;
50 st.wMilliseconds = 0;
51
52 ::GetSystemTime(&m_savedTime);
53 ::GetTimeZoneInformation(&m_tzi);
54
55 m_changed = ::SetSystemTime(&st) != 0;
56 }
57
58 ~DateChanger()
59 {
60 if ( m_changed )
61 {
62 ::SetSystemTime(&m_savedTime);
63 ::SetTimeZoneInformation(&m_tzi);
64 }
65 }
66
67 private:
68 SYSTEMTIME m_savedTime;
69 TIME_ZONE_INFORMATION m_tzi;
70 bool m_changed;
71 };
72
73 #endif // CHANGE_SYSTEM_DATE
74
75 // ----------------------------------------------------------------------------
76 // broken down date representation used for testing
77 // ----------------------------------------------------------------------------
78
79 struct Date
80 {
81 wxDateTime::wxDateTime_t day;
82 wxDateTime::Month month;
83 int year;
84 wxDateTime::wxDateTime_t hour, min, sec;
85 double jdn;
86 wxDateTime::WeekDay wday;
87 time_t gmticks, ticks;
88 long flags; //Test specific flags - currently only used by TestTimeFormat.
89
90 void Init(const wxDateTime::Tm& tm)
91 {
92 day = tm.mday;
93 month = tm.mon;
94 year = tm.year;
95 hour = tm.hour;
96 min = tm.min;
97 sec = tm.sec;
98 jdn = 0.0;
99 gmticks = ticks = -1;
100 }
101
102 wxDateTime DT() const
103 { return wxDateTime(day, month, year, hour, min, sec); }
104
105 bool SameDay(const wxDateTime::Tm& tm) const
106 {
107 return day == tm.mday && month == tm.mon && year == tm.year;
108 }
109
110 wxString Format() const
111 {
112 wxString s;
113 s.Printf(_T("%02d:%02d:%02d %10s %02d, %4d%s"),
114 hour, min, sec,
115 wxDateTime::GetMonthName(month).c_str(),
116 day,
117 abs(wxDateTime::ConvertYearToBC(year)),
118 year > 0 ? _T("AD") : _T("BC"));
119 return s;
120 }
121
122 wxString FormatDate() const
123 {
124 wxString s;
125 s.Printf(_T("%02d-%s-%4d%s"),
126 day,
127 wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(),
128 abs(wxDateTime::ConvertYearToBC(year)),
129 year > 0 ? _T("AD") : _T("BC"));
130 return s;
131 }
132 bool IsSet(long f) const { return (f && flags) ==f; }
133 };
134
135 // ----------------------------------------------------------------------------
136 // test data
137 // ----------------------------------------------------------------------------
138
139 static const Date testDates[] =
140 {
141 { 1, wxDateTime::Jan, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu, 0, -3600 },
142 { 7, wxDateTime::Feb, 2036, 00, 00, 00, 2464730.5, wxDateTime::Thu, -1, -1 },
143 { 8, wxDateTime::Feb, 2036, 00, 00, 00, 2464731.5, wxDateTime::Fri, -1, -1 },
144 { 1, wxDateTime::Jan, 2037, 00, 00, 00, 2465059.5, wxDateTime::Thu, -1, -1 },
145 { 1, wxDateTime::Jan, 2038, 00, 00, 00, 2465424.5, wxDateTime::Fri, -1, -1 },
146 { 21, wxDateTime::Jan, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon, -1, -1 },
147 { 29, wxDateTime::May, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat, 202219200, 202212000 },
148 { 29, wxDateTime::Feb, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun, 194400000, 194396400 },
149 { 1, wxDateTime::Jan, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon, -1, -1 },
150 { 1, wxDateTime::Jan, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon, -1, -1 },
151 { 15, wxDateTime::Oct, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri, -1, -1 },
152 { 4, wxDateTime::Oct, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon, -1, -1 },
153 { 1, wxDateTime::Mar, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu, -1, -1 },
154 { 1, wxDateTime::Jan, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon, -1, -1 },
155 { 31, wxDateTime::Dec, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun, -1, -1 },
156 { 1, wxDateTime::Jan, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat, -1, -1 },
157 { 12, wxDateTime::Aug, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri, -1, -1 },
158 { 12, wxDateTime::Aug, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat, -1, -1 },
159 { 24, wxDateTime::Nov, -4713, 00, 00, 00, -0.5, wxDateTime::Mon, -1, -1 },
160 };
161
162
163 // ----------------------------------------------------------------------------
164 // test class
165 // ----------------------------------------------------------------------------
166
167 class DateTimeTestCase : public CppUnit::TestCase
168 {
169 public:
170 DateTimeTestCase() { }
171
172 private:
173 CPPUNIT_TEST_SUITE( DateTimeTestCase );
174 CPPUNIT_TEST( TestLeapYears );
175 CPPUNIT_TEST( TestTimeSet );
176 CPPUNIT_TEST( TestTimeJDN );
177 CPPUNIT_TEST( TestTimeWNumber );
178 CPPUNIT_TEST( TestTimeWDays );
179 CPPUNIT_TEST( TestTimeDST );
180 CPPUNIT_TEST( TestTimeFormat );
181 CPPUNIT_TEST( TestTimeTicks );
182 CPPUNIT_TEST( TestTimeParse );
183 CPPUNIT_TEST( TestTimeArithmetics );
184 CPPUNIT_TEST( TestDSTBug );
185 CPPUNIT_TEST_SUITE_END();
186
187 void TestLeapYears();
188 void TestTimeSet();
189 void TestTimeJDN();
190 void TestTimeWNumber();
191 void TestTimeWDays();
192 void TestTimeDST();
193 void TestTimeFormat();
194 void TestTimeTicks();
195 void TestTimeParse();
196 void TestTimeArithmetics();
197 void TestDSTBug();
198
199 DECLARE_NO_COPY_CLASS(DateTimeTestCase)
200 };
201
202 // register in the unnamed registry so that these tests are run by default
203 CPPUNIT_TEST_SUITE_REGISTRATION( DateTimeTestCase );
204
205 // also include in it's own registry so that these tests can be run alone
206 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DateTimeTestCase, "DateTimeTestCase" );
207
208 // ============================================================================
209 // implementation
210 // ============================================================================
211
212 // test leap years detection
213 void DateTimeTestCase::TestLeapYears()
214 {
215 static const struct LeapYearTestData
216 {
217 int year;
218 bool isLeap;
219 } years[] =
220 {
221 { 1900, false },
222 { 1990, false },
223 { 1976, true },
224 { 2000, true },
225 { 2030, false },
226 { 1984, true },
227 { 2100, false },
228 { 2400, true },
229 };
230
231 for ( size_t n = 0; n < WXSIZEOF(years); n++ )
232 {
233 const LeapYearTestData& y = years[n];
234
235 CPPUNIT_ASSERT( wxDateTime::IsLeapYear(y.year) == y.isLeap );
236 }
237 }
238
239 // test constructing wxDateTime objects
240 void DateTimeTestCase::TestTimeSet()
241 {
242 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
243 {
244 const Date& d1 = testDates[n];
245 wxDateTime dt = d1.DT();
246
247 Date d2;
248 d2.Init(dt.GetTm());
249
250 wxString s1 = d1.Format(),
251 s2 = d2.Format();
252
253 CPPUNIT_ASSERT( s1 == s2 );
254 }
255 }
256
257 // test conversions to JDN &c
258 void DateTimeTestCase::TestTimeJDN()
259 {
260 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
261 {
262 const Date& d = testDates[n];
263 wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
264
265 // JDNs must be computed for UTC times
266 double jdn = dt.FromUTC().GetJulianDayNumber();
267
268 CPPUNIT_ASSERT( jdn == d.jdn );
269
270 dt.Set(jdn);
271 CPPUNIT_ASSERT( dt.GetJulianDayNumber() == jdn );
272 }
273 }
274
275 // test week days computation
276 void DateTimeTestCase::TestTimeWDays()
277 {
278 // test GetWeekDay()
279 size_t n;
280 for ( n = 0; n < WXSIZEOF(testDates); n++ )
281 {
282 const Date& d = testDates[n];
283 wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
284
285 wxDateTime::WeekDay wday = dt.GetWeekDay();
286 CPPUNIT_ASSERT( wday == d.wday );
287 }
288
289 // test SetToWeekDay()
290 struct WeekDateTestData
291 {
292 Date date; // the real date (precomputed)
293 int nWeek; // its week index in the month
294 wxDateTime::WeekDay wday; // the weekday
295 wxDateTime::Month month; // the month
296 int year; // and the year
297
298 wxString Format() const
299 {
300 wxString s, which;
301 switch ( nWeek < -1 ? -nWeek : nWeek )
302 {
303 case 1: which = _T("first"); break;
304 case 2: which = _T("second"); break;
305 case 3: which = _T("third"); break;
306 case 4: which = _T("fourth"); break;
307 case 5: which = _T("fifth"); break;
308
309 case -1: which = _T("last"); break;
310 }
311
312 if ( nWeek < -1 )
313 {
314 which += _T(" from end");
315 }
316
317 s.Printf(_T("The %s %s of %s in %d"),
318 which.c_str(),
319 wxDateTime::GetWeekDayName(wday).c_str(),
320 wxDateTime::GetMonthName(month).c_str(),
321 year);
322
323 return s;
324 }
325 };
326
327 // the array data was generated by the following python program
328 /*
329 from DateTime import *
330 from whrandom import *
331 from string import *
332
333 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
334 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
335
336 week = DateTimeDelta(7)
337
338 for n in range(20):
339 year = randint(1900, 2100)
340 month = randint(1, 12)
341 day = randint(1, 28)
342 dt = DateTime(year, month, day)
343 wday = dt.day_of_week
344
345 countFromEnd = choice([-1, 1])
346 weekNum = 0;
347
348 while dt.month is month:
349 dt = dt - countFromEnd * week
350 weekNum = weekNum + countFromEnd
351
352 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
353
354 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
355 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
356 */
357
358 static const WeekDateTestData weekDatesTestData[] =
359 {
360 { { 20, wxDateTime::Mar, 2045, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 3, wxDateTime::Mon, wxDateTime::Mar, 2045 },
361 { { 5, wxDateTime::Jun, 1985, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, -4, wxDateTime::Wed, wxDateTime::Jun, 1985 },
362 { { 12, wxDateTime::Nov, 1961, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, -3, wxDateTime::Sun, wxDateTime::Nov, 1961 },
363 { { 27, wxDateTime::Feb, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, -1, wxDateTime::Fri, wxDateTime::Feb, 2093 },
364 { { 4, wxDateTime::Jul, 2070, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, -4, wxDateTime::Fri, wxDateTime::Jul, 2070 },
365 { { 2, wxDateTime::Apr, 1906, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, -5, wxDateTime::Mon, wxDateTime::Apr, 1906 },
366 { { 19, wxDateTime::Jul, 2023, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, -2, wxDateTime::Wed, wxDateTime::Jul, 2023 },
367 { { 5, wxDateTime::May, 1958, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, -4, wxDateTime::Mon, wxDateTime::May, 1958 },
368 { { 11, wxDateTime::Aug, 1900, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 2, wxDateTime::Sat, wxDateTime::Aug, 1900 },
369 { { 14, wxDateTime::Feb, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 2, wxDateTime::Wed, wxDateTime::Feb, 1945 },
370 { { 25, wxDateTime::Jul, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, -1, wxDateTime::Tue, wxDateTime::Jul, 1967 },
371 { { 9, wxDateTime::May, 1916, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, -4, wxDateTime::Tue, wxDateTime::May, 1916 },
372 { { 20, wxDateTime::Jun, 1927, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 3, wxDateTime::Mon, wxDateTime::Jun, 1927 },
373 { { 2, wxDateTime::Aug, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 1, wxDateTime::Wed, wxDateTime::Aug, 2000 },
374 { { 20, wxDateTime::Apr, 2044, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 3, wxDateTime::Wed, wxDateTime::Apr, 2044 },
375 { { 20, wxDateTime::Feb, 1932, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, -2, wxDateTime::Sat, wxDateTime::Feb, 1932 },
376 { { 25, wxDateTime::Jul, 2069, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 4, wxDateTime::Thu, wxDateTime::Jul, 2069 },
377 { { 3, wxDateTime::Apr, 1925, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 1, wxDateTime::Fri, wxDateTime::Apr, 1925 },
378 { { 21, wxDateTime::Mar, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 3, wxDateTime::Sat, wxDateTime::Mar, 2093 },
379 { { 3, wxDateTime::Dec, 2074, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, -5, wxDateTime::Mon, wxDateTime::Dec, 2074 }
380 };
381
382 wxDateTime dt;
383 for ( n = 0; n < WXSIZEOF(weekDatesTestData); n++ )
384 {
385 const WeekDateTestData& wd = weekDatesTestData[n];
386
387 dt.SetToWeekDay(wd.wday, wd.nWeek, wd.month, wd.year);
388
389 const Date& d = wd.date;
390 CPPUNIT_ASSERT( d.SameDay(dt.GetTm()) );
391 }
392 }
393
394 // test the computation of (ISO) week numbers
395 void DateTimeTestCase::TestTimeWNumber()
396 {
397 struct WeekNumberTestData
398 {
399 Date date; // the date
400 wxDateTime::wxDateTime_t week; // the week number in the year
401 wxDateTime::wxDateTime_t wmon; // the week number in the month
402 wxDateTime::wxDateTime_t wmon2; // same but week starts with Sun
403 wxDateTime::wxDateTime_t dnum; // day number in the year
404 };
405
406 // data generated with the following python script:
407 /*
408 from DateTime import *
409 from whrandom import *
410 from string import *
411
412 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
413 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
414
415 def GetMonthWeek(dt):
416 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
417 if weekNumMonth < 0:
418 weekNumMonth = weekNumMonth + 53
419 return weekNumMonth
420
421 def GetLastSundayBefore(dt):
422 if dt.iso_week[2] == 7:
423 return dt
424 else:
425 return dt - DateTimeDelta(dt.iso_week[2])
426
427 for n in range(20):
428 year = randint(1900, 2100)
429 month = randint(1, 12)
430 day = randint(1, 28)
431 dt = DateTime(year, month, day)
432 dayNum = dt.day_of_year
433 weekNum = dt.iso_week[1]
434 weekNumMonth = GetMonthWeek(dt)
435
436 weekNumMonth2 = 0
437 dtSunday = GetLastSundayBefore(dt)
438
439 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
440 weekNumMonth2 = weekNumMonth2 + 1
441 dtSunday = dtSunday - DateTimeDelta(7)
442
443 data = { 'day': rjust(`day`, 2), \
444 'month': monthNames[month - 1], \
445 'year': year, \
446 'weekNum': rjust(`weekNum`, 2), \
447 'weekNumMonth': weekNumMonth, \
448 'weekNumMonth2': weekNumMonth2, \
449 'dayNum': rjust(`dayNum`, 3) }
450
451 print " { { %(day)s, "\
452 "wxDateTime::%(month)s, "\
453 "%(year)d }, "\
454 "%(weekNum)s, "\
455 "%(weekNumMonth)s, "\
456 "%(weekNumMonth2)s, "\
457 "%(dayNum)s }," % data
458
459 */
460 static const WeekNumberTestData weekNumberTestDates[] =
461 {
462 { { 27, wxDateTime::Dec, 1966, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 52, 5, 5, 361 },
463 { { 22, wxDateTime::Jul, 1926, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 29, 4, 4, 203 },
464 { { 22, wxDateTime::Oct, 2076, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 43, 4, 4, 296 },
465 { { 1, wxDateTime::Jul, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 26, 1, 1, 182 },
466 { { 8, wxDateTime::Nov, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 46, 2, 2, 313 },
467 { { 21, wxDateTime::Mar, 1920, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 12, 3, 4, 81 },
468 { { 7, wxDateTime::Jan, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 1, 2, 2, 7 },
469 { { 19, wxDateTime::Oct, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 42, 4, 4, 292 },
470 { { 13, wxDateTime::Aug, 1955, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 32, 2, 2, 225 },
471 { { 18, wxDateTime::Jul, 2087, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 29, 3, 3, 199 },
472 { { 2, wxDateTime::Sep, 2028, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 35, 1, 1, 246 },
473 { { 28, wxDateTime::Jul, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 30, 5, 4, 209 },
474 { { 15, wxDateTime::Jun, 1901, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 24, 3, 3, 166 },
475 { { 10, wxDateTime::Oct, 1939, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 41, 3, 2, 283 },
476 { { 3, wxDateTime::Dec, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 48, 1, 1, 337 },
477 { { 23, wxDateTime::Feb, 1940, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 8, 4, 4, 54 },
478 { { 2, wxDateTime::Jan, 1987, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 1, 1, 1, 2 },
479 { { 11, wxDateTime::Aug, 2079, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 32, 2, 2, 223 },
480 { { 2, wxDateTime::Feb, 2063, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 5, 1, 1, 33 },
481 { { 16, wxDateTime::Oct, 1942, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 42, 3, 3, 289 },
482 { { 30, wxDateTime::Dec, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 1, 5, 5, 364 },
483 { { 2, wxDateTime::Jan, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 1, 1, 1, 2 },
484 };
485
486 for ( size_t n = 0; n < WXSIZEOF(weekNumberTestDates); n++ )
487 {
488 const WeekNumberTestData& wn = weekNumberTestDates[n];
489 const Date& d = wn.date;
490
491 wxDateTime dt = d.DT();
492
493 wxDateTime::wxDateTime_t
494 week = dt.GetWeekOfYear(wxDateTime::Monday_First),
495 wmon = dt.GetWeekOfMonth(wxDateTime::Monday_First),
496 wmon2 = dt.GetWeekOfMonth(wxDateTime::Sunday_First),
497 dnum = dt.GetDayOfYear();
498
499 CPPUNIT_ASSERT( dnum == wn.dnum );
500 CPPUNIT_ASSERT( wmon == wn.wmon );
501 CPPUNIT_ASSERT( wmon2 == wn.wmon2 );
502 CPPUNIT_ASSERT( week == wn.week );
503
504 int year = d.year;
505 if ( week == 1 && d.month != wxDateTime::Jan )
506 {
507 // this means we're in the first week of the next year
508 year++;
509 }
510
511 wxDateTime
512 dt2 = wxDateTime::SetToWeekOfYear(year, week, dt.GetWeekDay());
513 CPPUNIT_ASSERT( dt2 == dt );
514 }
515 }
516
517 // test DST applicability
518 void DateTimeTestCase::TestTimeDST()
519 {
520 // taken from http://www.energy.ca.gov/daylightsaving.html
521 static const Date datesDST[2][2004 - 1900 + 1] =
522 {
523 {
524 { 1, wxDateTime::Apr, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
525 { 7, wxDateTime::Apr, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
526 { 5, wxDateTime::Apr, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
527 { 4, wxDateTime::Apr, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
528 { 3, wxDateTime::Apr, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
529 { 2, wxDateTime::Apr, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
530 { 7, wxDateTime::Apr, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
531 { 6, wxDateTime::Apr, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
532 { 5, wxDateTime::Apr, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
533 { 4, wxDateTime::Apr, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
534 { 2, wxDateTime::Apr, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
535 { 1, wxDateTime::Apr, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
536 { 7, wxDateTime::Apr, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
537 { 6, wxDateTime::Apr, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
538 { 4, wxDateTime::Apr, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
539 },
540 {
541 { 28, wxDateTime::Oct, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
542 { 27, wxDateTime::Oct, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
543 { 25, wxDateTime::Oct, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
544 { 31, wxDateTime::Oct, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
545 { 30, wxDateTime::Oct, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
546 { 29, wxDateTime::Oct, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
547 { 27, wxDateTime::Oct, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
548 { 26, wxDateTime::Oct, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
549 { 25, wxDateTime::Oct, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
550 { 31, wxDateTime::Oct, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
551 { 29, wxDateTime::Oct, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
552 { 28, wxDateTime::Oct, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
553 { 27, wxDateTime::Oct, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
554 { 26, wxDateTime::Oct, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
555 { 31, wxDateTime::Oct, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
556 }
557 };
558
559 for ( int year = 1990; year < 2005; year++ )
560 {
561 wxDateTime dtBegin = wxDateTime::GetBeginDST(year, wxDateTime::USA),
562 dtEnd = wxDateTime::GetEndDST(year, wxDateTime::USA);
563
564 size_t n = year - 1990;
565 const Date& dBegin = datesDST[0][n];
566 const Date& dEnd = datesDST[1][n];
567
568 CPPUNIT_ASSERT( dBegin.SameDay(dtBegin.GetTm()) );
569 CPPUNIT_ASSERT( dEnd.SameDay(dtEnd.GetTm()) );
570 }
571 }
572
573 // test wxDateTime -> text conversion
574 void DateTimeTestCase::TestTimeFormat()
575 {
576 // some information may be lost during conversion, so store what kind
577 // of info should we recover after a round trip
578 enum CompareKind
579 {
580 CompareNone, // don't try comparing
581 CompareBoth, // dates and times should be identical
582 CompareDate, // dates only
583 CompareTime // time only
584 };
585
586 const int WORKS_WITH_2DIGIT_YEAR(1);
587 static const struct
588 {
589 CompareKind compareKind;
590 long flagsneeded;
591 const wxChar *format;
592 } formatTestFormats[] =
593 {
594 { CompareBoth,0 , _T("---> %c") }, //Assumes %c show a 4digit year.
595 { CompareDate,0, _T("Date is %A, %d of %B, in year %Y") },
596 { CompareBoth,WORKS_WITH_2DIGIT_YEAR, _T("Date is %x, time is %X") },
597 { CompareTime,0, _T("Time is %H:%M:%S or %I:%M:%S %p") },
598 { CompareNone,0, _T("The day of year: %j, the week of year: %W") },
599 { CompareDate,0, _T("ISO date without separators: %Y%m%d") },
600 };
601
602 static const Date formatTestDates[] =
603 {
604 { 29, wxDateTime::May, 1976, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay, 0, 0 , WORKS_WITH_2DIGIT_YEAR},
605 { 31, wxDateTime::Dec, 1999, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay, 0, 0 , WORKS_WITH_2DIGIT_YEAR },
606 { 6, wxDateTime::Feb, 1937, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay, 0, 0 ,0 },
607 { 6, wxDateTime::Feb, 1856, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay, 0, 0 ,0 },
608 { 6, wxDateTime::Feb, 1857, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay, 0, 0 ,0 },
609 { 29, wxDateTime::May, 2076, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay, 0, 0 ,0 },
610 { 29, wxDateTime::Feb, 2400, 02, 15, 25, 0.0, wxDateTime::Inv_WeekDay, 0, 0 ,0 },
611 #if 0
612 // Need to add support for BCE dates.
613 { 01, wxDateTime::Jan, -52, 03, 16, 47, 0.0, wxDateTime::Inv_WeekDay, 0, 0 ,0 },
614 #endif
615 };
616
617 for ( size_t d = 0; d < WXSIZEOF(formatTestDates) + 1; d++ )
618 {
619 wxDateTime dt = d == 0 ? wxDateTime::Now() : formatTestDates[d - 1].DT();
620 for ( size_t n = 0; n < WXSIZEOF(formatTestFormats); n++ )
621 {
622 //Skip test if date hasn't got the required flags.
623 if ((d!=0) && !(formatTestDates[d - 1].IsSet(formatTestFormats[n].flagsneeded))) continue;
624
625 wxString s = dt.Format(formatTestFormats[n].format);
626 // what can we recover?
627 int kind = formatTestFormats[n].compareKind;
628
629 // convert back
630 wxDateTime dt2;
631 const wxChar *result = dt2.ParseFormat(s, formatTestFormats[n].format);
632 if ( !result )
633 {
634 // converion failed - should it have?
635 CPPUNIT_ASSERT( kind == CompareNone );
636 }
637 else // conversion succeeded
638 {
639 // should have parsed the entire string
640 CPPUNIT_ASSERT( !*result );
641
642 switch ( kind )
643 {
644 case CompareBoth:
645 CPPUNIT_ASSERT( dt2 == dt );
646 break;
647
648 case CompareDate:
649 CPPUNIT_ASSERT( dt.IsSameDate(dt2) );
650 break;
651
652 case CompareTime:
653 CPPUNIT_ASSERT( dt.IsSameTime(dt2) );
654 break;
655 }
656 }
657 }
658 }
659 }
660
661 void DateTimeTestCase::TestTimeTicks()
662 {
663 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
664 {
665 const Date& d = testDates[n];
666 if ( d.ticks == -1 )
667 continue;
668
669 wxDateTime dt = d.DT();
670 //RN: Translate according to test's time zone
671 //2nd param is to ignore DST - it's already factored
672 //into Vadim's tests
673 dt.MakeTimezone(wxDateTime::WEST, true);
674 long ticks = (dt.GetValue() / 1000).ToLong();
675 CPPUNIT_ASSERT( ticks == d.ticks );
676
677 dt = d.DT().FromTimezone(wxDateTime::GMT0);
678 ticks = (dt.GetValue() / 1000).ToLong();
679 CPPUNIT_ASSERT( ticks == d.gmticks );
680 }
681 }
682
683 // test text -> wxDateTime conversion
684 void DateTimeTestCase::TestTimeParse()
685 {
686 static const struct ParseTestData
687 {
688 const wxChar *format;
689 Date date; // NB: this should be in UTC
690 bool good;
691 } parseTestDates[] =
692 {
693 {
694 _T("Sat, 18 Dec 1999 00:46:40 +0100"),
695 { 17, wxDateTime::Dec, 1999, 23, 46, 40, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
696 true
697 },
698 {
699 _T("Wed, 1 Dec 1999 05:17:20 +0300"),
700 { 1, wxDateTime::Dec, 1999, 2, 17, 20, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
701 true
702 },
703 {
704 _T("Sun, 28 Aug 2005 03:31:30 +0200"),
705 { 28, wxDateTime::Aug, 2005, 1, 31, 30, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
706 true
707 },
708 };
709
710 for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
711 {
712 const wxChar *format = parseTestDates[n].format;
713
714 wxDateTime dt;
715 if ( dt.ParseRfc822Date(format) )
716 {
717 CPPUNIT_ASSERT( parseTestDates[n].good );
718
719 wxDateTime dtReal = parseTestDates[n].date.DT().FromUTC();
720 CPPUNIT_ASSERT( dt == dtReal );
721 }
722 else // failed to parse
723 {
724 CPPUNIT_ASSERT( !parseTestDates[n].good );
725 }
726 }
727 }
728
729 void DateTimeTestCase::TestTimeArithmetics()
730 {
731 static const wxDateSpan testArithmData[] =
732 {
733 wxDateSpan::Day(),
734 wxDateSpan::Week(),
735 wxDateSpan::Month(),
736 wxDateSpan::Year(),
737 };
738
739 // the test will *not* work with arbitrary date!
740 wxDateTime dt(2, wxDateTime::Dec, 1999),
741 dt1,
742 dt2;
743
744 for ( size_t n = 0; n < WXSIZEOF(testArithmData); n++ )
745 {
746 const wxDateSpan& span = testArithmData[n];
747 dt1 = dt + span;
748 dt2 = dt - span;
749
750 CPPUNIT_ASSERT( dt1 - span == dt );
751 CPPUNIT_ASSERT( dt2 + span == dt );
752 CPPUNIT_ASSERT( dt2 + 2*span == dt1 );
753 }
754 }
755
756 void DateTimeTestCase::TestDSTBug()
757 {
758 /////////////////////////
759 // Test GetEndDST()
760 wxDateTime dt = wxDateTime::GetEndDST(2004);
761 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
762 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
763 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
764 CPPUNIT_ASSERT_EQUAL(2, (int)dt.GetHour());
765 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
766 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
767 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
768
769 /////////////////////////
770 // Test ResetTime()
771 dt.SetHour(5);
772 CPPUNIT_ASSERT_EQUAL(5, (int)dt.GetHour());
773 dt.ResetTime();
774 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
775 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
776 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
777 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetHour());
778 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
779 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
780 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
781
782 dt.Set(1, 0, 0, 0);
783 CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
784
785 /////////////////////////
786 // Test Today()
787 #ifdef CHANGE_SYSTEM_DATE
788 {
789 DateChanger change(2004, 10, 31, 5, 0, 0);
790 dt = wxDateTime::Today();
791 }
792
793 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
794 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
795 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
796 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetHour());
797 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
798 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
799 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
800
801 /////////////////////////
802 // Test Set(hour, minute, second, milli)
803 wxDateTime dt2;
804 {
805 DateChanger change(2004, 10, 31, 5, 0, 0);
806 dt.Set(1, 30, 0, 0);
807 dt2.Set(5, 30, 0, 0);
808 }
809
810 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
811 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
812 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
813 CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
814 CPPUNIT_ASSERT_EQUAL(30, (int)dt.GetMinute());
815 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
816 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
817
818 CPPUNIT_ASSERT_EQUAL(31, (int)dt2.GetDay());
819 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt2.GetMonth());
820 CPPUNIT_ASSERT_EQUAL(2004, (int)dt2.GetYear());
821 CPPUNIT_ASSERT_EQUAL(5, (int)dt2.GetHour());
822 CPPUNIT_ASSERT_EQUAL(30, (int)dt2.GetMinute());
823 CPPUNIT_ASSERT_EQUAL(0, (int)dt2.GetSecond());
824 CPPUNIT_ASSERT_EQUAL(0, (int)dt2.GetMillisecond());
825 #endif // CHANGE_SYSTEM_DATE
826 }
827
828 #endif // wxUSE_DATETIME