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"
27 // need this to be able to use CPPUNIT_ASSERT_EQUAL with wxDateTime objects
28 static std::ostream
& operator<<(std::ostream
& ostr
, const wxDateTime
& dt
)
30 ostr
<< dt
.FormatISOCombined(' ');
35 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(wxDateTime::wxDateTime_t
)
37 // to test Today() meaningfully we must be able to change the system date which
38 // is not usually the case, but if we're under Win32 we can try it -- define
39 // the macro below to do it
40 //#define CHANGE_SYSTEM_DATE
43 #undef CHANGE_SYSTEM_DATE
46 #ifdef CHANGE_SYSTEM_DATE
51 DateChanger(int year
, int month
, int day
, int hour
, int min
, int sec
)
62 ::GetSystemTime(&m_savedTime
);
63 ::GetTimeZoneInformation(&m_tzi
);
65 m_changed
= ::SetSystemTime(&st
) != 0;
72 ::SetSystemTime(&m_savedTime
);
73 ::SetTimeZoneInformation(&m_tzi
);
78 SYSTEMTIME m_savedTime
;
79 TIME_ZONE_INFORMATION m_tzi
;
83 #endif // CHANGE_SYSTEM_DATE
85 // ----------------------------------------------------------------------------
86 // broken down date representation used for testing
87 // ----------------------------------------------------------------------------
91 wxDateTime::wxDateTime_t day
;
92 wxDateTime::Month month
;
94 wxDateTime::wxDateTime_t hour
, min
, sec
;
96 wxDateTime::WeekDay wday
;
99 void Init(const wxDateTime::Tm
& tm
)
111 wxDateTime
DT() const
112 { return wxDateTime(day
, month
, year
, hour
, min
, sec
); }
114 bool SameDay(const wxDateTime::Tm
& tm
) const
116 return day
== tm
.mday
&& month
== tm
.mon
&& year
== tm
.year
;
119 wxString
Format() const
122 s
.Printf(_T("%02d:%02d:%02d %10s %02d, %4d%s"),
124 wxDateTime::GetMonthName(month
).c_str(),
126 abs(wxDateTime::ConvertYearToBC(year
)),
127 year
> 0 ? _T("AD") : _T("BC"));
131 wxString
FormatDate() const
134 s
.Printf(_T("%02d-%s-%4d%s"),
136 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
137 abs(wxDateTime::ConvertYearToBC(year
)),
138 year
> 0 ? _T("AD") : _T("BC"));
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
147 static const Date testDates
[] =
149 { 1, wxDateTime::Jan
, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu
, 0 },
150 { 7, wxDateTime::Feb
, 2036, 00, 00, 00, 2464730.5, wxDateTime::Thu
, -1 },
151 { 8, wxDateTime::Feb
, 2036, 00, 00, 00, 2464731.5, wxDateTime::Fri
, -1 },
152 { 1, wxDateTime::Jan
, 2037, 00, 00, 00, 2465059.5, wxDateTime::Thu
, -1 },
153 { 1, wxDateTime::Jan
, 2038, 00, 00, 00, 2465424.5, wxDateTime::Fri
, -1 },
154 { 21, wxDateTime::Jan
, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon
, -1 },
155 { 29, wxDateTime::May
, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat
, 202219200 },
156 { 29, wxDateTime::Feb
, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun
, 194400000 },
157 { 1, wxDateTime::Jan
, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon
, -1 },
158 { 1, wxDateTime::Jan
, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon
, -1 },
159 { 15, wxDateTime::Oct
, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri
, -1 },
160 { 4, wxDateTime::Oct
, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon
, -1 },
161 { 1, wxDateTime::Mar
, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu
, -1 },
162 { 1, wxDateTime::Jan
, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon
, -1 },
163 { 31, wxDateTime::Dec
, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun
, -1 },
164 { 1, wxDateTime::Jan
, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat
, -1 },
165 { 12, wxDateTime::Aug
, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri
, -1 },
166 { 12, wxDateTime::Aug
, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat
, -1 },
167 { 24, wxDateTime::Nov
, -4713, 00, 00, 00, -0.5, wxDateTime::Mon
, -1 },
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
175 class DateTimeTestCase
: public CppUnit::TestCase
178 DateTimeTestCase() { }
181 CPPUNIT_TEST_SUITE( DateTimeTestCase
);
182 CPPUNIT_TEST( TestLeapYears
);
183 CPPUNIT_TEST( TestTimeSet
);
184 CPPUNIT_TEST( TestTimeJDN
);
185 CPPUNIT_TEST( TestTimeWNumber
);
186 CPPUNIT_TEST( TestTimeWDays
);
187 CPPUNIT_TEST( TestTimeDST
);
188 CPPUNIT_TEST( TestTimeFormat
);
189 CPPUNIT_TEST( TestTimeSpanFormat
);
190 CPPUNIT_TEST( TestTimeTicks
);
191 CPPUNIT_TEST( TestParceRFC822
);
192 CPPUNIT_TEST( TestDateParse
);
193 CPPUNIT_TEST( TestDateParseISO
);
194 CPPUNIT_TEST( TestDateTimeParse
);
195 CPPUNIT_TEST( TestTimeArithmetics
);
196 CPPUNIT_TEST( TestDSTBug
);
197 CPPUNIT_TEST( TestDateOnly
);
198 CPPUNIT_TEST_SUITE_END();
200 void TestLeapYears();
203 void TestTimeWNumber();
204 void TestTimeWDays();
206 void TestTimeFormat();
207 void TestTimeSpanFormat();
208 void TestTimeTicks();
209 void TestParceRFC822();
210 void TestDateParse();
211 void TestDateParseISO();
212 void TestDateTimeParse();
213 void TestTimeArithmetics();
217 DECLARE_NO_COPY_CLASS(DateTimeTestCase
)
220 // register in the unnamed registry so that these tests are run by default
221 CPPUNIT_TEST_SUITE_REGISTRATION( DateTimeTestCase
);
223 // also include in it's own registry so that these tests can be run alone
224 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DateTimeTestCase
, "DateTimeTestCase" );
226 // ============================================================================
228 // ============================================================================
230 // test leap years detection
231 void DateTimeTestCase::TestLeapYears()
233 static const struct LeapYearTestData
249 for ( size_t n
= 0; n
< WXSIZEOF(years
); n
++ )
251 const LeapYearTestData
& y
= years
[n
];
253 CPPUNIT_ASSERT( wxDateTime::IsLeapYear(y
.year
) == y
.isLeap
);
257 // test constructing wxDateTime objects
258 void DateTimeTestCase::TestTimeSet()
260 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
262 const Date
& d1
= testDates
[n
];
263 wxDateTime dt
= d1
.DT();
268 wxString s1
= d1
.Format(),
271 CPPUNIT_ASSERT( s1
== s2
);
275 // test conversions to JDN &c
276 void DateTimeTestCase::TestTimeJDN()
278 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
280 const Date
& d
= testDates
[n
];
281 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
283 // JDNs must be computed for UTC times
284 double jdn
= dt
.FromUTC().GetJulianDayNumber();
286 CPPUNIT_ASSERT_EQUAL( d
.jdn
, jdn
);
289 CPPUNIT_ASSERT_EQUAL( jdn
, dt
.GetJulianDayNumber() );
293 // test week days computation
294 void DateTimeTestCase::TestTimeWDays()
298 for ( n
= 0; n
< WXSIZEOF(testDates
); n
++ )
300 const Date
& d
= testDates
[n
];
301 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
303 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
304 CPPUNIT_ASSERT( wday
== d
.wday
);
307 // test SetToWeekDay()
308 struct WeekDateTestData
310 Date date
; // the real date (precomputed)
311 int nWeek
; // its week index in the month
312 wxDateTime::WeekDay wday
; // the weekday
313 wxDateTime::Month month
; // the month
314 int year
; // and the year
316 wxString
Format() const
319 switch ( nWeek
< -1 ? -nWeek
: nWeek
)
321 case 1: which
= _T("first"); break;
322 case 2: which
= _T("second"); break;
323 case 3: which
= _T("third"); break;
324 case 4: which
= _T("fourth"); break;
325 case 5: which
= _T("fifth"); break;
327 case -1: which
= _T("last"); break;
332 which
+= _T(" from end");
335 s
.Printf(_T("The %s %s of %s in %d"),
337 wxDateTime::GetWeekDayName(wday
).c_str(),
338 wxDateTime::GetMonthName(month
).c_str(),
345 // the array data was generated by the following python program
347 from DateTime import *
348 from whrandom import *
351 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
352 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
354 week = DateTimeDelta(7)
357 year = randint(1900, 2100)
358 month = randint(1, 12)
360 dt = DateTime(year, month, day)
361 wday = dt.day_of_week
363 countFromEnd = choice([-1, 1])
366 while dt.month is month:
367 dt = dt - countFromEnd * week
368 weekNum = weekNum + countFromEnd
370 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
372 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
373 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
376 static const WeekDateTestData weekDatesTestData
[] =
378 { { 20, wxDateTime::Mar
, 2045, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 3, wxDateTime::Mon
, wxDateTime::Mar
, 2045 },
379 { { 5, wxDateTime::Jun
, 1985, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -4, wxDateTime::Wed
, wxDateTime::Jun
, 1985 },
380 { { 12, wxDateTime::Nov
, 1961, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -3, wxDateTime::Sun
, wxDateTime::Nov
, 1961 },
381 { { 27, wxDateTime::Feb
, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -1, wxDateTime::Fri
, wxDateTime::Feb
, 2093 },
382 { { 4, wxDateTime::Jul
, 2070, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -4, wxDateTime::Fri
, wxDateTime::Jul
, 2070 },
383 { { 2, wxDateTime::Apr
, 1906, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -5, wxDateTime::Mon
, wxDateTime::Apr
, 1906 },
384 { { 19, wxDateTime::Jul
, 2023, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -2, wxDateTime::Wed
, wxDateTime::Jul
, 2023 },
385 { { 5, wxDateTime::May
, 1958, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -4, wxDateTime::Mon
, wxDateTime::May
, 1958 },
386 { { 11, wxDateTime::Aug
, 1900, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 2, wxDateTime::Sat
, wxDateTime::Aug
, 1900 },
387 { { 14, wxDateTime::Feb
, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 2, wxDateTime::Wed
, wxDateTime::Feb
, 1945 },
388 { { 25, wxDateTime::Jul
, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -1, wxDateTime::Tue
, wxDateTime::Jul
, 1967 },
389 { { 9, wxDateTime::May
, 1916, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -4, wxDateTime::Tue
, wxDateTime::May
, 1916 },
390 { { 20, wxDateTime::Jun
, 1927, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 3, wxDateTime::Mon
, wxDateTime::Jun
, 1927 },
391 { { 2, wxDateTime::Aug
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, wxDateTime::Wed
, wxDateTime::Aug
, 2000 },
392 { { 20, wxDateTime::Apr
, 2044, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 3, wxDateTime::Wed
, wxDateTime::Apr
, 2044 },
393 { { 20, wxDateTime::Feb
, 1932, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -2, wxDateTime::Sat
, wxDateTime::Feb
, 1932 },
394 { { 25, wxDateTime::Jul
, 2069, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 4, wxDateTime::Thu
, wxDateTime::Jul
, 2069 },
395 { { 3, wxDateTime::Apr
, 1925, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, wxDateTime::Fri
, wxDateTime::Apr
, 1925 },
396 { { 21, wxDateTime::Mar
, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 3, wxDateTime::Sat
, wxDateTime::Mar
, 2093 },
397 { { 3, wxDateTime::Dec
, 2074, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -5, wxDateTime::Mon
, wxDateTime::Dec
, 2074 }
401 for ( n
= 0; n
< WXSIZEOF(weekDatesTestData
); n
++ )
403 const WeekDateTestData
& wd
= weekDatesTestData
[n
];
405 dt
.SetToWeekDay(wd
.wday
, wd
.nWeek
, wd
.month
, wd
.year
);
407 const Date
& d
= wd
.date
;
408 CPPUNIT_ASSERT( d
.SameDay(dt
.GetTm()) );
412 // test the computation of (ISO) week numbers
413 void DateTimeTestCase::TestTimeWNumber()
415 struct WeekNumberTestData
417 Date date
; // the date
418 wxDateTime::wxDateTime_t week
; // the week number in the year
419 wxDateTime::wxDateTime_t wmon
; // the week number in the month
420 wxDateTime::wxDateTime_t wmon2
; // same but week starts with Sun
421 wxDateTime::wxDateTime_t dnum
; // day number in the year
424 // data generated with the following python script:
426 from DateTime import *
427 from whrandom import *
430 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
431 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
433 def GetMonthWeek(dt):
434 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
436 weekNumMonth = weekNumMonth + 53
439 def GetLastSundayBefore(dt):
440 if dt.iso_week[2] == 7:
443 return dt - DateTimeDelta(dt.iso_week[2])
446 year = randint(1900, 2100)
447 month = randint(1, 12)
449 dt = DateTime(year, month, day)
450 dayNum = dt.day_of_year
451 weekNum = dt.iso_week[1]
452 weekNumMonth = GetMonthWeek(dt)
455 dtSunday = GetLastSundayBefore(dt)
457 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
458 weekNumMonth2 = weekNumMonth2 + 1
459 dtSunday = dtSunday - DateTimeDelta(7)
461 data = { 'day': rjust(`day`, 2), \
462 'month': monthNames[month - 1], \
464 'weekNum': rjust(`weekNum`, 2), \
465 'weekNumMonth': weekNumMonth, \
466 'weekNumMonth2': weekNumMonth2, \
467 'dayNum': rjust(`dayNum`, 3) }
469 print " { { %(day)s, "\
470 "wxDateTime::%(month)s, "\
473 "%(weekNumMonth)s, "\
474 "%(weekNumMonth2)s, "\
475 "%(dayNum)s }," % data
478 static const WeekNumberTestData weekNumberTestDates
[] =
480 { { 27, wxDateTime::Dec
, 1966, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 52, 5, 5, 361 },
481 { { 22, wxDateTime::Jul
, 1926, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 29, 4, 4, 203 },
482 { { 22, wxDateTime::Oct
, 2076, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 43, 4, 4, 296 },
483 { { 1, wxDateTime::Jul
, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 26, 1, 1, 182 },
484 { { 8, wxDateTime::Nov
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 46, 2, 2, 313 },
485 { { 21, wxDateTime::Mar
, 1920, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 12, 3, 4, 81 },
486 { { 7, wxDateTime::Jan
, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 2, 2, 7 },
487 { { 19, wxDateTime::Oct
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 42, 4, 4, 292 },
488 { { 13, wxDateTime::Aug
, 1955, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 32, 2, 2, 225 },
489 { { 18, wxDateTime::Jul
, 2087, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 29, 3, 3, 199 },
490 { { 2, wxDateTime::Sep
, 2028, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 35, 1, 1, 246 },
491 { { 28, wxDateTime::Jul
, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 30, 5, 4, 209 },
492 { { 15, wxDateTime::Jun
, 1901, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 24, 3, 3, 166 },
493 { { 10, wxDateTime::Oct
, 1939, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 41, 3, 2, 283 },
494 { { 3, wxDateTime::Dec
, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 48, 1, 1, 337 },
495 { { 23, wxDateTime::Feb
, 1940, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 8, 4, 4, 54 },
496 { { 2, wxDateTime::Jan
, 1987, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 1, 1, 2 },
497 { { 11, wxDateTime::Aug
, 2079, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 32, 2, 2, 223 },
498 { { 2, wxDateTime::Feb
, 2063, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 5, 1, 1, 33 },
499 { { 16, wxDateTime::Oct
, 1942, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 42, 3, 3, 289 },
500 { { 30, wxDateTime::Dec
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 5, 5, 364 },
501 { { 2, wxDateTime::Jan
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 1, 1, 2 },
504 for ( size_t n
= 0; n
< WXSIZEOF(weekNumberTestDates
); n
++ )
506 const WeekNumberTestData
& wn
= weekNumberTestDates
[n
];
507 const Date
& d
= wn
.date
;
509 wxDateTime dt
= d
.DT();
511 wxDateTime::wxDateTime_t
512 week
= dt
.GetWeekOfYear(wxDateTime::Monday_First
),
513 wmon
= dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
514 wmon2
= dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
515 dnum
= dt
.GetDayOfYear();
517 CPPUNIT_ASSERT( dnum
== wn
.dnum
);
518 CPPUNIT_ASSERT( wmon
== wn
.wmon
);
519 CPPUNIT_ASSERT( wmon2
== wn
.wmon2
);
520 CPPUNIT_ASSERT( week
== wn
.week
);
523 if ( week
== 1 && d
.month
!= wxDateTime::Jan
)
525 // this means we're in the first week of the next year
530 dt2
= wxDateTime::SetToWeekOfYear(year
, week
, dt
.GetWeekDay());
531 CPPUNIT_ASSERT( dt2
== dt
);
535 // test DST applicability
536 void DateTimeTestCase::TestTimeDST()
538 // taken from http://www.energy.ca.gov/daylightsaving.html
539 static const Date datesDST
[2][2004 - 1900 + 1] =
542 { 1, wxDateTime::Apr
, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
543 { 7, wxDateTime::Apr
, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
544 { 5, wxDateTime::Apr
, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
545 { 4, wxDateTime::Apr
, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
546 { 3, wxDateTime::Apr
, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
547 { 2, wxDateTime::Apr
, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
548 { 7, wxDateTime::Apr
, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
549 { 6, wxDateTime::Apr
, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
550 { 5, wxDateTime::Apr
, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
551 { 4, wxDateTime::Apr
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
552 { 2, wxDateTime::Apr
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
553 { 1, wxDateTime::Apr
, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
554 { 7, wxDateTime::Apr
, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
555 { 6, wxDateTime::Apr
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
556 { 4, wxDateTime::Apr
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
559 { 28, wxDateTime::Oct
, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
560 { 27, wxDateTime::Oct
, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
561 { 25, wxDateTime::Oct
, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
562 { 31, wxDateTime::Oct
, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
563 { 30, wxDateTime::Oct
, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
564 { 29, wxDateTime::Oct
, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
565 { 27, wxDateTime::Oct
, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
566 { 26, wxDateTime::Oct
, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
567 { 25, wxDateTime::Oct
, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
568 { 31, wxDateTime::Oct
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
569 { 29, wxDateTime::Oct
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
570 { 28, wxDateTime::Oct
, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
571 { 27, wxDateTime::Oct
, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
572 { 26, wxDateTime::Oct
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
573 { 31, wxDateTime::Oct
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
577 for ( int year
= 1990; year
< 2005; year
++ )
579 wxDateTime dtBegin
= wxDateTime::GetBeginDST(year
, wxDateTime::USA
),
580 dtEnd
= wxDateTime::GetEndDST(year
, wxDateTime::USA
);
582 size_t n
= year
- 1990;
583 const Date
& dBegin
= datesDST
[0][n
];
584 const Date
& dEnd
= datesDST
[1][n
];
586 CPPUNIT_ASSERT( dBegin
.SameDay(dtBegin
.GetTm()) );
587 CPPUNIT_ASSERT( dEnd
.SameDay(dtEnd
.GetTm()) );
591 // test wxDateTime -> text conversion
592 void DateTimeTestCase::TestTimeFormat()
594 // some information may be lost during conversion, so store what kind
595 // of info should we recover after a round trip
598 CompareNone
, // don't try comparing
599 CompareBoth
, // dates and times should be identical
600 CompareYear
, // don't compare centuries (fails for 2 digit years)
601 CompareDate
, // dates only
602 CompareTime
// time only
607 CompareKind compareKind
;
609 } formatTestFormats
[] =
611 { CompareYear
, "---> %c" }, // %c could use 2 digit years
612 { CompareDate
, "Date is %A, %d of %B, in year %Y" },
613 { CompareYear
, "Date is %x, time is %X" }, // %x could use 2 digits
614 { CompareTime
, "Time is %H:%M:%S or %I:%M:%S %p" },
615 { CompareNone
, "The day of year: %j, the week of year: %W" },
616 { CompareDate
, "ISO date without separators: %Y%m%d" },
619 static const Date formatTestDates
[] =
621 { 29, wxDateTime::May
, 1976, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
622 { 31, wxDateTime::Dec
, 1999, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
623 { 6, wxDateTime::Feb
, 1937, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
624 { 6, wxDateTime::Feb
, 1856, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
625 { 6, wxDateTime::Feb
, 1857, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
626 { 29, wxDateTime::May
, 2076, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
627 { 29, wxDateTime::Feb
, 2400, 02, 15, 25, 0.0, wxDateTime::Inv_WeekDay
},
629 // Need to add support for BCE dates.
630 { 01, wxDateTime::Jan
, -52, 03, 16, 47, 0.0, wxDateTime::Inv_WeekDay
},
634 for ( size_t d
= 0; d
< WXSIZEOF(formatTestDates
); d
++ )
636 wxDateTime dt
= formatTestDates
[d
].DT();
637 for ( size_t n
= 0; n
< WXSIZEOF(formatTestFormats
); n
++ )
639 const char *fmt
= formatTestFormats
[n
].format
;
640 wxString s
= dt
.Format(fmt
);
642 // what can we recover?
643 CompareKind kind
= formatTestFormats
[n
].compareKind
;
647 const char *result
= dt2
.ParseFormat(s
, fmt
);
650 // conversion failed - should it have?
651 CPPUNIT_ASSERT( kind
== CompareNone
);
653 else // conversion succeeded
655 // should have parsed the entire string
656 CPPUNIT_ASSERT( !*result
);
661 if ( dt2
.GetCentury() != dt
.GetCentury() )
663 CPPUNIT_ASSERT_EQUAL(dt
.GetYear() % 100,
664 dt2
.GetYear() % 100);
666 dt2
.SetYear(dt
.GetYear());
668 // fall through and compare everything
671 CPPUNIT_ASSERT_EQUAL( dt
, dt2
);
675 CPPUNIT_ASSERT( dt
.IsSameDate(dt2
) );
679 CPPUNIT_ASSERT( dt
.IsSameTime(dt2
) );
683 wxFAIL_MSG( _T("unexpected") );
692 // test partially specified dates too
693 wxDateTime
dtDef(26, wxDateTime::Sep
, 2008);
694 CPPUNIT_ASSERT( dt
.ParseFormat("17", "%d") );
695 CPPUNIT_ASSERT_EQUAL( 17, dt
.GetDay() );
697 // test compilation of some calls which should compile (and not result in
698 // ambiguity because of char*<->wxCStrData<->wxString conversions)
700 CPPUNIT_ASSERT( !dt
.ParseFormat("foo") );
701 CPPUNIT_ASSERT( !dt
.ParseFormat(wxT("foo")) );
702 CPPUNIT_ASSERT( !dt
.ParseFormat(s
) );
703 CPPUNIT_ASSERT( !dt
.ParseFormat(s
.c_str()) );
705 CPPUNIT_ASSERT( !dt
.ParseFormat("foo", "%c") );
706 CPPUNIT_ASSERT( !dt
.ParseFormat(wxT("foo"), "%c") );
707 CPPUNIT_ASSERT( !dt
.ParseFormat(s
, "%c") );
708 CPPUNIT_ASSERT( !dt
.ParseFormat(s
.c_str(), "%c") );
710 CPPUNIT_ASSERT( !dt
.ParseFormat("foo", wxT("%c")) );
711 CPPUNIT_ASSERT( !dt
.ParseFormat(wxT("foo"), wxT("%c")) );
712 CPPUNIT_ASSERT( !dt
.ParseFormat(s
, "%c") );
713 CPPUNIT_ASSERT( !dt
.ParseFormat(s
.c_str(), wxT("%c")) );
716 CPPUNIT_ASSERT( !dt
.ParseFormat("foo", spec
) );
717 CPPUNIT_ASSERT( !dt
.ParseFormat(wxT("foo"), spec
) );
718 CPPUNIT_ASSERT( !dt
.ParseFormat(s
, spec
) );
719 CPPUNIT_ASSERT( !dt
.ParseFormat(s
.c_str(), spec
) );
722 void DateTimeTestCase::TestTimeSpanFormat()
724 static const struct TimeSpanFormatTestData
726 long h
, min
, sec
, msec
;
731 { 12, 34, 56, 789, "%H:%M:%S.%l", "12:34:56.789" },
732 { 1, 2, 3, 0, "%H:%M:%S", "01:02:03" },
733 { 1, 2, 3, 0, "%S", "3723" },
734 { -1, -2, -3, 0, "%S", "-3723" },
735 { -1, -2, -3, 0, "%H:%M:%S", "-01:02:03" },
736 { 26, 0, 0, 0, "%H", "26" },
737 { 26, 0, 0, 0, "%D, %H", "1, 02" },
738 { -26, 0, 0, 0, "%H", "-26" },
739 { -26, 0, 0, 0, "%D, %H", "-1, 02" },
740 { 219, 0, 0, 0, "%H", "219" },
741 { 219, 0, 0, 0, "%D, %H", "9, 03" },
742 { 219, 0, 0, 0, "%E, %D, %H", "1, 2, 03" },
743 { 0, -1, 0, 0, "%H:%M:%S", "-00:01:00" },
744 { 0, 0, -1, 0, "%H:%M:%S", "-00:00:01" },
747 for ( size_t n
= 0; n
< WXSIZEOF(testSpans
); n
++ )
749 const TimeSpanFormatTestData
& td
= testSpans
[n
];
750 wxTimeSpan
ts(td
.h
, td
.min
, td
.sec
, td
.msec
);
751 CPPUNIT_ASSERT_EQUAL( td
.result
, ts
.Format(td
.fmt
) );
755 void DateTimeTestCase::TestTimeTicks()
757 static const wxDateTime::TimeZone
TZ_LOCAL(wxDateTime::Local
);
758 static const wxDateTime::TimeZone
TZ_TEST(wxDateTime::NZST
);
760 // this offset is needed to make the test work in any time zone when we
761 // only have expected test results in UTC in testDates
762 static const long tzOffset
= TZ_LOCAL
.GetOffset() - TZ_TEST
.GetOffset();
764 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
766 const Date
& d
= testDates
[n
];
767 if ( d
.gmticks
== -1 )
770 wxDateTime dt
= d
.DT().MakeTimezone(TZ_TEST
, true /* no DST */);
772 // GetValue() returns internal UTC-based representation, we need to
773 // convert it to local TZ before comparing
774 time_t ticks
= (dt
.GetValue() / 1000).ToLong() + TZ_LOCAL
.GetOffset();
777 CPPUNIT_ASSERT_EQUAL( d
.gmticks
, ticks
+ tzOffset
);
779 dt
= d
.DT().FromTimezone(wxDateTime::UTC
);
780 ticks
= (dt
.GetValue() / 1000).ToLong();
781 CPPUNIT_ASSERT_EQUAL( d
.gmticks
, ticks
);
785 // test parsing dates in RFC822 format
786 void DateTimeTestCase::TestParceRFC822()
788 static const struct ParseTestData
791 Date date
; // NB: this should be in UTC
796 "Sat, 18 Dec 1999 00:46:40 +0100",
797 { 17, wxDateTime::Dec
, 1999, 23, 46, 40 },
801 "Wed, 1 Dec 1999 05:17:20 +0300",
802 { 1, wxDateTime::Dec
, 1999, 2, 17, 20 },
806 "Sun, 28 Aug 2005 03:31:30 +0200",
807 { 28, wxDateTime::Aug
, 2005, 1, 31, 30 },
812 "Sat, 18 Dec 1999 10:48:30 -0500",
813 { 18, wxDateTime::Dec
, 1999, 15, 48, 30 },
817 // seconds are optional according to the RFC
819 "Sun, 01 Jun 2008 16:30 +0200",
820 { 1, wxDateTime::Jun
, 2008, 14, 30, 00 },
824 // try some bogus ones too
826 "Sun, 01 Jun 2008 16:30: +0200",
832 for ( unsigned n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
834 const char * const datestr
= parseTestDates
[n
].rfc822
;
837 if ( dt
.ParseRfc822Date(datestr
) )
840 ("Erroneously parsed \"%s\"", datestr
),
841 parseTestDates
[n
].good
844 wxDateTime dtReal
= parseTestDates
[n
].date
.DT().FromUTC();
845 CPPUNIT_ASSERT_EQUAL( dtReal
, dt
);
847 else // failed to parse
850 ("Failed to parse \"%s\"", datestr
),
851 !parseTestDates
[n
].good
857 // test parsing dates in free format
858 void DateTimeTestCase::TestDateParse()
860 static const struct ParseTestData
863 Date date
; // NB: this should be in UTC
867 { "21 Mar 2006", { 21, wxDateTime::Mar
, 2006 }, true },
868 { "29 Feb 1976", { 29, wxDateTime::Feb
, 1976 }, true },
869 { "Feb 29 1976", { 29, wxDateTime::Feb
, 1976 }, true },
870 { "31/03/06", { 31, wxDateTime::Mar
, 6 }, true },
871 { "31/03/2006", { 31, wxDateTime::Mar
, 2006 }, true },
873 // some invalid ones too
881 CPPUNIT_ASSERT( dt
.ParseDate(_T("today")) );
882 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), dt
);
884 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
886 if ( dt
.ParseDate(parseTestDates
[n
].str
) )
888 CPPUNIT_ASSERT( parseTestDates
[n
].good
);
890 CPPUNIT_ASSERT_EQUAL( parseTestDates
[n
].date
.DT(), dt
);
892 else // failed to parse
894 CPPUNIT_ASSERT( !parseTestDates
[n
].good
);
899 void DateTimeTestCase::TestDateParseISO()
904 Date date
; // NB: this should be in UTC
908 { "2006-03-21", { 21, wxDateTime::Mar
, 2006 }, true },
909 { "1976-02-29", { 29, wxDateTime::Feb
, 1976 }, true },
910 { "0006-03-31", { 31, wxDateTime::Mar
, 6 }, true },
912 // some invalid ones too
922 wxDateTime::wxDateTime_t hour
, min
, sec
;
926 { "13:42:17", 13, 42, 17, true },
927 { "02:17:01", 2, 17, 1, true },
929 // some invalid ones too
936 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
939 if ( dt
.ParseISODate(parseTestDates
[n
].str
) )
941 CPPUNIT_ASSERT( parseTestDates
[n
].good
);
943 CPPUNIT_ASSERT_EQUAL( parseTestDates
[n
].date
.DT(), dt
);
945 for ( size_t m
= 0; m
< WXSIZEOF(parseTestTimes
); m
++ )
948 dtCombined
<< parseTestDates
[n
].str
950 << parseTestTimes
[m
].str
;
952 if ( dt
.ParseISOCombined(dtCombined
) )
954 CPPUNIT_ASSERT( parseTestTimes
[m
].good
);
956 CPPUNIT_ASSERT_EQUAL( parseTestTimes
[m
].hour
, dt
.GetHour()) ;
957 CPPUNIT_ASSERT_EQUAL( parseTestTimes
[m
].min
, dt
.GetMinute()) ;
958 CPPUNIT_ASSERT_EQUAL( parseTestTimes
[m
].sec
, dt
.GetSecond()) ;
960 else // failed to parse combined date/time
962 CPPUNIT_ASSERT( !parseTestTimes
[m
].good
);
966 else // failed to parse
968 CPPUNIT_ASSERT( !parseTestDates
[n
].good
);
973 void DateTimeTestCase::TestDateTimeParse()
975 static const struct ParseTestData
978 Date date
; // NB: this should be in UTC
982 { "Thu 22 Nov 2007 07:40:00 PM",
983 { 22, wxDateTime::Nov
, 2007, 19, 40, 0}, true },
988 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
990 if ( dt
.ParseDateTime(parseTestDates
[n
].str
) )
992 CPPUNIT_ASSERT( parseTestDates
[n
].good
);
994 CPPUNIT_ASSERT_EQUAL( parseTestDates
[n
].date
.DT(), dt
);
996 else // failed to parse
998 CPPUNIT_ASSERT( !parseTestDates
[n
].good
);
1003 void DateTimeTestCase::TestTimeArithmetics()
1005 static const wxDateSpan testArithmData
[] =
1009 wxDateSpan::Month(),
1013 // the test will *not* work with arbitrary date!
1014 wxDateTime
dt(2, wxDateTime::Dec
, 1999),
1018 for ( size_t n
= 0; n
< WXSIZEOF(testArithmData
); n
++ )
1020 const wxDateSpan
& span
= testArithmData
[n
];
1024 CPPUNIT_ASSERT( dt1
- span
== dt
);
1025 CPPUNIT_ASSERT( dt2
+ span
== dt
);
1026 CPPUNIT_ASSERT( dt2
+ 2*span
== dt1
);
1030 void DateTimeTestCase::TestDSTBug()
1032 /////////////////////////
1034 wxDateTime dt
= wxDateTime::GetEndDST(2004, wxDateTime::France
);
1035 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
1036 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
1037 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
1038 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
1039 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
1040 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
1041 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
1043 /////////////////////////
1046 CPPUNIT_ASSERT_EQUAL(5, (int)dt
.GetHour());
1048 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
1049 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
1050 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
1051 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetHour());
1052 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
1053 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
1054 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
1057 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
1059 /////////////////////////
1061 #ifdef CHANGE_SYSTEM_DATE
1063 DateChanger
change(2004, 10, 31, 5, 0, 0);
1064 dt
= wxDateTime::Today();
1067 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
1068 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
1069 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
1070 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetHour());
1071 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
1072 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
1073 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
1075 /////////////////////////
1076 // Test Set(hour, minute, second, milli)
1079 DateChanger
change(2004, 10, 31, 5, 0, 0);
1080 dt
.Set(1, 30, 0, 0);
1081 dt2
.Set(5, 30, 0, 0);
1084 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
1085 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
1086 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
1087 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
1088 CPPUNIT_ASSERT_EQUAL(30, (int)dt
.GetMinute());
1089 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
1090 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
1092 CPPUNIT_ASSERT_EQUAL(31, (int)dt2
.GetDay());
1093 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt2
.GetMonth());
1094 CPPUNIT_ASSERT_EQUAL(2004, (int)dt2
.GetYear());
1095 CPPUNIT_ASSERT_EQUAL(5, (int)dt2
.GetHour());
1096 CPPUNIT_ASSERT_EQUAL(30, (int)dt2
.GetMinute());
1097 CPPUNIT_ASSERT_EQUAL(0, (int)dt2
.GetSecond());
1098 CPPUNIT_ASSERT_EQUAL(0, (int)dt2
.GetMillisecond());
1099 #endif // CHANGE_SYSTEM_DATE
1102 void DateTimeTestCase::TestDateOnly()
1104 wxDateTime
dt(19, wxDateTime::Jan
, 2007, 15, 01, 00);
1106 static const wxDateTime::wxDateTime_t DATE_ZERO
= 0;
1107 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetHour() );
1108 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetMinute() );
1109 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetSecond() );
1110 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetMillisecond() );
1113 CPPUNIT_ASSERT_EQUAL( wxDateTime(19, wxDateTime::Jan
, 2007), dt
);
1115 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), wxDateTime::Now().GetDateOnly() );
1118 #endif // wxUSE_DATETIME