cakephp2-php8/lib/Cake/Test/Case/Utility/CakeTimeTest.php

1044 lines
34 KiB
PHP
Raw Normal View History

<?php
/**
2012-02-06 10:55:12 +00:00
* CakeTimeTest file
*
* PHP 5
*
2012-04-27 02:49:18 +00:00
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
2012-03-13 02:46:07 +00:00
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
*
2012-03-13 02:46:07 +00:00
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
2012-04-27 02:49:18 +00:00
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.View.Helper
* @since CakePHP(tm) v 1.2.0.4206
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
2012-02-06 10:55:12 +00:00
App::uses('CakeTime', 'Utility');
/**
2012-02-06 10:55:12 +00:00
* CakeTimeTest class
*
* @package Cake.Test.Case.View.Helper
*/
2012-02-06 10:55:12 +00:00
class CakeTimeTest extends CakeTestCase {
/**
* Default system timezone identifier
*
* @var string
*/
protected $_systemTimezoneIdentifier = null;
/**
* setUp method
*
* @return void
*/
public function setUp() {
2012-02-06 10:55:12 +00:00
$this->Time = new CakeTime();
$this->_systemTimezoneIdentifier = date_default_timezone_get();
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
unset($this->Time);
$this->_restoreSystemTimezone();
}
/**
* Restored the original system timezone
*
* @param string $timezoneIdentifier Timezone string
* @return void
*/
protected function _restoreSystemTimezone() {
date_default_timezone_set($this->_systemTimezoneIdentifier);
}
/**
* testToQuarter method
*
* @return void
*/
public function testToQuarter() {
$result = $this->Time->toQuarter('2007-12-25');
$this->assertEquals(4, $result);
$result = $this->Time->toQuarter('2007-9-25');
$this->assertEquals(3, $result);
$result = $this->Time->toQuarter('2007-3-25');
$this->assertEquals(1, $result);
$result = $this->Time->toQuarter('2007-3-25', true);
$this->assertEquals(array('2007-01-01', '2007-03-31'), $result);
$result = $this->Time->toQuarter('2007-5-25', true);
$this->assertEquals(array('2007-04-01', '2007-06-30'), $result);
$result = $this->Time->toQuarter('2007-8-25', true);
$this->assertEquals(array('2007-07-01', '2007-09-30'), $result);
$result = $this->Time->toQuarter('2007-12-25', true);
$this->assertEquals(array('2007-10-01', '2007-12-31'), $result);
}
/**
* testTimeAgoInWords method
*
* @return void
*/
public function testTimeAgoInWords() {
$result = $this->Time->timeAgoInWords('-1 week');
2012-05-07 20:32:30 +00:00
$this->assertEquals(date('l d/m, H:i', strtotime('-1 week')), $result);
$result = $this->Time->timeAgoInWords('+1 week');
2012-05-07 20:32:30 +00:00
$this->assertEquals('On ' . date('l d/m, H:i', strtotime('+1 week')), $result);
$result = $this->Time->timeAgoInWords(
strtotime('+4 months +2 weeks +3 days'),
2012-05-13 02:18:18 +00:00
array('end' => '8 years')
);
$this->assertEquals('4 months, 2 weeks, 3 days', $result);
$result = $this->Time->timeAgoInWords(
strtotime('+4 months +2 weeks +2 days'),
2012-05-13 02:18:18 +00:00
array('end' => '8 years')
);
$this->assertEquals('4 months, 2 weeks, 2 days', $result);
$result = $this->Time->timeAgoInWords(
strtotime('+4 months +2 weeks +1 day'),
2012-05-13 02:18:18 +00:00
array('end' => '8 years')
);
$this->assertEquals('4 months, 2 weeks, 1 day', $result);
$result = $this->Time->timeAgoInWords(
strtotime('+3 months +2 weeks +1 day'),
2012-05-13 02:18:18 +00:00
array('end' => '8 years')
);
$this->assertEquals('3 months, 2 weeks, 1 day', $result);
$result = $this->Time->timeAgoInWords(
strtotime('+3 months +2 weeks'),
2012-05-13 02:18:18 +00:00
array('end' => '8 years')
);
$this->assertEquals('3 months, 2 weeks', $result);
$result = $this->Time->timeAgoInWords(
strtotime('+3 months +1 week +6 days'),
2012-05-13 02:18:18 +00:00
array('end' => '8 years')
);
$this->assertEquals('3 months, 1 week, 6 days', $result);
$result = $this->Time->timeAgoInWords(
strtotime('+2 months +2 weeks +1 day'),
2012-05-13 02:18:18 +00:00
array('end' => '8 years')
);
$this->assertEquals('2 months, 2 weeks, 1 day', $result);
$result = $this->Time->timeAgoInWords(
strtotime('+2 months +2 weeks'),
2012-05-13 02:18:18 +00:00
array('end' => '8 years')
);
$this->assertEquals('2 months, 2 weeks', $result);
$result = $this->Time->timeAgoInWords(
strtotime('+2 months +1 week +6 days'),
2012-05-13 02:18:18 +00:00
array('end' => '8 years')
);
$this->assertEquals('2 months, 1 week, 6 days', $result);
$result = $this->Time->timeAgoInWords(
2012-05-13 02:18:18 +00:00
strtotime('-1 month -1 week -6 days'),
array('end' => '1 year', 'accuracy' => array('month' => 'month'))
);
2012-05-13 02:18:18 +00:00
$this->assertEquals('1 month ago', $result);
2012-05-13 03:25:11 +00:00
$timestamp = strtotime('-1 years -2 weeks -3 days');
2012-05-13 02:18:18 +00:00
$result = $this->Time->timeAgoInWords(
2012-05-13 03:25:11 +00:00
$timestamp,
2012-05-13 02:18:18 +00:00
array('accuracy' => array('year' => 'year'))
);
2012-05-13 03:25:11 +00:00
$expected = 'on ' . date('j/n/y', $timestamp);
2012-05-13 02:18:18 +00:00
$this->assertEquals($expected, $result);
$result = $this->Time->timeAgoInWords(
strtotime('+8 years +4 months +2 weeks +3 days'),
2012-05-13 02:18:18 +00:00
array('accuracy' => array('year' => 'year'), 'end' => '+10 years')
);
2012-05-07 20:32:30 +00:00
$expected = '8 years';
$this->assertEquals($expected, $result);
$result = $this->Time->timeAgoInWords(
strtotime('+8 years +4 months +2 weeks +3 days'),
2012-05-13 02:18:18 +00:00
array('accuracy' => array('year' => 'month'), 'end' => '+10 years')
);
2012-05-07 20:32:30 +00:00
$expected = '8 years, 4 months';
$this->assertEquals($expected, $result);
$result = $this->Time->timeAgoInWords(
strtotime('+8 years +4 months +2 weeks +3 days'),
2012-05-13 02:18:18 +00:00
array('accuracy' => array('year' => 'week'), 'end' => '+10 years')
);
2012-05-07 20:32:30 +00:00
$expected = '8 years, 4 months, 2 weeks';
$this->assertEquals($expected, $result);
$result = $this->Time->timeAgoInWords(
strtotime('+8 years +4 months +2 weeks +3 days'),
2012-05-13 02:18:18 +00:00
array('accuracy' => array('year' => 'day'), 'end' => '+10 years')
);
2012-05-07 20:32:30 +00:00
$expected = '8 years, 4 months, 2 weeks, 3 days';
$this->assertEquals($expected, $result);
$result = $this->Time->timeAgoInWords(
strtotime('+1 years +5 weeks'),
2012-05-13 02:18:18 +00:00
array('accuracy' => array('year' => 'year'), 'end' => '+10 years')
);
2012-05-07 20:32:30 +00:00
$expected = '1 year';
$this->assertEquals($expected, $result);
2012-05-07 20:32:30 +00:00
for ($i = 0; $i > -200; $i--) {
$seconds = round($i + ($i * $i));
$time = strtotime('+' . $seconds . ' seconds');
$result = $this->Time->timeAgoInWords($time);
2012-05-07 20:32:30 +00:00
if ($time == time()) {
$expected = 'just now';
$this->assertEquals($expected, $result);
2012-05-19 14:48:15 +00:00
} elseif (date("Y-m-d", $time) == date("Y-m-d")) {
2012-05-07 20:32:30 +00:00
$expected = 'Today, ' . date("H:i", $time);
$this->assertEquals($expected, $result);
}
}
$result = $this->Time->timeAgoInWords(
strtotime('-2 years -5 months -2 days'),
2012-05-13 02:18:18 +00:00
array('end' => '3 years')
);
$this->assertEquals('2 years, 5 months, 2 days ago', $result);
$result = $this->Time->timeAgoInWords('2007-9-25');
$this->assertEquals('on 25/9/07', $result);
$result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d');
$this->assertEquals('on 2007-09-25', $result);
2012-05-13 02:18:18 +00:00
$result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d');
$this->assertEquals('on 2007-09-25', $result);
$result = $this->Time->timeAgoInWords(
strtotime('-2 weeks -2 days'),
2012-05-13 02:18:18 +00:00
'Y-m-d'
);
$this->assertEquals('2 weeks, 2 days ago', $result);
$result = $this->Time->timeAgoInWords(
strtotime('+2 weeks +2 days'),
2012-05-13 02:18:18 +00:00
'Y-m-d'
);
$this->assertRegExp('/^2 weeks, [1|2] day(s)?$/', $result);
$result = $this->Time->timeAgoInWords(
strtotime('+2 months +2 days'),
array('end' => '1 month')
);
$this->assertEquals(
'on ' . date('j/n/y', strtotime('+2 months +2 days')),
$result
);
$result = $this->Time->timeAgoInWords(
strtotime('+2 months +2 days'),
array('end' => '3 month')
);
$this->assertRegExp('/2 months/', $result);
$result = $this->Time->timeAgoInWords(
strtotime('+2 months +12 days'),
array('end' => '3 month')
);
$this->assertRegExp('/2 months, 1 week/', $result);
$result = $this->Time->timeAgoInWords(
strtotime('+3 months +5 days'),
array('end' => '4 month')
);
$this->assertEquals('3 months, 5 days', $result);
$result = $this->Time->timeAgoInWords(
strtotime('-2 months -2 days'),
array('end' => '3 month')
);
$this->assertEquals('2 months, 2 days ago', $result);
$result = $this->Time->timeAgoInWords(
strtotime('-2 months -2 days'),
array('end' => '3 month')
);
$this->assertEquals('2 months, 2 days ago', $result);
$result = $this->Time->timeAgoInWords(
strtotime('+2 months +2 days'),
array('end' => '3 month')
);
$this->assertRegExp('/2 months/', $result);
$result = $this->Time->timeAgoInWords(
strtotime('+2 months +2 days'),
array('end' => '1 month', 'format' => 'Y-m-d')
);
$this->assertEquals('on ' . date('Y-m-d', strtotime('+2 months +2 days')), $result);
$result = $this->Time->timeAgoInWords(
strtotime('-2 months -2 days'),
array('end' => '1 month', 'format' => 'Y-m-d')
);
$this->assertEquals('on ' . date('Y-m-d', strtotime('-2 months -2 days')), $result);
$result = $this->Time->timeAgoInWords(
strtotime('-13 months -5 days'),
array('end' => '2 years')
);
$this->assertEquals('1 year, 1 month, 5 days ago', $result);
}
/**
* testNice method
*
* @return void
*/
public function testNice() {
$time = time() + 2 * DAY;
$this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
$time = time() - 2 * DAY;
$this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
$time = time();
$this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
$time = 0;
$this->assertEquals(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
$time = null;
$this->assertEquals(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
$time = time();
$this->assertEquals(date('D', $time), $this->Time->nice($time, null, '%a'));
$this->assertEquals(date('M d, Y', $time), $this->Time->nice($time, null, '%b %d, %Y'));
$this->Time->niceFormat = '%Y-%d-%m';
$this->assertEquals(date('Y-d-m', $time), $this->Time->nice($time));
$this->assertEquals('%Y-%d-%m', $this->Time->niceFormat);
CakeTime::$niceFormat = '%Y-%d-%m %H:%M:%S';
$this->assertEquals(date('Y-d-m H:i:s', $time), $this->Time->nice($time));
$this->assertEquals('%Y-%d-%m %H:%M:%S', $this->Time->niceFormat);
date_default_timezone_set('UTC');
$result = $this->Time->nice(null, 'America/New_York');
$expected = $this->Time->nice(time(), 'America/New_York');
$this->assertEquals($expected, $result);
$this->_restoreSystemTimezone();
}
/**
* testNiceShort method
*
* @return void
*/
public function testNiceShort() {
$time = time();
$this->assertEquals('Today, ' . date('H:i', $time), $this->Time->niceShort($time));
$time = time() - DAY;
$this->assertEquals('Yesterday, ' . date('H:i', $time), $this->Time->niceShort($time));
2012-05-13 00:50:29 +00:00
2012-05-07 20:32:30 +00:00
$time = time() + DAY;
$this->assertEquals('Tomorrow, ' . date('H:i', $time), $this->Time->niceShort($time));
2012-05-13 00:50:29 +00:00
date_default_timezone_set('Europe/London');
$result = $this->Time->niceShort('2005-01-15 10:00:00', new DateTimeZone('Europe/Brussels'));
$this->assertEquals('Jan 15th 2005, 11:00', $result);
date_default_timezone_set('UTC');
$result = $this->Time->niceShort(null, 'America/New_York');
$expected = $this->Time->niceShort(time(), 'America/New_York');
$this->assertEquals($expected, $result);
$this->_restoreSystemTimezone();
}
/**
* testDaysAsSql method
*
* @return void
*/
public function testDaysAsSql() {
$begin = time();
$end = time() + DAY;
$field = 'my_field';
$expected = '(my_field >= \'' . date('Y-m-d', $begin) . ' 00:00:00\') AND (my_field <= \'' . date('Y-m-d', $end) . ' 23:59:59\')';
$this->assertEquals($expected, $this->Time->daysAsSql($begin, $end, $field));
}
/**
* testDayAsSql method
*
* @return void
*/
public function testDayAsSql() {
$time = time();
$field = 'my_field';
$expected = '(my_field >= \'' . date('Y-m-d', $time) . ' 00:00:00\') AND (my_field <= \'' . date('Y-m-d', $time) . ' 23:59:59\')';
$this->assertEquals($expected, $this->Time->dayAsSql($time, $field));
}
/**
* testToUnix method
*
* @return void
*/
public function testToUnix() {
$this->assertEquals(time(), $this->Time->toUnix(time()));
$this->assertEquals(strtotime('+1 day'), $this->Time->toUnix('+1 day'));
$this->assertEquals(strtotime('+0 days'), $this->Time->toUnix('+0 days'));
$this->assertEquals(strtotime('-1 days'), $this->Time->toUnix('-1 days'));
$this->assertEquals(false, $this->Time->toUnix(''));
$this->assertEquals(false, $this->Time->toUnix(null));
}
/**
* testToServer method
*
* @return void
*/
public function testToServer() {
date_default_timezone_set('Europe/Paris');
$time = time();
$this->assertEquals(date('Y-m-d H:i:s', $time), $this->Time->toServer($time));
date_default_timezone_set('America/New_York');
$time = time();
date_default_timezone_set('Europe/Paris');
$result = $this->Time->toServer($time, 'America/New_York');
$this->assertEquals(date('Y-m-d H:i:s', $time), $result);
date_default_timezone_set('Europe/Paris');
$time = '2005-10-25 10:00:00';
$result = $this->Time->toServer($time);
$date = new DateTime($time, new DateTimeZone('UTC'));
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
$expected = $date->format('Y-m-d H:i:s');
$this->assertEquals($expected, $result);
$time = '2002-01-01 05:15:30';
$result = $this->Time->toServer($time, 'America/New_York');
$date = new DateTime($time, new DateTimeZone('America/New_York'));
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
$expected = $date->format('Y-m-d H:i:s');
$this->assertEquals($expected, $result);
$time = '2010-01-28T15:00:00+10:00';
$result = $this->Time->toServer($time, 'America/New_York');
$date = new DateTime($time);
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
$expected = $date->format('Y-m-d H:i:s');
$this->assertEquals($expected, $result);
$date = new DateTime(null, new DateTimeZone('America/New_York'));
$result = $this->Time->toServer($date, 'Pacific/Tahiti');
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
$expected = $date->format('Y-m-d H:i:s');
$this->assertEquals($expected, $result);
$this->_restoreSystemTimezone();
$time = time();
$result = $this->Time->toServer($time, null, 'l jS \of F Y h:i:s A');
$expected = date('l jS \of F Y h:i:s A', $time);
$this->assertEquals($expected, $result);
$this->assertFalse($this->Time->toServer(time(), new Object()));
date_default_timezone_set('UTC');
$serverTime = new DateTime('now');
$timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu');
foreach ($timezones as $timezone) {
$result = $this->Time->toServer($serverTime->format('Y-m-d H:i:s'), $timezone, 'U');
$tz = new DateTimeZone($timezone);
$this->assertEquals($serverTime->format('U'), $result + $tz->getOffset($serverTime));
}
date_default_timezone_set('UTC');
$date = new DateTime('now', new DateTimeZone('America/New_York'));
$result = $this->Time->toServer($date, null, 'Y-m-d H:i:s');
$date->setTimezone($this->Time->timezone());
$expected = $date->format('Y-m-d H:i:s');
$this->assertEquals($expected, $result);
$this->_restoreSystemTimezone();
}
/**
* testToAtom method
*
* @return void
*/
public function testToAtom() {
$this->assertEquals(date('Y-m-d\TH:i:s\Z'), $this->Time->toAtom(time()));
}
/**
* testToRss method
*
* @return void
*/
public function testToRss() {
$this->assertEquals(date('r'), $this->Time->toRss(time()));
if (!$this->skipIf(!class_exists('DateTimeZone'), '%s DateTimeZone class not available.')) {
$timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu');
2011-11-30 15:44:11 +00:00
foreach ($timezones as $timezone) {
$yourTimezone = new DateTimeZone($timezone);
$yourTime = new DateTime('now', $yourTimezone);
$userOffset = $yourTimezone->getOffset($yourTime) / HOUR;
$this->assertEquals($yourTime->format('r'), $this->Time->toRss(time(), $userOffset));
$this->assertEquals($yourTime->format('r'), $this->Time->toRss(time(), $timezone));
}
}
}
/**
* testFormat method
*
* @return void
*/
public function testFormat() {
$format = 'D-M-Y';
$tz = date_default_timezone_get();
$arr = array(time(), strtotime('+1 days'), strtotime('+1 days'), strtotime('+0 days'));
foreach ($arr as $val) {
$this->assertEquals(date($format, $val), $this->Time->format($format, $val));
$this->assertEquals(date($format, $val), $this->Time->format($format, $val, false, $tz));
}
$result = $this->Time->format('Y-m-d', null, 'never');
$this->assertEquals('never', $result);
$result = $this->Time->format('2012-01-13', '%d-%m-%Y', 'invalid', 'UTC');
$this->assertEquals('13-01-2012', $result);
$result = $this->Time->format('nonsense', '%d-%m-%Y', 'invalid', 'UTC');
$this->assertEquals('invalid', $result);
}
/**
* testOfGmt method
*
* @return void
*/
public function testGmt() {
$hour = 3;
$min = 4;
$sec = 2;
$month = 5;
$day = 14;
$year = 2007;
$time = mktime($hour, $min, $sec, $month, $day, $year);
$expected = gmmktime($hour, $min, $sec, $month, $day, $year);
$this->assertEquals($expected, $this->Time->gmt(date('Y-n-j G:i:s', $time)));
$hour = date('H');
$min = date('i');
$sec = date('s');
$month = date('m');
$day = date('d');
$year = date('Y');
$expected = gmmktime($hour, $min, $sec, $month, $day, $year);
$this->assertEquals($expected, $this->Time->gmt(null));
}
/**
* testIsToday method
*
* @return void
*/
public function testIsToday() {
$result = $this->Time->isToday('+1 day');
$this->assertFalse($result);
$result = $this->Time->isToday('+1 days');
$this->assertFalse($result);
$result = $this->Time->isToday('+0 day');
$this->assertTrue($result);
$result = $this->Time->isToday('-1 day');
$this->assertFalse($result);
}
/**
* testIsThisWeek method
*
* @return void
*/
public function testIsThisWeek() {
// A map of days which goes from -1 day of week to +1 day of week
$map = array(
'Mon' => array(-1, 7), 'Tue' => array(-2, 6), 'Wed' => array(-3, 5),
'Thu' => array(-4, 4), 'Fri' => array(-5, 3), 'Sat' => array(-6, 2),
'Sun' => array(-7, 1)
);
$days = $map[date('D')];
for ($day = $days[0] + 1; $day < $days[1]; $day++) {
$this->assertTrue($this->Time->isThisWeek(($day > 0 ? '+' : '') . $day . ' days'));
}
$this->assertFalse($this->Time->isThisWeek($days[0] . ' days'));
$this->assertFalse($this->Time->isThisWeek('+' . $days[1] . ' days'));
}
/**
* testIsThisMonth method
*
* @return void
*/
public function testIsThisMonth() {
$result = $this->Time->isThisMonth('+0 day');
$this->assertTrue($result);
$result = $this->Time->isThisMonth($time = mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y')));
$this->assertTrue($result);
$result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') - mt_rand(1, 12)));
$this->assertFalse($result);
$result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') + mt_rand(1, 12)));
$this->assertFalse($result);
}
/**
* testIsThisYear method
*
* @return void
*/
public function testIsThisYear() {
$result = $this->Time->isThisYear('+0 day');
$this->assertTrue($result);
$result = $this->Time->isThisYear(mktime(0, 0, 0, mt_rand(1, 12), mt_rand(1, 28), date('Y')));
$this->assertTrue($result);
}
/**
* testWasYesterday method
*
* @return void
*/
public function testWasYesterday() {
$result = $this->Time->wasYesterday('+1 day');
$this->assertFalse($result);
$result = $this->Time->wasYesterday('+1 days');
$this->assertFalse($result);
$result = $this->Time->wasYesterday('+0 day');
$this->assertFalse($result);
$result = $this->Time->wasYesterday('-1 day');
$this->assertTrue($result);
$result = $this->Time->wasYesterday('-1 days');
$this->assertTrue($result);
$result = $this->Time->wasYesterday('-2 days');
$this->assertFalse($result);
}
/**
* testIsTomorrow method
*
* @return void
*/
public function testIsTomorrow() {
$result = $this->Time->isTomorrow('+1 day');
$this->assertTrue($result);
$result = $this->Time->isTomorrow('+1 days');
$this->assertTrue($result);
$result = $this->Time->isTomorrow('+0 day');
$this->assertFalse($result);
$result = $this->Time->isTomorrow('-1 day');
$this->assertFalse($result);
}
/**
* testWasWithinLast method
*
* @return void
*/
public function testWasWithinLast() {
$this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
$this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week'));
$this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
$this->assertTrue($this->Time->wasWithinLast('1 second', '-1 second'));
$this->assertTrue($this->Time->wasWithinLast('1 minute', '-1 minute'));
$this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
$this->assertTrue($this->Time->wasWithinLast('1 month', '-1 month'));
$this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
$this->assertTrue($this->Time->wasWithinLast('1 week', '-1 day'));
$this->assertTrue($this->Time->wasWithinLast('2 week', '-1 week'));
$this->assertFalse($this->Time->wasWithinLast('1 second', '-1 year'));
$this->assertTrue($this->Time->wasWithinLast('10 minutes', '-1 second'));
$this->assertTrue($this->Time->wasWithinLast('23 minutes', '-1 minute'));
$this->assertFalse($this->Time->wasWithinLast('0 year', '-1 year'));
$this->assertTrue($this->Time->wasWithinLast('13 month', '-1 month'));
$this->assertTrue($this->Time->wasWithinLast('2 days', '-1 day'));
$this->assertFalse($this->Time->wasWithinLast('1 week', '-2 weeks'));
$this->assertFalse($this->Time->wasWithinLast('1 second', '-2 seconds'));
$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
$this->assertFalse($this->Time->wasWithinLast('1 hour', '-2 hours'));
$this->assertFalse($this->Time->wasWithinLast('1 month', '-2 months'));
$this->assertFalse($this->Time->wasWithinLast('1 year', '-2 years'));
$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 weeks'));
$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
$this->assertFalse($this->Time->wasWithinLast('0 days', '-2 days'));
$this->assertTrue($this->Time->wasWithinLast('1 hour', '-20 seconds'));
$this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds'));
$this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months'));
$this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months'));
$this->assertTrue($this->Time->wasWithinLast('5 ', '-3 days'));
$this->assertTrue($this->Time->wasWithinLast('1 ', '-1 hour'));
$this->assertTrue($this->Time->wasWithinLast('1 ', '-1 minute'));
$this->assertTrue($this->Time->wasWithinLast('1 ', '-23 hours -59 minutes -59 seconds'));
}
2012-05-13 01:25:57 +00:00
2012-05-07 20:32:30 +00:00
/**
* testWasWithinLast method
*
* @return void
*/
public function testIsWithinNext() {
$this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
$this->assertFalse($this->Time->isWithinNext('1 week', '-1 week'));
$this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
$this->assertFalse($this->Time->isWithinNext('1 second', '-1 second'));
$this->assertFalse($this->Time->isWithinNext('1 minute', '-1 minute'));
$this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
$this->assertFalse($this->Time->isWithinNext('1 month', '-1 month'));
$this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
$this->assertFalse($this->Time->isWithinNext('1 week', '-1 day'));
$this->assertFalse($this->Time->isWithinNext('2 week', '-1 week'));
$this->assertFalse($this->Time->isWithinNext('1 second', '-1 year'));
$this->assertFalse($this->Time->isWithinNext('10 minutes', '-1 second'));
$this->assertFalse($this->Time->isWithinNext('23 minutes', '-1 minute'));
$this->assertFalse($this->Time->isWithinNext('0 year', '-1 year'));
$this->assertFalse($this->Time->isWithinNext('13 month', '-1 month'));
$this->assertFalse($this->Time->isWithinNext('2 days', '-1 day'));
$this->assertFalse($this->Time->isWithinNext('1 week', '-2 weeks'));
$this->assertFalse($this->Time->isWithinNext('1 second', '-2 seconds'));
$this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
$this->assertFalse($this->Time->isWithinNext('1 hour', '-2 hours'));
$this->assertFalse($this->Time->isWithinNext('1 month', '-2 months'));
$this->assertFalse($this->Time->isWithinNext('1 year', '-2 years'));
$this->assertFalse($this->Time->isWithinNext('1 day', '-2 weeks'));
$this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
$this->assertFalse($this->Time->isWithinNext('0 days', '-2 days'));
$this->assertFalse($this->Time->isWithinNext('1 hour', '-20 seconds'));
$this->assertFalse($this->Time->isWithinNext('1 year', '-60 minutes -30 seconds'));
$this->assertFalse($this->Time->isWithinNext('3 years', '-2 months'));
$this->assertFalse($this->Time->isWithinNext('5 months', '-4 months'));
$this->assertFalse($this->Time->isWithinNext('5 ', '-3 days'));
$this->assertFalse($this->Time->isWithinNext('1 ', '-1 hour'));
$this->assertFalse($this->Time->isWithinNext('1 ', '-1 minute'));
$this->assertFalse($this->Time->isWithinNext('1 ', '-23 hours -59 minutes -59 seconds'));
2012-05-13 01:25:57 +00:00
2012-05-07 20:32:30 +00:00
$this->assertTrue($this->Time->isWithinNext('7 days', '6 days, 23 hours, 59 minutes, 59 seconds'));
$this->assertFalse($this->Time->isWithinNext('7 days', '6 days, 23 hours, 59 minutes, 61 seconds'));
}
/**
* testUserOffset method
*
* @return void
*/
public function testUserOffset() {
$timezoneServer = new DateTimeZone(date_default_timezone_get());
$timeServer = new DateTime('now', $timezoneServer);
$yourTimezone = $timezoneServer->getOffset($timeServer) / HOUR;
$expected = time();
$result = $this->Time->fromString(time(), $yourTimezone);
$this->assertEquals($expected, $result);
$result = $this->Time->fromString(time(), $timezoneServer->getName());
$this->assertEquals($expected, $result);
$result = $this->Time->fromString(time(), $timezoneServer);
$this->assertEquals($expected, $result);
Configure::write('Config.timezone', $timezoneServer->getName());
$result = $this->Time->fromString(time());
$this->assertEquals($expected, $result);
Configure::delete('Config.timezone');
}
/**
* test fromString()
*
* @return void
*/
public function testFromString() {
$result = $this->Time->fromString('');
$this->assertFalse($result);
$result = $this->Time->fromString(0, 0);
$this->assertFalse($result);
$result = $this->Time->fromString('+1 hour');
$expected = strtotime('+1 hour');
$this->assertEquals($expected, $result);
$timezone = date('Z', time());
$result = $this->Time->fromString('+1 hour', $timezone);
$expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
$this->assertEquals($expected, $result);
$timezone = date_default_timezone_get();
$result = $this->Time->fromString('+1 hour', $timezone);
$expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
$this->assertEquals($expected, $result);
}
/**
* test fromString() with a DateTime object as the dateString
*
* @return void
*/
public function testFromStringWithDateTime() {
date_default_timezone_set('UTC');
$date = new DateTime('+1 hour', new DateTimeZone('America/New_York'));
$result = $this->Time->fromString($date, 'UTC');
$date->setTimezone(new DateTimeZone('UTC'));
$expected = $date->format('U') + $date->getOffset();
$this->assertEquals($expected, $result);
date_default_timezone_set('Australia/Melbourne');
$date = new DateTime('+1 hour', new DateTimeZone('America/New_York'));
$result = $this->Time->fromString($date, 'Asia/Kuwait');
$date->setTimezone(new DateTimeZone('Asia/Kuwait'));
$expected = $date->format('U') + $date->getOffset();
$this->assertEquals($expected, $result);
$this->_restoreSystemTimezone();
}
/**
* test converting time specifiers using a time definition localfe file
*
* @return void
*/
public function testConvertSpecifiers() {
App::build(array(
'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS)
), App::RESET);
Configure::write('Config.language', 'time_test');
$time = strtotime('Thu Jan 14 11:43:39 2010');
$result = $this->Time->convertSpecifiers('%a', $time);
$expected = 'jue';
$this->assertEquals($expected, $result);
$result = $this->Time->convertSpecifiers('%A', $time);
$expected = 'jueves';
$this->assertEquals($expected, $result);
$result = $this->Time->convertSpecifiers('%c', $time);
$expected = 'jue %d ene %Y %H:%M:%S %Z';
$this->assertEquals($expected, $result);
$result = $this->Time->convertSpecifiers('%C', $time);
$expected = '20';
$this->assertEquals($expected, $result);
$result = $this->Time->convertSpecifiers('%D', $time);
$expected = '%m/%d/%y';
$this->assertEquals($expected, $result);
$result = $this->Time->convertSpecifiers('%b', $time);
$expected = 'ene';
$this->assertEquals($expected, $result);
$result = $this->Time->convertSpecifiers('%h', $time);
$expected = 'ene';
$this->assertEquals($expected, $result);
$result = $this->Time->convertSpecifiers('%B', $time);
$expected = 'enero';
$this->assertEquals($expected, $result);
$result = $this->Time->convertSpecifiers('%n', $time);
$expected = "\n";
$this->assertEquals($expected, $result);
$result = $this->Time->convertSpecifiers('%n', $time);
$expected = "\n";
$this->assertEquals($expected, $result);
$result = $this->Time->convertSpecifiers('%p', $time);
$expected = 'AM';
$this->assertEquals($expected, $result);
$result = $this->Time->convertSpecifiers('%P', $time);
$expected = 'am';
$this->assertEquals($expected, $result);
$result = $this->Time->convertSpecifiers('%r', $time);
$expected = '%I:%M:%S AM';
$this->assertEquals($expected, $result);
$result = $this->Time->convertSpecifiers('%R', $time);
$expected = '11:43';
$this->assertEquals($expected, $result);
$result = $this->Time->convertSpecifiers('%t', $time);
$expected = "\t";
$this->assertEquals($expected, $result);
$result = $this->Time->convertSpecifiers('%T', $time);
$expected = '%H:%M:%S';
$this->assertEquals($expected, $result);
$result = $this->Time->convertSpecifiers('%u', $time);
$expected = 4;
$this->assertEquals($expected, $result);
$result = $this->Time->convertSpecifiers('%x', $time);
$expected = '%d/%m/%y';
$this->assertEquals($expected, $result);
$result = $this->Time->convertSpecifiers('%X', $time);
$expected = '%H:%M:%S';
$this->assertEquals($expected, $result);
}
/**
* test convert %e on windows.
*
* @return void
*/
public function testConvertPercentE() {
2011-05-31 00:49:46 +00:00
$this->skipIf(DIRECTORY_SEPARATOR !== '\\', 'Cannot run windows tests on non-windows OS.');
$time = strtotime('Thu Jan 14 11:43:39 2010');
$result = $this->Time->convertSpecifiers('%e', $time);
$expected = '14';
$this->assertEquals($expected, $result);
$result = $this->Time->convertSpecifiers('%e', strtotime('2011-01-01'));
$expected = ' 1';
$this->assertEquals($expected, $result);
}
/**
* test formatting dates taking in account preferred i18n locale file
*
* @return void
*/
public function testI18nFormat() {
App::build(array(
'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS)
), App::RESET);
Configure::write('Config.language', 'time_test');
$time = strtotime('Thu Jan 14 13:59:28 2010');
$result = $this->Time->i18nFormat($time);
$expected = '14/01/10';
$this->assertEquals($expected, $result);
$result = $this->Time->i18nFormat($time, '%c');
$expected = 'jue 14 ene 2010 13:59:28 ' . utf8_encode(strftime('%Z', $time));
$this->assertEquals($expected, $result);
$result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
$expected = 'Time is 01:59:28 PM, and date is 14/01/10';
$this->assertEquals($expected, $result);
$time = strtotime('Wed Jan 13 13:59:28 2010');
$result = $this->Time->i18nFormat($time);
$expected = '13/01/10';
$this->assertEquals($expected, $result);
$result = $this->Time->i18nFormat($time, '%c');
$expected = 'mié 13 ene 2010 13:59:28 ' . utf8_encode(strftime('%Z', $time));
$this->assertEquals($expected, $result);
$result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
$expected = 'Time is 01:59:28 PM, and date is 13/01/10';
$this->assertEquals($expected, $result);
$result = $this->Time->i18nFormat('invalid date', '%x', 'Date invalid');
$expected = 'Date invalid';
$this->assertEquals($expected, $result);
}
/**
2012-02-23 23:29:53 +00:00
* test new format() syntax which inverts first and second parameters
*
* @return void
*/
public function testFormatNewSyntax() {
$time = time();
$this->assertEquals($this->Time->format($time), $this->Time->i18nFormat($time));
$this->assertEquals($this->Time->format($time, '%c'), $this->Time->i18nFormat($time, '%c'));
}
2012-05-12 15:03:56 +00:00
/**
* testListTimezones
*
* @return void
*/
public function testListTimezones() {
$return = CakeTime::listTimezones();
$this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
$this->assertEquals('Bangkok', $return['Asia']['Asia/Bangkok']);
$this->assertTrue(isset($return['America']['America/Argentina/Buenos_Aires']));
$this->assertEquals('Argentina/Buenos_Aires', $return['America']['America/Argentina/Buenos_Aires']);
$this->assertTrue(isset($return['UTC']['UTC']));
$this->assertFalse(isset($return['Cuba']));
$this->assertFalse(isset($return['US']));
$return = CakeTime::listTimezones('#^Asia/#');
$this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
$this->assertFalse(isset($return['Pacific']));
$return = CakeTime::listTimezones('#^(America|Pacific)/#', null, false);
$this->assertTrue(isset($return['America/Argentina/Buenos_Aires']));
$this->assertTrue(isset($return['Pacific/Tahiti']));
if (!$this->skipIf(version_compare(PHP_VERSION, '5.3.0', '<'))) {
$return = CakeTime::listTimezones(DateTimeZone::ASIA);
$this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
$this->assertFalse(isset($return['Pacific']));
$return = CakeTime::listTimezones(DateTimeZone::PER_COUNTRY, 'US', false);
$this->assertTrue(isset($return['Pacific/Honolulu']));
$this->assertFalse(isset($return['Asia/Bangkok']));
}
}
}