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)
7 // Copyright: (c) 2004 Vadim Zeitlin <vadim@wxwindows.org>
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
25 #include "wx/datetime.h"
26 #include "wx/wxcrt.h" // for wxStrstr()
28 // need this to be able to use CPPUNIT_ASSERT_EQUAL with wxDateTime objects
29 static std::ostream
& operator<<(std::ostream
& ostr
, const wxDateTime
& dt
)
31 ostr
<< dt
.FormatISOCombined(' ');
36 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(wxDateTime::wxDateTime_t
)
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
44 #undef CHANGE_SYSTEM_DATE
47 #ifdef CHANGE_SYSTEM_DATE
52 DateChanger(int year
, int month
, int day
, int hour
, int min
, int sec
)
63 ::GetSystemTime(&m_savedTime
);
64 ::GetTimeZoneInformation(&m_tzi
);
66 m_changed
= ::SetSystemTime(&st
) != 0;
73 ::SetSystemTime(&m_savedTime
);
74 ::SetTimeZoneInformation(&m_tzi
);
79 SYSTEMTIME m_savedTime
;
80 TIME_ZONE_INFORMATION m_tzi
;
84 #endif // CHANGE_SYSTEM_DATE
86 // helper class setting the locale to "C" for its lifetime
90 CLocaleSetter() : m_locOld(setlocale(LC_ALL
, "C")) { }
91 ~CLocaleSetter() { setlocale(LC_ALL
, m_locOld
); }
94 const char * const m_locOld
;
95 wxDECLARE_NO_COPY_CLASS(CLocaleSetter
);
98 // helper function translating week day/month names from English to the current
100 static wxString
TranslateDate(const wxString
& str
)
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
)
116 for ( wxDateTime::WeekDay wd
= wxDateTime::Sun
;
117 wd
< wxDateTime::Inv_WeekDay
;
122 wxDateTime::GetEnglishWeekDayName(wd
, wxDateTime::Name_Abbr
),
123 wxDateTime::GetWeekDayName(wd
, wxDateTime::Name_Abbr
)
127 for ( wxDateTime::Month mon
= wxDateTime::Jan
;
128 mon
< wxDateTime::Inv_Month
;
133 wxDateTime::GetEnglishMonthName(mon
, wxDateTime::Name_Abbr
),
134 wxDateTime::GetMonthName(mon
, wxDateTime::Name_Abbr
)
141 // ----------------------------------------------------------------------------
142 // broken down date representation used for testing
143 // ----------------------------------------------------------------------------
147 wxDateTime::wxDateTime_t day
;
148 wxDateTime::Month month
;
150 wxDateTime::wxDateTime_t hour
, min
, sec
;
152 wxDateTime::WeekDay wday
;
155 void Init(const wxDateTime::Tm
& tm
)
167 wxDateTime
DT() const
168 { return wxDateTime(day
, month
, year
, hour
, min
, sec
); }
170 bool SameDay(const wxDateTime::Tm
& tm
) const
172 return day
== tm
.mday
&& month
== tm
.mon
&& year
== tm
.year
;
175 wxString
Format() const
178 s
.Printf(wxT("%02d:%02d:%02d %10s %02d, %4d%s"),
180 wxDateTime::GetMonthName(month
).c_str(),
182 abs(wxDateTime::ConvertYearToBC(year
)),
183 year
> 0 ? wxT("AD") : wxT("BC"));
187 wxString
FormatDate() const
190 s
.Printf(wxT("%02d-%s-%4d%s"),
192 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
193 abs(wxDateTime::ConvertYearToBC(year
)),
194 year
> 0 ? wxT("AD") : wxT("BC"));
199 // ----------------------------------------------------------------------------
201 // ----------------------------------------------------------------------------
203 static const Date testDates
[] =
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 },
227 // ----------------------------------------------------------------------------
229 // ----------------------------------------------------------------------------
231 class DateTimeTestCase
: public CppUnit::TestCase
234 DateTimeTestCase() { }
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
);
245 CPPUNIT_TEST( TestTimeSpanFormat
);
246 CPPUNIT_TEST( TestTimeTicks
);
247 CPPUNIT_TEST( TestParceRFC822
);
248 CPPUNIT_TEST( TestDateParse
);
249 CPPUNIT_TEST( TestDateParseISO
);
250 CPPUNIT_TEST( TestDateTimeParse
);
251 CPPUNIT_TEST( TestTimeArithmetics
);
252 CPPUNIT_TEST( TestDSTBug
);
253 CPPUNIT_TEST( TestDateOnly
);
254 CPPUNIT_TEST_SUITE_END();
256 void TestLeapYears();
259 void TestTimeWNumber();
260 void TestTimeWDays();
262 void TestTimeFormat();
263 void TestTimeSpanFormat();
264 void TestTimeTicks();
265 void TestParceRFC822();
266 void TestDateParse();
267 void TestDateParseISO();
268 void TestDateTimeParse();
269 void TestTimeArithmetics();
273 DECLARE_NO_COPY_CLASS(DateTimeTestCase
)
276 // register in the unnamed registry so that these tests are run by default
277 CPPUNIT_TEST_SUITE_REGISTRATION( DateTimeTestCase
);
279 // also include in it's own registry so that these tests can be run alone
280 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DateTimeTestCase
, "DateTimeTestCase" );
282 // ============================================================================
284 // ============================================================================
286 // test leap years detection
287 void DateTimeTestCase::TestLeapYears()
289 static const struct LeapYearTestData
305 for ( size_t n
= 0; n
< WXSIZEOF(years
); n
++ )
307 const LeapYearTestData
& y
= years
[n
];
309 CPPUNIT_ASSERT_EQUAL( y
.isLeap
, wxDateTime::IsLeapYear(y
.year
) );
313 // test constructing wxDateTime objects
314 void DateTimeTestCase::TestTimeSet()
316 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
318 const Date
& d1
= testDates
[n
];
319 wxDateTime dt
= d1
.DT();
324 wxString s1
= d1
.Format(),
327 CPPUNIT_ASSERT_EQUAL( s1
, s2
);
331 // test conversions to JDN &c
332 void DateTimeTestCase::TestTimeJDN()
334 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
336 const Date
& d
= testDates
[n
];
337 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
339 // JDNs must be computed for UTC times
340 double jdn
= dt
.FromUTC().GetJulianDayNumber();
342 CPPUNIT_ASSERT_EQUAL( d
.jdn
, jdn
);
345 CPPUNIT_ASSERT_EQUAL( jdn
, dt
.GetJulianDayNumber() );
349 // test week days computation
350 void DateTimeTestCase::TestTimeWDays()
354 for ( n
= 0; n
< WXSIZEOF(testDates
); n
++ )
356 const Date
& d
= testDates
[n
];
357 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
359 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
360 CPPUNIT_ASSERT_EQUAL( d
.wday
, wday
);
363 // test SetToWeekDay()
364 struct WeekDateTestData
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
372 wxString
Format() const
375 switch ( nWeek
< -1 ? -nWeek
: nWeek
)
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;
383 case -1: which
= wxT("last"); break;
388 which
+= wxT(" from end");
391 s
.Printf(wxT("The %s %s of %s in %d"),
393 wxDateTime::GetWeekDayName(wday
).c_str(),
394 wxDateTime::GetMonthName(month
).c_str(),
401 // the array data was generated by the following python program
403 from DateTime import *
404 from whrandom import *
407 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
408 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
410 week = DateTimeDelta(7)
413 year = randint(1900, 2100)
414 month = randint(1, 12)
416 dt = DateTime(year, month, day)
417 wday = dt.day_of_week
419 countFromEnd = choice([-1, 1])
422 while dt.month is month:
423 dt = dt - countFromEnd * week
424 weekNum = weekNum + countFromEnd
426 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
428 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
429 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
432 static const WeekDateTestData weekDatesTestData
[] =
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 }
457 for ( n
= 0; n
< WXSIZEOF(weekDatesTestData
); n
++ )
459 const WeekDateTestData
& wd
= weekDatesTestData
[n
];
461 dt
.SetToWeekDay(wd
.wday
, wd
.nWeek
, wd
.month
, wd
.year
);
463 const Date
& d
= wd
.date
;
464 CPPUNIT_ASSERT( d
.SameDay(dt
.GetTm()) );
468 // test the computation of (ISO) week numbers
469 void DateTimeTestCase::TestTimeWNumber()
471 struct WeekNumberTestData
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
480 // data generated with the following python script:
482 from DateTime import *
483 from whrandom import *
486 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
487 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
489 def GetMonthWeek(dt):
490 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
492 weekNumMonth = weekNumMonth + 53
495 def GetLastSundayBefore(dt):
496 if dt.iso_week[2] == 7:
499 return dt - DateTimeDelta(dt.iso_week[2])
502 year = randint(1900, 2100)
503 month = randint(1, 12)
505 dt = DateTime(year, month, day)
506 dayNum = dt.day_of_year
507 weekNum = dt.iso_week[1]
508 weekNumMonth = GetMonthWeek(dt)
511 dtSunday = GetLastSundayBefore(dt)
513 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
514 weekNumMonth2 = weekNumMonth2 + 1
515 dtSunday = dtSunday - DateTimeDelta(7)
517 data = { 'day': rjust(`day`, 2), \
518 'month': monthNames[month - 1], \
520 'weekNum': rjust(`weekNum`, 2), \
521 'weekNumMonth': weekNumMonth, \
522 'weekNumMonth2': weekNumMonth2, \
523 'dayNum': rjust(`dayNum`, 3) }
525 print " { { %(day)s, "\
526 "wxDateTime::%(month)s, "\
529 "%(weekNumMonth)s, "\
530 "%(weekNumMonth2)s, "\
531 "%(dayNum)s }," % data
534 static const WeekNumberTestData weekNumberTestDates
[] =
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 },
560 for ( size_t n
= 0; n
< WXSIZEOF(weekNumberTestDates
); n
++ )
562 const WeekNumberTestData
& wn
= weekNumberTestDates
[n
];
563 const Date
& d
= wn
.date
;
565 wxDateTime dt
= d
.DT();
567 wxDateTime::wxDateTime_t
568 week
= dt
.GetWeekOfYear(wxDateTime::Monday_First
),
569 wmon
= dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
570 wmon2
= dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
571 dnum
= dt
.GetDayOfYear();
573 CPPUNIT_ASSERT_EQUAL( wn
.dnum
, dnum
);
574 CPPUNIT_ASSERT_EQUAL( wn
.wmon
, wmon
);
575 CPPUNIT_ASSERT_EQUAL( wn
.wmon2
, wmon2
);
576 CPPUNIT_ASSERT_EQUAL( wn
.week
, week
);
579 if ( week
== 1 && d
.month
!= wxDateTime::Jan
)
581 // this means we're in the first week of the next year
586 dt2
= wxDateTime::SetToWeekOfYear(year
, week
, dt
.GetWeekDay());
587 CPPUNIT_ASSERT_EQUAL( dt
, dt2
);
591 // test DST applicability
592 void DateTimeTestCase::TestTimeDST()
594 // taken from http://www.energy.ca.gov/daylightsaving.html
595 static const Date datesDST
[2][2009 - 1990 + 1] =
598 { 1, wxDateTime::Apr
, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
599 { 7, wxDateTime::Apr
, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
600 { 5, wxDateTime::Apr
, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
601 { 4, wxDateTime::Apr
, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
602 { 3, wxDateTime::Apr
, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
603 { 2, wxDateTime::Apr
, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
604 { 7, wxDateTime::Apr
, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
605 { 6, wxDateTime::Apr
, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
606 { 5, wxDateTime::Apr
, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
607 { 4, wxDateTime::Apr
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
608 { 2, wxDateTime::Apr
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
609 { 1, wxDateTime::Apr
, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
610 { 7, wxDateTime::Apr
, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
611 { 6, wxDateTime::Apr
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
612 { 4, wxDateTime::Apr
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
613 { 3, wxDateTime::Apr
, 2005, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
614 { 2, wxDateTime::Apr
, 2006, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
615 {11, wxDateTime::Mar
, 2007, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
616 { 9, wxDateTime::Mar
, 2008, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
617 { 8, wxDateTime::Mar
, 2009, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
620 { 28, wxDateTime::Oct
, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
621 { 27, wxDateTime::Oct
, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
622 { 25, wxDateTime::Oct
, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
623 { 31, wxDateTime::Oct
, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
624 { 30, wxDateTime::Oct
, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
625 { 29, wxDateTime::Oct
, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
626 { 27, wxDateTime::Oct
, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
627 { 26, wxDateTime::Oct
, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
628 { 25, wxDateTime::Oct
, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
629 { 31, wxDateTime::Oct
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
630 { 29, wxDateTime::Oct
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
631 { 28, wxDateTime::Oct
, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
632 { 27, wxDateTime::Oct
, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
633 { 26, wxDateTime::Oct
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
634 { 31, wxDateTime::Oct
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
635 { 30, wxDateTime::Oct
, 2005, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
636 { 29, wxDateTime::Oct
, 2006, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
637 { 4, wxDateTime::Nov
, 2007, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
638 { 2, wxDateTime::Nov
, 2008, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
639 { 1, wxDateTime::Nov
, 2009, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
644 for ( size_t n
= 0; n
< WXSIZEOF(datesDST
[0]); n
++ )
646 const int year
= 1990 + n
;
647 wxDateTime dtBegin
= wxDateTime::GetBeginDST(year
, wxDateTime::USA
),
648 dtEnd
= wxDateTime::GetEndDST(year
, wxDateTime::USA
);
650 const Date
& dBegin
= datesDST
[0][n
];
651 const Date
& dEnd
= datesDST
[1][n
];
653 CPPUNIT_ASSERT_EQUAL( dBegin
.DT().FormatDate(), dtBegin
.FormatDate() );
654 CPPUNIT_ASSERT_EQUAL( dEnd
.DT().FormatDate(), dtEnd
.FormatDate() );
658 // test wxDateTime -> text conversion
659 void DateTimeTestCase::TestTimeFormat()
661 // some information may be lost during conversion, so store what kind
662 // of info should we recover after a round trip
665 CompareNone
, // don't try comparing
666 CompareBoth
, // dates and times should be identical
667 CompareYear
, // don't compare centuries (fails for 2 digit years)
668 CompareDate
, // dates only
669 CompareTime
// time only
674 CompareKind compareKind
;
676 } formatTestFormats
[] =
678 { CompareYear
, "---> %c" }, // %c could use 2 digit years
679 { CompareDate
, "Date is %A, %d of %B, in year %Y" },
680 { CompareYear
, "Date is %x, time is %X" }, // %x could use 2 digits
681 { CompareTime
, "Time is %H:%M:%S or %I:%M:%S %p" },
682 { CompareNone
, "The day of year: %j, the week of year: %W" },
683 { CompareDate
, "ISO date without separators: %Y%m%d" },
686 static const Date formatTestDates
[] =
688 { 29, wxDateTime::May
, 1976, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
689 { 31, wxDateTime::Dec
, 1999, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
690 { 6, wxDateTime::Feb
, 1937, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
691 { 6, wxDateTime::Feb
, 1856, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
692 { 6, wxDateTime::Feb
, 1857, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
693 { 29, wxDateTime::May
, 2076, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
695 // FIXME: the test with 02:15:25 time doesn't pass because of DST
696 // computation problems, we get back 03:15:25
697 { 29, wxDateTime::Feb
, 2400, 04, 15, 25, 0.0, wxDateTime::Inv_WeekDay
},
699 // Need to add support for BCE dates.
700 { 01, wxDateTime::Jan
, -52, 03, 16, 47, 0.0, wxDateTime::Inv_WeekDay
},
704 for ( size_t d
= 0; d
< WXSIZEOF(formatTestDates
); d
++ )
706 wxDateTime dt
= formatTestDates
[d
].DT();
707 for ( unsigned n
= 0; n
< WXSIZEOF(formatTestFormats
); n
++ )
709 const char *fmt
= formatTestFormats
[n
].format
;
711 // skip the check with %p for those locales which have empty AM/PM strings:
712 // for those locales it's impossible to pass the test with %p...
714 wxDateTime::GetAmPmStrings(&am
, &pm
);
715 if (am
.empty() && pm
.empty() && wxStrstr(fmt
, "%p") != NULL
)
718 wxString s
= dt
.Format(fmt
);
720 // what can we recover?
721 CompareKind kind
= formatTestFormats
[n
].compareKind
;
725 const char *result
= dt2
.ParseFormat(s
, fmt
);
728 // conversion failed - should it have?
730 ("Test #%lu failed: failed to parse \"%s\"", n
, s
),
734 else // conversion succeeded
736 // currently ParseFormat() doesn't support "%Z" and so is
737 // incapable of parsing time zone part used at the end of date
738 // representations in many (but not "C") locales, compensate
739 // for it ourselves by simply consuming and ignoring it
740 while ( *result
&& (*result
>= 'A' && *result
<= 'Z') )
744 ("Test #%lu failed: \"%s\" was left unparsed in \"%s\"",
752 if ( dt2
.GetCentury() != dt
.GetCentury() )
754 CPPUNIT_ASSERT_EQUAL(dt
.GetYear() % 100,
755 dt2
.GetYear() % 100);
757 dt2
.SetYear(dt
.GetYear());
759 // fall through and compare everything
762 CPPUNIT_ASSERT_EQUAL( dt
, dt2
);
766 CPPUNIT_ASSERT( dt
.IsSameDate(dt2
) );
770 CPPUNIT_ASSERT( dt
.IsSameTime(dt2
) );
774 wxFAIL_MSG( wxT("unexpected") );
784 // special case which was known to fail
785 CPPUNIT_ASSERT( dt
.ParseFormat("02/06/1856", "%x") );
786 CPPUNIT_ASSERT_EQUAL( 1856, dt
.GetYear() );
789 // also test %l separately
790 CPPUNIT_ASSERT( dt
.ParseFormat("12:23:45.678", "%H:%M:%S.%l") );
791 CPPUNIT_ASSERT_EQUAL( 678, dt
.GetMillisecond() );
793 // test special case of %l matching 0 milliseconds
794 CPPUNIT_ASSERT( dt
.ParseFormat("12:23:45.000", "%H:%M:%S.%l") );
795 CPPUNIT_ASSERT_EQUAL( 0, dt
.GetMillisecond() );
797 // test partially specified dates too
798 wxDateTime
dtDef(26, wxDateTime::Sep
, 2008);
799 CPPUNIT_ASSERT( dt
.ParseFormat("17", "%d") );
800 CPPUNIT_ASSERT_EQUAL( 17, dt
.GetDay() );
802 // test compilation of some calls which should compile (and not result in
803 // ambiguity because of char*<->wxCStrData<->wxString conversions)
805 CPPUNIT_ASSERT( !dt
.ParseFormat("foo") );
806 CPPUNIT_ASSERT( !dt
.ParseFormat(wxT("foo")) );
807 CPPUNIT_ASSERT( !dt
.ParseFormat(s
) );
808 CPPUNIT_ASSERT( !dt
.ParseFormat(s
.c_str()) );
810 CPPUNIT_ASSERT( !dt
.ParseFormat("foo", "%c") );
811 CPPUNIT_ASSERT( !dt
.ParseFormat(wxT("foo"), "%c") );
812 CPPUNIT_ASSERT( !dt
.ParseFormat(s
, "%c") );
813 CPPUNIT_ASSERT( !dt
.ParseFormat(s
.c_str(), "%c") );
815 CPPUNIT_ASSERT( !dt
.ParseFormat("foo", wxT("%c")) );
816 CPPUNIT_ASSERT( !dt
.ParseFormat(wxT("foo"), wxT("%c")) );
817 CPPUNIT_ASSERT( !dt
.ParseFormat(s
, "%c") );
818 CPPUNIT_ASSERT( !dt
.ParseFormat(s
.c_str(), wxT("%c")) );
821 CPPUNIT_ASSERT( !dt
.ParseFormat("foo", spec
) );
822 CPPUNIT_ASSERT( !dt
.ParseFormat(wxT("foo"), spec
) );
823 CPPUNIT_ASSERT( !dt
.ParseFormat(s
, spec
) );
824 CPPUNIT_ASSERT( !dt
.ParseFormat(s
.c_str(), spec
) );
827 void DateTimeTestCase::TestTimeSpanFormat()
829 static const struct TimeSpanFormatTestData
831 long h
, min
, sec
, msec
;
836 { 12, 34, 56, 789, "%H:%M:%S.%l", "12:34:56.789" },
837 { 1, 2, 3, 0, "%H:%M:%S", "01:02:03" },
838 { 1, 2, 3, 0, "%S", "3723" },
839 { -1, -2, -3, 0, "%S", "-3723" },
840 { -1, -2, -3, 0, "%H:%M:%S", "-01:02:03" },
841 { 26, 0, 0, 0, "%H", "26" },
842 { 26, 0, 0, 0, "%D, %H", "1, 02" },
843 { -26, 0, 0, 0, "%H", "-26" },
844 { -26, 0, 0, 0, "%D, %H", "-1, 02" },
845 { 219, 0, 0, 0, "%H", "219" },
846 { 219, 0, 0, 0, "%D, %H", "9, 03" },
847 { 219, 0, 0, 0, "%E, %D, %H", "1, 2, 03" },
848 { 0, -1, 0, 0, "%H:%M:%S", "-00:01:00" },
849 { 0, 0, -1, 0, "%H:%M:%S", "-00:00:01" },
852 for ( size_t n
= 0; n
< WXSIZEOF(testSpans
); n
++ )
854 const TimeSpanFormatTestData
& td
= testSpans
[n
];
855 wxTimeSpan
ts(td
.h
, td
.min
, td
.sec
, td
.msec
);
856 CPPUNIT_ASSERT_EQUAL( td
.result
, ts
.Format(td
.fmt
) );
860 void DateTimeTestCase::TestTimeTicks()
862 static const wxDateTime::TimeZone
TZ_LOCAL(wxDateTime::Local
);
863 static const wxDateTime::TimeZone
TZ_TEST(wxDateTime::NZST
);
865 // this offset is needed to make the test work in any time zone when we
866 // only have expected test results in UTC in testDates
867 static const long tzOffset
= TZ_LOCAL
.GetOffset() - TZ_TEST
.GetOffset();
869 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
871 const Date
& d
= testDates
[n
];
872 if ( d
.gmticks
== -1 )
875 wxDateTime dt
= d
.DT().MakeTimezone(TZ_TEST
, true /* no DST */);
877 // GetValue() returns internal UTC-based representation, we need to
878 // convert it to local TZ before comparing
879 time_t ticks
= (dt
.GetValue() / 1000).ToLong() + TZ_LOCAL
.GetOffset();
882 CPPUNIT_ASSERT_EQUAL( d
.gmticks
, ticks
+ tzOffset
);
884 dt
= d
.DT().FromTimezone(wxDateTime::UTC
);
885 ticks
= (dt
.GetValue() / 1000).ToLong();
886 CPPUNIT_ASSERT_EQUAL( d
.gmticks
, ticks
);
890 // test parsing dates in RFC822 format
891 void DateTimeTestCase::TestParceRFC822()
893 static const struct ParseTestData
896 Date date
; // NB: this should be in UTC
901 "Sat, 18 Dec 1999 00:46:40 +0100",
902 { 17, wxDateTime::Dec
, 1999, 23, 46, 40 },
906 "Wed, 1 Dec 1999 05:17:20 +0300",
907 { 1, wxDateTime::Dec
, 1999, 2, 17, 20 },
911 "Sun, 28 Aug 2005 03:31:30 +0200",
912 { 28, wxDateTime::Aug
, 2005, 1, 31, 30 },
917 "Sat, 18 Dec 1999 10:48:30 -0500",
918 { 18, wxDateTime::Dec
, 1999, 15, 48, 30 },
922 // seconds are optional according to the RFC
924 "Sun, 01 Jun 2008 16:30 +0200",
925 { 1, wxDateTime::Jun
, 2008, 14, 30, 00 },
929 // try some bogus ones too
931 "Sun, 01 Jun 2008 16:30: +0200",
937 for ( unsigned n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
939 const char * const datestr
= parseTestDates
[n
].rfc822
;
942 if ( dt
.ParseRfc822Date(datestr
) )
945 ("Erroneously parsed \"%s\"", datestr
),
946 parseTestDates
[n
].good
949 wxDateTime dtReal
= parseTestDates
[n
].date
.DT().FromUTC();
950 CPPUNIT_ASSERT_EQUAL( dtReal
, dt
);
952 else // failed to parse
955 ("Failed to parse \"%s\"", datestr
),
956 !parseTestDates
[n
].good
962 // test parsing dates in free format
963 void DateTimeTestCase::TestDateParse()
965 static const struct ParseTestData
968 Date date
; // NB: this should be in UTC
972 { "21 Mar 2006", { 21, wxDateTime::Mar
, 2006 }, true },
973 { "29 Feb 1976", { 29, wxDateTime::Feb
, 1976 }, true },
974 { "Feb 29 1976", { 29, wxDateTime::Feb
, 1976 }, true },
975 { "31/03/06", { 31, wxDateTime::Mar
, 6 }, true },
976 { "31/03/2006", { 31, wxDateTime::Mar
, 2006 }, true },
978 // some invalid ones too
986 CPPUNIT_ASSERT( dt
.ParseDate(wxT("today")) );
987 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), dt
);
989 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
991 const wxString datestr
= TranslateDate(parseTestDates
[n
].str
);
993 const char * const end
= dt
.ParseDate(datestr
);
997 ("Erroneously parsed \"%s\"", datestr
),
998 parseTestDates
[n
].good
1001 CPPUNIT_ASSERT_EQUAL( parseTestDates
[n
].date
.DT(), dt
);
1003 else // failed to parse
1006 ("Failed to parse \"%s\"", datestr
),
1007 !parseTestDates
[n
].good
1013 void DateTimeTestCase::TestDateParseISO()
1018 Date date
; // NB: this should be in UTC
1020 } parseTestDates
[] =
1022 { "2006-03-21", { 21, wxDateTime::Mar
, 2006 }, true },
1023 { "1976-02-29", { 29, wxDateTime::Feb
, 1976 }, true },
1024 { "0006-03-31", { 31, wxDateTime::Mar
, 6 }, true },
1026 // some invalid ones too
1036 wxDateTime::wxDateTime_t hour
, min
, sec
;
1038 } parseTestTimes
[] =
1040 { "13:42:17", 13, 42, 17, true },
1041 { "02:17:01", 2, 17, 1, true },
1043 // some invalid ones too
1050 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
1053 if ( dt
.ParseISODate(parseTestDates
[n
].str
) )
1055 CPPUNIT_ASSERT( parseTestDates
[n
].good
);
1057 CPPUNIT_ASSERT_EQUAL( parseTestDates
[n
].date
.DT(), dt
);
1059 for ( size_t m
= 0; m
< WXSIZEOF(parseTestTimes
); m
++ )
1061 wxString dtCombined
;
1062 dtCombined
<< parseTestDates
[n
].str
1064 << parseTestTimes
[m
].str
;
1066 if ( dt
.ParseISOCombined(dtCombined
) )
1068 CPPUNIT_ASSERT( parseTestTimes
[m
].good
);
1070 CPPUNIT_ASSERT_EQUAL( parseTestTimes
[m
].hour
, dt
.GetHour()) ;
1071 CPPUNIT_ASSERT_EQUAL( parseTestTimes
[m
].min
, dt
.GetMinute()) ;
1072 CPPUNIT_ASSERT_EQUAL( parseTestTimes
[m
].sec
, dt
.GetSecond()) ;
1074 else // failed to parse combined date/time
1076 CPPUNIT_ASSERT( !parseTestTimes
[m
].good
);
1080 else // failed to parse
1082 CPPUNIT_ASSERT( !parseTestDates
[n
].good
);
1087 void DateTimeTestCase::TestDateTimeParse()
1089 static const struct ParseTestData
1092 Date date
; // NB: this should be in UTC
1094 } parseTestDates
[] =
1096 { "Thu 22 Nov 2007 07:40:00 PM",
1097 { 22, wxDateTime::Nov
, 2007, 19, 40, 0}, true },
1100 // the test strings here use "PM" which is not available in all locales so
1101 // we need to use "C" locale for them
1105 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
1107 const wxString datestr
= TranslateDate(parseTestDates
[n
].str
);
1109 const char * const end
= dt
.ParseDateTime(datestr
);
1113 ("Erroneously parsed \"%s\"", datestr
),
1114 parseTestDates
[n
].good
1117 CPPUNIT_ASSERT_EQUAL( parseTestDates
[n
].date
.DT(), dt
);
1119 else // failed to parse
1122 ("Failed to parse \"%s\"", datestr
),
1123 !parseTestDates
[n
].good
1126 CPPUNIT_ASSERT( !parseTestDates
[n
].good
);
1131 void DateTimeTestCase::TestTimeArithmetics()
1133 static const wxDateSpan testArithmData
[] =
1137 wxDateSpan::Month(),
1141 // the test will *not* work with arbitrary date!
1142 wxDateTime
dt(2, wxDateTime::Dec
, 1999),
1146 for ( size_t n
= 0; n
< WXSIZEOF(testArithmData
); n
++ )
1148 const wxDateSpan
& span
= testArithmData
[n
];
1152 CPPUNIT_ASSERT_EQUAL( dt
, dt1
- span
);
1153 CPPUNIT_ASSERT_EQUAL( dt
, dt2
+ span
);
1154 CPPUNIT_ASSERT_EQUAL( dt1
, dt2
+ 2*span
);
1158 void DateTimeTestCase::TestDSTBug()
1160 /////////////////////////
1162 wxDateTime dt
= wxDateTime::GetEndDST(2004, wxDateTime::France
);
1163 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
1164 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
1165 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
1166 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
1167 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
1168 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
1169 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
1171 /////////////////////////
1174 CPPUNIT_ASSERT_EQUAL(5, (int)dt
.GetHour());
1176 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
1177 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
1178 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
1179 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetHour());
1180 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
1181 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
1182 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
1185 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
1187 /////////////////////////
1189 #ifdef CHANGE_SYSTEM_DATE
1191 DateChanger
change(2004, 10, 31, 5, 0, 0);
1192 dt
= wxDateTime::Today();
1195 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
1196 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
1197 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
1198 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetHour());
1199 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
1200 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
1201 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
1203 /////////////////////////
1204 // Test Set(hour, minute, second, milli)
1207 DateChanger
change(2004, 10, 31, 5, 0, 0);
1208 dt
.Set(1, 30, 0, 0);
1209 dt2
.Set(5, 30, 0, 0);
1212 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
1213 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
1214 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
1215 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
1216 CPPUNIT_ASSERT_EQUAL(30, (int)dt
.GetMinute());
1217 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
1218 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
1220 CPPUNIT_ASSERT_EQUAL(31, (int)dt2
.GetDay());
1221 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt2
.GetMonth());
1222 CPPUNIT_ASSERT_EQUAL(2004, (int)dt2
.GetYear());
1223 CPPUNIT_ASSERT_EQUAL(5, (int)dt2
.GetHour());
1224 CPPUNIT_ASSERT_EQUAL(30, (int)dt2
.GetMinute());
1225 CPPUNIT_ASSERT_EQUAL(0, (int)dt2
.GetSecond());
1226 CPPUNIT_ASSERT_EQUAL(0, (int)dt2
.GetMillisecond());
1227 #endif // CHANGE_SYSTEM_DATE
1230 void DateTimeTestCase::TestDateOnly()
1232 wxDateTime
dt(19, wxDateTime::Jan
, 2007, 15, 01, 00);
1234 static const wxDateTime::wxDateTime_t DATE_ZERO
= 0;
1235 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetHour() );
1236 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetMinute() );
1237 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetSecond() );
1238 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetMillisecond() );
1241 CPPUNIT_ASSERT_EQUAL( wxDateTime(19, wxDateTime::Jan
, 2007), dt
);
1243 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), wxDateTime::Now().GetDateOnly() );
1246 #endif // wxUSE_DATETIME