Skip to content
This repository
Browse code

Merge branch 'simplify-cake-time' into 2.3

Conflicts:
	lib/Cake/Utility/CakeTime.php
  • Loading branch information...
commit ec54fa57c5bd0f7b78ae0c8293d1b96a6fa9f0fe 2 parents 51e0715 + 95b895b
Mark Story authored September 13, 2012

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

  1. 257  lib/Cake/Utility/CakeTime.php
257  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,20 +468,21 @@ 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
 /**
473 476
  * Returns true if given datetime string is within this month
  477
+ *
474 478
  * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
475 479
  * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object
476 480
  * @return boolean True if datetime string is within current month
477 481
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
478 482
  */
479 483
 	public static function isThisMonth($dateString, $timezone = null) {
480  
-		$date = self::fromString($dateString, $timezone);
481  
-		return date('m Y', $date) == date('m Y', time());
  484
+		$timestamp = self::fromString($dateString, $timezone);
  485
+		return date('m Y', $timestamp) == date('m Y', time());
482 486
 	}
483 487
 
484 488
 /**
@@ -490,8 +494,8 @@ public static function isThisMonth($dateString, $timezone = null) {
490 494
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
491 495
  */
492 496
 	public static function isThisYear($dateString, $timezone = null) {
493  
-		$date = self::fromString($dateString, $timezone);
494  
-		return date('Y', $date) == date('Y', time());
  497
+		$timestamp = self::fromString($dateString, $timezone);
  498
+		return date('Y', $timestamp) == date('Y', time());
495 499
 	}
496 500
 
497 501
 /**
@@ -504,8 +508,8 @@ public static function isThisYear($dateString, $timezone = null) {
504 508
  *
505 509
  */
506 510
 	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'));
  511
+		$timestamp = self::fromString($dateString, $timezone);
  512
+		return date('Y-m-d', $timestamp) == date('Y-m-d', strtotime('yesterday'));
509 513
 	}
510 514
 
511 515
 /**
@@ -517,8 +521,8 @@ public static function wasYesterday($dateString, $timezone = null) {
517 521
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
518 522
  */
519 523
 	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'));
  524
+		$timestamp = self::fromString($dateString, $timezone);
  525
+		return date('Y-m-d', $timestamp) == date('Y-m-d', strtotime('tomorrow'));
522 526
 	}
523 527
 
524 528
 /**
@@ -532,30 +536,21 @@ public static function isTomorrow($dateString, $timezone = null) {
532 536
 	public static function toQuarter($dateString, $range = false) {
533 537
 		$time = self::fromString($dateString);
534 538
 		$date = ceil(date('m', $time) / 3);
535  
-
536  
-		if ($range === true) {
537  
-			$range = 'Y-m-d';
  539
+		if ($range === false) {
  540
+			return $date;
538 541
 		}
539 542
 
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  
-			}
  543
+		$year = date('Y', $time);
  544
+		switch ($date) {
  545
+			case 1:
  546
+				return array($year . '-01-01', $year . '-03-31');
  547
+			case 2:
  548
+				return array($year . '-04-01', $year . '-06-30');
  549
+			case 3:
  550
+				return array($year . '-07-01', $year . '-09-30');
  551
+			case 4:
  552
+				return array($year . '-10-01', $year . '-12-31');
557 553
 		}
558  
-		return $date;
559 554
 	}
560 555
 
561 556
 /**
@@ -616,8 +611,7 @@ public static function toServer($dateString, $timezone = null, $format = 'Y-m-d
616 611
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
617 612
  */
618 613
 	public static function toAtom($dateString, $timezone = null) {
619  
-		$date = self::fromString($dateString, $timezone);
620  
-		return date('Y-m-d\TH:i:s\Z', $date);
  614
+		return date('Y-m-d\TH:i:s\Z', self::fromString($dateString, $timezone));
621 615
 	}
622 616
 
