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