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( TestTimeSpanFormat
);
226 CPPUNIT_TEST( TestTimeTicks
);
227 CPPUNIT_TEST( TestParceRFC822
);
228 CPPUNIT_TEST( TestDateParse
);
229 CPPUNIT_TEST( TestDateParseISO
);
230 CPPUNIT_TEST( TestDateTimeParse
);
231 CPPUNIT_TEST( TestTimeArithmetics
);
232 CPPUNIT_TEST( TestDSTBug
);
233 CPPUNIT_TEST( TestDateOnly
);
234 CPPUNIT_TEST_SUITE_END();
236 void TestLeapYears();
239 void TestTimeWNumber();
240 void TestTimeWDays();
242 void TestTimeFormat();
243 void TestTimeSpanFormat();
244 void TestTimeTicks();
245 void TestParceRFC822();
246 void TestDateParse();
247 void TestDateParseISO();
248 void TestDateTimeParse();
249 void TestTimeArithmetics();
253 DECLARE_NO_COPY_CLASS(DateTimeTestCase
)
256 // register in the unnamed registry so that these tests are run by default
257 CPPUNIT_TEST_SUITE_REGISTRATION( DateTimeTestCase
);
259 // also include in its own registry so that these tests can be run alone
260 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DateTimeTestCase
, "DateTimeTestCase" );
262 // ============================================================================
264 // ============================================================================
266 // test leap years detection
267 void DateTimeTestCase::TestLeapYears()
269 static const struct LeapYearTestData
285 for ( size_t n
= 0; n
< WXSIZEOF(years
); n
++ )
287 const LeapYearTestData
& y
= years
[n
];
289 CPPUNIT_ASSERT_EQUAL( y
.isLeap
, wxDateTime::IsLeapYear(y
.year
) );
293 // test constructing wxDateTime objects
294 void DateTimeTestCase::TestTimeSet()
296 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
298 const Date
& d1
= testDates
[n
];
299 wxDateTime dt
= d1
.DT();
304 wxString s1
= d1
.Format(),
307 CPPUNIT_ASSERT_EQUAL( s1
, s2
);
311 // test conversions to JDN &c
312 void DateTimeTestCase::TestTimeJDN()
314 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
316 const Date
& d
= testDates
[n
];
317 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
319 // JDNs must be computed for UTC times
320 double jdn
= dt
.FromUTC().GetJulianDayNumber();
322 CPPUNIT_ASSERT_EQUAL( d
.jdn
, jdn
);
325 CPPUNIT_ASSERT_EQUAL( jdn
, dt
.GetJulianDayNumber() );
329 // test week days computation
330 void DateTimeTestCase::TestTimeWDays()
334 for ( n
= 0; n
< WXSIZEOF(testDates
); n
++ )
336 const Date
& d
= testDates
[n
];
337 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
339 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
340 CPPUNIT_ASSERT_EQUAL( d
.wday
, wday
);
343 // test SetToWeekDay()
344 struct WeekDateTestData
346 Date date
; // the real date (precomputed)
347 int nWeek
; // its week index in the month
348 wxDateTime::WeekDay wday
; // the weekday
349 wxDateTime::Month month
; // the month
350 int year
; // and the year
352 wxString
Format() const
355 switch ( nWeek
< -1 ? -nWeek
: nWeek
)
357 case 1: which
= wxT("first"); break;
358 case 2: which
= wxT("second"); break;
359 case 3: which
= wxT("third"); break;
360 case 4: which
= wxT("fourth"); break;
361 case 5: which
= wxT("fifth"); break;
363 case -1: which
= wxT("last"); break;
368 which
+= wxT(" from end");
371 s
.Printf(wxT("The %s %s of %s in %d"),
373 wxDateTime::GetWeekDayName(wday
).c_str(),
374 wxDateTime::GetMonthName(month
).c_str(),
381 // the array data was generated by the following python program
383 from DateTime import *
384 from whrandom import *
387 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
388 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
390 week = DateTimeDelta(7)
393 year = randint(1900, 2100)
394 month = randint(1, 12)
396 dt = DateTime(year, month, day)
397 wday = dt.day_of_week
399 countFromEnd = choice([-1, 1])
402 while dt.month is month:
403 dt = dt - countFromEnd * week
404 weekNum = weekNum + countFromEnd
406 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
408 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
409 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
412 static const WeekDateTestData weekDatesTestData
[] =
414 { { 20, wxDateTime::Mar
, 2045, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 3, wxDateTime::Mon
, wxDateTime::Mar
, 2045 },
415 { { 5, wxDateTime::Jun
, 1985, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -4, wxDateTime::Wed
, wxDateTime::Jun
, 1985 },
416 { { 12, wxDateTime::Nov
, 1961, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -3, wxDateTime::Sun
, wxDateTime::Nov
, 1961 },
417 { { 27, wxDateTime::Feb
, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -1, wxDateTime::Fri
, wxDateTime::Feb
, 2093 },
418 { { 4, wxDateTime::Jul
, 2070, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -4, wxDateTime::Fri
, wxDateTime::Jul
, 2070 },
419 { { 2, wxDateTime::Apr
, 1906, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -5, wxDateTime::Mon
, wxDateTime::Apr
, 1906 },
420 { { 19, wxDateTime::Jul
, 2023, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -2, wxDateTime::Wed
, wxDateTime::Jul
, 2023 },
421 { { 5, wxDateTime::May
, 1958, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -4, wxDateTime::Mon
, wxDateTime::May
, 1958 },
422 { { 11, wxDateTime::Aug
, 1900, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 2, wxDateTime::Sat
, wxDateTime::Aug
, 1900 },
423 { { 14, wxDateTime::Feb
, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 2, wxDateTime::Wed
, wxDateTime::Feb
, 1945 },
424 { { 25, wxDateTime::Jul
, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -1, wxDateTime::Tue
, wxDateTime::Jul
, 1967 },
425 { { 9, wxDateTime::May
, 1916, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -4, wxDateTime::Tue
, wxDateTime::May
, 1916 },
426 { { 20, wxDateTime::Jun
, 1927, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 3, wxDateTime::Mon
, wxDateTime::Jun
, 1927 },
427 { { 2, wxDateTime::Aug
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, wxDateTime::Wed
, wxDateTime::Aug
, 2000 },
428 { { 20, wxDateTime::Apr
, 2044, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 3, wxDateTime::Wed
, wxDateTime::Apr
, 2044 },
429 { { 20, wxDateTime::Feb
, 1932, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -2, wxDateTime::Sat
, wxDateTime::Feb
, 1932 },
430 { { 25, wxDateTime::Jul
, 2069, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 4, wxDateTime::Thu
, wxDateTime::Jul
, 2069 },
431 { { 3, wxDateTime::Apr
, 1925, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, wxDateTime::Fri
, wxDateTime::Apr
, 1925 },
432 { { 21, wxDateTime::Mar
, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 3, wxDateTime::Sat
, wxDateTime::Mar
, 2093 },
433 { { 3, wxDateTime::Dec
, 2074, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -5, wxDateTime::Mon
, wxDateTime::Dec
, 2074 }
437 for ( n
= 0; n
< WXSIZEOF(weekDatesTestData
); n
++ )
439 const WeekDateTestData
& wd
= weekDatesTestData
[n
];
441 dt
.SetToWeekDay(wd
.wday
, wd
.nWeek
, wd
.month
, wd
.year
);
443 const Date
& d
= wd
.date
;
444 CPPUNIT_ASSERT( d
.SameDay(dt
.GetTm()) );
448 // test the computation of (ISO) week numbers
449 void DateTimeTestCase::TestTimeWNumber()
451 struct WeekNumberTestData
453 Date date
; // the date
454 wxDateTime::wxDateTime_t week
; // the week number in the year
455 wxDateTime::wxDateTime_t wmon
; // the week number in the month
456 wxDateTime::wxDateTime_t wmon2
; // same but week starts with Sun
457 wxDateTime::wxDateTime_t dnum
; // day number in the year
460 // data generated with the following python script:
462 from DateTime import *
463 from whrandom import *
466 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
467 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
469 def GetMonthWeek(dt):
470 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
472 weekNumMonth = weekNumMonth + 53
475 def GetLastSundayBefore(dt):
476 if dt.iso_week[2] == 7:
479 return dt - DateTimeDelta(dt.iso_week[2])
482 year = randint(1900, 2100)
483 month = randint(1, 12)
485 dt = DateTime(year, month, day)
486 dayNum = dt.day_of_year
487 weekNum = dt.iso_week[1]
488 weekNumMonth = GetMonthWeek(dt)
491 dtSunday = GetLastSundayBefore(dt)
493 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
494 weekNumMonth2 = weekNumMonth2 + 1
495 dtSunday = dtSunday - DateTimeDelta(7)
497 data = { 'day': rjust(`day`, 2), \
498 'month': monthNames[month - 1], \
500 'weekNum': rjust(`weekNum`, 2), \
501 'weekNumMonth': weekNumMonth, \
502 'weekNumMonth2': weekNumMonth2, \
503 'dayNum': rjust(`dayNum`, 3) }
505 print " { { %(day)s, "\
506 "wxDateTime::%(month)s, "\
509 "%(weekNumMonth)s, "\
510 "%(weekNumMonth2)s, "\
511 "%(dayNum)s }," % data
514 static const WeekNumberTestData weekNumberTestDates
[] =
516 { { 27, wxDateTime::Dec
, 1966, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 52, 5, 5, 361 },
517 { { 22, wxDateTime::Jul
, 1926, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 29, 4, 4, 203 },
518 { { 22, wxDateTime::Oct
, 2076, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 43, 4, 4, 296 },
519 { { 1, wxDateTime::Jul
, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 26, 1, 1, 182 },
520 { { 8, wxDateTime::Nov
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 46, 2, 2, 313 },
521 { { 21, wxDateTime::Mar
, 1920, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 12, 3, 4, 81 },
522 { { 7, wxDateTime::Jan
, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 2, 2, 7 },
523 { { 19, wxDateTime::Oct
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 42, 4, 4, 292 },
524 { { 13, wxDateTime::Aug
, 1955, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 32, 2, 2, 225 },
525 { { 18, wxDateTime::Jul
, 2087, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 29, 3, 3, 199 },
526 { { 2, wxDateTime::Sep
, 2028, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 35, 1, 1, 246 },
527 { { 28, wxDateTime::Jul
, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 30, 5, 4, 209 },
528 { { 15, wxDateTime::Jun
, 1901, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 24, 3, 3, 166 },
529 { { 10, wxDateTime::Oct
, 1939, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 41, 3, 2, 283 },
530 { { 3, wxDateTime::Dec
, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 48, 1, 1, 337 },
531 { { 23, wxDateTime::Feb
, 1940, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 8, 4, 4, 54 },
532 { { 2, wxDateTime::Jan
, 1987, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 1, 1, 2 },
533 { { 11, wxDateTime::Aug
, 2079, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 32, 2, 2, 223 },
534 { { 2, wxDateTime::Feb
, 2063, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 5, 1, 1, 33 },
535 { { 16, wxDateTime::Oct
, 1942, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 42, 3, 3, 289 },
536 { { 30, wxDateTime::Dec
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 5, 5, 364 },
537 { { 2, wxDateTime::Jan
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 1, 1, 2 },
538 { { 5, wxDateTime::Jan
, 2010, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 2, 2, 5 },
539 { { 3, wxDateTime::Jan
, 2011, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 2, 2, 3 },
540 { { 31, wxDateTime::Dec
, 2009, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 53, 5, 5, 365 },
541 { { 31, wxDateTime::Dec
, 2012, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 6, 6, 366 },
542 { { 29, wxDateTime::Dec
, 2013, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 52, 5, 5, 363 },
543 { { 30, wxDateTime::Dec
, 2013, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 6, 5, 364 },
544 { { 31, wxDateTime::Dec
, 2013, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 6, 5, 365 },
547 for ( size_t n
= 0; n
< WXSIZEOF(weekNumberTestDates
); n
++ )
549 const WeekNumberTestData
& wn
= weekNumberTestDates
[n
];
550 const Date
& d
= wn
.date
;
552 wxDateTime dt
= d
.DT();
554 wxDateTime::wxDateTime_t
555 week
= dt
.GetWeekOfYear(wxDateTime::Monday_First
),
556 wmon
= dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
557 wmon2
= dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
558 dnum
= dt
.GetDayOfYear();
560 WX_ASSERT_EQUAL_MESSAGE( ("day of year for %s", d
.Format()),
562 WX_ASSERT_EQUAL_MESSAGE( ("week of month (Monday) for %s", d
.Format()),
564 WX_ASSERT_EQUAL_MESSAGE( ("week of month (Sunday) for %s", d
.Format()),
566 WX_ASSERT_EQUAL_MESSAGE( ("week of year for %s", d
.Format()),
570 if ( week
== 1 && d
.month
!= wxDateTime::Jan
)
572 // this means we're in the first week of the next year
577 dt2
= wxDateTime::SetToWeekOfYear(year
, week
, dt
.GetWeekDay());
578 CPPUNIT_ASSERT_EQUAL( dt
, dt2
);
582 // test DST applicability
583 void DateTimeTestCase::TestTimeDST()
585 // taken from http://www.energy.ca.gov/daylightsaving.html
586 static const Date datesDST
[2][2009 - 1990 + 1] =
589 { 1, wxDateTime::Apr
, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
590 { 7, wxDateTime::Apr
, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
591 { 5, wxDateTime::Apr
, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
592 { 4, wxDateTime::Apr
, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
593 { 3, wxDateTime::Apr
, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
594 { 2, wxDateTime::Apr
, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
595 { 7, wxDateTime::Apr
, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
596 { 6, wxDateTime::Apr
, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
597 { 5, wxDateTime::Apr
, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
598 { 4, wxDateTime::Apr
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
599 { 2, wxDateTime::Apr
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
600 { 1, wxDateTime::Apr
, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
601 { 7, wxDateTime::Apr
, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
602 { 6, wxDateTime::Apr
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
603 { 4, wxDateTime::Apr
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
604 { 3, wxDateTime::Apr
, 2005, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
605 { 2, wxDateTime::Apr
, 2006, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
606 {11, wxDateTime::Mar
, 2007, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
607 { 9, wxDateTime::Mar
, 2008, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
608 { 8, wxDateTime::Mar
, 2009, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
611 { 28, wxDateTime::Oct
, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
612 { 27, wxDateTime::Oct
, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
613 { 25, wxDateTime::Oct
, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
614 { 31, wxDateTime::Oct
, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
615 { 30, wxDateTime::Oct
, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
616 { 29, wxDateTime::Oct
, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
617 { 27, wxDateTime::Oct
, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
618 { 26, wxDateTime::Oct
, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
619 { 25, wxDateTime::Oct
, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
620 { 31, wxDateTime::Oct
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
621 { 29, wxDateTime::Oct
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
622 { 28, wxDateTime::Oct
, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
623 { 27, wxDateTime::Oct
, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
624 { 26, wxDateTime::Oct
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
625 { 31, wxDateTime::Oct
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
626 { 30, wxDateTime::Oct
, 2005, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
627 { 29, wxDateTime::Oct
, 2006, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
628 { 4, wxDateTime::Nov
, 2007, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
629 { 2, wxDateTime::Nov
, 2008, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
630 { 1, wxDateTime::Nov
, 2009, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
635 for ( size_t n
= 0; n
< WXSIZEOF(datesDST
[0]); n
++ )
637 const int year
= 1990 + n
;
638 wxDateTime dtBegin
= wxDateTime::GetBeginDST(year
, wxDateTime::USA
),
639 dtEnd
= wxDateTime::GetEndDST(year
, wxDateTime::USA
);
641 const Date
& dBegin
= datesDST
[0][n
];
642 const Date
& dEnd
= datesDST
[1][n
];
644 CPPUNIT_ASSERT_EQUAL( dBegin
.DT().FormatDate(), dtBegin
.FormatDate() );
645 CPPUNIT_ASSERT_EQUAL( dEnd
.DT().FormatDate(), dtEnd
.FormatDate() );
649 // test wxDateTime -> text conversion
650 void DateTimeTestCase::TestTimeFormat()
652 // some information may be lost during conversion, so store what kind
653 // of info should we recover after a round trip
656 CompareNone
, // don't try comparing
657 CompareBoth
, // dates and times should be identical
658 CompareYear
, // don't compare centuries (fails for 2 digit years)
659 CompareDate
, // dates only
660 CompareTime
// time only
665 CompareKind compareKind
;
667 } formatTestFormats
[] =
669 { CompareYear
, "---> %c" }, // %c could use 2 digit years
670 { CompareDate
, "Date is %A, %d of %B, in year %Y" },
671 { CompareYear
, "Date is %x, time is %X" }, // %x could use 2 digits
672 { CompareTime
, "Time is %H:%M:%S or %I:%M:%S %p" },
673 { CompareNone
, "The day of year: %j, the week of year: %W" },
674 { CompareDate
, "ISO date without separators: %Y%m%d" },
675 { CompareBoth
, "RFC 2822 string: %Y-%m-%d %H:%M:%S.%l %z" },
679 const long timeZonesOffsets
[] =
681 wxDateTime::TimeZone(wxDateTime::Local
).GetOffset(),
683 // Fictitious TimeZone offsets to ensure time zone formating and
684 // interpretation works
689 static const Date formatTestDates
[] =
691 { 29, wxDateTime::May
, 1976, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
692 { 31, wxDateTime::Dec
, 1999, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
693 { 6, wxDateTime::Feb
, 1937, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
694 { 6, wxDateTime::Feb
, 1856, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
695 { 6, wxDateTime::Feb
, 1857, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
696 { 29, wxDateTime::May
, 2076, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
698 // FIXME: the test with 02:15:25 time doesn't pass because of DST
699 // computation problems, we get back 03:15:25
700 { 29, wxDateTime::Feb
, 2400, 04, 15, 25, 0.0, wxDateTime::Inv_WeekDay
},
702 // Need to add support for BCE dates.
703 { 01, wxDateTime::Jan
, -52, 03, 16, 47, 0.0, wxDateTime::Inv_WeekDay
},
707 for ( unsigned idxtz
= 0; idxtz
< WXSIZEOF(timeZonesOffsets
); ++idxtz
)
709 wxDateTime::TimeZone
tz(timeZonesOffsets
[idxtz
]);
710 const bool isLocalTz
= tz
.GetOffset() == -wxGetTimeZone();
712 for ( size_t d
= 0; d
< WXSIZEOF(formatTestDates
); d
++ )
714 wxDateTime dt
= formatTestDates
[d
].DT();
715 for ( unsigned n
= 0; n
< WXSIZEOF(formatTestFormats
); n
++ )
717 const char *fmt
= formatTestFormats
[n
].format
;
719 // skip the check with %p for those locales which have empty AM/PM strings:
720 // for those locales it's impossible to pass the test with %p...
722 wxDateTime::GetAmPmStrings(&am
, &pm
);
723 if (am
.empty() && pm
.empty() && wxStrstr(fmt
, "%p") != NULL
)
726 // what can we recover?
727 CompareKind kind
= formatTestFormats
[n
].compareKind
;
729 // When using a different time zone we must perform a time zone
730 // conversion below which doesn't always work correctly, check
731 // for the cases when it doesn't.
734 // DST computation doesn't work correctly for dates above
735 // 2038 currently on the systems with 32 bit time_t.
736 if ( dt
.GetYear() >= 2038 )
739 // We can't compare just dates nor just times when doing TZ
740 // conversion as both are affected by the DST: for the
741 // dates, the DST can switch midnight to 23:00 of the
742 // previous day while for the times DST can be different
743 // for the original date and today.
744 if ( kind
== CompareDate
|| kind
== CompareTime
)
748 // do convert date to string
749 wxString s
= dt
.Format(fmt
, tz
);
753 const char *result
= dt2
.ParseFormat(s
, fmt
);
756 // conversion failed - should it have?
758 ("Test #%u failed: failed to parse \"%s\"", n
, s
),
762 else // conversion succeeded
764 // currently ParseFormat() doesn't support "%Z" and so is
765 // incapable of parsing time zone part used at the end of date
766 // representations in many (but not "C") locales, compensate
767 // for it ourselves by simply consuming and ignoring it
768 while ( *result
&& (*result
>= 'A' && *result
<= 'Z') )
772 ("Test #%u failed: \"%s\" was left unparsed in \"%s\"",
777 // Without "%z" we can't recover the time zone used in the
778 // call to Format() so we need to call MakeFromTimezone()
780 if ( !strstr(fmt
, "%z") && !isLocalTz
)
781 dt2
.MakeFromTimezone(tz
);
786 if ( dt2
.GetCentury() != dt
.GetCentury() )
788 CPPUNIT_ASSERT_EQUAL(dt
.GetYear() % 100,
789 dt2
.GetYear() % 100);
791 dt2
.SetYear(dt
.GetYear());
793 // fall through and compare everything
796 CPPUNIT_ASSERT_EQUAL( dt
, dt2
);
800 CPPUNIT_ASSERT( dt
.IsSameDate(dt2
) );
804 CPPUNIT_ASSERT( dt
.IsSameTime(dt2
) );
808 wxFAIL_MSG( wxT("unexpected") );
819 // special case which was known to fail
820 CPPUNIT_ASSERT( dt
.ParseFormat("02/06/1856", "%x") );
821 CPPUNIT_ASSERT_EQUAL( 1856, dt
.GetYear() );
824 // also test %l separately
825 CPPUNIT_ASSERT( dt
.ParseFormat("12:23:45.678", "%H:%M:%S.%l") );
826 CPPUNIT_ASSERT_EQUAL( 678, dt
.GetMillisecond() );
828 // test special case of %l matching 0 milliseconds
829 CPPUNIT_ASSERT( dt
.ParseFormat("12:23:45.000", "%H:%M:%S.%l") );
830 CPPUNIT_ASSERT_EQUAL( 0, dt
.GetMillisecond() );
832 // test partially specified dates too
833 wxDateTime
dtDef(26, wxDateTime::Sep
, 2008);
834 CPPUNIT_ASSERT( dt
.ParseFormat("17", "%d") );
835 CPPUNIT_ASSERT_EQUAL( 17, dt
.GetDay() );
837 // test some degenerate cases
838 CPPUNIT_ASSERT( !dt
.ParseFormat("", "%z") );
839 CPPUNIT_ASSERT( !dt
.ParseFormat("", "%%") );
841 // test compilation of some calls which should compile (and not result in
842 // ambiguity because of char*<->wxCStrData<->wxString conversions)
844 CPPUNIT_ASSERT( !dt
.ParseFormat("foo") );
845 CPPUNIT_ASSERT( !dt
.ParseFormat(wxT("foo")) );
846 CPPUNIT_ASSERT( !dt
.ParseFormat(s
) );
847 dt
.ParseFormat(s
.c_str()); // Simply test compilation of this one.
849 CPPUNIT_ASSERT( !dt
.ParseFormat("foo", "%c") );
850 CPPUNIT_ASSERT( !dt
.ParseFormat(wxT("foo"), "%c") );
851 CPPUNIT_ASSERT( !dt
.ParseFormat(s
, "%c") );
852 dt
.ParseFormat(s
.c_str(), "%c");
854 CPPUNIT_ASSERT( !dt
.ParseFormat("foo", wxT("%c")) );
855 CPPUNIT_ASSERT( !dt
.ParseFormat(wxT("foo"), wxT("%c")) );
856 CPPUNIT_ASSERT( !dt
.ParseFormat(s
, "%c") );
857 dt
.ParseFormat(s
.c_str(), wxT("%c"));
860 CPPUNIT_ASSERT( !dt
.ParseFormat("foo", spec
) );
861 CPPUNIT_ASSERT( !dt
.ParseFormat(wxT("foo"), spec
) );
862 CPPUNIT_ASSERT( !dt
.ParseFormat(s
, spec
) );
863 dt
.ParseFormat(s
.c_str(), spec
);
866 void DateTimeTestCase::TestTimeSpanFormat()
868 static const struct TimeSpanFormatTestData
870 long h
, min
, sec
, msec
;
875 { 12, 34, 56, 789, "%H:%M:%S.%l", "12:34:56.789" },
876 { 1, 2, 3, 0, "%H:%M:%S", "01:02:03" },
877 { 1, 2, 3, 0, "%S", "3723" },
878 { -1, -2, -3, 0, "%S", "-3723" },
879 { -1, -2, -3, 0, "%H:%M:%S", "-01:02:03" },
880 { 26, 0, 0, 0, "%H", "26" },
881 { 26, 0, 0, 0, "%D, %H", "1, 02" },
882 { -26, 0, 0, 0, "%H", "-26" },
883 { -26, 0, 0, 0, "%D, %H", "-1, 02" },
884 { 219, 0, 0, 0, "%H", "219" },
885 { 219, 0, 0, 0, "%D, %H", "9, 03" },
886 { 219, 0, 0, 0, "%E, %D, %H", "1, 2, 03" },
887 { 0, -1, 0, 0, "%H:%M:%S", "-00:01:00" },
888 { 0, 0, -1, 0, "%H:%M:%S", "-00:00:01" },
891 for ( size_t n
= 0; n
< WXSIZEOF(testSpans
); n
++ )
893 const TimeSpanFormatTestData
& td
= testSpans
[n
];
894 wxTimeSpan
ts(td
.h
, td
.min
, td
.sec
, td
.msec
);
895 CPPUNIT_ASSERT_EQUAL( td
.result
, ts
.Format(td
.fmt
) );
899 void DateTimeTestCase::TestTimeTicks()
901 static const wxDateTime::TimeZone
TZ_LOCAL(wxDateTime::Local
);
902 static const wxDateTime::TimeZone
TZ_TEST(wxDateTime::NZST
);
904 // this offset is needed to make the test work in any time zone when we
905 // only have expected test results in UTC in testDates
906 static const long tzOffset
= TZ_LOCAL
.GetOffset() - TZ_TEST
.GetOffset();
908 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
910 const Date
& d
= testDates
[n
];
911 if ( d
.gmticks
== -1 )
914 wxDateTime dt
= d
.DT().MakeTimezone(TZ_TEST
, true /* no DST */);
916 // GetValue() returns internal UTC-based representation, we need to
917 // convert it to local TZ before comparing
918 time_t ticks
= (dt
.GetValue() / 1000).ToLong() + TZ_LOCAL
.GetOffset();
921 CPPUNIT_ASSERT_EQUAL( d
.gmticks
, ticks
+ tzOffset
);
923 dt
= d
.DT().FromTimezone(wxDateTime::UTC
);
924 ticks
= (dt
.GetValue() / 1000).ToLong();
925 CPPUNIT_ASSERT_EQUAL( d
.gmticks
, ticks
);
929 // test parsing dates in RFC822 format
930 void DateTimeTestCase::TestParceRFC822()
932 static const struct ParseTestData
935 Date date
; // NB: this should be in UTC
940 "Sat, 18 Dec 1999 00:46:40 +0100",
941 { 17, wxDateTime::Dec
, 1999, 23, 46, 40 },
945 "Wed, 1 Dec 1999 05:17:20 +0300",
946 { 1, wxDateTime::Dec
, 1999, 2, 17, 20 },
950 "Sun, 28 Aug 2005 03:31:30 +0200",
951 { 28, wxDateTime::Aug
, 2005, 1, 31, 30 },
956 "Sat, 18 Dec 1999 10:48:30 -0500",
957 { 18, wxDateTime::Dec
, 1999, 15, 48, 30 },
961 // seconds are optional according to the RFC
963 "Sun, 01 Jun 2008 16:30 +0200",
964 { 1, wxDateTime::Jun
, 2008, 14, 30, 00 },
968 // try some bogus ones too
970 "Sun, 01 Jun 2008 16:30: +0200",
976 for ( unsigned n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
978 const char * const datestr
= parseTestDates
[n
].rfc822
;
981 if ( dt
.ParseRfc822Date(datestr
) )
984 ("Erroneously parsed \"%s\"", datestr
),
985 parseTestDates
[n
].good
988 wxDateTime dtReal
= parseTestDates
[n
].date
.DT().FromUTC();
989 CPPUNIT_ASSERT_EQUAL( dtReal
, dt
);
991 else // failed to parse
994 ("Failed to parse \"%s\"", datestr
),
995 !parseTestDates
[n
].good
1001 // test parsing dates in free format
1002 void DateTimeTestCase::TestDateParse()
1004 static const struct ParseTestData
1007 Date date
; // NB: this should be in UTC
1009 } parseTestDates
[] =
1011 { "21 Mar 2006", { 21, wxDateTime::Mar
, 2006 }, true },
1012 { "29 Feb 1976", { 29, wxDateTime::Feb
, 1976 }, true },
1013 { "Feb 29 1976", { 29, wxDateTime::Feb
, 1976 }, true },
1014 { "31/03/06", { 31, wxDateTime::Mar
, 6 }, true },
1015 { "31/03/2006", { 31, wxDateTime::Mar
, 2006 }, true },
1017 // some invalid ones too
1026 CPPUNIT_ASSERT( dt
.ParseDate(wxT("today")) );
1027 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), dt
);
1029 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
1031 const wxString datestr
= TranslateDate(parseTestDates
[n
].str
);
1033 const char * const end
= dt
.ParseDate(datestr
);
1037 ("Erroneously parsed \"%s\"", datestr
),
1038 parseTestDates
[n
].good
1041 CPPUNIT_ASSERT_EQUAL( parseTestDates
[n
].date
.DT(), dt
);
1043 else // failed to parse
1046 ("Failed to parse \"%s\"", datestr
),
1047 !parseTestDates
[n
].good
1052 // Check that incomplete parse works correctly.
1053 const char* p
= dt
.ParseFormat("2012-03-23 12:34:56", "%Y-%m-%d");
1054 CPPUNIT_ASSERT_EQUAL( " 12:34:56", wxString(p
) );
1057 void DateTimeTestCase::TestDateParseISO()
1062 Date date
; // NB: this should be in UTC
1064 } parseTestDates
[] =
1066 { "2006-03-21", { 21, wxDateTime::Mar
, 2006 }, true },
1067 { "1976-02-29", { 29, wxDateTime::Feb
, 1976 }, true },
1068 { "0006-03-31", { 31, wxDateTime::Mar
, 6 }, true },
1070 // some invalid ones too
1080 wxDateTime::wxDateTime_t hour
, min
, sec
;
1082 } parseTestTimes
[] =
1084 { "13:42:17", 13, 42, 17, true },
1085 { "02:17:01", 2, 17, 1, true },
1087 // some invalid ones too
1094 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
1097 if ( dt
.ParseISODate(parseTestDates
[n
].str
) )
1099 CPPUNIT_ASSERT( parseTestDates
[n
].good
);
1101 CPPUNIT_ASSERT_EQUAL( parseTestDates
[n
].date
.DT(), dt
);
1103 for ( size_t m
= 0; m
< WXSIZEOF(parseTestTimes
); m
++ )
1105 wxString dtCombined
;
1106 dtCombined
<< parseTestDates
[n
].str
1108 << parseTestTimes
[m
].str
;
1110 if ( dt
.ParseISOCombined(dtCombined
) )
1112 CPPUNIT_ASSERT( parseTestTimes
[m
].good
);
1114 CPPUNIT_ASSERT_EQUAL( parseTestTimes
[m
].hour
, dt
.GetHour()) ;
1115 CPPUNIT_ASSERT_EQUAL( parseTestTimes
[m
].min
, dt
.GetMinute()) ;
1116 CPPUNIT_ASSERT_EQUAL( parseTestTimes
[m
].sec
, dt
.GetSecond()) ;
1118 else // failed to parse combined date/time
1120 CPPUNIT_ASSERT( !parseTestTimes
[m
].good
);
1124 else // failed to parse
1126 CPPUNIT_ASSERT( !parseTestDates
[n
].good
);
1131 void DateTimeTestCase::TestDateTimeParse()
1133 static const struct ParseTestData
1136 Date date
; // NB: this should be in UTC
1138 } parseTestDates
[] =
1141 "Thu 22 Nov 2007 07:40:00 PM",
1142 { 22, wxDateTime::Nov
, 2007, 19, 40, 0 },
1148 { 4, wxDateTime::Jan
, 2010, 14, 30, 0 },
1154 { 1, wxDateTime::Jan
, 9999, 0, 0, 0},
1159 "2012-01-01 10:12:05 +0100",
1160 { 1, wxDateTime::Jan
, 2012, 10, 12, 5, -1 },
1161 false // ParseDateTime does know yet +0100
1165 // the test strings here use "PM" which is not available in all locales so
1166 // we need to use "C" locale for them
1170 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
1172 const wxString datestr
= TranslateDate(parseTestDates
[n
].str
);
1174 const char * const end
= dt
.ParseDateTime(datestr
);
1178 ("Erroneously parsed \"%s\"", datestr
),
1179 parseTestDates
[n
].good
1182 CPPUNIT_ASSERT_EQUAL( parseTestDates
[n
].date
.DT(), dt
);
1184 else // failed to parse
1187 ("Failed to parse \"%s\"", datestr
),
1188 !parseTestDates
[n
].good
1191 CPPUNIT_ASSERT( !parseTestDates
[n
].good
);
1196 void DateTimeTestCase::TestTimeArithmetics()
1198 static const wxDateSpan testArithmData
[] =
1202 wxDateSpan::Month(),
1206 // the test will *not* work with arbitrary date!
1207 wxDateTime
dt(2, wxDateTime::Dec
, 1999),
1211 for ( size_t n
= 0; n
< WXSIZEOF(testArithmData
); n
++ )
1213 const wxDateSpan
& span
= testArithmData
[n
];
1217 CPPUNIT_ASSERT_EQUAL( dt
, dt1
- span
);
1218 CPPUNIT_ASSERT_EQUAL( dt
, dt2
+ span
);
1219 CPPUNIT_ASSERT_EQUAL( dt1
, dt2
+ 2*span
);
1220 CPPUNIT_ASSERT_EQUAL( span
, dt1
.DiffAsDateSpan(dt
) );
1223 // More date span arithmetics tests
1224 wxDateTime
dtd1(5, wxDateTime::Jun
, 1998);
1225 wxDateTime
dtd2(6, wxDateTime::Aug
, 1999);
1227 // All parts in dtd2 is after dtd1
1228 CPPUNIT_ASSERT_EQUAL( wxDateSpan(1, 2, 0, 1), dtd2
.DiffAsDateSpan(dtd1
) );
1230 // Year and month after, day earlier, so no full month
1231 // Jul has 31 days, so it's 31 - 5 + 4 = 30, or 4w 2d
1232 dtd2
.Set(4, wxDateTime::Aug
, 1999);
1233 CPPUNIT_ASSERT_EQUAL( wxDateSpan(1, 1, 4, 2), dtd2
.DiffAsDateSpan(dtd1
) );
1235 // Year and day after, month earlier, so no full year, but same day diff as
1237 dtd2
.Set(6, wxDateTime::May
, 1999);
1238 CPPUNIT_ASSERT_EQUAL( wxDateSpan(0, 11, 0, 1), dtd2
.DiffAsDateSpan(dtd1
) );
1240 // Year after, month and day earlier, so no full month and no full year
1241 // April has 30 days, so it's 30 - 5 + 4 = 29, or 4w 1d
1242 dtd2
.Set(4, wxDateTime::May
, 1999);
1243 CPPUNIT_ASSERT_EQUAL( wxDateSpan(0, 10, 4, 1), dtd2
.DiffAsDateSpan(dtd1
) );
1245 // And a reverse. Now we should use days in Jun (again 30 => 4w 1d)
1246 CPPUNIT_ASSERT_EQUAL( wxDateSpan(0, -10, -4, -1), dtd1
.DiffAsDateSpan(dtd2
) );
1249 void DateTimeTestCase::TestDSTBug()
1251 /////////////////////////
1253 wxDateTime dt
= wxDateTime::GetEndDST(2004, wxDateTime::France
);
1254 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
1255 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
1256 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
1257 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
1258 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
1259 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
1260 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
1262 /////////////////////////
1265 CPPUNIT_ASSERT_EQUAL(5, (int)dt
.GetHour());
1267 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
1268 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
1269 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
1270 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetHour());
1271 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
1272 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
1273 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
1276 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
1278 /////////////////////////
1280 #ifdef CHANGE_SYSTEM_DATE
1282 DateChanger
change(2004, 10, 31, 5, 0, 0);
1283 dt
= wxDateTime::Today();
1286 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
1287 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
1288 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
1289 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetHour());
1290 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
1291 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
1292 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
1294 /////////////////////////
1295 // Test Set(hour, minute, second, milli)
1298 DateChanger
change(2004, 10, 31, 5, 0, 0);
1299 dt
.Set(1, 30, 0, 0);
1300 dt2
.Set(5, 30, 0, 0);
1303 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
1304 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
1305 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
1306 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
1307 CPPUNIT_ASSERT_EQUAL(30, (int)dt
.GetMinute());
1308 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
1309 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
1311 CPPUNIT_ASSERT_EQUAL(31, (int)dt2
.GetDay());
1312 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt2
.GetMonth());
1313 CPPUNIT_ASSERT_EQUAL(2004, (int)dt2
.GetYear());
1314 CPPUNIT_ASSERT_EQUAL(5, (int)dt2
.GetHour());
1315 CPPUNIT_ASSERT_EQUAL(30, (int)dt2
.GetMinute());
1316 CPPUNIT_ASSERT_EQUAL(0, (int)dt2
.GetSecond());
1317 CPPUNIT_ASSERT_EQUAL(0, (int)dt2
.GetMillisecond());
1318 #endif // CHANGE_SYSTEM_DATE
1321 void DateTimeTestCase::TestDateOnly()
1323 wxDateTime
dt(19, wxDateTime::Jan
, 2007, 15, 01, 00);
1325 static const wxDateTime::wxDateTime_t DATE_ZERO
= 0;
1326 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetHour() );
1327 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetMinute() );
1328 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetSecond() );
1329 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetMillisecond() );
1332 CPPUNIT_ASSERT_EQUAL( wxDateTime(19, wxDateTime::Jan
, 2007), dt
);
1334 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), wxDateTime::Now().GetDateOnly() );
1337 #endif // wxUSE_DATETIME