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