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