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