]> git.saurik.com Git - wxWidgets.git/blame - tests/datetime/datetimetest.cpp
Fix popen2 deprecation warning.
[wxWidgets.git] / tests / datetime / datetimetest.cpp
CommitLineData
ccd6aacd
VZ
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
8899b155 14#include "testprec.h"
ccd6aacd
VZ
15
16#ifdef __BORLANDC__
17 #pragma hdrstop
18#endif
19
20#ifndef WX_PRECOMP
21#endif // WX_PRECOMP
22
8605eb1a
VZ
23#if wxUSE_DATETIME
24
ccd6aacd 25#include "wx/datetime.h"
1789c1c6 26#include "wx/wxcrt.h" // for wxStrstr()
8605eb1a
VZ
27
28// need this to be able to use CPPUNIT_ASSERT_EQUAL with wxDateTime objects
5098c258 29static std::ostream& operator<<(std::ostream& ostr, const wxDateTime& dt)
8605eb1a 30{
f3f2e255 31 ostr << dt.FormatISOCombined(' ');
8605eb1a
VZ
32
33 return ostr;
34}
ccd6aacd 35
b2eabfe8
VZ
36WX_CPPUNIT_ALLOW_EQUALS_TO_INT(wxDateTime::wxDateTime_t)
37
a480d0ab
VZ
38// to test Today() meaningfully we must be able to change the system date which
39// is not usually the case, but if we're under Win32 we can try it -- define
40// the macro below to do it
41//#define CHANGE_SYSTEM_DATE
42
43#ifndef __WINDOWS__
44 #undef CHANGE_SYSTEM_DATE
45#endif
46
47#ifdef CHANGE_SYSTEM_DATE
48
49class DateChanger
50{
51public:
52 DateChanger(int year, int month, int day, int hour, int min, int sec)
53 {
54 SYSTEMTIME st;
55 st.wDay = day;
56 st.wMonth = month;
57 st.wYear = year;
58 st.wHour = hour;
59 st.wMinute = min;
60 st.wSecond = sec;
61 st.wMilliseconds = 0;
62
63 ::GetSystemTime(&m_savedTime);
64 ::GetTimeZoneInformation(&m_tzi);
65
66 m_changed = ::SetSystemTime(&st) != 0;
67 }
68
69 ~DateChanger()
70 {
71 if ( m_changed )
72 {
73 ::SetSystemTime(&m_savedTime);
74 ::SetTimeZoneInformation(&m_tzi);
75 }
76 }
77
78private:
79 SYSTEMTIME m_savedTime;
80 TIME_ZONE_INFORMATION m_tzi;
81 bool m_changed;
82};
83
84#endif // CHANGE_SYSTEM_DATE
85
66f22f4a
VZ
86// helper function translating week day/month names from English to the current
87// locale
88static wxString TranslateDate(const wxString& str)
89{
90 // small optimization: if there are no alphabetic characters in the string,
91 // there is nothing to translate
92 wxString::const_iterator i, end = str.end();
93 for ( i = str.begin(); i != end; ++i )
94 {
95 if ( isalpha(*i) )
96 break;
97 }
98
99 if ( i == end )
100 return str;
101
102 wxString trans(str);
103
66f22f4a
VZ
104 for ( wxDateTime::WeekDay wd = wxDateTime::Sun;
105 wd < wxDateTime::Inv_WeekDay;
106 wxNextWDay(wd) )
107 {
108 trans.Replace
109 (
e538985e 110 wxDateTime::GetEnglishWeekDayName(wd, wxDateTime::Name_Abbr),
66f22f4a
VZ
111 wxDateTime::GetWeekDayName(wd, wxDateTime::Name_Abbr)
112 );
113 }
114
66f22f4a
VZ
115 for ( wxDateTime::Month mon = wxDateTime::Jan;
116 mon < wxDateTime::Inv_Month;
117 wxNextMonth(mon) )
118 {
119 trans.Replace
120 (
e538985e 121 wxDateTime::GetEnglishMonthName(mon, wxDateTime::Name_Abbr),
66f22f4a
VZ
122 wxDateTime::GetMonthName(mon, wxDateTime::Name_Abbr)
123 );
124 }
125
126 return trans;
127}
128
ccd6aacd
VZ
129// ----------------------------------------------------------------------------
130// broken down date representation used for testing
131// ----------------------------------------------------------------------------
132
133struct Date
134{
135 wxDateTime::wxDateTime_t day;
136 wxDateTime::Month month;
137 int year;
138 wxDateTime::wxDateTime_t hour, min, sec;
139 double jdn;
140 wxDateTime::WeekDay wday;
a669640b 141 time_t gmticks;
1bf29a7a 142
ccd6aacd
VZ
143 void Init(const wxDateTime::Tm& tm)
144 {
145 day = tm.mday;
146 month = tm.mon;
147 year = tm.year;
148 hour = tm.hour;
149 min = tm.min;
150 sec = tm.sec;
151 jdn = 0.0;
a669640b 152 gmticks = -1;
ccd6aacd
VZ
153 }
154
155 wxDateTime DT() const
156 { return wxDateTime(day, month, year, hour, min, sec); }
157
158 bool SameDay(const wxDateTime::Tm& tm) const
159 {
160 return day == tm.mday && month == tm.mon && year == tm.year;
161 }
162
163 wxString Format() const
164 {
165 wxString s;
9a83f860 166 s.Printf(wxT("%02d:%02d:%02d %10s %02d, %4d%s"),
ccd6aacd
VZ
167 hour, min, sec,
168 wxDateTime::GetMonthName(month).c_str(),
169 day,
170 abs(wxDateTime::ConvertYearToBC(year)),
9a83f860 171 year > 0 ? wxT("AD") : wxT("BC"));
ccd6aacd
VZ
172 return s;
173 }
174
175 wxString FormatDate() const
176 {
177 wxString s;
9a83f860 178 s.Printf(wxT("%02d-%s-%4d%s"),
ccd6aacd
VZ
179 day,
180 wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(),
181 abs(wxDateTime::ConvertYearToBC(year)),
9a83f860 182 year > 0 ? wxT("AD") : wxT("BC"));
ccd6aacd
VZ
183 return s;
184 }
185};
186
187// ----------------------------------------------------------------------------
188// test data
189// ----------------------------------------------------------------------------
190
191static const Date testDates[] =
192{
a669640b
VZ
193 { 1, wxDateTime::Jan, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu, 0 },
194 { 7, wxDateTime::Feb, 2036, 00, 00, 00, 2464730.5, wxDateTime::Thu, -1 },
195 { 8, wxDateTime::Feb, 2036, 00, 00, 00, 2464731.5, wxDateTime::Fri, -1 },
196 { 1, wxDateTime::Jan, 2037, 00, 00, 00, 2465059.5, wxDateTime::Thu, -1 },
197 { 1, wxDateTime::Jan, 2038, 00, 00, 00, 2465424.5, wxDateTime::Fri, -1 },
198 { 21, wxDateTime::Jan, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon, -1 },
199 { 29, wxDateTime::May, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat, 202219200 },
200 { 29, wxDateTime::Feb, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun, 194400000 },
201 { 1, wxDateTime::Jan, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon, -1 },
202 { 1, wxDateTime::Jan, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon, -1 },
203 { 15, wxDateTime::Oct, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri, -1 },
204 { 4, wxDateTime::Oct, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon, -1 },
205 { 1, wxDateTime::Mar, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu, -1 },
206 { 1, wxDateTime::Jan, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon, -1 },
207 { 31, wxDateTime::Dec, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun, -1 },
208 { 1, wxDateTime::Jan, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat, -1 },
209 { 12, wxDateTime::Aug, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri, -1 },
210 { 12, wxDateTime::Aug, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat, -1 },
211 { 24, wxDateTime::Nov, -4713, 00, 00, 00, -0.5, wxDateTime::Mon, -1 },
ccd6aacd
VZ
212};
213
214
215// ----------------------------------------------------------------------------
216// test class
217// ----------------------------------------------------------------------------
218
219class DateTimeTestCase : public CppUnit::TestCase
220{
221public:
222 DateTimeTestCase() { }
223
224private:
225 CPPUNIT_TEST_SUITE( DateTimeTestCase );
226 CPPUNIT_TEST( TestLeapYears );
227 CPPUNIT_TEST( TestTimeSet );
228 CPPUNIT_TEST( TestTimeJDN );
229 CPPUNIT_TEST( TestTimeWNumber );
230 CPPUNIT_TEST( TestTimeWDays );
231 CPPUNIT_TEST( TestTimeDST );
232 CPPUNIT_TEST( TestTimeFormat );
1bf29a7a 233 CPPUNIT_TEST( TestTimeSpanFormat );
ccd6aacd 234 CPPUNIT_TEST( TestTimeTicks );
8605eb1a
VZ
235 CPPUNIT_TEST( TestParceRFC822 );
236 CPPUNIT_TEST( TestDateParse );
f3f2e255 237 CPPUNIT_TEST( TestDateParseISO );
804eeca5 238 CPPUNIT_TEST( TestDateTimeParse );
ccd6aacd 239 CPPUNIT_TEST( TestTimeArithmetics );
a480d0ab 240 CPPUNIT_TEST( TestDSTBug );
fb96cf85 241 CPPUNIT_TEST( TestDateOnly );
ccd6aacd
VZ
242 CPPUNIT_TEST_SUITE_END();
243
244 void TestLeapYears();
245 void TestTimeSet();
246 void TestTimeJDN();
247 void TestTimeWNumber();
248 void TestTimeWDays();
249 void TestTimeDST();
250 void TestTimeFormat();
1bf29a7a 251 void TestTimeSpanFormat();
ccd6aacd 252 void TestTimeTicks();
8605eb1a
VZ
253 void TestParceRFC822();
254 void TestDateParse();
f3f2e255 255 void TestDateParseISO();
804eeca5 256 void TestDateTimeParse();
ccd6aacd 257 void TestTimeArithmetics();
a480d0ab 258 void TestDSTBug();
fb96cf85 259 void TestDateOnly();
ccd6aacd
VZ
260
261 DECLARE_NO_COPY_CLASS(DateTimeTestCase)
262};
263
264// register in the unnamed registry so that these tests are run by default
265CPPUNIT_TEST_SUITE_REGISTRATION( DateTimeTestCase );
266
267// also include in it's own registry so that these tests can be run alone
268CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DateTimeTestCase, "DateTimeTestCase" );
269
270// ============================================================================
271// implementation
272// ============================================================================
273
274// test leap years detection
275void DateTimeTestCase::TestLeapYears()
276{
277 static const struct LeapYearTestData
278 {
279 int year;
280 bool isLeap;
281 } years[] =
282 {
283 { 1900, false },
284 { 1990, false },
285 { 1976, true },
286 { 2000, true },
287 { 2030, false },
288 { 1984, true },
289 { 2100, false },
290 { 2400, true },
291 };
292
293 for ( size_t n = 0; n < WXSIZEOF(years); n++ )
294 {
295 const LeapYearTestData& y = years[n];
296
15cb637c 297 CPPUNIT_ASSERT_EQUAL( y.isLeap, wxDateTime::IsLeapYear(y.year) );
ccd6aacd
VZ
298 }
299}
300
301// test constructing wxDateTime objects
302void DateTimeTestCase::TestTimeSet()
303{
304 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
305 {
306 const Date& d1 = testDates[n];
307 wxDateTime dt = d1.DT();
308
309 Date d2;
310 d2.Init(dt.GetTm());
311
312 wxString s1 = d1.Format(),
313 s2 = d2.Format();
314
15cb637c 315 CPPUNIT_ASSERT_EQUAL( s1, s2 );
ccd6aacd
VZ
316 }
317}
318
319// test conversions to JDN &c
320void DateTimeTestCase::TestTimeJDN()
321{
322 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
323 {
324 const Date& d = testDates[n];
325 wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
d26adb9d
VZ
326
327 // JDNs must be computed for UTC times
328 double jdn = dt.FromUTC().GetJulianDayNumber();
ccd6aacd 329
93a800a9 330 CPPUNIT_ASSERT_EQUAL( d.jdn, jdn );
e3559425
VZ
331
332 dt.Set(jdn);
93a800a9 333 CPPUNIT_ASSERT_EQUAL( jdn, dt.GetJulianDayNumber() );
ccd6aacd
VZ
334 }
335}
336
337// test week days computation
338void DateTimeTestCase::TestTimeWDays()
339{
340 // test GetWeekDay()
341 size_t n;
342 for ( n = 0; n < WXSIZEOF(testDates); n++ )
343 {
344 const Date& d = testDates[n];
345 wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
346
347 wxDateTime::WeekDay wday = dt.GetWeekDay();
15cb637c 348 CPPUNIT_ASSERT_EQUAL( d.wday, wday );
ccd6aacd
VZ
349 }
350
351 // test SetToWeekDay()
352 struct WeekDateTestData
353 {
354 Date date; // the real date (precomputed)
355 int nWeek; // its week index in the month
356 wxDateTime::WeekDay wday; // the weekday
357 wxDateTime::Month month; // the month
358 int year; // and the year
359
360 wxString Format() const
361 {
362 wxString s, which;
363 switch ( nWeek < -1 ? -nWeek : nWeek )
364 {
9a83f860
VZ
365 case 1: which = wxT("first"); break;
366 case 2: which = wxT("second"); break;
367 case 3: which = wxT("third"); break;
368 case 4: which = wxT("fourth"); break;
369 case 5: which = wxT("fifth"); break;
ccd6aacd 370
9a83f860 371 case -1: which = wxT("last"); break;
ccd6aacd
VZ
372 }
373
374 if ( nWeek < -1 )
375 {
9a83f860 376 which += wxT(" from end");
ccd6aacd
VZ
377 }
378
9a83f860 379 s.Printf(wxT("The %s %s of %s in %d"),
ccd6aacd
VZ
380 which.c_str(),
381 wxDateTime::GetWeekDayName(wday).c_str(),
382 wxDateTime::GetMonthName(month).c_str(),
383 year);
384
385 return s;
386 }
387 };
388
389 // the array data was generated by the following python program
390 /*
391from DateTime import *
392from whrandom import *
393from string import *
394
395monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
396wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
397
398week = DateTimeDelta(7)
399
400for n in range(20):
401 year = randint(1900, 2100)
402 month = randint(1, 12)
403 day = randint(1, 28)
404 dt = DateTime(year, month, day)
405 wday = dt.day_of_week
406
407 countFromEnd = choice([-1, 1])
408 weekNum = 0;
409
410 while dt.month is month:
411 dt = dt - countFromEnd * week
412 weekNum = weekNum + countFromEnd
413
414 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
415
416 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
417 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
418 */
419
420 static const WeekDateTestData weekDatesTestData[] =
421 {
a669640b
VZ
422 { { 20, wxDateTime::Mar, 2045, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 3, wxDateTime::Mon, wxDateTime::Mar, 2045 },
423 { { 5, wxDateTime::Jun, 1985, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -4, wxDateTime::Wed, wxDateTime::Jun, 1985 },
424 { { 12, wxDateTime::Nov, 1961, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -3, wxDateTime::Sun, wxDateTime::Nov, 1961 },
425 { { 27, wxDateTime::Feb, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -1, wxDateTime::Fri, wxDateTime::Feb, 2093 },
426 { { 4, wxDateTime::Jul, 2070, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -4, wxDateTime::Fri, wxDateTime::Jul, 2070 },
427 { { 2, wxDateTime::Apr, 1906, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -5, wxDateTime::Mon, wxDateTime::Apr, 1906 },
428 { { 19, wxDateTime::Jul, 2023, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -2, wxDateTime::Wed, wxDateTime::Jul, 2023 },
429 { { 5, wxDateTime::May, 1958, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -4, wxDateTime::Mon, wxDateTime::May, 1958 },
430 { { 11, wxDateTime::Aug, 1900, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 2, wxDateTime::Sat, wxDateTime::Aug, 1900 },
431 { { 14, wxDateTime::Feb, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 2, wxDateTime::Wed, wxDateTime::Feb, 1945 },
432 { { 25, wxDateTime::Jul, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -1, wxDateTime::Tue, wxDateTime::Jul, 1967 },
433 { { 9, wxDateTime::May, 1916, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -4, wxDateTime::Tue, wxDateTime::May, 1916 },
434 { { 20, wxDateTime::Jun, 1927, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 3, wxDateTime::Mon, wxDateTime::Jun, 1927 },
435 { { 2, wxDateTime::Aug, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, wxDateTime::Wed, wxDateTime::Aug, 2000 },
436 { { 20, wxDateTime::Apr, 2044, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 3, wxDateTime::Wed, wxDateTime::Apr, 2044 },
437 { { 20, wxDateTime::Feb, 1932, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -2, wxDateTime::Sat, wxDateTime::Feb, 1932 },
438 { { 25, wxDateTime::Jul, 2069, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 4, wxDateTime::Thu, wxDateTime::Jul, 2069 },
439 { { 3, wxDateTime::Apr, 1925, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, wxDateTime::Fri, wxDateTime::Apr, 1925 },
440 { { 21, wxDateTime::Mar, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 3, wxDateTime::Sat, wxDateTime::Mar, 2093 },
441 { { 3, wxDateTime::Dec, 2074, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -5, wxDateTime::Mon, wxDateTime::Dec, 2074 }
ccd6aacd
VZ
442 };
443
ccd6aacd
VZ
444 wxDateTime dt;
445 for ( n = 0; n < WXSIZEOF(weekDatesTestData); n++ )
446 {
447 const WeekDateTestData& wd = weekDatesTestData[n];
448
449 dt.SetToWeekDay(wd.wday, wd.nWeek, wd.month, wd.year);
450
451 const Date& d = wd.date;
452 CPPUNIT_ASSERT( d.SameDay(dt.GetTm()) );
453 }
454}
455
456// test the computation of (ISO) week numbers
457void DateTimeTestCase::TestTimeWNumber()
458{
459 struct WeekNumberTestData
460 {
461 Date date; // the date
462 wxDateTime::wxDateTime_t week; // the week number in the year
463 wxDateTime::wxDateTime_t wmon; // the week number in the month
464 wxDateTime::wxDateTime_t wmon2; // same but week starts with Sun
465 wxDateTime::wxDateTime_t dnum; // day number in the year
466 };
467
468 // data generated with the following python script:
469 /*
470from DateTime import *
471from whrandom import *
472from string import *
473
474monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
475wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
476
477def GetMonthWeek(dt):
478 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
479 if weekNumMonth < 0:
480 weekNumMonth = weekNumMonth + 53
481 return weekNumMonth
482
483def GetLastSundayBefore(dt):
484 if dt.iso_week[2] == 7:
485 return dt
486 else:
487 return dt - DateTimeDelta(dt.iso_week[2])
488
489for n in range(20):
490 year = randint(1900, 2100)
491 month = randint(1, 12)
492 day = randint(1, 28)
493 dt = DateTime(year, month, day)
494 dayNum = dt.day_of_year
495 weekNum = dt.iso_week[1]
496 weekNumMonth = GetMonthWeek(dt)
497
498 weekNumMonth2 = 0
499 dtSunday = GetLastSundayBefore(dt)
500
501 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
502 weekNumMonth2 = weekNumMonth2 + 1
503 dtSunday = dtSunday - DateTimeDelta(7)
504
505 data = { 'day': rjust(`day`, 2), \
506 'month': monthNames[month - 1], \
507 'year': year, \
508 'weekNum': rjust(`weekNum`, 2), \
509 'weekNumMonth': weekNumMonth, \
510 'weekNumMonth2': weekNumMonth2, \
511 'dayNum': rjust(`dayNum`, 3) }
512
513 print " { { %(day)s, "\
514 "wxDateTime::%(month)s, "\
515 "%(year)d }, "\
516 "%(weekNum)s, "\
517 "%(weekNumMonth)s, "\
518 "%(weekNumMonth2)s, "\
519 "%(dayNum)s }," % data
520
521 */
522 static const WeekNumberTestData weekNumberTestDates[] =
523 {
a669640b
VZ
524 { { 27, wxDateTime::Dec, 1966, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 52, 5, 5, 361 },
525 { { 22, wxDateTime::Jul, 1926, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 29, 4, 4, 203 },
526 { { 22, wxDateTime::Oct, 2076, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 43, 4, 4, 296 },
527 { { 1, wxDateTime::Jul, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 26, 1, 1, 182 },
528 { { 8, wxDateTime::Nov, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 46, 2, 2, 313 },
529 { { 21, wxDateTime::Mar, 1920, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 12, 3, 4, 81 },
530 { { 7, wxDateTime::Jan, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 2, 2, 7 },
531 { { 19, wxDateTime::Oct, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 42, 4, 4, 292 },
532 { { 13, wxDateTime::Aug, 1955, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 32, 2, 2, 225 },
533 { { 18, wxDateTime::Jul, 2087, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 29, 3, 3, 199 },
534 { { 2, wxDateTime::Sep, 2028, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 35, 1, 1, 246 },
535 { { 28, wxDateTime::Jul, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 30, 5, 4, 209 },
536 { { 15, wxDateTime::Jun, 1901, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 24, 3, 3, 166 },
537 { { 10, wxDateTime::Oct, 1939, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 41, 3, 2, 283 },
538 { { 3, wxDateTime::Dec, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 48, 1, 1, 337 },
539 { { 23, wxDateTime::Feb, 1940, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 8, 4, 4, 54 },
540 { { 2, wxDateTime::Jan, 1987, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 1, 1, 2 },
541 { { 11, wxDateTime::Aug, 2079, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 32, 2, 2, 223 },
542 { { 2, wxDateTime::Feb, 2063, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 5, 1, 1, 33 },
543 { { 16, wxDateTime::Oct, 1942, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 42, 3, 3, 289 },
544 { { 30, wxDateTime::Dec, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 5, 5, 364 },
545 { { 2, wxDateTime::Jan, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 1, 1, 2 },
404013f8
VZ
546 { { 5, wxDateTime::Jan, 2010, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 2, 2, 5 },
547 { { 3, wxDateTime::Jan, 2011, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 2, 2, 3 },
ccd6aacd
VZ
548 };
549
550 for ( size_t n = 0; n < WXSIZEOF(weekNumberTestDates); n++ )
551 {
552 const WeekNumberTestData& wn = weekNumberTestDates[n];
553 const Date& d = wn.date;
554
555 wxDateTime dt = d.DT();
556
557 wxDateTime::wxDateTime_t
558 week = dt.GetWeekOfYear(wxDateTime::Monday_First),
559 wmon = dt.GetWeekOfMonth(wxDateTime::Monday_First),
560 wmon2 = dt.GetWeekOfMonth(wxDateTime::Sunday_First),
561 dnum = dt.GetDayOfYear();
562
404013f8
VZ
563 WX_ASSERT_EQUAL_MESSAGE( ("day of year for %s", d.Format()),
564 wn.dnum, dnum );
565 WX_ASSERT_EQUAL_MESSAGE( ("week of month (Monday) for %s", d.Format()),
566 wn.wmon, wmon );
567 WX_ASSERT_EQUAL_MESSAGE( ("week of month (Sunday) for %s", d.Format()),
568 wn.wmon2, wmon2 );
569 WX_ASSERT_EQUAL_MESSAGE( ("week of year for %s", d.Format()),
570 wn.week, week );
ccd6aacd 571
4c27e2fa
VZ
572 int year = d.year;
573 if ( week == 1 && d.month != wxDateTime::Jan )
574 {
575 // this means we're in the first week of the next year
576 year++;
577 }
578
579 wxDateTime
580 dt2 = wxDateTime::SetToWeekOfYear(year, week, dt.GetWeekDay());
15cb637c 581 CPPUNIT_ASSERT_EQUAL( dt, dt2 );
ccd6aacd
VZ
582 }
583}
584
585// test DST applicability
586void DateTimeTestCase::TestTimeDST()
587{
588 // taken from http://www.energy.ca.gov/daylightsaving.html
6b522db5 589 static const Date datesDST[2][2009 - 1990 + 1] =
ccd6aacd
VZ
590 {
591 {
a669640b
VZ
592 { 1, wxDateTime::Apr, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
593 { 7, wxDateTime::Apr, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
594 { 5, wxDateTime::Apr, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
595 { 4, wxDateTime::Apr, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
596 { 3, wxDateTime::Apr, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
597 { 2, wxDateTime::Apr, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
598 { 7, wxDateTime::Apr, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
599 { 6, wxDateTime::Apr, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
600 { 5, wxDateTime::Apr, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
601 { 4, wxDateTime::Apr, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
602 { 2, wxDateTime::Apr, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
603 { 1, wxDateTime::Apr, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
604 { 7, wxDateTime::Apr, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
605 { 6, wxDateTime::Apr, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
606 { 4, wxDateTime::Apr, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
6b522db5
VZ
607 { 3, wxDateTime::Apr, 2005, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
608 { 2, wxDateTime::Apr, 2006, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
609 {11, wxDateTime::Mar, 2007, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
610 { 9, wxDateTime::Mar, 2008, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
611 { 8, wxDateTime::Mar, 2009, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
ccd6aacd
VZ
612 },
613 {
a669640b
VZ
614 { 28, wxDateTime::Oct, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
615 { 27, wxDateTime::Oct, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
616 { 25, wxDateTime::Oct, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
617 { 31, wxDateTime::Oct, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
618 { 30, wxDateTime::Oct, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
619 { 29, wxDateTime::Oct, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
620 { 27, wxDateTime::Oct, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
621 { 26, wxDateTime::Oct, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
622 { 25, wxDateTime::Oct, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
623 { 31, wxDateTime::Oct, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
624 { 29, wxDateTime::Oct, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
625 { 28, wxDateTime::Oct, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
626 { 27, wxDateTime::Oct, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
627 { 26, wxDateTime::Oct, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
628 { 31, wxDateTime::Oct, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
6b522db5
VZ
629 { 30, wxDateTime::Oct, 2005, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
630 { 29, wxDateTime::Oct, 2006, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
631 { 4, wxDateTime::Nov, 2007, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
632 { 2, wxDateTime::Nov, 2008, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
633 { 1, wxDateTime::Nov, 2009, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
634
ccd6aacd
VZ
635 }
636 };
637
6b522db5 638 for ( size_t n = 0; n < WXSIZEOF(datesDST[0]); n++ )
ccd6aacd 639 {
6b522db5 640 const int year = 1990 + n;
ccd6aacd
VZ
641 wxDateTime dtBegin = wxDateTime::GetBeginDST(year, wxDateTime::USA),
642 dtEnd = wxDateTime::GetEndDST(year, wxDateTime::USA);
643
ccd6aacd
VZ
644 const Date& dBegin = datesDST[0][n];
645 const Date& dEnd = datesDST[1][n];
646
6b522db5
VZ
647 CPPUNIT_ASSERT_EQUAL( dBegin.DT().FormatDate(), dtBegin.FormatDate() );
648 CPPUNIT_ASSERT_EQUAL( dEnd.DT().FormatDate(), dtEnd.FormatDate() );
ccd6aacd
VZ
649 }
650}
651
652// test wxDateTime -> text conversion
653void DateTimeTestCase::TestTimeFormat()
654{
655 // some information may be lost during conversion, so store what kind
656 // of info should we recover after a round trip
657 enum CompareKind
658 {
659 CompareNone, // don't try comparing
660 CompareBoth, // dates and times should be identical
9807f995 661 CompareYear, // don't compare centuries (fails for 2 digit years)
ccd6aacd
VZ
662 CompareDate, // dates only
663 CompareTime // time only
664 };
665
666 static const struct
667 {
668 CompareKind compareKind;
71ebd60b 669 const char *format;
ccd6aacd
VZ
670 } formatTestFormats[] =
671 {
71ebd60b
VZ
672 { CompareYear, "---> %c" }, // %c could use 2 digit years
673 { CompareDate, "Date is %A, %d of %B, in year %Y" },
674 { CompareYear, "Date is %x, time is %X" }, // %x could use 2 digits
675 { CompareTime, "Time is %H:%M:%S or %I:%M:%S %p" },
676 { CompareNone, "The day of year: %j, the week of year: %W" },
677 { CompareDate, "ISO date without separators: %Y%m%d" },
ccd6aacd
VZ
678 };
679
680 static const Date formatTestDates[] =
681 {
9807f995
VZ
682 { 29, wxDateTime::May, 1976, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
683 { 31, wxDateTime::Dec, 1999, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
684 { 6, wxDateTime::Feb, 1937, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
685 { 6, wxDateTime::Feb, 1856, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
686 { 6, wxDateTime::Feb, 1857, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
687 { 29, wxDateTime::May, 2076, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
89a7e1ff
VZ
688
689 // FIXME: the test with 02:15:25 time doesn't pass because of DST
690 // computation problems, we get back 03:15:25
691 { 29, wxDateTime::Feb, 2400, 04, 15, 25, 0.0, wxDateTime::Inv_WeekDay },
ccd6aacd 692#if 0
42b62acf 693 // Need to add support for BCE dates.
9807f995 694 { 01, wxDateTime::Jan, -52, 03, 16, 47, 0.0, wxDateTime::Inv_WeekDay },
ccd6aacd
VZ
695#endif
696 };
697
9807f995 698 for ( size_t d = 0; d < WXSIZEOF(formatTestDates); d++ )
ccd6aacd 699 {
9807f995 700 wxDateTime dt = formatTestDates[d].DT();
2e6c4a57 701 for ( unsigned n = 0; n < WXSIZEOF(formatTestFormats); n++ )
ccd6aacd 702 {
71ebd60b 703 const char *fmt = formatTestFormats[n].format;
3aa3163f 704
a6121fc4
FM
705 // skip the check with %p for those locales which have empty AM/PM strings:
706 // for those locales it's impossible to pass the test with %p...
707 wxString am, pm;
708 wxDateTime::GetAmPmStrings(&am, &pm);
709 if (am.empty() && pm.empty() && wxStrstr(fmt, "%p") != NULL)
710 continue;
711
9807f995 712 wxString s = dt.Format(fmt);
1bf29a7a 713
ccd6aacd 714 // what can we recover?
9807f995 715 CompareKind kind = formatTestFormats[n].compareKind;
ccd6aacd
VZ
716
717 // convert back
718 wxDateTime dt2;
71ebd60b 719 const char *result = dt2.ParseFormat(s, fmt);
ccd6aacd
VZ
720 if ( !result )
721 {
b5f85206 722 // conversion failed - should it have?
2e6c4a57 723 WX_ASSERT_MESSAGE(
38914d5a 724 ("Test #%u failed: failed to parse \"%s\"", n, s),
2e6c4a57
VZ
725 kind == CompareNone
726 );
ccd6aacd
VZ
727 }
728 else // conversion succeeded
729 {
dc310ea0
VZ
730 // currently ParseFormat() doesn't support "%Z" and so is
731 // incapable of parsing time zone part used at the end of date
732 // representations in many (but not "C") locales, compensate
733 // for it ourselves by simply consuming and ignoring it
734 while ( *result && (*result >= 'A' && *result <= 'Z') )
735 result++;
736
2e6c4a57 737 WX_ASSERT_MESSAGE(
38914d5a 738 ("Test #%u failed: \"%s\" was left unparsed in \"%s\"",
2e6c4a57
VZ
739 n, result, s),
740 !*result
741 );
ccd6aacd
VZ
742
743 switch ( kind )
744 {
9807f995
VZ
745 case CompareYear:
746 if ( dt2.GetCentury() != dt.GetCentury() )
747 {
748 CPPUNIT_ASSERT_EQUAL(dt.GetYear() % 100,
749 dt2.GetYear() % 100);
750
751 dt2.SetYear(dt.GetYear());
752 }
753 // fall through and compare everything
754
ccd6aacd 755 case CompareBoth:
9807f995 756 CPPUNIT_ASSERT_EQUAL( dt, dt2 );
ccd6aacd
VZ
757 break;
758
759 case CompareDate:
760 CPPUNIT_ASSERT( dt.IsSameDate(dt2) );
761 break;
762
763 case CompareTime:
764 CPPUNIT_ASSERT( dt.IsSameTime(dt2) );
765 break;
d581cac5
VZ
766
767 case CompareNone:
9a83f860 768 wxFAIL_MSG( wxT("unexpected") );
d581cac5 769 break;
ccd6aacd
VZ
770 }
771 }
772 }
773 }
425eee41 774
b5f85206
VZ
775 wxDateTime dt;
776
89a7e1ff
VZ
777#if 0
778 // special case which was known to fail
779 CPPUNIT_ASSERT( dt.ParseFormat("02/06/1856", "%x") );
780 CPPUNIT_ASSERT_EQUAL( 1856, dt.GetYear() );
781#endif
782
52256b21
VZ
783 // also test %l separately
784 CPPUNIT_ASSERT( dt.ParseFormat("12:23:45.678", "%H:%M:%S.%l") );
785 CPPUNIT_ASSERT_EQUAL( 678, dt.GetMillisecond() );
786
787 // test special case of %l matching 0 milliseconds
788 CPPUNIT_ASSERT( dt.ParseFormat("12:23:45.000", "%H:%M:%S.%l") );
789 CPPUNIT_ASSERT_EQUAL( 0, dt.GetMillisecond() );
790
b5f85206
VZ
791 // test partially specified dates too
792 wxDateTime dtDef(26, wxDateTime::Sep, 2008);
793 CPPUNIT_ASSERT( dt.ParseFormat("17", "%d") );
794 CPPUNIT_ASSERT_EQUAL( 17, dt.GetDay() );
795
425eee41
VZ
796 // test compilation of some calls which should compile (and not result in
797 // ambiguity because of char*<->wxCStrData<->wxString conversions)
425eee41
VZ
798 wxString s("foo");
799 CPPUNIT_ASSERT( !dt.ParseFormat("foo") );
800 CPPUNIT_ASSERT( !dt.ParseFormat(wxT("foo")) );
801 CPPUNIT_ASSERT( !dt.ParseFormat(s) );
802 CPPUNIT_ASSERT( !dt.ParseFormat(s.c_str()) );
803
804 CPPUNIT_ASSERT( !dt.ParseFormat("foo", "%c") );
805 CPPUNIT_ASSERT( !dt.ParseFormat(wxT("foo"), "%c") );
806 CPPUNIT_ASSERT( !dt.ParseFormat(s, "%c") );
807 CPPUNIT_ASSERT( !dt.ParseFormat(s.c_str(), "%c") );
808
809 CPPUNIT_ASSERT( !dt.ParseFormat("foo", wxT("%c")) );
810 CPPUNIT_ASSERT( !dt.ParseFormat(wxT("foo"), wxT("%c")) );
811 CPPUNIT_ASSERT( !dt.ParseFormat(s, "%c") );
812 CPPUNIT_ASSERT( !dt.ParseFormat(s.c_str(), wxT("%c")) );
813
814 wxString spec("%c");
815 CPPUNIT_ASSERT( !dt.ParseFormat("foo", spec) );
816 CPPUNIT_ASSERT( !dt.ParseFormat(wxT("foo"), spec) );
817 CPPUNIT_ASSERT( !dt.ParseFormat(s, spec) );
818 CPPUNIT_ASSERT( !dt.ParseFormat(s.c_str(), spec) );
ccd6aacd
VZ
819}
820
1bf29a7a
VZ
821void DateTimeTestCase::TestTimeSpanFormat()
822{
823 static const struct TimeSpanFormatTestData
824 {
825 long h, min, sec, msec;
71ebd60b
VZ
826 const char *fmt;
827 const char *result;
1bf29a7a
VZ
828 } testSpans[] =
829 {
71ebd60b
VZ
830 { 12, 34, 56, 789, "%H:%M:%S.%l", "12:34:56.789" },
831 { 1, 2, 3, 0, "%H:%M:%S", "01:02:03" },
832 { 1, 2, 3, 0, "%S", "3723" },
833 { -1, -2, -3, 0, "%S", "-3723" },
834 { -1, -2, -3, 0, "%H:%M:%S", "-01:02:03" },
835 { 26, 0, 0, 0, "%H", "26" },
836 { 26, 0, 0, 0, "%D, %H", "1, 02" },
837 { -26, 0, 0, 0, "%H", "-26" },
838 { -26, 0, 0, 0, "%D, %H", "-1, 02" },
839 { 219, 0, 0, 0, "%H", "219" },
840 { 219, 0, 0, 0, "%D, %H", "9, 03" },
841 { 219, 0, 0, 0, "%E, %D, %H", "1, 2, 03" },
c9e46dea
VZ
842 { 0, -1, 0, 0, "%H:%M:%S", "-00:01:00" },
843 { 0, 0, -1, 0, "%H:%M:%S", "-00:00:01" },
1bf29a7a
VZ
844 };
845
846 for ( size_t n = 0; n < WXSIZEOF(testSpans); n++ )
847 {
848 const TimeSpanFormatTestData& td = testSpans[n];
849 wxTimeSpan ts(td.h, td.min, td.sec, td.msec);
c9e46dea 850 CPPUNIT_ASSERT_EQUAL( td.result, ts.Format(td.fmt) );
1bf29a7a
VZ
851 }
852}
853
ccd6aacd
VZ
854void DateTimeTestCase::TestTimeTicks()
855{
a669640b
VZ
856 static const wxDateTime::TimeZone TZ_LOCAL(wxDateTime::Local);
857 static const wxDateTime::TimeZone TZ_TEST(wxDateTime::NZST);
c4e25288 858
a669640b
VZ
859 // this offset is needed to make the test work in any time zone when we
860 // only have expected test results in UTC in testDates
861 static const long tzOffset = TZ_LOCAL.GetOffset() - TZ_TEST.GetOffset();
c4e25288 862
ccd6aacd
VZ
863 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
864 {
865 const Date& d = testDates[n];
a669640b 866 if ( d.gmticks == -1 )
ccd6aacd
VZ
867 continue;
868
a669640b 869 wxDateTime dt = d.DT().MakeTimezone(TZ_TEST, true /* no DST */);
c4e25288 870
a669640b
VZ
871 // GetValue() returns internal UTC-based representation, we need to
872 // convert it to local TZ before comparing
ece97e28 873 time_t ticks = (dt.GetValue() / 1000).ToLong() + TZ_LOCAL.GetOffset();
a669640b
VZ
874 if ( dt.IsDST() )
875 ticks += 3600;
1de532f5 876 CPPUNIT_ASSERT_EQUAL( d.gmticks, ticks + tzOffset );
ccd6aacd 877
c4e25288 878 dt = d.DT().FromTimezone(wxDateTime::UTC);
ccd6aacd 879 ticks = (dt.GetValue() / 1000).ToLong();
1de532f5 880 CPPUNIT_ASSERT_EQUAL( d.gmticks, ticks );
ccd6aacd
VZ
881 }
882}
883
8605eb1a
VZ
884// test parsing dates in RFC822 format
885void DateTimeTestCase::TestParceRFC822()
ccd6aacd
VZ
886{
887 static const struct ParseTestData
888 {
71ebd60b 889 const char *rfc822;
d26adb9d 890 Date date; // NB: this should be in UTC
ccd6aacd
VZ
891 bool good;
892 } parseTestDates[] =
893 {
d26adb9d 894 {
71ebd60b 895 "Sat, 18 Dec 1999 00:46:40 +0100",
1cf57808 896 { 17, wxDateTime::Dec, 1999, 23, 46, 40 },
d26adb9d
VZ
897 true
898 },
899 {
71ebd60b 900 "Wed, 1 Dec 1999 05:17:20 +0300",
1cf57808 901 { 1, wxDateTime::Dec, 1999, 2, 17, 20 },
d26adb9d
VZ
902 true
903 },
904 {
71ebd60b 905 "Sun, 28 Aug 2005 03:31:30 +0200",
cf9f6737 906 { 28, wxDateTime::Aug, 2005, 1, 31, 30 },
1cf57808
VZ
907 true
908 },
909
910 {
71ebd60b 911 "Sat, 18 Dec 1999 10:48:30 -0500",
cf9f6737 912 { 18, wxDateTime::Dec, 1999, 15, 48, 30 },
d26adb9d
VZ
913 true
914 },
cf9f6737
VZ
915
916 // seconds are optional according to the RFC
917 {
918 "Sun, 01 Jun 2008 16:30 +0200",
919 { 1, wxDateTime::Jun, 2008, 14, 30, 00 },
920 true
921 },
922
923 // try some bogus ones too
924 {
925 "Sun, 01 Jun 2008 16:30: +0200",
926 { 0 },
927 false
928 },
ccd6aacd
VZ
929 };
930
b7c746d0 931 for ( unsigned n = 0; n < WXSIZEOF(parseTestDates); n++ )
ccd6aacd 932 {
71ebd60b 933 const char * const datestr = parseTestDates[n].rfc822;
b7c746d0 934
ccd6aacd 935 wxDateTime dt;
b7c746d0 936 if ( dt.ParseRfc822Date(datestr) )
ccd6aacd 937 {
b7c746d0
VZ
938 WX_ASSERT_MESSAGE(
939 ("Erroneously parsed \"%s\"", datestr),
940 parseTestDates[n].good
941 );
ccd6aacd 942
d26adb9d 943 wxDateTime dtReal = parseTestDates[n].date.DT().FromUTC();
8605eb1a
VZ
944 CPPUNIT_ASSERT_EQUAL( dtReal, dt );
945 }
946 else // failed to parse
947 {
b7c746d0
VZ
948 WX_ASSERT_MESSAGE(
949 ("Failed to parse \"%s\"", datestr),
950 !parseTestDates[n].good
951 );
8605eb1a
VZ
952 }
953 }
954}
955
956// test parsing dates in free format
957void DateTimeTestCase::TestDateParse()
958{
959 static const struct ParseTestData
960 {
71ebd60b 961 const char *str;
8605eb1a
VZ
962 Date date; // NB: this should be in UTC
963 bool good;
964 } parseTestDates[] =
965 {
71ebd60b
VZ
966 { "21 Mar 2006", { 21, wxDateTime::Mar, 2006 }, true },
967 { "29 Feb 1976", { 29, wxDateTime::Feb, 1976 }, true },
968 { "Feb 29 1976", { 29, wxDateTime::Feb, 1976 }, true },
969 { "31/03/06", { 31, wxDateTime::Mar, 6 }, true },
970 { "31/03/2006", { 31, wxDateTime::Mar, 2006 }, true },
8605eb1a
VZ
971
972 // some invalid ones too
71ebd60b
VZ
973 { "29 Feb 2006" },
974 { "31/04/06" },
975 { "bloordyblop" },
e4f54cce 976 { "2 . . " },
8605eb1a
VZ
977 };
978
979 // special cases
980 wxDateTime dt;
9a83f860 981 CPPUNIT_ASSERT( dt.ParseDate(wxT("today")) );
8605eb1a
VZ
982 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), dt );
983
984 for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
985 {
66f22f4a 986 const wxString datestr = TranslateDate(parseTestDates[n].str);
3aa3163f
VZ
987
988 const char * const end = dt.ParseDate(datestr);
989 if ( end && !*end )
8605eb1a 990 {
3aa3163f
VZ
991 WX_ASSERT_MESSAGE(
992 ("Erroneously parsed \"%s\"", datestr),
993 parseTestDates[n].good
994 );
8605eb1a 995
804eeca5
VZ
996 CPPUNIT_ASSERT_EQUAL( parseTestDates[n].date.DT(), dt );
997 }
998 else // failed to parse
999 {
3aa3163f
VZ
1000 WX_ASSERT_MESSAGE(
1001 ("Failed to parse \"%s\"", datestr),
1002 !parseTestDates[n].good
1003 );
804eeca5
VZ
1004 }
1005 }
1006}
1007
f3f2e255
VZ
1008void DateTimeTestCase::TestDateParseISO()
1009{
1010 static const struct
1011 {
1012 const char *str;
1013 Date date; // NB: this should be in UTC
1014 bool good;
1015 } parseTestDates[] =
1016 {
1017 { "2006-03-21", { 21, wxDateTime::Mar, 2006 }, true },
1018 { "1976-02-29", { 29, wxDateTime::Feb, 1976 }, true },
1019 { "0006-03-31", { 31, wxDateTime::Mar, 6 }, true },
1020
1021 // some invalid ones too
1022 { "2006:03:31" },
1023 { "31/04/06" },
1024 { "bloordyblop" },
1025 { "" },
1026 };
1027
1028 static const struct
1029 {
1030 const char *str;
1031 wxDateTime::wxDateTime_t hour, min, sec;
1032 bool good;
1033 } parseTestTimes[] =
1034 {
1035 { "13:42:17", 13, 42, 17, true },
1036 { "02:17:01", 2, 17, 1, true },
1037
1038 // some invalid ones too
1039 { "66:03:31" },
1040 { "31/04/06" },
1041 { "bloordyblop" },
1042 { "" },
1043 };
1044
1045 for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
1046 {
1047 wxDateTime dt;
1048 if ( dt.ParseISODate(parseTestDates[n].str) )
1049 {
1050 CPPUNIT_ASSERT( parseTestDates[n].good );
1051
1052 CPPUNIT_ASSERT_EQUAL( parseTestDates[n].date.DT(), dt );
1053
1054 for ( size_t m = 0; m < WXSIZEOF(parseTestTimes); m++ )
1055 {
1056 wxString dtCombined;
1057 dtCombined << parseTestDates[n].str
1058 << 'T'
1059 << parseTestTimes[m].str;
1060
1061 if ( dt.ParseISOCombined(dtCombined) )
1062 {
1063 CPPUNIT_ASSERT( parseTestTimes[m].good );
1064
1065 CPPUNIT_ASSERT_EQUAL( parseTestTimes[m].hour, dt.GetHour()) ;
1066 CPPUNIT_ASSERT_EQUAL( parseTestTimes[m].min, dt.GetMinute()) ;
1067 CPPUNIT_ASSERT_EQUAL( parseTestTimes[m].sec, dt.GetSecond()) ;
1068 }
1069 else // failed to parse combined date/time
1070 {
1071 CPPUNIT_ASSERT( !parseTestTimes[m].good );
1072 }
1073 }
1074 }
1075 else // failed to parse
1076 {
1077 CPPUNIT_ASSERT( !parseTestDates[n].good );
1078 }
1079 }
1080}
1081
804eeca5
VZ
1082void DateTimeTestCase::TestDateTimeParse()
1083{
1084 static const struct ParseTestData
1085 {
71ebd60b 1086 const char *str;
804eeca5
VZ
1087 Date date; // NB: this should be in UTC
1088 bool good;
1089 } parseTestDates[] =
1090 {
8b7d411f
VZ
1091 {
1092 "Thu 22 Nov 2007 07:40:00 PM",
595c23b3 1093 { 22, wxDateTime::Nov, 2007, 19, 40, 0 },
8b7d411f
VZ
1094 true
1095 },
1096
1097 {
1098 "2010-01-04 14:30",
595c23b3 1099 { 4, wxDateTime::Jan, 2010, 14, 30, 0 },
8b7d411f
VZ
1100 true
1101 },
1102
1103 {
1104 "bloordyblop",
595c23b3 1105 { 1, wxDateTime::Jan, 9999, 0, 0, 0},
8b7d411f
VZ
1106 false
1107 },
804eeca5
VZ
1108 };
1109
66f22f4a
VZ
1110 // the test strings here use "PM" which is not available in all locales so
1111 // we need to use "C" locale for them
34d4d286
VZ
1112 CLocaleSetter cloc;
1113
804eeca5
VZ
1114 wxDateTime dt;
1115 for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
1116 {
66f22f4a 1117 const wxString datestr = TranslateDate(parseTestDates[n].str);
3aa3163f 1118
66f22f4a
VZ
1119 const char * const end = dt.ParseDateTime(datestr);
1120 if ( end && !*end )
804eeca5 1121 {
3aa3163f
VZ
1122 WX_ASSERT_MESSAGE(
1123 ("Erroneously parsed \"%s\"", datestr),
1124 parseTestDates[n].good
1125 );
804eeca5 1126
8605eb1a 1127 CPPUNIT_ASSERT_EQUAL( parseTestDates[n].date.DT(), dt );
ccd6aacd
VZ
1128 }
1129 else // failed to parse
1130 {
3aa3163f
VZ
1131 WX_ASSERT_MESSAGE(
1132 ("Failed to parse \"%s\"", datestr),
1133 !parseTestDates[n].good
1134 );
1135
ccd6aacd
VZ
1136 CPPUNIT_ASSERT( !parseTestDates[n].good );
1137 }
1138 }
1139}
1140
1141void DateTimeTestCase::TestTimeArithmetics()
1142{
1143 static const wxDateSpan testArithmData[] =
1144 {
1145 wxDateSpan::Day(),
1146 wxDateSpan::Week(),
1147 wxDateSpan::Month(),
1148 wxDateSpan::Year(),
1149 };
1150
1151 // the test will *not* work with arbitrary date!
1152 wxDateTime dt(2, wxDateTime::Dec, 1999),
1153 dt1,
1154 dt2;
1155
1156 for ( size_t n = 0; n < WXSIZEOF(testArithmData); n++ )
1157 {
1158 const wxDateSpan& span = testArithmData[n];
1159 dt1 = dt + span;
1160 dt2 = dt - span;
1161
15cb637c
VZ
1162 CPPUNIT_ASSERT_EQUAL( dt, dt1 - span );
1163 CPPUNIT_ASSERT_EQUAL( dt, dt2 + span );
1164 CPPUNIT_ASSERT_EQUAL( dt1, dt2 + 2*span );
ccd6aacd
VZ
1165 }
1166}
1167
a480d0ab
VZ
1168void DateTimeTestCase::TestDSTBug()
1169{
1170 /////////////////////////
1171 // Test GetEndDST()
c6a95dd6 1172 wxDateTime dt = wxDateTime::GetEndDST(2004, wxDateTime::France);
a480d0ab
VZ
1173 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
1174 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
1175 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
770882a6 1176 CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
a480d0ab
VZ
1177 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
1178 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
1179 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
1180
1181 /////////////////////////
1182 // Test ResetTime()
1183 dt.SetHour(5);
1184 CPPUNIT_ASSERT_EQUAL(5, (int)dt.GetHour());
1185 dt.ResetTime();
1186 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
1187 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
1188 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
1189 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetHour());
1190 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
1191 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
1192 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
1193
fcae7135
VZ
1194 dt.Set(1, 0, 0, 0);
1195 CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
1196
a480d0ab
VZ
1197 /////////////////////////
1198 // Test Today()
1199#ifdef CHANGE_SYSTEM_DATE
1200 {
1201 DateChanger change(2004, 10, 31, 5, 0, 0);
1202 dt = wxDateTime::Today();
1203 }
1204
1205 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
1206 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
1207 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
1208 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetHour());
1209 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
1210 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
1211 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
1212
1213 /////////////////////////
1214 // Test Set(hour, minute, second, milli)
1215 wxDateTime dt2;
1216 {
1217 DateChanger change(2004, 10, 31, 5, 0, 0);
1218 dt.Set(1, 30, 0, 0);
1219 dt2.Set(5, 30, 0, 0);
1220 }
1221
1222 CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
1223 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
1224 CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
1225 CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
1226 CPPUNIT_ASSERT_EQUAL(30, (int)dt.GetMinute());
1227 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
1228 CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
1229
1230 CPPUNIT_ASSERT_EQUAL(31, (int)dt2.GetDay());
1231 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt2.GetMonth());
1232 CPPUNIT_ASSERT_EQUAL(2004, (int)dt2.GetYear());
1233 CPPUNIT_ASSERT_EQUAL(5, (int)dt2.GetHour());
1234 CPPUNIT_ASSERT_EQUAL(30, (int)dt2.GetMinute());
1235 CPPUNIT_ASSERT_EQUAL(0, (int)dt2.GetSecond());
1236 CPPUNIT_ASSERT_EQUAL(0, (int)dt2.GetMillisecond());
1237#endif // CHANGE_SYSTEM_DATE
1238}
1239
fb96cf85
VZ
1240void DateTimeTestCase::TestDateOnly()
1241{
1242 wxDateTime dt(19, wxDateTime::Jan, 2007, 15, 01, 00);
1243
1244 static const wxDateTime::wxDateTime_t DATE_ZERO = 0;
1245 CPPUNIT_ASSERT_EQUAL( DATE_ZERO, dt.GetDateOnly().GetHour() );
1246 CPPUNIT_ASSERT_EQUAL( DATE_ZERO, dt.GetDateOnly().GetMinute() );
1247 CPPUNIT_ASSERT_EQUAL( DATE_ZERO, dt.GetDateOnly().GetSecond() );
1248 CPPUNIT_ASSERT_EQUAL( DATE_ZERO, dt.GetDateOnly().GetMillisecond() );
1249
1250 dt.ResetTime();
1251 CPPUNIT_ASSERT_EQUAL( wxDateTime(19, wxDateTime::Jan, 2007), dt );
1252
1253 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), wxDateTime::Now().GetDateOnly() );
1254}
1255
34af0de4 1256#endif // wxUSE_DATETIME