Browse Source

more tests

euromark 11 years ago
parent
commit
4b14ddde87
4 changed files with 92 additions and 145 deletions
  1. 17 5
      Lib/GeocodeLib.php
  2. 15 116
      Lib/Utility/TimeLib.php
  3. 60 1
      Test/Case/Lib/Utility/TimeLibTest.php
  4. 0 23
      View/Helper/DatetimeHelper.php

+ 17 - 5
Lib/GeocodeLib.php

@@ -692,12 +692,27 @@ class GeocodeLib {
 	 * @param float $unit (M=miles, K=kilometers, N=nautical miles, I=inches, F=feet)
 	 * @return integer distance: in km
 	 */
-	public function distance($pointX, $pointY, $unit = null) {
+	public function distance(array $pointX, array $pointY, $unit = null) {
 		if (empty($unit) || !array_key_exists(($unit = strtoupper($unit)), $this->units)) {
 			$unit = array_keys($this->units);
 			$unit = $unit[0];
 		}
 
+		$res = $this->calculateDistance($pointX, $pointY);
+		if (isset($this->units[$unit])) {
+			$res *= $this->units[$unit];
+		}
+		return ceil($res);
+	}
+
+	/**
+	 * GeocodeLib::calculateDistance()
+	 *
+	 * @param array $pointX
+	 * @param array $pointY
+	 * @return float
+	 */
+	public static function calculateDistance(array $pointX, array $pointY) {
 		/*
 		$res = 	6371.04 * ACOS( COS( PI()/2 - rad2deg(90 - $pointX['lat'])) *
 				COS( PI()/2 - rad2deg(90 - $pointY['lat'])) *
@@ -710,10 +725,7 @@ class GeocodeLib {
 
 		// seems to be the only working one (although slightly incorrect...)
 		$res = 69.09 * rad2deg(acos(sin(deg2rad($pointX['lat'])) * sin(deg2rad($pointY['lat'])) + cos(deg2rad($pointX['lat'])) * cos(deg2rad($pointY['lat'])) * cos(deg2rad($pointX['lng'] - $pointY['lng']))));
-		if (isset($this->units[$unit])) {
-			$res *= $this->units[$unit];
-		}
-		return ceil($res);
+		return $res;
 	}
 
 	/**

+ 15 - 116
Lib/Utility/TimeLib.php

@@ -1,6 +1,6 @@
 <?php
 App::uses('CakeTime', 'Utility');
-
+App::uses('GeocodeLib', 'Tools.Lib');
 /**
  * Extend CakeNumber with a few important improvements:
  * - correct timezones for date only input and therefore unchanged day here
@@ -33,7 +33,7 @@ class TimeLib extends CakeTime {
 	 * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
 	 * @return integer Offset in hours
 	 */
-	public function getGmtOffset($timezone = null) {
+	public static function getGmtOffset($timezone = null) {
 		$timezone = self::timezone($timezone);
 		$offset = $timezone->getOffset(new DateTime('@' . time()));
 		$offset = $offset / HOUR;
@@ -47,11 +47,17 @@ class TimeLib extends CakeTime {
 	 * @param float $lng
 	 * @return DateTimeZone Timezone object
 	 */
-	public function timezoneByCoordinates($lat, $lng) {
+	public static function timezoneByCoordinates($lat, $lng) {
 		$current = array('timezone' => null, 'distance' => 0);
 		$identifiers = DateTimeZone::listIdentifiers();
 		foreach ($identifiers as $identifier) {
-			//TODO
+			$timezone = new DateTimeZone($identifier);
+			$location = $timezone->getLocation();
+			$point = array('lat' => $location['latitude'], 'lng' => $location['longitude']);
+	 		$distance = (int)GeocodeLib::calculateDistance(compact('lat', 'lng'), $point);
+	 		if (!$current['distance'] || $distance < $current['distance']) {
+	 			$current = array('timezone' => $identifier, 'distance' => $distance);
+	 		}
 		}
 		return $current['timezone'];
 	}
@@ -291,20 +297,20 @@ class TimeLib extends CakeTime {
 	 * @FIXME: offset
 	 * not needed, use localDate!
 	 */
-	public static function cWeekDay($cweek, $year, $day, $offset = 0) {
+	public static function cWeekDay($cweek, $year, $day) {
 		$cweekBeginning = self::cweekBeginning($year, $cweek);
 		return $cweekBeginning + $day * DAY;
 	}
 
 	/**
-	 * @FIXME ???
 	 * Get number of days since the start of the week.
-	 * 1 = monday, 7 = sunday ? should be 0=sunday to 7=saturday (default)
+	 * 0=sunday to 7=saturday (default)
+	 *
 	 * @param integer $num Number of day.
 	 * @return integer Days since the start of the week.
 	 */
-	public static function cWeekMod($num, $offset = 0) {
-		$base = 7;
+	public static function cWeekMod($num) {
+		$base = 6;
 		return ($num - $base * floor($num / $base));
 	}
 
@@ -663,113 +669,6 @@ class TimeLib extends CakeTime {
 	}
 
 	/**
-	 * Can convert time from one unit to another
-	 *
-	 * @param integer INT | time
-	 * @param from CHAR
-	 * @param to CHAR
-	 * @param options: acc=>INT [accuracy], showZero=>BOOL, returnArray=>BOOL
-	 * @return mixed
-	 */
-	public static function convertTime($int, $from, $to, $options = array()) {
-		$accuracy = 0;	// 0 = only the "to"-element, 1..n = higher accurancy
-		$showZero = false;	// show only the non-zero elements
-		$returnArray = false;	// return as array instead of as string
-		if (!empty($options)) {
-			if (isset($options['acc'])) {
-				$accuracy = (int)$options['acc'];
-			}
-			if (isset($options['showZero'])) {
-				$showZero = (int)$options['showZero'];
-			}
-			if (isset($options['returnArray'])) {
-				$returnArray = $options['returnArray'];
-			}
-		}
-
-		$times = array(
-			's' => '0',
-			'm' => '1',
-			'h' => '2',
-			'd' => '3',
-			'w' => '4',
-			'm' => '5',
-			'y' => '6',
-		);
-		$options = array(
-			'0' => array(
-				'steps' => array('1' => 60, '2' => 3600, '3' => 86400, '4' => 86400 * 7, '5' => 86400 * 30, '6' => 86400 * 365),
-				'down' => 0,
-				'up' => 60,
-				'short' => 's',
-				'long' => 'seconds'
-			),
-			'1' => array(
-				'steps' => array('0' => 60, '2' => 60, '3' => 60 * 24, '4' => 60 * 24 * 7, '5' => 60 * 24 * 30, '6' => 60 * 24 * 365),
-				'down' => 60,
-				'up' => 60,
-				'short' => 'm',
-				'long' => 'minutes'
-			),
-			'2' => array(
-				'steps' => array('0' => 3600, '1' => 60, '3' => 24, '4' => 24 * 7, '5' => 24 * 30, '6' => 24 * 365),
-				'down' => 60,
-				'up' => 24,
-				'short' => 'h',
-				'long' => 'hours'
-			),
-			'3' => array(
-				'steps' => array('0' => 86400, '1' => 3600, '2' => 24, '4' => 7, '5' => 30, '6' => 365),
-				'down' => 24,
-				'up' => 7,
-				'short' => 'd',
-				'long' => 'days'
-			),
-			'4' => array(
-				'steps' => array('0' => 86400 * 7, '1' => 60 * 24 * 7, '2' => 24 * 7, '3' => 7, '5' => 4.2, '6' => 52),
-				'down' => 7,
-				'up' => 4.2,
-				'short' => 'w',
-				'long' => 'weeks'
-			),
-			'5' => array(
-				'steps' => array('0' => 86400 * 30, '1' => 60 * 24 * 30, '2' => 24 * 30, '3' => 30, '4' => 4.2, '6' => 12),
-				'down' => 4.2,
-				'up' => 12,
-				'short' => 'm',
-				'long' => 'months'
-			),
-			'6' => array(
-				'steps' => array('0' => 86400 * 365, '1' => 60 * 24 * 365, '2' => 24 * 365, '3' => 365, '4' => 52, '5' => 12),
-				'down' => 12,
-				'up' => 0,
-				'short' => 'y',
-				'long' => 'years'
-			),
-		);
-
-		if (array_key_exists($from, $times) && array_key_exists($to, $times)) {
-			$begin = $times[$from];
-			$end = $times[$to];
-		}
-
-		$minutes = $int;
-		if ($minutes < 60) {
-			return $minutes . 'min';
-		}
-
-		$calculated = floor($minutes / 60) . "h " . ($minutes % 60) . "min";
-
-		if ($returnArray) {
-			// return as array
-		} else {
-			// convert to the desired string
-		}
-
-		return $calculated;
-	}
-
-	/**
 	 * Returns the difference between a time and now in a "fuzzy" way.
 	 * Note that unlike [span], the "local" timestamp will always be the
 	 * current time. Displaying a fuzzy time instead of a date is usually

+ 60 - 1
Test/Case/Lib/Utility/TimeLibTest.php

@@ -257,6 +257,32 @@ class TimeLibTest extends MyCakeTestCase {
 	}
 
 	/**
+	 * TimeLibTest::testIsLeapYear()
+	 *
+	 * @return void
+	 */
+	public function testIsLeapYear() {
+		$is = TimeLib::isLeapYear('2000');
+		$this->assertTrue($is);
+
+		$is = TimeLib::isLeapYear('2001');
+		$this->assertFalse($is);
+	}
+
+	/**
+	 * TimeLibTest::testIsInRange()
+	 *
+	 * @return void
+	 */
+	public function testIsInRange() {
+		$is = TimeLib::isInRange(date(FORMAT_DB_DATETIME, time() + 22 * HOUR), DAY);
+		$this->assertTrue($is);
+
+		$is = TimeLib::isInRange(date(FORMAT_DB_DATETIME, time() + 26 * HOUR), DAY);
+		$this->assertFalse($is);
+	}
+
+	/**
 	 * TimeLibTest::testAgeBounds()
 	 *
 	 * @return void
@@ -467,7 +493,40 @@ class TimeLibTest extends MyCakeTestCase {
 	 * @return void
 	 */
 	public function testCweekMod() {
-		//$result = TimeLib::cWeekMod();
+		$result = TimeLib::cWeekMod(0);
+		$this->assertEquals(0, $result);
+
+		$result = TimeLib::cWeekMod(1);
+		$this->assertEquals(1, $result);
+
+		$result = TimeLib::cWeekMod(6);
+		$this->assertEquals(0, $result);
+	}
+
+	/**
+	 * TimeLibTest::testGetGmtOffset()
+	 *
+	 * @return void
+	 */
+	public function testGetGmtOffset() {
+		$result = TimeLib::getGmtOffset();
+		$this->assertEquals(0, $result);
+
+		$result = TimeLib::getGmtOffset('Europe/Berlin');
+		$this->assertTrue($result > 0, $result);
+
+		$result = TimeLib::getGmtOffset('America/Los_Angeles');
+		$this->assertTrue($result < 0, $result);
+	}
+
+	/**
+	 * TimeLibTest::testTimezoneByCoordinates()
+	 *
+	 * @return void
+	 */
+	public function testTimezoneByCoordinates() {
+		$result = TimeLib::timezoneByCoordinates(48, 11);
+		$this->assertEquals('Europe/Vaduz', $result);
 	}
 
 	/**

+ 0 - 23
View/Helper/DatetimeHelper.php

@@ -139,29 +139,6 @@ class DatetimeHelper extends TimeHelper {
 	}
 
 	/**
-	 * For birthdays etc
-	 *
-	 * @deprecated - use TimeLib::isInRange()
-	 * @param date
-	 * @param string days with +-
-	 * @param options
-	 */
-	public function isInRangeFromDays($dateString, $days, $options = array()) {
-		$date = explode(' ', $dateString);
-		list ($y, $m, $d) = explode('-', $date[0]);
-
-		$then = mktime(1, 1, 1, $m, $d, $y);
-		$now = mktime(1, 1, 1, date('n'), date('j'), $y);
-
-		$abs = abs($now - $then);
-
-		if ((int)($abs / DAY) <= $days) {
-			return true;
-		}
-		return false;
-	}
-
-	/**
 	 * Takes time as hh:mm:ss,
 	 * returns hh:mm
 	 * TODO: move to lib, but more generic