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