Fixing code formatting

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@3114 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2006-06-15 15:29:06 +00:00
parent 3d4d5e4388
commit 79ba4fa94d

View file

@ -34,7 +34,7 @@
* @package cake * @package cake
* @subpackage cake.cake.libs.view.helpers * @subpackage cake.cake.libs.view.helpers
*/ */
class TimeHelper extends Helper{ class TimeHelper extends Helper {
/** /**
* Returns given string trimmed to given length, adding an ending (default: "..") if necessary. * Returns given string trimmed to given length, adding an ending (default: "..") if necessary.
* *
@ -43,9 +43,9 @@ class TimeHelper extends Helper{
* @param string $ending Ending to be appended after trimmed string * @param string $ending Ending to be appended after trimmed string
* @return string Trimmed string * @return string Trimmed string
*/ */
function trim($string, $length, $ending = '..') { function trim($string, $length, $ending = '..') {
return substr($string, 0, $length) . (strlen($string) > $length ? $ending : null); return substr($string, 0, $length) . (strlen($string) > $length ? $ending : null);
} }
/** /**
* Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string. * Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string.
@ -53,13 +53,13 @@ class TimeHelper extends Helper{
* @param string $date_string Datetime string * @param string $date_string Datetime string
* @return string Formatted date string * @return string Formatted date string
*/ */
function fromString($date_string) { function fromString($date_string) {
if (is_integer($date_string)) { if (is_integer($date_string)) {
return $date_string; return $date_string;
} else { } else {
return strtotime($date_string); return strtotime($date_string);
} }
} }
/** /**
* Returns a nicely formatted date string for given Datetime string. * Returns a nicely formatted date string for given Datetime string.
@ -68,16 +68,16 @@ class TimeHelper extends Helper{
* @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT.
* @return string Formatted date string * @return string Formatted date string
*/ */
function nice($date_string = null, $return = false) { function nice($date_string = null, $return = false) {
if ($date_string != null) { if ($date_string != null) {
$date = $this->fromString($date_string); $date = $this->fromString($date_string);
} else { } else {
$date = time(); $date = time();
} }
$ret=date("D, M jS Y, H:i", $date); $ret = date("D, M jS Y, H:i", $date);
return $this->output($ret, $return); return $this->output($ret, $return);
} }
/** /**
* Returns a formatted descriptive date string for given datetime string. * Returns a formatted descriptive date string for given datetime string.
@ -91,21 +91,21 @@ class TimeHelper extends Helper{
* @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT.
* @return string Described, relative date string * @return string Described, relative date string
*/ */
function niceShort($date_string = null, $return = false) { function niceShort($date_string = null, $return = false) {
$date=$date_string ? $this->fromString($date_string) : time(); $date = $date_string ? $this->fromString($date_string) : time();
$y=$this->isThisYear($date) ? '' : ' Y'; $y = $this->isThisYear($date) ? '' : ' Y';
if ($this->isToday($date)) { if ($this->isToday($date)) {
$ret = "Today, " . date("H:i", $date); $ret = "Today, " . date("H:i", $date);
} elseif($this->wasYesterday($date)) { } elseif($this->wasYesterday($date)) {
$ret = "Yesterday, " . date("H:i", $date); $ret = "Yesterday, " . date("H:i", $date);
} else { } else {
$ret = date("M jS{$y}, H:i", $date); $ret = date("M jS{$y}, H:i", $date);
} }
return $this->output($ret, $return); return $this->output($ret, $return);
} }
/** /**
* Returns true if given datetime string is today. * Returns true if given datetime string is today.
@ -114,12 +114,11 @@ class TimeHelper extends Helper{
* @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT.
* @return boolean True if datetime string is today * @return boolean True if datetime string is today
*/ */
function isToday($date_string, $return = false) { function isToday($date_string, $return = false) {
$date=$this->fromString($date_string); $date = $this->fromString($date_string);
$ret =date('Y-m-d', $date) == date('Y-m-d', time()); $ret = date('Y-m-d', $date) == date('Y-m-d', time());
return $this->output($ret, $return);
return $this->output($ret, $return); }
}
/** /**
* Returns a partial SQL string to search for all records between two dates. * Returns a partial SQL string to search for all records between two dates.
@ -130,16 +129,15 @@ class TimeHelper extends Helper{
* @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT.
* @return string Partial SQL string. * @return string Partial SQL string.
*/ */
function daysAsSql($begin, $end, $field_name, $return = false) { function daysAsSql($begin, $end, $field_name, $return = false) {
$begin=$this->fromString($begin); $begin = $this->fromString($begin);
$end =$this->fromString($end); $end = $this->fromString($end);
$begin=date('Y-m-d', $begin) . ' 00:00:00'; $begin = date('Y-m-d', $begin) . ' 00:00:00';
$end =date('Y-m-d', $end) . ' 23:59:59'; $end = date('Y-m-d', $end) . ' 23:59:59';
$ret ="($field_name >= '$begin') AND ($field_name <= '$end')"; $ret ="($field_name >= '$begin') AND ($field_name <= '$end')";
return $this->output($ret, $return);
return $this->output($ret, $return); }
}
/** /**
* Returns a partial SQL string to search for all records between two times * Returns a partial SQL string to search for all records between two times
@ -150,12 +148,11 @@ class TimeHelper extends Helper{
* @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT.
* @return string Partial SQL string. * @return string Partial SQL string.
*/ */
function dayAsSql($date_string, $field_name, $return = false) { function dayAsSql($date_string, $field_name, $return = false) {
$date=$this->fromString($date_string); $date = $this->fromString($date_string);
$ret =$this->daysAsSql($date_string, $date_string, $field_name); $ret = $this->daysAsSql($date_string, $date_string, $field_name);
return $this->output($ret, $return);
return $this->output($ret, $return); }
}
/** /**
* Returns true if given datetime string is within current year. * Returns true if given datetime string is within current year.
@ -164,12 +161,12 @@ class TimeHelper extends Helper{
* @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT.
* @return boolean True if datetime string is within current year * @return boolean True if datetime string is within current year
*/ */
function isThisYear($date_string, $return = false) { function isThisYear($date_string, $return = false) {
$date=$this->fromString($date_string); $date = $this->fromString($date_string);
$ret =date('Y', $date) == date('Y', time()); $ret = date('Y', $date) == date('Y', time());
return $this->output($ret, $return); return $this->output($ret, $return);
} }
/** /**
* Returns true if given datetime string was yesterday. * Returns true if given datetime string was yesterday.
@ -178,12 +175,11 @@ class TimeHelper extends Helper{
* @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT.
* @return boolean True if datetime string was yesterday * @return boolean True if datetime string was yesterday
*/ */
function wasYesterday($date_string, $return = false) { function wasYesterday($date_string, $return = false) {
$date=$this->fromString($date_string); $date = $this->fromString($date_string);
$ret =date('Y-m-d', $date) == date('Y-m-d', strtotime('yesterday')); $ret = date('Y-m-d', $date) == date('Y-m-d', strtotime('yesterday'));
return $this->output($ret, $return);
return $this->output($ret, $return); }
}
/** /**
* Returns true if given datetime string is tomorrow. * Returns true if given datetime string is tomorrow.
@ -192,13 +188,11 @@ class TimeHelper extends Helper{
* @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT.
* @return boolean True if datetime string was yesterday * @return boolean True if datetime string was yesterday
*/ */
function isTomorrow($date_string, $return = false) { function isTomorrow($date_string, $return = false) {
$date=$this->fromString($date_string); $date = $this->fromString($date_string);
$ret =date('Y-m-d', $date) == date('Y-m-d', strtotime('tomorrow')); $ret = date('Y-m-d', $date) == date('Y-m-d', strtotime('tomorrow'));
return $this->output($ret, $return);
return $this->output($ret, $return); }
}
/** /**
* Returns a UNIX timestamp from a textual datetime description. Wrapper for PHP function strtotime(). * Returns a UNIX timestamp from a textual datetime description. Wrapper for PHP function strtotime().
* *
@ -206,12 +200,10 @@ class TimeHelper extends Helper{
* @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT.
* @return int Unix timestamp * @return int Unix timestamp
*/ */
function toUnix($date_string, $return = false) { function toUnix($date_string, $return = false) {
$ret=strtotime($date_string); $ret = strtotime($date_string);
return $this->output($ret, $return);
return $this->output($ret, $return); }
}
/** /**
* Returns a date formatted for Atom RSS feeds. * Returns a date formatted for Atom RSS feeds.
* *
@ -219,13 +211,11 @@ class TimeHelper extends Helper{
* @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT.
* @return string Formatted date string * @return string Formatted date string
*/ */
function toAtom($date_string, $return = false) { function toAtom($date_string, $return = false) {
$date=$this->fromString($date_string); $date = $this->fromString($date_string);
$ret =date('Y-m-d\TH:i:s\Z', $date); $ret = date('Y-m-d\TH:i:s\Z', $date);
return $this->output($ret, $return);
return $this->output($ret, $return); }
}
/** /**
* Formats date for RSS feeds * Formats date for RSS feeds
* *
@ -233,13 +223,11 @@ class TimeHelper extends Helper{
* @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT.
* @return string Formatted date string * @return string Formatted date string
*/ */
function toRSS($date_string, $return = false) { function toRSS($date_string, $return = false) {
$date=TimeHelper::fromString($date_string); $date = TimeHelper::fromString($date_string);
$ret =date("r", $date); $ret = date("r", $date);
return $this->output($ret, $return);
return $this->output($ret, $return); }
}
/** /**
* Returns either a relative date or a formatted date depending * Returns either a relative date or a formatted date depending
* on the difference between the current time and given datetime. * on the difference between the current time and given datetime.
@ -260,73 +248,65 @@ class TimeHelper extends Helper{
* @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT.
* @return string Relative time string. * @return string Relative time string.
*/ */
function timeAgoInWords($datetime_string, $format = 'j/n/y', $backwards = false, $return = false) { function timeAgoInWords($datetime_string, $format = 'j/n/y', $backwards = false, $return = false) {
$datetime =$this->fromString($datetime_string); $datetime =$this->fromString($datetime_string);
$in_seconds=$datetime; $in_seconds=$datetime;
if ($backwards) { if ($backwards) {
$diff = $in_seconds - time(); $diff = $in_seconds - time();
} else { } else {
$diff = time() - $in_seconds; $diff = time() - $in_seconds;
} }
$months=floor($diff / 2419200); $months = floor($diff / 2419200);
$diff -= $months * 2419200; $diff -= $months * 2419200;
$weeks=floor($diff / 604800); $weeks = floor($diff / 604800);
$diff -= $weeks * 604800; $diff -= $weeks * 604800;
$days=floor($diff / 86400); $days = floor($diff / 86400);
$diff -= $days * 86400; $diff -= $days * 86400;
$hours=floor($diff / 3600); $hours = floor($diff / 3600);
$diff -= $hours * 3600; $diff -= $hours * 3600;
$minutes=floor($diff / 60); $minutes = floor($diff / 60);
$diff -= $minutes * 60; $diff -= $minutes * 60;
$seconds=$diff; $seconds = $diff;
if ($months > 0) { if ($months > 0) {
// over a month old, just show date (mm/dd/yyyy format) // over a month old, just show date (mm/dd/yyyy format)
$relative_date='on ' . date($format, $in_seconds); $relative_date = 'on ' . date($format, $in_seconds);
$old=true; $old = true;
} else { } else {
$relative_date=''; $relative_date = '';
$old =false; $old = false;
if ($weeks > 0) { if ($weeks > 0) {
// weeks and days // weeks and days
$relative_date .= ($relative_date ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : ''); $relative_date .= ($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($days > 0) {
} elseif($days > 0) { // days and hours
// days and hours $relative_date .= ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '');
$relative_date .= ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : ''); $relative_date .= $hours > 0 ? ($relative_date ? ', ' : '') . $hours . ' hour' . ($hours > 1 ? 's' : '') : '';
$relative_date .= $hours > 0 } elseif($hours > 0) {
? ($relative_date ? ', ' : '') . $hours . ' hour' . ($hours > 1 ? 's' : '') : ''; // hours and minutes
} elseif($hours > 0) { $relative_date .= ($relative_date ? ', ' : '') . $hours . ' hour' . ($hours > 1 ? 's' : '');
// hours and minutes $relative_date .= $minutes > 0 ? ($relative_date ? ', ' : '') . $minutes . ' minute' . ($minutes > 1 ? 's' : '') : '';
$relative_date .= ($relative_date ? ', ' : '') . $hours . ' hour' . ($hours > 1 ? 's' : ''); } elseif($minutes > 0) {
$relative_date .= $minutes > 0 // minutes only
? ($relative_date ? ', ' : '') . $minutes . ' minute' . ($minutes > 1 ? 's' : '') : ''; $relative_date .= ($relative_date ? ', ' : '') . $minutes . ' minute' . ($minutes > 1 ? 's' : '');
} elseif($minutes > 0) { } else {
// minutes only // seconds only
$relative_date .= ($relative_date ? ', ' : '') . $minutes . ' minute' $relative_date .= ($relative_date ? ', ' : '') . $seconds . ' second' . ($seconds != 1 ? 's' : '');
. ($minutes > 1 ? 's' : ''); }
} else { }
// seconds only
$relative_date .= ($relative_date ? ', ' : '') . $seconds . ' second'
. ($seconds != 1 ? 's' : '');
}
}
$ret=$relative_date;
// show relative date and add proper verbiage
if (!$backwards && !$old) {
$ret .= ' ago';
}
return $this->output($ret, $return);
}
$ret = $relative_date;
// show relative date and add proper verbiage
if (!$backwards && !$old) {
$ret .= ' ago';
}
return $this->output($ret, $return);
}
/** /**
* Alias for timeAgoInWords * Alias for timeAgoInWords
* @param string $date_string Datetime string or Unix timestamp * @param string $date_string Datetime string or Unix timestamp
@ -335,18 +315,17 @@ class TimeHelper extends Helper{
* @return string Relative time string. * @return string Relative time string.
* @see timeAgoInWords * @see timeAgoInWords
*/ */
function relativeTime($datetime_string, $format = 'j/n/y', $return = false) { function relativeTime($datetime_string, $format = 'j/n/y', $return = false) {
$date=strtotime($datetime_string); $date = strtotime($datetime_string);
if (strtotime("now") > $date) { if (strtotime("now") > $date) {
$ret = $this->timeAgoInWords($datetime_string, $format, false); $ret = $this->timeAgoInWords($datetime_string, $format, false);
} else { } else {
$ret = $this->timeAgoInWords($datetime_string, $format, true); $ret = $this->timeAgoInWords($datetime_string, $format, true);
} }
return $this->output($ret, $return);
}
return $this->output($ret, $return);
}
/** /**
* Returns true if specified datetime was within the interval specified, else false. * Returns true if specified datetime was within the interval specified, else false.
* *
@ -355,79 +334,72 @@ class TimeHelper extends Helper{
* @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT.
* @return boolean * @return boolean
*/ */
function wasWithinLast($timeInterval, $date_string, $return = false) { function wasWithinLast($timeInterval, $date_string, $return = false) {
$date =$this->fromString($date_string); $date = $this->fromString($date_string);
$result =preg_split('/\\s/', $timeInterval); $result = preg_split('/\\s/', $timeInterval);
$numInterval =$result[0]; $numInterval = $result[0];
$textInterval=$result[1]; $textInterval = $result[1];
$currentTime =floor(time()); $currentTime = floor(time());
$seconds =($currentTime - floor($date)); $seconds = ($currentTime - floor($date));
switch($textInterval) switch($textInterval) {
{ case "seconds":
case "seconds": case "second":
case "second": $timePeriod = $seconds;
$timePeriod=$seconds; $ret = $return;
break;
$ret =$return; case "minutes":
break; case "minute":
$minutes = floor($seconds / 60);
$timePeriod = $minutes;
break;
case "minutes": case "hours":
case "minute": case "hour":
$minutes=floor($seconds / 60); $hours = floor($seconds / 3600);
$timePeriod = $hours;
break;
$timePeriod=$minutes; case "days":
break; case "day":
$days = floor($seconds / 86400);
$timePeriod = $days;
break;
case "hours": case "weeks":
case "hour": case "week":
$hours=floor($seconds / 3600); $weeks = floor($seconds / 604800);
$timePeriod=$hours; $timePeriod = $weeks;
break; break;
case "days": case "months":
case "day": case "month":
$days=floor($seconds / 86400); $months = floor($seconds / 2629743.83);
$timePeriod = $months;
break;
$timePeriod=$days; case "years":
break; case "year":
$years = floor($seconds / 31556926);
$timePeriod = $years;
break;
case "weeks": default:
case "week": $days = floor($seconds / 86400);
$weeks=floor($seconds / 604800); $timePeriod = $days;
break;
}
$timePeriod=$weeks; if ($timePeriod <= $numInterval) {
break; $ret = true;
} else {
$ret = false;
}
case "months": return $this->output($ret, $return);
case "month": }
$months=floor($seconds / 2629743.83);
$timePeriod=$months;
break;
case "years":
case "year":
$years=floor($seconds / 31556926);
$timePeriod=$years;
break;
default:
$days=floor($seconds / 86400);
$timePeriod=$days;
break;
}
if ($timePeriod <= $numInterval) {
$ret = true;
} else {
$ret = false;
}
return $this->output($ret, $return);
}
} }
?> ?>