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