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 // ----------------------------------------------------------------------------
23 #include "wx/time.h" // wxGetTimeZone()
26 #include "wx/wxcrt.h" // for wxStrstr()
30 // to test Today() meaningfully we must be able to change the system date which
31 // is not usually the case, but if we're under Win32 we can try it -- define
32 // the macro below to do it
33 //#define CHANGE_SYSTEM_DATE
36 #undef CHANGE_SYSTEM_DATE
39 #ifdef CHANGE_SYSTEM_DATE
44 DateChanger(int year
, int month
, int day
, int hour
, int min
, int sec
)
55 ::GetSystemTime(&m_savedTime
);
56 ::GetTimeZoneInformation(&m_tzi
);
58 m_changed
= ::SetSystemTime(&st
) != 0;
65 ::SetSystemTime(&m_savedTime
);
66 ::SetTimeZoneInformation(&m_tzi
);
71 SYSTEMTIME m_savedTime
;
72 TIME_ZONE_INFORMATION m_tzi
;
76 #endif // CHANGE_SYSTEM_DATE
78 // helper function translating week day/month names from English to the current
80 static wxString
TranslateDate(const wxString
& str
)
82 // small optimization: if there are no alphabetic characters in the string,
83 // there is nothing to translate
84 wxString::const_iterator i
, end
= str
.end();
85 for ( i
= str
.begin(); i
!= end
; ++i
)
96 for ( wxDateTime::WeekDay wd
= wxDateTime::Sun
;
97 wd
< wxDateTime::Inv_WeekDay
;
102 wxDateTime::GetEnglishWeekDayName(wd
, wxDateTime::Name_Abbr
),
103 wxDateTime::GetWeekDayName(wd
, wxDateTime::Name_Abbr
)
107 for ( wxDateTime::Month mon
= wxDateTime::Jan
;
108 mon
< wxDateTime::Inv_Month
;
113 wxDateTime::GetEnglishMonthName(mon
, wxDateTime::Name_Abbr
),
114 wxDateTime::GetMonthName(mon
, wxDateTime::Name_Abbr
)
121 // ----------------------------------------------------------------------------
122 // broken down date representation used for testing
123 // ----------------------------------------------------------------------------
127 wxDateTime::wxDateTime_t day
;
128 wxDateTime::Month month
;
130 wxDateTime::wxDateTime_t hour
, min
, sec
;
132 wxDateTime::WeekDay wday
;
135 void Init(const wxDateTime::Tm
& tm
)
147 wxDateTime
DT() const
148 { return wxDateTime(day
, month
, year
, hour
, min
, sec
); }
150 bool SameDay(const wxDateTime::Tm
& tm
) const
152 return day
== tm
.mday
&& month
== tm
.mon
&& year
== tm
.year
;
155 wxString
Format() const
158 s
.Printf(wxT("%02d:%02d:%02d %10s %02d, %4d%s"),
160 wxDateTime::GetMonthName(month
).c_str(),
162 abs(wxDateTime::ConvertYearToBC(year
)),
163 year
> 0 ? wxT("AD") : wxT("BC"));
167 wxString
FormatDate() const
170 s
.Printf(wxT("%02d-%s-%4d%s"),
172 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
173 abs(wxDateTime::ConvertYearToBC(year
)),
174 year
> 0 ? wxT("AD") : wxT("BC"));
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
183 static const Date testDates
[] =
185 { 1, wxDateTime::Jan
, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu
, 0 },
186 { 7, wxDateTime::Feb
, 2036, 00, 00, 00, 2464730.5, wxDateTime::Thu
, -1 },
187 { 8, wxDateTime::Feb
, 2036, 00, 00, 00, 2464731.5, wxDateTime::Fri
, -1 },
188 { 1, wxDateTime::Jan
, 2037, 00, 00, 00, 2465059.5, wxDateTime::Thu
, -1 },
189 { 1, wxDateTime::Jan
, 2038, 00, 00, 00, 2465424.5, wxDateTime::Fri
, -1 },
190 { 21, wxDateTime::Jan
, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon
, -1 },
191 { 29, wxDateTime::May
, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat
, 202219200 },
192 { 29, wxDateTime::Feb
, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun
, 194400000 },
193 { 1, wxDateTime::Jan
, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon
, -1 },
194 { 1, wxDateTime::Jan
, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon
, -1 },
195 { 15, wxDateTime::Oct
, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri
, -1 },
196 { 4, wxDateTime::Oct
, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon
, -1 },
197 { 1, wxDateTime::Mar
, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu
, -1 },
198 { 1, wxDateTime::Jan
, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon
, -1 },
199 { 31, wxDateTime::Dec
, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun
, -1 },
200 { 1, wxDateTime::Jan
, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat
, -1 },
201 { 12, wxDateTime::Aug
, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri
, -1 },
202 { 12, wxDateTime::Aug
, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat
, -1 },
203 { 24, wxDateTime::Nov
, -4713, 00, 00, 00, -0.5, wxDateTime::Mon
, -1 },
207 // ----------------------------------------------------------------------------
209 // ----------------------------------------------------------------------------
211 class DateTimeTestCase
: public CppUnit::TestCase
214 DateTimeTestCase() { }
217 CPPUNIT_TEST_SUITE( DateTimeTestCase
);
218 CPPUNIT_TEST( TestLeapYears
);
219 CPPUNIT_TEST( TestTimeSet
);
220 CPPUNIT_TEST( TestTimeJDN
);
221 CPPUNIT_TEST( TestTimeWNumber
);
222 CPPUNIT_TEST( TestTimeWDays
);
223 CPPUNIT_TEST( TestTimeDST
);
224 CPPUNIT_TEST( TestTimeFormat
);
225 CPPUNIT_TEST( TestTimeParse
);
226 CPPUNIT_TEST( TestTimeSpanFormat
);
227 CPPUNIT_TEST( TestTimeTicks
);
228 CPPUNIT_TEST( TestParceRFC822
);
229 CPPUNIT_TEST( TestDateParse
);
230 CPPUNIT_TEST( TestDateParseISO
);
231 CPPUNIT_TEST( TestDateTimeParse
);
232 CPPUNIT_TEST( TestTimeArithmetics
);
233 CPPUNIT_TEST( TestDSTBug
);
234 CPPUNIT_TEST( TestDateOnly
);
235 CPPUNIT_TEST_SUITE_END();
237 void TestLeapYears();
240 void TestTimeWNumber();
241 void TestTimeWDays();
243 void TestTimeFormat();
244 void TestTimeParse();
245 void TestTimeSpanFormat();
246 void TestTimeTicks();
247 void TestParceRFC822();
248 void TestDateParse();
249 void TestDateParseISO();
250 void TestDateTimeParse();
251 void TestTimeArithmetics();
255 DECLARE_NO_COPY_CLASS(DateTimeTestCase
)
258 // register in the unnamed registry so that these tests are run by default
259 CPPUNIT_TEST_SUITE_REGISTRATION( DateTimeTestCase
);
261 // also include in its own registry so that these tests can be run alone
262 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DateTimeTestCase
, "DateTimeTestCase" );
264 // ============================================================================
266 // ============================================================================
268 // test leap years detection
269 void DateTimeTestCase::TestLeapYears()
271 static const struct LeapYearTestData
287 for ( size_t n
= 0; n
< WXSIZEOF(years
); n
++ )
289 const LeapYearTestData
& y
= years
[n
];
291 CPPUNIT_ASSERT_EQUAL( y
.isLeap
, wxDateTime::IsLeapYear(y
.year
) );
295 // test constructing wxDateTime objects
296 void DateTimeTestCase::TestTimeSet()
298 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
300 const Date
& d1
= testDates
[n
];
301 wxDateTime dt
= d1
.DT();
306 wxString s1
= d1
.Format(),
309 CPPUNIT_ASSERT_EQUAL( s1
, s2
);
313 // test conversions to JDN &c
314 void DateTimeTestCase::TestTimeJDN()
316 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
318 const Date
& d
= testDates
[n
];
319 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
321 // JDNs must be computed for UTC times
322 double jdn
= dt
.FromUTC().GetJulianDayNumber();
324 CPPUNIT_ASSERT_EQUAL( d
.jdn
, jdn
);
327 CPPUNIT_ASSERT_EQUAL( jdn
, dt
.GetJulianDayNumber() );
331 // test week days computation
332 void DateTimeTestCase::TestTimeWDays()
336 for ( n
= 0; n
< WXSIZEOF(testDates
); n
++ )
338 const Date
& d
= testDates
[n
];
339 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
341 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
342 CPPUNIT_ASSERT_EQUAL( d
.wday
, wday
);
345 // test SetToWeekDay()
346 struct WeekDateTestData
348 Date date
; // the real date (precomputed)
349 int nWeek
; // its week index in the month
350 wxDateTime::WeekDay wday
; // the weekday
351 wxDateTime::Month month
; // the month
352 int year
; // and the year
354 wxString
Format() const
357 switch ( nWeek
< -1 ? -nWeek
: nWeek
)
359 case 1: which
= wxT("first"); break;
360 case 2: which
= wxT("second"); break;
361 case 3: which
= wxT("third"); break;
362 case 4: which
= wxT("fourth"); break;
363 case 5: which
= wxT("fifth"); break;
365 case -1: which
= wxT("last"); break;
370 which
+= wxT(" from end");
373 s
.Printf(wxT("The %s %s of %s in %d"),
375 wxDateTime::GetWeekDayName(wday
).c_str(),
376 wxDateTime::GetMonthName(month
).c_str(),
383 // the array data was generated by the following python program
385 from DateTime import *
386 from whrandom import *
389 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
390 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
392 week = DateTimeDelta(7)
395 year = randint(1900, 2100)
396 month = randint(1, 12)
398 dt = DateTime(year, month, day)
399 wday = dt.day_of_week
401 countFromEnd = choice([-1, 1])
404 while dt.month is month:
405 dt = dt - countFromEnd * week
406 weekNum = weekNum + countFromEnd
408 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
410 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
411 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
414 static const WeekDateTestData weekDatesTestData
[] =
416 { { 20, wxDateTime::Mar
, 2045, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 3, wxDateTime::Mon
, wxDateTime::Mar
, 2045 },
417 { { 5, wxDateTime::Jun
, 1985, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -4, wxDateTime::Wed
, wxDateTime::Jun
, 1985 },
418 { { 12, wxDateTime::Nov
, 1961, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -3, wxDateTime::Sun
, wxDateTime::Nov
, 1961 },
419 { { 27, wxDateTime::Feb
, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -1, wxDateTime::Fri
, wxDateTime::Feb
, 2093 },
420 { { 4, wxDateTime::Jul
, 2070, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -4, wxDateTime::Fri
, wxDateTime::Jul
, 2070 },
421 { { 2, wxDateTime::Apr
, 1906, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -5, wxDateTime::Mon
, wxDateTime::Apr
, 1906 },
422 { { 19, wxDateTime::Jul
, 2023, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -2, wxDateTime::Wed
, wxDateTime::Jul
, 2023 },
423 { { 5, wxDateTime::May
, 1958, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -4, wxDateTime::Mon
, wxDateTime::May
, 1958 },
424 { { 11, wxDateTime::Aug
, 1900, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 2, wxDateTime::Sat
, wxDateTime::Aug
, 1900 },
425 { { 14, wxDateTime::Feb
, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 2, wxDateTime::Wed
, wxDateTime::Feb
, 1945 },
426 { { 25, wxDateTime::Jul
, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -1, wxDateTime::Tue
, wxDateTime::Jul
, 1967 },
427 { { 9, wxDateTime::May
, 1916, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -4, wxDateTime::Tue
, wxDateTime::May
, 1916 },
428 { { 20, wxDateTime::Jun
, 1927, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 3, wxDateTime::Mon
, wxDateTime::Jun
, 1927 },
429 { { 2, wxDateTime::Aug
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, wxDateTime::Wed
, wxDateTime::Aug
, 2000 },
430 { { 20, wxDateTime::Apr
, 2044, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 3, wxDateTime::Wed
, wxDateTime::Apr
, 2044 },
431 { { 20, wxDateTime::Feb
, 1932, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -2, wxDateTime::Sat
, wxDateTime::Feb
, 1932 },
432 { { 25, wxDateTime::Jul
, 2069, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 4, wxDateTime::Thu
, wxDateTime::Jul
, 2069 },
433 { { 3, wxDateTime::Apr
, 1925, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, wxDateTime::Fri
, wxDateTime::Apr
, 1925 },
434 { { 21, wxDateTime::Mar
, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 3, wxDateTime::Sat
, wxDateTime::Mar
, 2093 },
435 { { 3, wxDateTime::Dec
, 2074, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -5, wxDateTime::Mon
, wxDateTime::Dec
, 2074 }
439 for ( n
= 0; n
< WXSIZEOF(weekDatesTestData
); n
++ )
441 const WeekDateTestData
& wd
= weekDatesTestData
[n
];
443 dt
.SetToWeekDay(wd
.wday
, wd
.nWeek
, wd
.month
, wd
.year
);
445 const Date
& d
= wd
.date
;
446 CPPUNIT_ASSERT( d
.SameDay(dt
.GetTm()) );
450 // test the computation of (ISO) week numbers
451 void DateTimeTestCase::TestTimeWNumber()
453 struct WeekNumberTestData
455 Date date
; // the date
456 wxDateTime::wxDateTime_t week
; // the week number in the year
457 wxDateTime::wxDateTime_t wmon
; // the week number in the month
458 wxDateTime::wxDateTime_t wmon2
; // same but week starts with Sun
459 wxDateTime::wxDateTime_t dnum
; // day number in the year
462 // data generated with the following python script:
464 from DateTime import *
465 from whrandom import *
468 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
469 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
471 def GetMonthWeek(dt):
472 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
474 weekNumMonth = weekNumMonth + 53
477 def GetLastSundayBefore(dt):
478 if dt.iso_week[2] == 7:
481 return dt - DateTimeDelta(dt.iso_week[2])
484 year = randint(1900, 2100)
485 month = randint(1, 12)
487 dt = DateTime(year, month, day)
488 dayNum = dt.day_of_year
489 weekNum = dt.iso_week[1]
490 weekNumMonth = GetMonthWeek(dt)
493 dtSunday = GetLastSundayBefore(dt)
495 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
496 weekNumMonth2 = weekNumMonth2 + 1
497 dtSunday = dtSunday - DateTimeDelta(7)
499 data = { 'day': rjust(`day`, 2), \
500 'month': monthNames[month - 1], \
502 'weekNum': rjust(`weekNum`, 2), \
503 'weekNumMonth': weekNumMonth, \
504 'weekNumMonth2': weekNumMonth2, \
505 'dayNum': rjust(`dayNum`, 3) }
507 print " { { %(day)s, "\
508 "wxDateTime::%(month)s, "\
511 "%(weekNumMonth)s, "\
512 "%(weekNumMonth2)s, "\
513 "%(dayNum)s }," % data
516 static const WeekNumberTestData weekNumberTestDates
[] =
518 { { 27, wxDateTime::Dec
, 1966, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 52, 5, 5, 361 },
519 { { 22, wxDateTime::Jul
, 1926, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 29, 4, 4, 203 },
520 { { 22, wxDateTime::Oct
, 2076, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 43, 4, 4, 296 },
521 { { 1, wxDateTime::Jul
, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 26, 1, 1, 182 },
522 { { 8, wxDateTime::Nov
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 46, 2, 2, 313 },
523 { { 21, wxDateTime::Mar
, 1920, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 12, 3, 4, 81 },
524 { { 7, wxDateTime::Jan
, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 2, 2, 7 },
525 { { 19, wxDateTime::Oct
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 42, 4, 4, 292 },
526 { { 13, wxDateTime::Aug
, 1955, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 32, 2, 2, 225 },
527 { { 18, wxDateTime::Jul
, 2087, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 29, 3, 3, 199 },
528 { { 2, wxDateTime::Sep
, 2028, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 35, 1, 1, 246 },
529 { { 28, wxDateTime::Jul
, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 30, 5, 4, 209 },
530 { { 15, wxDateTime::Jun
, 1901, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 24, 3, 3, 166 },
531 { { 10, wxDateTime::Oct
, 1939, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 41, 3, 2, 283 },
532 { { 3, wxDateTime::Dec
, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 48, 1, 1, 337 },
533 { { 23, wxDateTime::Feb
, 1940, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 8, 4, 4, 54 },
534 { { 2, wxDateTime::Jan
, 1987, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 1, 1, 2 },
535 { { 11, wxDateTime::Aug
, 2079, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 32, 2, 2, 223 },
536 { { 2, wxDateTime::Feb
, 2063, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 5, 1, 1, 33 },
537 { { 16, wxDateTime::Oct
, 1942, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 42, 3, 3, 289 },
538 { { 30, wxDateTime::Dec
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 5, 5, 364 },
539 { { 2, wxDateTime::Jan
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 1, 1, 2 },
540 { { 5, wxDateTime::Jan
, 2010, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 2, 2, 5 },
541 { { 3, wxDateTime::Jan
, 2011, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 2, 2, 3 },
542 { { 31, wxDateTime::Dec
, 2009, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 53, 5, 5, 365 },
543 { { 31, wxDateTime::Dec
, 2012, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 6, 6, 366 },
544 { { 29, wxDateTime::Dec
, 2013, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 52, 5, 5, 363 },
545 { { 30, wxDateTime::Dec
, 2013, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 6, 5, 364 },
546 { { 31, wxDateTime::Dec
, 2013, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 6, 5, 365 },
549 for ( size_t n
= 0; n
< WXSIZEOF(weekNumberTestDates
); n
++ )
551 const WeekNumberTestData
& wn
= weekNumberTestDates
[n
];
552 const Date
& d
= wn
.date
;
554 wxDateTime dt
= d
.DT();
556 wxDateTime::wxDateTime_t
557 week
= dt
.GetWeekOfYear(wxDateTime::Monday_First
),
558 wmon
= dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
559 wmon2
= dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
560 dnum
= dt
.GetDayOfYear();
562 WX_ASSERT_EQUAL_MESSAGE( ("day of year for %s", d
.Format()),
564 WX_ASSERT_EQUAL_MESSAGE( ("week of month (Monday) for %s", d
.Format()),
566 WX_ASSERT_EQUAL_MESSAGE( ("week of month (Sunday) for %s", d
.Format()),
568 WX_ASSERT_EQUAL_MESSAGE( ("week of year for %s", d
.Format()),
572 if ( week
== 1 && d
.month
!= wxDateTime::Jan
)
574 // this means we're in the first week of the next year
579 dt2
= wxDateTime::SetToWeekOfYear(year
, week
, dt
.GetWeekDay());
580 CPPUNIT_ASSERT_EQUAL( dt
, dt2
);
584 // test DST applicability
585 void DateTimeTestCase::TestTimeDST()
587 // taken from http://www.energy.ca.gov/daylightsaving.html
588 static const Date datesDST
[2][2009 - 1990 + 1] =
591 { 1, wxDateTime::Apr
, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
592 { 7, wxDateTime::Apr
, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
593 { 5, wxDateTime::Apr
, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
594 { 4, wxDateTime::Apr
, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
595 { 3, wxDateTime::Apr
, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
596 { 2, wxDateTime::Apr
, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
597 { 7, wxDateTime::Apr
, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
598 { 6, wxDateTime::Apr
, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
599 { 5, wxDateTime::Apr
, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
600 { 4, wxDateTime::Apr
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
601 { 2, wxDateTime::Apr
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
602 { 1, wxDateTime::Apr
, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
603 { 7, wxDateTime::Apr
, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
604 { 6, wxDateTime::Apr
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
605 { 4, wxDateTime::Apr
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
606 { 3, wxDateTime::Apr
, 2005, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
607 { 2, wxDateTime::Apr
, 2006, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
608 {11, wxDateTime::Mar
, 2007, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
609 { 9, wxDateTime::Mar
, 2008, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
610 { 8, wxDateTime::Mar
, 2009, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
613 { 28, wxDateTime::Oct
, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
614 { 27, wxDateTime::Oct
, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
615 { 25, wxDateTime::Oct
, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
616 { 31, wxDateTime::Oct
, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
617 { 30, wxDateTime::Oct
, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
618 { 29, wxDateTime::Oct
, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
619 { 27, wxDateTime::Oct
, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
620 { 26, wxDateTime::Oct
, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
621 { 25, wxDateTime::Oct
, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
622 { 31, wxDateTime::Oct
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
623 { 29, wxDateTime::Oct
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
624 { 28, wxDateTime::Oct
, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
625 { 27, wxDateTime::Oct
, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
626 { 26, wxDateTime::Oct
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
627 { 31, wxDateTime::Oct
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
628 { 30, wxDateTime::Oct
, 2005, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
629 { 29, wxDateTime::Oct
, 2006, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
630 { 4, wxDateTime::Nov
, 2007, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
631 { 2, wxDateTime::Nov
, 2008, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
632 { 1, wxDateTime::Nov
, 2009, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
637 for ( size_t n
= 0; n
< WXSIZEOF(datesDST
[0]); n
++ )
639 const int year
= 1990 + n
;
640 wxDateTime dtBegin
= wxDateTime::GetBeginDST(year
, wxDateTime::USA
),
641 dtEnd
= wxDateTime::GetEndDST(year
, wxDateTime::USA
);
643 const Date
& dBegin
= datesDST
[0][n
];
644 const Date
& dEnd
= datesDST
[1][n
];
646 CPPUNIT_ASSERT_EQUAL( dBegin
.DT().FormatDate(), dtBegin
.FormatDate() );
647 CPPUNIT_ASSERT_EQUAL( dEnd
.DT().FormatDate(), dtEnd
.FormatDate() );
651 // test wxDateTime -> text conversion
652 void DateTimeTestCase::TestTimeFormat()
654 // some information may be lost during conversion, so store what kind
655 // of info should we recover after a round trip
658 CompareNone
, // don't try comparing
659 CompareBoth
, // dates and times should be identical
660 CompareYear
, // don't compare centuries (fails for 2 digit years)
661 CompareDate
, // dates only
662 CompareTime
// time only
667 CompareKind compareKind
;
669 } formatTestFormats
[] =
671 { CompareYear
, "---> %c" }, // %c could use 2 digit years
672 { CompareDate
, "Date is %A, %d of %B, in year %Y" },
673 { CompareYear
, "Date is %x, time is %X" }, // %x could use 2 digits
674 { CompareTime
, "Time is %H:%M:%S or %I:%M:%S %p" },
675 { CompareNone
, "The day of year: %j, the week of year: %W" },
676 { CompareDate
, "ISO date without separators: %Y%m%d" },
677 { CompareBoth
, "RFC 2822 string: %Y-%m-%d %H:%M:%S.%l %z" },
681 const long timeZonesOffsets
[] =
683 wxDateTime::TimeZone(wxDateTime::Local
).GetOffset(),
685 // Fictitious TimeZone offsets to ensure time zone formating and
686 // interpretation works
691 static const Date formatTestDates
[] =
693 { 29, wxDateTime::May
, 1976, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
694 { 31, wxDateTime::Dec
, 1999, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
695 { 6, wxDateTime::Feb
, 1937, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
696 { 6, wxDateTime::Feb
, 1856, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
697 { 6, wxDateTime::Feb
, 1857, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
698 { 29, wxDateTime::May
, 2076, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
700 // FIXME: the test with 02:15:25 time doesn't pass because of DST
701 // computation problems, we get back 03:15:25
702 { 29, wxDateTime::Feb
, 2400, 04, 15, 25, 0.0, wxDateTime::Inv_WeekDay
},
704 // Need to add support for BCE dates.
705 { 01, wxDateTime::Jan
, -52, 03, 16, 47, 0.0, wxDateTime::Inv_WeekDay
},
709 for ( unsigned idxtz
= 0; idxtz
< WXSIZEOF(timeZonesOffsets
); ++idxtz
)
711 wxDateTime::TimeZone
tz(timeZonesOffsets
[idxtz
]);
712 const bool isLocalTz
= tz
.GetOffset() == -wxGetTimeZone();
714 for ( size_t d
= 0; d
< WXSIZEOF(formatTestDates
); d
++ )
716 wxDateTime dt
= formatTestDates
[d
].DT();
717 for ( unsigned n
= 0; n
< WXSIZEOF(formatTestFormats
); n
++ )
719 const char *fmt
= formatTestFormats
[n
].format
;
721 // skip the check with %p for those locales which have empty AM/PM strings:
722 // for those locales it's impossible to pass the test with %p...
724 wxDateTime::GetAmPmStrings(&am
, &pm
);
725 if (am
.empty() && pm
.empty() && wxStrstr(fmt
, "%p") != NULL
)
728 // what can we recover?
729 CompareKind kind
= formatTestFormats
[n
].compareKind
;
731 // When using a different time zone we must perform a time zone
732 // conversion below which doesn't always work correctly, check
733 // for the cases when it doesn't.
736 // DST computation doesn't work correctly for dates above
737 // 2038 currently on the systems with 32 bit time_t.
738 if ( dt
.GetYear() >= 2038 )
741 // We can't compare just dates nor just times when doing TZ
742 // conversion as both are affected by the DST: for the
743 // dates, the DST can switch midnight to 23:00 of the
744 // previous day while for the times DST can be different
745 // for the original date and today.
746 if ( kind
== CompareDate
|| kind
== CompareTime
)
750 // do convert date to string
751 wxString s
= dt
.Format(fmt
, tz
);
755 const char *result
= dt2
.ParseFormat(s
, fmt
);
758 // conversion failed - should it have?
760 ("Test #%u failed: failed to parse \"%s\"", n
, s
),
764 else // conversion succeeded
766 // currently ParseFormat() doesn't support "%Z" and so is
767 // incapable of parsing time zone part used at the end of date
768 // representations in many (but not "C") locales, compensate
769 // for it ourselves by simply consuming and ignoring it
770 while ( *result
&& (*result
>= 'A' && *result
<= 'Z') )
774 ("Test #%u failed: \"%s\" was left unparsed in \"%s\"",
779 // Without "%z" we can't recover the time zone used in the
780 // call to Format() so we need to call MakeFromTimezone()
782 if ( !strstr(fmt
, "%z") && !isLocalTz
)
783 dt2
.MakeFromTimezone(tz
);
788 if ( dt2
.GetCentury() != dt
.GetCentury() )
790 CPPUNIT_ASSERT_EQUAL(dt
.GetYear() % 100,
791 dt2
.GetYear() % 100);
793 dt2
.SetYear(dt
.GetYear());
795 // fall through and compare everything
798 CPPUNIT_ASSERT_EQUAL( dt
, dt2
);
802 CPPUNIT_ASSERT( dt
.IsSameDate(dt2
) );
806 CPPUNIT_ASSERT( dt
.IsSameTime(dt2
) );
810 wxFAIL_MSG( wxT("unexpected") );
821 // special case which was known to fail
822 CPPUNIT_ASSERT( dt
.ParseFormat("02/06/1856", "%x") );
823 CPPUNIT_ASSERT_EQUAL( 1856, dt
.GetYear() );
826 // also test %l separately
827 CPPUNIT_ASSERT( dt
.ParseFormat("12:23:45.678", "%H:%M:%S.%l") );
828 CPPUNIT_ASSERT_EQUAL( 678, dt
.GetMillisecond() );
830 // test special case of %l matching 0 milliseconds
831 CPPUNIT_ASSERT( dt
.ParseFormat("12:23:45.000", "%H:%M:%S.%l") );
832 CPPUNIT_ASSERT_EQUAL( 0, dt
.GetMillisecond() );
834 // test partially specified dates too
835 wxDateTime
dtDef(26, wxDateTime::Sep
, 2008);
836 CPPUNIT_ASSERT( dt
.ParseFormat("17", "%d") );
837 CPPUNIT_ASSERT_EQUAL( 17, dt
.GetDay() );
839 // test some degenerate cases
840 CPPUNIT_ASSERT( !dt
.ParseFormat("", "%z") );
841 CPPUNIT_ASSERT( !dt
.ParseFormat("", "%%") );
843 // test compilation of some calls which should compile (and not result in
844 // ambiguity because of char*<->wxCStrData<->wxString conversions)
846 CPPUNIT_ASSERT( !dt
.ParseFormat("foo") );
847 CPPUNIT_ASSERT( !dt
.ParseFormat(wxT("foo")) );
848 CPPUNIT_ASSERT( !dt
.ParseFormat(s
) );
849 dt
.ParseFormat(s
.c_str()); // Simply test compilation of this one.
851 CPPUNIT_ASSERT( !dt
.ParseFormat("foo", "%c") );
852 CPPUNIT_ASSERT( !dt
.ParseFormat(wxT("foo"), "%c") );
853 CPPUNIT_ASSERT( !dt
.ParseFormat(s
, "%c") );
854 dt
.ParseFormat(s
.c_str(), "%c");
856 CPPUNIT_ASSERT( !dt
.ParseFormat("foo", wxT("%c")) );
857 CPPUNIT_ASSERT( !dt
.ParseFormat(wxT("foo"), wxT("%c")) );
858 CPPUNIT_ASSERT( !dt
.ParseFormat(s
, "%c") );
859 dt
.ParseFormat(s
.c_str(), wxT("%c"));
862 CPPUNIT_ASSERT( !dt
.ParseFormat("foo", spec
) );
863 CPPUNIT_ASSERT( !dt
.ParseFormat(wxT("foo"), spec
) );
864 CPPUNIT_ASSERT( !dt
.ParseFormat(s
, spec
) );
865 dt
.ParseFormat(s
.c_str(), spec
);
868 // Test parsing time in free format.
869 void DateTimeTestCase::TestTimeParse()
873 // Parsing standard formats should work.
874 CPPUNIT_ASSERT( dt
.ParseTime("12:34:56") );
875 CPPUNIT_ASSERT_EQUAL( "12:34:56", dt
.FormatISOTime() );
877 // Parsing just hours should work too.
879 CPPUNIT_ASSERT( dt
.ParseTime("17") );
880 CPPUNIT_ASSERT_EQUAL( "17:00:00", dt
.FormatISOTime() );
882 // Parsing gibberish shouldn't work.
883 CPPUNIT_ASSERT( !dt
.ParseTime("bloordyblop") );
886 void DateTimeTestCase::TestTimeSpanFormat()
888 static const struct TimeSpanFormatTestData
890 long h
, min
, sec
, msec
;
895 { 12, 34, 56, 789, "%H:%M:%S.%l", "12:34:56.789" },
896 { 1, 2, 3, 0, "%H:%M:%S", "01:02:03" },
897 { 1, 2, 3, 0, "%S", "3723" },
898 { -1, -2, -3, 0, "%S", "-3723" },
899 { -1, -2, -3, 0, "%H:%M:%S", "-01:02:03" },
900 { 26, 0, 0, 0, "%H", "26" },
901 { 26, 0, 0, 0, "%D, %H", "1, 02" },
902 { -26, 0, 0, 0, "%H", "-26" },
903 { -26, 0, 0, 0, "%D, %H", "-1, 02" },
904 { 219, 0, 0, 0, "%H", "219" },
905 { 219, 0, 0, 0, "%D, %H", "9, 03" },
906 { 219, 0, 0, 0, "%E, %D, %H", "1, 2, 03" },
907 { 0, -1, 0, 0, "%H:%M:%S", "-00:01:00" },
908 { 0, 0, -1, 0, "%H:%M:%S", "-00:00:01" },
911 for ( size_t n
= 0; n
< WXSIZEOF(testSpans
); n
++ )
913 const TimeSpanFormatTestData
& td
= testSpans
[n
];
914 wxTimeSpan
ts(td
.h
, td
.min
, td
.sec
, td
.msec
);
915 CPPUNIT_ASSERT_EQUAL( td
.result
, ts
.Format(td
.fmt
) );
919 void DateTimeTestCase::TestTimeTicks()
921 static const wxDateTime::TimeZone
TZ_LOCAL(wxDateTime::Local
);
922 static const wxDateTime::TimeZone
TZ_TEST(wxDateTime::NZST
);
924 // this offset is needed to make the test work in any time zone when we
925 // only have expected test results in UTC in testDates
926 static const long tzOffset
= TZ_LOCAL
.GetOffset() - TZ_TEST
.GetOffset();
928 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
930 const Date
& d
= testDates
[n
];
931 if ( d
.gmticks
== -1 )
934 wxDateTime dt
= d
.DT().MakeTimezone(TZ_TEST
, true /* no DST */);
936 // GetValue() returns internal UTC-based representation, we need to
937 // convert it to local TZ before comparing
938 time_t ticks
= (dt
.GetValue() / 1000).ToLong() + TZ_LOCAL
.GetOffset();
941 CPPUNIT_ASSERT_EQUAL( d
.gmticks
, ticks
+ tzOffset
);
943 dt
= d
.DT().FromTimezone(wxDateTime::UTC
);
944 ticks
= (dt
.GetValue() / 1000).ToLong();
945 CPPUNIT_ASSERT_EQUAL( d
.gmticks
, ticks
);
949 // test parsing dates in RFC822 format
950 void DateTimeTestCase::TestParceRFC822()
952 static const struct ParseTestData
955 Date date
; // NB: this should be in UTC
960 "Sat, 18 Dec 1999 00:46:40 +0100",
961 { 17, wxDateTime::Dec
, 1999, 23, 46, 40 },
965 "Wed, 1 Dec 1999 05:17:20 +0300",
966 { 1, wxDateTime::Dec
, 1999, 2, 17, 20 },
970 "Sun, 28 Aug 2005 03:31:30 +0200",
971 { 28, wxDateTime::Aug
, 2005, 1, 31, 30 },
976 "Sat, 18 Dec 1999 10:48:30 -0500",
977 { 18, wxDateTime::Dec
, 1999, 15, 48, 30 },
981 // seconds are optional according to the RFC
983 "Sun, 01 Jun 2008 16:30 +0200",
984 { 1, wxDateTime::Jun
, 2008, 14, 30, 00 },
988 // try some bogus ones too
990 "Sun, 01 Jun 2008 16:30: +0200",
996 for ( unsigned n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
998 const char * const datestr
= parseTestDates
[n
].rfc822
;
1001 if ( dt
.ParseRfc822Date(datestr
) )
1004 ("Erroneously parsed \"%s\"", datestr
),
1005 parseTestDates
[n
].good
1008 wxDateTime dtReal
= parseTestDates
[n
].date
.DT().FromUTC();
1009 CPPUNIT_ASSERT_EQUAL( dtReal
, dt
);
1011 else // failed to parse
1014 ("Failed to parse \"%s\"", datestr
),
1015 !parseTestDates
[n
].good
1021 // test parsing dates in free format
1022 void DateTimeTestCase::TestDateParse()
1024 static const struct ParseTestData
1027 Date date
; // NB: this should be in UTC
1029 } parseTestDates
[] =
1031 { "21 Mar 2006", { 21, wxDateTime::Mar
, 2006 }, true },
1032 { "29 Feb 1976", { 29, wxDateTime::Feb
, 1976 }, true },
1033 { "Feb 29 1976", { 29, wxDateTime::Feb
, 1976 }, true },
1034 { "31/03/06", { 31, wxDateTime::Mar
, 6 }, true },
1035 { "31/03/2006", { 31, wxDateTime::Mar
, 2006 }, true },
1037 // some invalid ones too
1046 CPPUNIT_ASSERT( dt
.ParseDate(wxT("today")) );
1047 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), dt
);
1049 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
1051 const wxString datestr
= TranslateDate(parseTestDates
[n
].str
);
1053 const char * const end
= dt
.ParseDate(datestr
);
1057 ("Erroneously parsed \"%s\"", datestr
),
1058 parseTestDates
[n
].good
1061 CPPUNIT_ASSERT_EQUAL( parseTestDates
[n
].date
.DT(), dt
);
1063 else // failed to parse
1066 ("Failed to parse \"%s\"", datestr
),
1067 !parseTestDates
[n
].good
1072 // Check that incomplete parse works correctly.
1073 const char* p
= dt
.ParseFormat("2012-03-23 12:34:56", "%Y-%m-%d");
1074 CPPUNIT_ASSERT_EQUAL( " 12:34:56", wxString(p
) );
1077 void DateTimeTestCase::TestDateParseISO()
1082 Date date
; // NB: this should be in UTC
1084 } parseTestDates
[] =
1086 { "2006-03-21", { 21, wxDateTime::Mar
, 2006 }, true },
1087 { "1976-02-29", { 29, wxDateTime::Feb
, 1976 }, true },
1088 { "0006-03-31", { 31, wxDateTime::Mar
, 6 }, true },
1090 // some invalid ones too
1100 wxDateTime::wxDateTime_t hour
, min
, sec
;
1102 } parseTestTimes
[] =
1104 { "13:42:17", 13, 42, 17, true },
1105 { "02:17:01", 2, 17, 1, true },
1107 // some invalid ones too
1114 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
1117 if ( dt
.ParseISODate(parseTestDates
[n
].str
) )
1119 CPPUNIT_ASSERT( parseTestDates
[n
].good
);
1121 CPPUNIT_ASSERT_EQUAL( parseTestDates
[n
].date
.DT(), dt
);
1123 for ( size_t m
= 0; m
< WXSIZEOF(parseTestTimes
); m
++ )
1125 wxString dtCombined
;
1126 dtCombined
<< parseTestDates
[n
].str
1128 << parseTestTimes
[m
].str
;
1130 if ( dt
.ParseISOCombined(dtCombined
) )
1132 CPPUNIT_ASSERT( parseTestTimes
[m
].good
);
1134 CPPUNIT_ASSERT_EQUAL( parseTestTimes
[m
].hour
, dt
.GetHour()) ;
1135 CPPUNIT_ASSERT_EQUAL( parseTestTimes
[m
].min
, dt
.GetMinute()) ;
1136 CPPUNIT_ASSERT_EQUAL( parseTestTimes
[m
].sec
, dt
.GetSecond()) ;
1138 else // failed to parse combined date/time
1140 CPPUNIT_ASSERT( !parseTestTimes
[m
].good
);
1144 else // failed to parse
1146 CPPUNIT_ASSERT( !parseTestDates
[n
].good
);
1151 void DateTimeTestCase::TestDateTimeParse()
1153 static const struct ParseTestData
1156 Date date
; // NB: this should be in UTC
1158 } parseTestDates
[] =
1161 "Thu 22 Nov 2007 07:40:00 PM",
1162 { 22, wxDateTime::Nov
, 2007, 19, 40, 0 },
1168 { 4, wxDateTime::Jan
, 2010, 14, 30, 0 },
1174 { 1, wxDateTime::Jan
, 9999, 0, 0, 0},
1179 "2012-01-01 10:12:05 +0100",
1180 { 1, wxDateTime::Jan
, 2012, 10, 12, 5, -1 },
1181 false // ParseDateTime does know yet +0100
1185 // the test strings here use "PM" which is not available in all locales so
1186 // we need to use "C" locale for them
1190 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
1192 const wxString datestr
= TranslateDate(parseTestDates
[n
].str
);
1194 const char * const end
= dt
.ParseDateTime(datestr
);
1198 ("Erroneously parsed \"%s\"", datestr
),
1199 parseTestDates
[n
].good
1202 CPPUNIT_ASSERT_EQUAL( parseTestDates
[n
].date
.DT(), dt
);
1204 else // failed to parse
1207 ("Failed to parse \"%s\"", datestr
),
1208 !parseTestDates
[n
].good
1211 CPPUNIT_ASSERT( !parseTestDates
[n
].good
);
1216 void DateTimeTestCase::TestTimeArithmetics()
1218 static const wxDateSpan testArithmData
[] =
1222 wxDateSpan::Month(),
1226 // the test will *not* work with arbitrary date!
1227 wxDateTime
dt(2, wxDateTime::Dec
, 1999),
1231 for ( size_t n
= 0; n
< WXSIZEOF(testArithmData
); n
++ )
1233 const wxDateSpan
& span
= testArithmData
[n
];
1237 CPPUNIT_ASSERT_EQUAL( dt
, dt1
- span
);
1238 CPPUNIT_ASSERT_EQUAL( dt
, dt2
+ span
);
1239 CPPUNIT_ASSERT_EQUAL( dt1
, dt2
+ 2*span
);
1240 CPPUNIT_ASSERT_EQUAL( span
, dt1
.DiffAsDateSpan(dt
) );
1243 // More date span arithmetics tests
1244 wxDateTime
dtd1(5, wxDateTime::Jun
, 1998);
1245 wxDateTime
dtd2(6, wxDateTime::Aug
, 1999);
1247 // All parts in dtd2 is after dtd1
1248 CPPUNIT_ASSERT_EQUAL( wxDateSpan(1, 2, 0, 1), dtd2
.DiffAsDateSpan(dtd1
) );
1250 // Year and month after, day earlier, so no full month
1251 // Jul has 31 days, so it's 31 - 5 + 4 = 30, or 4w 2d
1252 dtd2
.Set(4, wxDateTime::Aug
, 1999);
1253 CPPUNIT_ASSERT_EQUAL( wxDateSpan(1, 1, 4, 2), dtd2
.DiffAsDateSpan(dtd1
) );
1255 // Year and day after, month earlier, so no full year, but same day diff as
1257 dtd2
.Set(6, wxDateTime::May
, 1999);
1258 CPPUNIT_ASSERT_EQUAL( wxDateSpan(0, 11, 0, 1), dtd2
.DiffAsDateSpan(dtd1
) );
1260 // Year after, month and day earlier, so no full month and no full year
1261 // April has 30 days, so it's 30 - 5 + 4 = 29, or 4w 1d
1262 dtd2
.Set(4, wxDateTime::May
, 1999);
1263 CPPUNIT_ASSERT_EQUAL( wxDateSpan(0, 10, 4, 1), dtd2
.DiffAsDateSpan(dtd1
) );
1265 // And a reverse. Now we should use days in Jun (again 30 => 4w 1d)
1266 CPPUNIT_ASSERT_EQUAL( wxDateSpan(0, -10, -4, -1), dtd1
.DiffAsDateSpan(dtd2
) );
1269 void DateTimeTestCase::TestDSTBug()
1271 /////////////////////////
1273 wxDateTime dt
= wxDateTime::GetEndDST(2004, wxDateTime::France
);
1274 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
1275 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
1276 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
1277 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
1278 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
1279 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
1280 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
1282 /////////////////////////
1285 CPPUNIT_ASSERT_EQUAL(5, (int)dt
.GetHour());
1287 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
1288 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
1289 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
1290 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetHour());
1291 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
1292 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
1293 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
1296 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
1298 /////////////////////////
1300 #ifdef CHANGE_SYSTEM_DATE
1302 DateChanger
change(2004, 10, 31, 5, 0, 0);
1303 dt
= wxDateTime::Today();
1306 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
1307 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
1308 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
1309 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetHour());
1310 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
1311 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
1312 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
1314 /////////////////////////
1315 // Test Set(hour, minute, second, milli)
1318 DateChanger
change(2004, 10, 31, 5, 0, 0);
1319 dt
.Set(1, 30, 0, 0);
1320 dt2
.Set(5, 30, 0, 0);
1323 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
1324 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
1325 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
1326 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
1327 CPPUNIT_ASSERT_EQUAL(30, (int)dt
.GetMinute());
1328 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
1329 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
1331 CPPUNIT_ASSERT_EQUAL(31, (int)dt2
.GetDay());
1332 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt2
.GetMonth());
1333 CPPUNIT_ASSERT_EQUAL(2004, (int)dt2
.GetYear());
1334 CPPUNIT_ASSERT_EQUAL(5, (int)dt2
.GetHour());
1335 CPPUNIT_ASSERT_EQUAL(30, (int)dt2
.GetMinute());
1336 CPPUNIT_ASSERT_EQUAL(0, (int)dt2
.GetSecond());
1337 CPPUNIT_ASSERT_EQUAL(0, (int)dt2
.GetMillisecond());
1338 #endif // CHANGE_SYSTEM_DATE
1341 void DateTimeTestCase::TestDateOnly()
1343 wxDateTime
dt(19, wxDateTime::Jan
, 2007, 15, 01, 00);
1345 static const wxDateTime::wxDateTime_t DATE_ZERO
= 0;
1346 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetHour() );
1347 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetMinute() );
1348 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetSecond() );
1349 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetMillisecond() );
1352 CPPUNIT_ASSERT_EQUAL( wxDateTime(19, wxDateTime::Jan
, 2007), dt
);
1354 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), wxDateTime::Now().GetDateOnly() );
1357 #endif // wxUSE_DATETIME