2
0

DateObjectTest.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Date
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../TestHelper.php';
  26. /**
  27. * Zend_Date
  28. */
  29. require_once 'Zend/Date.php';
  30. /**
  31. * @package Zend_Date
  32. * @subpackage UnitTests
  33. */
  34. class Zend_Date_DateObjectTest extends PHPUnit_Framework_TestCase
  35. {
  36. public function setUp()
  37. {
  38. date_default_timezone_set('Europe/Paris');
  39. require_once 'Zend/Cache.php';
  40. $cache = Zend_Cache::factory('Core', 'File',
  41. array('lifetime' => 120, 'automatic_serialization' => true),
  42. array('cache_dir' => dirname(__FILE__) . '/../_files/'));
  43. Zend_Date_DateObjectTestHelper::setOptions(array('cache' => $cache));
  44. }
  45. /**
  46. * Test for date object creation null value
  47. */
  48. public function testCreationNull()
  49. {
  50. // look if locale is detectable
  51. try {
  52. $locale = new Zend_Locale();
  53. } catch (Zend_Locale_Exception $e) {
  54. $this->markTestSkipped('Autodetection of locale failed');
  55. return;
  56. }
  57. $date = new Zend_Date(0);
  58. $this->assertTrue($date instanceof Zend_Date);
  59. }
  60. /**
  61. * Test for date object creation negative timestamp
  62. */
  63. public function testCreationNegative()
  64. {
  65. // look if locale is detectable
  66. try {
  67. $locale = new Zend_Locale();
  68. } catch (Zend_Locale_Exception $e) {
  69. $this->markTestSkipped('Autodetection of locale failed');
  70. return;
  71. }
  72. $date = new Zend_Date(1000);
  73. $this->assertTrue($date instanceof Zend_Date);
  74. }
  75. /**
  76. * Test for date object creation text given
  77. */
  78. public function testCreationFailed()
  79. {
  80. // look if locale is detectable
  81. try {
  82. $locale = new Zend_Locale();
  83. } catch (Zend_Locale_Exception $e) {
  84. $this->markTestSkipped('Autodetection of locale failed');
  85. return;
  86. }
  87. try {
  88. $date = new Zend_Date("notimestamp");
  89. $this->fail("exception expected");
  90. } catch (Zend_Date_Exception $e) {
  91. // success
  92. }
  93. }
  94. /**
  95. * Test for setUnixTimestamp
  96. */
  97. public function testsetUnixTimestamp()
  98. {
  99. $date = new Zend_Date_DateObjectTestHelper(Zend_Date::now());
  100. $diff = abs(time() - $date->getUnixTimestamp());
  101. $this->assertTrue(($diff < 2), "Zend_Date->setUnixTimestamp() returned a significantly "
  102. . "different timestamp than expected: $diff seconds");
  103. $date->setUnixTimestamp(0);
  104. $this->assertSame('0', (string)$date->setUnixTimestamp("12345678901234567890"));
  105. $this->assertSame("12345678901234567890", (string)$date->setUnixTimestamp("12345678901234567890"));
  106. $date->setUnixTimestamp();
  107. $diff = abs(time() - $date->getUnixTimestamp());
  108. $this->assertTrue($diff < 2, "setUnixTimestamp has a significantly different time than returned by time()): $diff seconds");
  109. }
  110. /**
  111. * Test for setUnixTimestampFailed
  112. */
  113. public function testsetUnixTimestampFailed()
  114. {
  115. try {
  116. $date = new Zend_Date_DateObjectTestHelper(Zend_Date::now());
  117. $date->setUnixTimestamp("notimestamp");
  118. $this->fail("exception expected");
  119. } catch (Zend_Date_Exception $e) {
  120. // success
  121. }
  122. }
  123. /**
  124. * Test for getUnixTimestamp
  125. */
  126. public function testgetUnixTimestamp()
  127. {
  128. $date = new Zend_Date_DateObjectTestHelper(Zend_Date::now());
  129. $result = $date->getUnixTimestamp();
  130. $diff = abs($result - time());
  131. $this->assertTrue($diff < 2, "Instance of Zend_Date_DateObject has a significantly different time than returned by setTime(): $diff seconds");
  132. }
  133. /**
  134. * Test for mktime
  135. */
  136. public function testMkTimeforDateValuesInPHPRange()
  137. {
  138. $date = new Zend_Date_DateObjectTestHelper(Zend_Date::now());
  139. $this->assertSame( mktime(0, 0, 0, 12, 30, 2037), $date->mktime(0, 0, 0, 12, 30, 2037, false));
  140. $this->assertSame(gmmktime(0, 0, 0, 12, 30, 2037), $date->mktime(0, 0, 0, 12, 30, 2037, true ));
  141. $this->assertSame( mktime(0, 0, 0, 1, 1, 2000), $date->mktime(0, 0, 0, 1, 1, 2000, false));
  142. $this->assertSame(gmmktime(0, 0, 0, 1, 1, 2000), $date->mktime(0, 0, 0, 1, 1, 2000, true ));
  143. $this->assertSame( mktime(0, 0, 0, 1, 1, 1970), $date->mktime(0, 0, 0, 1, 1, 1970, false));
  144. $this->assertSame(gmmktime(0, 0, 0, 1, 1, 1970), $date->mktime(0, 0, 0, 1, 1, 1970, true ));
  145. $this->assertSame( mktime(0, 0, 0, 12, 30, 1902), $date->mktime(0, 0, 0, 12, 30, 1902, false));
  146. $this->assertSame(gmmktime(0, 0, 0, 12, 30, 1902), $date->mktime(0, 0, 0, 12, 30, 1902, true ));
  147. }
  148. /**
  149. * Test for mktime
  150. */
  151. public function testMkTimeforDateValuesGreaterPHPRange()
  152. {
  153. $date = new Zend_Date_DateObjectTestHelper(Zend_Date::now());
  154. $this->assertSame(2232658800, $date->mktime(0, 0, 0,10, 1, 2040, false));
  155. $this->assertSame(2232662400, $date->mktime(0, 0, 0,10, 1, 2040, true ));
  156. $this->assertSame(7258114800, $date->mktime(0, 0, 0, 1, 1, 2200, false));
  157. $this->assertSame(7258118400, $date->mktime(0, 0, 0, 1, 1, 2200, true ));
  158. $this->assertSame(16749586800, $date->mktime(0, 0, 0,10,10, 2500, false));
  159. $this->assertSame(16749590400, $date->mktime(0, 0, 0,10,10, 2500, true ));
  160. $this->assertSame(32503676400, $date->mktime(0, 0, 0, 1, 1, 3000, false));
  161. $this->assertSame(32503680000, $date->mktime(0, 0, 0, 1, 1, 3000, true ));
  162. $this->assertSame(95617580400, $date->mktime(0, 0, 0, 1, 1, 5000, false));
  163. $this->assertSame(95617584000, $date->mktime(0, 0, 0, 1, 1, 5000, true ));
  164. // test for different set external timezone
  165. // the internal timezone should always be used for calculation
  166. $date->setTimezone('Europe/Paris');
  167. $this->assertSame(1577833200, $date->mktime(0, 0, 0, 1, 1, 2020, false));
  168. $this->assertSame(1577836800, $date->mktime(0, 0, 0, 1, 1, 2020, true ));
  169. date_default_timezone_set('Indian/Maldives');
  170. $this->assertSame(1577833200, $date->mktime(0, 0, 0, 1, 1, 2020, false));
  171. $this->assertSame(1577836800, $date->mktime(0, 0, 0, 1, 1, 2020, true ));
  172. }
  173. /**
  174. * Test for mktime
  175. */
  176. public function testMkTimeforDateValuesSmallerPHPRange()
  177. {
  178. $date = new Zend_Date_DateObjectTestHelper(Zend_Date::now());
  179. $this->assertSame(-2208992400, $date->mktime(0, 0, 0, 1, 1, 1900, false));
  180. $this->assertSame(-2208988800, $date->mktime(0, 0, 0, 1, 1, 1900, true ));
  181. $this->assertSame(-8520339600, $date->mktime(0, 0, 0, 1, 1, 1700, false));
  182. $this->assertSame(-8520336000, $date->mktime(0, 0, 0, 1, 1, 1700, true ));
  183. $this->assertSame(-14830995600, $date->mktime(0, 0, 0, 1, 1, 1500, false));
  184. $this->assertSame(-14830992000, $date->mktime(0, 0, 0, 1, 1, 1500, true ));
  185. $this->assertSame(-12219321600, $date->mktime(0, 0, 0,10,10, 1582, false));
  186. $this->assertSame(-12219321600, $date->mktime(0, 0, 0,10,10, 1582, true ));
  187. $this->assertSame(-30609795600, $date->mktime(0, 0, 0, 1, 1, 1000, false));
  188. $this->assertSame(-30609792000, $date->mktime(0, 0, 0, 1, 1, 1000, true ));
  189. $this->assertSame(-62167395600, $date->mktime(0, 0, 0, 1, 1, 0, false));
  190. $this->assertSame(-62167392000, $date->mktime(0, 0, 0, 1, 1, 0, true ));
  191. $this->assertSame(-125282595600, $date->mktime(0, 0, 0, 1, 1,-2000, false));
  192. $this->assertSame(-125282592000, $date->mktime(0, 0, 0, 1, 1,-2000, true));
  193. $this->assertSame(-2208992400, $date->mktime(0, 0, 0, 13, 1, 1899, false));
  194. $this->assertSame(-2208988800, $date->mktime(0, 0, 0, 13, 1, 1899, true));
  195. $this->assertSame(-2208992400, $date->mktime(0, 0, 0,-11, 1, 1901, false));
  196. $this->assertSame(-2208988800, $date->mktime(0, 0, 0,-11, 1, 1901, true));
  197. }
  198. public function testIsLeapYear()
  199. {
  200. $date = new Zend_Date_DateObjectTestHelper(Zend_Date::now());
  201. $this->assertTrue ($date->checkLeapYear(2000));
  202. $this->assertFalse($date->checkLeapYear(2002));
  203. $this->assertTrue ($date->checkLeapYear(2004));
  204. $this->assertFalse($date->checkLeapYear(1899));
  205. $this->assertTrue ($date->checkLeapYear(1500));
  206. $this->assertFalse($date->checkLeapYear(1455));
  207. }
  208. public function testWeekNumber()
  209. {
  210. $date = new Zend_Date_DateObjectTestHelper(Zend_Date::now());
  211. $this->assertSame((int) date('W',mktime(0, 0, 0, 1, 1, 2000)), $date->weekNumber(2000, 1, 1));
  212. $this->assertSame((int) date('W',mktime(0, 0, 0, 10, 1, 2020)), $date->weekNumber(2020, 10, 1));
  213. $this->assertSame((int) date('W',mktime(0, 0, 0, 5, 15, 2005)), $date->weekNumber(2005, 5, 15));
  214. $this->assertSame((int) date('W',mktime(0, 0, 0, 11, 22, 1994)), $date->weekNumber(1994, 11, 22));
  215. $this->assertSame((int) date('W',mktime(0, 0, 0, 12, 31, 2000)), $date->weekNumber(2000, 12, 31));
  216. $this->assertSame(52, $date->weekNumber(2050, 12, 31));
  217. $this->assertSame(23, $date->weekNumber(2050, 6, 6));
  218. $this->assertSame(52, $date->weekNumber(2056, 1, 1));
  219. $this->assertSame(52, $date->weekNumber(2049, 12, 31));
  220. $this->assertSame(53, $date->weekNumber(2048, 12, 31));
  221. $this->assertSame( 1, $date->weekNumber(2047, 12, 31));
  222. }
  223. public function testDayOfWeek()
  224. {
  225. $date = new Zend_Date_DateObjectTestHelper(Zend_Date::now());
  226. $this->assertSame((int) date('w',mktime(0, 0, 0, 1, 1, 2000)), $date->dayOfWeekHelper(2000, 1, 1));
  227. $this->assertSame((int) date('w',mktime(0, 0, 0, 1, 2, 2000)), $date->dayOfWeekHelper(2000, 1, 2));
  228. $this->assertSame((int) date('w',mktime(0, 0, 0, 1, 3, 2000)), $date->dayOfWeekHelper(2000, 1, 3));
  229. $this->assertSame((int) date('w',mktime(0, 0, 0, 1, 4, 2000)), $date->dayOfWeekHelper(2000, 1, 4));
  230. $this->assertSame((int) date('w',mktime(0, 0, 0, 1, 5, 2000)), $date->dayOfWeekHelper(2000, 1, 5));
  231. $this->assertSame((int) date('w',mktime(0, 0, 0, 1, 6, 2000)), $date->dayOfWeekHelper(2000, 1, 6));
  232. $this->assertSame((int) date('w',mktime(0, 0, 0, 1, 7, 2000)), $date->dayOfWeekHelper(2000, 1, 7));
  233. $this->assertSame((int) date('w',mktime(0, 0, 0, 1, 8, 2000)), $date->dayOfWeekHelper(2000, 1, 8));
  234. $this->assertSame(6, $date->dayOfWeekHelper(2050, 1, 1));
  235. $this->assertSame(0, $date->dayOfWeekHelper(2050, 1, 2));
  236. $this->assertSame(1, $date->dayOfWeekHelper(2050, 1, 3));
  237. $this->assertSame(2, $date->dayOfWeekHelper(2050, 1, 4));
  238. $this->assertSame(3, $date->dayOfWeekHelper(2050, 1, 5));
  239. $this->assertSame(4, $date->dayOfWeekHelper(2050, 1, 6));
  240. $this->assertSame(5, $date->dayOfWeekHelper(2050, 1, 7));
  241. $this->assertSame(6, $date->dayOfWeekHelper(2050, 1, 8));
  242. $this->assertSame(4, $date->dayOfWeekHelper(1500, 1, 1));
  243. }
  244. public function testCalcSunInternal()
  245. {
  246. $date = new Zend_Date_DateObjectTestHelper(10000000);
  247. $this->assertSame( 9961681, $date->calcSun(array('latitude' => 38.4, 'longitude' => -29), -0.0145439, true ));
  248. $this->assertSame(10010367, $date->calcSun(array('latitude' => 38.4, 'longitude' => -29), -0.0145439, false));
  249. $this->assertSame( 9967006, $date->calcSun(array('latitude' => -38.4, 'longitude' => -29), -0.0145439, true ));
  250. $this->assertSame(10005042, $date->calcSun(array('latitude' => -38.4, 'longitude' => -29), -0.0145439, false));
  251. $this->assertSame( 9947773, $date->calcSun(array('latitude' => 38.4, 'longitude' => 29), -0.0145439, true ));
  252. $this->assertSame( 9996438, $date->calcSun(array('latitude' => 38.4, 'longitude' => 29), -0.0145439, false));
  253. $this->assertSame( 9953077, $date->calcSun(array('latitude' => -38.4, 'longitude' => 29), -0.0145439, true ));
  254. $this->assertSame( 9991134, $date->calcSun(array('latitude' => -38.4, 'longitude' => 29), -0.0145439, false));
  255. $this->assertSame( 9923795, $date->calcSun(array('latitude' => 38.4, 'longitude' => 129), -0.0145439, true ));
  256. $this->assertSame( 9972422, $date->calcSun(array('latitude' => 38.4, 'longitude' => 129), -0.0145439, false));
  257. $this->assertSame( 9929062, $date->calcSun(array('latitude' => -38.4, 'longitude' => 129), -0.0145439, true ));
  258. $this->assertSame( 9967155, $date->calcSun(array('latitude' => -38.4, 'longitude' => 129), -0.0145439, false));
  259. $this->assertSame( 9985660, $date->calcSun(array('latitude' => 38.4, 'longitude' =>-129), -0.0145439, true ));
  260. $this->assertSame(10034383, $date->calcSun(array('latitude' => 38.4, 'longitude' =>-129), -0.0145439, false));
  261. $this->assertSame( 9991022, $date->calcSun(array('latitude' => -38.4, 'longitude' =>-129), -0.0145439, true ));
  262. $this->assertSame(10029021, $date->calcSun(array('latitude' => -38.4, 'longitude' =>-129), -0.0145439, false));
  263. $date = new Zend_Date_DateObjectTestHelper(-148309884);
  264. $this->assertSame(-148322663, $date->calcSun(array('latitude' => 38.4, 'longitude' => -29), -0.0145439, true ));
  265. $this->assertSame(-148274758, $date->calcSun(array('latitude' => 38.4, 'longitude' => -29), -0.0145439, false));
  266. $this->assertSame(-148318117, $date->calcSun(array('latitude' => -38.4, 'longitude' => -29), -0.0145439, true ));
  267. $this->assertSame(-148279304, $date->calcSun(array('latitude' => -38.4, 'longitude' => -29), -0.0145439, false));
  268. $this->assertSame(-148336570, $date->calcSun(array('latitude' => 38.4, 'longitude' => 29), -0.0145439, true ));
  269. $this->assertSame(-148288687, $date->calcSun(array('latitude' => 38.4, 'longitude' => 29), -0.0145439, false));
  270. $this->assertSame(-148332046, $date->calcSun(array('latitude' => -38.4, 'longitude' => 29), -0.0145439, true ));
  271. $this->assertSame(-148293211, $date->calcSun(array('latitude' => -38.4, 'longitude' => 29), -0.0145439, false));
  272. $this->assertSame(-148360548, $date->calcSun(array('latitude' => 38.4, 'longitude' => 129), -0.0145439, true ));
  273. $this->assertSame(-148312703, $date->calcSun(array('latitude' => 38.4, 'longitude' => 129), -0.0145439, false));
  274. $this->assertSame(-148356061, $date->calcSun(array('latitude' => -38.4, 'longitude' => 129), -0.0145439, true ));
  275. $this->assertSame(-148317189, $date->calcSun(array('latitude' => -38.4, 'longitude' => 129), -0.0145439, false));
  276. $this->assertSame(-148298686, $date->calcSun(array('latitude' => 38.4, 'longitude' =>-129), -0.0145439, true ));
  277. $this->assertSame(-148250742, $date->calcSun(array('latitude' => 38.4, 'longitude' =>-129), -0.0145439, false));
  278. $this->assertSame(-148294101, $date->calcSun(array('latitude' => -38.4, 'longitude' =>-129), -0.0145439, true ));
  279. $this->assertSame(-148255327, $date->calcSun(array('latitude' => -38.4, 'longitude' =>-129), -0.0145439, false));
  280. }
  281. public function testGetDate()
  282. {
  283. $date = new Zend_Date_DateObjectTestHelper(0);
  284. $this->assertTrue(is_array($date->getDateParts()));
  285. $this->assertTrue(is_array($date->getDateParts(1000000)));
  286. $test = array( 'seconds' => 40, 'minutes' => 46,
  287. 'hours' => 14, 'mday' => 12, 'wday' => 1,
  288. 'mon' => 1, 'year' => 1970, 'yday' => 11,
  289. 'weekday' => 'Monday', 'month' => 'January', 0 => 1000000);
  290. $result = $date->getDateParts(1000000);
  291. $this->assertSame((int) $test['seconds'], (int) $result['seconds']);
  292. $this->assertSame((int) $test['minutes'], (int) $result['minutes']);
  293. $this->assertSame((int) $test['hours'], (int) $result['hours'] );
  294. $this->assertSame((int) $test['mday'], (int) $result['mday'] );
  295. $this->assertSame((int) $test['wday'], (int) $result['wday'] );
  296. $this->assertSame((int) $test['mon'], (int) $result['mon'] );
  297. $this->assertSame((int) $test['year'], (int) $result['year'] );
  298. $this->assertSame((int) $test['yday'], (int) $result['yday'] );
  299. $this->assertSame( $test['weekday'], $result['weekday']);
  300. $this->assertSame( $test['month'], $result['month'] );
  301. $this->assertSame( $test[0], $result[0] );
  302. $test = array( 'seconds' => 20, 'minutes' => 33,
  303. 'hours' => 11, 'mday' => 6, 'wday' => 3,
  304. 'mon' => 3, 'year' => 1748, 'yday' => 65,
  305. 'weekday' => 'Wednesday', 'month' => 'February', 0 => -7000000000);
  306. $result = $date->getDateParts(-7000000000);
  307. $this->assertSame((int) $test['seconds'], (int) $result['seconds']);
  308. $this->assertSame((int) $test['minutes'], (int) $result['minutes']);
  309. $this->assertSame((int) $test['hours'], (int) $result['hours'] );
  310. $this->assertSame((int) $test['mday'], (int) $result['mday'] );
  311. $this->assertSame((int) $test['wday'], (int) $result['wday'] );
  312. $this->assertSame((int) $test['mon'], (int) $result['mon'] );
  313. $this->assertSame((int) $test['year'], (int) $result['year'] );
  314. $this->assertSame((int) $test['yday'], (int) $result['yday'] );
  315. $this->assertSame( $test['weekday'], $result['weekday']);
  316. $this->assertSame( $test['month'], $result['month'] );
  317. $this->assertSame( $test[0], $result[0] );
  318. $test = array( 'seconds' => 0, 'minutes' => 40,
  319. 'hours' => 2, 'mday' => 26, 'wday' => 2,
  320. 'mon' => 8, 'year' => 2188, 'yday' => 238,
  321. 'weekday' => 'Tuesday', 'month' => 'July', 0 => 6900000000);
  322. $result = $date->getDateParts(6900000000);
  323. $this->assertSame((int) $test['seconds'], (int) $result['seconds']);
  324. $this->assertSame((int) $test['minutes'], (int) $result['minutes']);
  325. $this->assertSame((int) $test['hours'], (int) $result['hours'] );
  326. $this->assertSame((int) $test['mday'], (int) $result['mday'] );
  327. $this->assertSame((int) $test['wday'], (int) $result['wday'] );
  328. $this->assertSame((int) $test['mon'], (int) $result['mon'] );
  329. $this->assertSame((int) $test['year'], (int) $result['year'] );
  330. $this->assertSame((int) $test['yday'], (int) $result['yday'] );
  331. $this->assertSame( $test['weekday'], $result['weekday']);
  332. $this->assertSame( $test['month'], $result['month'] );
  333. $this->assertSame( $test[0], $result[0] );
  334. $test = array( 'seconds' => 0, 'minutes' => 40,
  335. 'hours' => 2, 'mday' => 26, 'wday' => 3,
  336. 'mon' => 8, 'year' => 2188, 'yday' => 238,
  337. 'weekday' => 'Wednesday', 'month' => 'July', 0 => 6900000000);
  338. $result = $date->getDateParts(6900000000, true);
  339. $this->assertSame((int) $test['seconds'], (int) $result['seconds']);
  340. $this->assertSame((int) $test['minutes'], (int) $result['minutes']);
  341. $this->assertSame((int) $test['hours'], (int) $result['hours'] );
  342. $this->assertSame((int) $test['mday'], (int) $result['mday'] );
  343. $this->assertSame((int) $test['mon'], (int) $result['mon'] );
  344. $this->assertSame((int) $test['year'], (int) $result['year'] );
  345. $this->assertSame((int) $test['yday'], (int) $result['yday'] );
  346. }
  347. public function testDate()
  348. {
  349. $date = new Zend_Date_DateObjectTestHelper(0);
  350. $this->assertTrue($date->date('U') > 0);
  351. $this->assertSame( '0', $date->date('U',0 ));
  352. $this->assertSame( '0', $date->date('U',0,false ));
  353. $this->assertSame( '0', $date->date('U',0,true ));
  354. $this->assertSame( '6900007200', $date->date('U',6900000000 ));
  355. $this->assertSame( '-6999996400', $date->date('U',-7000000000));
  356. $this->assertSame( '06', $date->date('d',-7000000000));
  357. $this->assertSame( 'Wed', $date->date('D',-7000000000));
  358. $this->assertSame( '6', $date->date('j',-7000000000));
  359. $this->assertSame( 'Wednesday', $date->date('l',-7000000000));
  360. $this->assertSame( '3', $date->date('N',-7000000000));
  361. $this->assertSame( 'th', $date->date('S',-7000000000));
  362. $this->assertSame( '3', $date->date('w',-7000000000));
  363. $this->assertSame( '65', $date->date('z',-7000000000));
  364. $this->assertSame( '10', $date->date('W',-7000000000));
  365. $this->assertSame( 'March', $date->date('F',-7000000000));
  366. $this->assertSame( '03', $date->date('m',-7000000000));
  367. $this->assertSame( 'Mar', $date->date('M',-7000000000));
  368. $this->assertSame( '3', $date->date('n',-7000000000));
  369. $this->assertSame( '31', $date->date('t',-7000000000));
  370. $this->assertSame( 'CET', $date->date('T',-7000000000));
  371. $this->assertSame( '1', $date->date('L',-7000000000));
  372. $this->assertSame( '1748', $date->date('o',-7000000000));
  373. $this->assertSame( '1748', $date->date('Y',-7000000000));
  374. $this->assertSame( '48', $date->date('y',-7000000000));
  375. $this->assertSame( 'pm', $date->date('a',-7000000000));
  376. $this->assertSame( 'PM', $date->date('A',-7000000000));
  377. $this->assertSame( '523', $date->date('B',-7000000000));
  378. $this->assertSame( '12', $date->date('g',-7000000000));
  379. $this->assertSame( '12', $date->date('G',-7000000000));
  380. $this->assertSame( '12', $date->date('h',-7000000000));
  381. $this->assertSame( '12', $date->date('H',-7000000000));
  382. $this->assertSame( '33', $date->date('i',-7000000000));
  383. $this->assertSame( '20', $date->date('s',-7000000000));
  384. $this->assertSame('Europe/Paris', $date->date('e',-7000000000));
  385. $this->assertSame( '0', $date->date('I',-7000000000));
  386. $this->assertSame( '+0100', $date->date('O',-7000000000));
  387. $this->assertSame( '+01:00', $date->date('P',-7000000000));
  388. $this->assertSame( 'CET', $date->date('T',-7000000000));
  389. $this->assertSame( '3600', $date->date('Z',-7000000000));
  390. $this->assertSame( '1748-03-06T12:33:20+0100', $date->date('c',-7000000000));
  391. $this->assertSame('Wed, 06 Mar 1748 12:33:20 +0100', $date->date('r',-7000000000));
  392. $this->assertSame( '-6999996400', $date->date('U' ,-7000000000 ));
  393. $this->assertSame( 'H', $date->date('\\H' ,-7000000000 ));
  394. $this->assertSame( '.', $date->date('.' ,-7000000000 ));
  395. $this->assertSame( '12:33:20', $date->date('H:i:s',-7000000000 ));
  396. $this->assertSame( '06-Mar-1748', $date->date('d-M-Y',-7000000000 ));
  397. $this->assertSame( '6900000000', $date->date('U',6900000000, true));
  398. $this->assertSame( '152', $date->date('B',6900000000, true));
  399. $this->assertSame( '12', $date->date('g',6899993000, true));
  400. $this->assertSame( '1', $date->date('g',6899997000, true));
  401. $this->assertSame( '1', $date->date('g',6900039200, true));
  402. $this->assertSame( '12', $date->date('h',6899993000, true));
  403. $this->assertSame( '01', $date->date('h',6899997000, true));
  404. $this->assertSame( '01', $date->date('h',6900040200, true));
  405. $this->assertSame( 'UTC', $date->date('e',-7000000000,true));
  406. $this->assertSame( '0', $date->date('I',-7000000000,true));
  407. $this->assertSame( 'GMT', $date->date('T',-7000000000,true));
  408. $this->assertSame( '6', $date->date('N',6899740800, true));
  409. $this->assertSame( 'st', $date->date('S',6900518000, true));
  410. $this->assertSame( 'nd', $date->date('S',6900604800, true));
  411. $this->assertSame( 'rd', $date->date('S',6900691200, true));
  412. $this->assertSame( '7', $date->date('N',6900432000, true));
  413. $date->setTimezone('Europe/Vienna');
  414. date_default_timezone_set('Indian/Maldives');
  415. $reference = $date->date('U');
  416. $this->assertTrue(abs($reference - time()) < 2);
  417. $this->assertSame('69000000', $date->date('U',69000000));
  418. // ISO Year (o) depends on the week number so 1.1. can be last year is week is 52/53
  419. $this->assertSame('1739', $date->date('o',$date->mktime(0, 0, 0, 1, 1, 1740)));
  420. $this->assertSame('1740', $date->date('o',$date->mktime(0, 0, 0, 1, 1, 1741)));
  421. $this->assertSame('1742', $date->date('o',$date->mktime(0, 0, 0, 1, 1, 1742)));
  422. $this->assertSame('1743', $date->date('o',$date->mktime(0, 0, 0, 1, 1, 1743)));
  423. $this->assertSame('1744', $date->date('o',$date->mktime(0, 0, 0, 1, 1, 1744)));
  424. $this->assertSame('1744', $date->date('o',$date->mktime(0, 0, 0, 1, 1, 1745)));
  425. $this->assertSame('1745', $date->date('o',$date->mktime(0, 0, 0, 1, 1, 1746)));
  426. $this->assertSame('1746', $date->date('o',$date->mktime(0, 0, 0, 1, 1, 1747)));
  427. $this->assertSame('1748', $date->date('o',$date->mktime(0, 0, 0, 1, 1, 1748)));
  428. $this->assertSame('1749', $date->date('o',$date->mktime(0, 0, 0, 1, 1, 1749)));
  429. $this->assertSame('2049', $date->date('o',$date->mktime(0, 0, 0, 1, 1, 2050)));
  430. $this->assertSame('2050', $date->date('o',$date->mktime(0, 0, 0, 1, 1, 2051)));
  431. $this->assertSame('2052', $date->date('o',$date->mktime(0, 0, 0, 1, 1, 2052)));
  432. $this->assertSame('2053', $date->date('o',$date->mktime(0, 0, 0, 1, 1, 2053)));
  433. $this->assertSame('2054', $date->date('o',$date->mktime(0, 0, 0, 1, 1, 2054)));
  434. }
  435. function testMktimeDay0And32()
  436. {
  437. // the following functionality is used by isTomorrow() and isYesterday() in Zend_Date.
  438. $date = new Zend_Date_DateObjectTestHelper(0);
  439. $this->assertSame('20060101', $date->date('Ymd', $date->mktime(0, 0, 0, 12, 32, 2005)));
  440. $this->assertSame('20050301', $date->date('Ymd', $date->mktime(0, 0, 0, 2, 29, 2005)));
  441. $this->assertSame('20051231', $date->date('Ymd', $date->mktime(0, 0, 0, 1, 0, 2006)));
  442. $this->assertSame('20050131', $date->date('Ymd', $date->mktime(0, 0, 0, 2, 0, 2005)));
  443. }
  444. /**
  445. * Test for setTimezone()
  446. */
  447. public function testSetTimezone()
  448. {
  449. $date = new Zend_Date_DateObjectTestHelper(0);
  450. date_default_timezone_set('Europe/Vienna');
  451. $date->setTimezone('Indian/Maldives');
  452. $this->assertSame('Indian/Maldives', $date->getTimezone());
  453. try {
  454. $date->setTimezone('Unknown');
  455. // without new phpdate false timezones do not throw an exception !
  456. // known and expected behaviour
  457. if (function_exists('timezone_open')) {
  458. $this->fail("exception expected");
  459. }
  460. } catch (Zend_Date_Exception $e) {
  461. $this->assertRegexp('/not a known timezone/i', $e->getMessage());
  462. $this->assertSame('Unknown', $e->getOperand());
  463. }
  464. $this->assertSame('Indian/Maldives', $date->getTimezone());
  465. $date->setTimezone();
  466. $this->assertSame('Europe/Vienna', $date->getTimezone());
  467. }
  468. /**
  469. * Test for gmtOffset
  470. */
  471. public function testgetGmtOffset()
  472. {
  473. $date = new Zend_Date_DateObjectTestHelper(0);
  474. date_default_timezone_set('Europe/Vienna');
  475. $date->setTimezone();
  476. $this->assertSame(-3600, $date->getGmtOffset());
  477. $date->setTimezone('GMT');
  478. $this->assertSame( 0, $date->getGmtOffset());
  479. }
  480. /**
  481. * Test for _getTime
  482. */
  483. public function test_getTime()
  484. {
  485. $date = new Zend_Date_DateObjectTestHelper(Zend_Date::now());
  486. $time = $date->_getTime();
  487. $diff = abs(time() - $time);
  488. $this->assertTrue(($diff < 2), "Zend_Date_DateObject->_getTime() returned a significantly "
  489. . "different timestamp than expected: $diff seconds");
  490. }
  491. }
  492. class Zend_Date_DateObjectTestHelper extends Zend_Date
  493. {
  494. public function __construct($date = null, $part = null, $locale = null)
  495. {
  496. $this->setTimezone('Europe/Paris');
  497. parent::__construct($date, $part, $locale);
  498. }
  499. public function mktime($hour, $minute, $second, $month, $day, $year, $dst= -1, $gmt = false)
  500. {
  501. return parent::mktime($hour, $minute, $second, $month, $day, $year, $dst, $gmt);
  502. }
  503. public function getUnixTimestamp()
  504. {
  505. return parent::getUnixTimestamp();
  506. }
  507. public function setUnixTimestamp($timestamp = null)
  508. {
  509. return parent::setUnixTimestamp($timestamp);
  510. }
  511. public function weekNumber($year, $month, $day)
  512. {
  513. return parent::weekNumber($year, $month, $day);
  514. }
  515. public function dayOfWeekHelper($y, $m, $d)
  516. {
  517. return Zend_Date_DateObject::dayOfWeek($y, $m, $d);
  518. }
  519. public function calcSun($location, $horizon, $rise = false)
  520. {
  521. return parent::calcSun($location, $horizon, $rise);
  522. }
  523. public function date($format, $timestamp = null, $gmt = false)
  524. {
  525. return parent::date($format, $timestamp, $gmt);
  526. }
  527. public function getDateParts($timestamp = null, $fast = null)
  528. {
  529. return parent::getDateParts($timestamp, $fast);
  530. }
  531. public function _getTime($sync = null)
  532. {
  533. return parent::_getTime($sync);
  534. }
  535. }