| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121 |
- <?php
- namespace Tools\TestCase\Utility;
- use Tools\Utility\Time;
- use Tools\TestSuite\TestCase;
- use Tools\Misc\ZodiacLib;
- use Cake\Core\Configure;
- class TimeTest extends TestCase {
- public $Time = null;
- public function setUp() {
- $this->Time = new Time();
- parent::setUp();
- }
- /**
- * TimeTest::testObject()
- *
- * @return void
- */
- public function testObject() {
- $this->assertTrue(is_object($this->Time));
- $this->assertInstanceOf('Tools\Utility\Time', $this->Time);
- }
- /**
- * Currently only works with timezoned localized values, not with UTC!!!
- *
- * @return void
- */
- public function testIncrementDate() {
- $timezone = Configure::read('Config.timezone');
- //$timezone = Date$this->Time->timezone();
- Configure::write('Config.timezone', 'Europe/Berlin');
- $phpTimezone = date_default_timezone_get();
- date_default_timezone_set('Europe/Berlin');
- $from = '2012-12-31';
- $Date = $this->Time->incrementDate($from, 0, 0);
- $this->assertSame($from, $Date->format(FORMAT_DB_DATE));
- $from = '2012-12-31';
- $Date = $this->Time->incrementDate($from, 0, 1);
- $this->assertSame('2013-01-31', $Date->format(FORMAT_DB_DATE));
- $from = '2012-12-31';
- $Date = $this->Time->incrementDate($from, 0, 2);
- $this->assertSame('2013-02-28', $Date->format(FORMAT_DB_DATE));
- $from = '2012-12-31';
- $Date = $this->Time->incrementDate($from, 0, 4);
- $this->assertSame('2013-04-30', $Date->format(FORMAT_DB_DATE));
- $from = '2012-12-31';
- $Date = $this->Time->incrementDate($from, 1, 0);
- $this->assertSame('2013-12-31', $Date->format(FORMAT_DB_DATE));
- // from leap year
- $from = '2008-02-29';
- $Date = $this->Time->incrementDate($from, 1, 0);
- $this->assertSame('2009-02-28', $Date->format(FORMAT_DB_DATE));
- // into leap year
- $from = '2007-02-28';
- $Date = $this->Time->incrementDate($from, 1, 0);
- $this->assertSame('2008-02-29', $Date->format(FORMAT_DB_DATE));
- // other direction
- $from = '2012-12-31';
- $Date = $this->Time->incrementDate($from, 0, -1);
- $this->assertSame('2012-11-30', $Date->format(FORMAT_DB_DATE));
- $from = '2012-12-31';
- $Date = $this->Time->incrementDate($from, -1, -1);
- $this->assertSame('2011-11-30', $Date->format(FORMAT_DB_DATE));
- // including days
- $from = '2012-12-31';
- $Date = $this->Time->incrementDate($from, 0, 1, 1);
- $this->assertSame('2013-02-01', $Date->format(FORMAT_DB_DATE));
- // including days
- $from = '2012-12-31';
- $Date = $this->Time->incrementDate($from, 0, 1, 5);
- $this->assertSame('2013-02-05', $Date->format(FORMAT_DB_DATE));
- Configure::write('Config.timezone', $timezone);
- date_default_timezone_set($phpTimezone);
- }
- /**
- * TimeTest::testNiceDate()
- *
- * @return void
- */
- public function testNiceDate() {
- $res = setlocale(LC_TIME, 'de_DE.UTF-8', 'deu_deu');
- //$this->assertTrue(!empty($res));
- $values = array(
- array('2009-12-01 00:00:00', FORMAT_NICE_YMD, '01.12.2009'),
- array('2009-12-01 00:00:00', FORMAT_NICE_M_FULL, 'December'),
- );
- foreach ($values as $v) {
- $result = $this->Time->niceDate($v[0], $v[1]);
- $this->assertEquals($v[2], $result);
- }
- $date = '2009-12-01 00:00:00';
- $format = FORMAT_NICE_YMD;
- $result = $this->Time->niceDate($date, $format, array('oclock' => true));
- $expected = '01.12.2009';
- $this->assertEquals($expected, $result);
- $date = '2009-12-01 00:00:00';
- $format = FORMAT_NICE_YMDHM;
- $result = $this->Time->niceDate($date, $format, array('oclock' => true));
- $expected = '01.12.2009, 00:00 ' . __d('tools', 'o\'clock');
- $this->assertEquals($expected, $result);
- }
- /**
- * TimeTest::testNiceTime()
- *
- * @return void
- */
- public function testNiceTime() {
- $result = $this->Time->niceTime('22:11:18');
- $expected = '22:11';
- $this->assertEquals($expected, $result);
- $result = $this->Time->niceTime('2014-11-12 22:11:18');
- $this->assertEquals($expected, $result);
- }
- /**
- * Test that input as date only (YYYY-MM-DD) does not suddendly return a
- * different date on output due to timezone differences.
- * Here the timezone should not apply since we only input date and only output
- * date (time itself is irrelevant).
- *
- * @return void
- */
- public function testDateWithTimezone() {
- $res = setlocale(LC_TIME, 'de_DE.UTF-8', 'deu_deu');
- //$this->assertTrue(!empty($res));
- Configure::write('Config.timezone', 'America/Anchorage');
- $ret = $this->Time->niceDate('2009-12-01');
- //debug($ret);
- $this->assertEquals('01.12.2009', $ret);
- $ret = $this->Time->localDate('2009-12-01');
- //debug($ret);
- $this->assertEquals('01.12.2009', $ret);
- }
- /**
- * TimeTest::testParseLocalizedDate()
- *
- * @return void
- */
- public function testParseLocalizedDate() {
- $ret = $this->Time->parseLocalizedDate('15-Feb-2009', 'j-M-Y', 'start');
- //$this->debug($ret);
- $this->assertEquals('2009-02-15 00:00:00', $ret);
- // problem when not passing months or days as well - no way of knowing how exact the date was
- $ret = $this->Time->parseLocalizedDate('2009', 'Y', 'start');
- //pr($ret);
- //$this->assertEquals($ret, '2009-01-01 00:00:00');
- $ret = $this->Time->parseLocalizedDate('Feb 2009', 'M Y', 'start');
- //pr($ret);
- //$this->assertEquals($ret, '2009-02-01 00:00:00');
- $values = array(
- array(__d('tools', 'Today'), array(date(FORMAT_DB_DATETIME, mktime(0, 0, 0, date('m'), date('d'), date('Y'))), date(FORMAT_DB_DATETIME, mktime(23, 59, 59, date('m'), date('d'), date('Y'))))),
- array('2010', array('2010-01-01 00:00:00', '2010-12-31 23:59:59')),
- array('23.02.2011', array('2011-02-23 00:00:00', '2011-02-23 23:59:59')),
- array('22/02/2011', array('2011-02-22 00:00:00', '2011-02-22 23:59:59')),
- array('3/2/11', array('2011-02-03 00:00:00', '2011-02-03 23:59:59')),
- //array('2/12/9', array('2009-12-02 00:00:00', '2009-12-02 23:59:59')),
- //array('12/2009', array('2009-12-01 00:00:00', '2009-12-31 23:59:59')),
- );
- foreach ($values as $v) {
- $ret = $this->Time->parseLocalizedDate($v[0], null, 'start');
- //pr($ret);
- $this->assertEquals($v[1][0], $ret);
- $ret = $this->Time->parseLocalizedDate($v[0], null, 'end');
- //pr($ret);
- $this->assertEquals($v[1][1], $ret);
- }
- }
- /**
- * TimeTest::testLocalDate()
- *
- * @return void
- */
- public function testLocalDate() {
- $this->skipIf(php_sapi_name() === 'cli', 'for now');
- $res = setlocale(LC_TIME, array('de_DE.UTF-8', 'deu_deu'));
- $this->assertTrue(!empty($res));
- $values = array(
- array('2009-12-01 00:00:00', FORMAT_LOCAL_YMD, '01.12.2009'),
- array('2009-12-01 00:00:00', FORMAT_LOCAL_M_FULL, 'Dezember'),
- );
- foreach ($values as $v) {
- $ret = $this->Time->localDate($v[0], $v[1]);
- //$this->debug($ret);
- $this->assertEquals($v[2], $ret);
- }
- $date = '2009-12-01 00:00:00';
- $format = FORMAT_LOCAL_YMD;
- $result = $this->Time->localDate($date, $format, array('oclock' => true));
- $expected = '01.12.2009';
- $this->assertEquals($expected, $result);
- $date = '2009-12-01 00:00:00';
- $format = FORMAT_LOCAL_YMDHM;
- $result = $this->Time->localDate($date, $format, array('oclock' => true));
- $expected = '01.12.2009, 00:00 ' . __d('tools', 'o\'clock');
- $this->assertEquals($expected, $result);
- }
- /**
- * TimeTest::testPeriod()
- *
- * @return void
- */
- public function testPeriod() {
- //$this->out($this->_header(__FUNCTION__), true);
- $values = array(
- array(__d('tools', 'Today'), array(date(FORMAT_DB_DATETIME, mktime(0, 0, 0, date('m'), date('d'), date('Y'))), date(FORMAT_DB_DATETIME, mktime(23, 59, 59, date('m'), date('d'), date('Y'))))),
- array('2010', array('2010-01-01 00:00:00', '2010-12-31 23:59:59')),
- array('2011-02', array('2011-02-01 00:00:00', '2011-02-28 23:59:59')),
- array('2012-02', array('2012-02-01 00:00:00', '2012-02-29 23:59:59')),
- array('2010-02-23', array('2010-02-23 00:00:00', '2010-02-23 23:59:59')),
- array('2010-02-23 bis 2010-02-26', array('2010-02-23 00:00:00', '2010-02-26 23:59:59')),
- //array('2010-02-23 11:11:11 bis 2010-02-23 11:12:01', array('2010-02-23 11:11:11', '2010-02-23 11:12:01')),
- // localized
- array('23.02.2011', array('2011-02-23 00:00:00', '2011-02-23 23:59:59')),
- array('23.2.2010 bis 26.2.2011', array('2010-02-23 00:00:00', '2011-02-26 23:59:59')),
- );
- foreach ($values as $v) {
- $ret = $this->Time->period($v[0]);
- //pr($ret);
- $this->assertEquals($v[1], $ret);
- }
- }
- /**
- * TimeTest::testPeriodAsSql()
- *
- * @return void
- */
- public function testPeriodAsSql() {
- //$this->out($this->_header(__FUNCTION__), true);
- $values = array(
- array(__d('tools', 'Today'), "(Model.field >= '" . date(FORMAT_DB_DATE) . " 00:00:00') AND (Model.field <= '" . date(FORMAT_DB_DATE) . " 23:59:59')"),
- array(__d('tools', 'Yesterday') . ' ' . __d('tools', 'until') . ' ' . __d('tools', 'Today'), "(Model.field >= '" . date(FORMAT_DB_DATE, time() - DAY) . " 00:00:00') AND (Model.field <= '" . date(FORMAT_DB_DATE) . " 23:59:59')"),
- array(__d('tools', 'Today') . ' ' . __d('tools', 'until') . ' ' . __d('tools', 'Tomorrow'), "(Model.field >= '" . date(FORMAT_DB_DATE, time()) . " 00:00:00') AND (Model.field <= '" . date(FORMAT_DB_DATE, time() + DAY) . " 23:59:59')"),
- array(__d('tools', 'Yesterday') . ' ' . __d('tools', 'until') . ' ' . __d('tools', 'Tomorrow'), "(Model.field >= '" . date(FORMAT_DB_DATE, time() - DAY) . " 00:00:00') AND (Model.field <= '" . date(FORMAT_DB_DATE, time() + DAY) . " 23:59:59')"),
- );
- foreach ($values as $v) {
- $ret = $this->Time->periodAsSql($v[0], 'Model.field');
- //pr($v[1]);
- //pr($ret);
- $this->assertSame($v[1], $ret);
- }
- }
- /**
- * TimeTest::testDifference()
- *
- * @return void
- */
- public function testDifference() {
- //$this->out($this->_header(__FUNCTION__), true);
- $values = array(
- array('2010-02-23 11:11:11', '2010-02-23 11:12:01', 50),
- array('2010-02-23 11:11:11', '2010-02-24 11:12:01', DAY + 50)
- );
- foreach ($values as $v) {
- $ret = $this->Time->difference($v[0], $v[1]);
- $this->assertEquals($v[2], $ret);
- }
- }
- /**
- * TimeTest::testIsLeapYear()
- *
- * @return void
- */
- public function testIsLeapYear() {
- $this->Time = new Time('2001-01-01');
- $is = $this->Time->isLeapYear();
- $this->assertFalse($is);
- $this->Time = new Time('2008-01-01');
- $is = $this->Time->isLeapYear();
- $this->assertTrue($is);
- $this->Time = new Time('2000-01-01');
- $is = $this->Time->isLeapYear();
- $this->assertTrue($is);
- }
- /**
- * TimeTest::testIsInRange()
- *
- * @return void
- */
- public function testIsInRange() {
- $is = $this->Time->isInRange(date(FORMAT_DB_DATETIME, time() + 22 * HOUR), DAY);
- $this->assertTrue($is);
- $is = $this->Time->isInRange(date(FORMAT_DB_DATETIME, time() + 26 * HOUR), DAY);
- $this->assertFalse($is);
- $day = date(FORMAT_DB_DATETIME, time() + 10 * DAY);
- $this->assertTrue($this->Time->isInRange($day, 11 * DAY));
- $this->assertTrue($this->Time->isInRange($day, 10 * DAY));
- $this->assertFalse($this->Time->isInRange($day, 9 * DAY));
- $day = date(FORMAT_DB_DATETIME, time() - 78 * DAY);
- $this->assertTrue($this->Time->isInRange($day, 79 * DAY));
- $this->assertTrue($this->Time->isInRange($day, 78 * DAY));
- $this->assertFalse($this->Time->isInRange($day, 77 * DAY));
- }
- /**
- * Test age
- *
- * @return void
- */
- public function testAge() {
- $this->assertEquals('0', $this->Time->age(null));
- list($year, $month, $day) = explode('-', date('Y-m-d'));
- $this->assertEquals('0', $this->Time->age($year . '-' . $month . '-' . $day, null));
- list($year, $month, $day) = explode('-', date('Y-m-d', strtotime('-10 years')));
- $this->assertEquals('10', $this->Time->age($year . '-' . $month . '-' . $day, null));
- list($year, $month, $day) = explode('-', date('Y-m-d', strtotime('-10 years +1 day')));
- $this->assertEquals('9', $this->Time->age($year . '-' . $month . '-' . $day, null));
- list($year, $month, $day) = explode('-', date('Y-m-d', strtotime('-10 years -1 day')));
- $this->assertEquals('10', $this->Time->age($year . '-' . $month . '-' . $day, null));
- // Crossing years
- $this->assertEquals('2', $this->Time->age('2005-12-01', '2008-02-29'));
- $this->assertEquals('6', $this->Time->age('2002-01-29', '2008-12-02'));
- // Leap year
- $this->assertEquals('2', $this->Time->age('2005-03-01', '2008-02-28'));
- $this->assertEquals('2', $this->Time->age('2005-03-01', '2008-02-29'));
- $this->assertEquals('3', $this->Time->age('2005-03-01', '2008-03-01'));
- $this->assertEquals('3', $this->Time->age('2005-02-29', '2008-03-01'));
- // Future
- list($yearE, $monthE, $dayE) = explode('-', date('Y-m-d', strtotime('+10 years -1 day')));
- $this->assertEquals('9', $this->Time->age(null, $yearE . '-' . $monthE . '-' . $dayE));
- list($yearE, $monthE, $dayE) = explode('-', date('Y-m-d', strtotime('+10 years +1 day')));
- $this->assertEquals('10', $this->Time->age(null, $yearE . '-' . $monthE . '-' . $dayE));
- $birthday = '2033-04-09';
- $this->assertEquals(-1, $this->Time->age($birthday));
- $birthday = '1985-04-08';
- $relativeDate = '2010-04-07';
- $this->assertEquals('24', $this->Time->age($birthday, $relativeDate));
- $relativeDate = '2010-04-08';
- $this->assertEquals('25', $this->Time->age($birthday, $relativeDate));
- $relativeDate = '2010-04-09';
- $this->assertEquals('25', $this->Time->age($birthday, $relativeDate));
- }
- /**
- * TimeTest::testAgeBounds()
- *
- * @return void
- */
- public function testAgeBounds() {
- //$this->out($this->_header(__FUNCTION__), true);
- $values = array(
- array(20, 20, array('min' => '1990-07-07', 'max' => '1991-07-06')),
- array(10, 30, array('min' => '1980-07-07', 'max' => '2001-07-06')),
- array(11, 12, array('min' => '1998-07-07', 'max' => '2000-07-06'))
- );
- foreach ($values as $v) {
- //echo $v[0].'/'.$v[1];
- $ret = $this->Time->ageBounds($v[0], $v[1], true, '2011-07-06'); //TODO: relative time
- //pr($ret);
- if (isset($v[2])) {
- $this->assertSame($v[2], $ret);
- $this->assertEquals($v[0], $this->Time->age($v[2]['max'], '2011-07-06'));
- $this->assertEquals($v[1], $this->Time->age($v[2]['min'], '2011-07-06'));
- }
- }
- }
- /**
- * TimeTest::testAgeByYear()
- *
- * @return void
- */
- public function testAgeByYear() {
- //$this->out($this->_header(__FUNCTION__), true);
- // year only
- $is = $this->Time->ageByYear(2000);
- //$this->out($is);
- $this->assertEquals((date('Y') - 2001) . '/' . (date('Y') - 2000), $is);
- $is = $this->Time->ageByYear(1985);
- $this->assertEquals((date('Y') - 1986) . '/' . (date('Y') - 1985), $is);
- // with month
- if (($month = date('n') + 1) <= 12) {
- $is = $this->Time->ageByYear(2000, $month);
- //$this->out($is);
- //$this->assertEquals($is, (date('Y')-2001).'/'.(date('Y')-2000), null, '2000/'.$month);
- $this->assertSame(date('Y') - 2001, $is); //null, '2000/'.$month
- }
- if (($month = date('n') - 1) >= 1) {
- $is = $this->Time->ageByYear(2000, $month);
- //$this->out($is);
- //$this->assertEquals($is, (date('Y')-2001).'/'.(date('Y')-2000), null, '2000/'.$month);
- $this->assertSame(date('Y') - 2000, $is); //null, '2000/'.$month)
- }
- }
- /**
- * TimeTest::testDaysInMonth()
- *
- * @return void
- */
- public function testDaysInMonth() {
- //$this->out($this->_header(__FUNCTION__), true);
- $ret = $this->Time->daysInMonth('2004', '3');
- $this->assertEquals(31, $ret);
- $ret = $this->Time->daysInMonth('2006', '4');
- $this->assertEquals(30, $ret);
- $ret = $this->Time->daysInMonth('2007', '2');
- $this->assertEquals(28, $ret);
- $ret = $this->Time->daysInMonth('2008', '2');
- $this->assertEquals(29, $ret);
- }
- /**
- * TimeTest::testDay()
- *
- * @return void
- */
- public function testDayName() {
- //$this->out($this->_header(__FUNCTION__), true);
- $ret = $this->Time->dayName('0');
- $this->assertEquals(__d('tools', 'Sunday'), $ret);
- $ret = $this->Time->dayName(2, true);
- $this->assertEquals(__d('tools', 'Tue'), $ret);
- $ret = $this->Time->dayName(6);
- $this->assertEquals(__d('tools', 'Saturday'), $ret);
- $ret = $this->Time->dayName(6, false, 1);
- $this->assertEquals(__d('tools', 'Sunday'), $ret);
- $ret = $this->Time->dayName(0, false, 2);
- $this->assertEquals(__d('tools', 'Tuesday'), $ret);
- $ret = $this->Time->dayName(1, false, 6);
- $this->assertEquals(__d('tools', 'Sunday'), $ret);
- }
- /**
- * TimeTest::testMonth()
- *
- * @return void
- */
- public function testMonthName() {
- //$this->out($this->_header(__FUNCTION__), true);
- $ret = $this->Time->monthName('11');
- $this->assertEquals(__d('tools', 'November'), $ret);
- $ret = $this->Time->monthName(1);
- $this->assertEquals(__d('tools', 'January'), $ret);
- $ret = $this->Time->monthName(2, true, array('appendDot' => true));
- $this->assertEquals(__d('tools', 'Feb') . '.', $ret);
- $ret = $this->Time->monthName(5, true, array('appendDot' => true));
- $this->assertEquals(__d('tools', 'May'), $ret);
- }
- /**
- * TimeTest::testDays()
- *
- * @return void
- */
- public function testDayNames() {
- //$this->out($this->_header(__FUNCTION__), true);
- $ret = $this->Time->dayNames();
- $this->assertTrue(count($ret) === 7);
- }
- /**
- * TimeTest::testMonths()
- *
- * @return void
- */
- public function testMonthNames() {
- //$this->out($this->_header(__FUNCTION__), true);
- $ret = $this->Time->monthNames();
- $this->assertTrue(count($ret) === 12);
- }
- /**
- * TimeTest::testRelLengthOfTime()
- *
- * @return void
- */
- public function testRelLengthOfTime() {
- $ret = $this->Time->relLengthOfTime('1990-11-20');
- //pr($ret);
- $ret = $this->Time->relLengthOfTime('2012-11-20');
- //pr($ret);
- $res = $this->Time->relLengthOfTime(date(FORMAT_DB_DATETIME, time() - 3600));
- //pr($res);
- $this->assertTrue(!empty($res));
- $res = $this->Time->relLengthOfTime(date(FORMAT_DB_DATETIME, time() - 4 * DAY - 5 * HOUR), null, array('plural' => 'n'));
- //pr($res);
- //$this->assertEquals($res, 'Vor 4 Tagen, 5 '.__d('tools', 'Hours'));
- $this->assertEquals(__d('tools', '{0} ago', '4 ' . __d('tools', 'Days') . ', ' . '5 ' . __d('tools', 'Hours')), $res);
- $res = $this->Time->relLengthOfTime(date(FORMAT_DB_DATETIME, time() + 4 * DAY + 5 * HOUR), null, array('plural' => 'n'));
- //pr($res);
- $this->assertEquals(__d('tools', 'In {0}', '4 ' . __d('tools', 'Days') . ', ' . '5 ' . __d('tools', 'Hours')), $res);
- $res = $this->Time->relLengthOfTime(date(FORMAT_DB_DATETIME, time()), null, array('plural' => 'n'));
- //pr($res);
- $this->assertEquals($res, __d('tools', 'justNow'));
- }
- /**
- * Test cweek
- *
- * @return void
- */
- public function testCweek() {
- $year = 2008;
- $month = 12;
- $day = 29;
- $date = mktime(0, 0, 0, $month, $day, $year);
- $this->assertEquals('01/' . $year, $this->Time->cweek($year . '-' . $month . '-' . $day));
- $year = 2009;
- $month = 1;
- $day = 1;
- $date = mktime(0, 0, 0, $month, $day, $year);
- $this->assertEquals('01/' . $year, $this->Time->cweek($year . '-' . $month . '-' . $day));
- $year = 2009;
- $month = 1;
- $day = 9;
- $date = mktime(0, 0, 0, $month, $day, $year);
- $this->assertEquals('02/' . $year, $this->Time->cweek($year . '-' . $month . '-' . $day . ' 00:00:00'));
- $year = 2009;
- $month = 12;
- $day = 26;
- $date = mktime(0, 0, 0, $month, $day, $year);
- $this->assertEquals('52/' . $year, $this->Time->cweek($year . '-' . $month . '-' . $day));
- }
- /**
- * Test IsInTheFuture
- *
- * @return void
- */
- public function testIsInTheFuture() {
- $testDate = date(FORMAT_DB_DATE, time() + 2 * DAY);
- $is = $this->Time->isInTheFuture($testDate);
- $this->assertTrue($is);
- $testDate = date(FORMAT_DB_DATETIME, time() - 1 * MINUTE);
- $is = $this->Time->isInTheFuture($testDate);
- $this->assertFalse($is);
- }
- /**
- * Test IsNotTodayAndInTheFuture
- *
- * @return void
- */
- public function testIsNotTodayAndInTheFuture() {
- $testDate = date(FORMAT_DB_DATE, time());
- $is = $this->Time->isNotTodayAndInTheFuture($testDate);
- $this->assertFalse($is);
- $testDate = date(FORMAT_DB_DATETIME, time() + 1 * DAY);
- $is = $this->Time->isNotTodayAndInTheFuture($testDate);
- $this->assertTrue($is);
- }
- /**
- * Test IsDayAfterTomorrow
- *
- * @return void
- */
- public function testIsDayAfterTomorrow() {
- $testDate = date(FORMAT_DB_DATE, time() + 2 * DAY);
- $is = $this->Time->isDayAfterTomorrow($testDate);
- $this->assertTrue($is);
- $testDate = date(FORMAT_DB_DATETIME, time() - 1 * MINUTE);
- $is = $this->Time->isDayAfterTomorrow($testDate);
- $this->assertFalse($is);
- }
- /**
- * TimeTest::testLengthOfTime()
- *
- * @return void
- */
- public function testLengthOfTime() {
- //$this->out($this->_header(__FUNCTION__), true);
- $ret = $this->Time->lengthOfTime(60);
- //pr($ret);
- // FIX ME! Doesn't work!
- $ret = $this->Time->lengthOfTime(-60);
- //pr($ret);
- $ret = $this->Time->lengthOfTime(-121);
- //pr($ret);
- $this->assertEquals('6 ' . __d('tools', 'Minutes') . ', 40 ' . __d('tools', 'Seconds'), $this->Time->lengthOfTime(400));
- $res = $this->Time->lengthOfTime(400, 'i');
- //pr($res);
- $this->assertEquals('6 ' . __d('tools', 'Minutes'), $res);
- $res = $this->Time->lengthOfTime(6 * DAY);
- //pr($res);
- $this->assertEquals('6 ' . __d('tools', 'Days') . ', 0 ' . __d('tools', 'Hours'), $res);
- }
- /**
- * TimeTest::testFuzzyFromOffset()
- *
- * @return void
- */
- public function testFuzzyFromOffset() {
- //$this->out($this->_header(__FUNCTION__), true);
- $ret = $this->Time->fuzzyFromOffset(MONTH);
- //pr($ret);
- $ret = $this->Time->fuzzyFromOffset(120);
- //pr($ret);
- $ret = $this->Time->fuzzyFromOffset(DAY);
- //pr($ret);
- $ret = $this->Time->fuzzyFromOffset(DAY + 2 * MINUTE);
- //pr($ret);
- // FIX ME! Doesn't work!
- $ret = $this->Time->fuzzyFromOffset(-DAY);
- //pr($ret);
- }
- /**
- * TimeTest::testCweekMod()
- *
- * @return void
- */
- public function testCweekMod() {
- $result = $this->Time->cWeekMod(0);
- $this->assertEquals(0, $result);
- $result = $this->Time->cWeekMod(1);
- $this->assertEquals(1, $result);
- $result = $this->Time->cWeekMod(6);
- $this->assertEquals(0, $result);
- }
- /**
- * TimeTest::testTimezoneByCoordinates()
- *
- * @return void
- */
- public function testTimezoneByCoordinates() {
- $this->skipIf(true);
- $result = $this->Time->timezoneByCoordinates(48, 11);
- $this->assertEquals('Europe/Vaduz', $result);
- }
- public function testCweeks() {
- //$this->out($this->_header(__FUNCTION__), true);
- $ret = $this->Time->cweeks('2004');
- $this->assertEquals(53, $ret);
- $ret = $this->Time->cweeks('2010');
- $this->assertEquals(52, $ret);
- $ret = $this->Time->cweeks('2006');
- $this->assertEquals(52, $ret);
- $ret = $this->Time->cweeks('2007');
- $this->assertEquals(52, $ret);
- /*
- for ($i = 1990; $i < 2020; $i++) {
- //$this->out($this->Time->cweeks($i).BR;
- }
- */
- }
- public function testCweekBeginning() {
- //$this->out($this->_header(__FUNCTION__), true);
- $values = array(
- '2001' => 978303600, # Mon 01.01.2001, 00:00
- '2006' => 1136156400, # Mon 02.01.2006, 00:00
- '2010' => 1262559600, # Mon 04.01.2010, 00:00
- '2013' => 1356908400, # Mon 31.12.2012, 00:00
- );
- foreach ($values as $year => $expected) {
- $ret = $this->Time->cweekBeginning($year);
- //$this->out($ret);
- //$this->out($this->Time->niceDate($ret, 'D') . ' ' . $this->Time->niceDate($ret, FORMAT_NICE_YMDHMS));
- //$this->assertEquals($ret, $expected, null, $year);
- $this->assertTrue($ret <= $expected + HOUR && $ret >= $expected);
- }
- $values = array(
- array('2001', '1', 978303600), # Mon 01.01.2001, 00:00:00
- array('2001', '2', 978908400), # Mon 08.01.2001, 00:00:00
- array('2001', '5', 980722800), # Mon 29.01.2001, 00:00:00
- array('2001', '52', 1009148400), # Mon 24.12.2001, 00:00:00
- array('2013', '11', 1362956400), # Mon 11.03.2013, 00:00:00
- array('2006', '3', 1137366000), # Mon 16.01.2006, 00:00:00
- );
- foreach ($values as $v) {
- $ret = $this->Time->cweekBeginning($v[0], $v[1]);
- //$this->out($ret);
- //$this->out($this->Time->niceDate($ret, 'D') . ' ' . $this->Time->niceDate($ret, FORMAT_NICE_YMDHMS));
- //$this->assertSame($v[2], $ret, null, $v[1].'/'.$v[0]);
- $this->assertTrue($ret <= $v[2] + HOUR && $ret >= $v[2]);
- }
- }
- public function testCweekEnding() {
- //$this->out($this->_header(__FUNCTION__), true);
- $values = array(
- '2001' => 1009753199, # Sun 30.12.2001, 23:59:59
- '2006' => 1167605999, # Sun 31.12.2006, 23:59:59
- '2010' => 1294009199, # Sun 02.01.2011, 23:59:59
- '2013' => 1388357999, # Sun 29.12.2013, 23:59:59
- );
- foreach ($values as $year => $expected) {
- $ret = $this->Time->cweekEnding($year);
- //$this->out($ret);
- //$this->out($this->Time->niceDate($ret, 'D') . ' ' . $this->Time->niceDate($ret, FORMAT_NICE_YMDHMS));
- //$this->assertSame($expected, $ret);
- $this->assertTrue($ret <= $expected + HOUR && $ret >= $expected);
- }
- $values = array(
- array('2001', '1', 978908399), # Sun 07.01.2001, 23:59:59
- array('2001', '2', 979513199), # Sun 14.01.2001, 23:59:59
- array('2001', '5', 981327599), # Sun 04.02.2001, 23:59:59
- array('2001', '52', 1009753199), # Sun 30.12.2001, 23:59:59
- array('2013', '11', 1363561199), # Sun 17.03.2013, 23:59:59
- array('2006', '3', 1137970799), # Sun 22.01.2006, 23:59:59
- );
- foreach ($values as $v) {
- $ret = $this->Time->cweekEnding($v[0], $v[1]);
- //$this->out($ret);
- //$this->out($this->Time->niceDate($ret, 'D') . ' ' . $this->Time->niceDate($ret, FORMAT_NICE_YMDHMS));
- //$this->assertSame($v[2], $ret, null, $v[1].'/'.$v[0]);
- $this->assertTrue($ret <= $v[2] + HOUR && $ret >= $v[2]);
- }
- }
- /**
- * TimeTest::testAgeByHoroscop()
- *
- * @return void
- */
- public function testAgeByHoroscop() {
- $this->skipIf(php_sapi_name() === 'cli', 'Fix these tests');
- $is = $this->Time->ageByHoroscope(2000, ZodiacLib::SIGN_VIRGO);
- // between xxxx-08-24 and xxxx-09-23 the latter, otherwise the first:
- $this->assertEquals(date('Y') - 2000 - 1, $is);
- $this->assertEquals(array(date('Y') - 2000 - 1, date('Y') - 2000), $is);
- $is = $this->Time->ageByHoroscope(1991, ZodiacLib::SIGN_LIBRA);
- //pr($is);
- $this->assertEquals(date('Y') - 1991 - 1, $is);
- $is = $this->Time->ageByHoroscope(1986, ZodiacLib::SIGN_CAPRICORN);
- //pr($is);
- $this->assertEquals(array(date('Y') - 1986 - 1, date('Y') - 1986), $is);
- $is = $this->Time->ageByHoroscope(2000, ZodiacLib::SIGN_SCORPIO);
- //debug($is);
- $this->assertEquals(date('Y') - 2000 - 1, $is); //array(10, 11)
- }
- /**
- * TimeTest::testAgeRange()
- *
- * @return void
- */
- public function testAgeRange() {
- $is = $this->Time->ageRange(2000);
- $this->assertEquals(date('Y') - 2000 - 1, $is);
- $is = $this->Time->ageRange(date('Y') - 11, null, null, 5);
- $this->assertEquals(array(6, 10), $is);
- $is = $this->Time->ageRange(date('Y') - 13, null, null, 5);
- $this->assertEquals(array(11, 15), $is);
- $is = $this->Time->ageRange(1985, 23, 11);
- $this->assertEquals(date('Y') - 1985 - 1, $is);
- $is = $this->Time->ageRange(date('Y') - 29, null, null, 6);
- $this->assertEquals(array(25, 30), $is);
- $is = $this->Time->ageRange(date('Y') - 29, 21, 11, 7);
- $this->assertEquals(array(22, 28), $is);
- }
- /**
- * TimeTest::testParseDate()
- *
- * @return void
- */
- public function testParseDate() {
- //echo $this->_header(__FUNCTION__);
- $tests = array(
- '2010-12-11' => 1292022000,
- '2010-01-02' => 1262386800,
- '10-01-02' => 1262386800,
- '2.1.2010' => 1262386800,
- '2.1.10' => 1262386800,
- '02.01.10' => 1262386800,
- '02.01.2010' => 1262386800,
- '02.01.2010 22:11' => 1262386800,
- '2010-01-02 22:11' => 1262386800,
- );
- foreach ($tests as $was => $expected) {
- $is = $this->Time->parseDate($was);
- $this->assertTrue($is <= $expected + HOUR && $is >= $expected);
- }
- }
- /**
- * TimeTest::testParseTime()
- *
- * @return void
- */
- public function testParseTime() {
- //echo $this->_header(__FUNCTION__);
- $tests = array(
- '2:4' => 7440,
- '2:04' => 7440,
- '2' => 7200,
- '1,5' => 3600 + 1800,
- '1.5' => 3600 + 1800,
- '1.50' => 3600 + 1800,
- '1.01' => 3660,
- ':4' => 240,
- ':04' => 240,
- ':40' => 40 * MINUTE,
- '1:2:4' => 1 * HOUR + 2 * MINUTE + 4 * SECOND,
- '01:2:04' => 1 * HOUR + 2 * MINUTE + 4 * SECOND,
- '0:2:04' => 2 * MINUTE + 4 * SECOND,
- '::4' => 4 * SECOND,
- '::04' => 4 * SECOND,
- '::40' => 40 * SECOND,
- '2011-11-12 10:10:10' => 10 * HOUR + 10 * MINUTE + 10 * SECOND,
- );
- // positive
- foreach ($tests as $was => $expected) {
- $is = $this->Time->parseTime($was);
- //pr($is);
- $this->assertEquals($expected, $is);
- }
- unset($tests['2011-11-12 10:10:10']);
- // negative
- foreach ($tests as $was => $expected) {
- $is = $this->Time->parseTime('-' . $was);
- //pr($is);
- $this->assertEquals(-$expected, $is);
- }
- }
- /**
- * TimeTest::testBuildTime()
- *
- * @return void
- */
- public function testBuildTime() {
- //echo $this->_header(__FUNCTION__);
- $tests = array(
- 7440 => '2:04',
- 7220 => '2:00', # 02:00:20 => rounded to 2:00:00
- 5400 => '1:30',
- 3660 => '1:01',
- );
- // positive
- foreach ($tests as $was => $expected) {
- $is = $this->Time->buildTime($was);
- //pr($is);
- $this->assertEquals($expected, $is);
- }
- // negative
- foreach ($tests as $was => $expected) {
- $is = $this->Time->buildTime(-$was);
- //pr($is);
- $this->assertEquals('-' . $expected, $is);
- }
- }
- /**
- * TimeTest::testBuildDefaultTime()
- *
- * @return void
- */
- public function testBuildDefaultTime() {
- //echo $this->_header(__FUNCTION__);
- $tests = array(
- 7440 => '02:04:00',
- 7220 => '02:00:20',
- 5400 => '01:30:00',
- 3660 => '01:01:00',
- 1 * HOUR + 2 * MINUTE + 4 * SECOND => '01:02:04',
- );
- foreach ($tests as $was => $expected) {
- $is = $this->Time->buildDefaultTime($was);
- //pr($is);
- $this->assertEquals($expected, $is);
- }
- }
- /**
- * 9.30 => 9.50
- *
- * @return void
- */
- public function testStandardDecimal() {
- //echo $this->_header(__FUNCTION__);
- $value = '9.30';
- $is = $this->Time->standardToDecimalTime($value);
- $this->assertEquals('9.50', round($is, 2));
- $value = '9.3';
- $is = $this->Time->standardToDecimalTime($value);
- $this->assertEquals('9.50', round($is, 2));
- }
- /**
- * 9.50 => 9.30
- *
- * @return void
- */
- public function testDecimalStandard() {
- //echo $this->_header(__FUNCTION__);
- $value = '9.50';
- $is = $this->Time->decimalToStandardTime($value);
- $this->assertEquals('9.3', round($is, 2));
- $value = '9.5';
- $is = $this->Time->decimalToStandardTime($value);
- //pr($is);
- $this->assertEquals('9.3', $is);
- $is = $this->Time->decimalToStandardTime($value, 2);
- //pr($is);
- $this->assertEquals('9.30', $is);
- $is = $this->Time->decimalToStandardTime($value, 2, ':');
- //pr($is);
- $this->assertEquals('9:30', $is);
- }
- /**
- * TimeTest::testHasDaylightSavingTime()
- *
- * @return void
- */
- public function testHasDaylightSavingTime() {
- $timezone = 'Europe/Berlin';
- $x = $this->Time->hasDaylightSavingTime($timezone);
- $this->assertTrue($x);
- $timezone = 'Asia/Baghdad';
- $x = $this->Time->hasDaylightSavingTime($timezone);
- $this->assertFalse($x);
- }
- /**
- * TimeTest::testTimezone()
- *
- * @return void
- */
- public function testGetTimezone() {
- $timezone = $this->Time->getTimezone();
- // usually UTC
- $name = $timezone->getName();
- //$this->debug($name);
- $this->assertTrue(!empty($name));
- $location = $timezone->getLocation();
- //$this->debug($location);
- $this->assertTrue(!empty($location['country_code']));
- $this->assertTrue(isset($location['latitude']));
- $this->assertTrue(isset($location['longitude']));
- $offset = $timezone->getOffset(new \DateTime('@' . mktime(0, 0, 0, 1, 1, date('Y'))));
- //$this->debug($offset);
- $phpTimezone = date_default_timezone_get();
- $this->assertEquals($name, $phpTimezone);
- }
- /**
- * TimeTest::testGetGmtOffset()
- *
- * @return void
- */
- public function testGetGmtOffset() {
- $timezone = Configure::read('Config.timezone');
- $phpTimezone = date_default_timezone_get();
- Configure::write('Config.timezone', 'UTC');
- date_default_timezone_set('UTC');
- $result = $this->Time->getGmtOffset();
- $this->assertEquals(0, $result);
- $result = $this->Time->getGmtOffset('Europe/Berlin');
- $this->assertTrue($result > 0, $result);
- $result = $this->Time->getGmtOffset('America/Los_Angeles');
- $this->assertTrue($result < 0, $result);
- Configure::write('Config.timezone', $timezone);
- date_default_timezone_set($phpTimezone);
- }
- /**
- * DatetimeHelperTest::testTzOffset()
- *
- * @return void
- */
- public function testTzOffset() {
- $timezone = Configure::read('Config.timezone');
- $phpTimezone = date_default_timezone_get();
- Configure::write('Config.timezone', 'UTC');
- date_default_timezone_set('UTC');
- $result = $this->Time->tzOffset(0, false);
- $this->assertEquals(0, $result);
- Configure::write('Config.timezone', 'Europe/Berlin');
- date_default_timezone_set('Europe/Berlin');
- $factor = date('I') ? 2 : 1;
- $result = $this->Time->tzOffset($factor * HOUR, false);
- $this->assertEquals(0, $result);
- Configure::write('Config.timezone', $timezone);
- date_default_timezone_set($phpTimezone);
- }
- }
|