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