Skip to content
This repository
Browse code

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...
commit 95b895b19a42150410294d28c4743ed012ae7e4c 1 parent b227ff4
Carl Sutton authored September 12, 2012

Showing 1 changed file with 128 additions and 128 deletions. Show diff stats Hide diff stats

  1. 256  lib/Cake/Utility/CakeTime.php
256  lib/Cake/Utility/CakeTime.php
@@ -355,8 +355,7 @@ public static function nice($dateString = null, $timezone = null, $format = null
355 355
 		if (!$format) {
356 356
 			$format = self::$niceFormat;
357 357
 		}
358  
-		$format = self::convertSpecifiers($format, $date);
359  
-		return self::_strftime($format, $date);
  358
+		return self::_strftime(self::convertSpecifiers($format, $date), $date);
360 359
 	}
361 360
 
362 361
 /**
@@ -380,7 +379,15 @@ public static function niceShort($dateString = null, $timezone = null) {
380 379
 		}
381 380
 		$date = self::fromString($dateString, $timezone);
382 381
 
383  
-		$y = self::isThisYear($date) ? '' : ' %Y';
  382
+		if (self::isToday($dateString, $timezone)) {
  383
+			return __d('cake', 'Today, %s', self::_strftime("%H:%M", $date));
  384
+		}
  385
+		if (self::wasYesterday($dateString, $timezone)) {
  386
+			return __d('cake', 'Yesterday, %s', self::_strftime("%H:%M", $date));
  387
+		}
  388
+		if (self::isTomorrow($dateString, $timezone)) {
  389
+			return __d('cake', 'Tomorrow, %s', self::_strftime("%H:%M", $date));
  390
+		}
384 391
 
385 392
 		$d = self::_strftime("%w", $date);
386 393
 		$day = array(
@@ -392,22 +399,18 @@ public static function niceShort($dateString = null, $timezone = null) {
392 399
 			__d('cake', 'Friday'),
393 400
 			__d('cake', 'Saturday')
394 401
 		);
  402
+		if (self::wasWithinLast('7 days', $dateString, $timezone)) {
  403
+			return sprintf('%s %s', $day[$d], self::_strftime(self::$niceShortFormat, $date));
  404
+		}
  405
+		if (self::isWithinNext('7 days', $dateString, $timezone)) {
  406
+			return __d('cake', 'On %s %s', $day[$d], self::_strftime(self::$niceShortFormat, $date));
  407
+		}
395 408
 
396  
-		if (self::isToday($dateString, $timezone)) {
397  
-			$ret = __d('cake', 'Today, %s', self::_strftime("%H:%M", $date));
398  
-		} elseif (self::wasYesterday($dateString, $timezone)) {
399  
-			$ret = __d('cake', 'Yesterday, %s', self::_strftime("%H:%M", $date));
400  
-		} elseif (self::isTomorrow($dateString, $timezone)) {
401  
-			$ret = __d('cake', 'Tomorrow, %s', self::_strftime("%H:%M", $date));
402  
-		} elseif (self::wasWithinLast('7 days', $dateString, $timezone)) {
403  
-			$ret = sprintf('%s %s', $day[$d], self::_strftime(self::$niceShortFormat, $date));
404  
-		} elseif (self::isWithinNext('7 days', $dateString, $timezone)) {
405  
-			$ret = __d('cake', 'On %s %s', $day[$d], self::_strftime(self::$niceShortFormat, $date));
406  
-		} else {
407  
-			$format = self::convertSpecifiers("%b %eS{$y}, %H:%M", $date);
408  
-			$ret = self::_strftime($format, $date);
  409
+		$y = '';
  410
+		if (!self::isThisYear($date)) {
  411
+			$y = ' %Y';
409 412
 		}
410  
-		return $ret;
  413
+		return self::_strftime(self::convertSpecifiers("%b %eS{$y}, %H:%M", $date), $date);
411 414
 	}
412 415
 
413 416
 /**
@@ -452,8 +455,8 @@ public static function dayAsSql($dateString, $fieldName, $timezone = null) {
452 455
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
453 456
  */
454 457
 	public static function isToday($dateString, $timezone = null) {
455  
-		$date = self::fromString($dateString, $timezone);
456  
-		return date('Y-m-d', $date) == date('Y-m-d', time());
  458
+		$timestamp = self::fromString($dateString, $timezone);
  459
+		return date('Y-m-d', $timestamp) == date('Y-m-d', time());
457 460
 	}
