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)
6 // Copyright: (c) 2004 Vadim Zeitlin <vadim@wxwindows.org>
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
22 #include "wx/time.h" // wxGetTimeZone()
25 #include "wx/wxcrt.h" // for wxStrstr()
29 // to test Today() meaningfully we must be able to change the system date which
30 // is not usually the case, but if we're under Win32 we can try it -- define
31 // the macro below to do it
32 //#define CHANGE_SYSTEM_DATE
35 #undef CHANGE_SYSTEM_DATE
38 #ifdef CHANGE_SYSTEM_DATE
43 DateChanger(int year
, int month
, int day
, int hour
, int min
, int sec
)
54 ::GetSystemTime(&m_savedTime
);
55 ::GetTimeZoneInformation(&m_tzi
);
57 m_changed
= ::SetSystemTime(&st
) != 0;
64 ::SetSystemTime(&m_savedTime
);
65 ::SetTimeZoneInformation(&m_tzi
);
70 SYSTEMTIME m_savedTime
;
71 TIME_ZONE_INFORMATION m_tzi
;
75 #endif // CHANGE_SYSTEM_DATE
77 // helper function translating week day/month names from English to the current
79 static wxString
TranslateDate(const wxString
& str
)
81 // small optimization: if there are no alphabetic characters in the string,
82 // there is nothing to translate
83 wxString::const_iterator i
, end
= str
.end();
84 for ( i
= str
.begin(); i
!= end
; ++i
)
95 for ( wxDateTime::WeekDay wd
= wxDateTime::Sun
;
96 wd
< wxDateTime::Inv_WeekDay
;
101 wxDateTime::GetEnglishWeekDayName(wd
, wxDateTime::Name_Abbr
),
102 wxDateTime::GetWeekDayName(wd
, wxDateTime::Name_Abbr
)
106 for ( wxDateTime::Month mon
= wxDateTime::Jan
;
107 mon
< wxDateTime::Inv_Month
;
112 wxDateTime::GetEnglishMonthName(mon
, wxDateTime::Name_Abbr
),
113 wxDateTime::GetMonthName(mon
, wxDateTime::Name_Abbr
)
120 // ----------------------------------------------------------------------------
121 // broken down date representation used for testing
122 // ----------------------------------------------------------------------------
126 wxDateTime::wxDateTime_t day
;
127 wxDateTime::Month month
;
129 wxDateTime::wxDateTime_t hour
, min
, sec
;
131 wxDateTime::WeekDay wday
;
134 void Init(const wxDateTime::Tm
& tm
)
146 wxDateTime
DT() const
147 { return wxDateTime(day
, month
, year
, hour
, min
, sec
); }
149 bool SameDay(const wxDateTime::Tm
& tm
) const
151 return day
== tm
.mday
&& month
== tm
.mon
&& year
== tm
.year
;
154 wxString
Format() const
157 s
.Printf(wxT("%02d:%02d:%02d %10s %02d, %4d%s"),
159 wxDateTime::GetMonthName(month
).c_str(),
161 abs(wxDateTime::ConvertYearToBC(year
)),
162 year
> 0 ? wxT("AD") : wxT("BC"));
166 wxString
FormatDate() const
169 s
.Printf(wxT("%02d-%s-%4d%s"),
171 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
172 abs(wxDateTime::ConvertYearToBC(year
)),
173 year
> 0 ? wxT("AD") : wxT("BC"));
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
182 static const Date testDates
[] =
184 { 1, wxDateTime::Jan
, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu
, 0 },
185 { 7, wxDateTime::Feb
, 2036, 00, 00, 00, 2464730.5, wxDateTime::Thu
, -1 },
186 { 8, wxDateTime::Feb
, 2036, 00, 00, 00, 2464731.5, wxDateTime::Fri
, -1 },
187 { 1, wxDateTime::Jan
, 2037, 00, 00, 00, 2465059.5, wxDateTime::Thu
, -1 },
188 { 1, wxDateTime::Jan
, 2038, 00, 00, 00, 2465424.5, wxDateTime::Fri
, -1 },
189 { 21, wxDateTime::Jan
, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon
, -1 },
190 { 29, wxDateTime::May
, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat
, 202219200 },
191 { 29, wxDateTime::Feb
, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun
, 194400000 },
192 { 1, wxDateTime::Jan
, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon
, -1 },
193 { 1, wxDateTime::Jan
, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon
, -1 },
194 { 15, wxDateTime::Oct
, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri
, -1 },
195 { 4, wxDateTime::Oct
, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon
, -1 },
196 { 1, wxDateTime::Mar
, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu
, -1 },
197 { 1, wxDateTime::Jan
, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon
, -1 },
198 { 31, wxDateTime::Dec
, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun
, -1 },
199 { 1, wxDateTime::Jan
, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat
, -1 },
200 { 12, wxDateTime::Aug
, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri
, -1 },
201 { 12, wxDateTime::Aug
, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat
, -1 },
202 { 24, wxDateTime::Nov
, -4713, 00, 00, 00, -0.5, wxDateTime::Mon
, -1 },
206 // ----------------------------------------------------------------------------
208 // ----------------------------------------------------------------------------
210 class DateTimeTestCase
: public CppUnit::TestCase
213 DateTimeTestCase() { }
216 CPPUNIT_TEST_SUITE( DateTimeTestCase
);
217 CPPUNIT_TEST( TestLeapYears
);
218 CPPUNIT_TEST( TestTimeSet
);
219 CPPUNIT_TEST( TestTimeJDN
);
220 CPPUNIT_TEST( TestTimeWNumber
);
221 CPPUNIT_TEST( TestTimeWDays
);
222 CPPUNIT_TEST( TestTimeDST
);
223 CPPUNIT_TEST( TestTimeFormat
);
224 CPPUNIT_TEST( TestTimeParse
);
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 TestTimeParse();
244 void TestTimeSpanFormat();
245 void TestTimeTicks();
246 void TestParceRFC822();
247 void TestDateParse();
248 void TestDateParseISO();
249 void TestDateTimeParse();
250 void TestTimeArithmetics();
254 DECLARE_NO_COPY_CLASS(DateTimeTestCase
)
257 // register in the unnamed registry so that these tests are run by default
258 CPPUNIT_TEST_SUITE_REGISTRATION( DateTimeTestCase
);
260 // also include in its own registry so that these tests can be run alone
261 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DateTimeTestCase
, "DateTimeTestCase" );
263 // ============================================================================
265 // ============================================================================
267 // test leap years detection
268 void DateTimeTestCase::TestLeapYears()
270 static const struct LeapYearTestData
286 for ( size_t n
= 0; n
< WXSIZEOF(years
); n
++ )
288 const LeapYearTestData
& y
= years
[n
];
290 CPPUNIT_ASSERT_EQUAL( y
.isLeap
, wxDateTime::IsLeapYear(y
.year
) );
294 // test constructing wxDateTime objects
295 void DateTimeTestCase::TestTimeSet()
297 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
299 const Date
& d1
= testDates
[n
];
300 wxDateTime dt
= d1
.DT();
305 wxString s1
= d1
.Format(),
308 CPPUNIT_ASSERT_EQUAL( s1
, s2
);
312 // test conversions to JDN &c
313 void DateTimeTestCase::TestTimeJDN()
315 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
317 const Date
& d
= testDates
[n
];
318 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
320 // JDNs must be computed for UTC times
321 double jdn
= dt
.FromUTC().GetJulianDayNumber();
323 CPPUNIT_ASSERT_EQUAL( d
.jdn
, jdn
);
326 CPPUNIT_ASSERT_EQUAL( jdn
, dt
.GetJulianDayNumber() );
330 // test week days computation
331 void DateTimeTestCase::TestTimeWDays()
335 for ( n
= 0; n
< WXSIZEOF(testDates
); n
++ )
337 const Date
& d
= testDates
[n
];
338 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
340 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
341 CPPUNIT_ASSERT_EQUAL( d
.wday
, wday
);
344 // test SetToWeekDay()
345 struct WeekDateTestData
347 Date date
; // the real date (precomputed)
348 int nWeek
; // its week index in the month
349 wxDateTime::WeekDay wday
; // the weekday
350 wxDateTime::Month month
; // the month
351 int year
; // and the year
353 wxString
Format() const
356 switch ( nWeek
< -1 ? -nWeek
: nWeek
)
358 case 1: which
= wxT("first"); break;
359 case 2: which
= wxT("second"); break;
360 case 3: which
= wxT("third"); break;
361 case 4: which
= wxT("fourth"); break;
362 case 5: which
= wxT("fifth"); break;
364 case -1: which
= wxT("last"); break;
369 which
+= wxT(" from end");
372 s
.Printf(wxT("The %s %s of %s in %d"),
374 wxDateTime::GetWeekDayName(wday
).c_str(),
375 wxDateTime::GetMonthName(month
).c_str(),
382 // the array data was generated by the following python program
384 from DateTime import *
385 from whrandom import *
388 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
389 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
391 week = DateTimeDelta(7)
394 year = randint(1900, 2100)
395 month = randint(1, 12)
397 dt = DateTime(year, month, day)
398 wday = dt.day_of_week
400 countFromEnd = choice([-1, 1])
403 while dt.month is month:
404 dt = dt - countFromEnd * week
405 weekNum = weekNum + countFromEnd
407 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
409 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
410 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
413 static const WeekDateTestData weekDatesTestData
[] =
415 { { 20, wxDateTime::Mar
, 2045, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 3, wxDateTime::Mon
, wxDateTime::Mar
, 2045 },
416 { { 5, wxDateTime::Jun
, 1985, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -4, wxDateTime::Wed
, wxDateTime::Jun
, 1985 },
417 { { 12, wxDateTime::Nov
, 1961, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -3, wxDateTime::Sun
, wxDateTime::Nov
, 1961 },
418 { { 27, wxDateTime::Feb
, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -1, wxDateTime::Fri
, wxDateTime::Feb
, 2093 },
419 { { 4, wxDateTime::Jul
, 2070, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -4, wxDateTime::Fri
, wxDateTime::Jul
, 2070 },
420 { { 2, wxDateTime::Apr
, 1906, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -5, wxDateTime::Mon
, wxDateTime::Apr
, 1906 },
421 { { 19, wxDateTime::Jul
, 2023, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -2, wxDateTime::Wed
, wxDateTime::Jul
, 2023 },
422 { { 5, wxDateTime::May
, 1958, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -4, wxDateTime::Mon
, wxDateTime::May
, 1958 },
423 { { 11, wxDateTime::Aug
, 1900, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 2, wxDateTime::Sat
, wxDateTime::Aug
, 1900 },
424 { { 14, wxDateTime::Feb
, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 2, wxDateTime::Wed
, wxDateTime::Feb
, 1945 },
425 { { 25, wxDateTime::Jul
, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -1, wxDateTime::Tue
, wxDateTime::Jul
, 1967 },
426 { { 9, wxDateTime::May
, 1916, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -4, wxDateTime::Tue
, wxDateTime::May
, 1916 },
427 { { 20, wxDateTime::Jun
, 1927, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 3, wxDateTime::Mon
, wxDateTime::Jun
, 1927 },
428 { { 2, wxDateTime::Aug
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, wxDateTime::Wed
, wxDateTime::Aug
, 2000 },
429 { { 20, wxDateTime::Apr
, 2044, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 3, wxDateTime::Wed
, wxDateTime::Apr
, 2044 },
430 { { 20, wxDateTime::Feb
, 1932, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -2, wxDateTime::Sat
, wxDateTime::Feb
, 1932 },
431 { { 25, wxDateTime::Jul
, 2069, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 4, wxDateTime::Thu
, wxDateTime::Jul
, 2069 },
432 { { 3, wxDateTime::Apr
, 1925, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, wxDateTime::Fri
, wxDateTime::Apr
, 1925 },
433 { { 21, wxDateTime::Mar
, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 3, wxDateTime::Sat
, wxDateTime::Mar
, 2093 },
434 { { 3, wxDateTime::Dec
, 2074, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, -5, wxDateTime::Mon
, wxDateTime::Dec
, 2074 }
438 for ( n
= 0; n
< WXSIZEOF(weekDatesTestData
); n
++ )
440 const WeekDateTestData
& wd
= weekDatesTestData
[n
];
442 dt
.SetToWeekDay(wd
.wday
, wd
.nWeek
, wd
.month
, wd
.year
);
444 const Date
& d
= wd
.date
;
445 CPPUNIT_ASSERT( d
.SameDay(dt
.GetTm()) );
449 // test the computation of (ISO) week numbers
450 void DateTimeTestCase::TestTimeWNumber()
452 struct WeekNumberTestData
454 Date date
; // the date
455 wxDateTime::wxDateTime_t week
; // the week number in the year
456 wxDateTime::wxDateTime_t wmon
; // the week number in the month
457 wxDateTime::wxDateTime_t wmon2
; // same but week starts with Sun
458 wxDateTime::wxDateTime_t dnum
; // day number in the year
461 // data generated with the following python script:
463 from DateTime import *
464 from whrandom import *
467 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
468 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
470 def GetMonthWeek(dt):
471 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
473 weekNumMonth = weekNumMonth + 53
476 def GetLastSundayBefore(dt):
477 if dt.iso_week[2] == 7:
480 return dt - DateTimeDelta(dt.iso_week[2])
483 year = randint(1900, 2100)
484 month = randint(1, 12)
486 dt = DateTime(year, month, day)
487 dayNum = dt.day_of_year
488 weekNum = dt.iso_week[1]
489 weekNumMonth = GetMonthWeek(dt)
492 dtSunday = GetLastSundayBefore(dt)
494 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
495 weekNumMonth2 = weekNumMonth2 + 1
496 dtSunday = dtSunday - DateTimeDelta(7)
498 data = { 'day': rjust(`day`, 2), \
499 'month': monthNames[month - 1], \
501 'weekNum': rjust(`weekNum`, 2), \
502 'weekNumMonth': weekNumMonth, \
503 'weekNumMonth2': weekNumMonth2, \
504 'dayNum': rjust(`dayNum`, 3) }
506 print " { { %(day)s, "\
507 "wxDateTime::%(month)s, "\
510 "%(weekNumMonth)s, "\
511 "%(weekNumMonth2)s, "\
512 "%(dayNum)s }," % data
515 static const WeekNumberTestData weekNumberTestDates
[] =
517 { { 27, wxDateTime::Dec
, 1966, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 52, 5, 5, 361 },
518 { { 22, wxDateTime::Jul
, 1926, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 29, 4, 4, 203 },
519 { { 22, wxDateTime::Oct
, 2076, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 43, 4, 4, 296 },
520 { { 1, wxDateTime::Jul
, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 26, 1, 1, 182 },
521 { { 8, wxDateTime::Nov
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 46, 2, 2, 313 },
522 { { 21, wxDateTime::Mar
, 1920, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 12, 3, 4, 81 },
523 { { 7, wxDateTime::Jan
, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 2, 2, 7 },
524 { { 19, wxDateTime::Oct
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 42, 4, 4, 292 },
525 { { 13, wxDateTime::Aug
, 1955, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 32, 2, 2, 225 },
526 { { 18, wxDateTime::Jul
, 2087, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 29, 3, 3, 199 },
527 { { 2, wxDateTime::Sep
, 2028, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 35, 1, 1, 246 },
528 { { 28, wxDateTime::Jul
, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 30, 5, 4, 209 },
529 { { 15, wxDateTime::Jun
, 1901, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 24, 3, 3, 166 },
530 { { 10, wxDateTime::Oct
, 1939, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 41, 3, 2, 283 },
531 { { 3, wxDateTime::Dec
, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 48, 1, 1, 337 },
532 { { 23, wxDateTime::Feb
, 1940, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 8, 4, 4, 54 },
533 { { 2, wxDateTime::Jan
, 1987, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 1, 1, 2 },
534 { { 11, wxDateTime::Aug
, 2079, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 32, 2, 2, 223 },
535 { { 2, wxDateTime::Feb
, 2063, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 5, 1, 1, 33 },
536 { { 16, wxDateTime::Oct
, 1942, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 42, 3, 3, 289 },
537 { { 30, wxDateTime::Dec
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 5, 5, 364 },
538 { { 2, wxDateTime::Jan
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 1, 1, 2 },
539 { { 5, wxDateTime::Jan
, 2010, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 2, 2, 5 },
540 { { 3, wxDateTime::Jan
, 2011, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 2, 2, 3 },
541 { { 31, wxDateTime::Dec
, 2009, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 53, 5, 5, 365 },
542 { { 31, wxDateTime::Dec
, 2012, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 6, 6, 366 },
543 { { 29, wxDateTime::Dec
, 2013, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 52, 5, 5, 363 },
544 { { 30, wxDateTime::Dec
, 2013, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 6, 5, 364 },
545 { { 31, wxDateTime::Dec
, 2013, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 }, 1, 6, 5, 365 },
548 for ( size_t n
= 0; n
< WXSIZEOF(weekNumberTestDates
); n
++ )
550 const WeekNumberTestData
& wn
= weekNumberTestDates
[n
];
551 const Date
& d
= wn
.date
;
553 wxDateTime dt
= d
.DT();
555 wxDateTime::wxDateTime_t
556 week
= dt
.GetWeekOfYear(wxDateTime::Monday_First
),
557 wmon
= dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
558 wmon2
= dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
559 dnum
= dt
.GetDayOfYear();
561 WX_ASSERT_EQUAL_MESSAGE( ("day of year for %s", d
.Format()),
563 WX_ASSERT_EQUAL_MESSAGE( ("week of month (Monday) for %s", d
.Format()),
565 WX_ASSERT_EQUAL_MESSAGE( ("week of month (Sunday) for %s", d
.Format()),
567 WX_ASSERT_EQUAL_MESSAGE( ("week of year for %s", d
.Format()),
571 if ( week
== 1 && d
.month
!= wxDateTime::Jan
)
573 // this means we're in the first week of the next year
578 dt2
= wxDateTime::SetToWeekOfYear(year
, week
, dt
.GetWeekDay());
579 CPPUNIT_ASSERT_EQUAL( dt
, dt2
);
583 // test DST applicability
584 void DateTimeTestCase::TestTimeDST()
586 // taken from http://www.energy.ca.gov/daylightsaving.html
587 static const Date datesDST
[2][2009 - 1990 + 1] =
590 { 1, wxDateTime::Apr
, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
591 { 7, wxDateTime::Apr
, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
592 { 5, wxDateTime::Apr
, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
593 { 4, wxDateTime::Apr
, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
594 { 3, wxDateTime::Apr
, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
595 { 2, wxDateTime::Apr
, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
596 { 7, wxDateTime::Apr
, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
597 { 6, wxDateTime::Apr
, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
598 { 5, wxDateTime::Apr
, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
599 { 4, wxDateTime::Apr
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
600 { 2, wxDateTime::Apr
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
601 { 1, wxDateTime::Apr
, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
602 { 7, wxDateTime::Apr
, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
603 { 6, wxDateTime::Apr
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
604 { 4, wxDateTime::Apr
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
605 { 3, wxDateTime::Apr
, 2005, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
606 { 2, wxDateTime::Apr
, 2006, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
607 {11, wxDateTime::Mar
, 2007, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
608 { 9, wxDateTime::Mar
, 2008, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
609 { 8, wxDateTime::Mar
, 2009, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
612 { 28, wxDateTime::Oct
, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
613 { 27, wxDateTime::Oct
, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
614 { 25, wxDateTime::Oct
, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
615 { 31, wxDateTime::Oct
, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
616 { 30, wxDateTime::Oct
, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
617 { 29, wxDateTime::Oct
, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
618 { 27, wxDateTime::Oct
, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
619 { 26, wxDateTime::Oct
, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
620 { 25, wxDateTime::Oct
, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
621 { 31, wxDateTime::Oct
, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
622 { 29, wxDateTime::Oct
, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
623 { 28, wxDateTime::Oct
, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
624 { 27, wxDateTime::Oct
, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
625 { 26, wxDateTime::Oct
, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
626 { 31, wxDateTime::Oct
, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
627 { 30, wxDateTime::Oct
, 2005, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
628 { 29, wxDateTime::Oct
, 2006, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
629 { 4, wxDateTime::Nov
, 2007, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
630 { 2, wxDateTime::Nov
, 2008, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
631 { 1, wxDateTime::Nov
, 2009, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay
, 0 },
636 for ( size_t n
= 0; n
< WXSIZEOF(datesDST
[0]); n
++ )
638 const int year
= 1990 + n
;
639 wxDateTime dtBegin
= wxDateTime::GetBeginDST(year
, wxDateTime::USA
),
640 dtEnd
= wxDateTime::GetEndDST(year
, wxDateTime::USA
);
642 const Date
& dBegin
= datesDST
[0][n
];
643 const Date
& dEnd
= datesDST
[1][n
];
645 CPPUNIT_ASSERT_EQUAL( dBegin
.DT().FormatDate(), dtBegin
.FormatDate() );
646 CPPUNIT_ASSERT_EQUAL( dEnd
.DT().FormatDate(), dtEnd
.FormatDate() );
650 // test wxDateTime -> text conversion
651 void DateTimeTestCase::TestTimeFormat()
653 // some information may be lost during conversion, so store what kind
654 // of info should we recover after a round trip
657 CompareNone
, // don't try comparing
658 CompareBoth
, // dates and times should be identical
659 CompareYear
, // don't compare centuries (fails for 2 digit years)
660 CompareDate
, // dates only
661 CompareTime
// time only
666 CompareKind compareKind
;
668 } formatTestFormats
[] =
670 { CompareYear
, "---> %c" }, // %c could use 2 digit years
671 { CompareDate
, "Date is %A, %d of %B, in year %Y" },
672 { CompareYear
, "Date is %x, time is %X" }, // %x could use 2 digits
673 { CompareTime
, "Time is %H:%M:%S or %I:%M:%S %p" },
674 { CompareNone
, "The day of year: %j, the week of year: %W" },
675 { CompareDate
, "ISO date without separators: %Y%m%d" },
676 { CompareBoth
, "RFC 2822 string: %Y-%m-%d %H:%M:%S.%l %z" },
680 const long timeZonesOffsets
[] =
682 wxDateTime::TimeZone(wxDateTime::Local
).GetOffset(),
684 // Fictitious TimeZone offsets to ensure time zone formating and
685 // interpretation works
690 static const Date formatTestDates
[] =
692 { 29, wxDateTime::May
, 1976, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
693 { 31, wxDateTime::Dec
, 1999, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
694 { 6, wxDateTime::Feb
, 1937, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
695 { 6, wxDateTime::Feb
, 1856, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
696 { 6, wxDateTime::Feb
, 1857, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
697 { 29, wxDateTime::May
, 2076, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay
},
699 // FIXME: the test with 02:15:25 time doesn't pass because of DST
700 // computation problems, we get back 03:15:25
701 { 29, wxDateTime::Feb
, 2400, 04, 15, 25, 0.0, wxDateTime::Inv_WeekDay
},
703 // Need to add support for BCE dates.
704 { 01, wxDateTime::Jan
, -52, 03, 16, 47, 0.0, wxDateTime::Inv_WeekDay
},
708 for ( unsigned idxtz
= 0; idxtz
< WXSIZEOF(timeZonesOffsets
); ++idxtz
)
710 wxDateTime::TimeZone
tz(timeZonesOffsets
[idxtz
]);
711 const bool isLocalTz
= tz
.GetOffset() == -wxGetTimeZone();
713 for ( size_t d
= 0; d
< WXSIZEOF(formatTestDates
); d
++ )
715 wxDateTime dt
= formatTestDates
[d
].DT();
716 for ( unsigned n
= 0; n
< WXSIZEOF(formatTestFormats
); n
++ )
718 const char *fmt
= formatTestFormats
[n
].format
;
720 // skip the check with %p for those locales which have empty AM/PM strings:
721 // for those locales it's impossible to pass the test with %p...
723 wxDateTime::GetAmPmStrings(&am
, &pm
);
724 if (am
.empty() && pm
.empty() && wxStrstr(fmt
, "%p") != NULL
)
727 // what can we recover?
728 CompareKind kind
= formatTestFormats
[n
].compareKind
;
730 // When using a different time zone we must perform a time zone
731 // conversion below which doesn't always work correctly, check
732 // for the cases when it doesn't.
735 // DST computation doesn't work correctly for dates above
736 // 2038 currently on the systems with 32 bit time_t.
737 if ( dt
.GetYear() >= 2038 )
740 // We can't compare just dates nor just times when doing TZ
741 // conversion as both are affected by the DST: for the
742 // dates, the DST can switch midnight to 23:00 of the
743 // previous day while for the times DST can be different
744 // for the original date and today.
745 if ( kind
== CompareDate
|| kind
== CompareTime
)
749 // do convert date to string
750 wxString s
= dt
.Format(fmt
, tz
);
754 const char *result
= dt2
.ParseFormat(s
, fmt
);
757 // conversion failed - should it have?
759 ("Test #%u failed: failed to parse \"%s\"", n
, s
),
763 else // conversion succeeded
765 // currently ParseFormat() doesn't support "%Z" and so is
766 // incapable of parsing time zone part used at the end of date
767 // representations in many (but not "C") locales, compensate
768 // for it ourselves by simply consuming and ignoring it
769 while ( *result
&& (*result
>= 'A' && *result
<= 'Z') )
773 ("Test #%u failed: \"%s\" was left unparsed in \"%s\"",
778 // Without "%z" we can't recover the time zone used in the
779 // call to Format() so we need to call MakeFromTimezone()
781 if ( !strstr(fmt
, "%z") && !isLocalTz
)
782 dt2
.MakeFromTimezone(tz
);
787 if ( dt2
.GetCentury() != dt
.GetCentury() )
789 CPPUNIT_ASSERT_EQUAL(dt
.GetYear() % 100,
790 dt2
.GetYear() % 100);
792 dt2
.SetYear(dt
.GetYear());
794 // fall through and compare everything
797 CPPUNIT_ASSERT_EQUAL( dt
, dt2
);
801 CPPUNIT_ASSERT( dt
.IsSameDate(dt2
) );
805 CPPUNIT_ASSERT( dt
.IsSameTime(dt2
) );
809 wxFAIL_MSG( wxT("unexpected") );
820 // special case which was known to fail
821 CPPUNIT_ASSERT( dt
.ParseFormat("02/06/1856", "%x") );
822 CPPUNIT_ASSERT_EQUAL( 1856, dt
.GetYear() );
825 // also test %l separately
826 CPPUNIT_ASSERT( dt
.ParseFormat("12:23:45.678", "%H:%M:%S.%l") );
827 CPPUNIT_ASSERT_EQUAL( 678, dt
.GetMillisecond() );
829 // test special case of %l matching 0 milliseconds
830 CPPUNIT_ASSERT( dt
.ParseFormat("12:23:45.000", "%H:%M:%S.%l") );
831 CPPUNIT_ASSERT_EQUAL( 0, dt
.GetMillisecond() );
833 // test partially specified dates too
834 wxDateTime
dtDef(26, wxDateTime::Sep
, 2008);
835 CPPUNIT_ASSERT( dt
.ParseFormat("17", "%d") );
836 CPPUNIT_ASSERT_EQUAL( 17, dt
.GetDay() );
838 // test some degenerate cases
839 CPPUNIT_ASSERT( !dt
.ParseFormat("", "%z") );
840 CPPUNIT_ASSERT( !dt
.ParseFormat("", "%%") );
842 // test compilation of some calls which should compile (and not result in
843 // ambiguity because of char*<->wxCStrData<->wxString conversions)
845 CPPUNIT_ASSERT( !dt
.ParseFormat("foo") );
846 CPPUNIT_ASSERT( !dt
.ParseFormat(wxT("foo")) );
847 CPPUNIT_ASSERT( !dt
.ParseFormat(s
) );
848 dt
.ParseFormat(s
.c_str()); // Simply test compilation of this one.
850 CPPUNIT_ASSERT( !dt
.ParseFormat("foo", "%c") );
851 CPPUNIT_ASSERT( !dt
.ParseFormat(wxT("foo"), "%c") );
852 CPPUNIT_ASSERT( !dt
.ParseFormat(s
, "%c") );
853 dt
.ParseFormat(s
.c_str(), "%c");
855 CPPUNIT_ASSERT( !dt
.ParseFormat("foo", wxT("%c")) );
856 CPPUNIT_ASSERT( !dt
.ParseFormat(wxT("foo"), wxT("%c")) );
857 CPPUNIT_ASSERT( !dt
.ParseFormat(s
, "%c") );
858 dt
.ParseFormat(s
.c_str(), wxT("%c"));
861 CPPUNIT_ASSERT( !dt
.ParseFormat("foo", spec
) );
862 CPPUNIT_ASSERT( !dt
.ParseFormat(wxT("foo"), spec
) );
863 CPPUNIT_ASSERT( !dt
.ParseFormat(s
, spec
) );
864 dt
.ParseFormat(s
.c_str(), spec
);
867 // Test parsing time in free format.
868 void DateTimeTestCase::TestTimeParse()
872 // Parsing standard formats should work.
873 CPPUNIT_ASSERT( dt
.ParseTime("12:34:56") );
874 CPPUNIT_ASSERT_EQUAL( "12:34:56", dt
.FormatISOTime() );
876 // Parsing just hours should work too.
878 CPPUNIT_ASSERT( dt
.ParseTime("17") );
879 CPPUNIT_ASSERT_EQUAL( "17:00:00", dt
.FormatISOTime() );
881 // Parsing gibberish shouldn't work.
882 CPPUNIT_ASSERT( !dt
.ParseTime("bloordyblop") );
885 void DateTimeTestCase::TestTimeSpanFormat()
887 static const struct TimeSpanFormatTestData
889 long h
, min
, sec
, msec
;
894 { 12, 34, 56, 789, "%H:%M:%S.%l", "12:34:56.789" },
895 { 1, 2, 3, 0, "%H:%M:%S", "01:02:03" },
896 { 1, 2, 3, 0, "%S", "3723" },
897 { -1, -2, -3, 0, "%S", "-3723" },
898 { -1, -2, -3, 0, "%H:%M:%S", "-01:02:03" },
899 { 26, 0, 0, 0, "%H", "26" },
900 { 26, 0, 0, 0, "%D, %H", "1, 02" },
901 { -26, 0, 0, 0, "%H", "-26" },
902 { -26, 0, 0, 0, "%D, %H", "-1, 02" },
903 { 219, 0, 0, 0, "%H", "219" },
904 { 219, 0, 0, 0, "%D, %H", "9, 03" },
905 { 219, 0, 0, 0, "%E, %D, %H", "1, 2, 03" },
906 { 0, -1, 0, 0, "%H:%M:%S", "-00:01:00" },
907 { 0, 0, -1, 0, "%H:%M:%S", "-00:00:01" },
910 for ( size_t n
= 0; n
< WXSIZEOF(testSpans
); n
++ )
912 const TimeSpanFormatTestData
& td
= testSpans
[n
];
913 wxTimeSpan
ts(td
.h
, td
.min
, td
.sec
, td
.msec
);
914 CPPUNIT_ASSERT_EQUAL( td
.result
, ts
.Format(td
.fmt
) );
918 void DateTimeTestCase::TestTimeTicks()
920 static const wxDateTime::TimeZone
TZ_LOCAL(wxDateTime::Local
);
921 static const wxDateTime::TimeZone
TZ_TEST(wxDateTime::NZST
);
923 // this offset is needed to make the test work in any time zone when we
924 // only have expected test results in UTC in testDates
925 static const long tzOffset
= TZ_LOCAL
.GetOffset() - TZ_TEST
.GetOffset();
927 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
929 const Date
& d
= testDates
[n
];
930 if ( d
.gmticks
== -1 )
933 wxDateTime dt
= d
.DT().MakeTimezone(TZ_TEST
, true /* no DST */);
935 // GetValue() returns internal UTC-based representation, we need to
936 // convert it to local TZ before comparing
937 time_t ticks
= (dt
.GetValue() / 1000).ToLong() + TZ_LOCAL
.GetOffset();
940 CPPUNIT_ASSERT_EQUAL( d
.gmticks
, ticks
+ tzOffset
);
942 dt
= d
.DT().FromTimezone(wxDateTime::UTC
);
943 ticks
= (dt
.GetValue() / 1000).ToLong();
944 CPPUNIT_ASSERT_EQUAL( d
.gmticks
, ticks
);
948 // test parsing dates in RFC822 format
949 void DateTimeTestCase::TestParceRFC822()
951 static const struct ParseTestData
954 Date date
; // NB: this should be in UTC
959 "Sat, 18 Dec 1999 00:46:40 +0100",
960 { 17, wxDateTime::Dec
, 1999, 23, 46, 40 },
964 "Wed, 1 Dec 1999 05:17:20 +0300",
965 { 1, wxDateTime::Dec
, 1999, 2, 17, 20 },
969 "Sun, 28 Aug 2005 03:31:30 +0200",
970 { 28, wxDateTime::Aug
, 2005, 1, 31, 30 },
975 "Sat, 18 Dec 1999 10:48:30 -0500",
976 { 18, wxDateTime::Dec
, 1999, 15, 48, 30 },
980 // seconds are optional according to the RFC
982 "Sun, 01 Jun 2008 16:30 +0200",
983 { 1, wxDateTime::Jun
, 2008, 14, 30, 00 },
987 // try some bogus ones too
989 "Sun, 01 Jun 2008 16:30: +0200",
995 for ( unsigned n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
997 const char * const datestr
= parseTestDates
[n
].rfc822
;
1000 if ( dt
.ParseRfc822Date(datestr
) )
1003 ("Erroneously parsed \"%s\"", datestr
),
1004 parseTestDates
[n
].good
1007 wxDateTime dtReal
= parseTestDates
[n
].date
.DT().FromUTC();
1008 CPPUNIT_ASSERT_EQUAL( dtReal
, dt
);
1010 else // failed to parse
1013 ("Failed to parse \"%s\"", datestr
),
1014 !parseTestDates
[n
].good
1020 // test parsing dates in free format
1021 void DateTimeTestCase::TestDateParse()
1023 static const struct ParseTestData
1026 Date date
; // NB: this should be in UTC
1028 } parseTestDates
[] =
1030 { "21 Mar 2006", { 21, wxDateTime::Mar
, 2006 }, true },
1031 { "29 Feb 1976", { 29, wxDateTime::Feb
, 1976 }, true },
1032 { "Feb 29 1976", { 29, wxDateTime::Feb
, 1976 }, true },
1033 { "31/03/06", { 31, wxDateTime::Mar
, 6 }, true },
1034 { "31/03/2006", { 31, wxDateTime::Mar
, 2006 }, true },
1036 // some invalid ones too
1045 CPPUNIT_ASSERT( dt
.ParseDate(wxT("today")) );
1046 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), dt
);
1048 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
1050 const wxString datestr
= TranslateDate(parseTestDates
[n
].str
);
1052 const char * const end
= dt
.ParseDate(datestr
);
1056 ("Erroneously parsed \"%s\"", datestr
),
1057 parseTestDates
[n
].good
1060 CPPUNIT_ASSERT_EQUAL( parseTestDates
[n
].date
.DT(), dt
);
1062 else // failed to parse
1065 ("Failed to parse \"%s\"", datestr
),
1066 !parseTestDates
[n
].good
1071 // Check that incomplete parse works correctly.
1072 const char* p
= dt
.ParseFormat("2012-03-23 12:34:56", "%Y-%m-%d");
1073 CPPUNIT_ASSERT_EQUAL( " 12:34:56", wxString(p
) );
1076 void DateTimeTestCase::TestDateParseISO()
1081 Date date
; // NB: this should be in UTC
1083 } parseTestDates
[] =
1085 { "2006-03-21", { 21, wxDateTime::Mar
, 2006 }, true },
1086 { "1976-02-29", { 29, wxDateTime::Feb
, 1976 }, true },
1087 { "0006-03-31", { 31, wxDateTime::Mar
, 6 }, true },
1089 // some invalid ones too
1099 wxDateTime::wxDateTime_t hour
, min
, sec
;
1101 } parseTestTimes
[] =
1103 { "13:42:17", 13, 42, 17, true },
1104 { "02:17:01", 2, 17, 1, true },
1106 // some invalid ones too
1113 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
1116 if ( dt
.ParseISODate(parseTestDates
[n
].str
) )
1118 CPPUNIT_ASSERT( parseTestDates
[n
].good
);
1120 CPPUNIT_ASSERT_EQUAL( parseTestDates
[n
].date
.DT(), dt
);
1122 for ( size_t m
= 0; m
< WXSIZEOF(parseTestTimes
); m
++ )
1124 wxString dtCombined
;
1125 dtCombined
<< parseTestDates
[n
].str
1127 << parseTestTimes
[m
].str
;
1129 if ( dt
.ParseISOCombined(dtCombined
) )
1131 CPPUNIT_ASSERT( parseTestTimes
[m
].good
);
1133 CPPUNIT_ASSERT_EQUAL( parseTestTimes
[m
].hour
, dt
.GetHour()) ;
1134 CPPUNIT_ASSERT_EQUAL( parseTestTimes
[m
].min
, dt
.GetMinute()) ;
1135 CPPUNIT_ASSERT_EQUAL( parseTestTimes
[m
].sec
, dt
.GetSecond()) ;
1137 else // failed to parse combined date/time
1139 CPPUNIT_ASSERT( !parseTestTimes
[m
].good
);
1143 else // failed to parse
1145 CPPUNIT_ASSERT( !parseTestDates
[n
].good
);
1150 void DateTimeTestCase::TestDateTimeParse()
1152 static const struct ParseTestData
1155 Date date
; // NB: this should be in UTC
1157 } parseTestDates
[] =
1160 "Thu 22 Nov 2007 07:40:00 PM",
1161 { 22, wxDateTime::Nov
, 2007, 19, 40, 0 },
1167 { 4, wxDateTime::Jan
, 2010, 14, 30, 0 },
1173 { 1, wxDateTime::Jan
, 9999, 0, 0, 0},
1178 "2012-01-01 10:12:05 +0100",
1179 { 1, wxDateTime::Jan
, 2012, 10, 12, 5, -1 },
1180 false // ParseDateTime does know yet +0100
1184 // the test strings here use "PM" which is not available in all locales so
1185 // we need to use "C" locale for them
1189 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
1191 const wxString datestr
= TranslateDate(parseTestDates
[n
].str
);
1193 const char * const end
= dt
.ParseDateTime(datestr
);
1197 ("Erroneously parsed \"%s\"", datestr
),
1198 parseTestDates
[n
].good
1201 CPPUNIT_ASSERT_EQUAL( parseTestDates
[n
].date
.DT(), dt
);
1203 else // failed to parse
1206 ("Failed to parse \"%s\"", datestr
),
1207 !parseTestDates
[n
].good
1210 CPPUNIT_ASSERT( !parseTestDates
[n
].good
);
1215 void DateTimeTestCase::TestTimeArithmetics()
1217 static const wxDateSpan testArithmData
[] =
1221 wxDateSpan::Month(),
1225 // the test will *not* work with arbitrary date!
1226 wxDateTime
dt(2, wxDateTime::Dec
, 1999),
1230 for ( size_t n
= 0; n
< WXSIZEOF(testArithmData
); n
++ )
1232 const wxDateSpan
& span
= testArithmData
[n
];
1236 CPPUNIT_ASSERT_EQUAL( dt
, dt1
- span
);
1237 CPPUNIT_ASSERT_EQUAL( dt
, dt2
+ span
);
1238 CPPUNIT_ASSERT_EQUAL( dt1
, dt2
+ 2*span
);
1239 CPPUNIT_ASSERT_EQUAL( span
, dt1
.DiffAsDateSpan(dt
) );
1242 // More date span arithmetics tests
1243 wxDateTime
dtd1(5, wxDateTime::Jun
, 1998);
1244 wxDateTime
dtd2(6, wxDateTime::Aug
, 1999);
1246 // All parts in dtd2 is after dtd1
1247 CPPUNIT_ASSERT_EQUAL( wxDateSpan(1, 2, 0, 1), dtd2
.DiffAsDateSpan(dtd1
) );
1249 // Year and month after, day earlier, so no full month
1250 // Jul has 31 days, so it's 31 - 5 + 4 = 30, or 4w 2d
1251 dtd2
.Set(4, wxDateTime::Aug
, 1999);
1252 CPPUNIT_ASSERT_EQUAL( wxDateSpan(1, 1, 4, 2), dtd2
.DiffAsDateSpan(dtd1
) );
1254 // Year and day after, month earlier, so no full year, but same day diff as
1256 dtd2
.Set(6, wxDateTime::May
, 1999);
1257 CPPUNIT_ASSERT_EQUAL( wxDateSpan(0, 11, 0, 1), dtd2
.DiffAsDateSpan(dtd1
) );
1259 // Year after, month and day earlier, so no full month and no full year
1260 // April has 30 days, so it's 30 - 5 + 4 = 29, or 4w 1d
1261 dtd2
.Set(4, wxDateTime::May
, 1999);
1262 CPPUNIT_ASSERT_EQUAL( wxDateSpan(0, 10, 4, 1), dtd2
.DiffAsDateSpan(dtd1
) );
1264 // And a reverse. Now we should use days in Jun (again 30 => 4w 1d)
1265 CPPUNIT_ASSERT_EQUAL( wxDateSpan(0, -10, -4, -1), dtd1
.DiffAsDateSpan(dtd2
) );
1268 void DateTimeTestCase::TestDSTBug()
1270 /////////////////////////
1272 wxDateTime dt
= wxDateTime::GetEndDST(2004, wxDateTime::France
);
1273 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
1274 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
1275 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
1276 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
1277 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
1278 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
1279 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
1281 /////////////////////////
1284 CPPUNIT_ASSERT_EQUAL(5, (int)dt
.GetHour());
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());
1295 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
1297 /////////////////////////
1299 #ifdef CHANGE_SYSTEM_DATE
1301 DateChanger
change(2004, 10, 31, 5, 0, 0);
1302 dt
= wxDateTime::Today();
1305 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
1306 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
1307 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
1308 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetHour());
1309 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMinute());
1310 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
1311 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
1313 /////////////////////////
1314 // Test Set(hour, minute, second, milli)
1317 DateChanger
change(2004, 10, 31, 5, 0, 0);
1318 dt
.Set(1, 30, 0, 0);
1319 dt2
.Set(5, 30, 0, 0);
1322 CPPUNIT_ASSERT_EQUAL(31, (int)dt
.GetDay());
1323 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt
.GetMonth());
1324 CPPUNIT_ASSERT_EQUAL(2004, (int)dt
.GetYear());
1325 CPPUNIT_ASSERT_EQUAL(1, (int)dt
.GetHour());
1326 CPPUNIT_ASSERT_EQUAL(30, (int)dt
.GetMinute());
1327 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetSecond());
1328 CPPUNIT_ASSERT_EQUAL(0, (int)dt
.GetMillisecond());
1330 CPPUNIT_ASSERT_EQUAL(31, (int)dt2
.GetDay());
1331 CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct
, dt2
.GetMonth());
1332 CPPUNIT_ASSERT_EQUAL(2004, (int)dt2
.GetYear());
1333 CPPUNIT_ASSERT_EQUAL(5, (int)dt2
.GetHour());
1334 CPPUNIT_ASSERT_EQUAL(30, (int)dt2
.GetMinute());
1335 CPPUNIT_ASSERT_EQUAL(0, (int)dt2
.GetSecond());
1336 CPPUNIT_ASSERT_EQUAL(0, (int)dt2
.GetMillisecond());
1337 #endif // CHANGE_SYSTEM_DATE
1339 // Verify that setting the date to the beginning of the DST period moves it
1340 // forward (as this date on its own would be invalid). The problem here is
1341 // that our GetBeginDST() is far from being trustworthy, so just try a
1342 // couple of dates for the common time zones and check that all of them are
1343 // either unchanged or moved forward.
1344 wxDateTime
dtDST(10, wxDateTime::Mar
, 2013, 2, 0, 0);
1345 if ( dtDST
.GetHour() != 2 )
1346 CPPUNIT_ASSERT_EQUAL( 3, dtDST
.GetHour() );
1348 dtDST
= wxDateTime(31, wxDateTime::Mar
, 2013, 2, 0, 0);
1349 if ( dtDST
.GetHour() != 2 )
1350 CPPUNIT_ASSERT_EQUAL( 3, dtDST
.GetHour() );
1353 void DateTimeTestCase::TestDateOnly()
1355 wxDateTime
dt(19, wxDateTime::Jan
, 2007, 15, 01, 00);
1357 static const wxDateTime::wxDateTime_t DATE_ZERO
= 0;
1358 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetHour() );
1359 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetMinute() );
1360 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetSecond() );
1361 CPPUNIT_ASSERT_EQUAL( DATE_ZERO
, dt
.GetDateOnly().GetMillisecond() );
1364 CPPUNIT_ASSERT_EQUAL( wxDateTime(19, wxDateTime::Jan
, 2007), dt
);
1366 CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), wxDateTime::Now().GetDateOnly() );
1369 #endif // wxUSE_DATETIME