datetimetest.cpp 50 KB

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