458 461
 
459 462
 /**
@@ -465,8 +468,8 @@ public static function isToday($dateString, $timezone = null) {
465 468
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
466 469
  */
467 470
 	public static function isThisWeek($dateString, $timezone = null) {
468  
-		$date = self::fromString($dateString, $timezone);
469  
-		return date('W o', $date) == date('W o', time());
  471
+		$timestamp = self::fromString($dateString, $timezone);
  472
+		return date('W o', $timestamp) == date('W o', time());
470 473
 	}
471 474
 
472 475
 /**
@@ -477,8 +480,8 @@ public static function isThisWeek($dateString, $timezone = null) {
477 480
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
478 481
  */
479 482
 	public static function isThisMonth($dateString, $timezone = null) {
480  
-		$date = self::fromString($dateString);
481  
-		return date('m Y', $date) == date('m Y', time());
  483
+		$timestamp = self::fromString($dateString);
  484
+		return date('m Y', $timestamp) == date('m Y', time());
482 485
 	}
483 486
 
484 487
 /**
@@ -490,8 +493,8 @@ public static function isThisMonth($dateString, $timezone = null) {
490 493
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
491 494
  */
492 495
 	public static function isThisYear($dateString, $timezone = null) {
493  
-		$date = self::fromString($dateString, $timezone);
494  
-		return date('Y', $date) == date('Y', time());
  496
+		$timestamp = self::fromString($dateString, $timezone);
  497
+		return date('Y', $timestamp) == date('Y', time());
495 498
 	}
496 499
 
497 500
 /**
@@ -504,8 +507,8 @@ public static function isThisYear($dateString, $timezone = null) {
504 507
  *
505 508
  */
506 509
 	public static function wasYesterday($dateString, $timezone = null) {
507  
-		$date = self::fromString($dateString, $timezone);
508  
-		return date('Y-m-d', $date) == date('Y-m-d', strtotime('yesterday'));
  510
+		$timestamp = self::fromString($dateString, $timezone);
  511
+		return date('Y-m-d', $timestamp) == date('Y-m-d', strtotime('yesterday'));
509 512
 	}
510 513
 
511 514
 /**
@@ -517,8 +520,8 @@ public static function wasYesterday($dateString, $timezone = null) {
517 520
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
518 521
  */
519 522
 	public static function isTomorrow($dateString, $timezone = null) {
520  
-		$date = self::fromString($dateString, $timezone);
521  
-		return date('Y-m-d', $date) == date('Y-m-d', strtotime('tomorrow'));
  523
+		$timestamp = self::fromString($dateString, $timezone);
  524
+		return date('Y-m-d', $timestamp) == date('Y-m-d', strtotime('tomorrow'));
522 525
 	}
523 526
 
524 527
 /**
@@ -532,30 +535,21 @@ public static function isTomorrow($dateString, $timezone = null) {
532 535
 	public static function toQuarter($dateString, $range = false) {
533 536
 		$time = self::fromString($dateString);
534 537
 		$date = ceil(date('m', $time) / 3);
535  
-
536  
-		if ($range === true) {
537  
-			$range = 'Y-m-d';
  538
+		if ($range === false) {
  539
+			return $date;
538 540
 		}
539 541
 
540  
-		if ($range !== false) {
541  
-			$year = date('Y', $time);
542  
-
543  
-			switch ($date) {
544  
-				case 1:
545  
-					$date = array($year . '-01-01', $year . '-03-31');
546  
-					break;
547  
-				case 2:
548  
-					$date = array($year . '-04-01', $year . '-06-30');
549  
-					break;
550  
-				case 3:
551  
-					$date = array($year . '-07-01', $year . '-09-30');
552  
-					break;
553  
-				case 4:
554  
-					$date = array($year . '-10-01', $year . '-12-31');
555  
-					break;
556  
-			}
  542
+		$year = date('Y', $time);
  543
+		switch ($date) {
  544
+			case 1:
  545
+				return array($year . '-01-01', $year . '-03-31');
  546
+			case 2:
  547
+				return array($year . '-04-01', $year . '-06-30');
  548
+			case 3:
  549
+				return array($year . '-07-01', $year . '-09-30');
  550
+			case 4:
  551
+				return array($year . '-10-01', $year . '-12-31');
557 552
 		}
558  
-		return $date;
559 553
 	}
560 554
 
561 555
 /**
@@ -616,8 +610,7 @@ public static function toServer($dateString, $timezone = null, $format = 'Y-m-d
616 610
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
617 611
  */
