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