Simplify the cake time class

Removed a lot of nested ?: code and nested if/else statements broken down
with early return. Moving a few variables lower down the function calls as
there are times when they are not even needed.

Also removed setting variables that are returned on the next line.
This commit is contained in:
dogmatic69 2012-09-12 23:19:36 +01:00
parent b227ff486e
commit 95b895b19a

View file

@ -355,8 +355,7 @@ class CakeTime {
if (!$format) {
$format = self::$niceFormat;
}
$format = self::convertSpecifiers($format, $date);
return self::_strftime($format, $date);
return self::_strftime(self::convertSpecifiers($format, $date), $date);
}
/**
@ -380,7 +379,15 @@ class CakeTime {
}
$date = self::fromString($dateString, $timezone);
$y = self::isThisYear($date) ? '' : ' %Y';
if (self::isToday($dateString, $timezone)) {
return __d('cake', 'Today, %s', self::_strftime("%H:%M", $date));
}
if (self::wasYesterday($dateString, $timezone)) {
return __d('cake', 'Yesterday, %s', self::_strftime("%H:%M", $date));
}
if (self::isTomorrow($dateString, $timezone)) {
return __d('cake', 'Tomorrow, %s', self::_strftime("%H:%M", $date));
}
$d = self::_strftime("%w", $date);
$day = array(
@ -392,22 +399,18 @@ class CakeTime {
__d('cake', 'Friday'),
__d('cake', 'Saturday')
);
if (self::isToday($dateString, $timezone)) {
$ret = __d('cake', 'Today, %s', self::_strftime("%H:%M", $date));
} elseif (self::wasYesterday($dateString, $timezone)) {
$ret = __d('cake', 'Yesterday, %s', self::_strftime("%H:%M", $date));
} elseif (self::isTomorrow($dateString, $timezone)) {
$ret = __d('cake', 'Tomorrow, %s', self::_strftime("%H:%M", $date));
} elseif (self::wasWithinLast('7 days', $dateString, $timezone)) {
$ret = sprintf('%s %s', $day[$d], self::_strftime(self::$niceShortFormat, $date));
} elseif (self::isWithinNext('7 days', $dateString, $timezone)) {
$ret = __d('cake', 'On %s %s', $day[$d], self::_strftime(self::$niceShortFormat, $date));
} else {
$format = self::convertSpecifiers("%b %eS{$y}, %H:%M", $date);
$ret = self::_strftime($format, $date);
if (self::wasWithinLast('7 days', $dateString, $timezone)) {
return sprintf('%s %s', $day[$d], self::_strftime(self::$niceShortFormat, $date));
}
return $ret;
if (self::isWithinNext('7 days', $dateString, $timezone)) {
return __d('cake', 'On %s %s', $day[$d], self::_strftime(self::$niceShortFormat, $date));
}
$y = '';
if (!self::isThisYear($date)) {
$y = ' %Y';
}
return self::_strftime(self::convertSpecifiers("%b %eS{$y}, %H:%M", $date), $date);
}
/**
@ -452,8 +455,8 @@ class CakeTime {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
*/
public static function isToday($dateString, $timezone = null) {
$date = self::fromString($dateString, $timezone);
return date('Y-m-d', $date) == date('Y-m-d', time());
$timestamp = self::fromString($dateString, $timezone);
return date('Y-m-d', $timestamp) == date('Y-m-d', time());
}
/**
@ -465,8 +468,8 @@ class CakeTime {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
*/
public static function isThisWeek($dateString, $timezone = null) {
$date = self::fromString($dateString, $timezone);
return date('W o', $date) == date('W o', time());
$timestamp = self::fromString($dateString, $timezone);
return date('W o', $timestamp) == date('W o', time());
}
/**
@ -477,8 +480,8 @@ class CakeTime {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
*/
public static function isThisMonth($dateString, $timezone = null) {
$date = self::fromString($dateString);
return date('m Y', $date) == date('m Y', time());
$timestamp = self::fromString($dateString);
return date('m Y', $timestamp) == date('m Y', time());
}
/**
@ -490,8 +493,8 @@ class CakeTime {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
*/
public static function isThisYear($dateString, $timezone = null) {
$date = self::fromString($dateString, $timezone);
return date('Y', $date) == date('Y', time());
$timestamp = self::fromString($dateString, $timezone);
return date('Y', $timestamp) == date('Y', time());
}
/**
@ -504,8 +507,8 @@ class CakeTime {
*
*/
public static function wasYesterday($dateString, $timezone = null) {
$date = self::fromString($dateString, $timezone);
return date('Y-m-d', $date) == date('Y-m-d', strtotime('yesterday'));
$timestamp = self::fromString($dateString, $timezone);
return date('Y-m-d', $timestamp) == date('Y-m-d', strtotime('yesterday'));
}
/**
@ -517,8 +520,8 @@ class CakeTime {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
*/
public static function isTomorrow($dateString, $timezone = null) {
$date = self::fromString($dateString, $timezone);
return date('Y-m-d', $date) == date('Y-m-d', strtotime('tomorrow'));
$timestamp = self::fromString($dateString, $timezone);
return date('Y-m-d', $timestamp) == date('Y-m-d', strtotime('tomorrow'));
}
/**
@ -532,31 +535,22 @@ class CakeTime {
public static function toQuarter($dateString, $range = false) {
$time = self::fromString($dateString);
$date = ceil(date('m', $time) / 3);
if ($range === true) {
$range = 'Y-m-d';
if ($range === false) {
return $date;
}
if ($range !== false) {
$year = date('Y', $time);
switch ($date) {
case 1:
$date = array($year . '-01-01', $year . '-03-31');
break;
return array($year . '-01-01', $year . '-03-31');
case 2:
$date = array($year . '-04-01', $year . '-06-30');
break;
return array($year . '-04-01', $year . '-06-30');
case 3:
$date = array($year . '-07-01', $year . '-09-30');
break;
return array($year . '-07-01', $year . '-09-30');
case 4:
$date = array($year . '-10-01', $year . '-12-31');
break;
return array($year . '-10-01', $year . '-12-31');
}
}
return $date;
}
/**
* Returns a UNIX timestamp from a textual datetime description. Wrapper for PHP function strtotime().
@ -616,8 +610,7 @@ class CakeTime {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public static function toAtom($dateString, $timezone = null) {
$date = self::fromString($dateString, $timezone);
return date('Y-m-d\TH:i:s\Z', $date);
return date('Y-m-d\TH:i:s\Z', self::fromString($dateString, $timezone));
}
/**
@ -631,10 +624,12 @@ class CakeTime {
public static function toRSS($dateString, $timezone = null) {
$date = self::fromString($dateString, $timezone);
if (!is_null($timezone)) {
if (is_numeric($timezone)) {
if (is_null($timezone)) {
return date("r", $date);
}
$userOffset = $timezone;
} else {
if (!is_numeric($timezone)) {
if (!is_object($timezone)) {
$timezone = new DateTimeZone($timezone);
}
@ -642,17 +637,15 @@ class CakeTime {
$currentDate->setTimezone($timezone);
$userOffset = $timezone->getOffset($currentDate) / 60 / 60;
}
if ($userOffset == 0) {
$timezone = '+0000';
} else {
if ($userOffset != 0) {
$hours = (int)floor(abs($userOffset));
$minutes = (int)(fmod(abs($userOffset), $hours) * 60);
$timezone = ($userOffset < 0 ? '-' : '+') . str_pad($hours, 2, '0', STR_PAD_LEFT) . str_pad($minutes, 2, '0', STR_PAD_LEFT);
}
return date('D, d M Y H:i:s', $date) . ' ' . $timezone;
}
return date("r", $date);
}
/**
* Returns either a relative date or a formatted date depending
@ -729,12 +722,11 @@ class CakeTime {
$inSeconds = self::fromString($dateTime, $timezone);
$backwards = ($inSeconds > $now);
$futureTime = $now;
$pastTime = $inSeconds;
if ($backwards) {
$futureTime = $inSeconds;
$pastTime = $now;
} else {
$futureTime = $now;
$pastTime = $inSeconds;
}
$diff = $futureTime - $pastTime;
@ -800,12 +792,17 @@ class CakeTime {
$diff = $diff - ($minutes * 60);
$seconds = $diff;
}
$relativeDate = '';
$diff = $futureTime - $pastTime;
if ($diff == 0) {
return __d('cake', 'just now', 'just now');
}
if ($diff > abs($now - self::fromString($end))) {
$relativeDate = __d('cake', 'on %s', date($format, $inSeconds));
} else {
return __d('cake', 'on %s', date($format, $inSeconds));
}
$f = $accuracy['second'];
if ($years > 0) {
$f = $accuracy['year'];
} elseif (abs($months) > 0) {
@ -818,29 +815,37 @@ class CakeTime {
$f = $accuracy['hour'];
} elseif (abs($minutes) > 0) {
$f = $accuracy['minute'];
} else {
$f = $accuracy['second'];
}
$f = str_replace(array('year', 'month', 'week', 'day', 'hour', 'minute', 'second'), array(1, 2, 3, 4, 5, 6, 7), $f);
$relativeDate .= $f >= 1 && $years > 0 ? ($relativeDate ? ', ' : '') . __dn('cake', '%d year', '%d years', $years, $years) : '';
$relativeDate .= $f >= 2 && $months > 0 ? ($relativeDate ? ', ' : '') . __dn('cake', '%d month', '%d months', $months, $months) : '';
$relativeDate .= $f >= 3 && $weeks > 0 ? ($relativeDate ? ', ' : '') . __dn('cake', '%d week', '%d weeks', $weeks, $weeks) : '';
$relativeDate .= $f >= 4 && $days > 0 ? ($relativeDate ? ', ' : '') . __dn('cake', '%d day', '%d days', $days, $days) : '';
$relativeDate .= $f >= 5 && $hours > 0 ? ($relativeDate ? ', ' : '') . __dn('cake', '%d hour', '%d hours', $hours, $hours) : '';
$relativeDate .= $f >= 6 && $minutes > 0 ? ($relativeDate ? ', ' : '') . __dn('cake', '%d minute', '%d minutes', $minutes, $minutes) : '';
$relativeDate .= $f >= 7 && $seconds > 0 ? ($relativeDate ? ', ' : '') . __dn('cake', '%d second', '%d seconds', $seconds, $seconds) : '';
$relativeDate = '';
if ($f >= 1 && $years > 0) {
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d year', '%d years', $years, $years);
}
if ($f >= 2 && $months > 0) {
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d month', '%d months', $months, $months);
}
if ($f >= 3 && $weeks > 0) {
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d week', '%d weeks', $weeks, $weeks);
}
if ($f >= 4 && $days > 0) {
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d day', '%d days', $days, $days);
}
if ($f >= 5 && $hours > 0) {
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d hour', '%d hours', $hours, $hours);
}
if ($f >= 6 && $minutes > 0) {
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d minute', '%d minutes', $minutes, $minutes);
}
if ($f >= 7 && $seconds > 0) {
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d second', '%d seconds', $seconds, $seconds);
}
if (!$backwards) {
$relativeDate = __d('cake', '%s ago', $relativeDate);
}
return __d('cake', '%s ago', $relativeDate);
}
// If now
if ($diff == 0) {
$relativeDate = __d('cake', 'just now', 'just now');
}
return $relativeDate;
}
@ -863,10 +868,7 @@ class CakeTime {
$date = self::fromString($dateString, $timezone);
$interval = self::fromString('-' . $timeInterval);
if ($date >= $interval && $date <= time()) {
return true;
}
return false;
return $date >= $interval && $date <= time();
}
/**
@ -888,10 +890,7 @@ class CakeTime {
$date = self::fromString($dateString, $timezone);
$interval = self::fromString('+' . $timeInterval);
if ($date <= $interval && $date >= time()) {
return true;
}
return false;
return $date <= $interval && $date >= time();
}
/**
@ -902,18 +901,18 @@ class CakeTime {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public static function gmt($dateString = null) {
$time = time();
if ($dateString != null) {
$time = self::fromString($dateString);
} else {
$time = time();
}
$hour = intval(date("G", $time));
$minute = intval(date("i", $time));
$second = intval(date("s", $time));
$month = intval(date("n", $time));
$day = intval(date("j", $time));
$year = intval(date("Y", $time));
return gmmktime($hour, $minute, $second, $month, $day, $year);
return gmmktime(
intval(date('G', $time)),
intval(date('i', $time)),
intval(date('s', $time)),
intval(date('n', $time)),
intval(date('j', $time)),
intval(date('Y', $time))
);
}
/**
@ -943,7 +942,10 @@ class CakeTime {
public static function format($date, $format = null, $default = false, $timezone = null) {
//Backwards compatible params order
$time = self::fromString($format, $timezone);
$_time = is_numeric($time) ? false : self::fromString($date, $timezone);
$_time = false;
if (!is_numeric($time)) {
$_time = self::fromString($date, $timezone);
}
if (is_numeric($_time) && $time === false) {
return self::i18nFormat($_time, $format, $default, $timezone);
@ -973,8 +975,7 @@ class CakeTime {
if (empty($format)) {
$format = '%x';
}
$format = self::convertSpecifiers($format, $date);
return self::_strftime($format, $date);
return self::_strftime(self::convertSpecifiers($format, $date), $date);
}
/**
@ -1025,9 +1026,8 @@ class CakeTime {
}
}
return $return;
} else {
return array_combine($identifiers, $identifiers);
}
return array_combine($identifiers, $identifiers);
}
/**