623 617
 /**
@@ -631,27 +625,27 @@ public static function toAtom($dateString, $timezone = null) {
631 625
 	public static function toRSS($dateString, $timezone = null) {
632 626
 		$date = self::fromString($dateString, $timezone);
633 627
 
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);
  628
+		if (is_null($timezone)) {
  629
+			return date("r", $date);
  630
+		}
  631
+
  632
+		$userOffset = $timezone;
  633
+		if (!is_numeric($timezone)) {
  634
+			if (!is_object($timezone)) {
  635
+				$timezone = new DateTimeZone($timezone);
651 636
 			}
652  
-			return date('D, d M Y H:i:s', $date) . ' ' . $timezone;
  637
+			$currentDate = new DateTime('@' . $date);
  638
+			$currentDate->setTimezone($timezone);
  639
+			$userOffset = $timezone->getOffset($currentDate) / 60 / 60;
  640
+		}
  641
+
  642
+		$timezone = '+0000';
  643
+		if ($userOffset != 0) {
  644
+			$hours = (int)floor(abs($userOffset));
  645
+			$minutes = (int)(fmod(abs($userOffset), $hours) * 60);
  646
+			$timezone = ($userOffset < 0 ? '-' : '+') . str_pad($hours, 2, '0', STR_PAD_LEFT) . str_pad($minutes, 2, '0', STR_PAD_LEFT);
653 647
 		}
654  
-		return date("r", $date);
  648
+		return date('D, d M Y H:i:s', $date) . ' ' . $timezone;
655 649
 	}
656 650
 
657 651
 /**
@@ -729,12 +723,11 @@ public static function timeAgoInWords($dateTime, $options = array()) {
729 723
 		$inSeconds = self::fromString($dateTime, $timezone);
730 724
 		$backwards = ($inSeconds > $now);
731 725
 
  726
+		$futureTime = $now;
  727
+		$pastTime = $inSeconds;
732 728
 		if ($backwards) {
733 729
 			$futureTime = $inSeconds;
734 730
 			$pastTime = $now;
735  
-		} else {
736  
-			$futureTime = $now;
737  
-			$pastTime = $inSeconds;
738 731
 		}
739 732
 		$diff = $futureTime - $pastTime;
740 733
 
@@ -800,47 +793,60 @@ public static function timeAgoInWords($dateTime, $options = array()) {
800 793
 			$diff = $diff - ($minutes * 60);
801 794
 			$seconds = $diff;
802 795
 		}
803  
-		$relativeDate = '';
804 796
 		$diff = $futureTime - $pastTime;
805 797
 
  798
+		if ($diff == 0) {
  799
+			return __d('cake', 'just now', 'just now');
  800
+		}
  801
+
806 802
 		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  
-			}
  803
+			return __d('cake', 'on %s', date($format, $inSeconds));
  804
+		}
824 805
 
825  
-			$f = str_replace(array('year', 'month', 'week', 'day', 'hour', 'minute', 'second'), array(1, 2, 3, 4, 5, 6, 7), $f);
  806
+		$f = $accuracy['second'];
  807
+		if ($years > 0) {
  808
+			$f = $accuracy['year'];
  809
+		} elseif (abs($months) > 0) {
  810
+			$f = $accuracy['month'];
  811
+		} elseif (abs($weeks) > 0) {
  812
+			$f = $accuracy['week'];
  813
+		} elseif (abs($days) > 0) {
  814
+			$f = $accuracy['day'];
  815
+		} elseif (abs($hours) > 0) {
  816
+			$f = $accuracy['hour'];
  817
+		} elseif (abs($minutes) > 0) {
  818
+			$f = $accuracy['minute'];
  819
+		}
826 820
 
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) : '';
  821
+		$f = str_replace(array('year', 'month', 'week', 'day', 'hour', 'minute', 'second'), array(1, 2, 3, 4, 5, 6, 7), $f);
834 822
 
835  
-			if (!$backwards) {
836  
-				$relativeDate = __d('cake', '%s ago', $relativeDate);
837  
-			}
  823
+		$relativeDate = '';
  824
+		if ($f >= 1 && $years > 0) {
  825
+			$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d year', '%d years', $years, $years);
  826
+		}
  827
+		if ($f >= 2 && $months > 0) {
  828
+			$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d month', '%d months', $months, $months);
  829
+		}
  830
+		if ($f >= 3 && $weeks > 0) {
  831
+			$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d week', '%d weeks', $weeks, $weeks);
  832
+		}
  833
+		if ($f >= 4 && $days > 0) {
  834
+			$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d day', '%d days', $days, $days);
  835
+		}
  836
+		if ($f >= 5 && $hours > 0) {
  837
+			$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d hour', '%d hours', $hours, $hours);
  838
+		}
  839
+		if ($f >= 6 && $minutes > 0) {
  840
+			$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d minute', '%d minutes', $minutes, $minutes);
  841
+		}
  842
+		if ($f >= 7 && $seconds > 0) {
  843
+			$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d second', '%d seconds', $seconds, $seconds);
838 844
 		}
839 845
 
840  
-		// If now
841  
-		if ($diff == 0) {
842  
-			$relativeDate = __d('cake', 'just now', 'just now');
  846
+		if (!$backwards) {
  847
+			return __d('cake', '%s ago', $relativeDate);
843 848
 		}
  849
+
844 850
 		return $relativeDate;
845 851
 	}
846 852
 
@@ -863,10 +869,7 @@ public static function wasWithinLast($timeInterval, $dateString, $timezone = nul
863 869
 		$date = self::fromString($dateString, $timezone);
864 870
 		$interval = self::fromString('-' . $timeInterval);
865 871
 
866  
-		if ($date >= $interval && $date <= time()) {
867  
-			return true;
868  
-		}
869  
-		return false;
  872
+		return $date >= $interval && $date <= time();
870 873
 	}
871 874
 
872 875
 /**
@@ -888,10 +891,7 @@ public static function isWithinNext($timeInterval, $dateString, $timezone = null
888 891
 		$date = self::fromString($dateString, $timezone);
889 892
 		$interval = self::fromString('+' . $timeInterval);
890 893
 
891  
-		if ($date <= $interval && $date >= time()) {
892  
-			return true;
893  
-		}
894  
-		return false;
  894
+		return $date <= $interval && $date >= time();
895 895
 	}
896 896
 
897 897
 /**
@@ -902,18 +902,18 @@ public static function isWithinNext($timeInterval, $dateString, $timezone = null
902 902
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
903 903
  */
