From 5ec8eef6ce2968e25ddc0b4ac0b71a38e616a392 Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 10 Jul 2008 03:30:28 +0000 Subject: [PATCH] Added userOffset check to timeAgoInWords, tests added. Closes #5068. git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7333 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/view/helpers/time.php | 12 ++++++++---- cake/tests/cases/libs/view/helpers/time.test.php | 9 ++++++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/cake/libs/view/helpers/time.php b/cake/libs/view/helpers/time.php index a7ec93bd2..f50d608fb 100644 --- a/cake/libs/view/helpers/time.php +++ b/cake/libs/view/helpers/time.php @@ -313,13 +313,16 @@ class TimeHelper extends AppHelper { if (is_array($options) && isset($options['userOffset'])) { $userOffset = $options['userOffset']; } + $now = time(); + if (!is_null($userOffset)) { + $now = $this->convert(time(), $userOffset); + } $in_seconds = $this->fromString($dateTime, $userOffset); - $backwards = ($in_seconds > time()); + $backwards = ($in_seconds > $now); $format = 'j/n/y'; $end = '+1 month'; - $now = time(); - + if (is_array($options)) { if (isset($options['format'])) { $format = $options['format']; @@ -476,8 +479,8 @@ class TimeHelper extends AppHelper { * Returns true if specified datetime was within the interval specified, else false. * * @param mixed $timeInterval the numeric value with space then time type. Example of valid types: 6 hours, 2 days, 1 minute. - * @param int $userOffset User's offset from GMT (in hours) * @param mixed $dateString the datestring or unix timestamp to compare + * @param int $userOffset User's offset from GMT (in hours) * @return bool */ function wasWithinLast($timeInterval, $dateString, $userOffset = null) { @@ -521,6 +524,7 @@ class TimeHelper extends AppHelper { /** * Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string. * + * @param string $format date format string. defaults to 'd-m-Y' * @param string $dateString Datetime string * @param boolean $invalid flag to ignore results of fromString == false * @param int $userOffset User's offset from GMT (in hours) diff --git a/cake/tests/cases/libs/view/helpers/time.test.php b/cake/tests/cases/libs/view/helpers/time.test.php index d749bd15d..fcb1323f4 100644 --- a/cake/tests/cases/libs/view/helpers/time.test.php +++ b/cake/tests/cases/libs/view/helpers/time.test.php @@ -29,11 +29,10 @@ if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) { define('CAKEPHP_UNIT_TEST_EXECUTION', 1); } - -uses('view'.DS.'helpers'.DS.'app_helper', 'controller'.DS.'controller', 'model'.DS.'model', 'view'.DS.'helper', 'view'.DS.'helpers'.DS.'time'); +App::import('Helper', 'Time'); /** - * Short description for class. + * Time Test Case * * @package cake.tests * @subpackage cake.tests.cases.libs.view.helpers @@ -267,6 +266,10 @@ class TimeTest extends UnitTestCase { $result = $this->Time->timeAgoInWords(strtotime('-13 months, -5 days'), array('end' => '2 years')); $this->assertEqual($result, '1 year, 1 month, 5 days ago'); + + $fourHours = $this->Time->timeAgoInWords(strtotime('-5 days, -2 hours'), array('userOffset' => -4)); + $result = $this->Time->timeAgoInWords(strtotime('-5 days, -2 hours'), array('userOffset' => 4)); + $this->assertEqual($fourHours, $result); } /** * testRelative method