fixes for Italian locale: ignore a final 'CET' string and don't test '%p' for it
[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
652 // skip the check with %p for those locales which have empty AM/PM strings:
653 // for those locales it's impossible to pass the test with %p...
654 wxString am, pm;
655 wxDateTime::GetAmPmStrings(&am, &pm);
656 if (am.empty() && pm.empty() && wxStrstr(fmt, "%p") != NULL)
657 continue;
658
659 wxString s = dt.Format(fmt);
660
661 // what can we recover?
662 CompareKind kind = formatTestFormats[n].compareKind;
663
664 // convert back
665 wxDateTime dt2;
666 const char *result = dt2.ParseFormat(s, fmt);
667 if ( !result )
668 {
669 // conversion failed - should it have?
670 CPPUNIT_ASSERT( kind == CompareNone );
671 }
672 else // conversion succeeded
673 {
674 // ParseFormat() should have parsed the entire string or left
675 // some final useless strings (e.g. with Italian locale the
676 // 's' string for the first test date looks like
677 // "---> sab 29 mag 1976 18:30:00 CET"
678 // so we just need to ignore CET)
679 CPPUNIT_ASSERT( !*result || strcmp(result, "CET") == 0 );
680
681 switch ( kind )
682 {
683 case CompareYear:
684 if ( dt2.GetCentury() != dt.GetCentury() )
685 {
686 CPPUNIT_ASSERT_EQUAL(dt.GetYear() % 100,
687 dt2.GetYear() % 100);
688
689 dt2.SetYear(dt.GetYear());
690 }
691 // fall through and compare everything
692
693 case CompareBoth:
694 CPPUNIT_ASSERT_EQUAL( dt, dt2 );
695 break;
696
697 case CompareDate:
698 CPPUNIT_ASSERT( dt.IsSameDate(dt2) );
699 break;
700
701 case CompareTime:
702 CPPUNIT_ASSERT( dt.IsSameTime(dt2) );
703 break;
704
705 case CompareNone:
706 wxFAIL_MSG( _T("unexpected") );
707 break;
708 }
709 }
710 }
711 }
712
713 wxDateTime dt;
714
715 // test partially specified dates too
716 wxDateTime dtDef(26, wxDateTime::Sep, 2008);
717 CPPUNIT_ASSERT( dt.ParseFormat("17", "%d") );
718 CPPUNIT_ASSERT_EQUAL( 17, dt.GetDay() );
719
720 // test compilation of some calls which should compile (and not result in
721 // ambiguity because of char*<->wxCStrData<->wxString conversions)
722 wxString s("foo");
723 CPPUNIT_ASSERT( !dt.ParseFormat("foo") );
724 CPPUNIT_ASSERT( !dt.ParseFormat(wxT("foo")) );
725 CPPUNIT_ASSERT( !dt.ParseFormat(s) );
726 CPPUNIT_ASSERT( !dt.ParseFormat(s.c_str()) );
727
728 CPPUNIT_ASSERT( !dt.ParseFormat("foo", "%c") );
729 CPPUNIT_ASSERT( !dt.ParseFormat(wxT("foo"), "%c") );
730 CPPUNIT_ASSERT( !dt.ParseFormat(s, "%c") );
731 CPPUNIT_ASSERT( !dt.ParseFormat(s.c_str(), "%c") );
732
733 CPPUNIT_ASSERT( !dt.ParseFormat("foo", wxT("%c")) );
734 CPPUNIT_ASSERT( !dt.ParseFormat(wxT("foo"), wxT("%c")) );
735 CPPUNIT_ASSERT( !dt.ParseFormat(s, "%c") );
736 CPPUNIT_ASSERT( !dt.ParseFormat(s.c_str(), wxT("%c")) );
737
738 wxString spec("%c");
739 CPPUNIT_ASSERT( !dt.ParseFormat("foo", spec) );
740 CPPUNIT_ASSERT( !dt.ParseFormat(wxT("foo"), spec) );
741 CPPUNIT_ASSERT( !dt.ParseFormat(s, spec) );
742 CPPUNIT_ASSERT( !dt.ParseFormat(s.c_str(), spec) );
743 }
744
745 void DateTimeTestCase::TestTimeSpanFormat()
746 {
747 static const struct TimeSpanFormatTestData
748 {
749 long h, min, sec, msec;
750 const char *fmt;
751 const char *result;
752 } testSpans[] =
753 {
754 { 12, 34, 56, 789, "%H:%M:%S.%l", "12:34:56.789" },
755 { 1, 2, 3, 0, "%H:%M:%S", "01:02:03" },
756 { 1, 2, 3, 0, "%S", "3723" },
757 { -1, -2, -3, 0, "%S", "-3723" },
758 { -1, -2, -3, 0, "%H:%M:%S", "-01:02:03" },
759 { 26, 0, 0, 0, "%H", "26" },
760 { 26, 0, 0, 0, "%D, %H", "1, 02" },
761 { -26, 0, 0, 0, "%H", "-26" },
762 { -26, 0, 0, 0, "%D, %H", "-1, 02" },
763 { 219, 0, 0, 0, "%H", "219" },
764 { 219, 0, 0, 0, "%D, %H", "9, 03" },
765 { 219, 0, 0, 0, "%E, %D, %H", "1, 2, 03" },
766 { 0, -1, 0, 0, "%H:%M:%S", "-00:01:00" },
767 { 0, 0, -1, 0, "%H:%M:%S", "-00:00:01" },
768 };
769
770 for ( size_t n = 0; n < WXSIZEOF(testSpans); n++ )
771 {
772 const TimeSpanFormatTestData& td = testSpans[n];
773 wxTimeSpan ts(td.h, td.min, td.sec, td.msec);
774 CPPUNIT_ASSERT_EQUAL( td.result, ts.Format(td.fmt) );
775 }
776 }
777
778 void DateTimeTestCase::TestTimeTicks()
779 {
780 static const wxDateTime::TimeZone TZ_LOCAL(wxDateTime::Local);
781 static const wxDateTime::TimeZone TZ_TEST(wxDateTime::NZST);
782
783 // this offset is needed to make the test work in any time zone when we
784 // only have expected test results in UTC in testDates
785 static const long tzOffset = TZ_LOCAL.GetOffset() - TZ_TEST.GetOffset();
786
787 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
788 {
789 const Date& d = testDates[n];
790 if ( d.gmticks == -1 )
791 continue;
792
793 wxDateTime dt = d.DT().MakeTimezone(TZ_TEST, true /* no DST */);
794
795 // GetValue() returns internal UTC-based representation, we need to
796 // convert it to local TZ before comparing
797 time_t ticks = (dt.GetValue() / 1000).ToLong() + TZ_LOCAL.GetOffset();
798 if ( dt.IsDST() )
799 ticks += 3600;
800 CPPUNIT_ASSERT_EQUAL( d.gmticks, ticks + tzOffset );
801
802 dt = d.DT().FromTimezone(wxDateTime::UTC);
803 ticks = (dt.GetValue() / 1000).ToLong();
804 CPPUNIT_ASSERT_EQUAL( d.gmticks, ticks );
805 }
806 }
807
808 // test parsing dates in RFC822 format
809 void DateTimeTestCase::TestParceRFC822()
810 {
811 static const struct ParseTestData
812 {
813 const char *rfc822;
814 Date date; // NB: this should be in UTC
815 bool good;
816 } parseTestDates[] =
817 {
818 {
819 "Sat, 18 Dec 1999 00:46:40 +0100",
820 { 17, wxDateTime::Dec, 1999, 23, 46, 40 },
821 true
822 },
823 {
824 "Wed, 1 Dec 1999 05:17:20 +0300",
825 { 1, wxDateTime::Dec, 1999, 2, 17, 20 },
826 true
827 },
828 {
829 "Sun, 28 Aug 2005 03:31:30 +0200",
830 { 28, wxDateTime::Aug, 2005, 1, 31, 30 },
831 true
832 },
833
834 {
835 "Sat, 18 Dec 1999 10:48:30 -0500",
836 { 18, wxDateTime::Dec, 1999, 15, 48, 30 },
837 true
838 },
839
840 // seconds are optional according to the RFC
841 {
842 "Sun, 01 Jun 2008 16:30 +0200",
843 { 1, wxDateTime::Jun, 2008, 14, 30, 00 },
844 true
845 },
846
847 // try some bogus ones too
848 {
849 "Sun, 01 Jun 2008 16:30: +0200",
850 { 0 },
851 false
852 },
853 };
854
855 for ( unsigned n = 0; n < WXSIZEOF(parseTestDates); n++ )
856 {
857 const char * const datestr = parseTestDates[n].rfc822;
858
859 wxDateTime dt;
860 if ( dt.ParseRfc822Date(datestr) )
861 {
862 WX_ASSERT_MESSAGE(
863 ("Erroneously parsed \"%s\"", datestr),
864 parseTestDates[n].good
865 );
866
867 wxDateTime dtReal = parseTestDates[n].date.DT().FromUTC();
868 CPPUNIT_ASSERT_EQUAL( dtReal, dt );
869 }
870 else // failed to parse
871 {
872 WX_ASSERT_MESSAGE(
873 ("Failed to parse \"%s\"", datestr),
874 !parseTestDates[n].good
875 );
876 }
877 }
878 }
879
880 // test parsing dates in free format
881 void DateTimeTestCase::TestDateParse()
882 {
883 static const struct ParseTestData
884 {
885 const char *str;
886 Date date; // NB: this should be in UTC
887 bool good;
888 } parseTestDates[] =
889 {
890 { "21 Mar 2006", { 21, wxDateTime::Mar, 2006 }, true },
891 { "29 Feb 1976", { 29, wxDateTime::Feb, 1976 }, true },
892 { "Feb 29 1976", { 29, wxDateTime::Feb, 1976 }, true },
893 { "31/03/06", { 31, wxDateTime::Mar, 6 }, true },
894 { "31/03/2006", { 31, wxDateTime::Mar, 2006 }, true },
895
896 // some invalid ones too
897 { "29 Feb 2006" },
898 { "31/04/06" },
899 { "bloordyblop" },
900 };
901
902 // special cases
903 wxDateTime dt;
904 CPPUNIT_ASSERT( dt.ParseDate(_T("today")) );
905 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), dt );
906
907 for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
908 {
909 if ( dt.ParseDate(parseTestDates[n].str) )
910 {
911 CPPUNIT_ASSERT( parseTestDates[n].good );
912
913 CPPUNIT_ASSERT_EQUAL( parseTestDates[n].date.DT(), dt );
914 }
915 else // failed to parse
916 {
917 CPPUNIT_ASSERT( !parseTestDates[n].good );
918 }
919 }
920 }
921
922 void DateTimeTestCase::TestDateParseISO()
923 {
924 static const struct
925 {
926 const char *str;
927 Date date; // NB: this should be in UTC
928 bool good;
929 } parseTestDates[] =
930 {
931 { "2006-03-21", { 21, wxDateTime::Mar, 2006 }, true },
932 { "1976-02-29", { 29, wxDateTime::Feb, 1976 }, true },
933 { "0006-03-31", { 31, wxDateTime::Mar, 6 }, true },
934
935 // some invalid ones too
936 { "2006:03:31" },
937 { "31/04/06" },
938 { "bloordyblop" },
939 { "" },
940 };
941
942 static const struct
943 {
944 const char *str;
945 wxDateTime::wxDateTime_t hour, min, sec;
946 bool good;
947 } parseTestTimes[] =
948 {
949 { "13:42:17", 13, 42, 17, true },
950 { "02:17:01", 2, 17, 1, true },
951
952 // some invalid ones too
953 { "66:03:31" },
954 { "31/04/06" },
955 { "bloordyblop" },
956 { "" },
957 };
958
959 for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
960 {
961 wxDateTime dt;
962 if ( dt.ParseISODate(parseTestDates[n].str) )
963 {
964 CPPUNIT_ASSERT( parseTestDates[n].good );
965
966 CPPUNIT_ASSERT_EQUAL( parseTestDates[n].date.DT(), dt );
967
968 for ( size_t m = 0; m < WXSIZEOF(parseTestTimes); m++ )
969 {
970 wxString dtCombined;
971 dtCombined << parseTestDates[n].str
972 << 'T'
973 << parseTestTimes[m].str;
974
975 if ( dt.ParseISOCombined(dtCombined) )
976 {
977 CPPUNIT_ASSERT( parseTestTimes[m].good );
978
979 CPPUNIT_ASSERT_EQUAL( parseTestTimes[m].hour, dt.GetHour()) ;
980 CPPUNIT_ASSERT_EQUAL( parseTestTimes[m].min, dt.GetMinute()) ;
981 CPPUNIT_ASSERT_EQUAL( parseTestTimes[m].sec, dt.GetSecond()) ;
982 }
983 else // failed to parse combined date/time
984 {
985 CPPUNIT_ASSERT( !parseTestTimes[m].good );
986 }
987 }
988 }
989 else // failed to parse
990 {
991 CPPUNIT_ASSERT( !parseTestDates[n].good );
992 }
993 }
994 }
995
996 void DateTimeTestCase::TestDateTimeParse()
997 {
998 static const struct ParseTestData
999 {
1000 const char *str;
1001 Date date; // NB: this should be in UTC
1002 bool good;
1003 } parseTestDates[] =
1004 {
1005 { "Thu 22 Nov 2007 07:40:00 PM",
1006 { 22, wxDateTime::Nov, 2007, 19, 40, 0}, true },
1007 };
1008
1009 // special cases
1010 wxDateTime dt;
1011 for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
1012 {
1013 if ( dt.ParseDateTime(parseTestDates[n].str) )
1014 {
1015 CPPUNIT_ASSERT( parseTestDates[n].good );
1016
1017 CPPUNIT_ASSERT_EQUAL( parseTestDates[n].date.DT(), dt );
1018 }
1019 else // failed to parse
1020 {
1021 CPPUNIT_ASSERT( !parseTestDates[n].good );
1022 }
1023 }
1024 }
1025
1026 void DateTimeTestCase::TestTimeArithmetics()
1027 {
1028 static const wxDateSpan testArithmData[] =
1029 {
1030 wxDateSpan::Day(),
1031 wxDateSpan::Week(),
1032 wxDateSpan::Month(),
1033 wxDateSpan::Year(),
1034 };
1035
1036 // the test will *not* work with arbitrary date!
1037 wxDateTime dt(2, wxDateTime::Dec, 1999),
1038 dt1,
1039 dt2;
1040
1041 for ( size_t n = 0; n < WXSIZEOF(testArithmData); n++ )
1042 {
1043 const wxDateSpan& span = testArithmData[n];
1044 dt1 = dt + span;
1045 dt2 = dt - span;
1046
1047 CPPUNIT_ASSERT( dt1 - span == dt );
1048 CPPUNIT_ASSERT( dt2 + span == dt );
1049 CPPUNIT_ASSERT( dt2 + 2*span == dt1 );
1050 }
1051 }
1052
1053 void DateTimeTestCase::TestDSTBug()
1054 {
1055 /////////////////////////
1056 // Test GetEndDST()
1057 wxDateTime dt = wxDateTime::GetEndDST(2004, wxDateTime::France);
1058 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
1059 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
1060 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
1061 CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
1062 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
1063 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
1064 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
1065
1066 /////////////////////////
1067 // Test ResetTime()
1068 dt.SetHour(5);
1069 CPPUNIT_ASSERT_EQUAL(5, (int)dt.GetHour());
1070 dt.ResetTime();
1071 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
1072 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
1073 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
1074 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetHour());
1075 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
1076 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
1077 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
1078
1079 dt.Set(1, 0, 0, 0);
1080 CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
1081
1082 /////////////////////////
1083 // Test Today()
1084 #ifdef CHANGE_SYSTEM_DATE
1085 {
1086 DateChanger change(2004, 10, 31, 5, 0, 0);
1087 dt = wxDateTime::Today();
1088 }
1089
1090 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
1091 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
1092 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
1093 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetHour());
1094 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
1095 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
1096 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
1097
1098 /////////////////////////
1099 // Test Set(hour, minute, second, milli)
1100 wxDateTime dt2;
1101 {
1102 DateChanger change(2004, 10, 31, 5, 0, 0);
1103 dt.Set(1, 30, 0, 0);
1104 dt2.Set(5, 30, 0, 0);
1105 }
1106
1107 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
1108 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
1109 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
1110 CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
1111 CPPUNIT_ASSERT_EQUAL(30, (int)dt.GetMinute());
1112 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
1113 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
1114
1115 CPPUNIT_ASSERT_EQUAL(31, (int)dt2.GetDay());
1116 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt2.GetMonth());
1117 CPPUNIT_ASSERT_EQUAL(2004, (int)dt2.GetYear());
1118 CPPUNIT_ASSERT_EQUAL(5, (int)dt2.GetHour());
1119 CPPUNIT_ASSERT_EQUAL(30, (int)dt2.GetMinute());
1120 CPPUNIT_ASSERT_EQUAL(0, (int)dt2.GetSecond());
1121 CPPUNIT_ASSERT_EQUAL(0, (int)dt2.GetMillisecond());
1122 #endif // CHANGE_SYSTEM_DATE
1123 }
1124
1125 void DateTimeTestCase::TestDateOnly()
1126 {
1127 wxDateTime dt(19, wxDateTime::Jan, 2007, 15, 01, 00);
1128
1129 static const wxDateTime::wxDateTime_t DATE_ZERO = 0;
1130 CPPUNIT_ASSERT_EQUAL( DATE_ZERO, dt.GetDateOnly().GetHour() );
1131 CPPUNIT_ASSERT_EQUAL( DATE_ZERO, dt.GetDateOnly().GetMinute() );
1132 CPPUNIT_ASSERT_EQUAL( DATE_ZERO, dt.GetDateOnly().GetSecond() );
1133 CPPUNIT_ASSERT_EQUAL( DATE_ZERO, dt.GetDateOnly().GetMillisecond() );
1134
1135 dt.ResetTime();
1136 CPPUNIT_ASSERT_EQUAL( wxDateTime(19, wxDateTime::Jan, 2007), dt );
1137
1138 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), wxDateTime::Now().GetDateOnly() );
1139 }
1140
1141 #endif // wxUSE_DATETIME