904 904
 	public static function gmt($dateString = null) {
  905
+		$time = time();
905 906
 		if ($dateString != null) {
906 907
 			$time = self::fromString($dateString);
907  
-		} else {
908  
-			$time = time();
909 908
 		}
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);
  909
+		return gmmktime(
  910
+			intval(date('G', $time)),
  911
+			intval(date('i', $time)),
  912
+			intval(date('s', $time)),
  913
+			intval(date('n', $time)),
  914
+			intval(date('j', $time)),
  915
+			intval(date('Y', $time))
  916
+		);
917 917
 	}
918 918
 
919 919
 /**
@@ -943,7 +943,10 @@ public static function gmt($dateString = null) {
943 943
 	public static function format($date, $format = null, $default = false, $timezone = null) {
944 944
 		//Backwards compatible params order
945 945
 		$time = self::fromString($format, $timezone);
946  
-		$_time = is_numeric($time) ? false : self::fromString($date, $timezone);
  946
+		$_time = false;
  947
+		if (!is_numeric($time)) {
  948
+			$_time = self::fromString($date, $timezone);
  949
+		}
947 950
 
948 951
 		if (is_numeric($_time) && $time === false) {
949 952
 			return self::i18nFormat($_time, $format, $default, $timezone);
@@ -973,8 +976,7 @@ public static function i18nFormat($date, $format = null, $default = false, $time
973 976
 		if (empty($format)) {
974 977
 			$format = '%x';
975 978
 		}
976  
-		$format = self::convertSpecifiers($format, $date);
977  
-		return self::_strftime($format, $date);
  979
+		return self::_strftime(self::convertSpecifiers($format, $date), $date);
978 980
 	}
979 981
 
980 982
 /**
@@ -1025,9 +1027,8 @@ public static function listTimezones($filter = null, $country = null, $group = t
1025 1027
 				}
1026 1028
 			}
1027 1029
 			return $return;
1028  
-		} else {
1029  
-			return array_combine($identifiers, $identifiers);
1030 1030
 		}
  1031
+		return array_combine($identifiers, $identifiers);
1031 1032
 	}
1032 1033
 
1033 1034
 /**

0 notes on commit ec54fa5

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