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