]> git.saurik.com Git - wxWidgets.git/blob - tests/datetime/datetimetest.cpp
replacing old core graphics with new graphics context implementation
[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.FormatISODate() << _T(" ") << dt.FormatISOTime();
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
98 void Init(const wxDateTime::Tm& tm)
99 {
100 day = tm.mday;
101 month = tm.mon;
102 year = tm.year;
103 hour = tm.hour;
104 min = tm.min;
105 sec = tm.sec;
106 jdn = 0.0;
107 gmticks = ticks = -1;
108 }
109
110 wxDateTime DT() const
111 { return wxDateTime(day, month, year, hour, min, sec); }
112
113 bool SameDay(const wxDateTime::Tm& tm) const
114 {
115 return day == tm.mday && month == tm.mon && year == tm.year;
116 }
117
118 wxString Format() const
119 {
120 wxString s;
121 s.Printf(_T("%02d:%02d:%02d %10s %02d, %4d%s"),
122 hour, min, sec,
123 wxDateTime::GetMonthName(month).c_str(),
124 day,
125 abs(wxDateTime::ConvertYearToBC(year)),
126 year > 0 ? _T("AD") : _T("BC"));
127 return s;
128 }
129
130 wxString FormatDate() const
131 {
132 wxString s;
133 s.Printf(_T("%02d-%s-%4d%s"),
134 day,
135 wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(),
136 abs(wxDateTime::ConvertYearToBC(year)),
137 year > 0 ? _T("AD") : _T("BC"));
138 return s;
139 }
140 };
141
142 // ----------------------------------------------------------------------------
143 // test data
144 // ----------------------------------------------------------------------------
145
146 static const Date testDates[] =
147 {
148 { 1, wxDateTime::Jan, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu, 0, -3600 },
149 { 7, wxDateTime::Feb, 2036, 00, 00, 00, 2464730.5, wxDateTime::Thu, -1, -1 },
150 { 8, wxDateTime::Feb, 2036, 00, 00, 00, 2464731.5, wxDateTime::Fri, -1, -1 },
151 { 1, wxDateTime::Jan, 2037, 00, 00, 00, 2465059.5, wxDateTime::Thu, -1, -1 },
152 { 1, wxDateTime::Jan, 2038, 00, 00, 00, 2465424.5, wxDateTime::Fri, -1, -1 },
153 { 21, wxDateTime::Jan, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon, -1, -1 },
154 { 29, wxDateTime::May, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat, 202219200, 202212000 },
155 { 29, wxDateTime::Feb, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun, 194400000, 194396400 },
156 { 1, wxDateTime::Jan, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon, -1, -1 },
157 { 1, wxDateTime::Jan, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon, -1, -1 },
158 { 15, wxDateTime::Oct, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri, -1, -1 },
159 { 4, wxDateTime::Oct, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon, -1, -1 },
160 { 1, wxDateTime::Mar, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu, -1, -1 },
161 { 1, wxDateTime::Jan, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon, -1, -1 },
162 { 31, wxDateTime::Dec, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun, -1, -1 },
163 { 1, wxDateTime::Jan, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat, -1, -1 },
164 { 12, wxDateTime::Aug, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri, -1, -1 },
165 { 12, wxDateTime::Aug, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat, -1, -1 },
166 { 24, wxDateTime::Nov, -4713, 00, 00, 00, -0.5, wxDateTime::Mon, -1, -1 },
167 };
168
169
170 // ----------------------------------------------------------------------------
171 // test class
172 // ----------------------------------------------------------------------------
173
174 class DateTimeTestCase : public CppUnit::TestCase
175 {
176 public:
177 DateTimeTestCase() { }
178
179 private:
180 CPPUNIT_TEST_SUITE( DateTimeTestCase );
181 CPPUNIT_TEST( TestLeapYears );
182 CPPUNIT_TEST( TestTimeSet );
183 CPPUNIT_TEST( TestTimeJDN );
184 CPPUNIT_TEST( TestTimeWNumber );
185 CPPUNIT_TEST( TestTimeWDays );
186 CPPUNIT_TEST( TestTimeDST );
187 CPPUNIT_TEST( TestTimeFormat );
188 CPPUNIT_TEST( TestTimeSpanFormat );
189 CPPUNIT_TEST( TestTimeTicks );
190 CPPUNIT_TEST( TestParceRFC822 );
191 CPPUNIT_TEST( TestDateParse );
192 CPPUNIT_TEST( TestTimeArithmetics );
193 CPPUNIT_TEST( TestDSTBug );
194 CPPUNIT_TEST_SUITE_END();
195
196 void TestLeapYears();
197 void TestTimeSet();
198 void TestTimeJDN();
199 void TestTimeWNumber();
200 void TestTimeWDays();
201 void TestTimeDST();
202 void TestTimeFormat();
203 void TestTimeSpanFormat();
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 CompareYear, // don't compare centuries (fails for 2 digit years)
594 CompareDate, // dates only
595 CompareTime // time only
596 };
597
598 static const struct
599 {
600 CompareKind compareKind;
601 const wxChar *format;
602 } formatTestFormats[] =
603 {
604 { CompareYear, _T("---> %c") }, // %c could use 2 digit years
605 { CompareDate, _T("Date is %A, %d of %B, in year %Y") },
606 { CompareYear, _T("Date is %x, time is %X") }, // %x could use 2 digits
607 { CompareTime, _T("Time is %H:%M:%S or %I:%M:%S %p") },
608 { CompareNone, _T("The day of year: %j, the week of year: %W") },
609 { CompareDate, _T("ISO date without separators: %Y%m%d") },
610 };
611
612 static const Date formatTestDates[] =
613 {
614 { 29, wxDateTime::May, 1976, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
615 { 31, wxDateTime::Dec, 1999, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
616 { 6, wxDateTime::Feb, 1937, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
617 { 6, wxDateTime::Feb, 1856, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
618 { 6, wxDateTime::Feb, 1857, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
619 { 29, wxDateTime::May, 2076, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
620 { 29, wxDateTime::Feb, 2400, 02, 15, 25, 0.0, wxDateTime::Inv_WeekDay },
621 #if 0
622 // Need to add support for BCE dates.
623 { 01, wxDateTime::Jan, -52, 03, 16, 47, 0.0, wxDateTime::Inv_WeekDay },
624 #endif
625 };
626
627 for ( size_t d = 0; d < WXSIZEOF(formatTestDates); d++ )
628 {
629 wxDateTime dt = formatTestDates[d].DT();
630 for ( size_t n = 0; n < WXSIZEOF(formatTestFormats); n++ )
631 {
632 const wxChar *fmt = formatTestFormats[n].format;
633 wxString s = dt.Format(fmt);
634
635 // what can we recover?
636 CompareKind kind = formatTestFormats[n].compareKind;
637
638 // convert back
639 wxDateTime dt2;
640 const wxChar *result = dt2.ParseFormat(s, fmt);
641 if ( !result )
642 {
643 // converion failed - should it have?
644 CPPUNIT_ASSERT( kind == CompareNone );
645 }
646 else // conversion succeeded
647 {
648 // should have parsed the entire string
649 CPPUNIT_ASSERT( !*result );
650
651 switch ( kind )
652 {
653 case CompareYear:
654 if ( dt2.GetCentury() != dt.GetCentury() )
655 {
656 CPPUNIT_ASSERT_EQUAL(dt.GetYear() % 100,
657 dt2.GetYear() % 100);
658
659 dt2.SetYear(dt.GetYear());
660 }
661 // fall through and compare everything
662
663 case CompareBoth:
664 CPPUNIT_ASSERT_EQUAL( dt, dt2 );
665 break;
666
667 case CompareDate:
668 CPPUNIT_ASSERT( dt.IsSameDate(dt2) );
669 break;
670
671 case CompareTime:
672 CPPUNIT_ASSERT( dt.IsSameTime(dt2) );
673 break;
674 }
675 }
676 }
677 }
678 }
679
680 void DateTimeTestCase::TestTimeSpanFormat()
681 {
682 static const struct TimeSpanFormatTestData
683 {
684 long h, min, sec, msec;
685 const wxChar *fmt;
686 const wxChar *result;
687 } testSpans[] =
688 {
689 { 12, 34, 56, 789, _T("%H:%M:%S.%l"), _T("12:34:56.789") },
690 { 1, 2, 3, 0, _T("%H:%M:%S"), _T("01:02:03") },
691 { 1, 2, 3, 0, _T("%S"), _T("3723") },
692 { -1, -2, -3, 0, _T("%S"), _T("-3723") },
693 { -1, -2, -3, 0, _T("%H:%M:%S"), _T("-01:02:03") },
694 { 26, 0, 0, 0, _T("%H"), _T("26") },
695 { 26, 0, 0, 0, _T("%D, %H"), _T("1, 02") },
696 { -26, 0, 0, 0, _T("%H"), _T("-26") },
697 { -26, 0, 0, 0, _T("%D, %H"), _T("-1, 02") },
698 { 219, 0, 0, 0, _T("%H"), _T("219") },
699 { 219, 0, 0, 0, _T("%D, %H"), _T("9, 03") },
700 { 219, 0, 0, 0, _T("%E, %D, %H"), _T("1, 2, 03") },
701 };
702
703 for ( size_t n = 0; n < WXSIZEOF(testSpans); n++ )
704 {
705 const TimeSpanFormatTestData& td = testSpans[n];
706 wxTimeSpan ts(td.h, td.min, td.sec, td.msec);
707 CPPUNIT_ASSERT_EQUAL( wxString(td.result), ts.Format(td.fmt) );
708 }
709 }
710
711 void DateTimeTestCase::TestTimeTicks()
712 {
713 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
714 {
715 const Date& d = testDates[n];
716 if ( d.ticks == -1 )
717 continue;
718
719 wxDateTime dt = d.DT();
720 //RN: Translate according to test's time zone
721 //2nd param is to ignore DST - it's already factored
722 //into Vadim's tests
723 dt.MakeTimezone(wxDateTime::WEST, true);
724 long ticks = (dt.GetValue() / 1000).ToLong();
725 CPPUNIT_ASSERT( ticks == d.ticks );
726
727 dt = d.DT().FromTimezone(wxDateTime::GMT0);
728 ticks = (dt.GetValue() / 1000).ToLong();
729 CPPUNIT_ASSERT( ticks == d.gmticks );
730 }
731 }
732
733 // test parsing dates in RFC822 format
734 void DateTimeTestCase::TestParceRFC822()
735 {
736 static const struct ParseTestData
737 {
738 const wxChar *rfc822;
739 Date date; // NB: this should be in UTC
740 bool good;
741 } parseTestDates[] =
742 {
743 {
744 _T("Sat, 18 Dec 1999 00:46:40 +0100"),
745 { 17, wxDateTime::Dec, 1999, 23, 46, 40, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
746 true
747 },
748 {
749 _T("Wed, 1 Dec 1999 05:17:20 +0300"),
750 { 1, wxDateTime::Dec, 1999, 2, 17, 20, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
751 true
752 },
753 {
754 _T("Sun, 28 Aug 2005 03:31:30 +0200"),
755 { 28, wxDateTime::Aug, 2005, 1, 31, 30, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
756 true
757 },
758 };
759
760 for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
761 {
762 wxDateTime dt;
763 if ( dt.ParseRfc822Date(parseTestDates[n].rfc822) )
764 {
765 CPPUNIT_ASSERT( parseTestDates[n].good );
766
767 wxDateTime dtReal = parseTestDates[n].date.DT().FromUTC();
768 CPPUNIT_ASSERT_EQUAL( dtReal, dt );
769 }
770 else // failed to parse
771 {
772 CPPUNIT_ASSERT( !parseTestDates[n].good );
773 }
774 }
775 }
776
777 // test parsing dates in free format
778 void DateTimeTestCase::TestDateParse()
779 {
780 static const struct ParseTestData
781 {
782 const wxChar *str;
783 Date date; // NB: this should be in UTC
784 bool good;
785 } parseTestDates[] =
786 {
787 { _T("21 Mar 2006"), { 21, wxDateTime::Mar, 2006 }, true },
788 { _T("29 Feb 1976"), { 29, wxDateTime::Feb, 1976 }, true },
789 { _T("Feb 29 1976"), { 29, wxDateTime::Feb, 1976 }, true },
790 { _T("31/03/06"), { 31, wxDateTime::Mar, 6 }, true },
791 { _T("31/03/2006"), { 31, wxDateTime::Mar, 2006 }, true },
792
793 // some invalid ones too
794 { _T("29 Feb 2006") },
795 { _T("31/04/06") },
796 { _T("bloordyblop") }
797 };
798
799 // special cases
800 wxDateTime dt;
801 CPPUNIT_ASSERT( dt.ParseDate(_T("today")) );
802 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), dt );
803
804 for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
805 {
806 wxDateTime dt;
807 if ( dt.ParseDate(parseTestDates[n].str) )
808 {
809 CPPUNIT_ASSERT( parseTestDates[n].good );
810
811 CPPUNIT_ASSERT_EQUAL( parseTestDates[n].date.DT(), dt );
812 }
813 else // failed to parse
814 {
815 CPPUNIT_ASSERT( !parseTestDates[n].good );
816 }
817 }
818 }
819
820 void DateTimeTestCase::TestTimeArithmetics()
821 {
822 static const wxDateSpan testArithmData[] =
823 {
824 wxDateSpan::Day(),
825 wxDateSpan::Week(),
826 wxDateSpan::Month(),
827 wxDateSpan::Year(),
828 };
829
830 // the test will *not* work with arbitrary date!
831 wxDateTime dt(2, wxDateTime::Dec, 1999),
832 dt1,
833 dt2;
834
835 for ( size_t n = 0; n < WXSIZEOF(testArithmData); n++ )
836 {
837 const wxDateSpan& span = testArithmData[n];
838 dt1 = dt + span;
839 dt2 = dt - span;
840
841 CPPUNIT_ASSERT( dt1 - span == dt );
842 CPPUNIT_ASSERT( dt2 + span == dt );
843 CPPUNIT_ASSERT( dt2 + 2*span == dt1 );
844 }
845 }
846
847 void DateTimeTestCase::TestDSTBug()
848 {
849 /////////////////////////
850 // Test GetEndDST()
851 wxDateTime dt = wxDateTime::GetEndDST(2004);
852 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
853 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
854 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
855 CPPUNIT_ASSERT_EQUAL(2, (int)dt.GetHour());
856 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
857 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
858 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
859
860 /////////////////////////
861 // Test ResetTime()
862 dt.SetHour(5);
863 CPPUNIT_ASSERT_EQUAL(5, (int)dt.GetHour());
864 dt.ResetTime();
865 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
866 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
867 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
868 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetHour());
869 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
870 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
871 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
872
873 dt.Set(1, 0, 0, 0);
874 CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
875
876 /////////////////////////
877 // Test Today()
878 #ifdef CHANGE_SYSTEM_DATE
879 {
880 DateChanger change(2004, 10, 31, 5, 0, 0);
881 dt = wxDateTime::Today();
882 }
883
884 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
885 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
886 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
887 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetHour());
888 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
889 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
890 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
891
892 /////////////////////////
893 // Test Set(hour, minute, second, milli)
894 wxDateTime dt2;
895 {
896 DateChanger change(2004, 10, 31, 5, 0, 0);
897 dt.Set(1, 30, 0, 0);
898 dt2.Set(5, 30, 0, 0);
899 }
900
901 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
902 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
903 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
904 CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
905 CPPUNIT_ASSERT_EQUAL(30, (int)dt.GetMinute());
906 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
907 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
908
909 CPPUNIT_ASSERT_EQUAL(31, (int)dt2.GetDay());
910 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt2.GetMonth());
911 CPPUNIT_ASSERT_EQUAL(2004, (int)dt2.GetYear());
912 CPPUNIT_ASSERT_EQUAL(5, (int)dt2.GetHour());
913 CPPUNIT_ASSERT_EQUAL(30, (int)dt2.GetMinute());
914 CPPUNIT_ASSERT_EQUAL(0, (int)dt2.GetSecond());
915 CPPUNIT_ASSERT_EQUAL(0, (int)dt2.GetMillisecond());
916 #endif // CHANGE_SYSTEM_DATE
917 }
918
919 #endif // wxUSE_DATETIME