618 612
 	public static function toAtom($dateString, $timezone = null) {
619  
-		$date = self::fromString($dateString, $timezone);
620  
-		return date('Y-m-d\TH:i:s\Z', $date);
  613
+		return date('Y-m-d\TH:i:s\Z', self::fromString($dateString, $timezone));
621 614
 	}
622 615
 
623 616
 /**
@@ -631,27 +624,27 @@ public static function toAtom($dateString, $timezone = null) {
631 624
 	public static function toRSS($dateString, $timezone = null) {
632 625
 		$date = self::fromString($dateString, $timezone);
633 626
 
634  
-		if (!is_null($timezone)) {
635  
-			if (is_numeric($timezone)) {
636  
-				$userOffset = $timezone;
637  
-			} else {
638  
-				if (!is_object($timezone)) {
639  
-					$timezone = new DateTimeZone($timezone);
640  
-				}
641  
-				$currentDate = new DateTime('@' . $date);
642  
-				$currentDate->setTimezone($timezone);
643  
-				$userOffset = $timezone->getOffset($currentDate) / 60 / 60;
644  
-			}
645  
-			if ($userOffset == 0) {
646  
-				$timezone = '+0000';
647  
-			} else {
648  
-				$hours = (int)floor(abs($userOffset));
649  
-				$minutes = (int)(fmod(abs($userOffset), $hours) * 60);
650  
-				$timezone = ($userOffset < 0 ? '-' : '+') . str_pad($hours, 2, '0', STR_PAD_LEFT) . str_pad($minutes, 2, '0', STR_PAD_LEFT);
  627
+		if (is_null($timezone)) {
  628
+			return date("r", $date);
  629
+		}
  630
+
  631
+		$userOffset = $timezone;
  632
+		if (!is_numeric($timezone)) {
  633
+			if (!is_object($timezone)) {
  634
+				$timezone = new DateTimeZone($timezone);
651 635
 			}
652  
-			return date('D, d M Y H:i:s', $date) . ' ' . $timezone;
  636
+			$currentDate = new DateTime('@' . $date);
  637
+			$currentDate->setTimezone($timezone);
  638
+			$userOffset = $timezone->getOffset($currentDate) / 60 / 60;
653 639
 		}
654  
-		return date("r", $date);
  640
+
  641
+		$timezone = '+0000';
  642
+		if ($userOffset != 0) {
  643
+			$hours = (int)floor(abs($userOffset));
  644
+			$minutes = (int)(fmod(abs($userOffset), $hours) * 60);
  645
+			$timezone = ($userOffset < 0 ? '-' : '+') . str_pad($hours, 2, '0', STR_PAD_LEFT) . str_pad($minutes, 2, '0', STR_PAD_LEFT);
  646
+		}
  647
+		return date('D, d M Y H:i:s', $date) . ' ' . $timezone;
655 648
 	}
656 649
 
657 650
 /**
@@ -729,12 +722,11 @@ public static function timeAgoInWords($dateTime, $options = array()) {
729 722
 		$inSeconds = self::fromString($dateTime, $timezone);
730 723
 		$backwards = ($inSeconds > $now);
731 724
 
  725
+		$futureTime = $now;
  726
+		$pastTime = $inSeconds;
732 727
 		if ($backwards) {
733 728
 			$futureTime = $inSeconds;
734 729
 			$pastTime = $now;
735  
-		} else {
736  
-			$futureTime = $now;
737  
-			$pastTime = $inSeconds;
738 730
 		}
739 731
 		$diff = $futureTime - $pastTime;
740 732
 
@@ -800,47 +792,60 @@ public static function timeAgoInWords($dateTime, $options = array()) {
800 792
 			$diff = $diff - ($minutes * 60);
801 793
 			$seconds = $diff;
802 794
 		}
803  
-		$relativeDate = '';
804 795
 		$diff = $futureTime - $pastTime;
805 796
 
  797
+		if ($diff == 0) {
  798
+			return __d('cake', 'just now', 'just now');
  799
+		}
  800
+
806 801
 		if ($diff > abs($now - self::fromString($end))) {
807  
-			$relativeDate = __d('cake', 'on %s', date($format, $inSeconds));
808  
-		} else {
809  
-			if ($years > 0) {
810  
-				$f = $accuracy['year'];
811  
-			} elseif (abs($months) > 0) {
812  
-				$f = $accuracy['month'];
813  
-			} elseif (abs($weeks) > 0) {
814  
-				$f = $accuracy['week'];
815  
-			} elseif (abs($days) > 0) {
816  
-				$f = $accuracy['day'];
817  
-			} elseif (abs($hours) > 0) {
818  
-				$f = $accuracy['hour'];
819  
-			} elseif (abs($minutes) > 0) {
820  
-				$f = $accuracy['minute'];
821  
-			} else {
822  
-				$f = $accuracy['second'];
823  
-			}
  802
+			return __d('cake', 'on %s', date($format, $inSeconds));
  803
+		}
824 804
 
825  
-			$f = str_replace(array('year', 'month', 'week', 'day', 'hour', 'minute', 'second'), array(1, 2, 3, 4, 5, 6, 7), $f);
  805
+		$f = $accuracy['second'];
  806
+		if ($years > 0) {
  807
+			$f = $accuracy['year'];
  808
+		} elseif (abs($months) > 0) {
  809
+			$f = $accuracy['month'];
  810
+		} elseif (abs($weeks) > 0) {
  811
+			$f = $accuracy['week'];
  812
+		} elseif (abs($days) > 0) {
  813
+			$f = $accuracy['day'];
  814
+		} elseif (abs($hours) > 0) {
  815
+			$f = $accuracy['hour'];
  816
+		} elseif (abs($minutes) > 0) {
  817
+			$f = $accuracy['minute'];
  818
+		}
826 819
 
827  
-			$relativeDate .= $f >= 1 && $years > 0 ? ($relativeDate ? ', ' : '') . __dn('cake', '%d year', '%d years', $years, $years) : '';
828  
-			$relativeDate .= $f >= 2 && $months > 0 ? ($relativeDate ? ', ' : '') . __dn('cake', '%d month', '%d months', $months, $months) : '';
829  
-			$relativeDate .= $f >= 3 && $weeks > 0 ? ($relativeDate ? ', ' : '') . __dn('cake', '%d week', '%d weeks', $weeks, $weeks) : '';
830  
-			$relativeDate .= $f >= 4 && $days > 0 ? ($relativeDate ? ', ' : '') . __dn('cake', '%d day', '%d days', $days, $days) : '';
831  
-			$relativeDate .= $f >= 5 && $hours > 0 ? ($relativeDate ? ', ' : '') . __dn('cake', '%d hour', '%d hours', $hours, $hours) : '';
832  
-			$relativeDate .= $f >= 6 && $minutes > 0 ? ($relativeDate ? ', ' : '') . __dn('cake', '%d minute', '%d minutes', $minutes, $minutes) : '';
833  
-			$relativeDate .= $f >= 7 && $seconds > 0 ? ($relativeDate ? ', ' : '') . __dn('cake', '%d second', '%d seconds', $seconds, $seconds) : '';
  820
+		$f = str_replace(array('year', 'month', 'week', 'day', 'hour', 'minute', 'second'), array(1, 2, 3, 4, 5, 6, 7), $f);
834 821
 
835  
-			if (!$backwards) {
836  
-				$relativeDate = __d('cake', '%s ago', $relativeDate);
837  
-			}
  822
+		$relativeDate = '';
  823
+		if ($f >= 1 && $years > 0) {
  824
+			$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d year', '%d years', $years, $years);
  825
+		}
  826
+		if ($f >= 2 && $months > 0) {
  827
+			$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d month', '%d months', $months, $months);
  828
+		}
  829
+		if ($f >= 3 && $weeks > 0) {
  830
+			$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d week', '%d weeks', $weeks, $weeks);
  831
+		}
  832
+		if ($f >= 4 && $days > 0) {
  833
+			$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d day', '%d days', $days, $days);
  834
+		}
  835
+		if ($f >= 5 && $hours > 0) {
  836
+			$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d hour', '%d hours', $hours, $hours);
  837
+		}
  838
+		if ($f >= 6 && $minutes > 0) {
  839
+			$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d minute', '%d minutes', $minutes, $minutes);
  840
+		}
  841
+		if ($f >= 7 && $seconds > 0) {
  842
+			$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d second', '%d seconds', $seconds, $seconds);
838 843
 		}
839 844
 
840  
-		// If now
841  
-		if ($diff == 0) {
842  
-			$relativeDate = __d('cake', 'just now', 'just now');
  845
+		if (!$backwards) {
  846
+			return __d('cake', '%s ago', $relativeDate);
843 847
 		}
  848
+
844 849
 		return $relativeDate;
845 850
 	}
846 851
 
@@ -863,10 +868,7 @@ public static function wasWithinLast($timeInterval, $dateString, $timezone = nul
863 868
 		$date = self::fromString($dateString, $timezone);
864 869
 		$interval = self::fromString('-' . $timeInterval);
865 870
 
866  
-		if ($date >= $interval && $date <= time()) {
867  
-			return true;
868  
-		}
869  
-		return false;
  871
+		return $date >= $interval && $date <= time();
870 872
 	}
871 873
 
872 874
 /**
@@ -888,10 +890,7 @@ public static function isWithinNext($timeInterval, $dateString, $timezone = null
888 890
 		$date = self::fromString($dateString, $timezone);
889 891
 		$interval = self::fromString('+' . $timeInterval);
890 892
 
891  
-		if ($date <= $interval && $date >= time()) {
892  
-			return true;
893  
-		}
894  
-		return false;
  893
+		return $date <= $interval && $date >= time();
895 894
 	}
896 895
 
897 896
 /**
@@ -902,18 +901,18 @@ public static function isWithinNext($timeInterval, $dateString, $timezone = null
902 901
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
903 902
  */
