datetimetest.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  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. ///////////////////////////////////////////////////////////////////////////////
  8. // ----------------------------------------------------------------------------
  9. // headers
  10. // ----------------------------------------------------------------------------
  11. #include "testprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #if wxUSE_DATETIME
  16. #ifndef WX_PRECOMP
  17. #include "wx/time.h" // wxGetTimeZone()
  18. #endif // WX_PRECOMP
  19. #include "wx/wxcrt.h" // for wxStrstr()
  20. #include "testdate.h"
  21. // to test Today() meaningfully we must be able to change the system date which
  22. // is not usually the case, but if we're under Win32 we can try it -- define
  23. // the macro below to do it
  24. //#define CHANGE_SYSTEM_DATE
  25. #ifndef __WINDOWS__
  26. #undef CHANGE_SYSTEM_DATE
  27. #endif
  28. #ifdef CHANGE_SYSTEM_DATE
  29. class DateChanger
  30. {
  31. public:
  32. DateChanger(int year, int month, int day, int hour, int min, int sec)
  33. {
  34. SYSTEMTIME st;
  35. st.wDay = day;
  36. st.wMonth = month;
  37. st.wYear = year;
  38. st.wHour = hour;
  39. st.wMinute = min;
  40. st.wSecond = sec;
  41. st.wMilliseconds = 0;
  42. ::GetSystemTime(&m_savedTime);
  43. ::GetTimeZoneInformation(&m_tzi);
  44. m_changed = ::SetSystemTime(&st) != 0;
  45. }
  46. ~DateChanger()
  47. {
  48. if ( m_changed )
  49. {
  50. ::SetSystemTime(&m_savedTime);
  51. ::SetTimeZoneInformation(&m_tzi);
  52. }
  53. }
  54. private:
  55. SYSTEMTIME m_savedTime;
  56. TIME_ZONE_INFORMATION m_tzi;
  57. bool m_changed;
  58. };
  59. #endif // CHANGE_SYSTEM_DATE
  60. // helper function translating week day/month names from English to the current
  61. // locale
  62. static wxString TranslateDate(const wxString& str)
  63. {
  64. // small optimization: if there are no alphabetic characters in the string,
  65. // there is nothing to translate
  66. wxString::const_iterator i, end = str.end();
  67. for ( i = str.begin(); i != end; ++i )
  68. {
  69. if ( isalpha(*i) )
  70. break;
  71. }
  72. if ( i == end )
  73. return str;
  74. wxString trans(str);
  75. for ( wxDateTime::WeekDay wd = wxDateTime::Sun;
  76. wd < wxDateTime::Inv_WeekDay;
  77. wxNextWDay(wd) )
  78. {
  79. trans.Replace
  80. (
  81. wxDateTime::GetEnglishWeekDayName(wd, wxDateTime::Name_Abbr),
  82. wxDateTime::GetWeekDayName(wd, wxDateTime::Name_Abbr)
  83. );
  84. }
  85. for ( wxDateTime::Month mon = wxDateTime::Jan;
  86. mon < wxDateTime::Inv_Month;
  87. wxNextMonth(mon) )
  88. {
  89. trans.Replace
  90. (
  91. wxDateTime::GetEnglishMonthName(mon, wxDateTime::Name_Abbr),
  92. wxDateTime::GetMonthName(mon, wxDateTime::Name_Abbr)
  93. );
  94. }
  95. return trans;
  96. }
  97. // ----------------------------------------------------------------------------
  98. // broken down date representation used for testing
  99. // ----------------------------------------------------------------------------
  100. struct Date
  101. {
  102. wxDateTime::wxDateTime_t day;
  103. wxDateTime::Month month;
  104. int year;
  105. wxDateTime::wxDateTime_t hour, min, sec;
  106. double jdn;
  107. wxDateTime::WeekDay wday;
  108. time_t gmticks;
  109. void Init(const wxDateTime::Tm& tm)
  110. {
  111. day = tm.mday;
  112. month = tm.mon;
  113. year = tm.year;
  114. hour = tm.hour;
  115. min = tm.min;
  116. sec = tm.sec;
  117. jdn = 0.0;
  118. gmticks = -1;
  119. }
  120. wxDateTime DT() const
  121. { return wxDateTime(day, month, year, hour, min, sec); }
  122. bool SameDay(const wxDateTime::Tm& tm) const
  123. {
  124. return day == tm.mday && month == tm.mon && year == tm.year;
  125. }
  126. wxString Format() const
  127. {
  128. wxString s;
  129. s.Printf(wxT("%02d:%02d:%02d %10s %02d, %4d%s"),
  130. hour, min, sec,
  131. wxDateTime::GetMonthName(month).c_str(),
  132. day,
  133. abs(wxDateTime::ConvertYearToBC(year)),
  134. year > 0 ? wxT("AD") : wxT("BC"));
  135. return s;
  136. }
  137. wxString FormatDate() const
  138. {
  139. wxString s;
  140. s.Printf(wxT("%02d-%s-%4d%s"),
  141. day,
  142. wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(),
  143. abs(wxDateTime::ConvertYearToBC(year)),
  144. year > 0 ? wxT("AD") : wxT("BC"));
  145. return s;
  146. }
  147. };
  148. // ----------------------------------------------------------------------------
  149. // test data
  150. // ----------------------------------------------------------------------------
  151. static const Date testDates[] =
  152. {
  153. { 1, wxDateTime::Jan, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu, 0 },
  154. { 7, wxDateTime::Feb, 2036, 00, 00, 00, 2464730.5, wxDateTime::Thu, -1 },
  155. { 8, wxDateTime::Feb, 2036, 00, 00, 00, 2464731.5, wxDateTime::Fri, -1 },
  156. { 1, wxDateTime::Jan, 2037, 00, 00, 00, 2465059.5, wxDateTime::Thu, -1 },
  157. { 1, wxDateTime::Jan, 2038, 00, 00, 00, 2465424.5, wxDateTime::Fri, -1 },
  158. { 21, wxDateTime::Jan, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon, -1 },
  159. { 29, wxDateTime::May, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat, 202219200 },
  160. { 29, wxDateTime::Feb, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun, 194400000 },
  161. { 1, wxDateTime::Jan, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon, -1 },
  162. { 1, wxDateTime::Jan, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon, -1 },
  163. { 15, wxDateTime::Oct, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri, -1 },
  164. { 4, wxDateTime::Oct, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon, -1 },
  165. { 1, wxDateTime::Mar, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu, -1 },
  166. { 1, wxDateTime::Jan, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon, -1 },
  167. { 31, wxDateTime::Dec, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun, -1 },
  168. { 1, wxDateTime::Jan, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat, -1 },
  169. { 12, wxDateTime::Aug, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri, -1 },
  170. { 12, wxDateTime::Aug, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat, -1 },
  171. { 24, wxDateTime::Nov, -4713, 00, 00, 00, -0.5, wxDateTime::Mon, -1 },
  172. };
  173. // ----------------------------------------------------------------------------
  174. // test class
  175. // ----------------------------------------------------------------------------
  176. class DateTimeTestCase : public CppUnit::TestCase
  177. {
  178. public:
  179. DateTimeTestCase() { }
  180. private:
  181. CPPUNIT_TEST_SUITE( DateTimeTestCase );
  182. CPPUNIT_TEST( TestLeapYears );
  183. CPPUNIT_TEST( TestTimeSet );
  184. CPPUNIT_TEST( TestTimeJDN );
  185. CPPUNIT_TEST( TestTimeWNumber );
  186. CPPUNIT_TEST( TestTimeWDays );
  187. CPPUNIT_TEST( TestTimeDST );
  188. CPPUNIT_TEST( TestTimeFormat );
  189. CPPUNIT_TEST( TestTimeParse );
  190. CPPUNIT_TEST( TestTimeSpanFormat );
  191. CPPUNIT_TEST( TestTimeTicks );
  192. CPPUNIT_TEST( TestParceRFC822 );
  193. CPPUNIT_TEST( TestDateParse );
  194. CPPUNIT_TEST( TestDateParseISO );
  195. CPPUNIT_TEST( TestDateTimeParse );
  196. CPPUNIT_TEST( TestDateWeekFormat );
  197. CPPUNIT_TEST( TestTimeArithmetics );
  198. CPPUNIT_TEST( TestDSTBug );
  199. CPPUNIT_TEST( TestDateOnly );
  200. CPPUNIT_TEST_SUITE_END();
  201. void TestLeapYears();
  202. void TestTimeSet();
  203. void TestTimeJDN();
  204. void TestTimeWNumber();
  205. void TestTimeWDays();
  206. void TestTimeDST();
  207. void TestTimeFormat();
  208. void TestTimeParse();
  209. void TestTimeSpanFormat();
  210. void TestTimeTicks();
  211. void TestParceRFC822();
  212. void TestDateParse();
  213. void TestDateParseISO();
  214. void TestDateTimeParse();
  215. void TestDateWeekFormat();
  216. void TestTimeArithmetics();
  217. void TestDSTBug();
  218. void TestDateOnly();
  219. DECLARE_NO_COPY_CLASS(DateTimeTestCase)
  220. };
  221. // register in the unnamed registry so that these tests are run by default
  222. CPPUNIT_TEST_SUITE_REGISTRATION( DateTimeTestCase );
  223. // also include in its own registry so that these tests can be run alone
  224. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DateTimeTestCase, "DateTimeTestCase" );
  225. // ============================================================================
  226. // implementation
  227. // ============================================================================
  228. // test leap years detection
  229. void DateTimeTestCase::TestLeapYears()
  230. {
  231. static const struct LeapYearTestData
  232. {
  233. int year;
  234. bool isLeap;
  235. } years[] =
  236. {
  237. { 1900, false },
  238. { 1990, false },
  239. { 1976, true },
  240. { 2000, true },
  241. { 2030, false },
  242. { 1984, true },
  243. { 2100, false },
  244. { 2400, true },
  245. };
  246. for ( size_t n = 0; n < WXSIZEOF(years); n++ )
  247. {
  248. const LeapYearTestData& y = years[n];
  249. CPPUNIT_ASSERT_EQUAL( y.isLeap, wxDateTime::IsLeapYear(y.year) );
  250. }
  251. }
  252. // test constructing wxDateTime objects
  253. void DateTimeTestCase::TestTimeSet()
  254. {
  255. for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
  256. {
  257. const Date& d1 = testDates[n];
  258. wxDateTime dt = d1.DT();
  259. Date d2;
  260. d2.Init(dt.GetTm());
  261. wxString s1 = d1.Format(),
  262. s2 = d2.Format();
  263. CPPUNIT_ASSERT_EQUAL( s1, s2 );
  264. }
  265. }
  266. // test conversions to JDN &c
  267. void DateTimeTestCase::TestTimeJDN()
  268. {
  269. for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
  270. {
  271. const Date& d = testDates[n];
  272. wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
  273. // JDNs must be computed for UTC times
  274. double jdn = dt.FromUTC().GetJulianDayNumber();
  275. CPPUNIT_ASSERT_EQUAL( d.jdn, jdn );
  276. dt.Set(jdn);
  277. CPPUNIT_ASSERT_EQUAL( jdn, dt.GetJulianDayNumber() );
  278. }
  279. }
  280. // test week days computation
  281. void DateTimeTestCase::TestTimeWDays()
  282. {
  283. // test GetWeekDay()
  284. size_t n;
  285. for ( n = 0; n < WXSIZEOF(testDates); n++ )
  286. {
  287. const Date& d = testDates[n];
  288. wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
  289. wxDateTime::WeekDay wday = dt.GetWeekDay();
  290. CPPUNIT_ASSERT_EQUAL( d.wday, wday );
  291. }
  292. // test SetToWeekDay()
  293. struct WeekDateTestData
  294. {
  295. Date date; // the real date (precomputed)
  296. int nWeek; // its week index in the month
  297. wxDateTime::WeekDay wday; // the weekday
  298. wxDateTime::Month month; // the month
  299. int year; // and the year
  300. wxString Format() const
  301. {
  302. wxString s, which;
  303. switch ( nWeek < -1 ? -nWeek : nWeek )
  304. {
  305. case 1: which = wxT("first"); break;
  306. case 2: which = wxT("second"); break;
  307. case 3: which = wxT("third"); break;
  308. case 4: which = wxT("fourth"); break;
  309. case 5: which = wxT("fifth"); break;
  310. case -1: which = wxT("last"); break;
  311. }
  312. if ( nWeek < -1 )
  313. {
  314. which += wxT(" from end");
  315. }
  316. s.Printf(wxT("The %s %s of %s in %d"),
  317. which.c_str(),
  318. wxDateTime::GetWeekDayName(wday).c_str(),
  319. wxDateTime::GetMonthName(month).c_str(),
  320. year);
  321. return s;
  322. }
  323. };
  324. // the array data was generated by the following python program
  325. /*
  326. from DateTime import *
  327. from whrandom import *
  328. from string import *
  329. monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
  330. wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
  331. week = DateTimeDelta(7)
  332. for n in range(20):
  333. year = randint(1900, 2100)
  334. month = randint(1, 12)
  335. day = randint(1, 28)
  336. dt = DateTime(year, month, day)
  337. wday = dt.day_of_week
  338. countFromEnd = choice([-1, 1])
  339. weekNum = 0;
  340. while dt.month is month:
  341. dt = dt - countFromEnd * week
  342. weekNum = weekNum + countFromEnd
  343. data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
  344. print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
  345. "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
  346. */
  347. static const WeekDateTestData weekDatesTestData[] =
  348. {
  349. { { 20, wxDateTime::Mar, 2045, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 3, wxDateTime::Mon, wxDateTime::Mar, 2045 },
  350. { { 5, wxDateTime::Jun, 1985, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -4, wxDateTime::Wed, wxDateTime::Jun, 1985 },
  351. { { 12, wxDateTime::Nov, 1961, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -3, wxDateTime::Sun, wxDateTime::Nov, 1961 },
  352. { { 27, wxDateTime::Feb, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -1, wxDateTime::Fri, wxDateTime::Feb, 2093 },
  353. { { 4, wxDateTime::Jul, 2070, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -4, wxDateTime::Fri, wxDateTime::Jul, 2070 },
  354. { { 2, wxDateTime::Apr, 1906, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -5, wxDateTime::Mon, wxDateTime::Apr, 1906 },
  355. { { 19, wxDateTime::Jul, 2023, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -2, wxDateTime::Wed, wxDateTime::Jul, 2023 },
  356. { { 5, wxDateTime::May, 1958, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -4, wxDateTime::Mon, wxDateTime::May, 1958 },
  357. { { 11, wxDateTime::Aug, 1900, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 2, wxDateTime::Sat, wxDateTime::Aug, 1900 },
  358. { { 14, wxDateTime::Feb, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 2, wxDateTime::Wed, wxDateTime::Feb, 1945 },
  359. { { 25, wxDateTime::Jul, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -1, wxDateTime::Tue, wxDateTime::Jul, 1967 },
  360. { { 9, wxDateTime::May, 1916, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -4, wxDateTime::Tue, wxDateTime::May, 1916 },
  361. { { 20, wxDateTime::Jun, 1927, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 3, wxDateTime::Mon, wxDateTime::Jun, 1927 },
  362. { { 2, wxDateTime::Aug, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, wxDateTime::Wed, wxDateTime::Aug, 2000 },
  363. { { 20, wxDateTime::Apr, 2044, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 3, wxDateTime::Wed, wxDateTime::Apr, 2044 },
  364. { { 20, wxDateTime::Feb, 1932, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -2, wxDateTime::Sat, wxDateTime::Feb, 1932 },
  365. { { 25, wxDateTime::Jul, 2069, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 4, wxDateTime::Thu, wxDateTime::Jul, 2069 },
  366. { { 3, wxDateTime::Apr, 1925, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, wxDateTime::Fri, wxDateTime::Apr, 1925 },
  367. { { 21, wxDateTime::Mar, 2093, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 3, wxDateTime::Sat, wxDateTime::Mar, 2093 },
  368. { { 3, wxDateTime::Dec, 2074, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, -5, wxDateTime::Mon, wxDateTime::Dec, 2074 }
  369. };
  370. wxDateTime dt;
  371. for ( n = 0; n < WXSIZEOF(weekDatesTestData); n++ )
  372. {
  373. const WeekDateTestData& wd = weekDatesTestData[n];
  374. dt.SetToWeekDay(wd.wday, wd.nWeek, wd.month, wd.year);
  375. const Date& d = wd.date;
  376. CPPUNIT_ASSERT( d.SameDay(dt.GetTm()) );
  377. }
  378. }
  379. // test the computation of (ISO) week numbers
  380. void DateTimeTestCase::TestTimeWNumber()
  381. {
  382. struct WeekNumberTestData
  383. {
  384. Date date; // the date
  385. wxDateTime::wxDateTime_t week; // the week number in the year
  386. wxDateTime::wxDateTime_t wmon; // the week number in the month
  387. wxDateTime::wxDateTime_t wmon2; // same but week starts with Sun
  388. wxDateTime::wxDateTime_t dnum; // day number in the year
  389. };
  390. // data generated with the following python script:
  391. /*
  392. from DateTime import *
  393. from whrandom import *
  394. from string import *
  395. monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
  396. wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
  397. def GetMonthWeek(dt):
  398. weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
  399. if weekNumMonth < 0:
  400. weekNumMonth = weekNumMonth + 53
  401. return weekNumMonth
  402. def GetLastSundayBefore(dt):
  403. if dt.iso_week[2] == 7:
  404. return dt
  405. else:
  406. return dt - DateTimeDelta(dt.iso_week[2])
  407. for n in range(20):
  408. year = randint(1900, 2100)
  409. month = randint(1, 12)
  410. day = randint(1, 28)
  411. dt = DateTime(year, month, day)
  412. dayNum = dt.day_of_year
  413. weekNum = dt.iso_week[1]
  414. weekNumMonth = GetMonthWeek(dt)
  415. weekNumMonth2 = 0
  416. dtSunday = GetLastSundayBefore(dt)
  417. while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
  418. weekNumMonth2 = weekNumMonth2 + 1
  419. dtSunday = dtSunday - DateTimeDelta(7)
  420. data = { 'day': rjust(`day`, 2), \
  421. 'month': monthNames[month - 1], \
  422. 'year': year, \
  423. 'weekNum': rjust(`weekNum`, 2), \
  424. 'weekNumMonth': weekNumMonth, \
  425. 'weekNumMonth2': weekNumMonth2, \
  426. 'dayNum': rjust(`dayNum`, 3) }
  427. print " { { %(day)s, "\
  428. "wxDateTime::%(month)s, "\
  429. "%(year)d }, "\
  430. "%(weekNum)s, "\
  431. "%(weekNumMonth)s, "\
  432. "%(weekNumMonth2)s, "\
  433. "%(dayNum)s }," % data
  434. */
  435. static const WeekNumberTestData weekNumberTestDates[] =
  436. {
  437. { { 27, wxDateTime::Dec, 1966, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 52, 5, 5, 361 },
  438. { { 22, wxDateTime::Jul, 1926, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 29, 4, 4, 203 },
  439. { { 22, wxDateTime::Oct, 2076, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 43, 4, 4, 296 },
  440. { { 1, wxDateTime::Jul, 1967, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 26, 1, 1, 182 },
  441. { { 8, wxDateTime::Nov, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 46, 2, 2, 313 },
  442. { { 21, wxDateTime::Mar, 1920, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 12, 3, 4, 81 },
  443. { { 7, wxDateTime::Jan, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 2, 2, 7 },
  444. { { 19, wxDateTime::Oct, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 42, 4, 4, 292 },
  445. { { 13, wxDateTime::Aug, 1955, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 32, 2, 2, 225 },
  446. { { 18, wxDateTime::Jul, 2087, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 29, 3, 3, 199 },
  447. { { 2, wxDateTime::Sep, 2028, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 35, 1, 1, 246 },
  448. { { 28, wxDateTime::Jul, 1945, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 30, 5, 4, 209 },
  449. { { 15, wxDateTime::Jun, 1901, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 24, 3, 3, 166 },
  450. { { 10, wxDateTime::Oct, 1939, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 41, 3, 2, 283 },
  451. { { 3, wxDateTime::Dec, 1965, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 48, 1, 1, 337 },
  452. { { 23, wxDateTime::Feb, 1940, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 8, 4, 4, 54 },
  453. { { 2, wxDateTime::Jan, 1987, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 1, 1, 2 },
  454. { { 11, wxDateTime::Aug, 2079, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 32, 2, 2, 223 },
  455. { { 2, wxDateTime::Feb, 2063, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 5, 1, 1, 33 },
  456. { { 16, wxDateTime::Oct, 1942, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 42, 3, 3, 289 },
  457. { { 30, wxDateTime::Dec, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 5, 5, 364 },
  458. { { 2, wxDateTime::Jan, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 1, 1, 2 },
  459. { { 5, wxDateTime::Jan, 2010, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 2, 2, 5 },
  460. { { 3, wxDateTime::Jan, 2011, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 2, 2, 3 },
  461. { { 31, wxDateTime::Dec, 2009, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 53, 5, 5, 365 },
  462. { { 31, wxDateTime::Dec, 2012, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 6, 6, 366 },
  463. { { 29, wxDateTime::Dec, 2013, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 52, 5, 5, 363 },
  464. { { 30, wxDateTime::Dec, 2013, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 6, 5, 364 },
  465. { { 31, wxDateTime::Dec, 2013, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 6, 5, 365 },
  466. };
  467. for ( size_t n = 0; n < WXSIZEOF(weekNumberTestDates); n++ )
  468. {
  469. const WeekNumberTestData& wn = weekNumberTestDates[n];
  470. const Date& d = wn.date;
  471. wxDateTime dt = d.DT();
  472. wxDateTime::wxDateTime_t
  473. week = dt.GetWeekOfYear(wxDateTime::Monday_First),
  474. wmon = dt.GetWeekOfMonth(wxDateTime::Monday_First),
  475. wmon2 = dt.GetWeekOfMonth(wxDateTime::Sunday_First),
  476. dnum = dt.GetDayOfYear();
  477. WX_ASSERT_EQUAL_MESSAGE( ("day of year for %s", d.Format()),
  478. wn.dnum, dnum );
  479. WX_ASSERT_EQUAL_MESSAGE( ("week of month (Monday) for %s", d.Format()),
  480. wn.wmon, wmon );
  481. WX_ASSERT_EQUAL_MESSAGE( ("week of month (Sunday) for %s", d.Format()),
  482. wn.wmon2, wmon2 );
  483. WX_ASSERT_EQUAL_MESSAGE( ("week of year for %s", d.Format()),
  484. wn.week, week );
  485. int year = d.year;
  486. if ( week == 1 && d.month != wxDateTime::Jan )
  487. {
  488. // this means we're in the first week of the next year
  489. year++;
  490. }
  491. wxDateTime
  492. dt2 = wxDateTime::SetToWeekOfYear(year, week, dt.GetWeekDay());
  493. CPPUNIT_ASSERT_EQUAL( dt, dt2 );
  494. }
  495. }
  496. // test DST applicability
  497. void DateTimeTestCase::TestTimeDST()
  498. {
  499. // taken from http://www.energy.ca.gov/daylightsaving.html
  500. static const Date datesDST[2][2009 - 1990 + 1] =
  501. {
  502. {
  503. { 1, wxDateTime::Apr, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  504. { 7, wxDateTime::Apr, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  505. { 5, wxDateTime::Apr, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  506. { 4, wxDateTime::Apr, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  507. { 3, wxDateTime::Apr, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  508. { 2, wxDateTime::Apr, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  509. { 7, wxDateTime::Apr, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  510. { 6, wxDateTime::Apr, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  511. { 5, wxDateTime::Apr, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  512. { 4, wxDateTime::Apr, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  513. { 2, wxDateTime::Apr, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  514. { 1, wxDateTime::Apr, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  515. { 7, wxDateTime::Apr, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  516. { 6, wxDateTime::Apr, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  517. { 4, wxDateTime::Apr, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  518. { 3, wxDateTime::Apr, 2005, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  519. { 2, wxDateTime::Apr, 2006, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  520. {11, wxDateTime::Mar, 2007, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  521. { 9, wxDateTime::Mar, 2008, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  522. { 8, wxDateTime::Mar, 2009, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  523. },
  524. {
  525. { 28, wxDateTime::Oct, 1990, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  526. { 27, wxDateTime::Oct, 1991, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  527. { 25, wxDateTime::Oct, 1992, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  528. { 31, wxDateTime::Oct, 1993, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  529. { 30, wxDateTime::Oct, 1994, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  530. { 29, wxDateTime::Oct, 1995, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  531. { 27, wxDateTime::Oct, 1996, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  532. { 26, wxDateTime::Oct, 1997, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  533. { 25, wxDateTime::Oct, 1998, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  534. { 31, wxDateTime::Oct, 1999, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  535. { 29, wxDateTime::Oct, 2000, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  536. { 28, wxDateTime::Oct, 2001, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  537. { 27, wxDateTime::Oct, 2002, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  538. { 26, wxDateTime::Oct, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  539. { 31, wxDateTime::Oct, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  540. { 30, wxDateTime::Oct, 2005, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  541. { 29, wxDateTime::Oct, 2006, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  542. { 4, wxDateTime::Nov, 2007, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  543. { 2, wxDateTime::Nov, 2008, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  544. { 1, wxDateTime::Nov, 2009, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 },
  545. }
  546. };
  547. for ( size_t n = 0; n < WXSIZEOF(datesDST[0]); n++ )
  548. {
  549. const int year = 1990 + n;
  550. wxDateTime dtBegin = wxDateTime::GetBeginDST(year, wxDateTime::USA),
  551. dtEnd = wxDateTime::GetEndDST(year, wxDateTime::USA);
  552. const Date& dBegin = datesDST[0][n];
  553. const Date& dEnd = datesDST[1][n];
  554. CPPUNIT_ASSERT_EQUAL( dBegin.DT().FormatDate(), dtBegin.FormatDate() );
  555. CPPUNIT_ASSERT_EQUAL( dEnd.DT().FormatDate(), dtEnd.FormatDate() );
  556. }
  557. }
  558. // test wxDateTime -> text conversion
  559. void DateTimeTestCase::TestTimeFormat()
  560. {
  561. // some information may be lost during conversion, so store what kind
  562. // of info should we recover after a round trip
  563. enum CompareKind
  564. {
  565. CompareNone, // don't try comparing
  566. CompareBoth, // dates and times should be identical
  567. CompareYear, // don't compare centuries (fails for 2 digit years)
  568. CompareDate, // dates only
  569. CompareTime // time only
  570. };
  571. static const struct
  572. {
  573. CompareKind compareKind;
  574. const char *format;
  575. } formatTestFormats[] =
  576. {
  577. { CompareYear, "---> %c" }, // %c could use 2 digit years
  578. { CompareDate, "Date is %A, %d of %B, in year %Y" },
  579. { CompareYear, "Date is %x, time is %X" }, // %x could use 2 digits
  580. { CompareTime, "Time is %H:%M:%S or %I:%M:%S %p" },
  581. { CompareNone, "The day of year: %j, the week of year: %W" },
  582. { CompareDate, "ISO date without separators: %Y%m%d" },
  583. { CompareBoth, "RFC 2822 string: %Y-%m-%d %H:%M:%S.%l %z" },
  584. };
  585. const long timeZonesOffsets[] =
  586. {
  587. wxDateTime::TimeZone(wxDateTime::Local).GetOffset(),
  588. // Fictitious TimeZone offsets to ensure time zone formating and
  589. // interpretation works
  590. -(3600 + 2*60),
  591. 3*3600 + 30*60
  592. };
  593. static const Date formatTestDates[] =
  594. {
  595. { 29, wxDateTime::May, 1976, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
  596. { 31, wxDateTime::Dec, 1999, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
  597. { 6, wxDateTime::Feb, 1937, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
  598. { 6, wxDateTime::Feb, 1856, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
  599. { 6, wxDateTime::Feb, 1857, 23, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
  600. { 29, wxDateTime::May, 2076, 18, 30, 00, 0.0, wxDateTime::Inv_WeekDay },
  601. // FIXME: the test with 02:15:25 time doesn't pass because of DST
  602. // computation problems, we get back 03:15:25
  603. { 29, wxDateTime::Feb, 2400, 04, 15, 25, 0.0, wxDateTime::Inv_WeekDay },
  604. #if 0
  605. // Need to add support for BCE dates.
  606. { 01, wxDateTime::Jan, -52, 03, 16, 47, 0.0, wxDateTime::Inv_WeekDay },
  607. #endif
  608. };
  609. for ( unsigned idxtz = 0; idxtz < WXSIZEOF(timeZonesOffsets); ++idxtz )
  610. {
  611. wxDateTime::TimeZone tz(timeZonesOffsets[idxtz]);
  612. const bool isLocalTz = tz.GetOffset() == -wxGetTimeZone();
  613. for ( size_t d = 0; d < WXSIZEOF(formatTestDates); d++ )
  614. {
  615. wxDateTime dt = formatTestDates[d].DT();
  616. for ( unsigned n = 0; n < WXSIZEOF(formatTestFormats); n++ )
  617. {
  618. const char *fmt = formatTestFormats[n].format;
  619. // skip the check with %p for those locales which have empty AM/PM strings:
  620. // for those locales it's impossible to pass the test with %p...
  621. wxString am, pm;
  622. wxDateTime::GetAmPmStrings(&am, &pm);
  623. if (am.empty() && pm.empty() && wxStrstr(fmt, "%p") != NULL)
  624. continue;
  625. // what can we recover?
  626. CompareKind kind = formatTestFormats[n].compareKind;
  627. // When using a different time zone we must perform a time zone
  628. // conversion below which doesn't always work correctly, check
  629. // for the cases when it doesn't.
  630. if ( !isLocalTz )
  631. {
  632. // DST computation doesn't work correctly for dates above
  633. // 2038 currently on the systems with 32 bit time_t.
  634. if ( dt.GetYear() >= 2038 )
  635. continue;
  636. // We can't compare just dates nor just times when doing TZ
  637. // conversion as both are affected by the DST: for the
  638. // dates, the DST can switch midnight to 23:00 of the
  639. // previous day while for the times DST can be different
  640. // for the original date and today.
  641. if ( kind == CompareDate || kind == CompareTime )
  642. continue;
  643. }
  644. // do convert date to string
  645. wxString s = dt.Format(fmt, tz);
  646. // convert back
  647. wxDateTime dt2;
  648. const char *result = dt2.ParseFormat(s, fmt);
  649. if ( !result )
  650. {
  651. // conversion failed - should it have?
  652. WX_ASSERT_MESSAGE(
  653. ("Test #%u failed: failed to parse \"%s\"", n, s),
  654. kind == CompareNone
  655. );
  656. }
  657. else // conversion succeeded
  658. {
  659. // currently ParseFormat() doesn't support "%Z" and so is
  660. // incapable of parsing time zone part used at the end of date
  661. // representations in many (but not "C") locales, compensate
  662. // for it ourselves by simply consuming and ignoring it
  663. while ( *result && (*result >= 'A' && *result <= 'Z') )
  664. result++;
  665. WX_ASSERT_MESSAGE(
  666. ("Test #%u failed: \"%s\" was left unparsed in \"%s\"",
  667. n, result, s),
  668. !*result
  669. );
  670. // Without "%z" we can't recover the time zone used in the
  671. // call to Format() so we need to call MakeFromTimezone()
  672. // explicitly.
  673. if ( !strstr(fmt, "%z") && !isLocalTz )
  674. dt2.MakeFromTimezone(tz);
  675. switch ( kind )
  676. {
  677. case CompareYear:
  678. if ( dt2.GetCentury() != dt.GetCentury() )
  679. {
  680. CPPUNIT_ASSERT_EQUAL(dt.GetYear() % 100,
  681. dt2.GetYear() % 100);
  682. dt2.SetYear(dt.GetYear());
  683. }
  684. // fall through and compare everything
  685. case CompareBoth:
  686. CPPUNIT_ASSERT_EQUAL( dt, dt2 );
  687. break;
  688. case CompareDate:
  689. CPPUNIT_ASSERT( dt.IsSameDate(dt2) );
  690. break;
  691. case CompareTime:
  692. CPPUNIT_ASSERT( dt.IsSameTime(dt2) );
  693. break;
  694. case CompareNone:
  695. wxFAIL_MSG( wxT("unexpected") );
  696. break;
  697. }
  698. }
  699. }
  700. }
  701. }
  702. wxDateTime dt;
  703. #if 0
  704. // special case which was known to fail
  705. CPPUNIT_ASSERT( dt.ParseFormat("02/06/1856", "%x") );
  706. CPPUNIT_ASSERT_EQUAL( 1856, dt.GetYear() );
  707. #endif
  708. // also test %l separately
  709. CPPUNIT_ASSERT( dt.ParseFormat("12:23:45.678", "%H:%M:%S.%l") );
  710. CPPUNIT_ASSERT_EQUAL( 678, dt.GetMillisecond() );
  711. // test special case of %l matching 0 milliseconds
  712. CPPUNIT_ASSERT( dt.ParseFormat("12:23:45.000", "%H:%M:%S.%l") );
  713. CPPUNIT_ASSERT_EQUAL( 0, dt.GetMillisecond() );
  714. // test partially specified dates too
  715. wxDateTime dtDef(26, wxDateTime::Sep, 2008);
  716. CPPUNIT_ASSERT( dt.ParseFormat("17", "%d") );
  717. CPPUNIT_ASSERT_EQUAL( 17, dt.GetDay() );
  718. // test some degenerate cases
  719. CPPUNIT_ASSERT( !dt.ParseFormat("", "%z") );
  720. CPPUNIT_ASSERT( !dt.ParseFormat("", "%%") );
  721. // test compilation of some calls which should compile (and not result in
  722. // ambiguity because of char*<->wxCStrData<->wxString conversions)
  723. wxString s("foo");
  724. CPPUNIT_ASSERT( !dt.ParseFormat("foo") );
  725. CPPUNIT_ASSERT( !dt.ParseFormat(wxT("foo")) );
  726. CPPUNIT_ASSERT( !dt.ParseFormat(s) );
  727. dt.ParseFormat(s.c_str()); // Simply test compilation of this one.
  728. CPPUNIT_ASSERT( !dt.ParseFormat("foo", "%c") );
  729. CPPUNIT_ASSERT( !dt.ParseFormat(wxT("foo"), "%c") );
  730. CPPUNIT_ASSERT( !dt.ParseFormat(s, "%c") );
  731. dt.ParseFormat(s.c_str(), "%c");
  732. CPPUNIT_ASSERT( !dt.ParseFormat("foo", wxT("%c")) );
  733. CPPUNIT_ASSERT( !dt.ParseFormat(wxT("foo"), wxT("%c")) );
  734. CPPUNIT_ASSERT( !dt.ParseFormat(s, "%c") );
  735. dt.ParseFormat(s.c_str(), wxT("%c"));
  736. wxString spec("%c");
  737. CPPUNIT_ASSERT( !dt.ParseFormat("foo", spec) );
  738. CPPUNIT_ASSERT( !dt.ParseFormat(wxT("foo"), spec) );
  739. CPPUNIT_ASSERT( !dt.ParseFormat(s, spec) );
  740. dt.ParseFormat(s.c_str(), spec);
  741. }
  742. // Test parsing time in free format.
  743. void DateTimeTestCase::TestTimeParse()
  744. {
  745. wxDateTime dt;
  746. // Parsing standard formats should work.
  747. CPPUNIT_ASSERT( dt.ParseTime("12:34:56") );
  748. CPPUNIT_ASSERT_EQUAL( "12:34:56", dt.FormatISOTime() );
  749. // Parsing just hours should work too.
  750. dt.ResetTime();
  751. CPPUNIT_ASSERT( dt.ParseTime("17") );
  752. CPPUNIT_ASSERT_EQUAL( "17:00:00", dt.FormatISOTime() );
  753. // Parsing gibberish shouldn't work.
  754. CPPUNIT_ASSERT( !dt.ParseTime("bloordyblop") );
  755. }
  756. void DateTimeTestCase::TestTimeSpanFormat()
  757. {
  758. static const struct TimeSpanFormatTestData
  759. {
  760. long h, min, sec, msec;
  761. const char *fmt;
  762. const char *result;
  763. } testSpans[] =
  764. {
  765. { 12, 34, 56, 789, "%H:%M:%S.%l", "12:34:56.789" },
  766. { 1, 2, 3, 0, "%H:%M:%S", "01:02:03" },
  767. { 1, 2, 3, 0, "%S", "3723" },
  768. { -1, -2, -3, 0, "%S", "-3723" },
  769. { -1, -2, -3, 0, "%H:%M:%S", "-01:02:03" },
  770. { 26, 0, 0, 0, "%H", "26" },
  771. { 26, 0, 0, 0, "%D, %H", "1, 02" },
  772. { -26, 0, 0, 0, "%H", "-26" },
  773. { -26, 0, 0, 0, "%D, %H", "-1, 02" },
  774. { 219, 0, 0, 0, "%H", "219" },
  775. { 219, 0, 0, 0, "%D, %H", "9, 03" },
  776. { 219, 0, 0, 0, "%E, %D, %H", "1, 2, 03" },
  777. { 0, -1, 0, 0, "%H:%M:%S", "-00:01:00" },
  778. { 0, 0, -1, 0, "%H:%M:%S", "-00:00:01" },
  779. };
  780. for ( size_t n = 0; n < WXSIZEOF(testSpans); n++ )
  781. {
  782. const TimeSpanFormatTestData& td = testSpans[n];
  783. wxTimeSpan ts(td.h, td.min, td.sec, td.msec);
  784. CPPUNIT_ASSERT_EQUAL( td.result, ts.Format(td.fmt) );
  785. }
  786. }
  787. void DateTimeTestCase::TestTimeTicks()
  788. {
  789. static const wxDateTime::TimeZone TZ_LOCAL(wxDateTime::Local);
  790. static const wxDateTime::TimeZone TZ_TEST(wxDateTime::NZST);
  791. // this offset is needed to make the test work in any time zone when we
  792. // only have expected test results in UTC in testDates
  793. static const long tzOffset = TZ_LOCAL.GetOffset() - TZ_TEST.GetOffset();
  794. for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
  795. {
  796. const Date& d = testDates[n];
  797. if ( d.gmticks == -1 )
  798. continue;
  799. wxDateTime dt = d.DT().MakeTimezone(TZ_TEST, true /* no DST */);
  800. // GetValue() returns internal UTC-based representation, we need to
  801. // convert it to local TZ before comparing
  802. time_t ticks = (dt.GetValue() / 1000).ToLong() + TZ_LOCAL.GetOffset();
  803. if ( dt.IsDST() )
  804. ticks += 3600;
  805. CPPUNIT_ASSERT_EQUAL( d.gmticks, ticks + tzOffset );
  806. dt = d.DT().FromTimezone(wxDateTime::UTC);
  807. ticks = (dt.GetValue() / 1000).ToLong();
  808. CPPUNIT_ASSERT_EQUAL( d.gmticks, ticks );
  809. }
  810. }
  811. // test parsing dates in RFC822 format
  812. void DateTimeTestCase::TestParceRFC822()
  813. {
  814. static const struct ParseTestData
  815. {
  816. const char *rfc822;
  817. Date date; // NB: this should be in UTC
  818. bool good;
  819. } parseTestDates[] =
  820. {
  821. {
  822. "Sat, 18 Dec 1999 00:46:40 +0100",
  823. { 17, wxDateTime::Dec, 1999, 23, 46, 40 },
  824. true
  825. },
  826. {
  827. "Wed, 1 Dec 1999 05:17:20 +0300",
  828. { 1, wxDateTime::Dec, 1999, 2, 17, 20 },
  829. true
  830. },
  831. {
  832. "Sun, 28 Aug 2005 03:31:30 +0200",
  833. { 28, wxDateTime::Aug, 2005, 1, 31, 30 },
  834. true
  835. },
  836. {
  837. "Sat, 18 Dec 1999 10:48:30 -0500",
  838. { 18, wxDateTime::Dec, 1999, 15, 48, 30 },
  839. true
  840. },
  841. // seconds are optional according to the RFC
  842. {
  843. "Sun, 01 Jun 2008 16:30 +0200",
  844. { 1, wxDateTime::Jun, 2008, 14, 30, 00 },
  845. true
  846. },
  847. // try some bogus ones too
  848. {
  849. "Sun, 01 Jun 2008 16:30: +0200",
  850. { 0 },
  851. false
  852. },
  853. };
  854. for ( unsigned n = 0; n < WXSIZEOF(parseTestDates); n++ )
  855. {
  856. const char * const datestr = parseTestDates[n].rfc822;
  857. wxDateTime dt;
  858. if ( dt.ParseRfc822Date(datestr) )
  859. {
  860. WX_ASSERT_MESSAGE(
  861. ("Erroneously parsed \"%s\"", datestr),
  862. parseTestDates[n].good
  863. );
  864. wxDateTime dtReal = parseTestDates[n].date.DT().FromUTC();
  865. CPPUNIT_ASSERT_EQUAL( dtReal, dt );
  866. }
  867. else // failed to parse
  868. {
  869. WX_ASSERT_MESSAGE(
  870. ("Failed to parse \"%s\"", datestr),
  871. !parseTestDates[n].good
  872. );
  873. }
  874. }
  875. }
  876. // test parsing dates in free format
  877. void DateTimeTestCase::TestDateParse()
  878. {
  879. static const struct ParseTestData
  880. {
  881. const char *str;
  882. Date date; // NB: this should be in UTC
  883. bool good;
  884. } parseTestDates[] =
  885. {
  886. { "21 Mar 2006", { 21, wxDateTime::Mar, 2006 }, true },
  887. { "29 Feb 1976", { 29, wxDateTime::Feb, 1976 }, true },
  888. { "Feb 29 1976", { 29, wxDateTime::Feb, 1976 }, true },
  889. { "31/03/06", { 31, wxDateTime::Mar, 6 }, true },
  890. { "31/03/2006", { 31, wxDateTime::Mar, 2006 }, true },
  891. // some invalid ones too
  892. { "29 Feb 2006" },
  893. { "31/04/06" },
  894. { "bloordyblop" },
  895. { "2 . . " },
  896. };
  897. // special cases
  898. wxDateTime dt;
  899. CPPUNIT_ASSERT( dt.ParseDate(wxT("today")) );
  900. CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), dt );
  901. for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
  902. {
  903. const wxString datestr = TranslateDate(parseTestDates[n].str);
  904. const char * const end = dt.ParseDate(datestr);
  905. if ( end && !*end )
  906. {
  907. WX_ASSERT_MESSAGE(
  908. ("Erroneously parsed \"%s\"", datestr),
  909. parseTestDates[n].good
  910. );
  911. CPPUNIT_ASSERT_EQUAL( parseTestDates[n].date.DT(), dt );
  912. }
  913. else // failed to parse
  914. {
  915. WX_ASSERT_MESSAGE(
  916. ("Failed to parse \"%s\"", datestr),
  917. !parseTestDates[n].good
  918. );
  919. }
  920. }
  921. // Check that incomplete parse works correctly.
  922. const char* p = dt.ParseFormat("2012-03-23 12:34:56", "%Y-%m-%d");
  923. CPPUNIT_ASSERT_EQUAL( " 12:34:56", wxString(p) );
  924. }
  925. void DateTimeTestCase::TestDateParseISO()
  926. {
  927. static const struct
  928. {
  929. const char *str;
  930. Date date; // NB: this should be in UTC
  931. bool good;
  932. } parseTestDates[] =
  933. {
  934. { "2006-03-21", { 21, wxDateTime::Mar, 2006 }, true },
  935. { "1976-02-29", { 29, wxDateTime::Feb, 1976 }, true },
  936. { "0006-03-31", { 31, wxDateTime::Mar, 6 }, true },
  937. // some invalid ones too
  938. { "2006:03:31" },
  939. { "31/04/06" },
  940. { "bloordyblop" },
  941. { "" },
  942. };
  943. static const struct
  944. {
  945. const char *str;
  946. wxDateTime::wxDateTime_t hour, min, sec;
  947. bool good;
  948. } parseTestTimes[] =
  949. {
  950. { "13:42:17", 13, 42, 17, true },
  951. { "02:17:01", 2, 17, 1, true },
  952. // some invalid ones too
  953. { "66:03:31" },
  954. { "31/04/06" },
  955. { "bloordyblop" },
  956. { "" },
  957. };
  958. for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
  959. {
  960. wxDateTime dt;
  961. if ( dt.ParseISODate(parseTestDates[n].str) )
  962. {
  963. CPPUNIT_ASSERT( parseTestDates[n].good );
  964. CPPUNIT_ASSERT_EQUAL( parseTestDates[n].date.DT(), dt );
  965. for ( size_t m = 0; m < WXSIZEOF(parseTestTimes); m++ )
  966. {
  967. wxString dtCombined;
  968. dtCombined << parseTestDates[n].str
  969. << 'T'
  970. << parseTestTimes[m].str;
  971. if ( dt.ParseISOCombined(dtCombined) )
  972. {
  973. CPPUNIT_ASSERT( parseTestTimes[m].good );
  974. CPPUNIT_ASSERT_EQUAL( parseTestTimes[m].hour, dt.GetHour()) ;
  975. CPPUNIT_ASSERT_EQUAL( parseTestTimes[m].min, dt.GetMinute()) ;
  976. CPPUNIT_ASSERT_EQUAL( parseTestTimes[m].sec, dt.GetSecond()) ;
  977. }
  978. else // failed to parse combined date/time
  979. {
  980. CPPUNIT_ASSERT( !parseTestTimes[m].good );
  981. }
  982. }
  983. }
  984. else // failed to parse
  985. {
  986. CPPUNIT_ASSERT( !parseTestDates[n].good );
  987. }
  988. }
  989. }
  990. void DateTimeTestCase::TestDateTimeParse()
  991. {
  992. static const struct ParseTestData
  993. {
  994. const char *str;
  995. Date date; // NB: this should be in UTC
  996. bool good;
  997. } parseTestDates[] =
  998. {
  999. {
  1000. "Thu 22 Nov 2007 07:40:00 PM",
  1001. { 22, wxDateTime::Nov, 2007, 19, 40, 0 },
  1002. true
  1003. },
  1004. {
  1005. "2010-01-04 14:30",
  1006. { 4, wxDateTime::Jan, 2010, 14, 30, 0 },
  1007. true
  1008. },
  1009. {
  1010. "bloordyblop",
  1011. { 1, wxDateTime::Jan, 9999, 0, 0, 0},
  1012. false
  1013. },
  1014. {
  1015. "2012-01-01 10:12:05 +0100",
  1016. { 1, wxDateTime::Jan, 2012, 10, 12, 5, -1 },
  1017. false // ParseDateTime does know yet +0100
  1018. },
  1019. };
  1020. // the test strings here use "PM" which is not available in all locales so
  1021. // we need to use "C" locale for them
  1022. CLocaleSetter cloc;
  1023. wxDateTime dt;
  1024. for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
  1025. {
  1026. const wxString datestr = TranslateDate(parseTestDates[n].str);
  1027. const char * const end = dt.ParseDateTime(datestr);
  1028. if ( end && !*end )
  1029. {
  1030. WX_ASSERT_MESSAGE(
  1031. ("Erroneously parsed \"%s\"", datestr),
  1032. parseTestDates[n].good
  1033. );
  1034. CPPUNIT_ASSERT_EQUAL( parseTestDates[n].date.DT(), dt );
  1035. }
  1036. else // failed to parse
  1037. {
  1038. WX_ASSERT_MESSAGE(
  1039. ("Failed to parse \"%s\"", datestr),
  1040. !parseTestDates[n].good
  1041. );
  1042. CPPUNIT_ASSERT( !parseTestDates[n].good );
  1043. }
  1044. }
  1045. }
  1046. void DateTimeTestCase::TestDateWeekFormat()
  1047. {
  1048. static const struct DateWeekTestData
  1049. {
  1050. int y, m, d;
  1051. const char* result; // expected output of "%G%V"
  1052. } testWeeks[] =
  1053. {
  1054. // Some manual test cases.
  1055. { 2014, 1, 1, "2014-01" },
  1056. { 2014, 1, 2, "2014-01" },
  1057. { 2014, 1, 3, "2014-01" },
  1058. { 2014, 1, 4, "2014-01" },
  1059. { 2014, 1, 5, "2014-01" },
  1060. { 2014, 1, 6, "2014-02" },
  1061. { 2014, 1, 7, "2014-02" },
  1062. { 2014, 12, 24, "2014-52" },
  1063. { 2014, 12, 25, "2014-52" },
  1064. { 2014, 12, 26, "2014-52" },
  1065. { 2014, 12, 27, "2014-52" },
  1066. { 2014, 12, 28, "2014-52" },
  1067. { 2014, 12, 29, "2015-01" },
  1068. { 2014, 12, 30, "2015-01" },
  1069. { 2014, 12, 31, "2015-01" },
  1070. { 2015, 12, 24, "2015-52" },
  1071. { 2015, 12, 25, "2015-52" },
  1072. { 2015, 12, 26, "2015-52" },
  1073. { 2015, 12, 27, "2015-52" },
  1074. { 2015, 12, 28, "2015-53" },
  1075. { 2015, 12, 29, "2015-53" },
  1076. { 2015, 12, 30, "2015-53" },
  1077. { 2015, 12, 31, "2015-53" },
  1078. { 2016, 1, 1, "2015-53" },
  1079. { 2016, 1, 2, "2015-53" },
  1080. { 2016, 1, 3, "2015-53" },
  1081. { 2016, 1, 4, "2016-01" },
  1082. { 2016, 1, 5, "2016-01" },
  1083. { 2016, 1, 6, "2016-01" },
  1084. { 2016, 1, 7, "2016-01" },
  1085. // The rest of the tests was generated using the following zsh command:
  1086. //
  1087. // for n in {0..19}; date --date $((RANDOM%100 + 2000))-$((RANDOM%12+1))-$((RANDOM%31+1)) +'{ %Y, %_m, %_d, "%G-%V" },'
  1088. //
  1089. // (ignore invalid dates if any are randomly created).
  1090. { 2017, 11, 28, "2017-48" },
  1091. { 2086, 9, 6, "2086-36" },
  1092. { 2060, 11, 11, "2060-46" },
  1093. { 2009, 5, 10, "2009-19" },
  1094. { 2032, 12, 8, "2032-50" },
  1095. { 2025, 4, 7, "2025-15" },
  1096. { 2080, 5, 20, "2080-21" },
  1097. { 2077, 7, 19, "2077-29" },
  1098. { 2084, 12, 17, "2084-50" },
  1099. { 2071, 4, 13, "2071-16" },
  1100. { 2006, 1, 3, "2006-01" },
  1101. { 2053, 8, 1, "2053-31" },
  1102. { 2097, 8, 14, "2097-33" },
  1103. { 2067, 1, 3, "2067-01" },
  1104. { 2039, 9, 27, "2039-39" },
  1105. { 2095, 2, 10, "2095-06" },
  1106. { 2004, 7, 7, "2004-28" },
  1107. { 2049, 12, 27, "2049-52" },
  1108. { 2071, 8, 19, "2071-34" },
  1109. { 2010, 11, 30, "2010-48" },
  1110. };
  1111. for ( size_t n = 0; n < WXSIZEOF(testWeeks); n++ )
  1112. {
  1113. const DateWeekTestData& td = testWeeks[n];
  1114. wxDateTime d(td.d, wxDateTime::Month(td.m - 1), td.y);
  1115. CPPUNIT_ASSERT_EQUAL( td.result, d.Format("%G-%V") );
  1116. if ( td.y > 2000 )
  1117. CPPUNIT_ASSERT_EQUAL( td.result + 2, d.Format("%g-%V") );
  1118. }
  1119. }
  1120. void DateTimeTestCase::TestTimeArithmetics()
  1121. {
  1122. static const wxDateSpan testArithmData[] =
  1123. {
  1124. wxDateSpan::Day(),
  1125. wxDateSpan::Week(),
  1126. wxDateSpan::Month(),
  1127. wxDateSpan::Year(),
  1128. };
  1129. // the test will *not* work with arbitrary date!
  1130. wxDateTime dt(2, wxDateTime::Dec, 1999),
  1131. dt1,
  1132. dt2;
  1133. for ( size_t n = 0; n < WXSIZEOF(testArithmData); n++ )
  1134. {
  1135. const wxDateSpan& span = testArithmData[n];
  1136. dt1 = dt + span;
  1137. dt2 = dt - span;
  1138. CPPUNIT_ASSERT_EQUAL( dt, dt1 - span );
  1139. CPPUNIT_ASSERT_EQUAL( dt, dt2 + span );
  1140. CPPUNIT_ASSERT_EQUAL( dt1, dt2 + 2*span );
  1141. CPPUNIT_ASSERT_EQUAL( span, dt1.DiffAsDateSpan(dt) );
  1142. }
  1143. // More date span arithmetics tests
  1144. wxDateTime dtd1(5, wxDateTime::Jun, 1998);
  1145. wxDateTime dtd2(6, wxDateTime::Aug, 1999);
  1146. // All parts in dtd2 is after dtd1
  1147. CPPUNIT_ASSERT_EQUAL( wxDateSpan(1, 2, 0, 1), dtd2.DiffAsDateSpan(dtd1) );
  1148. // Year and month after, day earlier, so no full month
  1149. // Jul has 31 days, so it's 31 - 5 + 4 = 30, or 4w 2d
  1150. dtd2.Set(4, wxDateTime::Aug, 1999);
  1151. CPPUNIT_ASSERT_EQUAL( wxDateSpan(1, 1, 4, 2), dtd2.DiffAsDateSpan(dtd1) );
  1152. // Year and day after, month earlier, so no full year, but same day diff as
  1153. // first example
  1154. dtd2.Set(6, wxDateTime::May, 1999);
  1155. CPPUNIT_ASSERT_EQUAL( wxDateSpan(0, 11, 0, 1), dtd2.DiffAsDateSpan(dtd1) );
  1156. // Year after, month and day earlier, so no full month and no full year
  1157. // April has 30 days, so it's 30 - 5 + 4 = 29, or 4w 1d
  1158. dtd2.Set(4, wxDateTime::May, 1999);
  1159. CPPUNIT_ASSERT_EQUAL( wxDateSpan(0, 10, 4, 1), dtd2.DiffAsDateSpan(dtd1) );
  1160. // And a reverse. Now we should use days in Jun (again 30 => 4w 1d)
  1161. CPPUNIT_ASSERT_EQUAL( wxDateSpan(0, -10, -4, -1), dtd1.DiffAsDateSpan(dtd2) );
  1162. }
  1163. void DateTimeTestCase::TestDSTBug()
  1164. {
  1165. /////////////////////////
  1166. // Test GetEndDST()
  1167. wxDateTime dt = wxDateTime::GetEndDST(2004, wxDateTime::France);
  1168. CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
  1169. CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
  1170. CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
  1171. CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
  1172. CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
  1173. CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
  1174. CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
  1175. /////////////////////////
  1176. // Test ResetTime()
  1177. dt.SetHour(5);
  1178. CPPUNIT_ASSERT_EQUAL(5, (int)dt.GetHour());
  1179. dt.ResetTime();
  1180. CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
  1181. CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
  1182. CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
  1183. CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetHour());
  1184. CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
  1185. CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
  1186. CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
  1187. dt.Set(1, 0, 0, 0);
  1188. CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
  1189. /////////////////////////
  1190. // Test Today()
  1191. #ifdef CHANGE_SYSTEM_DATE
  1192. {
  1193. DateChanger change(2004, 10, 31, 5, 0, 0);
  1194. dt = wxDateTime::Today();
  1195. }
  1196. CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
  1197. CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
  1198. CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
  1199. CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetHour());
  1200. CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
  1201. CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
  1202. CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
  1203. /////////////////////////
  1204. // Test Set(hour, minute, second, milli)
  1205. wxDateTime dt2;
  1206. {
  1207. DateChanger change(2004, 10, 31, 5, 0, 0);
  1208. dt.Set(1, 30, 0, 0);
  1209. dt2.Set(5, 30, 0, 0);
  1210. }
  1211. CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
  1212. CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
  1213. CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
  1214. CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
  1215. CPPUNIT_ASSERT_EQUAL(30, (int)dt.GetMinute());
  1216. CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
  1217. CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
  1218. CPPUNIT_ASSERT_EQUAL(31, (int)dt2.GetDay());
  1219. CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt2.GetMonth());
  1220. CPPUNIT_ASSERT_EQUAL(2004, (int)dt2.GetYear());
  1221. CPPUNIT_ASSERT_EQUAL(5, (int)dt2.GetHour());
  1222. CPPUNIT_ASSERT_EQUAL(30, (int)dt2.GetMinute());
  1223. CPPUNIT_ASSERT_EQUAL(0, (int)dt2.GetSecond());
  1224. CPPUNIT_ASSERT_EQUAL(0, (int)dt2.GetMillisecond());
  1225. #endif // CHANGE_SYSTEM_DATE
  1226. // Verify that setting the date to the beginning of the DST period moves it
  1227. // forward (as this date on its own would be invalid). The problem here is
  1228. // that our GetBeginDST() is far from being trustworthy, so just try a
  1229. // couple of dates for the common time zones and check that all of them are
  1230. // either unchanged or moved forward.
  1231. wxDateTime dtDST(10, wxDateTime::Mar, 2013, 2, 0, 0);
  1232. if ( dtDST.GetHour() != 2 )
  1233. CPPUNIT_ASSERT_EQUAL( 3, dtDST.GetHour() );
  1234. dtDST = wxDateTime(31, wxDateTime::Mar, 2013, 2, 0, 0);
  1235. if ( dtDST.GetHour() != 2 )
  1236. CPPUNIT_ASSERT_EQUAL( 3, dtDST.GetHour() );
  1237. }
  1238. void DateTimeTestCase::TestDateOnly()
  1239. {
  1240. wxDateTime dt(19, wxDateTime::Jan, 2007, 15, 01, 00);
  1241. static const wxDateTime::wxDateTime_t DATE_ZERO = 0;
  1242. CPPUNIT_ASSERT_EQUAL( DATE_ZERO, dt.GetDateOnly().GetHour() );
  1243. CPPUNIT_ASSERT_EQUAL( DATE_ZERO, dt.GetDateOnly().GetMinute() );
  1244. CPPUNIT_ASSERT_EQUAL( DATE_ZERO, dt.GetDateOnly().GetSecond() );
  1245. CPPUNIT_ASSERT_EQUAL( DATE_ZERO, dt.GetDateOnly().GetMillisecond() );
  1246. dt.ResetTime();
  1247. CPPUNIT_ASSERT_EQUAL( wxDateTime(19, wxDateTime::Jan, 2007), dt );
  1248. CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), wxDateTime::Now().GetDateOnly() );
  1249. }
  1250. #endif // wxUSE_DATETIME