ソースを参照

incrementDate to work with fuzzy month/year increments/decrements

euromark 12 年 前
コミット
b62c4d91f2

+ 28 - 0
Lib/Utility/TimeLib.php

@@ -355,6 +355,34 @@ class TimeLib extends CakeTime {
 	}
 
 	/**
+	 * Handles month/year increment calculations in a safe way, avoiding the pitfall of "fuzzy" month units.
+	 *
+	 * @param mixed $startDate Either a date string or a DateTime object
+	 * @param int $years Years to increment/decrement
+	 * @param int $months Months to increment/decrement
+	 * @return object DateTime with incremented/decremented month/year values.
+	 */
+	public static function incrementDate($startDate, $years = 0, $months = 0) {
+		if (!is_object($startDate)) {
+			$startDate = new DateTime($startDate);
+		}
+		$startingTimeStamp = $startDate->getTimestamp();
+		// Get the month value of the given date:
+		$monthString = date('Y-m', $startingTimeStamp);
+		// Create a date string corresponding to the 1st of the give month,
+		// making it safe for monthly/yearly calculations:
+		$safeDateString = "first day of $monthString";
+
+		// offset is wrong
+		$months++;
+		// Increment date by given month/year increments:
+		$incrementedDateString = "$safeDateString $months month $years year";
+		$newTimeStamp = strtotime($incrementedDateString);
+		$newDate = DateTime::createFromFormat('U', $newTimeStamp);
+		return $newDate;
+	}
+
+	/**
 	 * get the age bounds (min, max) as timestamp that would result in the given age(s)
 	 * note: expects valid age (> 0 and < 120)
 	 * @param $firstAge

+ 41 - 0
Test/Case/Lib/Utility/TimeLibTest.php

@@ -13,6 +13,47 @@ class TimeLibTest extends MyCakeTestCase {
 		$this->assertTrue(is_a($this->Time, 'TimeLib'));
 	}
 
+	public function testIncrementDate() {
+		$from = '2012-12-31';
+		$Date = TimeLib::incrementDate($from, 0, 0);
+		$this->assertSame($from, $Date->format(FORMAT_DB_DATE));
+
+		$from = '2012-12-31';
+		$Date = TimeLib::incrementDate($from, 0, 1);
+		$this->assertSame('2013-01-31', $Date->format(FORMAT_DB_DATE));
+
+		$from = '2012-12-31';
+		$Date = TimeLib::incrementDate($from, 0, 2);
+		$this->assertSame('2013-02-28', $Date->format(FORMAT_DB_DATE));
+
+		$from = '2012-12-31';
+		$Date = TimeLib::incrementDate($from, 0, 4);
+		$this->assertSame('2013-04-30', $Date->format(FORMAT_DB_DATE));
+
+		$from = '2012-12-31';
+		$Date = TimeLib::incrementDate($from, 1, 0);
+		$this->assertSame('2013-12-31', $Date->format(FORMAT_DB_DATE));
+
+		// from leap year
+		$from = '2008-02-29';
+		$Date = TimeLib::incrementDate($from, 1, 0);
+		$this->assertSame('2009-02-28', $Date->format(FORMAT_DB_DATE));
+
+		// into leap year
+		$from = '2007-02-28';
+		$Date = TimeLib::incrementDate($from, 1, 0);
+		$this->assertSame('2008-02-29', $Date->format(FORMAT_DB_DATE));
+
+		// other direction
+		$from = '2012-12-31';
+		$Date = TimeLib::incrementDate($from, 0, -1);
+		$this->assertSame('2012-11-30', $Date->format(FORMAT_DB_DATE));
+
+		$from = '2012-12-31';
+		$Date = TimeLib::incrementDate($from, -1, -1);
+		$this->assertSame('2011-11-30', $Date->format(FORMAT_DB_DATE));
+	}
+
 	public function testNiceDate() {
 		$res = setlocale(LC_TIME, 'de_DE.UTF-8', 'deu_deu');
 		//debug($res);

+ 7 - 5
View/Helper/CommonHelper.php

@@ -16,22 +16,24 @@ class CommonHelper extends AppHelper {
 /*** Layout Stuff ***/
 
 	/**
-	 * convinience function for clean ROBOTS allowance
-	 * @param STRING private/public
+	 * Convenience function for clean ROBOTS allowance
+	 *
+	 * @param string $type - private/public
+	 * @return string HTML
 	 * 2008-12-08 ms
 	 */
 	public function metaRobots($type = null) {
-		if (($meta = Configure::read('Config.robots')) !== null) {
+		if ($type === null && ($meta = Configure::read('Config.robots')) !== null) {
 			$type = $meta;
 		}
 		$content = array();
 		if ($type === 'public') {
 			$this->privatePage = false;
-			$content['robots']= array('index','follow','noarchive');
+			$content['robots']= array('index', 'follow', 'noarchive');
 
 		} else {
 			$this->privatePage = true;
-			$content['robots']= array('noindex','nofollow','noarchive');
+			$content['robots']= array('noindex', 'nofollow', 'noarchive');
 		}
 
 		$return = '<meta name="robots" content="' . implode(',', $content['robots']) . '" />';