904 903
 	public static function gmt($dateString = null) {
  904
+		$time = time();
905 905
 		if ($dateString != null) {
906 906
 			$time = self::fromString($dateString);
907  
-		} else {
908  
-			$time = time();
909 907
 		}
910  
-		$hour = intval(date("G", $time));
911  
-		$minute = intval(date("i", $time));
912  
-		$second = intval(date("s", $time));
913  
-		$month = intval(date("n", $time));
914  
-		$day = intval(date("j", $time));
915  
-		$year = intval(date("Y", $time));
916  
-		return gmmktime($hour, $minute, $second, $month, $day, $year);
  908
+		return gmmktime(
  909
+			intval(date('G', $time)),
  910
+			intval(date('i', $time)),
  911
+			intval(date('s', $time)),
  912
+			intval(date('n', $time)),
  913
+			intval(date('j', $time)),
  914
+			intval(date('Y', $time))
  915
+		);
917 916
 	}
918 917
 
919 918
 /**
@@ -943,7 +942,10 @@ public static function gmt($dateString = null) {
943 942
 	public static function format($date, $format = null, $default = false, $timezone = null) {
944 943
 		//Backwards compatible params order
945 944
 		$time = self::fromString($format, $timezone);
946  
-		$_time = is_numeric($time) ? false : self::fromString($date, $timezone);
  945
+		$_time = false;
  946
+		if (!is_numeric($time)) {
  947
+			$_time = self::fromString($date, $timezone);
  948
+		}
947 949
 
948 950
 		if (is_numeric($_time) && $time === false) {
949 951
 			return self::i18nFormat($_time, $format, $default, $timezone);
@@ -973,8 +975,7 @@ public static function i18nFormat($date, $format = null, $default = false, $time
973 975
 		if (empty($format)) {
974 976
 			$format = '%x';
975 977
 		}
976  
-		$format = self::convertSpecifiers($format, $date);
977  
-		return self::_strftime($format, $date);
  978
+		return self::_strftime(self::convertSpecifiers($format, $date), $date);
978 979
 	}
979 980
 
980 981
 /**
@@ -1025,9 +1026,8 @@ public static function listTimezones($filter = null, $country = null, $group = t
1025 1026
 				}
1026 1027
 			}
1027 1028
 			return $return;
1028  
-		} else {
1029  
-			return array_combine($identifiers, $identifiers);
1030 1029
 		}
  1030
+		return array_combine($identifiers, $identifiers);
1031 1031
 	}
1032 1032
 
1033 1033
 /**

0 notes on commit 95b895b

Please sign in to comment.
Something went wrong with that request. Please try again.