📄 time.test.php
字号:
* testOfNiceShort method * * @access public * @return void */ function testOfNiceShort() { $time = time() + 2 * DAY; if (date('Y', $time) == date('Y')) { $this->assertEqual(date('M jS, H:i', $time), $this->Time->niceShort($time)); } else { $this->assertEqual(date('M jSY, H:i', $time), $this->Time->niceShort($time)); } $time = time(); $this->assertEqual('Today, '.date('H:i', $time), $this->Time->niceShort($time)); $time = time() - DAY; $this->assertEqual('Yesterday, '.date('H:i', $time), $this->Time->niceShort($time)); } /** * testOfDaysAsSql method * * @access public * @return void */ function testOfDaysAsSql() { $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->assertEqual($expected, $this->Time->daysAsSql($begin, $end, $field)); }/** * testOfDayAsSql method * * @access public * @return void */ function testOfDayAsSql() { $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->assertEqual($expected, $this->Time->dayAsSql($time, $field)); }/** * testToUnix method * * @access public * @return void */ function testToUnix() { $this->assertEqual(time(), $this->Time->toUnix(time())); $this->assertEqual(strtotime('+1 day'), $this->Time->toUnix('+1 day')); $this->assertEqual(strtotime('+0 days'), $this->Time->toUnix('+0 days')); $this->assertEqual(strtotime('-1 days'), $this->Time->toUnix('-1 days')); $this->assertEqual(false, $this->Time->toUnix('')); $this->assertEqual(false, $this->Time->toUnix(null)); }/** * testOfToAtom method * * @access public * @return void */ function testOfToAtom() { $this->assertEqual(date('Y-m-d\TH:i:s\Z'), $this->Time->toAtom(time())); }/** * testOfToRss method * * @access public * @return void */ function testOfToRss() { $this->assertEqual(date('r'), $this->Time->toRss(time())); }/** * testOfFormat method * * @access public * @return void */ function testOfFormat() { $format = 'D-M-Y'; $arr = array(time(), strtotime('+1 days'), strtotime('+1 days'), strtotime('+0 days')); foreach ($arr as $val) { $this->assertEqual(date($format, $val), $this->Time->format($format, $val)); } $result = $this->Time->format('Y-m-d', null, 'never'); $this->assertEqual($result, 'never'); }/** * testOfGmt method * * @access public * @return void */ function testOfGmt() { $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->assertEqual($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->assertEqual($expected, $this->Time->gmt(null)); }/** * testOfIsToday method * * @access public * @return void */ function testOfIsToday() { $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); } /** * testOfIsThisWeek method * * @access public * @return void */ function testOfIsThisWeek() { switch (date('D')) { case 'Mon' : for ($i = 0; $i < 6; $i++) { $this->assertTrue($this->Time->isThisWeek("+$i days")); } $this->assertFalse($this->Time->isThisWeek("+7 days")); $this->assertFalse($this->Time->isThisWeek("-1 days")); break; case 'Tue' : for ($i = -1; $i < 5; $i++) { $this->assertTrue($this->Time->isThisWeek("+$i days")); } $this->assertFalse($this->Time->isThisWeek("+6 days")); $this->assertFalse($this->Time->isThisWeek("-2 days")); break; case 'Wed' : for ($i = -2; $i < 5; $i++) { $this->assertTrue($this->Time->isThisWeek("+$i days")); } $this->assertFalse($this->Time->isThisWeek("+5 days")); $this->assertFalse($this->Time->isThisWeek("-3 days")); break; case 'Thu' : for ($i = -3; $i < 4; $i++) { $this->assertTrue($this->Time->isThisWeek("+$i days")); } $this->assertFalse($this->Time->isThisWeek("+4 days")); $this->assertFalse($this->Time->isThisWeek("-4 days")); break; case 'Fri' : for ($i = -4; $i < 3; $i++) { $this->assertTrue($this->Time->isThisWeek("+$i days")); } $this->assertFalse($this->Time->isThisWeek("+3 days")); $this->assertFalse($this->Time->isThisWeek("-5 days")); break; case 'Sat' : for ($i = -5; $i < 2; $i++) { $this->assertTrue($this->Time->isThisWeek("+$i days")); } $this->assertFalse($this->Time->isThisWeek("+2 days")); $this->assertFalse($this->Time->isThisWeek("-6 days")); break; case 'Sun' : for ($i = -6; $i < 1; $i++) { $this->assertTrue($this->Time->isThisWeek("+$i days")); } $this->assertFalse($this->Time->isThisWeek("+1 days")); $this->assertFalse($this->Time->isThisWeek("-7 days")); break; } } /** * testOfIsThisMonth method * * @access public * @return void */ function testOfIsThisMonth() { $result = $this->Time->isThisMonth('+0 day'); $this->assertTrue($result); $result = $this->Time->isThisMonth($time = mktime(0, 0, 0, date('m'), rand(1, 28), date('Y'))); $this->assertTrue($result); $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), rand(1, 28), date('Y')-rand(1, 12))); $this->assertFalse($result); $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), rand(1, 28), date('Y')+rand(1, 12))); $this->assertFalse($result); } /** * testOfIsThisYear method * * @access public * @return void */ function testOfIsThisYear() { $result = $this->Time->isThisYear('+0 day'); $this->assertTrue($result); $result = $this->Time->isThisYear(mktime(0, 0, 0, rand(1, 12), rand(1, 28), date('Y'))); $this->assertTrue($result); } function testOfWasYesterday() { $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); } function testOfIsTomorrow() { $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); } function testOfWasWithinLast() { $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')); } 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->assertEqual($result, $expected); } function tearDown() { unset($this->Time); }}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -