PageRenderTime 99ms CodeModel.GetById 57ms app.highlight 36ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Test/Case/Utility/CakeTimeTest.php

https://bitbucket.org/songlipeng2003/octopart
PHP | 812 lines | 538 code | 116 blank | 158 comment | 26 complexity | d0fdef2eec4708a883bd29fa1c3d2431 MD5 | raw file
  1<?php
  2/**
  3 * CakeTimeTest file
  4 *
  5 * PHP 5
  6 *
  7 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9 *
 10 * Licensed under The MIT License
 11 * Redistributions of files must retain the above copyright notice
 12 *
 13 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 14 * @link          http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
 15 * @package       Cake.Test.Case.View.Helper
 16 * @since         CakePHP(tm) v 1.2.0.4206
 17 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 18 */
 19App::uses('CakeTime', 'Utility');
 20
 21/**
 22 * CakeTimeTest class
 23 *
 24 * @package       Cake.Test.Case.View.Helper
 25 */
 26class CakeTimeTest extends CakeTestCase {
 27
 28/**
 29 * setUp method
 30 *
 31 * @return void
 32 */
 33	public function setUp() {
 34		$this->Time = new CakeTime();
 35	}
 36
 37/**
 38 * tearDown method
 39 *
 40 * @return void
 41 */
 42	public function tearDown() {
 43		unset($this->Time);
 44	}
 45
 46/**
 47 * testToQuarter method
 48 *
 49 * @return void
 50 */
 51	public function testToQuarter() {
 52		$result = $this->Time->toQuarter('2007-12-25');
 53		$this->assertEquals($result, 4);
 54
 55		$result = $this->Time->toQuarter('2007-9-25');
 56		$this->assertEquals($result, 3);
 57
 58		$result = $this->Time->toQuarter('2007-3-25');
 59		$this->assertEquals($result, 1);
 60
 61		$result = $this->Time->toQuarter('2007-3-25', true);
 62		$this->assertEquals($result, array('2007-01-01', '2007-03-31'));
 63
 64		$result = $this->Time->toQuarter('2007-5-25', true);
 65		$this->assertEquals($result, array('2007-04-01', '2007-06-30'));
 66
 67		$result = $this->Time->toQuarter('2007-8-25', true);
 68		$this->assertEquals($result, array('2007-07-01', '2007-09-30'));
 69
 70		$result = $this->Time->toQuarter('2007-12-25', true);
 71		$this->assertEquals($result, array('2007-10-01', '2007-12-31'));
 72	}
 73
 74/**
 75 * testTimeAgoInWords method
 76 *
 77 * @return void
 78 */
 79	public function testTimeAgoInWords() {
 80		$result = $this->Time->timeAgoInWords('-1 week');
 81		$this->assertEquals($result, '1 week ago');
 82
 83		$result = $this->Time->timeAgoInWords('+1 week');
 84		$this->assertEquals($result, '1 week');
 85
 86		$result = $this->Time->timeAgoInWords(strtotime('+4 months +2 weeks +3 days'), array('end' => '8 years'), true);
 87		$this->assertEquals($result, '4 months, 2 weeks, 3 days');
 88
 89		$result = $this->Time->timeAgoInWords(strtotime('+4 months +2 weeks +2 days'), array('end' => '8 years'), true);
 90		$this->assertEquals($result, '4 months, 2 weeks, 2 days');
 91
 92		$result = $this->Time->timeAgoInWords(strtotime('+4 months +2 weeks +1 day'), array('end' => '8 years'), true);
 93		$this->assertEquals($result, '4 months, 2 weeks, 1 day');
 94
 95		$result = $this->Time->timeAgoInWords(strtotime('+3 months +2 weeks +1 day'), array('end' => '8 years'), true);
 96		$this->assertEquals($result, '3 months, 2 weeks, 1 day');
 97
 98		$result = $this->Time->timeAgoInWords(strtotime('+3 months +2 weeks'), array('end' => '8 years'), true);
 99		$this->assertEquals($result, '3 months, 2 weeks');
100
101		$result = $this->Time->timeAgoInWords(strtotime('+3 months +1 week +6 days'), array('end' => '8 years'), true);
102		$this->assertEquals($result, '3 months, 1 week, 6 days');
103
104		$result = $this->Time->timeAgoInWords(strtotime('+2 months +2 weeks +1 day'), array('end' => '8 years'), true);
105		$this->assertEquals($result, '2 months, 2 weeks, 1 day');
106
107		$result = $this->Time->timeAgoInWords(strtotime('+2 months +2 weeks'), array('end' => '8 years'), true);
108		$this->assertEquals($result, '2 months, 2 weeks');
109
110		$result = $this->Time->timeAgoInWords(strtotime('+2 months +1 week +6 days'), array('end' => '8 years'), true);
111		$this->assertEquals($result, '2 months, 1 week, 6 days');
112
113		$result = $this->Time->timeAgoInWords(strtotime('+1 month +1 week +6 days'), array('end' => '8 years'), true);
114		$this->assertEquals($result, '1 month, 1 week, 6 days');
115
116		for ($i = 0; $i < 200; $i ++) {
117			$years = mt_rand(0, 3);
118			$months = mt_rand(0, 11);
119			$weeks = mt_rand(0, 3);
120			$days = mt_rand(0, 6);
121			$hours = 0;
122			$minutes = 0;
123			$seconds = 0;
124			$relative_date = '';
125
126			// Trying to take into account the number of days in a month
127			$month = date('m') - $months;
128			if ($month <= 0) {
129				$month = $months % 12;
130			}
131			$time = mktime(0, 0, 0, $month, 1, date('y') - $years);
132			$diffDays = date('t') - date('t', $time);
133
134			if ($diffDays > 0 && date('j') - date('t', $time) - $days > 0 && $months > 0 && $weeks === 0) {
135				continue;
136			}
137
138			if ($years > 0) {
139				// years and months and days
140				$relative_date .= ($relative_date ? ', -' : '-') . $years . ' year' . ($years > 1 ? 's' : '');
141				$relative_date .= $months > 0 ? ($relative_date ? ', -' : '-') . $months . ' month' . ($months > 1 ? 's' : '') : '';
142				$relative_date .= $weeks > 0 ? ($relative_date ? ', -' : '-') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : '';
143				$relative_date .= $days > 0 ? ($relative_date ? ', -' : '-') . $days . ' day' . ($days > 1 ? 's' : '') : '';
144			} elseif (abs($months) > 0) {
145				// months, weeks and days
146				$relative_date .= ($relative_date ? ', -' : '-') . $months . ' month' . ($months > 1 ? 's' : '');
147				$relative_date .= $weeks > 0 ? ($relative_date ? ', -' : '-') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : '';
148				$relative_date .= $days > 0 ? ($relative_date ? ', -' : '-') . $days . ' day' . ($days > 1 ? 's' : '') : '';
149			} elseif (abs($weeks) > 0) {
150				// weeks and days
151				$relative_date .= ($relative_date ? ', -' : '-') . $weeks . ' week' . ($weeks > 1 ? 's' : '');
152				$relative_date .= $days > 0 ? ($relative_date ? ', -' : '-') . $days . ' day' . ($days > 1 ? 's' : '') : '';
153			} elseif (abs($days) > 0) {
154				// days and hours
155				$relative_date .= ($relative_date ? ', -' : '-') . $days . ' day' . ($days > 1 ? 's' : '');
156				$relative_date .= $hours > 0 ? ($relative_date ? ', -' : '-') . $hours . ' hour' . ($hours > 1 ? 's' : '') : '';
157			} elseif (abs($hours) > 0) {
158				// hours and minutes
159				$relative_date .= ($relative_date ? ', -' : '-') . $hours . ' hour' . ($hours > 1 ? 's' : '');
160				$relative_date .= $minutes > 0 ? ($relative_date ? ', -' : '-') . $minutes . ' minute' . ($minutes > 1 ? 's' : '') : '';
161			} elseif (abs($minutes) > 0) {
162				// minutes only
163				$relative_date .= ($relative_date ? ', -' : '-') . $minutes . ' minute' . ($minutes > 1 ? 's' : '');
164			} else {
165				// seconds only
166				$relative_date .= ($relative_date ? ', -' : '-') . $seconds . ' second' . ($seconds != 1 ? 's' : '');
167			}
168
169			if (date('j/n/y', strtotime(str_replace(',', '', $relative_date))) != '1/1/70') {
170				$result = $this->Time->timeAgoInWords(strtotime(str_replace(',', '', $relative_date)), array('end' => '8 years'), true);
171				if ($relative_date == '0 seconds') {
172					$relative_date = '0 seconds ago';
173				}
174
175				$relative_date = str_replace('-', '', $relative_date) . ' ago';
176				$this->assertEquals($result, $relative_date);
177
178			}
179		}
180
181		for ($i = 0; $i < 200; $i ++) {
182			$years = mt_rand(0, 3);
183			$months = mt_rand(0, 11);
184			$weeks = mt_rand(0, 3);
185			$days = mt_rand(0, 6);
186			$hours = 0;
187			$minutes = 0;
188			$seconds = 0;
189
190			$relative_date = '';
191
192			if ($years > 0) {
193				// years and months and days
194				$relative_date .= ($relative_date ? ', ' : '') . $years . ' year' . ($years > 1 ? 's' : '');
195				$relative_date .= $months > 0 ? ($relative_date ? ', ' : '') . $months . ' month' . ($months > 1 ? 's' : '') : '';
196				$relative_date .= $weeks > 0 ? ($relative_date ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : '';
197				$relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : '';
198			} elseif (abs($months) > 0) {
199				// months, weeks and days
200				$relative_date .= ($relative_date ? ', ' : '') . $months . ' month' . ($months > 1 ? 's' : '');
201				$relative_date .= $weeks > 0 ? ($relative_date ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : '';
202				$relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : '';
203			} elseif (abs($weeks) > 0) {
204				// weeks and days
205				$relative_date .= ($relative_date ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : '');
206				$relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : '';
207			} elseif (abs($days) > 0) {
208				// days and hours
209				$relative_date .= ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '');
210				$relative_date .= $hours > 0 ? ($relative_date ? ', ' : '') . $hours . ' hour' . ($hours > 1 ? 's' : '') : '';
211			} elseif (abs($hours) > 0) {
212				// hours and minutes
213				$relative_date .= ($relative_date ? ', ' : '') . $hours . ' hour' . ($hours > 1 ? 's' : '');
214				$relative_date .= $minutes > 0 ? ($relative_date ? ', ' : '') . $minutes . ' minute' . ($minutes > 1 ? 's' : '') : '';
215			} elseif (abs($minutes) > 0) {
216				// minutes only
217				$relative_date .= ($relative_date ? ', ' : '') . $minutes . ' minute' . ($minutes > 1 ? 's' : '');
218			} else {
219				// seconds only
220				$relative_date .= ($relative_date ? ', ' : '') . $seconds . ' second' . ($seconds != 1 ? 's' : '');
221			}
222
223			if (date('j/n/y', strtotime(str_replace(',', '', $relative_date))) != '1/1/70') {
224				$result = $this->Time->timeAgoInWords(strtotime(str_replace(',', '', $relative_date)), array('end' => '8 years'), true);
225				if ($relative_date == '0 seconds') {
226					$relative_date = '0 seconds ago';
227				}
228
229				$relative_date = str_replace('-', '', $relative_date) . '';
230				$this->assertEquals($result, $relative_date);
231			}
232		}
233
234		$result = $this->Time->timeAgoInWords(strtotime('-2 years -5 months -2 days'), array('end' => '3 years'), true);
235		$this->assertEquals($result, '2 years, 5 months, 2 days ago');
236
237		$result = $this->Time->timeAgoInWords('2007-9-25');
238		$this->assertEquals($result, 'on 25/9/07');
239
240		$result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d');
241		$this->assertEquals($result, 'on 2007-09-25');
242
243		$result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d', true);
244		$this->assertEquals($result, 'on 2007-09-25');
245
246		$result = $this->Time->timeAgoInWords(strtotime('-2 weeks -2 days'), 'Y-m-d', false);
247		$this->assertEquals($result, '2 weeks, 2 days ago');
248
249		$result = $this->Time->timeAgoInWords(strtotime('+2 weeks +2 days'), 'Y-m-d', true);
250		$this->assertRegExp('/^2 weeks, [1|2] day(s)?$/', $result);
251
252		$result = $this->Time->timeAgoInWords(strtotime('+2 months +2 days'), array('end' => '1 month'));
253		$this->assertEquals($result, 'on ' . date('j/n/y', strtotime('+2 months +2 days')));
254
255		$result = $this->Time->timeAgoInWords(strtotime('+2 months +2 days'), array('end' => '3 month'));
256		$this->assertRegExp('/2 months/', $result);
257
258		$result = $this->Time->timeAgoInWords(strtotime('+2 months +12 days'), array('end' => '3 month'));
259		$this->assertRegExp('/2 months, 1 week/', $result);
260
261		$result = $this->Time->timeAgoInWords(strtotime('+3 months +5 days'), array('end' => '4 month'));
262		$this->assertEquals($result, '3 months, 5 days');
263
264		$result = $this->Time->timeAgoInWords(strtotime('-2 months -2 days'), array('end' => '3 month'));
265		$this->assertEquals($result, '2 months, 2 days ago');
266
267		$result = $this->Time->timeAgoInWords(strtotime('-2 months -2 days'), array('end' => '3 month'));
268		$this->assertEquals($result, '2 months, 2 days ago');
269
270		$result = $this->Time->timeAgoInWords(strtotime('+2 months +2 days'), array('end' => '3 month'));
271		$this->assertRegExp('/2 months/', $result);
272
273		$result = $this->Time->timeAgoInWords(strtotime('+2 months +2 days'), array('end' => '1 month', 'format' => 'Y-m-d'));
274		$this->assertEquals($result, 'on ' . date('Y-m-d', strtotime('+2 months +2 days')));
275
276		$result = $this->Time->timeAgoInWords(strtotime('-2 months -2 days'), array('end' => '1 month', 'format' => 'Y-m-d'));
277		$this->assertEquals($result, 'on ' . date('Y-m-d', strtotime('-2 months -2 days')));
278
279		$result = $this->Time->timeAgoInWords(strtotime('-13 months -5 days'), array('end' => '2 years'));
280		$this->assertEquals($result, '1 year, 1 month, 5 days ago');
281
282		$fourHours = $this->Time->timeAgoInWords(strtotime('-5 days -2 hours'), array('userOffset' => -4));
283		$result = $this->Time->timeAgoInWords(strtotime('-5 days -2 hours'), array('userOffset' => 4));
284		$this->assertEquals($fourHours, $result);
285
286		$result = $this->Time->timeAgoInWords(strtotime('-2 hours'));
287		$expected = '2 hours ago';
288		$this->assertEquals($expected, $result);
289
290		$result = $this->Time->timeAgoInWords(strtotime('-12 minutes'));
291		$expected = '12 minutes ago';
292		$this->assertEquals($expected, $result);
293
294		$result = $this->Time->timeAgoInWords(strtotime('-12 seconds'));
295		$expected = '12 seconds ago';
296		$this->assertEquals($expected, $result);
297
298		$time = strtotime('-3 years -12 months');
299		$result = $this->Time->timeAgoInWords($time);
300		$expected = 'on ' . date('j/n/y', $time);
301		$this->assertEquals($expected, $result);
302	}
303
304/**
305 * testNice method
306 *
307 * @return void
308 */
309	public function testNice() {
310		$time = time() + 2 * DAY;
311		$this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
312
313		$time = time() - 2 * DAY;
314		$this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
315
316		$time = time();
317		$this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
318
319		$time = 0;
320		$this->assertEquals(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
321
322		$time = null;
323		$this->assertEquals(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
324
325		$time = time();
326		$this->assertEquals(date('D', $time), $this->Time->nice($time, null, '%a'));
327		$this->assertEquals(date('M d, Y', $time), $this->Time->nice($time, null, '%b %d, %Y'));
328
329		$this->Time->niceFormat = '%Y-%d-%m';
330		$this->assertEquals(date('Y-d-m', $time), $this->Time->nice($time));
331		$this->assertEquals('%Y-%d-%m', $this->Time->niceFormat);
332
333		CakeTime::$niceFormat = '%Y-%d-%m %H:%M:%S';
334		$this->assertEquals(date('Y-d-m H:i:s', $time), $this->Time->nice($time));
335		$this->assertEquals('%Y-%d-%m %H:%M:%S', $this->Time->niceFormat);
336	}
337
338/**
339 * testNiceShort method
340 *
341 * @return void
342 */
343	public function testNiceShort() {
344		$time = time() + 2 * DAY;
345		if (date('Y', $time) == date('Y')) {
346			$this->assertEquals(date('M jS, H:i', $time), $this->Time->niceShort($time));
347		} else {
348			$this->assertEquals(date('M jS Y, H:i', $time), $this->Time->niceShort($time));
349		}
350
351		$time = time();
352		$this->assertEquals('Today, ' . date('H:i', $time), $this->Time->niceShort($time));
353
354		$time = time() - DAY;
355		$this->assertEquals('Yesterday, ' . date('H:i', $time), $this->Time->niceShort($time));
356	}
357
358/**
359 * testDaysAsSql method
360 *
361 * @return void
362 */
363	public function testDaysAsSql() {
364		$begin = time();
365		$end = time() + DAY;
366		$field = 'my_field';
367		$expected = '(my_field >= \''.date('Y-m-d', $begin).' 00:00:00\') AND (my_field <= \''.date('Y-m-d', $end).' 23:59:59\')';
368		$this->assertEquals($expected, $this->Time->daysAsSql($begin, $end, $field));
369	}
370
371/**
372 * testDayAsSql method
373 *
374 * @return void
375 */
376	public function testDayAsSql() {
377		$time = time();
378		$field = 'my_field';
379		$expected = '(my_field >= \''.date('Y-m-d', $time).' 00:00:00\') AND (my_field <= \''.date('Y-m-d', $time).' 23:59:59\')';
380		$this->assertEquals($expected, $this->Time->dayAsSql($time, $field));
381	}
382
383/**
384 * testToUnix method
385 *
386 * @return void
387 */
388	public function testToUnix() {
389		$this->assertEquals(time(), $this->Time->toUnix(time()));
390		$this->assertEquals(strtotime('+1 day'), $this->Time->toUnix('+1 day'));
391		$this->assertEquals(strtotime('+0 days'), $this->Time->toUnix('+0 days'));
392		$this->assertEquals(strtotime('-1 days'), $this->Time->toUnix('-1 days'));
393		$this->assertEquals(false, $this->Time->toUnix(''));
394		$this->assertEquals(false, $this->Time->toUnix(null));
395	}
396
397/**
398 * testToAtom method
399 *
400 * @return void
401 */
402	public function testToAtom() {
403		$this->assertEquals(date('Y-m-d\TH:i:s\Z'), $this->Time->toAtom(time()));
404	}
405
406/**
407 * testToRss method
408 *
409 * @return void
410 */
411	public function testToRss() {
412		$this->assertEquals(date('r'), $this->Time->toRss(time()));
413
414		if (!$this->skipIf(!class_exists('DateTimeZone'), '%s DateTimeZone class not available.')) {
415			$timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu');
416			foreach ($timezones as $timezone) {
417				$yourTimezone = new DateTimeZone($timezone);
418				$yourTime = new DateTime('now', $yourTimezone);
419				$userOffset = $yourTimezone->getOffset($yourTime) / HOUR;
420				$this->assertEquals($yourTime->format('r'), $this->Time->toRss(time(), $userOffset));
421			}
422		}
423	}
424
425/**
426 * testFormat method
427 *
428 * @return void
429 */
430	public function testFormat() {
431		$format = 'D-M-Y';
432		$arr = array(time(), strtotime('+1 days'), strtotime('+1 days'), strtotime('+0 days'));
433		foreach ($arr as $val) {
434			$this->assertEquals(date($format, $val), $this->Time->format($format, $val));
435		}
436
437		$result = $this->Time->format('Y-m-d', null, 'never');
438		$this->assertEquals($result, 'never');
439	}
440
441/**
442 * testOfGmt method
443 *
444 * @return void
445 */
446	public function testGmt() {
447		$hour = 3;
448		$min = 4;
449		$sec = 2;
450		$month = 5;
451		$day = 14;
452		$year = 2007;
453		$time = mktime($hour, $min, $sec, $month, $day, $year);
454		$expected = gmmktime($hour, $min, $sec, $month, $day, $year);
455		$this->assertEquals($expected, $this->Time->gmt(date('Y-n-j G:i:s', $time)));
456
457		$hour = date('H');
458		$min = date('i');
459		$sec = date('s');
460		$month = date('m');
461		$day = date('d');
462		$year = date('Y');
463		$expected = gmmktime($hour, $min, $sec, $month, $day, $year);
464		$this->assertEquals($expected, $this->Time->gmt(null));
465	}
466
467/**
468 * testIsToday method
469 *
470 * @return void
471 */
472	public function testIsToday() {
473		$result = $this->Time->isToday('+1 day');
474		$this->assertFalse($result);
475		$result = $this->Time->isToday('+1 days');
476		$this->assertFalse($result);
477		$result = $this->Time->isToday('+0 day');
478		$this->assertTrue($result);
479		$result = $this->Time->isToday('-1 day');
480		$this->assertFalse($result);
481	}
482
483/**
484 * testIsThisWeek method
485 *
486 * @return void
487 */
488	public function testIsThisWeek() {
489		// A map of days which goes from -1 day of week to +1 day of week
490		$map = array(
491			'Mon' => array(-1, 7), 'Tue' => array(-2, 6), 'Wed' => array(-3, 5),
492			'Thu' => array(-4, 4), 'Fri' => array(-5, 3), 'Sat' => array(-6, 2),
493			'Sun' => array(-7, 1)
494		);
495		$days = $map[date('D')];
496
497		for ($day = $days[0] + 1; $day < $days[1]; $day++) {
498			$this->assertTrue($this->Time->isThisWeek(($day > 0 ? '+' : '') . $day . ' days'));
499		}
500		$this->assertFalse($this->Time->isThisWeek($days[0] . ' days'));
501		$this->assertFalse($this->Time->isThisWeek('+' . $days[1] . ' days'));
502	}
503
504/**
505 * testIsThisMonth method
506 *
507 * @return void
508 */
509	public function testIsThisMonth() {
510		$result = $this->Time->isThisMonth('+0 day');
511		$this->assertTrue($result);
512		$result = $this->Time->isThisMonth($time = mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y')));
513		$this->assertTrue($result);
514		$result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') - mt_rand(1, 12)));
515		$this->assertFalse($result);
516		$result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') + mt_rand(1, 12)));
517		$this->assertFalse($result);
518
519	}
520
521/**
522 * testIsThisYear method
523 *
524 * @return void
525 */
526	public function testIsThisYear() {
527		$result = $this->Time->isThisYear('+0 day');
528		$this->assertTrue($result);
529		$result = $this->Time->isThisYear(mktime(0, 0, 0, mt_rand(1, 12), mt_rand(1, 28), date('Y')));
530		$this->assertTrue($result);
531	}
532
533/**
534 * testWasYesterday method
535 *
536 * @return void
537 */
538	public function testWasYesterday() {
539		$result = $this->Time->wasYesterday('+1 day');
540		$this->assertFalse($result);
541		$result = $this->Time->wasYesterday('+1 days');
542		$this->assertFalse($result);
543		$result = $this->Time->wasYesterday('+0 day');
544		$this->assertFalse($result);
545		$result = $this->Time->wasYesterday('-1 day');
546		$this->assertTrue($result);
547		$result = $this->Time->wasYesterday('-1 days');
548		$this->assertTrue($result);
549		$result = $this->Time->wasYesterday('-2 days');
550		$this->assertFalse($result);
551	}
552
553/**
554 * testIsTomorrow method
555 *
556 * @return void
557 */
558	public function testIsTomorrow() {
559		$result = $this->Time->isTomorrow('+1 day');
560		$this->assertTrue($result);
561		$result = $this->Time->isTomorrow('+1 days');
562		$this->assertTrue($result);
563		$result = $this->Time->isTomorrow('+0 day');
564		$this->assertFalse($result);
565		$result = $this->Time->isTomorrow('-1 day');
566		$this->assertFalse($result);
567	}
568
569/**
570 * testWasWithinLast method
571 *
572 * @return void
573 */
574	public function testWasWithinLast() {
575		$this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
576		$this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week'));
577		$this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
578		$this->assertTrue($this->Time->wasWithinLast('1 second', '-1 second'));
579		$this->assertTrue($this->Time->wasWithinLast('1 minute', '-1 minute'));
580		$this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
581		$this->assertTrue($this->Time->wasWithinLast('1 month', '-1 month'));
582		$this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
583
584		$this->assertTrue($this->Time->wasWithinLast('1 week', '-1 day'));
585		$this->assertTrue($this->Time->wasWithinLast('2 week', '-1 week'));
586		$this->assertFalse($this->Time->wasWithinLast('1 second', '-1 year'));
587		$this->assertTrue($this->Time->wasWithinLast('10 minutes', '-1 second'));
588		$this->assertTrue($this->Time->wasWithinLast('23 minutes', '-1 minute'));
589		$this->assertFalse($this->Time->wasWithinLast('0 year', '-1 year'));
590		$this->assertTrue($this->Time->wasWithinLast('13 month', '-1 month'));
591		$this->assertTrue($this->Time->wasWithinLast('2 days', '-1 day'));
592
593		$this->assertFalse($this->Time->wasWithinLast('1 week', '-2 weeks'));
594		$this->assertFalse($this->Time->wasWithinLast('1 second', '-2 seconds'));
595		$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
596		$this->assertFalse($this->Time->wasWithinLast('1 hour', '-2 hours'));
597		$this->assertFalse($this->Time->wasWithinLast('1 month', '-2 months'));
598		$this->assertFalse($this->Time->wasWithinLast('1 year', '-2 years'));
599
600		$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 weeks'));
601		$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
602		$this->assertFalse($this->Time->wasWithinLast('0 days', '-2 days'));
603		$this->assertTrue($this->Time->wasWithinLast('1 hour', '-20 seconds'));
604		$this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds'));
605		$this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months'));
606		$this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months'));
607
608		$this->assertTrue($this->Time->wasWithinLast('5 ', '-3 days'));
609		$this->assertTrue($this->Time->wasWithinLast('1   ', '-1 hour'));
610		$this->assertTrue($this->Time->wasWithinLast('1   ', '-1 minute'));
611		$this->assertTrue($this->Time->wasWithinLast('1   ', '-23 hours -59 minutes -59 seconds'));
612	}
613
614/**
615 * testUserOffset method
616 *
617 * @return void
618 */
619	public function testUserOffset() {
620		$timezoneServer = new DateTimeZone(date_default_timezone_get());
621		$timeServer = new DateTime('now', $timezoneServer);
622		$yourTimezone = $timezoneServer->getOffset($timeServer) / HOUR;
623
624		$expected = time();
625		$result = $this->Time->fromString(time(), $yourTimezone);
626		$this->assertEquals($expected, $result);
627	}
628
629/**
630 * test fromString()
631 *
632 * @return void
633 */
634	public function testFromString() {
635		$result = $this->Time->fromString('');
636		$this->assertFalse($result);
637
638		$result = $this->Time->fromString(0, 0);
639		$this->assertFalse($result);
640
641		$result = $this->Time->fromString('+1 hour');
642		$expected = strtotime('+1 hour');
643		$this->assertEquals($expected, $result);
644
645		$timezone = date('Z', time());
646		$result = $this->Time->fromString('+1 hour', $timezone);
647		$expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
648		$this->assertEquals($expected, $result);
649	}
650
651/**
652 * test converting time specifiers using a time definition localfe file
653 *
654 * @return void
655 */
656	public function testConvertSpecifiers() {
657		App::build(array(
658			'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS)
659		), App::RESET);
660		Configure::write('Config.language', 'time_test');
661		$time = strtotime('Thu Jan 14 11:43:39 2010');
662
663		$result = $this->Time->convertSpecifiers('%a', $time);
664		$expected = 'jue';
665		$this->assertEquals($expected, $result);
666
667		$result = $this->Time->convertSpecifiers('%A', $time);
668		$expected = 'jueves';
669		$this->assertEquals($expected, $result);
670
671		$result = $this->Time->convertSpecifiers('%c', $time);
672		$expected = 'jue %d ene %Y %H:%M:%S %Z';
673		$this->assertEquals($expected, $result);
674
675		$result = $this->Time->convertSpecifiers('%C', $time);
676		$expected = '20';
677		$this->assertEquals($expected, $result);
678
679		$result = $this->Time->convertSpecifiers('%D', $time);
680		$expected = '%m/%d/%y';
681		$this->assertEquals($expected, $result);
682
683		$result = $this->Time->convertSpecifiers('%b', $time);
684		$expected = 'ene';
685		$this->assertEquals($expected, $result);
686
687		$result = $this->Time->convertSpecifiers('%h', $time);
688		$expected = 'ene';
689		$this->assertEquals($expected, $result);
690
691		$result = $this->Time->convertSpecifiers('%B', $time);
692		$expected = 'enero';
693		$this->assertEquals($expected, $result);
694
695		$result = $this->Time->convertSpecifiers('%n', $time);
696		$expected = "\n";
697		$this->assertEquals($expected, $result);
698
699		$result = $this->Time->convertSpecifiers('%n', $time);
700		$expected = "\n";
701		$this->assertEquals($expected, $result);
702
703		$result = $this->Time->convertSpecifiers('%p', $time);
704		$expected = 'AM';
705		$this->assertEquals($expected, $result);
706
707		$result = $this->Time->convertSpecifiers('%P', $time);
708		$expected = 'am';
709		$this->assertEquals($expected, $result);
710
711		$result = $this->Time->convertSpecifiers('%r', $time);
712		$expected = '%I:%M:%S AM';
713		$this->assertEquals($expected, $result);
714
715		$result = $this->Time->convertSpecifiers('%R', $time);
716		$expected = '11:43';
717		$this->assertEquals($expected, $result);
718
719		$result = $this->Time->convertSpecifiers('%t', $time);
720		$expected = "\t";
721		$this->assertEquals($expected, $result);
722
723		$result = $this->Time->convertSpecifiers('%T', $time);
724		$expected = '%H:%M:%S';
725		$this->assertEquals($expected, $result);
726
727		$result = $this->Time->convertSpecifiers('%u', $time);
728		$expected = 4;
729		$this->assertEquals($expected, $result);
730
731		$result = $this->Time->convertSpecifiers('%x', $time);
732		$expected = '%d/%m/%y';
733		$this->assertEquals($expected, $result);
734
735		$result = $this->Time->convertSpecifiers('%X', $time);
736		$expected = '%H:%M:%S';
737		$this->assertEquals($expected, $result);
738	}
739
740/**
741 * test convert %e on windows.
742 *
743 * @return void
744 */
745	public function testConvertPercentE() {
746		$this->skipIf(DIRECTORY_SEPARATOR !== '\\', 'Cannot run windows tests on non-windows OS.');
747
748		$time = strtotime('Thu Jan 14 11:43:39 2010');
749		$result = $this->Time->convertSpecifiers('%e', $time);
750		$expected = '14';
751		$this->assertEquals($expected, $result);
752
753		$result = $this->Time->convertSpecifiers('%e', strtotime('2011-01-01'));
754		$expected = ' 1';
755		$this->assertEquals($expected, $result);
756	}
757
758/**
759 * test formatting dates taking in account preferred i18n locale file
760 *
761 * @return void
762 */
763	public function testI18nFormat() {
764		App::build(array(
765			'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS)
766		), App::RESET);
767		Configure::write('Config.language', 'time_test');
768
769		$time = strtotime('Thu Jan 14 13:59:28 2010');
770
771		$result = $this->Time->i18nFormat($time);
772		$expected = '14/01/10';
773		$this->assertEquals($expected, $result);
774
775		$result = $this->Time->i18nFormat($time, '%c');
776		$expected = 'jue 14 ene 2010 13:59:28 ' . strftime('%Z', $time);
777		$this->assertEquals($expected, $result);
778
779		$result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
780		$expected = 'Time is 01:59:28 PM, and date is 14/01/10';
781		$this->assertEquals($expected, $result);
782
783		$time = strtotime('Wed Jan 13 13:59:28 2010');
784
785		$result = $this->Time->i18nFormat($time);
786		$expected = '13/01/10';
787		$this->assertEquals($expected, $result);
788
789		$result = $this->Time->i18nFormat($time, '%c');
790		$expected = 'miƩ 13 ene 2010 13:59:28 ' . strftime('%Z', $time);
791		$this->assertEquals($expected, $result);
792
793		$result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
794		$expected = 'Time is 01:59:28 PM, and date is 13/01/10';
795		$this->assertEquals($expected, $result);
796
797		$result = $this->Time->i18nFormat('invalid date', '%x', 'Date invalid');
798		$expected = 'Date invalid';
799		$this->assertEquals($expected, $result);
800	}
801
802/**
803 * test new format() syntax which inverts first and second parameters
804 *
805 * @return void
806 */
807	public function testFormatNewSyntax() {
808		$time = time();
809		$this->assertEquals($this->Time->format($time), $this->Time->i18nFormat($time));
810		$this->assertEquals($this->Time->format($time, '%c'), $this->Time->i18nFormat($time, '%c'));
811	}
812}