fixing the time helper leap year problem,

adding tests to time helper to bring it close to 90% coverage

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6758 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
DarkAngelBGE 2008-05-05 14:38:03 +00:00
parent 25033f1599
commit 0628b24760
2 changed files with 318 additions and 99 deletions

View file

@ -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 {
$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 --;
}
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;
$days = floor($diff / 86400);
$diff = $diff - ($days * 86400);
$hours = floor($diff / 3600);
@ -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.

View file

@ -56,6 +56,15 @@ 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() {
@ -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);
}
}
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,7 +162,7 @@ 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' : '') : '';
@ -182,11 +193,13 @@ 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."<br />";
$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);
}
@ -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);
}