Permalink
Browse files

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.
  • Loading branch information...
1 parent b227ff4 commit 95b895b19a42150410294d28c4743ed012ae7e4c @dogmatic69 dogmatic69 committed Sep 12, 2012
Showing with 128 additions and 128 deletions.
  1. +128 −128 lib/Cake/Utility/CakeTime.php
View
256 lib/Cake/Utility/CakeTime.php
@@ -355,8 +355,7 @@ public static function nice($dateString = null, $timezone = null, $format = null
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 @@ public static function niceShort($dateString = null, $timezone = null) {
}
$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 @@ public static function niceShort($dateString = null, $timezone = null) {
__d('cake', 'Friday'),
__d('cake', 'Saturday')
);
+ if (self::wasWithinLast('7 days', $dateString, $timezone)) {
+ return sprintf('%s %s', $day[$d], self::_strftime(self::$niceShortFormat, $date));
+ }
+ if (self::isWithinNext('7 days', $dateString, $timezone)) {
+ return __d('cake', 'On %s %s', $day[$d], self::_strftime(self::$niceShortFormat, $date));
+ }
- 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);
+ $y = '';
+ if (!self::isThisYear($date)) {
+ $y = ' %Y';
}
- return $ret;
+ return self::_strftime(self::convertSpecifiers("%b %eS{$y}, %H:%M", $date), $date);
}
/**
@@ -452,8 +455,8 @@ public static function dayAsSql($dateString, $fieldName, $timezone = null) {
* @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 @@ public static function isToday($dateString, $timezone = null) {
* @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 @@ public static function isThisWeek($dateString, $timezone = null) {
* @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 @@ public static function isThisMonth($dateString, $timezone = null) {
* @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 @@ public static function isThisYear($dateString, $timezone = null) {
*
*/
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 @@ public static function wasYesterday($dateString, $timezone = null) {
* @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,30 +535,21 @@ public static function isTomorrow($dateString, $timezone = null) {
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;
- case 2:
- $date = array($year . '-04-01', $year . '-06-30');
- break;
- case 3:
- $date = array($year . '-07-01', $year . '-09-30');
- break;
- case 4:
- $date = array($year . '-10-01', $year . '-12-31');
- break;
- }
+ $year = date('Y', $time);
+ switch ($date) {
+ case 1:
+ return array($year . '-01-01', $year . '-03-31');
+ case 2:
+ return array($year . '-04-01', $year . '-06-30');
+ case 3:
+ return array($year . '-07-01', $year . '-09-30');
+ case 4:
+ return array($year . '-10-01', $year . '-12-31');
}
- return $date;
}
/**
@@ -616,8 +610,7 @@ public static function toServer($dateString, $timezone = null, $format = 'Y-m-d
* @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,27 +624,27 @@ public static function toAtom($dateString, $timezone = null) {
public static function toRSS($dateString, $timezone = null) {
$date = self::fromString($dateString, $timezone);
- if (!is_null($timezone)) {
- if (is_numeric($timezone)) {
- $userOffset = $timezone;
- } else {
- if (!is_object($timezone)) {
- $timezone = new DateTimeZone($timezone);
- }
- $currentDate = new DateTime('@' . $date);
- $currentDate->setTimezone($timezone);
- $userOffset = $timezone->getOffset($currentDate) / 60 / 60;
- }
- if ($userOffset == 0) {
- $timezone = '+0000';
- } else {
- $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);
+ if (is_null($timezone)) {
+ return date("r", $date);
+ }
+
+ $userOffset = $timezone;
+ if (!is_numeric($timezone)) {
+ if (!is_object($timezone)) {
+ $timezone = new DateTimeZone($timezone);
}
- return date('D, d M Y H:i:s', $date) . ' ' . $timezone;
+ $currentDate = new DateTime('@' . $date);
+ $currentDate->setTimezone($timezone);
+ $userOffset = $timezone->getOffset($currentDate) / 60 / 60;
}
- return date("r", $date);
+
+ $timezone = '+0000';
+ 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;
}
/**
@@ -729,12 +722,11 @@ public static function timeAgoInWords($dateTime, $options = array()) {
$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,47 +792,60 @@ public static function timeAgoInWords($dateTime, $options = array()) {
$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 {
- if ($years > 0) {
- $f = $accuracy['year'];
- } elseif (abs($months) > 0) {
- $f = $accuracy['month'];
- } elseif (abs($weeks) > 0) {
- $f = $accuracy['week'];
- } elseif (abs($days) > 0) {
- $f = $accuracy['day'];
- } elseif (abs($hours) > 0) {
- $f = $accuracy['hour'];
- } elseif (abs($minutes) > 0) {
- $f = $accuracy['minute'];
- } else {
- $f = $accuracy['second'];
- }
+ return __d('cake', 'on %s', date($format, $inSeconds));
+ }
- $f = str_replace(array('year', 'month', 'week', 'day', 'hour', 'minute', 'second'), array(1, 2, 3, 4, 5, 6, 7), $f);
+ $f = $accuracy['second'];
+ if ($years > 0) {
+ $f = $accuracy['year'];
+ } elseif (abs($months) > 0) {
+ $f = $accuracy['month'];
+ } elseif (abs($weeks) > 0) {
+ $f = $accuracy['week'];
+ } elseif (abs($days) > 0) {
+ $f = $accuracy['day'];
+ } elseif (abs($hours) > 0) {
+ $f = $accuracy['hour'];
+ } elseif (abs($minutes) > 0) {
+ $f = $accuracy['minute'];
+ }
- $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) : '';
+ $f = str_replace(array('year', 'month', 'week', 'day', 'hour', 'minute', 'second'), array(1, 2, 3, 4, 5, 6, 7), $f);
- if (!$backwards) {
- $relativeDate = __d('cake', '%s ago', $relativeDate);
- }
+ $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 now
- if ($diff == 0) {
- $relativeDate = __d('cake', 'just now', 'just now');
+ if (!$backwards) {
+ return __d('cake', '%s ago', $relativeDate);
}
+
return $relativeDate;
}
@@ -863,10 +868,7 @@ public static function wasWithinLast($timeInterval, $dateString, $timezone = nul
$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 @@ public static function isWithinNext($timeInterval, $dateString, $timezone = null
$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 @@ public static function isWithinNext($timeInterval, $dateString, $timezone = null
* @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 @@ public static function gmt($dateString = null) {
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 @@ public static function i18nFormat($date, $format = null, $default = false, $time
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 @@ public static function listTimezones($filter = null, $country = null, $group = t
}
}
return $return;
- } else {
- return array_combine($identifiers, $identifiers);
}
+ return array_combine($identifiers, $identifiers);
}
/**

0 comments on commit 95b895b

Please sign in to comment.