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