diff --git a/cake/libs/view/helpers/time.php b/cake/libs/view/helpers/time.php index 0a750b5f6..0a7101643 100644 --- a/cake/libs/view/helpers/time.php +++ b/cake/libs/view/helpers/time.php @@ -136,7 +136,7 @@ class TimeHelper extends AppHelper { * @return boolean True if datetime string is within current week */ function isThisWeek($date_string) { - $date = $this->fromString($date_string) + 86400; + $date = $this->fromString($date_string); return date('W Y', $date) == date('W Y', time()); } /** @@ -293,61 +293,71 @@ class TimeHelper extends AppHelper { $diff = $future_time - $past_time; // If more than a week, then take into account the length of months - if($diff >= 604800) { + if ($diff >= 604800) { $current = array(); $date = array(); list($future['H'], $future['i'], $future['s'], $future['d'], $future['m'], $future['Y']) = explode('/', date('H/i/s/d/m/Y', $future_time)); + list($past['H'], $past['i'], $past['s'], $past['d'], $past['m'], $past['Y']) = explode('/', date('H/i/s/d/m/Y', $past_time)); $years = $months = $weeks = $days = $hours = $minutes = $seconds = 0; - if($future['Y'] == $past['Y'] && $future['m'] == $past['m']) { + if ($future['Y'] == $past['Y'] && $future['m'] == $past['m']) { $months = 0; $years = 0; } else { - if($future['Y'] == $past['Y']) { + if ($future['Y'] == $past['Y']) { $months = $future['m'] - $past['m']; } else { $years = $future['Y'] - $past['Y']; $months = $future['m'] + ((12 * $years) - $past['m']); - if($months >= 12) { + if ($months >= 12) { $years = floor($months / 12); $months = $months - ($years * 12); } - if($future['m'] < $past['m'] && $future['Y'] - $past['Y'] == 1) { + if ($future['m'] < $past['m'] && $future['Y'] - $past['Y'] == 1) { $years --; } - } + } } - if($future['d'] >= $past['d']) { + if ($future['d'] >= $past['d']) { $days = $future['d'] - $past['d']; - } else { + } else { $days_in_past_month = date('t', $past_time); $days_in_future_month = date('t', mktime(0, 0, 0, $future['m'] - 1, 1, $future['Y'])); - $days = ($days_in_future_month - $past['d']) + $future['d']; - - if($future['m'] != $past['m']) { + + if (!$backwards) { + $days = ($days_in_past_month - $past['d']) + $future['d']; + } else { + $days = ($days_in_future_month - $past['d']) + $future['d']; + } + + if ($future['m'] != $past['m']) { $months --; } } - if($months == 0 && $years >= 1 && $diff < ($years * 31536000)){ + + if ($months == 0 && $years >= 1 && $diff < ($years * 31536000)){ $months = 11; - $years --; + $years --; } - if($months >= 12) { + + if ($months >= 12) { $years = $years + 1; $months = $months - 12; } - if($days >= 7) { + + if ($days >= 7) { $weeks = floor($days / 7); $days = $days - ($weeks * 7); } } else { - $years = $months = $weeks = 0; + $years = $months = $weeks = 0; $days = floor($diff / 86400); + $diff = $diff - ($days * 86400); $hours = floor($diff / 3600); @@ -356,7 +366,7 @@ class TimeHelper extends AppHelper { $minutes = floor($diff / 60); $diff = $diff - ($minutes * 60); $seconds = $diff; - } + } $relative_date = ''; $diff = $future_time - $past_time; @@ -368,7 +378,7 @@ class TimeHelper extends AppHelper { $relative_date .= ($relative_date ? ', ' : '') . $years . ' year' . ($years > 1 ? 's' : ''); $relative_date .= $months > 0 ? ($relative_date ? ', ' : '') . $months . ' month' . ($months > 1 ? 's' : '') : ''; $relative_date .= $weeks > 0 ? ($relative_date ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : ''; - $relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : ''; + $relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : ''; } elseif (abs($months) > 0) { // months, weeks and days $relative_date .= ($relative_date ? ', ' : '') . $months . ' month' . ($months > 1 ? 's' : ''); @@ -420,69 +430,19 @@ class TimeHelper extends AppHelper { * @return bool */ function wasWithinLast($timeInterval, $date_string) { - $ret = false; + $tmp = r(' ', '', $timeInterval); + if (is_int($tmp) || preg_match('/[\d]+/', $tmp)) { + $timeInterval = $tmp.' days'; + } + $date = $this->fromString($date_string); - $result = preg_split('/\\s/', $timeInterval); - $numInterval = $result[0]; - $textInterval = $result[1]; - $currentTime = floor(time()); - $seconds = ($currentTime - floor($date)); + $interval = $this->fromString('-'.$timeInterval); - switch($textInterval) { - case "seconds": - case "second": - $timePeriod = $seconds; - break; - - case "minutes": - case "minute": - $minutes = floor($seconds / 60); - $timePeriod = $minutes; - break; - - case "hours": - case "hour": - $hours = floor($seconds / 3600); - $timePeriod = $hours; - break; - - case "days": - case "day": - $days = floor($seconds / 86400); - $timePeriod = $days; - break; - - case "weeks": - case "week": - $weeks = floor($seconds / 604800); - $timePeriod = $weeks; - break; - - case "months": - case "month": - $months = floor($seconds / 2638523.0769231); - $timePeriod = $months; - break; - - case "years": - case "year": - $years = floor($seconds / 31556926); - $timePeriod = $years; - break; - - default: - $days = floor($seconds / 86400); - $timePeriod = $days; - break; + if ($date >= $interval && $date <= time()) { + return true; } - if ($timePeriod <= $numInterval) { - $ret = true; - } else { - $ret = false; - } - - return $this->output($ret); + return false; } /** * Returns gmt, given either a UNIX timestamp or a valid strtotime() date string. diff --git a/cake/tests/cases/libs/view/helpers/time.test.php b/cake/tests/cases/libs/view/helpers/time.test.php index 77e905e01..8e0974136 100644 --- a/cake/tests/cases/libs/view/helpers/time.test.php +++ b/cake/tests/cases/libs/view/helpers/time.test.php @@ -56,35 +56,44 @@ class TimeTest extends UnitTestCase { $result = $this->Time->toQuarter('2007-3-25', true); $this->assertEqual($result, array('2007-01-01', '2007-03-31')); + + $result = $this->Time->toQuarter('2007-5-25', true); + $this->assertEqual($result, array('2007-04-01', '2007-06-30')); + + $result = $this->Time->toQuarter('2007-8-25', true); + $this->assertEqual($result, array('2007-07-01', '2007-09-30')); + + $result = $this->Time->toQuarter('2007-12-25', true); + $this->assertEqual($result, array('2007-10-01', '2007-12-31')); } function testTimeAgoInWords() { $result = $this->Time->timeAgoInWords(strtotime('4 months, 2 weeks, 3 days'), array('end' => '8 years'), true); - $this->assertEqual($result, '4 months, 2 weeks, 3 days'); + $this->assertEqual($result, '4 months, 2 weeks, 3 days'); $result = $this->Time->timeAgoInWords(strtotime('4 months, 2 weeks, 2 days'), array('end' => '8 years'), true); - $this->assertEqual($result, '4 months, 2 weeks, 2 days'); + $this->assertEqual($result, '4 months, 2 weeks, 2 days'); $result = $this->Time->timeAgoInWords(strtotime('4 months, 2 weeks, 1 day'), array('end' => '8 years'), true); - $this->assertEqual($result, '4 months, 2 weeks, 1 day'); + $this->assertEqual($result, '4 months, 2 weeks, 1 day'); $result = $this->Time->timeAgoInWords(strtotime('3 months, 2 weeks, 1 day'), array('end' => '8 years'), true); - $this->assertEqual($result, '3 months, 2 weeks, 1 day'); + $this->assertEqual($result, '3 months, 2 weeks, 1 day'); $result = $this->Time->timeAgoInWords(strtotime('3 months, 2 weeks'), array('end' => '8 years'), true); - $this->assertEqual($result, '3 months, 2 weeks'); + $this->assertEqual($result, '3 months, 2 weeks'); $result = $this->Time->timeAgoInWords(strtotime('3 months, 1 week, 6 days'), array('end' => '8 years'), true); - $this->assertEqual($result, '3 months, 1 week, 6 days'); + $this->assertEqual($result, '3 months, 1 week, 6 days'); $result = $this->Time->timeAgoInWords(strtotime('2 months, 2 weeks, 1 day'), array('end' => '8 years'), true); - $this->assertEqual($result, '2 months, 2 weeks, 1 day'); + $this->assertEqual($result, '2 months, 2 weeks, 1 day'); $result = $this->Time->timeAgoInWords(strtotime('2 months, 2 weeks'), array('end' => '8 years'), true); - $this->assertEqual($result, '2 months, 2 weeks'); + $this->assertEqual($result, '2 months, 2 weeks'); $result = $this->Time->timeAgoInWords(strtotime('2 months, 1 week, 6 days'), array('end' => '8 years'), true); - $this->assertEqual($result, '2 months, 1 week, 6 days'); + $this->assertEqual($result, '2 months, 1 week, 6 days'); $result = $this->Time->timeAgoInWords(strtotime('1 month, 1 week, 6 days'), array('end' => '8 years'), true); $this->assertEqual($result, '1 month, 1 week, 6 days'); @@ -99,7 +108,7 @@ class TimeTest extends UnitTestCase { $seconds = 0; $relative_date = ''; - if($years > 0) { + if ($years > 0) { // years and months and days $relative_date .= ($relative_date ? ', -' : '-') . $years . ' year' . ($years > 1 ? 's' : ''); $relative_date .= $months > 0 ? ($relative_date ? ', -' : '-') . $months . ' month' . ($months > 1 ? 's' : '') : ''; @@ -130,17 +139,19 @@ class TimeTest extends UnitTestCase { $relative_date .= ($relative_date ? ', -' : '-') . $seconds . ' second' . ($seconds != 1 ? 's' : ''); } - if(date('j/n/y', strtotime($relative_date)) != '1/1/70') { + if (date('j/n/y', strtotime($relative_date)) != '1/1/70') { $result = $this->Time->timeAgoInWords(strtotime($relative_date), array('end' => '8 years'), true); - if($relative_date == '0 seconds') { + if ($relative_date == '0 seconds') { $relative_date = '0 seconds ago'; } + $relative_date = str_replace('-', '', $relative_date) . ' ago'; - $this->assertEqual($result, $relative_date); + $this->assertEqual($result, $relative_date); + } } - for($i = 0; $i < 200; $i ++) { + for ($i = 0; $i < 200; $i ++) { $years = rand(0, 3); $months = rand(0, 11); $weeks = rand(0, 3); @@ -151,12 +162,12 @@ class TimeTest extends UnitTestCase { $relative_date = ''; - if($years > 0) { + if ($years > 0) { // years and months and days $relative_date .= ($relative_date ? ', ' : '') . $years . ' year' . ($years > 1 ? 's' : ''); $relative_date .= $months > 0 ? ($relative_date ? ', ' : '') . $months . ' month' . ($months > 1 ? 's' : '') : ''; $relative_date .= $weeks > 0 ? ($relative_date ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : ''; - $relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : ''; + $relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : ''; } elseif (abs($months) > 0) { // months, weeks and days $relative_date .= ($relative_date ? ', ' : '') . $months . ' month' . ($months > 1 ? 's' : ''); @@ -182,18 +193,20 @@ class TimeTest extends UnitTestCase { $relative_date .= ($relative_date ? ', ' : '') . $seconds . ' second' . ($seconds != 1 ? 's' : ''); } - if(date('j/n/y', strtotime($relative_date)) != '1/1/70') { + if (date('j/n/y', strtotime($relative_date)) != '1/1/70') { + // echo $relative_date."
"; $result = $this->Time->timeAgoInWords(strtotime($relative_date), array('end' => '8 years'), true); - if($relative_date == '0 seconds') { + if ($relative_date == '0 seconds') { $relative_date = '0 seconds ago'; } + $relative_date = str_replace('-', '', $relative_date) . ''; - $this->assertEqual($result, $relative_date); + $this->assertEqual($result, $relative_date); } } - + $result = $this->Time->timeAgoInWords(strtotime('-2 years, -5 months, -2 days'), array('end' => '3 years'), true); - $this->assertEqual($result, '2 years, 5 months, 2 days ago'); + $this->assertEqual($result, '2 years, 5 months, 2 days ago'); $result = $this->Time->timeAgoInWords('2007-9-25'); $this->assertEqual($result, 'on 25/9/07'); @@ -236,6 +249,9 @@ class TimeTest extends UnitTestCase { $result = $this->Time->timeAgoInWords(strtotime('-2 months, -2 days'), array('end' => '1 month', 'format' => 'Y-m-d')); $this->assertEqual($result, 'on ' . date('Y-m-d', strtotime('-2 months, -2 days'))); + + $result = $this->Time->timeAgoInWords(strtotime('-13 months, -5 days'), array('end' => '2 years')); + $this->assertEqual($result, '1 year, 1 month, 5 days ago'); } function testRelative() { @@ -245,6 +261,249 @@ class TimeTest extends UnitTestCase { $this->assertEqual($result, '1 week'); } + function testOfNice() { + $time = time() + 2 * DAY; + $this->assertEqual(date('D, M jS Y, H:i', $time), $this->Time->nice($time)); + + $time = time() - 2 * DAY; + $this->assertEqual(date('D, M jS Y, H:i', $time), $this->Time->nice($time)); + + $time = time(); + $this->assertEqual(date('D, M jS Y, H:i', $time), $this->Time->nice($time)); + + $time = 0; + $this->assertEqual(date('D, M jS Y, H:i', time()), $this->Time->nice($time)); + + $time = null; + $this->assertEqual(date('D, M jS Y, H:i', time()), $this->Time->nice($time)); + } + + function testOfNiceShort() { + $time = time() + 2 * DAY; + if (date('Y', $time) == date('Y')) { + $this->assertEqual(date('M jS, H:i', $time), $this->Time->niceShort($time)); + } else { + $this->assertEqual(date('M jSY, H:i', $time), $this->Time->niceShort($time)); + } + + $time = time(); + $this->assertEqual('Today, '.date('H:i', $time), $this->Time->niceShort($time)); + + $time = time() - DAY; + $this->assertEqual('Yesterday, '.date('H:i', $time), $this->Time->niceShort($time)); + } + + function testOfDaysAsSql() { + $begin = time(); + $end = time() + DAY; + $field = 'my_field'; + $expected = '(my_field >= \''.date('Y-m-d', $begin).' 00:00:00\') AND (my_field <= \''.date('Y-m-d', $end).' 23:59:59\')'; + $this->assertEqual($expected, $this->Time->daysAsSql($begin, $end, $field)); + } + + function testOfDayAsSql() { + $time = time(); + $field = 'my_field'; + $expected = '(my_field >= \''.date('Y-m-d', $time).' 00:00:00\') AND (my_field <= \''.date('Y-m-d', $time).' 23:59:59\')'; + $this->assertEqual($expected, $this->Time->dayAsSql($time, $field)); + } + + function testToUnix() { + $this->assertEqual(time(), $this->Time->toUnix(time())); + $this->assertEqual(strtotime('+1 day'), $this->Time->toUnix('+1 day')); + $this->assertEqual(strtotime('+0 days'), $this->Time->toUnix('+0 days')); + $this->assertEqual(strtotime('-1 days'), $this->Time->toUnix('-1 days')); + $this->assertEqual(false, $this->Time->toUnix('')); + $this->assertEqual(false, $this->Time->toUnix(null)); + } + + function testOfToAtom() { + $this->assertEqual(date('Y-m-d\TH:i:s\Z'), $this->Time->toAtom(time())); + } + + function testOfToRss() { + $this->assertEqual(date('r'), $this->Time->toRss(time())); + } + + function testOfFormat() { + $format = 'D-M-Y'; + $arr = array(time(), strtotime('+1 days'), strtotime('+1 days'), strtotime('+0 days')); + foreach ($arr as $val) { + $this->assertEqual(date($format, $val), $this->Time->format($format, $val)); + } + } + + function testOfGmt() { + $hour = 3; + $min = 4; + $sec = 2; + $month = 5; + $day = 14; + $year = 2007; + $time = mktime($hour, $min, $sec, $month, $day, $year); + $expected = gmmktime($hour, $min, $sec, $month, $day, $year); + $this->assertEqual($expected, $this->Time->gmt(date('Y-n-j G:i:s', $time))); + + $hour = date('H'); + $min = date('i'); + $sec = date('s'); + $month = date('m'); + $day = date('d'); + $year = date('Y'); + $expected = gmmktime($hour, $min, $sec, $month, $day, $year); + $this->assertEqual($expected, $this->Time->gmt(null)); + } + + function testOfIsToday() { + $result = $this->Time->isToday('+1 day'); + $this->assertFalse($result); + $result = $this->Time->isToday('+1 days'); + $this->assertFalse($result); + $result = $this->Time->isToday('+0 day'); + $this->assertTrue($result); + $result = $this->Time->isToday('-1 day'); + $this->assertFalse($result); + } + + function testOfIsThisWeek() { + switch (date('D')) { + case 'Mon' : + for ($i = 0; $i < 6; $i++) { + $this->assertTrue($this->Time->isThisWeek("+$i days")); + } + $this->assertFalse($this->Time->isThisWeek("+7 days")); + $this->assertFalse($this->Time->isThisWeek("-1 days")); + break; + case 'Tue' : + for ($i = -1; $i < 5; $i++) { + $this->assertTrue($this->Time->isThisWeek("+$i days")); + } + $this->assertFalse($this->Time->isThisWeek("+6 days")); + $this->assertFalse($this->Time->isThisWeek("-2 days")); + break; + case 'Wed' : + for ($i = -2; $i < 4; $i++) { + $this->assertTrue($this->Time->isThisWeek("+$i days")); + } + $this->assertFalse($this->Time->isThisWeek("+5 days")); + $this->assertFalse($this->Time->isThisWeek("-3 days")); + break; + case 'Thu' : + for ($i = -3; $i < 3; $i++) { + $this->assertTrue($this->Time->isThisWeek("+$i days")); + } + $this->assertFalse($this->Time->isThisWeek("+4 days")); + $this->assertFalse($this->Time->isThisWeek("-4 days")); + break; + case 'Fri' : + for ($i = -4; $i < 2; $i++) { + $this->assertTrue($this->Time->isThisWeek("+$i days")); + } + $this->assertFalse($this->Time->isThisWeek("+3 days")); + $this->assertFalse($this->Time->isThisWeek("-5 days")); + break; + case 'Sat' : + for ($i = -5; $i < 1; $i++) { + $this->assertTrue($this->Time->isThisWeek("+$i days")); + } + $this->assertFalse($this->Time->isThisWeek("+2 days")); + $this->assertFalse($this->Time->isThisWeek("-6 days")); + break; + case 'Sun' : + for ($i = 6; $i < 0; $i++) { + $this->assertTrue($this->Time->isThisWeek("+$i days")); + } + $this->assertFalse($this->Time->isThisWeek("+1 days")); + $this->assertFalse($this->Time->isThisWeek("-7 days")); + break; + } + } + + function testOfIsThisMonth() { + $result = $this->Time->isThisMonth('+0 day'); + $this->assertTrue($result); + $result = $this->Time->isThisMonth($time = mktime(0, 0, 0, date('m'), rand(1, 28), date('Y'))); + $this->assertTrue($result); + $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), rand(1, 28), date('Y')-rand(1, 12))); + $this->assertFalse($result); + $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), rand(1, 28), date('Y')+rand(1, 12))); + $this->assertFalse($result); + + } + + function testOfIsThisYear() { + $result = $this->Time->isThisYear('+0 day'); + $this->assertTrue($result); + $result = $this->Time->isThisYear(mktime(0, 0, 0, rand(1, 12), rand(1, 28), date('Y'))); + $this->assertTrue($result); + } + + function testOfWasYesterday() { + $result = $this->Time->wasYesterday('+1 day'); + $this->assertFalse($result); + $result = $this->Time->wasYesterday('+1 days'); + $this->assertFalse($result); + $result = $this->Time->wasYesterday('+0 day'); + $this->assertFalse($result); + $result = $this->Time->wasYesterday('-1 day'); + $this->assertTrue($result); + $result = $this->Time->wasYesterday('-1 days'); + $this->assertTrue($result); + $result = $this->Time->wasYesterday('-2 days'); + $this->assertFalse($result); + } + + function testOfIsTomorrow() { + $result = $this->Time->isTomorrow('+1 day'); + $this->assertTrue($result); + $result = $this->Time->isTomorrow('+1 days'); + $this->assertTrue($result); + $result = $this->Time->isTomorrow('+0 day'); + $this->assertFalse($result); + $result = $this->Time->isTomorrow('-1 day'); + $this->assertFalse($result); + } + + function testOfWasWithinLast() { + $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day')); + $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week')); + $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year')); + $this->assertTrue($this->Time->wasWithinLast('1 second', '-1 second')); + $this->assertTrue($this->Time->wasWithinLast('1 minute', '-1 minute')); + $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year')); + $this->assertTrue($this->Time->wasWithinLast('1 month', '-1 month')); + $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day')); + + $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 day')); + $this->assertTrue($this->Time->wasWithinLast('2 week', '-1 week')); + $this->assertFalse($this->Time->wasWithinLast('1 second', '-1 year')); + $this->assertTrue($this->Time->wasWithinLast('10 minutes', '-1 second')); + $this->assertTrue($this->Time->wasWithinLast('23 minutes', '-1 minute')); + $this->assertFalse($this->Time->wasWithinLast('0 year', '-1 year')); + $this->assertTrue($this->Time->wasWithinLast('13 month', '-1 month')); + $this->assertTrue($this->Time->wasWithinLast('2 days', '-1 day')); + + $this->assertFalse($this->Time->wasWithinLast('1 week', '-2 weeks')); + $this->assertFalse($this->Time->wasWithinLast('1 second', '-2 seconds')); + $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days')); + $this->assertFalse($this->Time->wasWithinLast('1 hour', '-2 hours')); + $this->assertFalse($this->Time->wasWithinLast('1 month', '-2 months')); + $this->assertFalse($this->Time->wasWithinLast('1 year', '-2 years')); + + $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 weeks')); + $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days')); + $this->assertFalse($this->Time->wasWithinLast('0 days', '-2 days')); + $this->assertTrue($this->Time->wasWithinLast('1 hour', '-20 seconds')); + $this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds')); + $this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months')); + $this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months')); + + $this->assertTrue($this->Time->wasWithinLast('5 ', '-3 days')); + $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 hour')); + $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 minute')); + $this->assertTrue($this->Time->wasWithinLast('1 ', '-23 hours -59 minutes - 59 seconds')); + } + function tearDown() { unset($this->Time); }