2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2012-02-06 10:55:12 +00:00
|
|
|
* CakeTimeTest file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
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)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
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
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.View.Helper
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.4206
|
2010-10-03 16:27:27 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-06 10:55:12 +00:00
|
|
|
App::uses('CakeTime', 'Utility');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-02-06 10:55:12 +00:00
|
|
|
* CakeTimeTest class
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.View.Helper
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-06 10:55:12 +00:00
|
|
|
class CakeTimeTest extends CakeTestCase {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-05-13 10:22:29 +00:00
|
|
|
/**
|
|
|
|
* Default system timezone identifier
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_systemTimezoneIdentifier = null;
|
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* setUp method
|
2008-07-05 14:31:22 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function setUp() {
|
2012-02-06 10:55:12 +00:00
|
|
|
$this->Time = new CakeTime();
|
2012-05-13 10:22:29 +00:00
|
|
|
$this->_systemTimezoneIdentifier = date_default_timezone_get();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-18 17:55:58 +00:00
|
|
|
/**
|
|
|
|
* tearDown method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function tearDown() {
|
2009-03-18 17:55:58 +00:00
|
|
|
unset($this->Time);
|
2012-05-13 10:22:29 +00:00
|
|
|
$this->_restoreSystemTimezone();
|
2009-03-18 17:55:58 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-05-13 10:22:29 +00:00
|
|
|
/**
|
|
|
|
* Restored the original system timezone
|
|
|
|
*
|
|
|
|
* @param string $timezoneIdentifier Timezone string
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _restoreSystemTimezone() {
|
|
|
|
date_default_timezone_set($this->_systemTimezoneIdentifier);
|
|
|
|
}
|
2012-06-25 01:57:00 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testToQuarter method
|
2008-07-05 14:31:22 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testToQuarter() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$result = $this->Time->toQuarter('2007-12-25');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(4, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->Time->toQuarter('2007-9-25');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(3, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->Time->toQuarter('2007-3-25');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(1, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->Time->toQuarter('2007-3-25', true);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('2007-01-01', '2007-03-31'), $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->Time->toQuarter('2007-5-25', true);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('2007-04-01', '2007-06-30'), $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->Time->toQuarter('2007-8-25', true);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('2007-07-01', '2007-09-30'), $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->Time->toQuarter('2007-12-25', true);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('2007-10-01', '2007-12-31'), $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
2012-06-25 01:57:00 +00:00
|
|
|
* provider for timeAgoInWords() tests
|
2008-07-05 14:31:22 +00:00
|
|
|
*
|
2012-06-25 01:57:00 +00:00
|
|
|
* @return array
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2012-06-25 01:57:00 +00:00
|
|
|
public static function timeAgoProvider() {
|
|
|
|
return array(
|
|
|
|
array('-12 seconds', '12 seconds ago'),
|
|
|
|
array('-12 minutes', '12 minutes ago'),
|
|
|
|
array('-2 hours', '2 hours ago'),
|
|
|
|
array('-1 day', '1 day ago'),
|
|
|
|
array('-2 days', '2 days ago'),
|
|
|
|
array('-2 days -3 hours', '2 days, 3 hours ago'),
|
|
|
|
array('-1 week', '1 week ago'),
|
|
|
|
array('-2 weeks -2 days', '2 weeks, 2 days ago'),
|
|
|
|
array('+1 week', '1 week'),
|
|
|
|
array('+1 week 1 day', '1 week, 1 day'),
|
|
|
|
array('+2 weeks 2 day', '2 weeks, 2 days'),
|
|
|
|
array('2007-9-24', 'on 24/9/07'),
|
|
|
|
array('now', 'just now'),
|
2012-05-13 01:09:04 +00:00
|
|
|
);
|
2012-06-25 01:57:00 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-06-25 01:57:00 +00:00
|
|
|
/**
|
|
|
|
* testTimeAgoInWords method
|
|
|
|
*
|
|
|
|
* @dataProvider timeAgoProvider
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTimeAgoInWords($input, $expected) {
|
|
|
|
$result = $this->Time->timeAgoInWords($input);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-06-25 01:57:00 +00:00
|
|
|
/**
|
|
|
|
* provider for timeAgo with an end date.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function timeAgoEndProvider() {
|
|
|
|
return array(
|
|
|
|
array(
|
|
|
|
'+4 months +2 weeks +3 days',
|
|
|
|
'4 months, 2 weeks, 3 days',
|
|
|
|
'8 years'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'+4 months +2 weeks +1 day',
|
|
|
|
'4 months, 2 weeks, 1 day',
|
|
|
|
'8 years'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'+3 months +2 weeks',
|
|
|
|
'3 months, 2 weeks',
|
|
|
|
'8 years'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'+3 months +2 weeks +1 day',
|
|
|
|
'3 months, 2 weeks, 1 day',
|
|
|
|
'8 years'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'+1 months +1 week +1 day',
|
|
|
|
'1 month, 1 week, 1 day',
|
|
|
|
'8 years'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'+2 months +2 days',
|
|
|
|
'2 months, 2 days',
|
|
|
|
'on ' . date('j/n/y', strtotime('+2 months +2 days'))
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'+2 months +12 days',
|
|
|
|
'2 months, 1 week, 5 days',
|
|
|
|
'3 months'
|
|
|
|
),
|
2012-05-13 01:09:04 +00:00
|
|
|
);
|
2012-06-25 01:57:00 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-06-25 01:57:00 +00:00
|
|
|
/**
|
|
|
|
* test the end option for timeAgoInWords
|
|
|
|
*
|
|
|
|
* @dataProvider timeAgoEndProvider
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTimeAgoInWordsEnd($input, $expected, $end) {
|
2012-05-13 01:09:04 +00:00
|
|
|
$result = $this->Time->timeAgoInWords(
|
2012-06-25 01:57:00 +00:00
|
|
|
$input, array('end' => $end)
|
2012-05-13 01:09:04 +00:00
|
|
|
);
|
2012-06-25 01:57:00 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-06-25 01:57:00 +00:00
|
|
|
/**
|
|
|
|
* Test the accuracy option for timeAgoInWords()
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTimeAgoInWordsAccuracy() {
|
2012-05-13 01:09:04 +00:00
|
|
|
$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-13 01:09:04 +00:00
|
|
|
);
|
2012-05-07 20:32:30 +00:00
|
|
|
$expected = '8 years';
|
|
|
|
$this->assertEquals($expected, $result);
|
2012-05-13 01:09:04 +00:00
|
|
|
|
|
|
|
$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-13 01:09:04 +00:00
|
|
|
);
|
2012-05-07 20:32:30 +00:00
|
|
|
$expected = '8 years, 4 months';
|
|
|
|
$this->assertEquals($expected, $result);
|
2012-05-13 01:09:04 +00:00
|
|
|
|
|
|
|
$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-13 01:09:04 +00:00
|
|
|
);
|
2012-05-07 20:32:30 +00:00
|
|
|
$expected = '8 years, 4 months, 2 weeks';
|
|
|
|
$this->assertEquals($expected, $result);
|
2012-05-13 01:09:04 +00:00
|
|
|
|
|
|
|
$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);
|
2012-05-13 01:09:04 +00:00
|
|
|
|
|
|
|
$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-13 01:09:04 +00:00
|
|
|
);
|
2012-05-07 20:32:30 +00:00
|
|
|
$expected = '1 year';
|
|
|
|
$this->assertEquals($expected, $result);
|
2012-06-25 01:57:00 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-06-25 01:57:00 +00:00
|
|
|
/**
|
|
|
|
* Test the format option of timeAgoInWords()
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTimeAgoInWordsWithFormat() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('on 2007-09-25', $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-05-13 02:18:18 +00:00
|
|
|
$result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('on 2007-09-25', $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-05-13 01:09:04 +00:00
|
|
|
$result = $this->Time->timeAgoInWords(
|
|
|
|
strtotime('+2 weeks +2 days'),
|
2012-05-13 02:18:18 +00:00
|
|
|
'Y-m-d'
|
2012-05-13 01:09:04 +00:00
|
|
|
);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp('/^2 weeks, [1|2] day(s)?$/', $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-06-25 00:54:47 +00:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test timeAgoInWords() with negative values.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTimeAgoInWordsNegativeValues() {
|
2012-05-13 01:09:04 +00:00
|
|
|
$result = $this->Time->timeAgoInWords(
|
|
|
|
strtotime('-2 months -2 days'),
|
|
|
|
array('end' => '3 month')
|
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('2 months, 2 days ago', $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-05-13 01:09:04 +00:00
|
|
|
$result = $this->Time->timeAgoInWords(
|
2012-06-25 00:54:47 +00:00
|
|
|
strtotime('-2 months -2 days'),
|
2012-05-13 01:09:04 +00:00
|
|
|
array('end' => '3 month')
|
|
|
|
);
|
2012-06-25 00:54:47 +00:00
|
|
|
$this->assertEquals('2 months, 2 days ago', $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-05-13 01:09:04 +00:00
|
|
|
$result = $this->Time->timeAgoInWords(
|
2012-06-25 00:54:47 +00:00
|
|
|
strtotime('-2 months -2 days'),
|
2012-05-13 01:09:04 +00:00
|
|
|
array('end' => '1 month', 'format' => 'Y-m-d')
|
|
|
|
);
|
2012-06-25 00:54:47 +00:00
|
|
|
$this->assertEquals('on ' . date('Y-m-d', strtotime('-2 months -2 days')), $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-05-13 01:09:04 +00:00
|
|
|
$result = $this->Time->timeAgoInWords(
|
2012-06-25 00:54:47 +00:00
|
|
|
strtotime('-2 years -5 months -2 days'),
|
|
|
|
array('end' => '3 years')
|
2012-05-13 01:09:04 +00:00
|
|
|
);
|
2012-06-25 00:54:47 +00:00
|
|
|
$this->assertEquals('2 years, 5 months, 2 days ago', $result);
|
|
|
|
|
|
|
|
$result = $this->Time->timeAgoInWords(
|
|
|
|
strtotime('-2 weeks -2 days'),
|
|
|
|
'Y-m-d'
|
|
|
|
);
|
|
|
|
$this->assertEquals('2 weeks, 2 days ago', $result);
|
|
|
|
|
|
|
|
$time = strtotime('-3 years -12 months');
|
|
|
|
$result = $this->Time->timeAgoInWords($time);
|
|
|
|
$expected = 'on ' . date('j/n/y', $time);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$result = $this->Time->timeAgoInWords(
|
|
|
|
strtotime('-1 month -1 week -6 days'),
|
|
|
|
array('end' => '1 year', 'accuracy' => array('month' => 'month'))
|
|
|
|
);
|
|
|
|
$this->assertEquals('1 month ago', $result);
|
|
|
|
|
|
|
|
$timestamp = strtotime('-1 years -2 weeks -3 days');
|
|
|
|
$result = $this->Time->timeAgoInWords(
|
|
|
|
$timestamp,
|
|
|
|
array('accuracy' => array('year' => 'year'))
|
|
|
|
);
|
|
|
|
$expected = 'on ' . date('j/n/y', $timestamp);
|
|
|
|
$this->assertEquals($expected, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-05-13 01:09:04 +00:00
|
|
|
$result = $this->Time->timeAgoInWords(
|
|
|
|
strtotime('-13 months -5 days'),
|
|
|
|
array('end' => '2 years')
|
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('1 year, 1 month, 5 days ago', $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
2008-10-08 17:19:07 +00:00
|
|
|
* testNice method
|
2008-07-05 14:31:22 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testNice() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$time = time() + 2 * DAY;
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$time = time() - 2 * DAY;
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$time = time();
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$time = 0;
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$time = null;
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
|
2011-01-06 03:06:52 +00:00
|
|
|
|
|
|
|
$time = time();
|
2011-11-16 00:07:56 +00:00
|
|
|
$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'));
|
2011-01-06 03:06:52 +00:00
|
|
|
|
|
|
|
$this->Time->niceFormat = '%Y-%d-%m';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(date('Y-d-m', $time), $this->Time->nice($time));
|
2012-02-13 02:14:33 +00:00
|
|
|
$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);
|
2012-05-13 10:22:29 +00:00
|
|
|
|
|
|
|
date_default_timezone_set('UTC');
|
|
|
|
$result = $this->Time->nice(null, 'America/New_York');
|
|
|
|
$expected = $this->Time->nice(time(), 'America/New_York');
|
2012-05-13 13:51:47 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2012-05-13 10:22:29 +00:00
|
|
|
|
|
|
|
$this->_restoreSystemTimezone();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-10-08 17:19:07 +00:00
|
|
|
/**
|
|
|
|
* testNiceShort method
|
2008-07-05 14:31:22 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testNiceShort() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$time = time();
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals('Today, ' . date('H:i', $time), $this->Time->niceShort($time));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$time = time() - DAY;
|
2011-11-16 00:07:56 +00:00
|
|
|
$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'));
|
2012-05-13 13:51:47 +00:00
|
|
|
$this->assertEquals('Jan 15th 2005, 11:00', $result);
|
2012-05-13 10:22:29 +00:00
|
|
|
|
|
|
|
date_default_timezone_set('UTC');
|
|
|
|
$result = $this->Time->niceShort(null, 'America/New_York');
|
|
|
|
$expected = $this->Time->niceShort(time(), 'America/New_York');
|
2012-05-13 13:51:47 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2012-05-13 10:22:29 +00:00
|
|
|
|
|
|
|
$this->_restoreSystemTimezone();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-10-08 17:19:07 +00:00
|
|
|
/**
|
|
|
|
* testDaysAsSql method
|
2008-07-05 14:31:22 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testDaysAsSql() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$begin = time();
|
|
|
|
$end = time() + DAY;
|
|
|
|
$field = 'my_field';
|
2012-03-14 02:59:20 +00:00
|
|
|
$expected = '(my_field >= \'' . date('Y-m-d', $begin) . ' 00:00:00\') AND (my_field <= \'' . date('Y-m-d', $end) . ' 23:59:59\')';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $this->Time->daysAsSql($begin, $end, $field));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
2008-10-08 17:19:07 +00:00
|
|
|
* testDayAsSql method
|
2008-07-05 14:31:22 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testDayAsSql() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$time = time();
|
|
|
|
$field = 'my_field';
|
2012-03-14 02:59:20 +00:00
|
|
|
$expected = '(my_field >= \'' . date('Y-m-d', $time) . ' 00:00:00\') AND (my_field <= \'' . date('Y-m-d', $time) . ' 23:59:59\')';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $this->Time->dayAsSql($time, $field));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testToUnix method
|
2008-07-05 14:31:22 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testToUnix() {
|
2011-11-16 00:07:56 +00:00
|
|
|
$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));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-04-18 20:33:39 +00:00
|
|
|
/**
|
|
|
|
* testToServer method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testToServer() {
|
2012-05-13 13:51:47 +00:00
|
|
|
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()));
|
|
|
|
|
2012-04-18 20:33:39 +00:00
|
|
|
date_default_timezone_set('UTC');
|
2012-05-13 10:22:29 +00:00
|
|
|
|
2012-04-18 20:33:39 +00:00
|
|
|
$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));
|
|
|
|
}
|
|
|
|
|
2012-05-13 13:51:47 +00:00
|
|
|
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);
|
|
|
|
|
2012-05-13 10:22:29 +00:00
|
|
|
$this->_restoreSystemTimezone();
|
2012-04-18 20:33:39 +00:00
|
|
|
}
|
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
2008-10-08 17:19:07 +00:00
|
|
|
* testToAtom method
|
2008-07-05 14:31:22 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testToAtom() {
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(date('Y-m-d\TH:i:s\Z'), $this->Time->toAtom(time()));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
2008-10-08 17:19:07 +00:00
|
|
|
* testToRss method
|
2008-07-05 14:31:22 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testToRss() {
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(date('r'), $this->Time->toRss(time()));
|
2011-08-26 21:16:49 +00:00
|
|
|
|
|
|
|
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) {
|
2011-08-26 21:16:49 +00:00
|
|
|
$yourTimezone = new DateTimeZone($timezone);
|
|
|
|
$yourTime = new DateTime('now', $yourTimezone);
|
|
|
|
$userOffset = $yourTimezone->getOffset($yourTime) / HOUR;
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($yourTime->format('r'), $this->Time->toRss(time(), $userOffset));
|
2012-04-11 22:03:16 +00:00
|
|
|
$this->assertEquals($yourTime->format('r'), $this->Time->toRss(time(), $timezone));
|
2011-10-28 05:01:17 +00:00
|
|
|
}
|
2011-08-26 21:16:49 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
2008-10-08 17:19:07 +00:00
|
|
|
* testFormat method
|
2008-07-05 14:31:22 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testFormat() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$format = 'D-M-Y';
|
2012-05-17 11:52:47 +00:00
|
|
|
$tz = date_default_timezone_get();
|
2008-05-30 11:40:08 +00:00
|
|
|
$arr = array(time(), strtotime('+1 days'), strtotime('+1 days'), strtotime('+0 days'));
|
|
|
|
foreach ($arr as $val) {
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(date($format, $val), $this->Time->format($format, $val));
|
2012-05-17 11:52:47 +00:00
|
|
|
$this->assertEquals(date($format, $val), $this->Time->format($format, $val, false, $tz));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-07-05 14:31:22 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$result = $this->Time->format('Y-m-d', null, 'never');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('never', $result);
|
2012-06-09 12:58:39 +00:00
|
|
|
|
2012-06-24 17:41:52 +00:00
|
|
|
$result = $this->Time->format('2012-01-13', '%d-%m-%Y', 'invalid');
|
2012-06-09 12:58:39 +00:00
|
|
|
$this->assertEquals('13-01-2012', $result);
|
|
|
|
|
|
|
|
$result = $this->Time->format('nonsense', '%d-%m-%Y', 'invalid', 'UTC');
|
|
|
|
$this->assertEquals('invalid', $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testOfGmt method
|
2008-07-05 14:31:22 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testGmt() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$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);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $this->Time->gmt(date('Y-n-j G:i:s', $time)));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$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);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $this->Time->gmt(null));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
2008-10-08 17:19:07 +00:00
|
|
|
* testIsToday method
|
2008-07-05 14:31:22 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testIsToday() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$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);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-10-08 17:19:07 +00:00
|
|
|
/**
|
|
|
|
* testIsThisWeek method
|
2008-07-05 14:31:22 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testIsThisWeek() {
|
2009-10-21 21:19:21 +00:00
|
|
|
// 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'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-10-21 21:19:21 +00:00
|
|
|
$this->assertFalse($this->Time->isThisWeek($days[0] . ' days'));
|
|
|
|
$this->assertFalse($this->Time->isThisWeek('+' . $days[1] . ' days'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-10-08 17:19:07 +00:00
|
|
|
/**
|
|
|
|
* testIsThisMonth method
|
2008-07-05 14:31:22 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testIsThisMonth() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$result = $this->Time->isThisMonth('+0 day');
|
|
|
|
$this->assertTrue($result);
|
2008-07-05 14:31:22 +00:00
|
|
|
$result = $this->Time->isThisMonth($time = mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y')));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($result);
|
2008-07-05 14:31:22 +00:00
|
|
|
$result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') - mt_rand(1, 12)));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertFalse($result);
|
2008-07-05 14:31:22 +00:00
|
|
|
$result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') + mt_rand(1, 12)));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-10-08 17:19:07 +00:00
|
|
|
/**
|
|
|
|
* testIsThisYear method
|
2008-07-05 14:31:22 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testIsThisYear() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$result = $this->Time->isThisYear('+0 day');
|
|
|
|
$this->assertTrue($result);
|
2008-07-05 14:31:22 +00:00
|
|
|
$result = $this->Time->isThisYear(mktime(0, 0, 0, mt_rand(1, 12), mt_rand(1, 28), date('Y')));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
2011-11-08 06:16:55 +00:00
|
|
|
|
|
|
|
/**
|
2008-10-08 17:19:07 +00:00
|
|
|
* testWasYesterday method
|
2008-07-05 14:31:22 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testWasYesterday() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$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);
|
|
|
|
}
|
2011-11-08 06:16:55 +00:00
|
|
|
|
|
|
|
/**
|
2008-10-08 17:19:07 +00:00
|
|
|
* testIsTomorrow method
|
2008-07-05 14:31:22 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testIsTomorrow() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$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);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2008-10-08 17:19:07 +00:00
|
|
|
* testWasWithinLast method
|
2008-07-05 14:31:22 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testWasWithinLast() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$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'));
|
2008-07-05 14:31:22 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$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'));
|
2008-07-05 14:31:22 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$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'));
|
2008-07-05 14:31:22 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$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'));
|
|
|
|
}
|
2011-11-08 06:16:55 +00:00
|
|
|
|
|
|
|
/**
|
2008-06-05 15:20:45 +00:00
|
|
|
* testUserOffset method
|
2008-07-05 14:31:22 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testUserOffset() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$timezoneServer = new DateTimeZone(date_default_timezone_get());
|
2008-07-05 14:31:22 +00:00
|
|
|
$timeServer = new DateTime('now', $timezoneServer);
|
2008-05-30 11:40:08 +00:00
|
|
|
$yourTimezone = $timezoneServer->getOffset($timeServer) / HOUR;
|
2008-07-05 14:31:22 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$expected = time();
|
|
|
|
$result = $this->Time->fromString(time(), $yourTimezone);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2012-04-11 22:03:16 +00:00
|
|
|
|
|
|
|
$result = $this->Time->fromString(time(), $timezoneServer->getName());
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$result = $this->Time->fromString(time(), $timezoneServer);
|
|
|
|
$this->assertEquals($expected, $result);
|
2012-04-13 04:06:43 +00:00
|
|
|
|
|
|
|
Configure::write('Config.timezone', $timezoneServer->getName());
|
|
|
|
$result = $this->Time->fromString(time());
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
Configure::delete('Config.timezone');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-10-08 17:19:07 +00:00
|
|
|
/**
|
|
|
|
* test fromString()
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testFromString() {
|
2008-10-08 17:19:07 +00:00
|
|
|
$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');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2008-10-08 17:19:07 +00:00
|
|
|
|
|
|
|
$timezone = date('Z', time());
|
|
|
|
$result = $this->Time->fromString('+1 hour', $timezone);
|
|
|
|
$expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
|
2012-04-11 22:03:16 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$timezone = date_default_timezone_get();
|
|
|
|
$result = $this->Time->fromString('+1 hour', $timezone);
|
|
|
|
$expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2012-06-24 10:10:28 +00:00
|
|
|
|
|
|
|
date_default_timezone_set('UTC');
|
|
|
|
$date = new DateTime('now', new DateTimeZone('Europe/London'));
|
|
|
|
$this->Time->fromString($date);
|
|
|
|
$this->assertEquals('Europe/London', $date->getTimeZone()->getName());
|
|
|
|
|
|
|
|
$this->_restoreSystemTimezone();
|
2008-10-08 17:19:07 +00:00
|
|
|
}
|
2010-01-14 18:41:57 +00:00
|
|
|
|
2012-05-13 13:51:47 +00:00
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
|
2010-01-14 18:41:57 +00:00
|
|
|
/**
|
|
|
|
* test converting time specifiers using a time definition localfe file
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testConvertSpecifiers() {
|
2010-01-14 18:41:57 +00:00
|
|
|
App::build(array(
|
2012-02-18 12:31:29 +00:00
|
|
|
'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS)
|
2012-02-18 12:04:54 +00:00
|
|
|
), App::RESET);
|
2010-01-14 18:41:57 +00:00
|
|
|
Configure::write('Config.language', 'time_test');
|
2010-04-13 05:06:44 +00:00
|
|
|
$time = strtotime('Thu Jan 14 11:43:39 2010');
|
2010-01-14 18:41:57 +00:00
|
|
|
|
2010-01-14 20:08:48 +00:00
|
|
|
$result = $this->Time->convertSpecifiers('%a', $time);
|
2010-01-14 18:41:57 +00:00
|
|
|
$expected = 'jue';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-14 18:41:57 +00:00
|
|
|
|
2010-01-14 20:08:48 +00:00
|
|
|
$result = $this->Time->convertSpecifiers('%A', $time);
|
2010-01-14 18:41:57 +00:00
|
|
|
$expected = 'jueves';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-14 18:41:57 +00:00
|
|
|
|
2010-01-14 20:08:48 +00:00
|
|
|
$result = $this->Time->convertSpecifiers('%c', $time);
|
2010-01-14 18:41:57 +00:00
|
|
|
$expected = 'jue %d ene %Y %H:%M:%S %Z';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-14 18:41:57 +00:00
|
|
|
|
2010-01-14 20:08:48 +00:00
|
|
|
$result = $this->Time->convertSpecifiers('%C', $time);
|
2010-01-14 18:41:57 +00:00
|
|
|
$expected = '20';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-14 18:41:57 +00:00
|
|
|
|
2010-01-14 20:08:48 +00:00
|
|
|
$result = $this->Time->convertSpecifiers('%D', $time);
|
2010-01-14 18:41:57 +00:00
|
|
|
$expected = '%m/%d/%y';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-14 18:41:57 +00:00
|
|
|
|
2010-01-14 20:08:48 +00:00
|
|
|
$result = $this->Time->convertSpecifiers('%b', $time);
|
2010-01-14 18:41:57 +00:00
|
|
|
$expected = 'ene';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-14 18:41:57 +00:00
|
|
|
|
2010-01-14 20:08:48 +00:00
|
|
|
$result = $this->Time->convertSpecifiers('%h', $time);
|
2010-01-14 18:41:57 +00:00
|
|
|
$expected = 'ene';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-14 18:41:57 +00:00
|
|
|
|
2010-01-14 20:08:48 +00:00
|
|
|
$result = $this->Time->convertSpecifiers('%B', $time);
|
2010-01-14 18:41:57 +00:00
|
|
|
$expected = 'enero';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-14 18:41:57 +00:00
|
|
|
|
2010-01-14 20:08:48 +00:00
|
|
|
$result = $this->Time->convertSpecifiers('%n', $time);
|
2010-01-14 18:41:57 +00:00
|
|
|
$expected = "\n";
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-14 18:41:57 +00:00
|
|
|
|
2010-01-14 20:08:48 +00:00
|
|
|
$result = $this->Time->convertSpecifiers('%n', $time);
|
2010-01-14 18:41:57 +00:00
|
|
|
$expected = "\n";
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-14 18:41:57 +00:00
|
|
|
|
2010-01-14 20:08:48 +00:00
|
|
|
$result = $this->Time->convertSpecifiers('%p', $time);
|
2010-01-14 18:41:57 +00:00
|
|
|
$expected = 'AM';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-14 18:41:57 +00:00
|
|
|
|
2010-01-14 20:08:48 +00:00
|
|
|
$result = $this->Time->convertSpecifiers('%P', $time);
|
2010-01-14 18:41:57 +00:00
|
|
|
$expected = 'am';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-14 18:41:57 +00:00
|
|
|
|
2010-01-14 20:08:48 +00:00
|
|
|
$result = $this->Time->convertSpecifiers('%r', $time);
|
2010-01-14 18:41:57 +00:00
|
|
|
$expected = '%I:%M:%S AM';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-14 18:41:57 +00:00
|
|
|
|
2010-01-14 20:08:48 +00:00
|
|
|
$result = $this->Time->convertSpecifiers('%R', $time);
|
2010-01-14 18:41:57 +00:00
|
|
|
$expected = '11:43';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-14 18:41:57 +00:00
|
|
|
|
2010-01-14 20:08:48 +00:00
|
|
|
$result = $this->Time->convertSpecifiers('%t', $time);
|
2010-01-14 18:41:57 +00:00
|
|
|
$expected = "\t";
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-14 18:41:57 +00:00
|
|
|
|
2010-01-14 20:08:48 +00:00
|
|
|
$result = $this->Time->convertSpecifiers('%T', $time);
|
2010-01-14 18:41:57 +00:00
|
|
|
$expected = '%H:%M:%S';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-14 18:41:57 +00:00
|
|
|
|
2010-01-14 20:08:48 +00:00
|
|
|
$result = $this->Time->convertSpecifiers('%u', $time);
|
2010-01-14 18:41:57 +00:00
|
|
|
$expected = 4;
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-14 18:41:57 +00:00
|
|
|
|
2010-01-14 20:08:48 +00:00
|
|
|
$result = $this->Time->convertSpecifiers('%x', $time);
|
2010-01-14 18:41:57 +00:00
|
|
|
$expected = '%d/%m/%y';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-14 18:41:57 +00:00
|
|
|
|
2010-01-14 20:08:48 +00:00
|
|
|
$result = $this->Time->convertSpecifiers('%X', $time);
|
2010-01-14 18:41:57 +00:00
|
|
|
$expected = '%H:%M:%S';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-14 18:41:57 +00:00
|
|
|
}
|
|
|
|
|
2011-02-21 15:48:34 +00:00
|
|
|
/**
|
|
|
|
* test convert %e on windows.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testConvertPercentE() {
|
2011-05-31 00:49:46 +00:00
|
|
|
$this->skipIf(DIRECTORY_SEPARATOR !== '\\', 'Cannot run windows tests on non-windows OS.');
|
|
|
|
|
2011-02-21 15:48:34 +00:00
|
|
|
$time = strtotime('Thu Jan 14 11:43:39 2010');
|
|
|
|
$result = $this->Time->convertSpecifiers('%e', $time);
|
|
|
|
$expected = '14';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2011-02-21 15:48:34 +00:00
|
|
|
|
|
|
|
$result = $this->Time->convertSpecifiers('%e', strtotime('2011-01-01'));
|
|
|
|
$expected = ' 1';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2011-02-21 15:48:34 +00:00
|
|
|
}
|
|
|
|
|
2010-01-14 18:41:57 +00:00
|
|
|
/**
|
|
|
|
* test formatting dates taking in account preferred i18n locale file
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testI18nFormat() {
|
2010-01-14 18:41:57 +00:00
|
|
|
App::build(array(
|
2012-02-18 12:31:29 +00:00
|
|
|
'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS)
|
2012-02-18 12:04:54 +00:00
|
|
|
), App::RESET);
|
2010-01-14 18:41:57 +00:00
|
|
|
Configure::write('Config.language', 'time_test');
|
2011-11-19 15:08:04 +00:00
|
|
|
|
2010-04-13 05:06:44 +00:00
|
|
|
$time = strtotime('Thu Jan 14 13:59:28 2010');
|
2010-01-14 18:41:57 +00:00
|
|
|
|
|
|
|
$result = $this->Time->i18nFormat($time);
|
|
|
|
$expected = '14/01/10';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-14 18:41:57 +00:00
|
|
|
|
2010-01-14 20:08:48 +00:00
|
|
|
$result = $this->Time->i18nFormat($time, '%c');
|
2012-04-27 00:39:32 +00:00
|
|
|
$expected = 'jue 14 ene 2010 13:59:28 ' . utf8_encode(strftime('%Z', $time));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-14 18:41:57 +00:00
|
|
|
|
2010-01-14 20:08:48 +00:00
|
|
|
$result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
|
2010-01-14 18:41:57 +00:00
|
|
|
$expected = 'Time is 01:59:28 PM, and date is 14/01/10';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-14 18:41:57 +00:00
|
|
|
|
2011-11-19 15:08:04 +00:00
|
|
|
$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');
|
2012-04-27 00:39:32 +00:00
|
|
|
$expected = 'mié 13 ene 2010 13:59:28 ' . utf8_encode(strftime('%Z', $time));
|
2011-11-19 15:08:04 +00:00
|
|
|
$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);
|
|
|
|
|
2010-01-14 20:08:48 +00:00
|
|
|
$result = $this->Time->i18nFormat('invalid date', '%x', 'Date invalid');
|
2010-01-14 18:41:57 +00:00
|
|
|
$expected = 'Date invalid';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-01-14 18:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-23 23:29:53 +00:00
|
|
|
* test new format() syntax which inverts first and second parameters
|
2010-01-14 18:41:57 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testFormatNewSyntax() {
|
2010-01-14 18:41:57 +00:00
|
|
|
$time = time();
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($this->Time->format($time), $this->Time->i18nFormat($time));
|
|
|
|
$this->assertEquals($this->Time->format($time, '%c'), $this->Time->i18nFormat($time, '%c'));
|
2010-01-14 18:41:57 +00:00
|
|
|
}
|
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']));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-25 13:23:19 +00:00
|
|
|
/**
|
|
|
|
* Tests that using CakeTime::format() with the correct sytax actually converts
|
|
|
|
* from one timezone to the other correctly
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
public function testCorrectTimezoneConversion() {
|
|
|
|
date_default_timezone_set('UTC');
|
|
|
|
$date = '2012-01-01 10:00:00';
|
|
|
|
$converted = CakeTime::format($date, '%Y-%m-%d %H:%M:%S', '', 'Europe/Copenhagen');
|
|
|
|
$expected = new DateTime($date);
|
|
|
|
$expected->setTimezone(new DateTimeZone('Europe/Copenhagen'));
|
|
|
|
$this->assertEquals($expected->format('Y-m-d H:i:s'), $converted);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|