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