Implement support for "%z" in wxDateTime::Format() and Parse().
[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 { CompareBoth, "RFC 2822 string: %Y-%m-%d %H:%M:%S.%l %z" },
670
671 };
672
673 const long timeZonesOffsets[] =
674 {
675 wxDateTime::TimeZone(wxDateTime::Local).GetOffset(),
676
677 // Fictitious TimeZone offsets to ensure time zone formating and
678 // interpretation works
679 -(3600 + 2*60),
680 3*3600 + 30*60
681 };
682
683 static const Date formatTestDates[] =
684 {
685 { 29, wxDateTime::May, 1976, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
686 { 31, wxDateTime::Dec, 1999, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
687 { 6, wxDateTime::Feb, 1937, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
688 { 6, wxDateTime::Feb, 1856, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
689 { 6, wxDateTime::Feb, 1857, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
690 { 29, wxDateTime::May, 2076, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
691
692 // FIXME: the test with 02:15:25 time doesn't pass because of DST
693 // computation problems, we get back 03:15:25
694 { 29, wxDateTime::Feb, 2400, 04, 15, 25, 0.0, wxDateTime::Inv_WeekDay },
695 #if 0
696 // Need to add support for BCE dates.
697 { 01, wxDateTime::Jan, -52, 03, 16, 47, 0.0, wxDateTime::Inv_WeekDay },
698 #endif
699 };
700
701 for ( unsigned idxtz = 0; idxtz < WXSIZEOF(timeZonesOffsets); ++idxtz )
702 {
703 wxDateTime::TimeZone tz(timeZonesOffsets[idxtz]);
704 const bool isLocalTz = tz.GetOffset() == -wxGetTimeZone();
705
706 for ( size_t d = 0; d < WXSIZEOF(formatTestDates); d++ )
707 {
708 wxDateTime dt = formatTestDates[d].DT();
709 for ( unsigned n = 0; n < WXSIZEOF(formatTestFormats); n++ )
710 {
711 const char *fmt = formatTestFormats[n].format;
712
713 // skip the check with %p for those locales which have empty AM/PM strings:
714 // for those locales it's impossible to pass the test with %p...
715 wxString am, pm;
716 wxDateTime::GetAmPmStrings(&am, &pm);
717 if (am.empty() && pm.empty() && wxStrstr(fmt, "%p") != NULL)
718 continue;
719
720 // what can we recover?
721 CompareKind kind = formatTestFormats[n].compareKind;
722
723 // When using a different time zone we must perform a time zone
724 // conversion below which doesn't always work correctly, check
725 // for the cases when it doesn't.
726 if ( !isLocalTz )
727 {
728 // DST computation doesn't work correctly for dates above
729 // 2038 currently on the systems with 32 bit time_t.
730 if ( dt.GetYear() >= 2038 )
731 continue;
732
733 // We can't compare just dates nor just times when doing TZ
734 // conversion as both are affected by the DST: for the
735 // dates, the DST can switch midnight to 23:00 of the
736 // previous day while for the times DST can be different
737 // for the original date and today.
738 if ( kind == CompareDate || kind == CompareTime )
739 continue;
740 }
741
742 // do convert date to string
743 wxString s = dt.Format(fmt, tz);
744
745 // convert back
746 wxDateTime dt2;
747 const char *result = dt2.ParseFormat(s, fmt);
748 if ( !result )
749 {
750 // conversion failed - should it have?
751 WX_ASSERT_MESSAGE(
752 ("Test #%u failed: failed to parse \"%s\"", n, s),
753 kind == CompareNone
754 );
755 }
756 else // conversion succeeded
757 {
758 // currently ParseFormat() doesn't support "%Z" and so is
759 // incapable of parsing time zone part used at the end of date
760 // representations in many (but not "C") locales, compensate
761 // for it ourselves by simply consuming and ignoring it
762 while ( *result && (*result >= 'A' && *result <= 'Z') )
763 result++;
764
765 WX_ASSERT_MESSAGE(
766 ("Test #%u failed: \"%s\" was left unparsed in \"%s\"",
767 n, result, s),
768 !*result
769 );
770
771 // Without "%z" we can't recover the time zone used in the
772 // call to Format() so we need to call MakeFromTimezone()
773 // explicitly.
774 if ( !strstr(fmt, "%z") && !isLocalTz )
775 dt2.MakeFromTimezone(tz);
776
777 switch ( kind )
778 {
779 case CompareYear:
780 if ( dt2.GetCentury() != dt.GetCentury() )
781 {
782 CPPUNIT_ASSERT_EQUAL(dt.GetYear() % 100,
783 dt2.GetYear() % 100);
784
785 dt2.SetYear(dt.GetYear());
786 }
787 // fall through and compare everything
788
789 case CompareBoth:
790 CPPUNIT_ASSERT_EQUAL( dt, dt2 );
791 break;
792
793 case CompareDate:
794 CPPUNIT_ASSERT( dt.IsSameDate(dt2) );
795 break;
796
797 case CompareTime:
798 CPPUNIT_ASSERT( dt.IsSameTime(dt2) );
799 break;
800
801 case CompareNone:
802 wxFAIL_MSG( wxT("unexpected") );
803 break;
804 }
805 }
806 }
807 }
808 }
809
810 wxDateTime dt;
811
812 #if 0
813 // special case which was known to fail
814 CPPUNIT_ASSERT( dt.ParseFormat("02/06/1856", "%x") );
815 CPPUNIT_ASSERT_EQUAL( 1856, dt.GetYear() );
816 #endif
817
818 // also test %l separately
819 CPPUNIT_ASSERT( dt.ParseFormat("12:23:45.678", "%H:%M:%S.%l") );
820 CPPUNIT_ASSERT_EQUAL( 678, dt.GetMillisecond() );
821
822 // test special case of %l matching 0 milliseconds
823 CPPUNIT_ASSERT( dt.ParseFormat("12:23:45.000", "%H:%M:%S.%l") );
824 CPPUNIT_ASSERT_EQUAL( 0, dt.GetMillisecond() );
825
826 // test partially specified dates too
827 wxDateTime dtDef(26, wxDateTime::Sep, 2008);
828 CPPUNIT_ASSERT( dt.ParseFormat("17", "%d") );
829 CPPUNIT_ASSERT_EQUAL( 17, dt.GetDay() );
830
831 // test compilation of some calls which should compile (and not result in
832 // ambiguity because of char*<->wxCStrData<->wxString conversions)
833 wxString s("foo");
834 CPPUNIT_ASSERT( !dt.ParseFormat("foo") );
835 CPPUNIT_ASSERT( !dt.ParseFormat(wxT("foo")) );
836 CPPUNIT_ASSERT( !dt.ParseFormat(s) );
837 CPPUNIT_ASSERT( !dt.ParseFormat(s.c_str()) );
838
839 CPPUNIT_ASSERT( !dt.ParseFormat("foo", "%c") );
840 CPPUNIT_ASSERT( !dt.ParseFormat(wxT("foo"), "%c") );
841 CPPUNIT_ASSERT( !dt.ParseFormat(s, "%c") );
842 CPPUNIT_ASSERT( !dt.ParseFormat(s.c_str(), "%c") );
843
844 CPPUNIT_ASSERT( !dt.ParseFormat("foo", wxT("%c")) );
845 CPPUNIT_ASSERT( !dt.ParseFormat(wxT("foo"), wxT("%c")) );
846 CPPUNIT_ASSERT( !dt.ParseFormat(s, "%c") );
847 CPPUNIT_ASSERT( !dt.ParseFormat(s.c_str(), wxT("%c")) );
848
849 wxString spec("%c");
850 CPPUNIT_ASSERT( !dt.ParseFormat("foo", spec) );
851 CPPUNIT_ASSERT( !dt.ParseFormat(wxT("foo"), spec) );
852 CPPUNIT_ASSERT( !dt.ParseFormat(s, spec) );
853 CPPUNIT_ASSERT( !dt.ParseFormat(s.c_str(), spec) );
854 }
855
856 void DateTimeTestCase::TestTimeSpanFormat()
857 {
858 static const struct TimeSpanFormatTestData
859 {
860 long h, min, sec, msec;
861 const char *fmt;
862 const char *result;
863 } testSpans[] =
864 {
865 { 12, 34, 56, 789, "%H:%M:%S.%l", "12:34:56.789" },
866 { 1, 2, 3, 0, "%H:%M:%S", "01:02:03" },
867 { 1, 2, 3, 0, "%S", "3723" },
868 { -1, -2, -3, 0, "%S", "-3723" },
869 { -1, -2, -3, 0, "%H:%M:%S", "-01:02:03" },
870 { 26, 0, 0, 0, "%H", "26" },
871 { 26, 0, 0, 0, "%D, %H", "1, 02" },
872 { -26, 0, 0, 0, "%H", "-26" },
873 { -26, 0, 0, 0, "%D, %H", "-1, 02" },
874 { 219, 0, 0, 0, "%H", "219" },
875 { 219, 0, 0, 0, "%D, %H", "9, 03" },
876 { 219, 0, 0, 0, "%E, %D, %H", "1, 2, 03" },
877 { 0, -1, 0, 0, "%H:%M:%S", "-00:01:00" },
878 { 0, 0, -1, 0, "%H:%M:%S", "-00:00:01" },
879 };
880
881 for ( size_t n = 0; n < WXSIZEOF(testSpans); n++ )
882 {
883 const TimeSpanFormatTestData& td = testSpans[n];
884 wxTimeSpan ts(td.h, td.min, td.sec, td.msec);
885 CPPUNIT_ASSERT_EQUAL( td.result, ts.Format(td.fmt) );
886 }
887 }
888
889 void DateTimeTestCase::TestTimeTicks()
890 {
891 static const wxDateTime::TimeZone TZ_LOCAL(wxDateTime::Local);
892 static const wxDateTime::TimeZone TZ_TEST(wxDateTime::NZST);
893
894 // this offset is needed to make the test work in any time zone when we
895 // only have expected test results in UTC in testDates
896 static const long tzOffset = TZ_LOCAL.GetOffset() - TZ_TEST.GetOffset();
897
898 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
899 {
900 const Date& d = testDates[n];
901 if ( d.gmticks == -1 )
902 continue;
903
904 wxDateTime dt = d.DT().MakeTimezone(TZ_TEST, true /* no DST */);
905
906 // GetValue() returns internal UTC-based representation, we need to
907 // convert it to local TZ before comparing
908 time_t ticks = (dt.GetValue() / 1000).ToLong() + TZ_LOCAL.GetOffset();
909 if ( dt.IsDST() )
910 ticks += 3600;
911 CPPUNIT_ASSERT_EQUAL( d.gmticks, ticks + tzOffset );
912
913 dt = d.DT().FromTimezone(wxDateTime::UTC);
914 ticks = (dt.GetValue() / 1000).ToLong();
915 CPPUNIT_ASSERT_EQUAL( d.gmticks, ticks );
916 }
917 }
918
919 // test parsing dates in RFC822 format
920 void DateTimeTestCase::TestParceRFC822()
921 {
922 static const struct ParseTestData
923 {
924 const char *rfc822;
925 Date date; // NB: this should be in UTC
926 bool good;
927 } parseTestDates[] =
928 {
929 {
930 "Sat, 18 Dec 1999 00:46:40 +0100",
931 { 17, wxDateTime::Dec, 1999, 23, 46, 40 },
932 true
933 },
934 {
935 "Wed, 1 Dec 1999 05:17:20 +0300",
936 { 1, wxDateTime::Dec, 1999, 2, 17, 20 },
937 true
938 },
939 {
940 "Sun, 28 Aug 2005 03:31:30 +0200",
941 { 28, wxDateTime::Aug, 2005, 1, 31, 30 },
942 true
943 },
944
945 {
946 "Sat, 18 Dec 1999 10:48:30 -0500",
947 { 18, wxDateTime::Dec, 1999, 15, 48, 30 },
948 true
949 },
950
951 // seconds are optional according to the RFC
952 {
953 "Sun, 01 Jun 2008 16:30 +0200",
954 { 1, wxDateTime::Jun, 2008, 14, 30, 00 },
955 true
956 },
957
958 // try some bogus ones too
959 {
960 "Sun, 01 Jun 2008 16:30: +0200",
961 { 0 },
962 false
963 },
964 };
965
966 for ( unsigned n = 0; n < WXSIZEOF(parseTestDates); n++ )
967 {
968 const char * const datestr = parseTestDates[n].rfc822;
969
970 wxDateTime dt;
971 if ( dt.ParseRfc822Date(datestr) )
972 {
973 WX_ASSERT_MESSAGE(
974 ("Erroneously parsed \"%s\"", datestr),
975 parseTestDates[n].good
976 );
977
978 wxDateTime dtReal = parseTestDates[n].date.DT().FromUTC();
979 CPPUNIT_ASSERT_EQUAL( dtReal, dt );
980 }
981 else // failed to parse
982 {
983 WX_ASSERT_MESSAGE(
984 ("Failed to parse \"%s\"", datestr),
985 !parseTestDates[n].good
986 );
987 }
988 }
989 }
990
991 // test parsing dates in free format
992 void DateTimeTestCase::TestDateParse()
993 {
994 static const struct ParseTestData
995 {
996 const char *str;
997 Date date; // NB: this should be in UTC
998 bool good;
999 } parseTestDates[] =
1000 {
1001 { "21 Mar 2006", { 21, wxDateTime::Mar, 2006 }, true },
1002 { "29 Feb 1976", { 29, wxDateTime::Feb, 1976 }, true },
1003 { "Feb 29 1976", { 29, wxDateTime::Feb, 1976 }, true },
1004 { "31/03/06", { 31, wxDateTime::Mar, 6 }, true },
1005 { "31/03/2006", { 31, wxDateTime::Mar, 2006 }, true },
1006
1007 // some invalid ones too
1008 { "29 Feb 2006" },
1009 { "31/04/06" },
1010 { "bloordyblop" },
1011 { "2 . . " },
1012 };
1013
1014 // special cases
1015 wxDateTime dt;
1016 CPPUNIT_ASSERT( dt.ParseDate(wxT("today")) );
1017 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), dt );
1018
1019 for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
1020 {
1021 const wxString datestr = TranslateDate(parseTestDates[n].str);
1022
1023 const char * const end = dt.ParseDate(datestr);
1024 if ( end && !*end )
1025 {
1026 WX_ASSERT_MESSAGE(
1027 ("Erroneously parsed \"%s\"", datestr),
1028 parseTestDates[n].good
1029 );
1030
1031 CPPUNIT_ASSERT_EQUAL( parseTestDates[n].date.DT(), dt );
1032 }
1033 else // failed to parse
1034 {
1035 WX_ASSERT_MESSAGE(
1036 ("Failed to parse \"%s\"", datestr),
1037 !parseTestDates[n].good
1038 );
1039 }
1040 }
1041 }
1042
1043 void DateTimeTestCase::TestDateParseISO()
1044 {
1045 static const struct
1046 {
1047 const char *str;
1048 Date date; // NB: this should be in UTC
1049 bool good;
1050 } parseTestDates[] =
1051 {
1052 { "2006-03-21", { 21, wxDateTime::Mar, 2006 }, true },
1053 { "1976-02-29", { 29, wxDateTime::Feb, 1976 }, true },
1054 { "0006-03-31", { 31, wxDateTime::Mar, 6 }, true },
1055
1056 // some invalid ones too
1057 { "2006:03:31" },
1058 { "31/04/06" },
1059 { "bloordyblop" },
1060 { "" },
1061 };
1062
1063 static const struct
1064 {
1065 const char *str;
1066 wxDateTime::wxDateTime_t hour, min, sec;
1067 bool good;
1068 } parseTestTimes[] =
1069 {
1070 { "13:42:17", 13, 42, 17, true },
1071 { "02:17:01", 2, 17, 1, true },
1072
1073 // some invalid ones too
1074 { "66:03:31" },
1075 { "31/04/06" },
1076 { "bloordyblop" },
1077 { "" },
1078 };
1079
1080 for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
1081 {
1082 wxDateTime dt;
1083 if ( dt.ParseISODate(parseTestDates[n].str) )
1084 {
1085 CPPUNIT_ASSERT( parseTestDates[n].good );
1086
1087 CPPUNIT_ASSERT_EQUAL( parseTestDates[n].date.DT(), dt );
1088
1089 for ( size_t m = 0; m < WXSIZEOF(parseTestTimes); m++ )
1090 {
1091 wxString dtCombined;
1092 dtCombined << parseTestDates[n].str
1093 << 'T'
1094 << parseTestTimes[m].str;
1095
1096 if ( dt.ParseISOCombined(dtCombined) )
1097 {
1098 CPPUNIT_ASSERT( parseTestTimes[m].good );
1099
1100 CPPUNIT_ASSERT_EQUAL( parseTestTimes[m].hour, dt.GetHour()) ;
1101 CPPUNIT_ASSERT_EQUAL( parseTestTimes[m].min, dt.GetMinute()) ;
1102 CPPUNIT_ASSERT_EQUAL( parseTestTimes[m].sec, dt.GetSecond()) ;
1103 }
1104 else // failed to parse combined date/time
1105 {
1106 CPPUNIT_ASSERT( !parseTestTimes[m].good );
1107 }
1108 }
1109 }
1110 else // failed to parse
1111 {
1112 CPPUNIT_ASSERT( !parseTestDates[n].good );
1113 }
1114 }
1115 }
1116
1117 void DateTimeTestCase::TestDateTimeParse()
1118 {
1119 static const struct ParseTestData
1120 {
1121 const char *str;
1122 Date date; // NB: this should be in UTC
1123 bool good;
1124 } parseTestDates[] =
1125 {
1126 {
1127 "Thu 22 Nov 2007 07:40:00 PM",
1128 { 22, wxDateTime::Nov, 2007, 19, 40, 0 },
1129 true
1130 },
1131
1132 {
1133 "2010-01-04 14:30",
1134 { 4, wxDateTime::Jan, 2010, 14, 30, 0 },
1135 true
1136 },
1137
1138 {
1139 "bloordyblop",
1140 { 1, wxDateTime::Jan, 9999, 0, 0, 0},
1141 false
1142 },
1143
1144 {
1145 "2012-01-01 10:12:05 +0100",
1146 { 1, wxDateTime::Jan, 2012, 10, 12, 5, -1 },
1147 false // ParseDateTime does know yet +0100
1148 },
1149 };
1150
1151 // the test strings here use "PM" which is not available in all locales so
1152 // we need to use "C" locale for them
1153 CLocaleSetter cloc;
1154
1155 wxDateTime dt;
1156 for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
1157 {
1158 const wxString datestr = TranslateDate(parseTestDates[n].str);
1159
1160 const char * const end = dt.ParseDateTime(datestr);
1161 if ( end && !*end )
1162 {
1163 WX_ASSERT_MESSAGE(
1164 ("Erroneously parsed \"%s\"", datestr),
1165 parseTestDates[n].good
1166 );
1167
1168 CPPUNIT_ASSERT_EQUAL( parseTestDates[n].date.DT(), dt );
1169 }
1170 else // failed to parse
1171 {
1172 WX_ASSERT_MESSAGE(
1173 ("Failed to parse \"%s\"", datestr),
1174 !parseTestDates[n].good
1175 );
1176
1177 CPPUNIT_ASSERT( !parseTestDates[n].good );
1178 }
1179 }
1180 }
1181
1182 void DateTimeTestCase::TestTimeArithmetics()
1183 {
1184 static const wxDateSpan testArithmData[] =
1185 {
1186 wxDateSpan::Day(),
1187 wxDateSpan::Week(),
1188 wxDateSpan::Month(),
1189 wxDateSpan::Year(),
1190 };
1191
1192 // the test will *not* work with arbitrary date!
1193 wxDateTime dt(2, wxDateTime::Dec, 1999),
1194 dt1,
1195 dt2;
1196
1197 for ( size_t n = 0; n < WXSIZEOF(testArithmData); n++ )
1198 {
1199 const wxDateSpan& span = testArithmData[n];
1200 dt1 = dt + span;
1201 dt2 = dt - span;
1202
1203 CPPUNIT_ASSERT_EQUAL( dt, dt1 - span );
1204 CPPUNIT_ASSERT_EQUAL( dt, dt2 + span );
1205 CPPUNIT_ASSERT_EQUAL( dt1, dt2 + 2*span );
1206 }
1207 }
1208
1209 void DateTimeTestCase::TestDSTBug()
1210 {
1211 /////////////////////////
1212 // Test GetEndDST()
1213 wxDateTime dt = wxDateTime::GetEndDST(2004, wxDateTime::France);
1214 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
1215 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
1216 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
1217 CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
1218 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
1219 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
1220 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
1221
1222 /////////////////////////
1223 // Test ResetTime()
1224 dt.SetHour(5);
1225 CPPUNIT_ASSERT_EQUAL(5, (int)dt.GetHour());
1226 dt.ResetTime();
1227 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
1228 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
1229 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
1230 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetHour());
1231 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
1232 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
1233 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
1234
1235 dt.Set(1, 0, 0, 0);
1236 CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
1237
1238 /////////////////////////
1239 // Test Today()
1240 #ifdef CHANGE_SYSTEM_DATE
1241 {
1242 DateChanger change(2004, 10, 31, 5, 0, 0);
1243 dt = wxDateTime::Today();
1244 }
1245
1246 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
1247 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
1248 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
1249 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetHour());
1250 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
1251 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
1252 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
1253
1254 /////////////////////////
1255 // Test Set(hour, minute, second, milli)
1256 wxDateTime dt2;
1257 {
1258 DateChanger change(2004, 10, 31, 5, 0, 0);
1259 dt.Set(1, 30, 0, 0);
1260 dt2.Set(5, 30, 0, 0);
1261 }
1262
1263 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
1264 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
1265 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
1266 CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
1267 CPPUNIT_ASSERT_EQUAL(30, (int)dt.GetMinute());
1268 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
1269 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
1270
1271 CPPUNIT_ASSERT_EQUAL(31, (int)dt2.GetDay());
1272 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt2.GetMonth());
1273 CPPUNIT_ASSERT_EQUAL(2004, (int)dt2.GetYear());
1274 CPPUNIT_ASSERT_EQUAL(5, (int)dt2.GetHour());
1275 CPPUNIT_ASSERT_EQUAL(30, (int)dt2.GetMinute());
1276 CPPUNIT_ASSERT_EQUAL(0, (int)dt2.GetSecond());
1277 CPPUNIT_ASSERT_EQUAL(0, (int)dt2.GetMillisecond());
1278 #endif // CHANGE_SYSTEM_DATE
1279 }
1280
1281 void DateTimeTestCase::TestDateOnly()
1282 {
1283 wxDateTime dt(19, wxDateTime::Jan, 2007, 15, 01, 00);
1284
1285 static const wxDateTime::wxDateTime_t DATE_ZERO = 0;
1286 CPPUNIT_ASSERT_EQUAL( DATE_ZERO, dt.GetDateOnly().GetHour() );
1287 CPPUNIT_ASSERT_EQUAL( DATE_ZERO, dt.GetDateOnly().GetMinute() );
1288 CPPUNIT_ASSERT_EQUAL( DATE_ZERO, dt.GetDateOnly().GetSecond() );
1289 CPPUNIT_ASSERT_EQUAL( DATE_ZERO, dt.GetDateOnly().GetMillisecond() );
1290
1291 dt.ResetTime();
1292 CPPUNIT_ASSERT_EQUAL( wxDateTime(19, wxDateTime::Jan, 2007), dt );
1293
1294 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), wxDateTime::Now().GetDateOnly() );
1295 }
1296
1297 #endif // wxUSE_DATETIME