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