correct wxDateTime DST begin/end computations for years > 2006 in USA (closes #10425)
[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
27 // need this to be able to use CPPUNIT_ASSERT_EQUAL with wxDateTime objects
28 static std::ostream& operator<<(std::ostream& ostr, const wxDateTime& dt)
29 {
30 ostr << dt.FormatISOCombined(' ');
31
32 return ostr;
33 }
34
35 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(wxDateTime::wxDateTime_t)
36
37 // to test Today() meaningfully we must be able to change the system date which
38 // is not usually the case, but if we're under Win32 we can try it -- define
39 // the macro below to do it
40 //#define CHANGE_SYSTEM_DATE
41
42 #ifndef __WINDOWS__
43 #undef CHANGE_SYSTEM_DATE
44 #endif
45
46 #ifdef CHANGE_SYSTEM_DATE
47
48 class DateChanger
49 {
50 public:
51 DateChanger(int year, int month, int day, int hour, int min, int sec)
52 {
53 SYSTEMTIME st;
54 st.wDay = day;
55 st.wMonth = month;
56 st.wYear = year;
57 st.wHour = hour;
58 st.wMinute = min;
59 st.wSecond = sec;
60 st.wMilliseconds = 0;
61
62 ::GetSystemTime(&m_savedTime);
63 ::GetTimeZoneInformation(&m_tzi);
64
65 m_changed = ::SetSystemTime(&st) != 0;
66 }
67
68 ~DateChanger()
69 {
70 if ( m_changed )
71 {
72 ::SetSystemTime(&m_savedTime);
73 ::SetTimeZoneInformation(&m_tzi);
74 }
75 }
76
77 private:
78 SYSTEMTIME m_savedTime;
79 TIME_ZONE_INFORMATION m_tzi;
80 bool m_changed;
81 };
82
83 #endif // CHANGE_SYSTEM_DATE
84
85 // ----------------------------------------------------------------------------
86 // broken down date representation used for testing
87 // ----------------------------------------------------------------------------
88
89 struct Date
90 {
91 wxDateTime::wxDateTime_t day;
92 wxDateTime::Month month;
93 int year;
94 wxDateTime::wxDateTime_t hour, min, sec;
95 double jdn;
96 wxDateTime::WeekDay wday;
97 time_t gmticks;
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 = -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 };
142
143 // ----------------------------------------------------------------------------
144 // test data
145 // ----------------------------------------------------------------------------
146
147 static const Date testDates[] =
148 {
149 { 1, wxDateTime::Jan, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu, 0 },
150 { 7, wxDateTime::Feb, 2036, 00, 00, 00, 2464730.5, wxDateTime::Thu, -1 },
151 { 8, wxDateTime::Feb, 2036, 00, 00, 00, 2464731.5, wxDateTime::Fri, -1 },
152 { 1, wxDateTime::Jan, 2037, 00, 00, 00, 2465059.5, wxDateTime::Thu, -1 },
153 { 1, wxDateTime::Jan, 2038, 00, 00, 00, 2465424.5, wxDateTime::Fri, -1 },
154 { 21, wxDateTime::Jan, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon, -1 },
155 { 29, wxDateTime::May, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat, 202219200 },
156 { 29, wxDateTime::Feb, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun, 194400000 },
157 { 1, wxDateTime::Jan, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon, -1 },
158 { 1, wxDateTime::Jan, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon, -1 },
159 { 15, wxDateTime::Oct, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri, -1 },
160 { 4, wxDateTime::Oct, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon, -1 },
161 { 1, wxDateTime::Mar, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu, -1 },
162 { 1, wxDateTime::Jan, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon, -1 },
163 { 31, wxDateTime::Dec, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun, -1 },
164 { 1, wxDateTime::Jan, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat, -1 },
165 { 12, wxDateTime::Aug, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri, -1 },
166 { 12, wxDateTime::Aug, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat, -1 },
167 { 24, wxDateTime::Nov, -4713, 00, 00, 00, -0.5, wxDateTime::Mon, -1 },
168 };
169
170
171 // ----------------------------------------------------------------------------
172 // test class
173 // ----------------------------------------------------------------------------
174
175 class DateTimeTestCase : public CppUnit::TestCase
176 {
177 public:
178 DateTimeTestCase() { }
179
180 private:
181 CPPUNIT_TEST_SUITE( DateTimeTestCase );
182 CPPUNIT_TEST( TestLeapYears );
183 CPPUNIT_TEST( TestTimeSet );
184 CPPUNIT_TEST( TestTimeJDN );
185 CPPUNIT_TEST( TestTimeWNumber );
186 CPPUNIT_TEST( TestTimeWDays );
187 CPPUNIT_TEST( TestTimeDST );
188 CPPUNIT_TEST( TestTimeFormat );
189 CPPUNIT_TEST( TestTimeSpanFormat );
190 CPPUNIT_TEST( TestTimeTicks );
191 CPPUNIT_TEST( TestParceRFC822 );
192 CPPUNIT_TEST( TestDateParse );
193 CPPUNIT_TEST( TestDateParseISO );
194 CPPUNIT_TEST( TestDateTimeParse );
195 CPPUNIT_TEST( TestTimeArithmetics );
196 CPPUNIT_TEST( TestDSTBug );
197 CPPUNIT_TEST( TestDateOnly );
198 CPPUNIT_TEST_SUITE_END();
199
200 void TestLeapYears();
201 void TestTimeSet();
202 void TestTimeJDN();
203 void TestTimeWNumber();
204 void TestTimeWDays();
205 void TestTimeDST();
206 void TestTimeFormat();
207 void TestTimeSpanFormat();
208 void TestTimeTicks();
209 void TestParceRFC822();
210 void TestDateParse();
211 void TestDateParseISO();
212 void TestDateTimeParse();
213 void TestTimeArithmetics();
214 void TestDSTBug();
215 void TestDateOnly();
216
217 DECLARE_NO_COPY_CLASS(DateTimeTestCase)
218 };
219
220 // register in the unnamed registry so that these tests are run by default
221 CPPUNIT_TEST_SUITE_REGISTRATION( DateTimeTestCase );
222
223 // also include in it's own registry so that these tests can be run alone
224 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DateTimeTestCase, "DateTimeTestCase" );
225
226 // ============================================================================
227 // implementation
228 // ============================================================================
229
230 // test leap years detection
231 void DateTimeTestCase::TestLeapYears()
232 {
233 static const struct LeapYearTestData
234 {
235 int year;
236 bool isLeap;
237 } years[] =
238 {
239 { 1900, false },
240 { 1990, false },
241 { 1976, true },
242 { 2000, true },
243 { 2030, false },
244 { 1984, true },
245 { 2100, false },
246 { 2400, true },
247 };
248
249 for ( size_t n = 0; n < WXSIZEOF(years); n++ )
250 {
251 const LeapYearTestData& y = years[n];
252
253 CPPUNIT_ASSERT( wxDateTime::IsLeapYear(y.year) == y.isLeap );
254 }
255 }
256
257 // test constructing wxDateTime objects
258 void DateTimeTestCase::TestTimeSet()
259 {
260 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
261 {
262 const Date& d1 = testDates[n];
263 wxDateTime dt = d1.DT();
264
265 Date d2;
266 d2.Init(dt.GetTm());
267
268 wxString s1 = d1.Format(),
269 s2 = d2.Format();
270
271 CPPUNIT_ASSERT( s1 == s2 );
272 }
273 }
274
275 // test conversions to JDN &c
276 void DateTimeTestCase::TestTimeJDN()
277 {
278 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
279 {
280 const Date& d = testDates[n];
281 wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
282
283 // JDNs must be computed for UTC times
284 double jdn = dt.FromUTC().GetJulianDayNumber();
285
286 CPPUNIT_ASSERT_EQUAL( d.jdn, jdn );
287
288 dt.Set(jdn);
289 CPPUNIT_ASSERT_EQUAL( jdn, dt.GetJulianDayNumber() );
290 }
291 }
292
293 // test week days computation
294 void DateTimeTestCase::TestTimeWDays()
295 {
296 // test GetWeekDay()
297 size_t n;
298 for ( n = 0; n < WXSIZEOF(testDates); n++ )
299 {
300 const Date& d = testDates[n];
301 wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
302
303 wxDateTime::WeekDay wday = dt.GetWeekDay();
304 CPPUNIT_ASSERT( wday == d.wday );
305 }
306
307 // test SetToWeekDay()
308 struct WeekDateTestData
309 {
310 Date date; // the real date (precomputed)
311 int nWeek; // its week index in the month
312 wxDateTime::WeekDay wday; // the weekday
313 wxDateTime::Month month; // the month
314 int year; // and the year
315
316 wxString Format() const
317 {
318 wxString s, which;
319 switch ( nWeek < -1 ? -nWeek : nWeek )
320 {
321 case 1: which = _T("first"); break;
322 case 2: which = _T("second"); break;
323 case 3: which = _T("third"); break;
324 case 4: which = _T("fourth"); break;
325 case 5: which = _T("fifth"); break;
326
327 case -1: which = _T("last"); break;
328 }
329
330 if ( nWeek < -1 )
331 {
332 which += _T(" from end");
333 }
334
335 s.Printf(_T("The %s %s of %s in %d"),
336 which.c_str(),
337 wxDateTime::GetWeekDayName(wday).c_str(),
338 wxDateTime::GetMonthName(month).c_str(),
339 year);
340
341 return s;
342 }
343 };
344
345 // the array data was generated by the following python program
346 /*
347 from DateTime import *
348 from whrandom import *
349 from string import *
350
351 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
352 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
353
354 week = DateTimeDelta(7)
355
356 for n in range(20):
357 year = randint(1900, 2100)
358 month = randint(1, 12)
359 day = randint(1, 28)
360 dt = DateTime(year, month, day)
361 wday = dt.day_of_week
362
363 countFromEnd = choice([-1, 1])
364 weekNum = 0;
365
366 while dt.month is month:
367 dt = dt - countFromEnd * week
368 weekNum = weekNum + countFromEnd
369
370 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
371
372 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
373 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
374 */
375
376 static const WeekDateTestData weekDatesTestData[] =
377 {
378 { { 20, wxDateTime::Mar, 2045, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 3, wxDateTime::Mon, wxDateTime::Mar, 2045 },
379 { { 5, wxDateTime::Jun, 1985, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -4, wxDateTime::Wed, wxDateTime::Jun, 1985 },
380 { { 12, wxDateTime::Nov, 1961, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -3, wxDateTime::Sun, wxDateTime::Nov, 1961 },
381 { { 27, wxDateTime::Feb, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -1, wxDateTime::Fri, wxDateTime::Feb, 2093 },
382 { { 4, wxDateTime::Jul, 2070, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -4, wxDateTime::Fri, wxDateTime::Jul, 2070 },
383 { { 2, wxDateTime::Apr, 1906, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -5, wxDateTime::Mon, wxDateTime::Apr, 1906 },
384 { { 19, wxDateTime::Jul, 2023, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -2, wxDateTime::Wed, wxDateTime::Jul, 2023 },
385 { { 5, wxDateTime::May, 1958, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -4, wxDateTime::Mon, wxDateTime::May, 1958 },
386 { { 11, wxDateTime::Aug, 1900, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 2, wxDateTime::Sat, wxDateTime::Aug, 1900 },
387 { { 14, wxDateTime::Feb, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 2, wxDateTime::Wed, wxDateTime::Feb, 1945 },
388 { { 25, wxDateTime::Jul, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -1, wxDateTime::Tue, wxDateTime::Jul, 1967 },
389 { { 9, wxDateTime::May, 1916, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -4, wxDateTime::Tue, wxDateTime::May, 1916 },
390 { { 20, wxDateTime::Jun, 1927, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 3, wxDateTime::Mon, wxDateTime::Jun, 1927 },
391 { { 2, wxDateTime::Aug, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, wxDateTime::Wed, wxDateTime::Aug, 2000 },
392 { { 20, wxDateTime::Apr, 2044, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 3, wxDateTime::Wed, wxDateTime::Apr, 2044 },
393 { { 20, wxDateTime::Feb, 1932, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -2, wxDateTime::Sat, wxDateTime::Feb, 1932 },
394 { { 25, wxDateTime::Jul, 2069, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 4, wxDateTime::Thu, wxDateTime::Jul, 2069 },
395 { { 3, wxDateTime::Apr, 1925, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, wxDateTime::Fri, wxDateTime::Apr, 1925 },
396 { { 21, wxDateTime::Mar, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 3, wxDateTime::Sat, wxDateTime::Mar, 2093 },
397 { { 3, wxDateTime::Dec, 2074, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -5, wxDateTime::Mon, wxDateTime::Dec, 2074 }
398 };
399
400 wxDateTime dt;
401 for ( n = 0; n < WXSIZEOF(weekDatesTestData); n++ )
402 {
403 const WeekDateTestData& wd = weekDatesTestData[n];
404
405 dt.SetToWeekDay(wd.wday, wd.nWeek, wd.month, wd.year);
406
407 const Date& d = wd.date;
408 CPPUNIT_ASSERT( d.SameDay(dt.GetTm()) );
409 }
410 }
411
412 // test the computation of (ISO) week numbers
413 void DateTimeTestCase::TestTimeWNumber()
414 {
415 struct WeekNumberTestData
416 {
417 Date date; // the date
418 wxDateTime::wxDateTime_t week; // the week number in the year
419 wxDateTime::wxDateTime_t wmon; // the week number in the month
420 wxDateTime::wxDateTime_t wmon2; // same but week starts with Sun
421 wxDateTime::wxDateTime_t dnum; // day number in the year
422 };
423
424 // data generated with the following python script:
425 /*
426 from DateTime import *
427 from whrandom import *
428 from string import *
429
430 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
431 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
432
433 def GetMonthWeek(dt):
434 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
435 if weekNumMonth < 0:
436 weekNumMonth = weekNumMonth + 53
437 return weekNumMonth
438
439 def GetLastSundayBefore(dt):
440 if dt.iso_week[2] == 7:
441 return dt
442 else:
443 return dt - DateTimeDelta(dt.iso_week[2])
444
445 for n in range(20):
446 year = randint(1900, 2100)
447 month = randint(1, 12)
448 day = randint(1, 28)
449 dt = DateTime(year, month, day)
450 dayNum = dt.day_of_year
451 weekNum = dt.iso_week[1]
452 weekNumMonth = GetMonthWeek(dt)
453
454 weekNumMonth2 = 0
455 dtSunday = GetLastSundayBefore(dt)
456
457 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
458 weekNumMonth2 = weekNumMonth2 + 1
459 dtSunday = dtSunday - DateTimeDelta(7)
460
461 data = { 'day': rjust(`day`, 2), \
462 'month': monthNames[month - 1], \
463 'year': year, \
464 'weekNum': rjust(`weekNum`, 2), \
465 'weekNumMonth': weekNumMonth, \
466 'weekNumMonth2': weekNumMonth2, \
467 'dayNum': rjust(`dayNum`, 3) }
468
469 print " { { %(day)s, "\
470 "wxDateTime::%(month)s, "\
471 "%(year)d }, "\
472 "%(weekNum)s, "\
473 "%(weekNumMonth)s, "\
474 "%(weekNumMonth2)s, "\
475 "%(dayNum)s }," % data
476
477 */
478 static const WeekNumberTestData weekNumberTestDates[] =
479 {
480 { { 27, wxDateTime::Dec, 1966, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 52, 5, 5, 361 },
481 { { 22, wxDateTime::Jul, 1926, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 29, 4, 4, 203 },
482 { { 22, wxDateTime::Oct, 2076, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 43, 4, 4, 296 },
483 { { 1, wxDateTime::Jul, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 26, 1, 1, 182 },
484 { { 8, wxDateTime::Nov, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 46, 2, 2, 313 },
485 { { 21, wxDateTime::Mar, 1920, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 12, 3, 4, 81 },
486 { { 7, wxDateTime::Jan, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 2, 2, 7 },
487 { { 19, wxDateTime::Oct, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 42, 4, 4, 292 },
488 { { 13, wxDateTime::Aug, 1955, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 32, 2, 2, 225 },
489 { { 18, wxDateTime::Jul, 2087, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 29, 3, 3, 199 },
490 { { 2, wxDateTime::Sep, 2028, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 35, 1, 1, 246 },
491 { { 28, wxDateTime::Jul, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 30, 5, 4, 209 },
492 { { 15, wxDateTime::Jun, 1901, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 24, 3, 3, 166 },
493 { { 10, wxDateTime::Oct, 1939, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 41, 3, 2, 283 },
494 { { 3, wxDateTime::Dec, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 48, 1, 1, 337 },
495 { { 23, wxDateTime::Feb, 1940, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 8, 4, 4, 54 },
496 { { 2, wxDateTime::Jan, 1987, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 1, 1, 2 },
497 { { 11, wxDateTime::Aug, 2079, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 32, 2, 2, 223 },
498 { { 2, wxDateTime::Feb, 2063, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 5, 1, 1, 33 },
499 { { 16, wxDateTime::Oct, 1942, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 42, 3, 3, 289 },
500 { { 30, wxDateTime::Dec, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 5, 5, 364 },
501 { { 2, wxDateTime::Jan, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 1, 1, 2 },
502 };
503
504 for ( size_t n = 0; n < WXSIZEOF(weekNumberTestDates); n++ )
505 {
506 const WeekNumberTestData& wn = weekNumberTestDates[n];
507 const Date& d = wn.date;
508
509 wxDateTime dt = d.DT();
510
511 wxDateTime::wxDateTime_t
512 week = dt.GetWeekOfYear(wxDateTime::Monday_First),
513 wmon = dt.GetWeekOfMonth(wxDateTime::Monday_First),
514 wmon2 = dt.GetWeekOfMonth(wxDateTime::Sunday_First),
515 dnum = dt.GetDayOfYear();
516
517 CPPUNIT_ASSERT( dnum == wn.dnum );
518 CPPUNIT_ASSERT( wmon == wn.wmon );
519 CPPUNIT_ASSERT( wmon2 == wn.wmon2 );
520 CPPUNIT_ASSERT( week == wn.week );
521
522 int year = d.year;
523 if ( week == 1 && d.month != wxDateTime::Jan )
524 {
525 // this means we're in the first week of the next year
526 year++;
527 }
528
529 wxDateTime
530 dt2 = wxDateTime::SetToWeekOfYear(year, week, dt.GetWeekDay());
531 CPPUNIT_ASSERT( dt2 == dt );
532 }
533 }
534
535 // test DST applicability
536 void DateTimeTestCase::TestTimeDST()
537 {
538 // taken from http://www.energy.ca.gov/daylightsaving.html
539 static const Date datesDST[2][2009 - 1990 + 1] =
540 {
541 {
542 { 1, wxDateTime::Apr, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
543 { 7, wxDateTime::Apr, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
544 { 5, wxDateTime::Apr, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
545 { 4, wxDateTime::Apr, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
546 { 3, wxDateTime::Apr, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
547 { 2, wxDateTime::Apr, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
548 { 7, wxDateTime::Apr, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
549 { 6, wxDateTime::Apr, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
550 { 5, wxDateTime::Apr, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
551 { 4, wxDateTime::Apr, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
552 { 2, wxDateTime::Apr, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
553 { 1, wxDateTime::Apr, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
554 { 7, wxDateTime::Apr, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
555 { 6, wxDateTime::Apr, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
556 { 4, wxDateTime::Apr, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
557 { 3, wxDateTime::Apr, 2005, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
558 { 2, wxDateTime::Apr, 2006, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
559 {11, wxDateTime::Mar, 2007, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
560 { 9, wxDateTime::Mar, 2008, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
561 { 8, wxDateTime::Mar, 2009, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
562 },
563 {
564 { 28, wxDateTime::Oct, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
565 { 27, wxDateTime::Oct, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
566 { 25, wxDateTime::Oct, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
567 { 31, wxDateTime::Oct, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
568 { 30, wxDateTime::Oct, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
569 { 29, wxDateTime::Oct, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
570 { 27, wxDateTime::Oct, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
571 { 26, wxDateTime::Oct, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
572 { 25, wxDateTime::Oct, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
573 { 31, wxDateTime::Oct, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
574 { 29, wxDateTime::Oct, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
575 { 28, wxDateTime::Oct, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
576 { 27, wxDateTime::Oct, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
577 { 26, wxDateTime::Oct, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
578 { 31, wxDateTime::Oct, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
579 { 30, wxDateTime::Oct, 2005, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
580 { 29, wxDateTime::Oct, 2006, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
581 { 4, wxDateTime::Nov, 2007, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
582 { 2, wxDateTime::Nov, 2008, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
583 { 1, wxDateTime::Nov, 2009, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
584
585 }
586 };
587
588 for ( size_t n = 0; n < WXSIZEOF(datesDST[0]); n++ )
589 {
590 const int year = 1990 + n;
591 wxDateTime dtBegin = wxDateTime::GetBeginDST(year, wxDateTime::USA),
592 dtEnd = wxDateTime::GetEndDST(year, wxDateTime::USA);
593
594 const Date& dBegin = datesDST[0][n];
595 const Date& dEnd = datesDST[1][n];
596
597 CPPUNIT_ASSERT_EQUAL( dBegin.DT().FormatDate(), dtBegin.FormatDate() );
598 CPPUNIT_ASSERT_EQUAL( dEnd.DT().FormatDate(), dtEnd.FormatDate() );
599 }
600 }
601
602 // test wxDateTime -> text conversion
603 void DateTimeTestCase::TestTimeFormat()
604 {
605 // some information may be lost during conversion, so store what kind
606 // of info should we recover after a round trip
607 enum CompareKind
608 {
609 CompareNone, // don't try comparing
610 CompareBoth, // dates and times should be identical
611 CompareYear, // don't compare centuries (fails for 2 digit years)
612 CompareDate, // dates only
613 CompareTime // time only
614 };
615
616 static const struct
617 {
618 CompareKind compareKind;
619 const char *format;
620 } formatTestFormats[] =
621 {
622 { CompareYear, "---> %c" }, // %c could use 2 digit years
623 { CompareDate, "Date is %A, %d of %B, in year %Y" },
624 { CompareYear, "Date is %x, time is %X" }, // %x could use 2 digits
625 { CompareTime, "Time is %H:%M:%S or %I:%M:%S %p" },
626 { CompareNone, "The day of year: %j, the week of year: %W" },
627 { CompareDate, "ISO date without separators: %Y%m%d" },
628 };
629
630 static const Date formatTestDates[] =
631 {
632 { 29, wxDateTime::May, 1976, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
633 { 31, wxDateTime::Dec, 1999, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
634 { 6, wxDateTime::Feb, 1937, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
635 { 6, wxDateTime::Feb, 1856, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
636 { 6, wxDateTime::Feb, 1857, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
637 { 29, wxDateTime::May, 2076, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
638 { 29, wxDateTime::Feb, 2400, 02, 15, 25, 0.0, wxDateTime::Inv_WeekDay },
639 #if 0
640 // Need to add support for BCE dates.
641 { 01, wxDateTime::Jan, -52, 03, 16, 47, 0.0, wxDateTime::Inv_WeekDay },
642 #endif
643 };
644
645 for ( size_t d = 0; d < WXSIZEOF(formatTestDates); d++ )
646 {
647 wxDateTime dt = formatTestDates[d].DT();
648 for ( size_t n = 0; n < WXSIZEOF(formatTestFormats); n++ )
649 {
650 const char *fmt = formatTestFormats[n].format;
651 wxString s = dt.Format(fmt);
652
653 // what can we recover?
654 CompareKind kind = formatTestFormats[n].compareKind;
655
656 // convert back
657 wxDateTime dt2;
658 const char *result = dt2.ParseFormat(s, fmt);
659 if ( !result )
660 {
661 // conversion failed - should it have?
662 CPPUNIT_ASSERT( kind == CompareNone );
663 }
664 else // conversion succeeded
665 {
666 // should have parsed the entire string
667 CPPUNIT_ASSERT( !*result );
668
669 switch ( kind )
670 {
671 case CompareYear:
672 if ( dt2.GetCentury() != dt.GetCentury() )
673 {
674 CPPUNIT_ASSERT_EQUAL(dt.GetYear() % 100,
675 dt2.GetYear() % 100);
676
677 dt2.SetYear(dt.GetYear());
678 }
679 // fall through and compare everything
680
681 case CompareBoth:
682 CPPUNIT_ASSERT_EQUAL( dt, dt2 );
683 break;
684
685 case CompareDate:
686 CPPUNIT_ASSERT( dt.IsSameDate(dt2) );
687 break;
688
689 case CompareTime:
690 CPPUNIT_ASSERT( dt.IsSameTime(dt2) );
691 break;
692
693 case CompareNone:
694 wxFAIL_MSG( _T("unexpected") );
695 break;
696 }
697 }
698 }
699 }
700
701 wxDateTime dt;
702
703 // test partially specified dates too
704 wxDateTime dtDef(26, wxDateTime::Sep, 2008);
705 CPPUNIT_ASSERT( dt.ParseFormat("17", "%d") );
706 CPPUNIT_ASSERT_EQUAL( 17, dt.GetDay() );
707
708 // test compilation of some calls which should compile (and not result in
709 // ambiguity because of char*<->wxCStrData<->wxString conversions)
710 wxString s("foo");
711 CPPUNIT_ASSERT( !dt.ParseFormat("foo") );
712 CPPUNIT_ASSERT( !dt.ParseFormat(wxT("foo")) );
713 CPPUNIT_ASSERT( !dt.ParseFormat(s) );
714 CPPUNIT_ASSERT( !dt.ParseFormat(s.c_str()) );
715
716 CPPUNIT_ASSERT( !dt.ParseFormat("foo", "%c") );
717 CPPUNIT_ASSERT( !dt.ParseFormat(wxT("foo"), "%c") );
718 CPPUNIT_ASSERT( !dt.ParseFormat(s, "%c") );
719 CPPUNIT_ASSERT( !dt.ParseFormat(s.c_str(), "%c") );
720
721 CPPUNIT_ASSERT( !dt.ParseFormat("foo", wxT("%c")) );
722 CPPUNIT_ASSERT( !dt.ParseFormat(wxT("foo"), wxT("%c")) );
723 CPPUNIT_ASSERT( !dt.ParseFormat(s, "%c") );
724 CPPUNIT_ASSERT( !dt.ParseFormat(s.c_str(), wxT("%c")) );
725
726 wxString spec("%c");
727 CPPUNIT_ASSERT( !dt.ParseFormat("foo", spec) );
728 CPPUNIT_ASSERT( !dt.ParseFormat(wxT("foo"), spec) );
729 CPPUNIT_ASSERT( !dt.ParseFormat(s, spec) );
730 CPPUNIT_ASSERT( !dt.ParseFormat(s.c_str(), spec) );
731 }
732
733 void DateTimeTestCase::TestTimeSpanFormat()
734 {
735 static const struct TimeSpanFormatTestData
736 {
737 long h, min, sec, msec;
738 const char *fmt;
739 const char *result;
740 } testSpans[] =
741 {
742 { 12, 34, 56, 789, "%H:%M:%S.%l", "12:34:56.789" },
743 { 1, 2, 3, 0, "%H:%M:%S", "01:02:03" },
744 { 1, 2, 3, 0, "%S", "3723" },
745 { -1, -2, -3, 0, "%S", "-3723" },
746 { -1, -2, -3, 0, "%H:%M:%S", "-01:02:03" },
747 { 26, 0, 0, 0, "%H", "26" },
748 { 26, 0, 0, 0, "%D, %H", "1, 02" },
749 { -26, 0, 0, 0, "%H", "-26" },
750 { -26, 0, 0, 0, "%D, %H", "-1, 02" },
751 { 219, 0, 0, 0, "%H", "219" },
752 { 219, 0, 0, 0, "%D, %H", "9, 03" },
753 { 219, 0, 0, 0, "%E, %D, %H", "1, 2, 03" },
754 { 0, -1, 0, 0, "%H:%M:%S", "-00:01:00" },
755 { 0, 0, -1, 0, "%H:%M:%S", "-00:00:01" },
756 };
757
758 for ( size_t n = 0; n < WXSIZEOF(testSpans); n++ )
759 {
760 const TimeSpanFormatTestData& td = testSpans[n];
761 wxTimeSpan ts(td.h, td.min, td.sec, td.msec);
762 CPPUNIT_ASSERT_EQUAL( td.result, ts.Format(td.fmt) );
763 }
764 }
765
766 void DateTimeTestCase::TestTimeTicks()
767 {
768 static const wxDateTime::TimeZone TZ_LOCAL(wxDateTime::Local);
769 static const wxDateTime::TimeZone TZ_TEST(wxDateTime::NZST);
770
771 // this offset is needed to make the test work in any time zone when we
772 // only have expected test results in UTC in testDates
773 static const long tzOffset = TZ_LOCAL.GetOffset() - TZ_TEST.GetOffset();
774
775 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
776 {
777 const Date& d = testDates[n];
778 if ( d.gmticks == -1 )
779 continue;
780
781 wxDateTime dt = d.DT().MakeTimezone(TZ_TEST, true /* no DST */);
782
783 // GetValue() returns internal UTC-based representation, we need to
784 // convert it to local TZ before comparing
785 time_t ticks = (dt.GetValue() / 1000).ToLong() + TZ_LOCAL.GetOffset();
786 if ( dt.IsDST() )
787 ticks += 3600;
788 CPPUNIT_ASSERT_EQUAL( d.gmticks, ticks + tzOffset );
789
790 dt = d.DT().FromTimezone(wxDateTime::UTC);
791 ticks = (dt.GetValue() / 1000).ToLong();
792 CPPUNIT_ASSERT_EQUAL( d.gmticks, ticks );
793 }
794 }
795
796 // test parsing dates in RFC822 format
797 void DateTimeTestCase::TestParceRFC822()
798 {
799 static const struct ParseTestData
800 {
801 const char *rfc822;
802 Date date; // NB: this should be in UTC
803 bool good;
804 } parseTestDates[] =
805 {
806 {
807 "Sat, 18 Dec 1999 00:46:40 +0100",
808 { 17, wxDateTime::Dec, 1999, 23, 46, 40 },
809 true
810 },
811 {
812 "Wed, 1 Dec 1999 05:17:20 +0300",
813 { 1, wxDateTime::Dec, 1999, 2, 17, 20 },
814 true
815 },
816 {
817 "Sun, 28 Aug 2005 03:31:30 +0200",
818 { 28, wxDateTime::Aug, 2005, 1, 31, 30 },
819 true
820 },
821
822 {
823 "Sat, 18 Dec 1999 10:48:30 -0500",
824 { 18, wxDateTime::Dec, 1999, 15, 48, 30 },
825 true
826 },
827
828 // seconds are optional according to the RFC
829 {
830 "Sun, 01 Jun 2008 16:30 +0200",
831 { 1, wxDateTime::Jun, 2008, 14, 30, 00 },
832 true
833 },
834
835 // try some bogus ones too
836 {
837 "Sun, 01 Jun 2008 16:30: +0200",
838 { 0 },
839 false
840 },
841 };
842
843 for ( unsigned n = 0; n < WXSIZEOF(parseTestDates); n++ )
844 {
845 const char * const datestr = parseTestDates[n].rfc822;
846
847 wxDateTime dt;
848 if ( dt.ParseRfc822Date(datestr) )
849 {
850 WX_ASSERT_MESSAGE(
851 ("Erroneously parsed \"%s\"", datestr),
852 parseTestDates[n].good
853 );
854
855 wxDateTime dtReal = parseTestDates[n].date.DT().FromUTC();
856 CPPUNIT_ASSERT_EQUAL( dtReal, dt );
857 }
858 else // failed to parse
859 {
860 WX_ASSERT_MESSAGE(
861 ("Failed to parse \"%s\"", datestr),
862 !parseTestDates[n].good
863 );
864 }
865 }
866 }
867
868 // test parsing dates in free format
869 void DateTimeTestCase::TestDateParse()
870 {
871 static const struct ParseTestData
872 {
873 const char *str;
874 Date date; // NB: this should be in UTC
875 bool good;
876 } parseTestDates[] =
877 {
878 { "21 Mar 2006", { 21, wxDateTime::Mar, 2006 }, true },
879 { "29 Feb 1976", { 29, wxDateTime::Feb, 1976 }, true },
880 { "Feb 29 1976", { 29, wxDateTime::Feb, 1976 }, true },
881 { "31/03/06", { 31, wxDateTime::Mar, 6 }, true },
882 { "31/03/2006", { 31, wxDateTime::Mar, 2006 }, true },
883
884 // some invalid ones too
885 { "29 Feb 2006" },
886 { "31/04/06" },
887 { "bloordyblop" },
888 };
889
890 // special cases
891 wxDateTime dt;
892 CPPUNIT_ASSERT( dt.ParseDate(_T("today")) );
893 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), dt );
894
895 for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
896 {
897 if ( dt.ParseDate(parseTestDates[n].str) )
898 {
899 CPPUNIT_ASSERT( parseTestDates[n].good );
900
901 CPPUNIT_ASSERT_EQUAL( parseTestDates[n].date.DT(), dt );
902 }
903 else // failed to parse
904 {
905 CPPUNIT_ASSERT( !parseTestDates[n].good );
906 }
907 }
908 }
909
910 void DateTimeTestCase::TestDateParseISO()
911 {
912 static const struct
913 {
914 const char *str;
915 Date date; // NB: this should be in UTC
916 bool good;
917 } parseTestDates[] =
918 {
919 { "2006-03-21", { 21, wxDateTime::Mar, 2006 }, true },
920 { "1976-02-29", { 29, wxDateTime::Feb, 1976 }, true },
921 { "0006-03-31", { 31, wxDateTime::Mar, 6 }, true },
922
923 // some invalid ones too
924 { "2006:03:31" },
925 { "31/04/06" },
926 { "bloordyblop" },
927 { "" },
928 };
929
930 static const struct
931 {
932 const char *str;
933 wxDateTime::wxDateTime_t hour, min, sec;
934 bool good;
935 } parseTestTimes[] =
936 {
937 { "13:42:17", 13, 42, 17, true },
938 { "02:17:01", 2, 17, 1, true },
939
940 // some invalid ones too
941 { "66:03:31" },
942 { "31/04/06" },
943 { "bloordyblop" },
944 { "" },
945 };
946
947 for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
948 {
949 wxDateTime dt;
950 if ( dt.ParseISODate(parseTestDates[n].str) )
951 {
952 CPPUNIT_ASSERT( parseTestDates[n].good );
953
954 CPPUNIT_ASSERT_EQUAL( parseTestDates[n].date.DT(), dt );
955
956 for ( size_t m = 0; m < WXSIZEOF(parseTestTimes); m++ )
957 {
958 wxString dtCombined;
959 dtCombined << parseTestDates[n].str
960 << 'T'
961 << parseTestTimes[m].str;
962
963 if ( dt.ParseISOCombined(dtCombined) )
964 {
965 CPPUNIT_ASSERT( parseTestTimes[m].good );
966
967 CPPUNIT_ASSERT_EQUAL( parseTestTimes[m].hour, dt.GetHour()) ;
968 CPPUNIT_ASSERT_EQUAL( parseTestTimes[m].min, dt.GetMinute()) ;
969 CPPUNIT_ASSERT_EQUAL( parseTestTimes[m].sec, dt.GetSecond()) ;
970 }
971 else // failed to parse combined date/time
972 {
973 CPPUNIT_ASSERT( !parseTestTimes[m].good );
974 }
975 }
976 }
977 else // failed to parse
978 {
979 CPPUNIT_ASSERT( !parseTestDates[n].good );
980 }
981 }
982 }
983
984 void DateTimeTestCase::TestDateTimeParse()
985 {
986 static const struct ParseTestData
987 {
988 const char *str;
989 Date date; // NB: this should be in UTC
990 bool good;
991 } parseTestDates[] =
992 {
993 { "Thu 22 Nov 2007 07:40:00 PM",
994 { 22, wxDateTime::Nov, 2007, 19, 40, 0}, true },
995 };
996
997 // special cases
998 wxDateTime dt;
999 for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
1000 {
1001 if ( dt.ParseDateTime(parseTestDates[n].str) )
1002 {
1003 CPPUNIT_ASSERT( parseTestDates[n].good );
1004
1005 CPPUNIT_ASSERT_EQUAL( parseTestDates[n].date.DT(), dt );
1006 }
1007 else // failed to parse
1008 {
1009 CPPUNIT_ASSERT( !parseTestDates[n].good );
1010 }
1011 }
1012 }
1013
1014 void DateTimeTestCase::TestTimeArithmetics()
1015 {
1016 static const wxDateSpan testArithmData[] =
1017 {
1018 wxDateSpan::Day(),
1019 wxDateSpan::Week(),
1020 wxDateSpan::Month(),
1021 wxDateSpan::Year(),
1022 };
1023
1024 // the test will *not* work with arbitrary date!
1025 wxDateTime dt(2, wxDateTime::Dec, 1999),
1026 dt1,
1027 dt2;
1028
1029 for ( size_t n = 0; n < WXSIZEOF(testArithmData); n++ )
1030 {
1031 const wxDateSpan& span = testArithmData[n];
1032 dt1 = dt + span;
1033 dt2 = dt - span;
1034
1035 CPPUNIT_ASSERT( dt1 - span == dt );
1036 CPPUNIT_ASSERT( dt2 + span == dt );
1037 CPPUNIT_ASSERT( dt2 + 2*span == dt1 );
1038 }
1039 }
1040
1041 void DateTimeTestCase::TestDSTBug()
1042 {
1043 /////////////////////////
1044 // Test GetEndDST()
1045 wxDateTime dt = wxDateTime::GetEndDST(2004, wxDateTime::France);
1046 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
1047 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
1048 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
1049 CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
1050 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
1051 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
1052 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
1053
1054 /////////////////////////
1055 // Test ResetTime()
1056 dt.SetHour(5);
1057 CPPUNIT_ASSERT_EQUAL(5, (int)dt.GetHour());
1058 dt.ResetTime();
1059 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
1060 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
1061 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
1062 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetHour());
1063 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
1064 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
1065 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
1066
1067 dt.Set(1, 0, 0, 0);
1068 CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
1069
1070 /////////////////////////
1071 // Test Today()
1072 #ifdef CHANGE_SYSTEM_DATE
1073 {
1074 DateChanger change(2004, 10, 31, 5, 0, 0);
1075 dt = wxDateTime::Today();
1076 }
1077
1078 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
1079 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
1080 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
1081 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetHour());
1082 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
1083 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
1084 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
1085
1086 /////////////////////////
1087 // Test Set(hour, minute, second, milli)
1088 wxDateTime dt2;
1089 {
1090 DateChanger change(2004, 10, 31, 5, 0, 0);
1091 dt.Set(1, 30, 0, 0);
1092 dt2.Set(5, 30, 0, 0);
1093 }
1094
1095 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
1096 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
1097 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
1098 CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
1099 CPPUNIT_ASSERT_EQUAL(30, (int)dt.GetMinute());
1100 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
1101 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
1102
1103 CPPUNIT_ASSERT_EQUAL(31, (int)dt2.GetDay());
1104 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt2.GetMonth());
1105 CPPUNIT_ASSERT_EQUAL(2004, (int)dt2.GetYear());
1106 CPPUNIT_ASSERT_EQUAL(5, (int)dt2.GetHour());
1107 CPPUNIT_ASSERT_EQUAL(30, (int)dt2.GetMinute());
1108 CPPUNIT_ASSERT_EQUAL(0, (int)dt2.GetSecond());
1109 CPPUNIT_ASSERT_EQUAL(0, (int)dt2.GetMillisecond());
1110 #endif // CHANGE_SYSTEM_DATE
1111 }
1112
1113 void DateTimeTestCase::TestDateOnly()
1114 {
1115 wxDateTime dt(19, wxDateTime::Jan, 2007, 15, 01, 00);
1116
1117 static const wxDateTime::wxDateTime_t DATE_ZERO = 0;
1118 CPPUNIT_ASSERT_EQUAL( DATE_ZERO, dt.GetDateOnly().GetHour() );
1119 CPPUNIT_ASSERT_EQUAL( DATE_ZERO, dt.GetDateOnly().GetMinute() );
1120 CPPUNIT_ASSERT_EQUAL( DATE_ZERO, dt.GetDateOnly().GetSecond() );
1121 CPPUNIT_ASSERT_EQUAL( DATE_ZERO, dt.GetDateOnly().GetMillisecond() );
1122
1123 dt.ResetTime();
1124 CPPUNIT_ASSERT_EQUAL( wxDateTime(19, wxDateTime::Jan, 2007), dt );
1125
1126 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), wxDateTime::Now().GetDateOnly() );
1127 }
1128
1129 #endif // wxUSE_DATETIME