added wxTimeSpan::Format() test
[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/ioswrap.h"
27
28 // need this to be able to use CPPUNIT_ASSERT_EQUAL with wxDateTime objects
29 static wxSTD ostream& operator<<(wxSTD ostream& ostr, const wxDateTime& dt)
30 {
31 ostr << dt.Format();
32
33 return ostr;
34 }
35
36 // to test Today() meaningfully we must be able to change the system date which
37 // is not usually the case, but if we're under Win32 we can try it -- define
38 // the macro below to do it
39 //#define CHANGE_SYSTEM_DATE
40
41 #ifndef __WINDOWS__
42 #undef CHANGE_SYSTEM_DATE
43 #endif
44
45 #ifdef CHANGE_SYSTEM_DATE
46
47 class DateChanger
48 {
49 public:
50 DateChanger(int year, int month, int day, int hour, int min, int sec)
51 {
52 SYSTEMTIME st;
53 st.wDay = day;
54 st.wMonth = month;
55 st.wYear = year;
56 st.wHour = hour;
57 st.wMinute = min;
58 st.wSecond = sec;
59 st.wMilliseconds = 0;
60
61 ::GetSystemTime(&m_savedTime);
62 ::GetTimeZoneInformation(&m_tzi);
63
64 m_changed = ::SetSystemTime(&st) != 0;
65 }
66
67 ~DateChanger()
68 {
69 if ( m_changed )
70 {
71 ::SetSystemTime(&m_savedTime);
72 ::SetTimeZoneInformation(&m_tzi);
73 }
74 }
75
76 private:
77 SYSTEMTIME m_savedTime;
78 TIME_ZONE_INFORMATION m_tzi;
79 bool m_changed;
80 };
81
82 #endif // CHANGE_SYSTEM_DATE
83
84 // ----------------------------------------------------------------------------
85 // broken down date representation used for testing
86 // ----------------------------------------------------------------------------
87
88 struct Date
89 {
90 wxDateTime::wxDateTime_t day;
91 wxDateTime::Month month;
92 int year;
93 wxDateTime::wxDateTime_t hour, min, sec;
94 double jdn;
95 wxDateTime::WeekDay wday;
96 time_t gmticks, ticks;
97 long flags; //Test specific flags - currently only used by TestTimeFormat.
98
99 void Init(const wxDateTime::Tm& tm)
100 {
101 day = tm.mday;
102 month = tm.mon;
103 year = tm.year;
104 hour = tm.hour;
105 min = tm.min;
106 sec = tm.sec;
107 jdn = 0.0;
108 gmticks = ticks = -1;
109 }
110
111 wxDateTime DT() const
112 { return wxDateTime(day, month, year, hour, min, sec); }
113
114 bool SameDay(const wxDateTime::Tm& tm) const
115 {
116 return day == tm.mday && month == tm.mon && year == tm.year;
117 }
118
119 wxString Format() const
120 {
121 wxString s;
122 s.Printf(_T("%02d:%02d:%02d %10s %02d, %4d%s"),
123 hour, min, sec,
124 wxDateTime::GetMonthName(month).c_str(),
125 day,
126 abs(wxDateTime::ConvertYearToBC(year)),
127 year > 0 ? _T("AD") : _T("BC"));
128 return s;
129 }
130
131 wxString FormatDate() const
132 {
133 wxString s;
134 s.Printf(_T("%02d-%s-%4d%s"),
135 day,
136 wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(),
137 abs(wxDateTime::ConvertYearToBC(year)),
138 year > 0 ? _T("AD") : _T("BC"));
139 return s;
140 }
141 bool IsSet(long f) const { return (f && flags) ==f; }
142 };
143
144 // ----------------------------------------------------------------------------
145 // test data
146 // ----------------------------------------------------------------------------
147
148 static const Date testDates[] =
149 {
150 { 1, wxDateTime::Jan, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu, 0, -3600 },
151 { 7, wxDateTime::Feb, 2036, 00, 00, 00, 2464730.5, wxDateTime::Thu, -1, -1 },
152 { 8, wxDateTime::Feb, 2036, 00, 00, 00, 2464731.5, wxDateTime::Fri, -1, -1 },
153 { 1, wxDateTime::Jan, 2037, 00, 00, 00, 2465059.5, wxDateTime::Thu, -1, -1 },
154 { 1, wxDateTime::Jan, 2038, 00, 00, 00, 2465424.5, wxDateTime::Fri, -1, -1 },
155 { 21, wxDateTime::Jan, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon, -1, -1 },
156 { 29, wxDateTime::May, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat, 202219200, 202212000 },
157 { 29, wxDateTime::Feb, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun, 194400000, 194396400 },
158 { 1, wxDateTime::Jan, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon, -1, -1 },
159 { 1, wxDateTime::Jan, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon, -1, -1 },
160 { 15, wxDateTime::Oct, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri, -1, -1 },
161 { 4, wxDateTime::Oct, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon, -1, -1 },
162 { 1, wxDateTime::Mar, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu, -1, -1 },
163 { 1, wxDateTime::Jan, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon, -1, -1 },
164 { 31, wxDateTime::Dec, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun, -1, -1 },
165 { 1, wxDateTime::Jan, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat, -1, -1 },
166 { 12, wxDateTime::Aug, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri, -1, -1 },
167 { 12, wxDateTime::Aug, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat, -1, -1 },
168 { 24, wxDateTime::Nov, -4713, 00, 00, 00, -0.5, wxDateTime::Mon, -1, -1 },
169 };
170
171
172 // ----------------------------------------------------------------------------
173 // test class
174 // ----------------------------------------------------------------------------
175
176 class DateTimeTestCase : public CppUnit::TestCase
177 {
178 public:
179 DateTimeTestCase() { }
180
181 private:
182 CPPUNIT_TEST_SUITE( DateTimeTestCase );
183 CPPUNIT_TEST( TestLeapYears );
184 CPPUNIT_TEST( TestTimeSet );
185 CPPUNIT_TEST( TestTimeJDN );
186 CPPUNIT_TEST( TestTimeWNumber );
187 CPPUNIT_TEST( TestTimeWDays );
188 CPPUNIT_TEST( TestTimeDST );
189 CPPUNIT_TEST( TestTimeFormat );
190 CPPUNIT_TEST( TestTimeSpanFormat );
191 CPPUNIT_TEST( TestTimeTicks );
192 CPPUNIT_TEST( TestParceRFC822 );
193 CPPUNIT_TEST( TestDateParse );
194 CPPUNIT_TEST( TestTimeArithmetics );
195 CPPUNIT_TEST( TestDSTBug );
196 CPPUNIT_TEST_SUITE_END();
197
198 void TestLeapYears();
199 void TestTimeSet();
200 void TestTimeJDN();
201 void TestTimeWNumber();
202 void TestTimeWDays();
203 void TestTimeDST();
204 void TestTimeFormat();
205 void TestTimeSpanFormat();
206 void TestTimeTicks();
207 void TestParceRFC822();
208 void TestDateParse();
209 void TestTimeArithmetics();
210 void TestDSTBug();
211
212 DECLARE_NO_COPY_CLASS(DateTimeTestCase)
213 };
214
215 // register in the unnamed registry so that these tests are run by default
216 CPPUNIT_TEST_SUITE_REGISTRATION( DateTimeTestCase );
217
218 // also include in it's own registry so that these tests can be run alone
219 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DateTimeTestCase, "DateTimeTestCase" );
220
221 // ============================================================================
222 // implementation
223 // ============================================================================
224
225 // test leap years detection
226 void DateTimeTestCase::TestLeapYears()
227 {
228 static const struct LeapYearTestData
229 {
230 int year;
231 bool isLeap;
232 } years[] =
233 {
234 { 1900, false },
235 { 1990, false },
236 { 1976, true },
237 { 2000, true },
238 { 2030, false },
239 { 1984, true },
240 { 2100, false },
241 { 2400, true },
242 };
243
244 for ( size_t n = 0; n < WXSIZEOF(years); n++ )
245 {
246 const LeapYearTestData& y = years[n];
247
248 CPPUNIT_ASSERT( wxDateTime::IsLeapYear(y.year) == y.isLeap );
249 }
250 }
251
252 // test constructing wxDateTime objects
253 void DateTimeTestCase::TestTimeSet()
254 {
255 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
256 {
257 const Date& d1 = testDates[n];
258 wxDateTime dt = d1.DT();
259
260 Date d2;
261 d2.Init(dt.GetTm());
262
263 wxString s1 = d1.Format(),
264 s2 = d2.Format();
265
266 CPPUNIT_ASSERT( s1 == s2 );
267 }
268 }
269
270 // test conversions to JDN &c
271 void DateTimeTestCase::TestTimeJDN()
272 {
273 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
274 {
275 const Date& d = testDates[n];
276 wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
277
278 // JDNs must be computed for UTC times
279 double jdn = dt.FromUTC().GetJulianDayNumber();
280
281 CPPUNIT_ASSERT( jdn == d.jdn );
282
283 dt.Set(jdn);
284 CPPUNIT_ASSERT( dt.GetJulianDayNumber() == jdn );
285 }
286 }
287
288 // test week days computation
289 void DateTimeTestCase::TestTimeWDays()
290 {
291 // test GetWeekDay()
292 size_t n;
293 for ( n = 0; n < WXSIZEOF(testDates); n++ )
294 {
295 const Date& d = testDates[n];
296 wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
297
298 wxDateTime::WeekDay wday = dt.GetWeekDay();
299 CPPUNIT_ASSERT( wday == d.wday );
300 }
301
302 // test SetToWeekDay()
303 struct WeekDateTestData
304 {
305 Date date; // the real date (precomputed)
306 int nWeek; // its week index in the month
307 wxDateTime::WeekDay wday; // the weekday
308 wxDateTime::Month month; // the month
309 int year; // and the year
310
311 wxString Format() const
312 {
313 wxString s, which;
314 switch ( nWeek < -1 ? -nWeek : nWeek )
315 {
316 case 1: which = _T("first"); break;
317 case 2: which = _T("second"); break;
318 case 3: which = _T("third"); break;
319 case 4: which = _T("fourth"); break;
320 case 5: which = _T("fifth"); break;
321
322 case -1: which = _T("last"); break;
323 }
324
325 if ( nWeek < -1 )
326 {
327 which += _T(" from end");
328 }
329
330 s.Printf(_T("The %s %s of %s in %d"),
331 which.c_str(),
332 wxDateTime::GetWeekDayName(wday).c_str(),
333 wxDateTime::GetMonthName(month).c_str(),
334 year);
335
336 return s;
337 }
338 };
339
340 // the array data was generated by the following python program
341 /*
342 from DateTime import *
343 from whrandom import *
344 from string import *
345
346 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
347 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
348
349 week = DateTimeDelta(7)
350
351 for n in range(20):
352 year = randint(1900, 2100)
353 month = randint(1, 12)
354 day = randint(1, 28)
355 dt = DateTime(year, month, day)
356 wday = dt.day_of_week
357
358 countFromEnd = choice([-1, 1])
359 weekNum = 0;
360
361 while dt.month is month:
362 dt = dt - countFromEnd * week
363 weekNum = weekNum + countFromEnd
364
365 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
366
367 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
368 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
369 */
370
371 static const WeekDateTestData weekDatesTestData[] =
372 {
373 { { 20, wxDateTime::Mar, 2045, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 3, wxDateTime::Mon, wxDateTime::Mar, 2045 },
374 { { 5, wxDateTime::Jun, 1985, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, -4, wxDateTime::Wed, wxDateTime::Jun, 1985 },
375 { { 12, wxDateTime::Nov, 1961, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, -3, wxDateTime::Sun, wxDateTime::Nov, 1961 },
376 { { 27, wxDateTime::Feb, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, -1, wxDateTime::Fri, wxDateTime::Feb, 2093 },
377 { { 4, wxDateTime::Jul, 2070, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, -4, wxDateTime::Fri, wxDateTime::Jul, 2070 },
378 { { 2, wxDateTime::Apr, 1906, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, -5, wxDateTime::Mon, wxDateTime::Apr, 1906 },
379 { { 19, wxDateTime::Jul, 2023, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, -2, wxDateTime::Wed, wxDateTime::Jul, 2023 },
380 { { 5, wxDateTime::May, 1958, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, -4, wxDateTime::Mon, wxDateTime::May, 1958 },
381 { { 11, wxDateTime::Aug, 1900, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 2, wxDateTime::Sat, wxDateTime::Aug, 1900 },
382 { { 14, wxDateTime::Feb, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 2, wxDateTime::Wed, wxDateTime::Feb, 1945 },
383 { { 25, wxDateTime::Jul, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, -1, wxDateTime::Tue, wxDateTime::Jul, 1967 },
384 { { 9, wxDateTime::May, 1916, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, -4, wxDateTime::Tue, wxDateTime::May, 1916 },
385 { { 20, wxDateTime::Jun, 1927, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 3, wxDateTime::Mon, wxDateTime::Jun, 1927 },
386 { { 2, wxDateTime::Aug, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 1, wxDateTime::Wed, wxDateTime::Aug, 2000 },
387 { { 20, wxDateTime::Apr, 2044, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 3, wxDateTime::Wed, wxDateTime::Apr, 2044 },
388 { { 20, wxDateTime::Feb, 1932, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, -2, wxDateTime::Sat, wxDateTime::Feb, 1932 },
389 { { 25, wxDateTime::Jul, 2069, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 4, wxDateTime::Thu, wxDateTime::Jul, 2069 },
390 { { 3, wxDateTime::Apr, 1925, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 1, wxDateTime::Fri, wxDateTime::Apr, 1925 },
391 { { 21, wxDateTime::Mar, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 3, wxDateTime::Sat, wxDateTime::Mar, 2093 },
392 { { 3, wxDateTime::Dec, 2074, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, -5, wxDateTime::Mon, wxDateTime::Dec, 2074 }
393 };
394
395 wxDateTime dt;
396 for ( n = 0; n < WXSIZEOF(weekDatesTestData); n++ )
397 {
398 const WeekDateTestData& wd = weekDatesTestData[n];
399
400 dt.SetToWeekDay(wd.wday, wd.nWeek, wd.month, wd.year);
401
402 const Date& d = wd.date;
403 CPPUNIT_ASSERT( d.SameDay(dt.GetTm()) );
404 }
405 }
406
407 // test the computation of (ISO) week numbers
408 void DateTimeTestCase::TestTimeWNumber()
409 {
410 struct WeekNumberTestData
411 {
412 Date date; // the date
413 wxDateTime::wxDateTime_t week; // the week number in the year
414 wxDateTime::wxDateTime_t wmon; // the week number in the month
415 wxDateTime::wxDateTime_t wmon2; // same but week starts with Sun
416 wxDateTime::wxDateTime_t dnum; // day number in the year
417 };
418
419 // data generated with the following python script:
420 /*
421 from DateTime import *
422 from whrandom import *
423 from string import *
424
425 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
426 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
427
428 def GetMonthWeek(dt):
429 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
430 if weekNumMonth < 0:
431 weekNumMonth = weekNumMonth + 53
432 return weekNumMonth
433
434 def GetLastSundayBefore(dt):
435 if dt.iso_week[2] == 7:
436 return dt
437 else:
438 return dt - DateTimeDelta(dt.iso_week[2])
439
440 for n in range(20):
441 year = randint(1900, 2100)
442 month = randint(1, 12)
443 day = randint(1, 28)
444 dt = DateTime(year, month, day)
445 dayNum = dt.day_of_year
446 weekNum = dt.iso_week[1]
447 weekNumMonth = GetMonthWeek(dt)
448
449 weekNumMonth2 = 0
450 dtSunday = GetLastSundayBefore(dt)
451
452 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
453 weekNumMonth2 = weekNumMonth2 + 1
454 dtSunday = dtSunday - DateTimeDelta(7)
455
456 data = { 'day': rjust(`day`, 2), \
457 'month': monthNames[month - 1], \
458 'year': year, \
459 'weekNum': rjust(`weekNum`, 2), \
460 'weekNumMonth': weekNumMonth, \
461 'weekNumMonth2': weekNumMonth2, \
462 'dayNum': rjust(`dayNum`, 3) }
463
464 print " { { %(day)s, "\
465 "wxDateTime::%(month)s, "\
466 "%(year)d }, "\
467 "%(weekNum)s, "\
468 "%(weekNumMonth)s, "\
469 "%(weekNumMonth2)s, "\
470 "%(dayNum)s }," % data
471
472 */
473 static const WeekNumberTestData weekNumberTestDates[] =
474 {
475 { { 27, wxDateTime::Dec, 1966, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 52, 5, 5, 361 },
476 { { 22, wxDateTime::Jul, 1926, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 29, 4, 4, 203 },
477 { { 22, wxDateTime::Oct, 2076, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 43, 4, 4, 296 },
478 { { 1, wxDateTime::Jul, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 26, 1, 1, 182 },
479 { { 8, wxDateTime::Nov, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 46, 2, 2, 313 },
480 { { 21, wxDateTime::Mar, 1920, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 12, 3, 4, 81 },
481 { { 7, wxDateTime::Jan, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 1, 2, 2, 7 },
482 { { 19, wxDateTime::Oct, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 42, 4, 4, 292 },
483 { { 13, wxDateTime::Aug, 1955, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 32, 2, 2, 225 },
484 { { 18, wxDateTime::Jul, 2087, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 29, 3, 3, 199 },
485 { { 2, wxDateTime::Sep, 2028, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 35, 1, 1, 246 },
486 { { 28, wxDateTime::Jul, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 30, 5, 4, 209 },
487 { { 15, wxDateTime::Jun, 1901, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 24, 3, 3, 166 },
488 { { 10, wxDateTime::Oct, 1939, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 41, 3, 2, 283 },
489 { { 3, wxDateTime::Dec, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 48, 1, 1, 337 },
490 { { 23, wxDateTime::Feb, 1940, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 8, 4, 4, 54 },
491 { { 2, wxDateTime::Jan, 1987, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 1, 1, 1, 2 },
492 { { 11, wxDateTime::Aug, 2079, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 32, 2, 2, 223 },
493 { { 2, wxDateTime::Feb, 2063, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 5, 1, 1, 33 },
494 { { 16, wxDateTime::Oct, 1942, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 42, 3, 3, 289 },
495 { { 30, wxDateTime::Dec, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 1, 5, 5, 364 },
496 { { 2, wxDateTime::Jan, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, 1, 1, 1, 2 },
497 };
498
499 for ( size_t n = 0; n < WXSIZEOF(weekNumberTestDates); n++ )
500 {
501 const WeekNumberTestData& wn = weekNumberTestDates[n];
502 const Date& d = wn.date;
503
504 wxDateTime dt = d.DT();
505
506 wxDateTime::wxDateTime_t
507 week = dt.GetWeekOfYear(wxDateTime::Monday_First),
508 wmon = dt.GetWeekOfMonth(wxDateTime::Monday_First),
509 wmon2 = dt.GetWeekOfMonth(wxDateTime::Sunday_First),
510 dnum = dt.GetDayOfYear();
511
512 CPPUNIT_ASSERT( dnum == wn.dnum );
513 CPPUNIT_ASSERT( wmon == wn.wmon );
514 CPPUNIT_ASSERT( wmon2 == wn.wmon2 );
515 CPPUNIT_ASSERT( week == wn.week );
516
517 int year = d.year;
518 if ( week == 1 && d.month != wxDateTime::Jan )
519 {
520 // this means we're in the first week of the next year
521 year++;
522 }
523
524 wxDateTime
525 dt2 = wxDateTime::SetToWeekOfYear(year, week, dt.GetWeekDay());
526 CPPUNIT_ASSERT( dt2 == dt );
527 }
528 }
529
530 // test DST applicability
531 void DateTimeTestCase::TestTimeDST()
532 {
533 // taken from http://www.energy.ca.gov/daylightsaving.html
534 static const Date datesDST[2][2004 - 1900 + 1] =
535 {
536 {
537 { 1, wxDateTime::Apr, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
538 { 7, wxDateTime::Apr, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
539 { 5, wxDateTime::Apr, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
540 { 4, wxDateTime::Apr, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
541 { 3, wxDateTime::Apr, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
542 { 2, wxDateTime::Apr, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
543 { 7, wxDateTime::Apr, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
544 { 6, wxDateTime::Apr, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
545 { 5, wxDateTime::Apr, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
546 { 4, wxDateTime::Apr, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
547 { 2, wxDateTime::Apr, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
548 { 1, wxDateTime::Apr, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
549 { 7, wxDateTime::Apr, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
550 { 6, wxDateTime::Apr, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
551 { 4, wxDateTime::Apr, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
552 },
553 {
554 { 28, wxDateTime::Oct, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
555 { 27, wxDateTime::Oct, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
556 { 25, wxDateTime::Oct, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
557 { 31, wxDateTime::Oct, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
558 { 30, wxDateTime::Oct, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
559 { 29, wxDateTime::Oct, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
560 { 27, wxDateTime::Oct, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
561 { 26, wxDateTime::Oct, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
562 { 25, wxDateTime::Oct, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
563 { 31, wxDateTime::Oct, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
564 { 29, wxDateTime::Oct, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
565 { 28, wxDateTime::Oct, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
566 { 27, wxDateTime::Oct, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
567 { 26, wxDateTime::Oct, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
568 { 31, wxDateTime::Oct, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
569 }
570 };
571
572 for ( int year = 1990; year < 2005; year++ )
573 {
574 wxDateTime dtBegin = wxDateTime::GetBeginDST(year, wxDateTime::USA),
575 dtEnd = wxDateTime::GetEndDST(year, wxDateTime::USA);
576
577 size_t n = year - 1990;
578 const Date& dBegin = datesDST[0][n];
579 const Date& dEnd = datesDST[1][n];
580
581 CPPUNIT_ASSERT( dBegin.SameDay(dtBegin.GetTm()) );
582 CPPUNIT_ASSERT( dEnd.SameDay(dtEnd.GetTm()) );
583 }
584 }
585
586 // test wxDateTime -> text conversion
587 void DateTimeTestCase::TestTimeFormat()
588 {
589 // some information may be lost during conversion, so store what kind
590 // of info should we recover after a round trip
591 enum CompareKind
592 {
593 CompareNone, // don't try comparing
594 CompareBoth, // dates and times should be identical
595 CompareDate, // dates only
596 CompareTime // time only
597 };
598
599 const int WORKS_WITH_2DIGIT_YEAR(1);
600 static const struct
601 {
602 CompareKind compareKind;
603 long flagsneeded;
604 const wxChar *format;
605 } formatTestFormats[] =
606 {
607 { CompareBoth,0 , _T("---> %c") }, //Assumes %c show a 4digit year.
608 { CompareDate,0, _T("Date is %A, %d of %B, in year %Y") },
609 { CompareBoth,WORKS_WITH_2DIGIT_YEAR, _T("Date is %x, time is %X") },
610 { CompareTime,0, _T("Time is %H:%M:%S or %I:%M:%S %p") },
611 { CompareNone,0, _T("The day of year: %j, the week of year: %W") },
612 { CompareDate,0, _T("ISO date without separators: %Y%m%d") },
613 };
614
615 static const Date formatTestDates[] =
616 {
617 { 29, wxDateTime::May, 1976, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay, 0, 0 , WORKS_WITH_2DIGIT_YEAR},
618 { 31, wxDateTime::Dec, 1999, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay, 0, 0 , WORKS_WITH_2DIGIT_YEAR },
619 { 6, wxDateTime::Feb, 1937, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay, 0, 0 ,0 },
620 { 6, wxDateTime::Feb, 1856, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay, 0, 0 ,0 },
621 { 6, wxDateTime::Feb, 1857, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay, 0, 0 ,0 },
622 { 29, wxDateTime::May, 2076, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay, 0, 0 ,0 },
623 { 29, wxDateTime::Feb, 2400, 02, 15, 25, 0.0, wxDateTime::Inv_WeekDay, 0, 0 ,0 },
624 #if 0
625 // Need to add support for BCE dates.
626 { 01, wxDateTime::Jan, -52, 03, 16, 47, 0.0, wxDateTime::Inv_WeekDay, 0, 0 ,0 },
627 #endif
628 };
629
630 for ( size_t d = 0; d < WXSIZEOF(formatTestDates) + 1; d++ )
631 {
632 wxDateTime dt = d == 0 ? wxDateTime::Now() : formatTestDates[d - 1].DT();
633 for ( size_t n = 0; n < WXSIZEOF(formatTestFormats); n++ )
634 {
635 //Skip test if date hasn't got the required flags.
636 if ((d!=0) && !(formatTestDates[d - 1].IsSet(formatTestFormats[n].flagsneeded))) continue;
637
638 wxString s = dt.Format(formatTestFormats[n].format);
639 // what can we recover?
640 int kind = formatTestFormats[n].compareKind;
641
642 // convert back
643 wxDateTime dt2;
644 const wxChar *result = dt2.ParseFormat(s, formatTestFormats[n].format);
645 if ( !result )
646 {
647 // converion failed - should it have?
648 CPPUNIT_ASSERT( kind == CompareNone );
649 }
650 else // conversion succeeded
651 {
652 // should have parsed the entire string
653 CPPUNIT_ASSERT( !*result );
654
655 switch ( kind )
656 {
657 case CompareBoth:
658 CPPUNIT_ASSERT( dt2 == dt );
659 break;
660
661 case CompareDate:
662 CPPUNIT_ASSERT( dt.IsSameDate(dt2) );
663 break;
664
665 case CompareTime:
666 CPPUNIT_ASSERT( dt.IsSameTime(dt2) );
667 break;
668 }
669 }
670 }
671 }
672 }
673
674 void DateTimeTestCase::TestTimeSpanFormat()
675 {
676 static const struct TimeSpanFormatTestData
677 {
678 long h, min, sec, msec;
679 const wxChar *fmt;
680 const wxChar *result;
681 } testSpans[] =
682 {
683 { 12, 34, 56, 789, _T("%H:%M:%S.%l"), _T("12:34:56.789") },
684 { 1, 2, 3, 0, _T("%H:%M:%S"), _T("01:02:03") },
685 { 1, 2, 3, 0, _T("%S"), _T("3723") },
686 };
687
688 for ( size_t n = 0; n < WXSIZEOF(testSpans); n++ )
689 {
690 const TimeSpanFormatTestData& td = testSpans[n];
691 wxTimeSpan ts(td.h, td.min, td.sec, td.msec);
692 CPPUNIT_ASSERT_EQUAL( wxString(td.result), ts.Format(td.fmt) );
693 }
694 }
695
696 void DateTimeTestCase::TestTimeTicks()
697 {
698 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
699 {
700 const Date& d = testDates[n];
701 if ( d.ticks == -1 )
702 continue;
703
704 wxDateTime dt = d.DT();
705 //RN: Translate according to test's time zone
706 //2nd param is to ignore DST - it's already factored
707 //into Vadim's tests
708 dt.MakeTimezone(wxDateTime::WEST, true);
709 long ticks = (dt.GetValue() / 1000).ToLong();
710 CPPUNIT_ASSERT( ticks == d.ticks );
711
712 dt = d.DT().FromTimezone(wxDateTime::GMT0);
713 ticks = (dt.GetValue() / 1000).ToLong();
714 CPPUNIT_ASSERT( ticks == d.gmticks );
715 }
716 }
717
718 // test parsing dates in RFC822 format
719 void DateTimeTestCase::TestParceRFC822()
720 {
721 static const struct ParseTestData
722 {
723 const wxChar *rfc822;
724 Date date; // NB: this should be in UTC
725 bool good;
726 } parseTestDates[] =
727 {
728 {
729 _T("Sat, 18 Dec 1999 00:46:40 +0100"),
730 { 17, wxDateTime::Dec, 1999, 23, 46, 40, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
731 true
732 },
733 {
734 _T("Wed, 1 Dec 1999 05:17:20 +0300"),
735 { 1, wxDateTime::Dec, 1999, 2, 17, 20, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
736 true
737 },
738 {
739 _T("Sun, 28 Aug 2005 03:31:30 +0200"),
740 { 28, wxDateTime::Aug, 2005, 1, 31, 30, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
741 true
742 },
743 };
744
745 for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
746 {
747 wxDateTime dt;
748 if ( dt.ParseRfc822Date(parseTestDates[n].rfc822) )
749 {
750 CPPUNIT_ASSERT( parseTestDates[n].good );
751
752 wxDateTime dtReal = parseTestDates[n].date.DT().FromUTC();
753 CPPUNIT_ASSERT_EQUAL( dtReal, dt );
754 }
755 else // failed to parse
756 {
757 CPPUNIT_ASSERT( !parseTestDates[n].good );
758 }
759 }
760 }
761
762 // test parsing dates in free format
763 void DateTimeTestCase::TestDateParse()
764 {
765 static const struct ParseTestData
766 {
767 const wxChar *str;
768 Date date; // NB: this should be in UTC
769 bool good;
770 } parseTestDates[] =
771 {
772 { _T("21 Mar 2006"), { 21, wxDateTime::Mar, 2006 }, true },
773 { _T("29 Feb 1976"), { 29, wxDateTime::Feb, 1976 }, true },
774 { _T("Feb 29 1976"), { 29, wxDateTime::Feb, 1976 }, true },
775 { _T("31/03/06"), { 31, wxDateTime::Mar, 6 }, true },
776 { _T("31/03/2006"), { 31, wxDateTime::Mar, 2006 }, true },
777
778 // some invalid ones too
779 { _T("29 Feb 2006") },
780 { _T("31/04/06") },
781 { _T("bloordyblop") }
782 };
783
784 // special cases
785 wxDateTime dt;
786 CPPUNIT_ASSERT( dt.ParseDate(_T("today")) );
787 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), dt );
788
789 for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
790 {
791 wxDateTime dt;
792 if ( dt.ParseDate(parseTestDates[n].str) )
793 {
794 CPPUNIT_ASSERT( parseTestDates[n].good );
795
796 CPPUNIT_ASSERT_EQUAL( parseTestDates[n].date.DT(), dt );
797 }
798 else // failed to parse
799 {
800 CPPUNIT_ASSERT( !parseTestDates[n].good );
801 }
802 }
803 }
804
805 void DateTimeTestCase::TestTimeArithmetics()
806 {
807 static const wxDateSpan testArithmData[] =
808 {
809 wxDateSpan::Day(),
810 wxDateSpan::Week(),
811 wxDateSpan::Month(),
812 wxDateSpan::Year(),
813 };
814
815 // the test will *not* work with arbitrary date!
816 wxDateTime dt(2, wxDateTime::Dec, 1999),
817 dt1,
818 dt2;
819
820 for ( size_t n = 0; n < WXSIZEOF(testArithmData); n++ )
821 {
822 const wxDateSpan& span = testArithmData[n];
823 dt1 = dt + span;
824 dt2 = dt - span;
825
826 CPPUNIT_ASSERT( dt1 - span == dt );
827 CPPUNIT_ASSERT( dt2 + span == dt );
828 CPPUNIT_ASSERT( dt2 + 2*span == dt1 );
829 }
830 }
831
832 void DateTimeTestCase::TestDSTBug()
833 {
834 /////////////////////////
835 // Test GetEndDST()
836 wxDateTime dt = wxDateTime::GetEndDST(2004);
837 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
838 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
839 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
840 CPPUNIT_ASSERT_EQUAL(2, (int)dt.GetHour());
841 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
842 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
843 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
844
845 /////////////////////////
846 // Test ResetTime()
847 dt.SetHour(5);
848 CPPUNIT_ASSERT_EQUAL(5, (int)dt.GetHour());
849 dt.ResetTime();
850 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
851 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
852 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
853 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetHour());
854 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
855 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
856 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
857
858 dt.Set(1, 0, 0, 0);
859 CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
860
861 /////////////////////////
862 // Test Today()
863 #ifdef CHANGE_SYSTEM_DATE
864 {
865 DateChanger change(2004, 10, 31, 5, 0, 0);
866 dt = wxDateTime::Today();
867 }
868
869 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
870 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
871 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
872 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetHour());
873 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
874 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
875 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
876
877 /////////////////////////
878 // Test Set(hour, minute, second, milli)
879 wxDateTime dt2;
880 {
881 DateChanger change(2004, 10, 31, 5, 0, 0);
882 dt.Set(1, 30, 0, 0);
883 dt2.Set(5, 30, 0, 0);
884 }
885
886 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
887 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
888 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
889 CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
890 CPPUNIT_ASSERT_EQUAL(30, (int)dt.GetMinute());
891 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
892 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
893
894 CPPUNIT_ASSERT_EQUAL(31, (int)dt2.GetDay());
895 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt2.GetMonth());
896 CPPUNIT_ASSERT_EQUAL(2004, (int)dt2.GetYear());
897 CPPUNIT_ASSERT_EQUAL(5, (int)dt2.GetHour());
898 CPPUNIT_ASSERT_EQUAL(30, (int)dt2.GetMinute());
899 CPPUNIT_ASSERT_EQUAL(0, (int)dt2.GetSecond());
900 CPPUNIT_ASSERT_EQUAL(0, (int)dt2.GetMillisecond());
901 #endif // CHANGE_SYSTEM_DATE
902 }
903
904 #endif // wxUSE_DATETIME