PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/unit_tests/classes/w2p/Utilities/DateTest.php

https://github.com/web2project/web2project
PHP | 1891 lines | 1012 code | 335 blank | 544 comment | 0 complexity | a621ce1211b1d0af3e4b0ae3d6662628 MD5 | raw file
Possible License(s): LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Class for testing Date functionality
  4. *
  5. *
  6. * PHP version 5
  7. *
  8. * LICENSE: This source file is subject to Clear BSD License. Please see the
  9. * LICENSE file in root of site for further details
  10. *
  11. * @author D. Keith Casey, Jr.<caseydk@users.sourceforge.net>
  12. * @category w2p_Utilities_Date_Test
  13. * @package web2project
  14. * @subpackage unit_tests
  15. * @license Clear BSD
  16. * @link http://www.web2project.net
  17. */
  18. class w2p_Utilities_DateTest extends CommonSetup
  19. {
  20. /**
  21. * Stores old working days value while tests being run
  22. *
  23. * @param string
  24. * @access private
  25. */
  26. private $old_working_days;
  27. /**
  28. * Stores old day start value while tests being run
  29. *
  30. * @param int
  31. * @access private
  32. */
  33. private $old_cal_day_start;
  34. /**
  35. * Stores old say end value while test being run
  36. *
  37. * @param int
  38. * @access private
  39. */
  40. private $old_cal_day_end;
  41. /**
  42. * Save our global settings before running tests
  43. */
  44. protected function setUp()
  45. {
  46. global $w2Pconfig;
  47. parent::setUp();
  48. // Save old working days, day start and end
  49. $this->old_working_days = $w2Pconfig['cal_working_days'];
  50. $this->old_cal_day_start = $w2Pconfig['cal_day_start'];
  51. $this->old_cal_day_end = $w2Pconfig['cal_day_end'];
  52. $this->old_dwh = $w2Pconfig['daily_working_hours'];
  53. $w2Pconfig['cal_working_days'] = '1,2,3,4,5';
  54. $w2Pconfig['cal_day_start'] = 9;
  55. $w2Pconfig['cal_day_end'] = 17;
  56. $w2Pconfig['daily_working_hours'] = 8;
  57. }
  58. /**
  59. * Restore our global settings after running tests
  60. */
  61. protected function tearDown()
  62. {
  63. global $w2Pconfig;
  64. parent::tearDown();
  65. // Restore old working days, day start and end
  66. $w2Pconfig['cal_working_days'] = $this->old_working_days;
  67. $w2Pconfig['cal_day_start'] = $this->old_cal_day_start;
  68. $w2Pconfig['cal_day_end'] = $this->old_cal_day_end;
  69. $w2Pconfig['daily_working_hours'] = $this->old_dwh;
  70. }
  71. /**
  72. * Tests constructor without arguments
  73. */
  74. public function testConstructorNoDateTimeNoTz()
  75. {
  76. $date = new w2p_Utilities_Date();
  77. $datetime = new DateTime();
  78. $timezone = new DateTimeZone($datetime->getTimezone()->getName());
  79. $this->assertInstanceOf('w2p_Utilities_Date', $date);
  80. $this->assertEquals($datetime->format('Y'), $date->year);
  81. $this->assertEquals($datetime->format('m'), $date->month);
  82. $this->assertEquals($datetime->format('d'), $date->day);
  83. $this->assertEquals($datetime->format('H'), $date->hour);
  84. $this->assertEquals($datetime->format('i'), $date->minute);
  85. $this->assertEquals($datetime->format('s'), $date->second);
  86. $this->assertEquals($timezone->getName(), $date->tz['id']);
  87. }
  88. /**
  89. * Tests constructor with a datetime, but no timezone
  90. */
  91. public function testConstructorDateTimeNoTz()
  92. {
  93. $date = new w2p_Utilities_Date('2010-08-07 11:00:00');
  94. $datetime = new DateTime('2010-08-07 11:00:00');
  95. $timezone = new DateTimeZone($datetime->getTimezone()->getName());
  96. $this->assertInstanceOf('w2p_Utilities_Date', $date);
  97. $this->assertEquals($datetime->format('Y'), $date->year);
  98. $this->assertEquals($datetime->format('m'), $date->month);
  99. $this->assertEquals($datetime->format('d'), $date->day);
  100. $this->assertEquals($datetime->format('H'), $date->hour);
  101. $this->assertEquals($datetime->format('i'), $date->minute);
  102. $this->assertEquals($datetime->format('s'), $date->second);
  103. $this->assertEquals($timezone->getName(), $date->tz['id']);
  104. }
  105. /**
  106. * Tests constructor with a datetime and timezone
  107. */
  108. public function testConstructorDateTimeTz()
  109. {
  110. $date = new w2p_Utilities_Date('2010-08-07 11:00:00', 'America/Halifax');
  111. $datetime = new DateTime('2010-08-07 11:00:00', new DateTimeZone('America/Halifax'));
  112. $timezone = new DateTimeZone($datetime->getTimezone()->getName());
  113. $this->assertInstanceOf('w2p_Utilities_Date', $date);
  114. $this->assertEquals($datetime->format('Y'), $date->year);
  115. $this->assertEquals($datetime->format('m'), $date->month);
  116. $this->assertEquals($datetime->format('d'), $date->day);
  117. $this->assertEquals($datetime->format('H'), $date->hour);
  118. $this->assertEquals($datetime->format('i'), $date->minute);
  119. $this->assertEquals($datetime->format('s'), $date->second);
  120. $this->assertEquals($timezone->getName(), $date->tz['id']);
  121. }
  122. /**
  123. * Tests constructor with an invalid datetime
  124. */
  125. public function testConstructorInvalidDateTime()
  126. {
  127. $date = new w2p_Utilities_Date('2010-35-35 28:65:85');
  128. $datetime = new DateTime();
  129. $timezone = new DateTimeZone($datetime->getTimezone()->getName());
  130. $this->assertInstanceOf('w2p_Utilities_Date', $date);
  131. $this->assertEquals(2010, $date->year);
  132. $this->assertEquals(35, $date->month);
  133. $this->assertEquals(35, $date->day);
  134. $this->assertEquals(28, $date->hour);
  135. $this->assertEquals(65, $date->minute);
  136. $this->assertEquals(85, $date->second);
  137. $this->assertEquals($timezone->getName(), $date->tz['id']);
  138. }
  139. /**
  140. * Tests constructor with an invalid timezone
  141. *
  142. * expectedException PHPUnit_Framework_Error
  143. */
  144. public function testConstructorInvalidTimezone()
  145. {
  146. $date = new w2p_Utilities_Date('2010-08-07 22:10:27', 'Halifax');
  147. $datetime = new DateTime('2010-08-07 22:10:27');
  148. $this->assertInstanceOf('w2p_Utilities_Date', $date);
  149. $this->assertEquals($datetime->format('Y'), $date->year);
  150. $this->assertEquals($datetime->format('m'), $date->month);
  151. $this->assertEquals($datetime->format('d'), $date->day);
  152. $this->assertEquals($datetime->format('H'), $date->hour);
  153. $this->assertEquals($datetime->format('i'), $date->minute);
  154. $this->assertEquals($datetime->format('s'), $date->second);
  155. $this->assertEquals('Halifax', $date->tz['id']);
  156. $this->assertFalse(isset($data->tz['offset']));
  157. $this->assertFalse(isset($data->tz['longname']));
  158. $this->assertFalse(isset($data->tz['shortname']));
  159. $this->assertFalse(isset($data->tz['dstlongname']));
  160. $this->assertFalse(isset($data->tz['dstshortname']));
  161. $this->assertFalse(isset($data->tz['hasdst']));
  162. }
  163. /**
  164. * Tests compare function when days are greater and don't convert timezone
  165. */
  166. public function testCompareDayGreaterNoConvertTz()
  167. {
  168. $date1 = new w2p_Utilities_Date('2010-08-07 00:00:00');
  169. $date2 = new w2p_Utilities_Date('2010-08-06 00:00:00');
  170. $this->assertEquals(1, $date1->compare($date1, $date2));
  171. }
  172. /**
  173. * Tests compare function when days are the same, hours are greater
  174. * and don't convert timezone
  175. */
  176. public function testCompareHourGreaterNoConvertTz()
  177. {
  178. $date1 = new w2p_Utilities_Date('2010-08-07 02:00:00');
  179. $date2 = new w2p_Utilities_Date('2010-08-07 01:00:00');
  180. $this->assertEquals(1, $date1->compare($date1, $date2));
  181. }
  182. /**
  183. * Tests compare function when days and hours are the same, minutes are
  184. * greater and don't convert timezone
  185. */
  186. public function testCompareMinuteGreaterNotConvertTz()
  187. {
  188. $date1 = new w2p_Utilities_Date('2010-08-07 01:01:00');
  189. $date2 = new w2p_Utilities_Date('2010-08-07 01:00:00');
  190. $this->assertEquals(1, $date1->compare($date1, $date2));
  191. }
  192. /**
  193. * Tests compare function when days, hours and minutes are the same,
  194. * seconds are greater and don't convert timezone
  195. */
  196. public function testCompareSecondGreaterNoConvertTz()
  197. {
  198. $date1 = new w2p_Utilities_Date('2010-08-07 01:01:01');
  199. $date2 = new w2p_Utilities_Date('2010-08-07 01:01:00');
  200. $this->assertEquals(1, $date1->compare($date1, $date2));
  201. }
  202. /**
  203. * Tests compare function when days are lesser and don't convert timezone
  204. */
  205. public function testCompareDayLesserNoConvertTz()
  206. {
  207. $date1 = new w2p_Utilities_Date('2010-08-06 00:00:00');
  208. $date2 = new w2p_Utilities_Date('2010-08-07 00:00:00');
  209. $this->assertEquals(-1, $date1->compare($date1, $date2));
  210. }
  211. /**
  212. * Tests compare function when days are the same, hours are lesser
  213. * and don't convert timezone
  214. */
  215. public function testCompareHourLesserNoConvertTz()
  216. {
  217. $date1 = new w2p_Utilities_Date('2010-08-07 01:00:00');
  218. $date2 = new w2p_Utilities_Date('2010-08-07 02:00:00');
  219. $this->assertEquals(-1, $date1->compare($date1, $date2));
  220. }
  221. /**
  222. * Tests compare function when days and hours are the same, minutes are
  223. * lesser and don't convert timezone
  224. */
  225. public function testCompareMinuteLesserNotConvertTz()
  226. {
  227. $date1 = new w2p_Utilities_Date('2010-08-07 01:00:00');
  228. $date2 = new w2p_Utilities_Date('2010-08-07 01:01:00');
  229. $this->assertEquals(-1, $date1->compare($date1, $date2));
  230. }
  231. /**
  232. * Tests compare function when days, hours and minutes are the same,
  233. * seconds are lesser and don't convert timezone
  234. */
  235. public function testCompareSecondLesserNoConvertTz()
  236. {
  237. $date1 = new w2p_Utilities_Date('2010-08-07 01:01:00');
  238. $date2 = new w2p_Utilities_Date('2010-08-07 01:01:01');
  239. $this->assertEquals(-1, $date1->compare($date1, $date2));
  240. }
  241. /**
  242. * Tests compare function when both dates are equal, don't convert timezones
  243. */
  244. public function testCompareEqualNoConvertTz()
  245. {
  246. $date1 = new w2p_Utilities_Date('2010-08-07 00:00:00');
  247. $date2 = new w2p_Utilities_Date('2010-08-07 00:00:00');
  248. $this->assertEquals(0, $date1->compare($date1, $date2));
  249. }
  250. /**
  251. * Tests compare function when days are greater and convert timezone
  252. */
  253. public function testCompareDayGreaterConvertTz()
  254. {
  255. $date1 = new w2p_Utilities_Date('2010-08-07 00:00:00', 'America/Halifax');
  256. $date2 = new w2p_Utilities_Date('2010-08-06 00:00:00', 'America/Chicago');
  257. $this->assertEquals(1, $date1->compare($date1, $date2, true));
  258. }
  259. /**
  260. * Tests compare function when days are the same, hours are greater
  261. * and convert timezone
  262. */
  263. public function testCompareHourGreaterConvertTz()
  264. {
  265. $date1 = new w2p_Utilities_Date('2010-08-07 00:00:00', 'America/Halifax');
  266. $date2 = new w2p_Utilities_Date('2010-08-06 21:00:00', 'America/Chicago');
  267. $this->assertEquals(1, $date1->compare($date1, $date2, true));
  268. }
  269. /**
  270. * Tests compare function when days and hours are the same, minutes are
  271. * greater and convert timezone
  272. */
  273. public function testCompareMinuteGreaterConvertTz()
  274. {
  275. $date1 = new w2p_Utilities_Date('2010-08-07 01:01:00', 'America/Halifax');
  276. $date2 = new w2p_Utilities_Date('2010-08-06 23:00:00', 'America/Chicago');
  277. $this->assertEquals(1, $date1->compare($date1, $date2, true));
  278. }
  279. /**
  280. * Tests compare function when days, hours and minutes are the same,
  281. * seconds are greater and convert timezone
  282. */
  283. public function testCompareSecondGreaterConvertTz()
  284. {
  285. $date1 = new w2p_Utilities_Date('2010-08-07 01:01:01');
  286. $date2 = new w2p_Utilities_Date('2010-08-06 23:01:00');
  287. $this->assertEquals(1, $date1->compare($date1, $date2, true));
  288. }
  289. /**
  290. * Tests compare function when days are lesser and convert timezone
  291. */
  292. public function testCompareDayLesserConvertTz()
  293. {
  294. $date1 = new w2p_Utilities_Date('2010-08-06 00:00:00', 'America/Halifax');
  295. $date2 = new w2p_Utilities_Date('2010-08-07 00:00:00', 'America/Chicago');
  296. $this->assertEquals(-1, $date1->compare($date1, $date2, true));
  297. }
  298. /**
  299. * Tests compare function when days are the same, hours are lesser
  300. * and convert timezone
  301. */
  302. public function testCompareHourLesserConvertTz()
  303. {
  304. $date1 = new w2p_Utilities_Date('2010-08-07 01:00:00', 'America/Halifax');
  305. $date2 = new w2p_Utilities_Date('2010-08-07 02:00:00', 'America/Chicago');
  306. $this->assertEquals(-1, $date1->compare($date1, $date2, true));
  307. }
  308. /**
  309. * Tests compare function when days and hours are the same, minutes are
  310. * lesser and convert timezone
  311. */
  312. public function testCompareMinuteLesserConvertTz()
  313. {
  314. $date1 = new w2p_Utilities_Date('2010-08-07 01:00:00', 'America/Halifax');
  315. $date2 = new w2p_Utilities_Date('2010-08-06 23:01:00', 'America/Chicago');
  316. $this->assertEquals(-1, $date1->compare($date1, $date2, true));
  317. }
  318. /**
  319. * Tests compare function when days, hours and minutes are the same,
  320. * seconds are lesser and convert timezone
  321. */
  322. public function testCompareSecondLesserConvertTz()
  323. {
  324. $date1 = new w2p_Utilities_Date('2010-08-07 01:01:00', 'America/Halifax');
  325. $date2 = new w2p_Utilities_Date('2010-08-06 23:01:01', 'America/Chicago');
  326. $this->assertEquals(-1, $date1->compare($date1, $date2, true));
  327. }
  328. /**
  329. * Tests compare function when both dates are equal, convert timezones
  330. */
  331. public function testCompareEqualConvertTz()
  332. {
  333. $date1 = new w2p_Utilities_Date('2010-08-07 00:00:00', 'America/Halifax');
  334. $date2 = new w2p_Utilities_Date('2010-08-06 22:00:00', 'America/Chicago');
  335. $this->assertEquals(0, $date1->compare($date1, $date2, true));
  336. }
  337. /**
  338. * Tests addDays function with a full positive day
  339. */
  340. public function testAddDaysPositiveFullDay()
  341. {
  342. $date = new w2p_Utilities_Date('2010-08-08 00:00:00');
  343. $date->addDays(3);
  344. $this->assertEquals('2010-08-11 00:00:00', $date->getDate(DATE_FORMAT_ISO));
  345. }
  346. /**
  347. * Tests addDays function with a full negative day
  348. */
  349. public function testAddDaysNegativeFullDay()
  350. {
  351. $date = new w2p_Utilities_Date('2010-08-08 00:00:00');
  352. $date->addDays(-3);
  353. $this->assertEquals('2010-08-05 00:00:00', $date->getDate(DATE_FORMAT_ISO));
  354. }
  355. /**
  356. * Tests addDays function with partial positive day
  357. */
  358. public function testAddDaysPositivePartialDay()
  359. {
  360. $date = new w2p_Utilities_Date('2010-08-08 00:00:00');
  361. $date->addDays(2.5);
  362. $this->assertEquals('2010-08-10 12:00:00', $date->getDate(DATE_FORMAT_ISO));
  363. }
  364. /**
  365. * Test addDays function with partial negative day
  366. */
  367. public function testAddDaysNegativePartialDay()
  368. {
  369. $date = new w2p_Utilities_Date('2010-08-08 00:00:00');
  370. $date->addDays(-2.5);
  371. $this->assertEquals('2010-08-05 12:00:00', $date->getDate(DATE_FORMAT_ISO));
  372. }
  373. /**
  374. * Tests addDays function with partial positive day spanning over midnight
  375. */
  376. public function testAddDaysPostivePartialDayAcrossDay()
  377. {
  378. $date = new w2p_Utilities_Date('2010-08-08 14:00:00');
  379. $date->addDays(2.5);
  380. $this->assertEquals('2010-08-11 02:00:00', $date->getDate(DATE_FORMAT_ISO));
  381. }
  382. /**
  383. * Tests addDays function when the days being added spans the end of a month
  384. */
  385. public function testAddDaysAcrossMonth()
  386. {
  387. $date = new w2p_Utilities_Date('2010-08-31 00:00:00');
  388. $date->addDays(2);
  389. $this->assertEquals('2010-09-02 00:00:00', $date->getDate(DATE_FORMAT_ISO));
  390. }
  391. /**
  392. * Tests addDays function when the days being added spans the end of a year
  393. */
  394. public function testAddDaysAcrossYear()
  395. {
  396. $date = new w2p_Utilities_Date('2010-12-31 00:00:00');
  397. $date->addDays(2);
  398. $this->assertEquals('2011-01-02 00:00:00', $date->getDate(DATE_FORMAT_ISO));
  399. }
  400. /**
  401. * Tests addMonths function with a positive full month
  402. */
  403. public function testAddMonthsMoreThan12()
  404. {
  405. $date = new w2p_Utilities_Date('2010-08-08 00:00:00');
  406. $date->addMonths(14);
  407. $this->assertEquals('2011-10-08 00:00:00', $date->getDate(DATE_FORMAT_ISO));
  408. }
  409. /**
  410. * Tests addMonths function with a positive full month
  411. */
  412. public function testAddMonthsPositiveFullMonth()
  413. {
  414. $date = new w2p_Utilities_Date('2010-08-08 00:00:00');
  415. $date->addMonths(2);
  416. $this->assertEquals('2010-10-08 00:00:00', $date->getDate(DATE_FORMAT_ISO));
  417. }
  418. /**
  419. * Tests addMonths function with a negative full month
  420. */
  421. public function testAddMonthsNegativeFullMonth()
  422. {
  423. $date = new w2p_Utilities_Date('2010-08-08 00:00:00');
  424. $date->addMonths(-2);
  425. $this->assertEquals('2010-06-08 00:00:00', $date->getDate(DATE_FORMAT_ISO));
  426. }
  427. /**
  428. * Tests addMonths function with a negative full month
  429. */
  430. public function testAddMonthsNegativeLessThan12()
  431. {
  432. $date = new w2p_Utilities_Date('2010-08-08 00:00:00');
  433. $date->addMonths(-27);
  434. $this->assertEquals('2008-05-08 00:00:00', $date->getDate(DATE_FORMAT_ISO));
  435. }
  436. /**
  437. * Tests addMonths function with a positive partial month
  438. */
  439. public function testAddMonthsPositivePartialMonth()
  440. {
  441. $date = new w2p_Utilities_Date('2010-08-08 00:00:00');
  442. $date->addMonths(2.5);
  443. $this->assertEquals('2010-10-08 00:00:00', $date->getDate(DATE_FORMAT_ISO));
  444. }
  445. /**
  446. * Tests addMonths function with a negative partial month
  447. */
  448. public function testAddMonthsNegativePartialMonth()
  449. {
  450. $date = new w2p_Utilities_Date('2010-08-08 00:00:00');
  451. $date->addMonths(-2.5);
  452. $this->assertEquals('2010-06-08 00:00:00', $date->getDate(DATE_FORMAT_ISO));
  453. }
  454. /**
  455. * Tests addMonths when the number of months spans a year
  456. */
  457. public function testAddMonthsPositiveAcrossYear()
  458. {
  459. $date = new w2p_Utilities_Date('2010-12-01 00:00:00');
  460. $date->addMonths(1);
  461. $this->assertEquals('2011-01-01 00:00:00', $date->getDate(DATE_FORMAT_ISO));
  462. }
  463. /**
  464. * Tests addMonths when the number of months spans a year
  465. */
  466. public function testAddMonthsNegativeAcrossYear()
  467. {
  468. $date = new w2p_Utilities_Date('2010-01-01 00:00:00');
  469. $date->addMonths(-1);
  470. $this->assertEquals('2009-12-01 00:00:00', $date->getDate(DATE_FORMAT_ISO));
  471. }
  472. /**
  473. * Tests dateDiff when not passing an object
  474. */
  475. public function testDateDiffNotObject()
  476. {
  477. $date = new w2p_Utilities_Date('2010-08-11 00:00:00');
  478. $this->assertFalse($date->dateDiff(1));
  479. }
  480. /**
  481. * Tests dateDiff when the date being compared against is in the future and
  482. * is a full day
  483. */
  484. public function testDateDiffFutureFullDay()
  485. {
  486. $date = new w2p_Utilities_Date('2010-08-11 00:00:00');
  487. $date_diff = $date->dateDiff(new w2p_Utilities_Date('2010-08-13 00:00:00'));
  488. $this->assertEquals(2, $date_diff);
  489. }
  490. /**
  491. * Tests dateDiff when the date being compared against is in the past and
  492. * is a full day
  493. */
  494. public function testDateDiffPastFullDay()
  495. {
  496. $date = new w2p_Utilities_Date('2010-08-11 00:00:00');
  497. $date_diff = $date->dateDiff(new w2p_Utilities_Date('2010-08-07 00:00:00'));
  498. $this->assertEquals(4, $date_diff);
  499. }
  500. /**
  501. * Tests dateDiff when teh date being compared against is in the future and
  502. * is a partial day
  503. */
  504. public function testDateDiffFuturePartialDay()
  505. {
  506. $date = new w2p_Utilities_Date('2010-08-11 00:00:00');
  507. $date_diff = $date->dateDiff(new w2p_Utilities_Date('2010-08-13 12:00:00'));
  508. $this->assertEquals(2, $date_diff);
  509. }
  510. /**
  511. * Tests dateDiff when the date being compared against is in the past and is
  512. * a partial day
  513. */
  514. public function testDateDiffPastPartialDay()
  515. {
  516. $date = new w2p_Utilities_Date('2010-08-11 00:00:00');
  517. $date_diff = $date->dateDiff(new w2p_Utilities_Date('2010-08-07 06:00:00'));
  518. $this->assertEquals(4, $date_diff);
  519. }
  520. /**
  521. * Tests setTime when hour is set and minute and second are not
  522. */
  523. public function testSetTimeHourNoMinuteNoSecond()
  524. {
  525. $date = new w2p_Utilities_Date('2010-08-11 00:00:00');
  526. $date->setTime(12);
  527. $this->assertEquals('2010-08-11 12:00:00', $date->getDate(DATE_FORMAT_ISO));
  528. }
  529. /**
  530. * Tests setTime when hour and minute is set and second is not
  531. */
  532. public function testSetTimeHourMinuteNoSecond()
  533. {
  534. $date = new w2p_Utilities_Date('2010-08-11 00:00:00');
  535. $date->setTime(12, 12);
  536. $this->assertEquals('2010-08-11 12:12:00', $date->getDate(DATE_FORMAT_ISO));
  537. }
  538. /**
  539. * Tests setTime when hour, minute and second are set
  540. */
  541. public function testSetTimeHourMinuteSecond()
  542. {
  543. $date = new w2p_Utilities_Date('2010-08-11 00:00:00');
  544. $date->setTime(12, 12, 12);
  545. $this->assertEquals('2010-08-11 12:12:12', $date->getDate(DATE_FORMAT_ISO));
  546. }
  547. /**
  548. * Tests setTime with invalid hour
  549. */
  550. public function testSetTimeInvalidHour()
  551. {
  552. $date = new w2p_Utilities_Date('2010-08-11 00:00:00');
  553. $date->setTime(25);
  554. $this->assertEquals('2010-08-11 00:00:00', $date->getDate(DATE_FORMAT_ISO));
  555. }
  556. /**
  557. * Tests setTime with invalid minute
  558. */
  559. public function testSetTimeInvalidMinute()
  560. {
  561. $date = new w2p_Utilities_Date('2010-08-11 00:00:00');
  562. $date->setTime(12, 61);
  563. $this->assertEquals('2010-08-11 12:00:00', $date->getDate(DATE_FORMAT_ISO));
  564. }
  565. /**
  566. * Tests setTime with invalid second
  567. */
  568. public function testSetTimeInvalidSecond()
  569. {
  570. $date = new w2p_Utilities_Date('2010-08-11 00:00:00');
  571. $date->setTime(12, 12, 61);
  572. $this->assertEquals('2010-08-11 12:12:00', $date->getDate(DATE_FORMAT_ISO));
  573. }
  574. /**
  575. * Tests isWorkingDay with a proper working day
  576. */
  577. public function testIsWorkingDayYes()
  578. {
  579. $date = new w2p_Utilities_Date('2010-08-11 00:00:00');
  580. $this->assertTrue($date->isWorkingDay());
  581. }
  582. /**
  583. * Tests isWorkingDay with a non working day
  584. */
  585. public function testIsWorkingDayNo()
  586. {
  587. $date = new w2p_Utilities_Date('2010-08-08 00:00:00');
  588. $this->assertFalse($date->isWorkingDay());
  589. }
  590. /**
  591. * Tests isWorkingDay with a proper working day, and cal_working_days
  592. * is null
  593. */
  594. public function testIsWorkingDayNullWorkingDaysYes()
  595. {
  596. global $w2Pconfig;
  597. $w2Pconfig['cal_working_days'] = null;
  598. $date = new w2p_Utilities_Date('2010-08-10 00:00:00');
  599. $this->assertTrue($date->isWorkingDay());
  600. }
  601. /**
  602. * Tests isWorkingDay with a non working day, and call_working_days
  603. * is null
  604. */
  605. public function testIsWorkingDayNullWorkingDaysNo()
  606. {
  607. global $w2Pconfig;
  608. $w2Pconfig['cal_working_days'] = null;
  609. $date = new w2p_Utilities_Date('2010-08-07 00:00:00');
  610. $this->assertFalse($date->isWorkingDay());
  611. }
  612. /**
  613. * Tests calcDuration with positive change on same day
  614. */
  615. public function testCalcDurationIntraDayPositive()
  616. {
  617. $date = new w2p_Utilities_Date('2010-09-03 11:00:00');
  618. $this->assertEquals(1, $date->calcDuration(new w2p_Utilities_Date('2010-09-03 12:00:00')));
  619. }
  620. /**
  621. * Tests calcDuration with positive change across a day
  622. */
  623. public function testCalcDurationAcrossDayPostive()
  624. {
  625. $date = new w2p_Utilities_Date('2010-09-02 16:00:00');
  626. $this->assertEquals(2, $date->calcDuration(new w2p_Utilities_Date('2010-09-03 10:00:00')));
  627. }
  628. /**
  629. * Tests calcDuration with positive change across multiple days
  630. */
  631. public function testCalcDurationAcrossMultipleDaysPositive()
  632. {
  633. $date = new w2p_Utilities_Date('2010-09-01 15:00:00');
  634. $this->assertEquals(11, $date->calcDuration(new w2p_Utilities_Date('2010-09-03 10:00:00')));
  635. }
  636. /**
  637. * Tests calcDuration with positive change across non-working days
  638. */
  639. public function testCalcDurationAcrossNonWorkingDaysPositive()
  640. {
  641. $date = new w2p_Utilities_Date('2010-09-03 15:00:00');
  642. $this->assertEquals(3, $date->calcDuration(new w2p_Utilities_Date('2010-09-06 10:00:00')));
  643. }
  644. /**
  645. * Tests calcDuration with negative change on same day
  646. */
  647. public function testCalcDurationIntraDayNegative()
  648. {
  649. $date = new w2p_Utilities_Date('2010-09-03 12:00:00');
  650. $this->assertEquals(-1, $date->calcDuration(new w2p_Utilities_Date('2010-09-03 11:00:00')));
  651. }
  652. /**
  653. * Tests calcDuration with negative change across a day
  654. */
  655. public function testCalcDurationAcrossDayNegative()
  656. {
  657. $date = new w2p_Utilities_Date('2010-09-03 10:00:00');
  658. $this->assertEquals(-2, $date->calcDuration(new w2p_Utilities_Date('2010-09-02 16:00:00')));
  659. }
  660. /**
  661. * Tests getAMPM when it is AM
  662. */
  663. public function testGetAMPMAM()
  664. {
  665. $date = new w2p_Utilities_Date('2010-08-19 10:00:00');
  666. $this->assertEquals('am', $date->getAMPM());
  667. }
  668. /**
  669. * Tests getAMPM when it is PM
  670. */
  671. public function testGetAMPMPM()
  672. {
  673. $date = new w2p_Utilities_Date('2010-08-19 13:00:00');
  674. $this->assertEquals('pm', $date->getAMPM());
  675. }
  676. /**
  677. * Tests next_working_day when not a working day, and not preserving
  678. * hours
  679. */
  680. public function testNextWorkingDayNotWorkingDayNoPreserveHours()
  681. {
  682. $date = new w2p_Utilities_Date('2010-08-07 00:00:00');
  683. $date->next_working_day();
  684. $this->assertEquals('2010-08-09 09:00:00', $date->getDate(DATE_FORMAT_ISO));
  685. }
  686. /**
  687. * Tests next_working_day when not a working day and preserving hours
  688. */
  689. public function testNextWorkingDayNotWorkingDayPreserveHours()
  690. {
  691. $date = new w2p_Utilities_Date('2010-08-07 10:00:00');
  692. $date->next_working_day(true);
  693. $this->assertEquals('2010-08-09 10:00:00', $date->getDate(DATE_FORMAT_ISO));
  694. }
  695. /**
  696. * Tests next_working_day when its past end of working day and not
  697. * preserving hours
  698. */
  699. public function testNextWorkingDayPastEndOfDayNoPreserveHours()
  700. {
  701. $date = new w2p_Utilities_Date('2010-08-24 18:00:00');
  702. $date->next_working_day();
  703. $this->assertEquals('2010-08-25 09:00:00', $date->getDate(DATE_FORMAT_ISO));
  704. }
  705. /**
  706. * Tests next_working_day when its past end of working day and preserving
  707. * hours
  708. */
  709. public function testNextWorkingDayPastEndOfDayPreserveHours()
  710. {
  711. $date = new w2p_Utilities_Date('2010-08-24 18:00:00');
  712. $date->next_working_day(true);
  713. $this->assertEquals('2010-08-25 18:00:00', $date->getDate(DATE_FORMAT_ISO));
  714. }
  715. /**
  716. * Tests next_working_day when its exactly the end of working day and not
  717. * preserving hours
  718. */
  719. public function testNextWorkingDayEndOfDayNoPreserveHours()
  720. {
  721. $date = new w2p_Utilities_Date('2010-08-24 17:00:00');
  722. $date->next_working_day();
  723. $this->assertEquals('2010-08-25 09:00:00', $date->getDate(DATE_FORMAT_ISO));
  724. }
  725. /**
  726. * Tests next_working_day when it is a working day
  727. */
  728. public function testNextWorkingDayIsWorkingDay()
  729. {
  730. $date = new w2p_Utilities_Date('2010-08-24 13:00:00');
  731. $date->next_working_day();
  732. $this->assertEquals('2010-08-24 13:00:00', $date->getDate(DATE_FORMAT_ISO));
  733. }
  734. /**
  735. * Tests prev_working_day when not a working day and not preserving hours
  736. */
  737. public function testPrevWorkingDayNotWorkingDayNoPreserveHours()
  738. {
  739. $date = new w2p_Utilities_Date('2010-08-07 00:00:00');
  740. $date->prev_working_day();
  741. $this->assertEquals('2010-08-06 17:00:00', $date->getDate(DATE_FORMAT_ISO));
  742. }
  743. /**
  744. * Tests prev_working_day when not a working day and preserving hours
  745. */
  746. public function testPrevWorkingDayNotWorkingDayPreserveHours()
  747. {
  748. $date = new w2p_Utilities_Date('2010-08-07 00:00:00');
  749. $date->prev_working_day(true);
  750. $this->assertEquals('2010-08-06 00:00:00', $date->getDate(DATE_FORMAT_ISO));
  751. }
  752. /**
  753. * Tests prev_working_day when before start of day and not preserving hours
  754. */
  755. public function testPrevWorkingDayBeforeStartOfDayNoPreserveHours()
  756. {
  757. $date = new w2p_Utilities_Date('2010-08-07 00:00:00');
  758. $date->prev_working_day();
  759. $this->assertEquals('2010-08-06 17:00:00', $date->getDate(DATE_FORMAT_ISO));
  760. }
  761. /**
  762. * Tests prev_working_day when before start of day and preserving hours
  763. */
  764. public function testPrevWorkingDayBeforeStartOfDayPreserveHours()
  765. {
  766. $date = new w2p_Utilities_Date('2010-08-07 00:00:00');
  767. $date->prev_working_day(true);
  768. $this->assertEquals('2010-08-06 00:00:00', $date->getDate(DATE_FORMAT_ISO));
  769. }
  770. /**
  771. * Tests prev_working_day when it is the start of day and not preserving hours
  772. */
  773. public function testPrevWorkingDayStartOfDayNoPreserveHours()
  774. {
  775. $date = new w2p_Utilities_Date('2010-08-07 09:00:00');
  776. $date->prev_working_day();
  777. $this->assertEquals('2010-08-06 17:00:00', $date->getDate(DATE_FORMAT_ISO));
  778. }
  779. /**
  780. * Tests prev_working_day when it is the start of day and preserving hours
  781. */
  782. public function testPrevWorkingDayStartOfDayPreserveHours()
  783. {
  784. $date = new w2p_Utilities_Date('2010-08-07 09:00:00');
  785. $date->prev_working_day(true);
  786. $this->assertEquals('2010-08-06 09:00:00', $date->getDate(DATE_FORMAT_ISO));
  787. }
  788. /**
  789. * Tests prev_working_day when it is a working day
  790. */
  791. public function testPrevWorkingDayIsWorkingDay()
  792. {
  793. $date = new w2p_Utilities_Date('2010-08-24 13:00:00');
  794. $date->prev_working_day();
  795. $this->assertEquals('2010-08-24 13:00:00', $date->getDate(DATE_FORMAT_ISO));
  796. }
  797. /**
  798. * Tests addDuration with positive full days
  799. */
  800. public function testAddDurationPositiveDurationFullDayDuration()
  801. {
  802. $date = new w2p_Utilities_Date('2010-08-30 10:00:00');
  803. $date->addDuration(1, 24);
  804. $this->assertEquals('2010-08-31 10:00:00', $date->getDate(DATE_FORMAT_ISO));
  805. }
  806. /**
  807. * Tests addDuration with positive full days across a month
  808. */
  809. public function testAddDurationPositiverDurationFullDayDurationAcrossMonth()
  810. {
  811. $date = new w2p_Utilities_Date('2010-08-31 10:00:00');
  812. $date->addDuration(1, 24);
  813. $this->assertEquals('2010-09-01 10:00:00', $date->getDate(DATE_FORMAT_ISO));
  814. }
  815. /**
  816. * Tests addDuration with negative full days
  817. */
  818. public function testAddDurationNegativeDurationFullDayDuration()
  819. {
  820. $date = new w2p_Utilities_Date('2010-08-31 10:00:00');
  821. $date->addDuration(-1, 24);
  822. $this->assertEquals('2010-08-30 10:00:00', $date->getDate(DATE_FORMAT_ISO));
  823. }
  824. /**
  825. * Tests addDuration with negative full days across a month
  826. */
  827. public function testAddDurationNegativeDurationFullDayDurationAcrossMonth()
  828. {
  829. $date = new w2p_Utilities_Date('2010-09-01 10:00:00');
  830. $date->addDuration(-1, 24);
  831. $this->assertEquals('2010-08-31 10:00:00', $date->getDate(DATE_FORMAT_ISO));
  832. }
  833. /**
  834. * Tests addDuration with positive hour
  835. */
  836. public function testAddDurationPositiveHourDuration()
  837. {
  838. $date = new w2p_Utilities_Date('2010-08-30 10:00:00');
  839. $date->addDuration(1);
  840. $this->assertEquals('2010-08-30 11:00:00', $date->getDate(DATE_FORMAT_ISO));
  841. }
  842. /**
  843. * Tests addDuration with positive hour across a day
  844. */
  845. public function testAddDurationPositiveHourDurationAcrossDay()
  846. {
  847. $date = new w2p_Utilities_Date('2010-08-30 16:00:00');
  848. $date->addDuration(2);
  849. $this->assertEquals('2010-08-31 10:00:00', $date->getDate(DATE_FORMAT_ISO));
  850. }
  851. /**
  852. * Tests addDuration with positive hour on non working day
  853. */
  854. public function testAddDurationPositiveHourDurationNonWorkingDay()
  855. {
  856. $date = new w2p_Utilities_Date('2010-08-28 10:00:00');
  857. $date->addDuration(1);
  858. $this->assertEquals('2010-08-30 10:00:00', $date->getDate(DATE_FORMAT_ISO));
  859. }
  860. /**
  861. * Tests addDuration with positive hour when adding mulitple days
  862. */
  863. public function testAddDurationPositiveHourDurationMultipleDays()
  864. {
  865. $date = new w2p_Utilities_Date('2010-08-30 10:00:00');
  866. $date->addDuration(17);
  867. $this->assertEquals('2010-09-01 11:00:00', $date->getDate(DATE_FORMAT_ISO));
  868. }
  869. /**
  870. * Tests add duration with positive hour when it's friday afternoon(next
  871. * day is not a working day)
  872. */
  873. public function testAddDurationPositiveHourDurationFridayAfternoon()
  874. {
  875. $date = new w2p_Utilities_Date('2010-08-27 16:00:00');
  876. $date->addDuration(2);
  877. $this->assertEquals('2010-08-30 10:00:00', $date->getDate(DATE_FORMAT_ISO));
  878. }
  879. /**
  880. * Tests addDuration with negative hour
  881. */
  882. public function testAddDurationNegativeHourDuration()
  883. {
  884. $date = new w2p_Utilities_Date('2010-08-30 10:00:00');
  885. $date->addDuration(-1);
  886. $this->assertEquals('2010-08-30 09:00:00', $date->getDate(DATE_FORMAT_ISO));
  887. }
  888. /**
  889. * Tests addDuration with negative hour across a day
  890. */
  891. public function testAddDurationNegativeHourDurationAcrossDay()
  892. {
  893. $date = new w2p_Utilities_Date('2010-08-31 10:00:00');
  894. $date->addDuration(-2);
  895. $this->assertEquals('2010-08-30 16:00:00', $date->getDate(DATE_FORMAT_ISO));
  896. }
  897. /**
  898. * Tests addDuration with negative hour on non working day
  899. */
  900. public function testAddDurationNegativeHourDurationNonWorkingDay()
  901. {
  902. $date = new w2p_Utilities_Date('2010-08-28 10:00:00');
  903. $date->addDuration(-1);
  904. $this->assertEquals('2010-08-27 16:00:00', $date->getDate(DATE_FORMAT_ISO));
  905. }
  906. /**
  907. * Tests addDuration with negative hour spanning multiple days
  908. */
  909. public function testAddDurationNegativeHourDurationMultipleDays()
  910. {
  911. $date = new w2p_Utilities_Date('2010-09-01 11:00:00');
  912. $date->addDuration(-17);
  913. $this->assertEquals('2010-08-30 10:00:00', $date->getDate(DATE_FORMAT_ISO));
  914. }
  915. /**
  916. * Tests addDuration with negative hour when it's Monday morning (prev day
  917. * is not a working day)
  918. */
  919. public function testAddDurationNegativeHourDurationMondayMorning()
  920. {
  921. $date = new w2p_Utilities_Date('2010-08-30 10:00:00');
  922. $date->addDuration(-2);
  923. $this->assertEquals('2010-08-27 16:00:00', $date->getDate(DATE_FORMAT_ISO));
  924. }
  925. /**
  926. * Tests addDuration with an invalid duration type (valid are 1(hours), or
  927. * 24(days))
  928. */
  929. public function testAddDurationInvalidDurationType()
  930. {
  931. $date = new w2p_Utilities_Date('2010-08-30 10:00:00');
  932. $date->addDuration(1, 17);
  933. $this->assertEquals('2010-08-30 10:00:00', $date->getDate(DATE_FORMAT_ISO));
  934. }
  935. /**
  936. * Tests calcDuration with negative change across multiple days
  937. */
  938. public function testCalcDurationAcrossMultipleDaysNegative()
  939. {
  940. $date = new w2p_Utilities_Date('2010-09-03 10:00:00');
  941. $this->assertEquals(-11, $date->calcDuration(new w2p_Utilities_Date('2010-09-01 15:00:00')));
  942. }
  943. /**
  944. * Tests calcDuration with negative change across non-working days
  945. */
  946. public function testCalcDurationAcrossNonWorkingDaysNegative()
  947. {
  948. $date = new w2p_Utilities_Date('2010-09-06 10:00:00');
  949. $this->assertEquals(-3, $date->calcDuration(new w2p_Utilities_Date('2010-09-03 15:00:00')));
  950. }
  951. /**
  952. * Tests workingDaysInSpan on same day
  953. */
  954. public function testWorkingDaysInSpanSameDay()
  955. {
  956. $date = new w2p_Utilities_Date('2010-09-14 10:00:00');
  957. $this->assertEquals(1, $date->workingDaysInSpan(new w2p_Utilities_Date('2010-09-14 12:00:00')));
  958. }
  959. /**
  960. * Tests workingDaysInSpan with multiple positive days
  961. */
  962. public function testWorkingDaysInSpanMultiDaysPositive()
  963. {
  964. $date = new w2p_Utilities_Date('2010-09-14 10:00:00');
  965. $this->assertEquals(3, $date->workingDaysInSpan(new w2p_Utilities_Date('2010-09-16 12:00:00')));
  966. }
  967. /**
  968. * Tests workingDaysInSpan with multiple negative days
  969. */
  970. public function testWorkingDaysInSpanMultiDaysNegative()
  971. {
  972. $date = new w2p_Utilities_Date('2010-09-14 10:00:00');
  973. $this->assertEquals(2, $date->workingDaysInSpan(new w2p_Utilities_Date('2010-09-12 10:00:00')));
  974. }
  975. /**
  976. * Test workingDaysInSpan with multiple positive days including non
  977. * working days
  978. */
  979. public function testWorkingDaysInSpanMultiDaysPositiveWithNonWorking()
  980. {
  981. $date = new w2p_Utilities_Date('2010-09-14 10:00:00');
  982. $this->assertEquals(5, $date->workingDaysInSpan(new w2p_Utilities_Date('2010-09-20 10:00:00')));
  983. }
  984. /**
  985. * Test workingDaysInSpan with multiple negative days including non
  986. * working days
  987. */
  988. public function testWorkingDaysInSpanMultiDaysNegativeWithNonWorking()
  989. {
  990. $date = new w2p_Utilities_Date('2010-09-14 10:00:00');
  991. $this->assertEquals(3, $date->workingDaysInSpan(new w2p_Utilities_Date('2010-09-10 10:00:00')));
  992. }
  993. /**
  994. * Tests Duplicate
  995. */
  996. public function testDuplicate()
  997. {
  998. $date = new w2p_Utilities_Date('2010-09-14 10:00:00');
  999. $date2 = $date->duplicate();
  1000. $this->assertEquals($date, $date2);
  1001. }
  1002. /**
  1003. * Test Duplicate after changing one of the properties
  1004. */
  1005. public function testDuplicateDifferent()
  1006. {
  1007. $date = new w2p_Utilities_Date('2010-09-14 10:00:00');
  1008. $date2 = $date->duplicate();
  1009. $date->minute = 15;
  1010. $this->assertNotEquals($date, $date2);
  1011. }
  1012. /**
  1013. * Tests calcFinish when adding an hour on same day
  1014. */
  1015. public function testCalcFinishSameDayHours()
  1016. {
  1017. $date = new w2p_Utilities_Date('2010-09-15 10:00:00');
  1018. $finish = $date->calcFinish(2, 1);
  1019. $this->assertEquals('2010-09-15 12:00:00', $finish->getDate(DATE_FORMAT_ISO));
  1020. }
  1021. /**
  1022. * Tests calcFinish when the minute is > (half the cal_day_increment) so should be rounded to 45
  1023. */
  1024. public function testCalcFinish45()
  1025. {
  1026. $date = new w2p_Utilities_Date('2010-09-15 10:39:00');
  1027. $finish = $date->calcFinish(1, 1);
  1028. $this->assertEquals('2010-09-15 11:45:00', $finish->getDate(DATE_FORMAT_ISO));
  1029. }
  1030. /**
  1031. * Tests calcFinish when the minute is > (half the cal_day_increment) so should be rounded to 30
  1032. */
  1033. public function testCalcFinish30()
  1034. {
  1035. $date = new w2p_Utilities_Date('2010-09-15 10:24:00');
  1036. $finish = $date->calcFinish(1, 1);
  1037. $this->assertEquals('2010-09-15 11:30:00', $finish->getDate(DATE_FORMAT_ISO));
  1038. }
  1039. /**
  1040. * Tests calcFinish when the minute is > (half the cal_day_increment) so should be rounded to 15
  1041. */
  1042. public function testCalcFinish15()
  1043. {
  1044. $date = new w2p_Utilities_Date('2010-09-15 10:09:00');
  1045. $finish = $date->calcFinish(1, 1);
  1046. $this->assertEquals('2010-09-15 11:15:00', $finish->getDate(DATE_FORMAT_ISO));
  1047. }
  1048. /**
  1049. * Tests calcFinish when the minute is < (half the cal_day_increment) so should be rounded to 0
  1050. */
  1051. public function testCalcFinish00()
  1052. {
  1053. $date = new w2p_Utilities_Date('2010-09-15 10:07:00');
  1054. $finish = $date->calcFinish(1, 1);
  1055. $this->assertEquals('2010-09-15 11:00:00', $finish->getDate(DATE_FORMAT_ISO));
  1056. }
  1057. /**
  1058. * Tests calcFinish on a non working day
  1059. */
  1060. public function testCalcFinishNonWorkingDay()
  1061. {
  1062. $date = new w2p_Utilities_Date('2010-09-18 10:00:00');
  1063. $finish = $date->calcFinish(1, 1);
  1064. $this->assertEquals('2010-09-20 11:00:00', $finish->getDate(DATE_FORMAT_ISO));
  1065. }
  1066. /**
  1067. * Tests calcFinish across a Day
  1068. */
  1069. public function testCalcFinishAcrossDayHoursOnLastDay()
  1070. {
  1071. $date = new w2p_Utilities_Date('2010-09-20 16:00:00');
  1072. $finish = $date->calcFinish(2, 1);
  1073. $this->assertEquals('2010-09-21 10:00:00', $finish->getDate(DATE_FORMAT_ISO));
  1074. }
  1075. /**
  1076. * Test calcFinish Across multiple days when it ends at end of day
  1077. * (no hours to add to last day)
  1078. */
  1079. public function testCalcFinishAcrossMultipleDaysNoHoursLastDay()
  1080. {
  1081. $date = new w2p_Utilities_Date('2010-09-20 16:00:00');
  1082. $finish = $date->calcFinish(16, 1);
  1083. $this->assertEquals('2010-09-22 16:00:00', $finish->getDate(DATE_FORMAT_ISO));
  1084. }
  1085. /**
  1086. * Tests adding a single day, with day duration, when the time is equal to
  1087. * day start
  1088. */
  1089. public function testCalcFinishAddDayStartDayDuration()
  1090. {
  1091. $date = new w2p_Utilities_Date('2010-09-20 09:00:00');
  1092. $finish = $date->calcFinish(1, 24);
  1093. $this->assertEquals('2010-09-20 17:00:00', $finish->getDate(DATE_FORMAT_ISO));
  1094. }
  1095. /**
  1096. * Tests calcFinish, with day duration
  1097. */
  1098. public function testCalcFinishAddDaysDayDuration()
  1099. {
  1100. $date = new w2p_Utilities_Date('2010-09-20 10:00:00');
  1101. $finish = $date->calcFinish(2, 24);
  1102. $this->assertEquals('2010-09-22 10:00:00', $finish->getDate(DATE_FORMAT_ISO));
  1103. }
  1104. /**
  1105. * Test calcFinish with day duration across non working days
  1106. */
  1107. public function testCalcFinishAddDaysDayDurationAcrossNonWorkingDays()
  1108. {
  1109. $date = new w2p_Utilities_Date('2010-09-17 10:00:00');
  1110. $finish = $date->calcFinish(2, 24);
  1111. $this->assertEquals('2010-09-21 10:00:00', $finish->getDate(DATE_FORMAT_ISO));
  1112. }
  1113. /**
  1114. * Tests converting between timezones
  1115. */
  1116. public function testConvertTZ()
  1117. {
  1118. $myDate1 = new w2p_Utilities_Date('', 'US/Eastern');
  1119. $myDate2 = new w2p_Utilities_Date('', 'CST');
  1120. $myDate2->convertTZ('EST');
  1121. //This tweaks the test data in case the +1 is across the day change.
  1122. $tmpHour = ($myDate1->hour+1 >=24) ? $myDate1->hour+1-24 : $myDate1->hour+1;
  1123. $this->assertEquals($tmpHour, $myDate2->hour);
  1124. $this->assertEquals($myDate1->minute, $myDate2->minute);
  1125. $myDate2->convertTZ('PST');
  1126. $tmpHour = ($myDate1->hour-2 < 0) ? $myDate1->hour-2+24 : $myDate1->hour-2;
  1127. $this->assertEquals($tmpHour, $myDate2->hour);
  1128. }
  1129. /**
  1130. * Tests setting the timezone of a date object
  1131. */
  1132. public function testSetTZ()
  1133. {
  1134. $date = new w2p_Utilities_Date('', 'US/Atlantic');
  1135. $this->assertEquals(new w2p_Utilities_Date('', 'US/Atlantic'), $date);
  1136. $date->setTZ('US/Eastern');
  1137. $this->assertEquals(new w2p_Utilities_Date('', 'US/Eastern'), $date);
  1138. }
  1139. /**
  1140. * Tests adding seconds
  1141. */
  1142. public function testAddSecondsPositive()
  1143. {
  1144. $date = new w2p_Utilities_Date('2010-09-21 09:00:00');
  1145. $date->addSeconds(59);
  1146. $this->assertEquals('2010-09-21 09:00:59', $date->getDate(DATE_FORMAT_ISO));
  1147. }
  1148. /**
  1149. * Tests adding negative seconds
  1150. */
  1151. public function testAddSecondsNegative()
  1152. {
  1153. $date = new w2p_Utilities_Date('2010-09-21 09:00:00');
  1154. $date->addSeconds(-59);
  1155. $this->assertEquals('2010-09-21 08:59:01', $date->getDate(DATE_FORMAT_ISO));
  1156. }
  1157. /**
  1158. * Test adding seconds across a minute
  1159. */
  1160. public function testAddSecondsAcrossMinute()
  1161. {
  1162. $date = new w2p_Utilities_Date('2010-09-21 09:00:00');
  1163. $date->addSeconds(65);
  1164. $this->assertEquals('2010-09-21 09:01:05', $date->getDate(DATE_FORMAT_ISO));
  1165. }
  1166. /**
  1167. * Tests adding seconds across an hour
  1168. */
  1169. public function testAddSecondsAcrossHour()
  1170. {
  1171. $date = new w2p_Utilities_Date('2010-09-21 09:59:00');
  1172. $date->addSeconds(65);
  1173. $this->assertEquals('2010-09-21 10:00:05', $date->getDate(DATE_FORMAT_ISO));
  1174. }
  1175. /**
  1176. * Tests adding seconds across a day
  1177. */
  1178. public function testAddSecondsAcrossDay()
  1179. {
  1180. $date = new w2p_Utilities_Date('2010-09-21 23:59:00');
  1181. $date->addSeconds(65);
  1182. $this->assertEquals('2010-09-22 00:00:05', $date->getDate(DATE_FORMAT_ISO));
  1183. }
  1184. /**
  1185. * Tests adding seconds across a year
  1186. */
  1187. public function testAddSecondsAcrossYear()
  1188. {
  1189. $date = new w2p_Utilities_Date('2010-12-31 23:59:00');
  1190. $date->addSeconds(65);
  1191. $this->assertEquals('2011-01-01 00:00:05', $date->getDate(DATE_FORMAT_ISO));
  1192. }
  1193. /**
  1194. * Tests after when the date is after
  1195. */
  1196. public function testAfterIsAfter()
  1197. {
  1198. $date1 = new w2p_Utilities_Date('2010-11-04 11:00:00');
  1199. $date2 = new w2p_Utilities_Date('2010-11-04 10:00:00');
  1200. $this->assertTrue($date1->after($date2));
  1201. }
  1202. /**
  1203. * Tests after when the date is before
  1204. */
  1205. public function testAfterIsBefore()
  1206. {
  1207. $date1 = new w2p_Utilities_Date('2010-11-04 11:00:00');
  1208. $date2 = new w2p_Utilities_Date('2010-11-04 12:00:00');
  1209. $this->assertFalse($date1->after($date2));
  1210. }
  1211. /**
  1212. * Tests after then the dates are equal
  1213. */
  1214. public function testAfterIsSame()
  1215. {
  1216. $date1 = new w2p_Utilities_Date('2010-11-04 11:00:00');
  1217. $date2 = new w2p_Utilities_Date('2010-11-04 11:00:00');
  1218. $this->assertFalse($date1->after($date2));
  1219. }
  1220. /**
  1221. * Tests before when the date is before
  1222. */
  1223. public function testBeforeIsBefore()
  1224. {
  1225. $date1 = new w2p_Utilities_Date('2010-11-04 10:00:00');
  1226. $date2 = new w2p_Utilities_Date('2010-11-04 11:00:00');
  1227. $this->assertTrue($date1->before($date2));
  1228. }
  1229. /**
  1230. * Tests before when the date is after
  1231. */
  1232. public function testBeforeIsAfter()
  1233. {
  1234. $date1 = new w2p_Utilities_Date('2010-11-04 11:00:00');
  1235. $date2 = new w2p_Utilities_Date('2010-11-04 10:00:00');
  1236. $this->assertFalse($date1->before($date2));
  1237. }
  1238. /**
  1239. * Tests before when the dates are equal
  1240. */
  1241. public function testBeforeIsSame()
  1242. {
  1243. $date1 = new w2p_Utilities_Date('2010-11-04 11:00:00');
  1244. $date2 = new w2p_Utilities_Date('2010-11-04 11:00:00');
  1245. $this->assertFalse($date1->before($date2));
  1246. }
  1247. /**
  1248. * Tests getDate with ISO format
  1249. */
  1250. public function testGetDateIso()
  1251. {
  1252. $date = new w2p_Utilities_Date('2010-11-05 11:00:00');
  1253. $this->assertEquals('2010-11-05 11:00:00', $date->getDate(DATE_FORMAT_ISO));
  1254. }
  1255. /**
  1256. * Tests getDate with TIMESTAMP format
  1257. */
  1258. public function testGetDateTimestamp()
  1259. {
  1260. $date = new w2p_Utilities_Date('2010-11-05 11:00:00');
  1261. $this->assertEquals('20101105110000', $date->getDate(DATE_FORMAT_TIMESTAMP));
  1262. }
  1263. /**
  1264. * Tests getDate with UNIXTIME format
  1265. */
  1266. public function testGetDateUnixtime()
  1267. {
  1268. $date = new w2p_Utilities_Date('2010-11-05 11:00:00');
  1269. $offset = $date->tz['offset']/1000 + $date->tz['hasdst']*3600;
  1270. $this->assertEquals(1288954800-$offset, $date->getDate(DATE_FORMAT_UNIXTIME));
  1271. }
  1272. /**
  1273. * Tests getDate with an ivalid format
  1274. */
  1275. public function testGetDateInvalidFormat()
  1276. {
  1277. $date = new w2p_Utilities_Date('2010-11-05 11:00:00');
  1278. $this->assertNull($date->getDate(DATE_FORMAT_INVALID));
  1279. }
  1280. /**
  1281. * Tests getDay
  1282. */
  1283. public function testGetDay()
  1284. {
  1285. $date = new w2p_Utilities_Date('2010-11-05 11:00:00');
  1286. $this->assertEquals(5, $date->getDay());
  1287. }
  1288. /**
  1289. * Tests getDay with invalid day, this *should* break
  1290. */
  1291. public function testGetDayInvalidDay()
  1292. {
  1293. $date = new w2p_Utilities_Date('2010-11-34 11:00:00');
  1294. $this->assertEquals(34, $date->getDay());
  1295. }
  1296. /**
  1297. * Tests getDaysInMonth
  1298. */
  1299. public function testGetDaysInMonth()
  1300. {
  1301. $date = new w2p_Utilities_Date('2010-11-05 11:00:00');
  1302. $this->assertEquals(30, $date->getDaysInMonth());
  1303. $date->setMonth(12);
  1304. $this->assertEquals(31, $date->getDaysInMonth());
  1305. }
  1306. /**
  1307. * Tests getHour
  1308. */
  1309. public function testGetHour()
  1310. {
  1311. $date = new w2p_Utilities_Date('2010-11-06 11:00:00');
  1312. $this->assertEquals(11, $date->getHour());
  1313. }
  1314. /**
  1315. * Test getHour when the hour is greater then 12
  1316. */
  1317. public function testGetHourGreaterThenTwelve()
  1318. {
  1319. $date = new w2p_Utilities_Date('2010-11-06 15:00:00');
  1320. $this->assertEquals(15, $date->getHour());
  1321. }
  1322. /**
  1323. * Tests getHour with an invalid Hour, this should break...
  1324. */
  1325. public function testGetHourInvalidHour()
  1326. {
  1327. $date = new w2p_Utilities_Date('2010-11-06 25:00:00');
  1328. $this->assertEquals(25, $date->getHour());
  1329. }
  1330. /**
  1331. * Tests getMinute
  1332. */
  1333. public function testGetMinute()
  1334. {
  1335. $

Large files files are truncated, but you can click here to view the full file