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