]> git.saurik.com Git - wxWidgets.git/blob - tests/datetime/datetimetest.cpp
Instantiate NSSecureTextField instead of NSTextField when wxTE_PASSWORD is specified.
[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.FormatISODate() << _T(" ") << dt.FormatISOTime();
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
98 void Init(const wxDateTime::Tm& tm)
99 {
100 day = tm.mday;
101 month = tm.mon;
102 year = tm.year;
103 hour = tm.hour;
104 min = tm.min;
105 sec = tm.sec;
106 jdn = 0.0;
107 gmticks = ticks = -1;
108 }
109
110 wxDateTime DT() const
111 { return wxDateTime(day, month, year, hour, min, sec); }
112
113 bool SameDay(const wxDateTime::Tm& tm) const
114 {
115 return day == tm.mday && month == tm.mon && year == tm.year;
116 }
117
118 wxString Format() const
119 {
120 wxString s;
121 s.Printf(_T("%02d:%02d:%02d %10s %02d, %4d%s"),
122 hour, min, sec,
123 wxDateTime::GetMonthName(month).c_str(),
124 day,
125 abs(wxDateTime::ConvertYearToBC(year)),
126 year > 0 ? _T("AD") : _T("BC"));
127 return s;
128 }
129
130 wxString FormatDate() const
131 {
132 wxString s;
133 s.Printf(_T("%02d-%s-%4d%s"),
134 day,
135 wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(),
136 abs(wxDateTime::ConvertYearToBC(year)),
137 year > 0 ? _T("AD") : _T("BC"));
138 return s;
139 }
140 };
141
142 // ----------------------------------------------------------------------------
143 // test data
144 // ----------------------------------------------------------------------------
145
146 static const Date testDates[] =
147 {
148 { 1, wxDateTime::Jan, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu, 0, -3600 },
149 { 7, wxDateTime::Feb, 2036, 00, 00, 00, 2464730.5, wxDateTime::Thu, -1, -1 },
150 { 8, wxDateTime::Feb, 2036, 00, 00, 00, 2464731.5, wxDateTime::Fri, -1, -1 },
151 { 1, wxDateTime::Jan, 2037, 00, 00, 00, 2465059.5, wxDateTime::Thu, -1, -1 },
152 { 1, wxDateTime::Jan, 2038, 00, 00, 00, 2465424.5, wxDateTime::Fri, -1, -1 },
153 { 21, wxDateTime::Jan, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon, -1, -1 },
154 { 29, wxDateTime::May, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat, 202219200, 202212000 },
155 { 29, wxDateTime::Feb, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun, 194400000, 194396400 },
156 { 1, wxDateTime::Jan, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon, -1, -1 },
157 { 1, wxDateTime::Jan, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon, -1, -1 },
158 { 15, wxDateTime::Oct, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri, -1, -1 },
159 { 4, wxDateTime::Oct, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon, -1, -1 },
160 { 1, wxDateTime::Mar, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu, -1, -1 },
161 { 1, wxDateTime::Jan, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon, -1, -1 },
162 { 31, wxDateTime::Dec, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun, -1, -1 },
163 { 1, wxDateTime::Jan, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat, -1, -1 },
164 { 12, wxDateTime::Aug, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri, -1, -1 },
165 { 12, wxDateTime::Aug, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat, -1, -1 },
166 { 24, wxDateTime::Nov, -4713, 00, 00, 00, -0.5, wxDateTime::Mon, -1, -1 },
167 };
168
169
170 // ----------------------------------------------------------------------------
171 // test class
172 // ----------------------------------------------------------------------------
173
174 class DateTimeTestCase : public CppUnit::TestCase
175 {
176 public:
177 DateTimeTestCase() { }
178
179 private:
180 CPPUNIT_TEST_SUITE( DateTimeTestCase );
181 CPPUNIT_TEST( TestLeapYears );
182 CPPUNIT_TEST( TestTimeSet );
183 CPPUNIT_TEST( TestTimeJDN );
184 CPPUNIT_TEST( TestTimeWNumber );
185 CPPUNIT_TEST( TestTimeWDays );
186 CPPUNIT_TEST( TestTimeDST );
187 CPPUNIT_TEST( TestTimeFormat );
188 CPPUNIT_TEST( TestTimeSpanFormat );
189 CPPUNIT_TEST( TestTimeTicks );
190 CPPUNIT_TEST( TestParceRFC822 );
191 CPPUNIT_TEST( TestDateParse );
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 TestTimeArithmetics();
209 void TestDSTBug();
210 void TestDateOnly();
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 CompareYear, // don't compare centuries (fails for 2 digit years)
596 CompareDate, // dates only
597 CompareTime // time only
598 };
599
600 static const struct
601 {
602 CompareKind compareKind;
603 const wxChar *format;
604 } formatTestFormats[] =
605 {
606 { CompareYear, _T("---> %c") }, // %c could use 2 digit years
607 { CompareDate, _T("Date is %A, %d of %B, in year %Y") },
608 { CompareYear, _T("Date is %x, time is %X") }, // %x could use 2 digits
609 { CompareTime, _T("Time is %H:%M:%S or %I:%M:%S %p") },
610 { CompareNone, _T("The day of year: %j, the week of year: %W") },
611 { CompareDate, _T("ISO date without separators: %Y%m%d") },
612 };
613
614 static const Date formatTestDates[] =
615 {
616 { 29, wxDateTime::May, 1976, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
617 { 31, wxDateTime::Dec, 1999, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
618 { 6, wxDateTime::Feb, 1937, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
619 { 6, wxDateTime::Feb, 1856, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
620 { 6, wxDateTime::Feb, 1857, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
621 { 29, wxDateTime::May, 2076, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
622 { 29, wxDateTime::Feb, 2400, 02, 15, 25, 0.0, wxDateTime::Inv_WeekDay },
623 #if 0
624 // Need to add support for BCE dates.
625 { 01, wxDateTime::Jan, -52, 03, 16, 47, 0.0, wxDateTime::Inv_WeekDay },
626 #endif
627 };
628
629 for ( size_t d = 0; d < WXSIZEOF(formatTestDates); d++ )
630 {
631 wxDateTime dt = formatTestDates[d].DT();
632 for ( size_t n = 0; n < WXSIZEOF(formatTestFormats); n++ )
633 {
634 const wxChar *fmt = formatTestFormats[n].format;
635 wxString s = dt.Format(fmt);
636
637 // what can we recover?
638 CompareKind kind = formatTestFormats[n].compareKind;
639
640 // convert back
641 wxDateTime dt2;
642 const wxChar *result = dt2.ParseFormat(s.c_str(), fmt);
643 if ( !result )
644 {
645 // converion failed - should it have?
646 CPPUNIT_ASSERT( kind == CompareNone );
647 }
648 else // conversion succeeded
649 {
650 // should have parsed the entire string
651 CPPUNIT_ASSERT( !*result );
652
653 switch ( kind )
654 {
655 case CompareYear:
656 if ( dt2.GetCentury() != dt.GetCentury() )
657 {
658 CPPUNIT_ASSERT_EQUAL(dt.GetYear() % 100,
659 dt2.GetYear() % 100);
660
661 dt2.SetYear(dt.GetYear());
662 }
663 // fall through and compare everything
664
665 case CompareBoth:
666 CPPUNIT_ASSERT_EQUAL( dt, dt2 );
667 break;
668
669 case CompareDate:
670 CPPUNIT_ASSERT( dt.IsSameDate(dt2) );
671 break;
672
673 case CompareTime:
674 CPPUNIT_ASSERT( dt.IsSameTime(dt2) );
675 break;
676
677 case CompareNone:
678 wxFAIL_MSG( _T("unexpected") );
679 break;
680 }
681 }
682 }
683 }
684 }
685
686 void DateTimeTestCase::TestTimeSpanFormat()
687 {
688 static const struct TimeSpanFormatTestData
689 {
690 long h, min, sec, msec;
691 const wxChar *fmt;
692 const wxChar *result;
693 } testSpans[] =
694 {
695 { 12, 34, 56, 789, _T("%H:%M:%S.%l"), _T("12:34:56.789") },
696 { 1, 2, 3, 0, _T("%H:%M:%S"), _T("01:02:03") },
697 { 1, 2, 3, 0, _T("%S"), _T("3723") },
698 { -1, -2, -3, 0, _T("%S"), _T("-3723") },
699 { -1, -2, -3, 0, _T("%H:%M:%S"), _T("-01:02:03") },
700 { 26, 0, 0, 0, _T("%H"), _T("26") },
701 { 26, 0, 0, 0, _T("%D, %H"), _T("1, 02") },
702 { -26, 0, 0, 0, _T("%H"), _T("-26") },
703 { -26, 0, 0, 0, _T("%D, %H"), _T("-1, 02") },
704 { 219, 0, 0, 0, _T("%H"), _T("219") },
705 { 219, 0, 0, 0, _T("%D, %H"), _T("9, 03") },
706 { 219, 0, 0, 0, _T("%E, %D, %H"), _T("1, 2, 03") },
707 };
708
709 for ( size_t n = 0; n < WXSIZEOF(testSpans); n++ )
710 {
711 const TimeSpanFormatTestData& td = testSpans[n];
712 wxTimeSpan ts(td.h, td.min, td.sec, td.msec);
713 CPPUNIT_ASSERT_EQUAL( wxString(td.result), ts.Format(td.fmt) );
714 }
715 }
716
717 void DateTimeTestCase::TestTimeTicks()
718 {
719 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
720 {
721 const Date& d = testDates[n];
722 if ( d.ticks == -1 )
723 continue;
724
725 wxDateTime dt = d.DT();
726 //RN: Translate according to test's time zone
727 //2nd param is to ignore DST - it's already factored
728 //into Vadim's tests
729 dt.MakeTimezone(wxDateTime::WEST, true);
730 long ticks = (dt.GetValue() / 1000).ToLong();
731 CPPUNIT_ASSERT( ticks == d.ticks );
732
733 dt = d.DT().FromTimezone(wxDateTime::GMT0);
734 ticks = (dt.GetValue() / 1000).ToLong();
735 CPPUNIT_ASSERT( ticks == d.gmticks );
736 }
737 }
738
739 // test parsing dates in RFC822 format
740 void DateTimeTestCase::TestParceRFC822()
741 {
742 static const struct ParseTestData
743 {
744 const wxChar *rfc822;
745 Date date; // NB: this should be in UTC
746 bool good;
747 } parseTestDates[] =
748 {
749 {
750 _T("Sat, 18 Dec 1999 00:46:40 +0100"),
751 { 17, wxDateTime::Dec, 1999, 23, 46, 40, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
752 true
753 },
754 {
755 _T("Wed, 1 Dec 1999 05:17:20 +0300"),
756 { 1, wxDateTime::Dec, 1999, 2, 17, 20, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
757 true
758 },
759 {
760 _T("Sun, 28 Aug 2005 03:31:30 +0200"),
761 { 28, wxDateTime::Aug, 2005, 1, 31, 30, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
762 true
763 },
764 };
765
766 for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
767 {
768 wxDateTime dt;
769 if ( dt.ParseRfc822Date(parseTestDates[n].rfc822) )
770 {
771 CPPUNIT_ASSERT( parseTestDates[n].good );
772
773 wxDateTime dtReal = parseTestDates[n].date.DT().FromUTC();
774 CPPUNIT_ASSERT_EQUAL( dtReal, dt );
775 }
776 else // failed to parse
777 {
778 CPPUNIT_ASSERT( !parseTestDates[n].good );
779 }
780 }
781 }
782
783 // test parsing dates in free format
784 void DateTimeTestCase::TestDateParse()
785 {
786 static const struct ParseTestData
787 {
788 const wxChar *str;
789 Date date; // NB: this should be in UTC
790 bool good;
791 } parseTestDates[] =
792 {
793 { _T("21 Mar 2006"), { 21, wxDateTime::Mar, 2006 }, true },
794 { _T("29 Feb 1976"), { 29, wxDateTime::Feb, 1976 }, true },
795 { _T("Feb 29 1976"), { 29, wxDateTime::Feb, 1976 }, true },
796 { _T("31/03/06"), { 31, wxDateTime::Mar, 6 }, true },
797 { _T("31/03/2006"), { 31, wxDateTime::Mar, 2006 }, true },
798
799 // some invalid ones too
800 { _T("29 Feb 2006") },
801 { _T("31/04/06") },
802 { _T("bloordyblop") }
803 };
804
805 // special cases
806 wxDateTime dt;
807 CPPUNIT_ASSERT( dt.ParseDate(_T("today")) );
808 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), dt );
809
810 for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
811 {
812 wxDateTime dt;
813 if ( dt.ParseDate(parseTestDates[n].str) )
814 {
815 CPPUNIT_ASSERT( parseTestDates[n].good );
816
817 CPPUNIT_ASSERT_EQUAL( parseTestDates[n].date.DT(), dt );
818 }
819 else // failed to parse
820 {
821 CPPUNIT_ASSERT( !parseTestDates[n].good );
822 }
823 }
824 }
825
826 void DateTimeTestCase::TestTimeArithmetics()
827 {
828 static const wxDateSpan testArithmData[] =
829 {
830 wxDateSpan::Day(),
831 wxDateSpan::Week(),
832 wxDateSpan::Month(),
833 wxDateSpan::Year(),
834 };
835
836 // the test will *not* work with arbitrary date!
837 wxDateTime dt(2, wxDateTime::Dec, 1999),
838 dt1,
839 dt2;
840
841 for ( size_t n = 0; n < WXSIZEOF(testArithmData); n++ )
842 {
843 const wxDateSpan& span = testArithmData[n];
844 dt1 = dt + span;
845 dt2 = dt - span;
846
847 CPPUNIT_ASSERT( dt1 - span == dt );
848 CPPUNIT_ASSERT( dt2 + span == dt );
849 CPPUNIT_ASSERT( dt2 + 2*span == dt1 );
850 }
851 }
852
853 void DateTimeTestCase::TestDSTBug()
854 {
855 /////////////////////////
856 // Test GetEndDST()
857 wxDateTime dt = wxDateTime::GetEndDST(2004);
858 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
859 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
860 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
861 CPPUNIT_ASSERT_EQUAL(2, (int)dt.GetHour());
862 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
863 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
864 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
865
866 /////////////////////////
867 // Test ResetTime()
868 dt.SetHour(5);
869 CPPUNIT_ASSERT_EQUAL(5, (int)dt.GetHour());
870 dt.ResetTime();
871 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
872 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
873 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
874 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetHour());
875 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
876 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
877 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
878
879 dt.Set(1, 0, 0, 0);
880 CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
881
882 /////////////////////////
883 // Test Today()
884 #ifdef CHANGE_SYSTEM_DATE
885 {
886 DateChanger change(2004, 10, 31, 5, 0, 0);
887 dt = wxDateTime::Today();
888 }
889
890 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
891 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
892 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
893 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetHour());
894 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
895 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
896 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
897
898 /////////////////////////
899 // Test Set(hour, minute, second, milli)
900 wxDateTime dt2;
901 {
902 DateChanger change(2004, 10, 31, 5, 0, 0);
903 dt.Set(1, 30, 0, 0);
904 dt2.Set(5, 30, 0, 0);
905 }
906
907 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
908 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
909 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
910 CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
911 CPPUNIT_ASSERT_EQUAL(30, (int)dt.GetMinute());
912 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
913 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
914
915 CPPUNIT_ASSERT_EQUAL(31, (int)dt2.GetDay());
916 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt2.GetMonth());
917 CPPUNIT_ASSERT_EQUAL(2004, (int)dt2.GetYear());
918 CPPUNIT_ASSERT_EQUAL(5, (int)dt2.GetHour());
919 CPPUNIT_ASSERT_EQUAL(30, (int)dt2.GetMinute());
920 CPPUNIT_ASSERT_EQUAL(0, (int)dt2.GetSecond());
921 CPPUNIT_ASSERT_EQUAL(0, (int)dt2.GetMillisecond());
922 #endif // CHANGE_SYSTEM_DATE
923 }
924
925 void DateTimeTestCase::TestDateOnly()
926 {
927 wxDateTime dt(19, wxDateTime::Jan, 2007, 15, 01, 00);
928
929 static const wxDateTime::wxDateTime_t DATE_ZERO = 0;
930 CPPUNIT_ASSERT_EQUAL( DATE_ZERO, dt.GetDateOnly().GetHour() );
931 CPPUNIT_ASSERT_EQUAL( DATE_ZERO, dt.GetDateOnly().GetMinute() );
932 CPPUNIT_ASSERT_EQUAL( DATE_ZERO, dt.GetDateOnly().GetSecond() );
933 CPPUNIT_ASSERT_EQUAL( DATE_ZERO, dt.GetDateOnly().GetMillisecond() );
934
935 dt.ResetTime();
936 CPPUNIT_ASSERT_EQUAL( wxDateTime(19, wxDateTime::Jan, 2007), dt );
937
938 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), wxDateTime::Now().GetDateOnly() );
939 }
940
941 #endif // wxUSE_DATETIME