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/ioswrap.h"
28 // need this to be able to use CPPUNIT_ASSERT_EQUAL with wxDateTime objects
29 static wxSTD ostream
& operator<<(wxSTD ostream
& ostr
, const wxDateTime
& dt
)
31 ostr
<< dt
.FormatISODate() << _T(" ") << dt
.FormatISOTime();
36 // to test Today() meaningfully we must be able to change the system date which
37 // is not usually the case, but if we're under Win32 we can try it -- define
38 // the macro below to do it
39 //#define CHANGE_SYSTEM_DATE
42 #undef CHANGE_SYSTEM_DATE
45 #ifdef CHANGE_SYSTEM_DATE
50 DateChanger(int year
, int month
, int day
, int hour
, int min
, int sec
)
61 ::GetSystemTime(&m_savedTime
);
62 ::GetTimeZoneInformation(&m_tzi
);
64 m_changed
= ::SetSystemTime(&st
) != 0;
71 ::SetSystemTime(&m_savedTime
);
72 ::SetTimeZoneInformation(&m_tzi
);
77 SYSTEMTIME m_savedTime
;
78 TIME_ZONE_INFORMATION m_tzi
;
82 #endif // CHANGE_SYSTEM_DATE
84 // ----------------------------------------------------------------------------
85 // broken down date representation used for testing
86 // ----------------------------------------------------------------------------
90 wxDateTime::wxDateTime_t day
;
91 wxDateTime::Month month
;
93 wxDateTime::wxDateTime_t hour
, min
, sec
;
95 wxDateTime::WeekDay wday
;
96 time_t gmticks
, ticks
;
98 void Init(const wxDateTime::Tm
& tm
)
107 gmticks
= ticks
= -1;
110 wxDateTime
DT() const
111 { return wxDateTime(day
, month
, year
, hour
, min
, sec
); }
113 bool SameDay(const wxDateTime::Tm
& tm
) const
115 return day
== tm
.mday
&& month
== tm
.mon
&& year
== tm
.year
;
118 wxString
Format() const
121 s
.Printf(_T("%02d:%02d:%02d %10s %02d, %4d%s"),
123 wxDateTime::GetMonthName(month
).c_str(),
125 abs(wxDateTime::ConvertYearToBC(year
)),
126 year
> 0 ? _T("AD") : _T("BC"));
130 wxString
FormatDate() const
133 s
.Printf(_T("%02d-%s-%4d%s"),
135 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
136 abs(wxDateTime::ConvertYearToBC(year
)),
137 year
> 0 ? _T("AD") : _T("BC"));
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 static const Date testDates
[] =
148 { 1, wxDateTime::Jan
, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu
, 0, -3600 },
149 { 7, wxDateTime::Feb
, 2036, 00, 00, 00, 2464730.5, wxDateTime::Thu
, -1, -1 },
150 { 8, wxDateTime::Feb
, 2036, 00, 00, 00, 2464731.5, wxDateTime::Fri
, -1, -1 },
151 { 1, wxDateTime::Jan
, 2037, 00, 00, 00, 2465059.5, wxDateTime::Thu
, -1, -1 },
152 { 1, wxDateTime::Jan
, 2038, 00, 00, 00, 2465424.5, wxDateTime::Fri
, -1, -1 },
153 { 21, wxDateTime::Jan
, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon
, -1, -1 },
154 { 29, wxDateTime::May
, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat
, 202219200, 202212000 },
155 { 29, wxDateTime::Feb
, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun
, 194400000, 194396400 },
156 { 1, wxDateTime::Jan
, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon
, -1, -1 },
157 { 1, wxDateTime::Jan
, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon
, -1, -1 },
158 { 15, wxDateTime::Oct
, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri
, -1, -1 },
159 { 4, wxDateTime::Oct
, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon
, -1, -1 },
160 { 1, wxDateTime::Mar
, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu
, -1, -1 },
161 { 1, wxDateTime::Jan
, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon
, -1, -1 },
162 { 31, wxDateTime::Dec
, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun
, -1, -1 },
163 { 1, wxDateTime::Jan
, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat
, -1, -1 },
164 { 12, wxDateTime::Aug
, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri
, -1, -1 },
165 { 12, wxDateTime::Aug
, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat
, -1, -1 },
166 { 24, wxDateTime::Nov
, -4713, 00, 00, 00, -0.5, wxDateTime::Mon
, -1, -1 },
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
174 class DateTimeTestCase
: public CppUnit::TestCase
177 DateTimeTestCase() { }
180 CPPUNIT_TEST_SUITE( DateTimeTestCase
);
181 CPPUNIT_TEST( TestLeapYears
);
182 CPPUNIT_TEST( TestTimeSet
);
183 CPPUNIT_TEST( TestTimeJDN
);
184 CPPUNIT_TEST( TestTimeWNumber
);
185 CPPUNIT_TEST( TestTimeWDays
);
186 CPPUNIT_TEST( TestTimeDST
);
187 CPPUNIT_TEST( TestTimeFormat
);
188 CPPUNIT_TEST( TestTimeSpanFormat
);
189 CPPUNIT_TEST( TestTimeTicks
);
190 CPPUNIT_TEST( TestParceRFC822
);
191 CPPUNIT_TEST( TestDateParse
);
192 CPPUNIT_TEST( TestTimeArithmetics
);
193 CPPUNIT_TEST( TestDSTBug
);
194 CPPUNIT_TEST_SUITE_END();
196 void TestLeapYears();
199 void TestTimeWNumber();
200 void TestTimeWDays();
202 void TestTimeFormat();
203 void TestTimeSpanFormat();
204 void TestTimeTicks();
205 void TestParceRFC822();
206 void TestDateParse();
207 void TestTimeArithmetics();
210 DECLARE_NO_COPY_CLASS(DateTimeTestCase
)
213 // register in the unnamed registry so that these tests are run by default
214 CPPUNIT_TEST_SUITE_REGISTRATION( DateTimeTestCase
);
216 // also include in it's own registry so that these tests can be run alone
217 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DateTimeTestCase
, "DateTimeTestCase" );
219 // ============================================================================
221 // ============================================================================
223 // test leap years detection
224 void DateTimeTestCase::TestLeapYears()
226 static const struct LeapYearTestData
242 for ( size_t n
= 0; n
< WXSIZEOF(years
); n
++ )
244 const LeapYearTestData
& y
= years
[n
];
246 CPPUNIT_ASSERT( wxDateTime::IsLeapYear(y
.year
) == y
.isLeap
);
250 // test constructing wxDateTime objects
251 void DateTimeTestCase::TestTimeSet()
253 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
255 const Date
& d1
= testDates
[n
];
256 wxDateTime dt
= d1
.DT();
261 wxString s1
= d1
.Format(),
264 CPPUNIT_ASSERT( s1
== s2
);
268 // test conversions to JDN &c
269 void DateTimeTestCase::TestTimeJDN()
271 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
273 const Date
& d
= testDates
[n
];
274 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
276 // JDNs must be computed for UTC times
277 double jdn
= dt
.FromUTC().GetJulianDayNumber();
279 CPPUNIT_ASSERT( jdn
== d
.jdn
);
282 CPPUNIT_ASSERT( dt
.GetJulianDayNumber() == jdn
);
286 // test week days computation
287 void DateTimeTestCase::TestTimeWDays()
291 for ( n
= 0; n
< WXSIZEOF(testDates
); n
++ )
293 const Date
& d
= testDates
[n
];
294 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
296 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
297 CPPUNIT_ASSERT( wday
== d
.wday
);
300 // test SetToWeekDay()
301 struct WeekDateTestData
303 Date date
; // the real date (precomputed)
304 int nWeek
; // its week index in the month
305 wxDateTime::WeekDay wday
; // the weekday
306 wxDateTime::Month month
; // the month
307 int year
; // and the year
309 wxString
Format() const
312 switch ( nWeek
< -1 ? -nWeek
: nWeek
)
314 case 1: which
= _T("first"); break;
315 case 2: which
= _T("second"); break;
316 case 3: which
= _T("third"); break;
317 case 4: which
= _T("fourth"); break;
318 case 5: which
= _T("fifth"); break;
320 case -1: which
= _T("last"); break;
325 which
+= _T(" from end");
328 s
.Printf(_T("The %s %s of %s in %d"),
330 wxDateTime::GetWeekDayName(wday
).c_str(),
331 wxDateTime::GetMonthName(month
).c_str(),
338 // the array data was generated by the following python program
340 from DateTime import *
341 from whrandom import *
344 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
345 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
347 week = DateTimeDelta(7)
350 year = randint(1900, 2100)
351 month = randint(1, 12)
353 dt = DateTime(year, month, day)
354 wday = dt.day_of_week
356 countFromEnd = choice([-1, 1])
359 while dt.month is month:
360 dt = dt - countFromEnd * week
361 weekNum = weekNum + countFromEnd
363 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
365 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
366 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
369 static const WeekDateTestData weekDatesTestData
[] =
371 { { 20, wxDateTime::Mar
, 2045, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 3, wxDateTime::Mon
, wxDateTime::Mar
, 2045 },
372 { { 5, wxDateTime::Jun
, 1985, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -4, wxDateTime::Wed
, wxDateTime::Jun
, 1985 },
373 { { 12, wxDateTime::Nov
, 1961, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -3, wxDateTime::Sun
, wxDateTime::Nov
, 1961 },
374 { { 27, wxDateTime::Feb
, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -1, wxDateTime::Fri
, wxDateTime::Feb
, 2093 },
375 { { 4, wxDateTime::Jul
, 2070, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -4, wxDateTime::Fri
, wxDateTime::Jul
, 2070 },
376 { { 2, wxDateTime::Apr
, 1906, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -5, wxDateTime::Mon
, wxDateTime::Apr
, 1906 },
377 { { 19, wxDateTime::Jul
, 2023, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -2, wxDateTime::Wed
, wxDateTime::Jul
, 2023 },
378 { { 5, wxDateTime::May
, 1958, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -4, wxDateTime::Mon
, wxDateTime::May
, 1958 },
379 { { 11, wxDateTime::Aug
, 1900, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 2, wxDateTime::Sat
, wxDateTime::Aug
, 1900 },
380 { { 14, wxDateTime::Feb
, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 2, wxDateTime::Wed
, wxDateTime::Feb
, 1945 },
381 { { 25, wxDateTime::Jul
, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -1, wxDateTime::Tue
, wxDateTime::Jul
, 1967 },
382 { { 9, wxDateTime::May
, 1916, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -4, wxDateTime::Tue
, wxDateTime::May
, 1916 },
383 { { 20, wxDateTime::Jun
, 1927, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 3, wxDateTime::Mon
, wxDateTime::Jun
, 1927 },
384 { { 2, wxDateTime::Aug
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, wxDateTime::Wed
, wxDateTime::Aug
, 2000 },
385 { { 20, wxDateTime::Apr
, 2044, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 3, wxDateTime::Wed
, wxDateTime::Apr
, 2044 },
386 { { 20, wxDateTime::Feb
, 1932, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -2, wxDateTime::Sat
, wxDateTime::Feb
, 1932 },
387 { { 25, wxDateTime::Jul
, 2069, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 4, wxDateTime::Thu
, wxDateTime::Jul
, 2069 },
388 { { 3, wxDateTime::Apr
, 1925, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, wxDateTime::Fri
, wxDateTime::Apr
, 1925 },
389 { { 21, wxDateTime::Mar
, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 3, wxDateTime::Sat
, wxDateTime::Mar
, 2093 },
390 { { 3, wxDateTime::Dec
, 2074, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, -5, wxDateTime::Mon
, wxDateTime::Dec
, 2074 }
394 for ( n
= 0; n
< WXSIZEOF(weekDatesTestData
); n
++ )
396 const WeekDateTestData
& wd
= weekDatesTestData
[n
];
398 dt
.SetToWeekDay(wd
.wday
, wd
.nWeek
, wd
.month
, wd
.year
);
400 const Date
& d
= wd
.date
;
401 CPPUNIT_ASSERT( d
.SameDay(dt
.GetTm()) );
405 // test the computation of (ISO) week numbers
406 void DateTimeTestCase::TestTimeWNumber()
408 struct WeekNumberTestData
410 Date date
; // the date
411 wxDateTime::wxDateTime_t week
; // the week number in the year
412 wxDateTime::wxDateTime_t wmon
; // the week number in the month
413 wxDateTime::wxDateTime_t wmon2
; // same but week starts with Sun
414 wxDateTime::wxDateTime_t dnum
; // day number in the year
417 // data generated with the following python script:
419 from DateTime import *
420 from whrandom import *
423 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
424 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
426 def GetMonthWeek(dt):
427 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
429 weekNumMonth = weekNumMonth + 53
432 def GetLastSundayBefore(dt):
433 if dt.iso_week[2] == 7:
436 return dt - DateTimeDelta(dt.iso_week[2])
439 year = randint(1900, 2100)
440 month = randint(1, 12)
442 dt = DateTime(year, month, day)
443 dayNum = dt.day_of_year
444 weekNum = dt.iso_week[1]
445 weekNumMonth = GetMonthWeek(dt)
448 dtSunday = GetLastSundayBefore(dt)
450 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
451 weekNumMonth2 = weekNumMonth2 + 1
452 dtSunday = dtSunday - DateTimeDelta(7)
454 data = { 'day': rjust(`day`, 2), \
455 'month': monthNames[month - 1], \
457 'weekNum': rjust(`weekNum`, 2), \
458 'weekNumMonth': weekNumMonth, \
459 'weekNumMonth2': weekNumMonth2, \
460 'dayNum': rjust(`dayNum`, 3) }
462 print " { { %(day)s, "\
463 "wxDateTime::%(month)s, "\
466 "%(weekNumMonth)s, "\
467 "%(weekNumMonth2)s, "\
468 "%(dayNum)s }," % data
471 static const WeekNumberTestData weekNumberTestDates
[] =
473 { { 27, wxDateTime::Dec
, 1966, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 52, 5, 5, 361 },
474 { { 22, wxDateTime::Jul
, 1926, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 29, 4, 4, 203 },
475 { { 22, wxDateTime::Oct
, 2076, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 43, 4, 4, 296 },
476 { { 1, wxDateTime::Jul
, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 26, 1, 1, 182 },
477 { { 8, wxDateTime::Nov
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 46, 2, 2, 313 },
478 { { 21, wxDateTime::Mar
, 1920, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 12, 3, 4, 81 },
479 { { 7, wxDateTime::Jan
, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, 2, 2, 7 },
480 { { 19, wxDateTime::Oct
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 42, 4, 4, 292 },
481 { { 13, wxDateTime::Aug
, 1955, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 32, 2, 2, 225 },
482 { { 18, wxDateTime::Jul
, 2087, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 29, 3, 3, 199 },
483 { { 2, wxDateTime::Sep
, 2028, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 35, 1, 1, 246 },
484 { { 28, wxDateTime::Jul
, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 30, 5, 4, 209 },
485 { { 15, wxDateTime::Jun
, 1901, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 24, 3, 3, 166 },
486 { { 10, wxDateTime::Oct
, 1939, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 41, 3, 2, 283 },
487 { { 3, wxDateTime::Dec
, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 48, 1, 1, 337 },
488 { { 23, wxDateTime::Feb
, 1940, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 8, 4, 4, 54 },
489 { { 2, wxDateTime::Jan
, 1987, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, 1, 1, 2 },
490 { { 11, wxDateTime::Aug
, 2079, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 32, 2, 2, 223 },
491 { { 2, wxDateTime::Feb
, 2063, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 5, 1, 1, 33 },
492 { { 16, wxDateTime::Oct
, 1942, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 42, 3, 3, 289 },
493 { { 30, wxDateTime::Dec
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, 5, 5, 364 },
494 { { 2, wxDateTime::Jan
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 }, 1, 1, 1, 2 },
497 for ( size_t n
= 0; n
< WXSIZEOF(weekNumberTestDates
); n
++ )
499 const WeekNumberTestData
& wn
= weekNumberTestDates
[n
];
500 const Date
& d
= wn
.date
;
502 wxDateTime dt
= d
.DT();
504 wxDateTime::wxDateTime_t
505 week
= dt
.GetWeekOfYear(wxDateTime::Monday_First
),
506 wmon
= dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
507 wmon2
= dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
508 dnum
= dt
.GetDayOfYear();
510 CPPUNIT_ASSERT( dnum
== wn
.dnum
);
511 CPPUNIT_ASSERT( wmon
== wn
.wmon
);
512 CPPUNIT_ASSERT( wmon2
== wn
.wmon2
);
513 CPPUNIT_ASSERT( week
== wn
.week
);
516 if ( week
== 1 && d
.month
!= wxDateTime::Jan
)
518 // this means we're in the first week of the next year
523 dt2
= wxDateTime::SetToWeekOfYear(year
, week
, dt
.GetWeekDay());
524 CPPUNIT_ASSERT( dt2
== dt
);
528 // test DST applicability
529 void DateTimeTestCase::TestTimeDST()
531 // taken from http://www.energy.ca.gov/daylightsaving.html
532 static const Date datesDST
[2][2004 - 1900 + 1] =
535 { 1, wxDateTime::Apr
, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
536 { 7, wxDateTime::Apr
, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
537 { 5, wxDateTime::Apr
, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
538 { 4, wxDateTime::Apr
, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
539 { 3, wxDateTime::Apr
, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
540 { 2, wxDateTime::Apr
, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
541 { 7, wxDateTime::Apr
, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
542 { 6, wxDateTime::Apr
, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
543 { 5, wxDateTime::Apr
, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
544 { 4, wxDateTime::Apr
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
545 { 2, wxDateTime::Apr
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
546 { 1, wxDateTime::Apr
, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
547 { 7, wxDateTime::Apr
, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
548 { 6, wxDateTime::Apr
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
549 { 4, wxDateTime::Apr
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
552 { 28, wxDateTime::Oct
, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
553 { 27, wxDateTime::Oct
, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
554 { 25, wxDateTime::Oct
, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
555 { 31, wxDateTime::Oct
, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
556 { 30, wxDateTime::Oct
, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
557 { 29, wxDateTime::Oct
, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
558 { 27, wxDateTime::Oct
, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
559 { 26, wxDateTime::Oct
, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
560 { 25, wxDateTime::Oct
, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
561 { 31, wxDateTime::Oct
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
562 { 29, wxDateTime::Oct
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
563 { 28, wxDateTime::Oct
, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
564 { 27, wxDateTime::Oct
, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
565 { 26, wxDateTime::Oct
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
566 { 31, wxDateTime::Oct
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
570 for ( int year
= 1990; year
< 2005; year
++ )
572 wxDateTime dtBegin
= wxDateTime::GetBeginDST(year
, wxDateTime::USA
),
573 dtEnd
= wxDateTime::GetEndDST(year
, wxDateTime::USA
);
575 size_t n
= year
- 1990;
576 const Date
& dBegin
= datesDST
[0][n
];
577 const Date
& dEnd
= datesDST
[1][n
];
579 CPPUNIT_ASSERT( dBegin
.SameDay(dtBegin
.GetTm()) );
580 CPPUNIT_ASSERT( dEnd
.SameDay(dtEnd
.GetTm()) );
584 // test wxDateTime -> text conversion
585 void DateTimeTestCase::TestTimeFormat()
587 // some information may be lost during conversion, so store what kind
588 // of info should we recover after a round trip
591 CompareNone
, // don't try comparing
592 CompareBoth
, // dates and times should be identical
593 CompareYear
, // don't compare centuries (fails for 2 digit years)
594 CompareDate
, // dates only
595 CompareTime
// time only
600 CompareKind compareKind
;
601 const wxChar
*format
;
602 } formatTestFormats
[] =
604 { CompareYear
, _T("---> %c") }, // %c could use 2 digit years
605 { CompareDate
, _T("Date is %A, %d of %B, in year %Y") },
606 { CompareYear
, _T("Date is %x, time is %X") }, // %x could use 2 digits
607 { CompareTime
, _T("Time is %H:%M:%S or %I:%M:%S %p") },
608 { CompareNone
, _T("The day of year: %j, the week of year: %W") },
609 { CompareDate
, _T("ISO date without separators: %Y%m%d") },
612 static const Date formatTestDates
[] =
614 { 29, wxDateTime::May
, 1976, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
615 { 31, wxDateTime::Dec
, 1999, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
616 { 6, wxDateTime::Feb
, 1937, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
617 { 6, wxDateTime::Feb
, 1856, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
618 { 6, wxDateTime::Feb
, 1857, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
619 { 29, wxDateTime::May
, 2076, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
620 { 29, wxDateTime::Feb
, 2400, 02, 15, 25, 0.0, wxDateTime::Inv_WeekDay
},
622 // Need to add support for BCE dates.
623 { 01, wxDateTime::Jan
, -52, 03, 16, 47, 0.0, wxDateTime::Inv_WeekDay
},
627 for ( size_t d
= 0; d
< WXSIZEOF(formatTestDates
); d
++ )
629 wxDateTime dt
= formatTestDates
[d
].DT();
630 for ( size_t n
= 0; n
< WXSIZEOF(formatTestFormats
); n
++ )
632 const wxChar
*fmt
= formatTestFormats
[n
].format
;
633 wxString s
= dt
.Format(fmt
);
635 // what can we recover?
636 CompareKind kind
= formatTestFormats
[n
].compareKind
;
640 const wxChar
*result
= dt2
.ParseFormat(s
, fmt
);
643 // converion failed - should it have?
644 CPPUNIT_ASSERT( kind
== CompareNone
);
646 else // conversion succeeded
648 // should have parsed the entire string
649 CPPUNIT_ASSERT( !*result
);
654 if ( dt2
.GetCentury() != dt
.GetCentury() )
656 CPPUNIT_ASSERT_EQUAL(dt
.GetYear() % 100,
657 dt2
.GetYear() % 100);
659 dt2
.SetYear(dt
.GetYear());
661 // fall through and compare everything
664 CPPUNIT_ASSERT_EQUAL( dt
, dt2
);
668 CPPUNIT_ASSERT( dt
.IsSameDate(dt2
) );
672 CPPUNIT_ASSERT( dt
.IsSameTime(dt2
) );
676 wxFAIL_MSG( _T("unexpected") );
684 void DateTimeTestCase::TestTimeSpanFormat()
686 static const struct TimeSpanFormatTestData
688 long h
, min
, sec
, msec
;
690 const wxChar
*result
;
693 { 12, 34, 56, 789, _T("%H:%M:%S.%l"), _T("12:34:56.789") },
694 { 1, 2, 3, 0, _T("%H:%M:%S"), _T("01:02:03") },
695 { 1, 2, 3, 0, _T("%S"), _T("3723") },
696 { -1, -2, -3, 0, _T("%S"), _T("-3723") },
697 { -1, -2, -3, 0, _T("%H:%M:%S"), _T("-01:02:03") },
698 { 26, 0, 0, 0, _T("%H"), _T("26") },
699 { 26, 0, 0, 0, _T("%D, %H"), _T("1, 02") },
700 { -26, 0, 0, 0, _T("%H"), _T("-26") },
701 { -26, 0, 0, 0, _T("%D, %H"), _T("-1, 02") },
702 { 219, 0, 0, 0, _T("%H"), _T("219") },
703 { 219, 0, 0, 0, _T("%D, %H"), _T("9, 03") },
704 { 219, 0, 0, 0, _T("%E, %D, %H"), _T("1, 2, 03") },
707 for ( size_t n
= 0; n
< WXSIZEOF(testSpans
); n
++ )
709 const TimeSpanFormatTestData
& td
= testSpans
[n
];
710 wxTimeSpan
ts(td
.h
, td
.min
, td
.sec
, td
.msec
);
711 CPPUNIT_ASSERT_EQUAL( wxString(td
.result
), ts
.Format(td
.fmt
) );
715 void DateTimeTestCase::TestTimeTicks()
717 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
719 const Date
& d
= testDates
[n
];
723 wxDateTime dt
= d
.DT();
724 //RN: Translate according to test's time zone
725 //2nd param is to ignore DST - it's already factored
727 dt
.MakeTimezone(wxDateTime::WEST
, true);
728 long ticks
= (dt
.GetValue() / 1000).ToLong();
729 CPPUNIT_ASSERT( ticks
== d
.ticks
);
731 dt
= d
.DT().FromTimezone(wxDateTime::GMT0
);
732 ticks
= (dt
.GetValue() / 1000).ToLong();
733 CPPUNIT_ASSERT( ticks
== d
.gmticks
);
737 // test parsing dates in RFC822 format
738 void DateTimeTestCase::TestParceRFC822()
740 static const struct ParseTestData
742 const wxChar
*rfc822
;
743 Date date
; // NB: this should be in UTC
748 _T("Sat, 18 Dec 1999 00:46:40 +0100"),
749 { 17, wxDateTime::Dec
, 1999, 23, 46, 40, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
753 _T("Wed, 1 Dec 1999 05:17:20 +0300"),
754 { 1, wxDateTime::Dec
, 1999, 2, 17, 20, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
758 _T("Sun, 28 Aug 2005 03:31:30 +0200"),
759 { 28, wxDateTime::Aug
, 2005, 1, 31, 30, 0.0, wxDateTime::Inv_WeekDay
, 0, 0 },
764 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
767 if ( dt
.ParseRfc822Date(parseTestDates
[n
].rfc822
) )
769 CPPUNIT_ASSERT( parseTestDates
[n
].good
);
771 wxDateTime dtReal
= parseTestDates
[n
].date
.DT().FromUTC();
772 CPPUNIT_ASSERT_EQUAL( dtReal
, dt
);
774 else // failed to parse
776 CPPUNIT_ASSERT( !parseTestDates
[n
].good
);
781 // test parsing dates in free format
782 void DateTimeTestCase::TestDateParse()
784 static const struct ParseTestData
787 Date date
; // NB: this should be in UTC
791 { _T("21 Mar 2006"), { 21, wxDateTime::Mar
, 2006 }, true },
792 { _T("29 Feb 1976"), { 29, wxDateTime::Feb
, 1976 }, true },
793 { _T("Feb 29 1976"), { 29, wxDateTime::Feb
, 1976 }, true },
794 { _T("31/03/06"), { 31, wxDateTime::Mar
, 6 }, true },
795 { _T("31/03/2006"), { 31, wxDateTime::Mar
, 2006 }, true },
797 // some invalid ones too
798 { _T("29 Feb 2006") },
800 { _T("bloordyblop") }
805 CPPUNIT_ASSERT( dt
.ParseDate(_T("today")) );
806 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), dt
);
808 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
811 if ( dt
.ParseDate(parseTestDates
[n
].str
) )
813 CPPUNIT_ASSERT( parseTestDates
[n
].good
);
815 CPPUNIT_ASSERT_EQUAL( parseTestDates
[n
].date
.DT(), dt
);
817 else // failed to parse
819 CPPUNIT_ASSERT( !parseTestDates
[n
].good
);
824 void DateTimeTestCase::TestTimeArithmetics()
826 static const wxDateSpan testArithmData
[] =
834 // the test will *not* work with arbitrary date!
835 wxDateTime
dt(2, wxDateTime::Dec
, 1999),
839 for ( size_t n
= 0; n
< WXSIZEOF(testArithmData
); n
++ )
841 const wxDateSpan
& span
= testArithmData
[n
];
845 CPPUNIT_ASSERT( dt1
- span
== dt
);
846 CPPUNIT_ASSERT( dt2
+ span
== dt
);
847 CPPUNIT_ASSERT( dt2
+ 2*span
== dt1
);
851 void DateTimeTestCase::TestDSTBug()
853 /////////////////////////
855 wxDateTime dt
= wxDateTime::GetEndDST(2004);
856 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
857 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
858 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
859 CPPUNIT_ASSERT_EQUAL(2, (int)dt
.GetHour());
860 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
861 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
862 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
864 /////////////////////////
867 CPPUNIT_ASSERT_EQUAL(5, (int)dt
.GetHour());
869 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
870 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
871 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
872 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetHour());
873 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
874 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
875 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
878 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
880 /////////////////////////
882 #ifdef CHANGE_SYSTEM_DATE
884 DateChanger
change(2004, 10, 31, 5, 0, 0);
885 dt
= wxDateTime::Today();
888 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
889 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
890 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
891 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetHour());
892 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
893 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
894 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
896 /////////////////////////
897 // Test Set(hour, minute, second, milli)
900 DateChanger
change(2004, 10, 31, 5, 0, 0);
902 dt2
.Set(5, 30, 0, 0);
905 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
906 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
907 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
908 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
909 CPPUNIT_ASSERT_EQUAL(30, (int)dt
.GetMinute());
910 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
911 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
913 CPPUNIT_ASSERT_EQUAL(31, (int)dt2
.GetDay());
914 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt2
.GetMonth());
915 CPPUNIT_ASSERT_EQUAL(2004, (int)dt2
.GetYear());
916 CPPUNIT_ASSERT_EQUAL(5, (int)dt2
.GetHour());
917 CPPUNIT_ASSERT_EQUAL(30, (int)dt2
.GetMinute());
918 CPPUNIT_ASSERT_EQUAL(0, (int)dt2
.GetSecond());
919 CPPUNIT_ASSERT_EQUAL(0, (int)dt2
.GetMillisecond());
920 #endif // CHANGE_SYSTEM_DATE
923 #endif // wxUSE_DATETIME