| 12345 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* CakeTimeTest file
|
|
|
4 |
*
|
|
|
5 |
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
|
|
6 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
7 |
*
|
|
|
8 |
* Licensed under The MIT License
|
|
|
9 |
* For full copyright and license information, please see the LICENSE.txt
|
|
|
10 |
* Redistributions of files must retain the above copyright notice
|
|
|
11 |
*
|
|
|
12 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
13 |
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
|
|
14 |
* @package Cake.Test.Case.View.Helper
|
|
|
15 |
* @since CakePHP(tm) v 1.2.0.4206
|
|
|
16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
App::uses('CakeTime', 'Utility');
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* CakeTimeTest class
|
|
|
23 |
*
|
|
|
24 |
* @package Cake.Test.Case.View.Helper
|
|
|
25 |
*/
|
|
|
26 |
class CakeTimeTest extends CakeTestCase {
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Default system timezone identifier
|
|
|
30 |
*
|
|
|
31 |
* @var string
|
|
|
32 |
*/
|
|
|
33 |
protected $_systemTimezoneIdentifier = null;
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* setUp method
|
|
|
37 |
*
|
|
|
38 |
* @return void
|
|
|
39 |
*/
|
|
|
40 |
public function setUp() {
|
|
|
41 |
parent::setUp();
|
|
|
42 |
$this->Time = new CakeTime();
|
|
|
43 |
$this->_systemTimezoneIdentifier = date_default_timezone_get();
|
|
|
44 |
Configure::write('Config.language', 'eng');
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* tearDown method
|
|
|
49 |
*
|
|
|
50 |
* @return void
|
|
|
51 |
*/
|
|
|
52 |
public function tearDown() {
|
|
|
53 |
parent::tearDown();
|
|
|
54 |
unset($this->Time);
|
|
|
55 |
$this->_restoreSystemTimezone();
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Restored the original system timezone
|
|
|
60 |
*
|
|
|
61 |
* @param string $timezoneIdentifier Timezone string
|
|
|
62 |
* @return void
|
|
|
63 |
*/
|
|
|
64 |
protected function _restoreSystemTimezone() {
|
|
|
65 |
date_default_timezone_set($this->_systemTimezoneIdentifier);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* testToQuarter method
|
|
|
70 |
*
|
|
|
71 |
* @return void
|
|
|
72 |
*/
|
|
|
73 |
public function testToQuarter() {
|
|
|
74 |
$result = $this->Time->toQuarter('2007-12-25');
|
|
|
75 |
$this->assertEquals(4, $result);
|
|
|
76 |
|
|
|
77 |
$result = $this->Time->toQuarter('2007-9-25');
|
|
|
78 |
$this->assertEquals(3, $result);
|
|
|
79 |
|
|
|
80 |
$result = $this->Time->toQuarter('2007-3-25');
|
|
|
81 |
$this->assertEquals(1, $result);
|
|
|
82 |
|
|
|
83 |
$result = $this->Time->toQuarter('2007-3-25', true);
|
|
|
84 |
$this->assertEquals(array('2007-01-01', '2007-03-31'), $result);
|
|
|
85 |
|
|
|
86 |
$result = $this->Time->toQuarter('2007-5-25', true);
|
|
|
87 |
$this->assertEquals(array('2007-04-01', '2007-06-30'), $result);
|
|
|
88 |
|
|
|
89 |
$result = $this->Time->toQuarter('2007-8-25', true);
|
|
|
90 |
$this->assertEquals(array('2007-07-01', '2007-09-30'), $result);
|
|
|
91 |
|
|
|
92 |
$result = $this->Time->toQuarter('2007-12-25', true);
|
|
|
93 |
$this->assertEquals(array('2007-10-01', '2007-12-31'), $result);
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* provider for timeAgoInWords() tests
|
|
|
98 |
*
|
|
|
99 |
* @return array
|
|
|
100 |
*/
|
|
|
101 |
public static function timeAgoProvider() {
|
|
|
102 |
return array(
|
|
|
103 |
array('-12 seconds', '12 seconds ago'),
|
|
|
104 |
array('-12 minutes', '12 minutes ago'),
|
|
|
105 |
array('-2 hours', '2 hours ago'),
|
|
|
106 |
array('-1 day', '1 day ago'),
|
|
|
107 |
array('-2 days', '2 days ago'),
|
|
|
108 |
array('-2 days -3 hours', '2 days, 3 hours ago'),
|
|
|
109 |
array('-1 week', '1 week ago'),
|
|
|
110 |
array('-2 weeks -2 days', '2 weeks, 2 days ago'),
|
|
|
111 |
array('+1 week', '1 week'),
|
|
|
112 |
array('+1 week 1 day', '1 week, 1 day'),
|
|
|
113 |
array('+2 weeks 2 day', '2 weeks, 2 days'),
|
|
|
114 |
array('2007-9-24', 'on 24/9/07'),
|
|
|
115 |
array('now', 'just now'),
|
|
|
116 |
);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* testTimeAgoInWords method
|
|
|
121 |
*
|
|
|
122 |
* @dataProvider timeAgoProvider
|
|
|
123 |
* @return void
|
|
|
124 |
*/
|
|
|
125 |
public function testTimeAgoInWords($input, $expected) {
|
|
|
126 |
$result = $this->Time->timeAgoInWords($input);
|
|
|
127 |
$this->assertEquals($expected, $result);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* provider for timeAgo with an end date.
|
|
|
132 |
*
|
|
|
133 |
* @return void
|
|
|
134 |
*/
|
|
|
135 |
public function timeAgoEndProvider() {
|
|
|
136 |
return array(
|
|
|
137 |
array(
|
|
|
138 |
'+4 months +2 weeks +3 days',
|
|
|
139 |
'4 months, 2 weeks, 3 days',
|
|
|
140 |
'8 years'
|
|
|
141 |
),
|
|
|
142 |
array(
|
|
|
143 |
'+4 months +2 weeks +1 day',
|
|
|
144 |
'4 months, 2 weeks, 1 day',
|
|
|
145 |
'8 years'
|
|
|
146 |
),
|
|
|
147 |
array(
|
|
|
148 |
'+3 months +2 weeks',
|
|
|
149 |
'3 months, 2 weeks',
|
|
|
150 |
'8 years'
|
|
|
151 |
),
|
|
|
152 |
array(
|
|
|
153 |
'+3 months +2 weeks +1 day',
|
|
|
154 |
'3 months, 2 weeks, 1 day',
|
|
|
155 |
'8 years'
|
|
|
156 |
),
|
|
|
157 |
array(
|
|
|
158 |
'+1 months +1 week +1 day',
|
|
|
159 |
'1 month, 1 week, 1 day',
|
|
|
160 |
'8 years'
|
|
|
161 |
),
|
|
|
162 |
array(
|
|
|
163 |
'+2 months +2 days',
|
|
|
164 |
'2 months, 2 days',
|
|
|
165 |
'on ' . date('j/n/y', strtotime('+2 months +2 days'))
|
|
|
166 |
),
|
|
|
167 |
array(
|
|
|
168 |
'+2 months +12 days',
|
|
|
169 |
'2 months, 1 week, 5 days',
|
|
|
170 |
'3 months'
|
|
|
171 |
),
|
|
|
172 |
);
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
/**
|
|
|
176 |
* test the end option for timeAgoInWords
|
|
|
177 |
*
|
|
|
178 |
* @dataProvider timeAgoEndProvider
|
|
|
179 |
* @return void
|
|
|
180 |
*/
|
|
|
181 |
public function testTimeAgoInWordsEnd($input, $expected, $end) {
|
|
|
182 |
$result = $this->Time->timeAgoInWords(
|
|
|
183 |
$input, array('end' => $end)
|
|
|
184 |
);
|
|
|
185 |
$this->assertEquals($expected, $result);
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
/**
|
|
|
189 |
* test the custom string options for timeAgoInWords
|
|
|
190 |
*
|
|
|
191 |
* @return void
|
|
|
192 |
*/
|
|
|
193 |
public function testTimeAgoInWordsCustomStrings() {
|
|
|
194 |
$result = $this->Time->timeAgoInWords(
|
|
|
195 |
strtotime('-8 years -4 months -2 weeks -3 days'),
|
|
|
196 |
array('relativeString' => 'at least %s ago', 'accuracy' => array('year' => 'year'), 'end' => '+10 years')
|
|
|
197 |
);
|
|
|
198 |
$expected = 'at least 8 years ago';
|
|
|
199 |
$this->assertEquals($expected, $result);
|
|
|
200 |
|
|
|
201 |
$result = $this->Time->timeAgoInWords(
|
|
|
202 |
strtotime('+4 months +2 weeks +3 days'),
|
|
|
203 |
array('absoluteString' => 'exactly on %s', 'accuracy' => array('year' => 'year'), 'end' => '+2 months')
|
|
|
204 |
);
|
|
|
205 |
$expected = 'exactly on ' . date('j/n/y', strtotime('+4 months +2 weeks +3 days'));
|
|
|
206 |
$this->assertEquals($expected, $result);
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
/**
|
|
|
210 |
* Test the accuracy option for timeAgoInWords()
|
|
|
211 |
*
|
|
|
212 |
* @return void
|
|
|
213 |
*/
|
|
|
214 |
public function testTimeAgoInWordsAccuracy() {
|
|
|
215 |
$result = $this->Time->timeAgoInWords(
|
|
|
216 |
strtotime('+8 years +4 months +2 weeks +3 days'),
|
|
|
217 |
array('accuracy' => array('year' => 'year'), 'end' => '+10 years')
|
|
|
218 |
);
|
|
|
219 |
$expected = '8 years';
|
|
|
220 |
$this->assertEquals($expected, $result);
|
|
|
221 |
|
|
|
222 |
$result = $this->Time->timeAgoInWords(
|
|
|
223 |
strtotime('+8 years +4 months +2 weeks +3 days'),
|
|
|
224 |
array('accuracy' => array('year' => 'month'), 'end' => '+10 years')
|
|
|
225 |
);
|
|
|
226 |
$expected = '8 years, 4 months';
|
|
|
227 |
$this->assertEquals($expected, $result);
|
|
|
228 |
|
|
|
229 |
$result = $this->Time->timeAgoInWords(
|
|
|
230 |
strtotime('+8 years +4 months +2 weeks +3 days'),
|
|
|
231 |
array('accuracy' => array('year' => 'week'), 'end' => '+10 years')
|
|
|
232 |
);
|
|
|
233 |
$expected = '8 years, 4 months, 2 weeks';
|
|
|
234 |
$this->assertEquals($expected, $result);
|
|
|
235 |
|
|
|
236 |
$result = $this->Time->timeAgoInWords(
|
|
|
237 |
strtotime('+8 years +4 months +2 weeks +3 days'),
|
|
|
238 |
array('accuracy' => array('year' => 'day'), 'end' => '+10 years')
|
|
|
239 |
);
|
|
|
240 |
$expected = '8 years, 4 months, 2 weeks, 3 days';
|
|
|
241 |
$this->assertEquals($expected, $result);
|
|
|
242 |
|
|
|
243 |
$result = $this->Time->timeAgoInWords(
|
|
|
244 |
strtotime('+1 years +5 weeks'),
|
|
|
245 |
array('accuracy' => array('year' => 'year'), 'end' => '+10 years')
|
|
|
246 |
);
|
|
|
247 |
$expected = '1 year';
|
|
|
248 |
$this->assertEquals($expected, $result);
|
|
|
249 |
|
|
|
250 |
$result = $this->Time->timeAgoInWords(
|
|
|
251 |
strtotime('+58 minutes'),
|
|
|
252 |
array('accuracy' => 'hour')
|
|
|
253 |
);
|
|
|
254 |
$expected = 'in about an hour';
|
|
|
255 |
$this->assertEquals($expected, $result);
|
|
|
256 |
|
|
|
257 |
$result = $this->Time->timeAgoInWords(
|
|
|
258 |
strtotime('+23 hours'),
|
|
|
259 |
array('accuracy' => 'day')
|
|
|
260 |
);
|
|
|
261 |
$expected = 'in about a day';
|
|
|
262 |
$this->assertEquals($expected, $result);
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
/**
|
|
|
266 |
* Test the format option of timeAgoInWords()
|
|
|
267 |
*
|
|
|
268 |
* @return void
|
|
|
269 |
*/
|
|
|
270 |
public function testTimeAgoInWordsWithFormat() {
|
|
|
271 |
$result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d');
|
|
|
272 |
$this->assertEquals('on 2007-09-25', $result);
|
|
|
273 |
|
|
|
274 |
$result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d');
|
|
|
275 |
$this->assertEquals('on 2007-09-25', $result);
|
|
|
276 |
|
|
|
277 |
$result = $this->Time->timeAgoInWords(
|
|
|
278 |
strtotime('+2 weeks +2 days'),
|
|
|
279 |
'Y-m-d'
|
|
|
280 |
);
|
|
|
281 |
$this->assertRegExp('/^2 weeks, [1|2] day(s)?$/', $result);
|
|
|
282 |
|
|
|
283 |
$result = $this->Time->timeAgoInWords(
|
|
|
284 |
strtotime('+2 months +2 days'),
|
|
|
285 |
array('end' => '1 month', 'format' => 'Y-m-d')
|
|
|
286 |
);
|
|
|
287 |
$this->assertEquals('on ' . date('Y-m-d', strtotime('+2 months +2 days')), $result);
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
/**
|
|
|
291 |
* test timeAgoInWords() with negative values.
|
|
|
292 |
*
|
|
|
293 |
* @return void
|
|
|
294 |
*/
|
|
|
295 |
public function testTimeAgoInWordsNegativeValues() {
|
|
|
296 |
$result = $this->Time->timeAgoInWords(
|
|
|
297 |
strtotime('-2 months -2 days'),
|
|
|
298 |
array('end' => '3 month')
|
|
|
299 |
);
|
|
|
300 |
$this->assertEquals('2 months, 2 days ago', $result);
|
|
|
301 |
|
|
|
302 |
$result = $this->Time->timeAgoInWords(
|
|
|
303 |
strtotime('-2 months -2 days'),
|
|
|
304 |
array('end' => '3 month')
|
|
|
305 |
);
|
|
|
306 |
$this->assertEquals('2 months, 2 days ago', $result);
|
|
|
307 |
|
|
|
308 |
$result = $this->Time->timeAgoInWords(
|
|
|
309 |
strtotime('-2 months -2 days'),
|
|
|
310 |
array('end' => '1 month', 'format' => 'Y-m-d')
|
|
|
311 |
);
|
|
|
312 |
$this->assertEquals('on ' . date('Y-m-d', strtotime('-2 months -2 days')), $result);
|
|
|
313 |
|
|
|
314 |
$result = $this->Time->timeAgoInWords(
|
|
|
315 |
strtotime('-2 years -5 months -2 days'),
|
|
|
316 |
array('end' => '3 years')
|
|
|
317 |
);
|
|
|
318 |
$this->assertEquals('2 years, 5 months, 2 days ago', $result);
|
|
|
319 |
|
|
|
320 |
$result = $this->Time->timeAgoInWords(
|
|
|
321 |
strtotime('-2 weeks -2 days'),
|
|
|
322 |
'Y-m-d'
|
|
|
323 |
);
|
|
|
324 |
$this->assertEquals('2 weeks, 2 days ago', $result);
|
|
|
325 |
|
|
|
326 |
$time = strtotime('-3 years -12 months');
|
|
|
327 |
$result = $this->Time->timeAgoInWords($time);
|
|
|
328 |
$expected = 'on ' . date('j/n/y', $time);
|
|
|
329 |
$this->assertEquals($expected, $result);
|
|
|
330 |
|
|
|
331 |
$result = $this->Time->timeAgoInWords(
|
|
|
332 |
strtotime('-1 month -1 week -6 days'),
|
|
|
333 |
array('end' => '1 year', 'accuracy' => array('month' => 'month'))
|
|
|
334 |
);
|
|
|
335 |
$this->assertEquals('1 month ago', $result);
|
|
|
336 |
|
|
|
337 |
$timestamp = strtotime('-1 years -2 weeks -3 days');
|
|
|
338 |
$result = $this->Time->timeAgoInWords(
|
|
|
339 |
$timestamp,
|
|
|
340 |
array('accuracy' => array('year' => 'year'))
|
|
|
341 |
);
|
|
|
342 |
$expected = 'on ' . date('j/n/y', $timestamp);
|
|
|
343 |
$this->assertEquals($expected, $result);
|
|
|
344 |
|
|
|
345 |
$result = $this->Time->timeAgoInWords(
|
|
|
346 |
strtotime('-13 months -5 days'),
|
|
|
347 |
array('end' => '2 years')
|
|
|
348 |
);
|
|
|
349 |
$this->assertEquals('1 year, 1 month, 5 days ago', $result);
|
|
|
350 |
|
|
|
351 |
$result = $this->Time->timeAgoInWords(
|
|
|
352 |
strtotime('-58 minutes'),
|
|
|
353 |
array('accuracy' => 'hour')
|
|
|
354 |
);
|
|
|
355 |
$this->assertEquals('about an hour ago', $result);
|
|
|
356 |
|
|
|
357 |
$result = $this->Time->timeAgoInWords(
|
|
|
358 |
strtotime('-23 hours'),
|
|
|
359 |
array('accuracy' => 'day')
|
|
|
360 |
);
|
|
|
361 |
$this->assertEquals('about a day ago', $result);
|
|
|
362 |
}
|
|
|
363 |
|
|
|
364 |
/**
|
|
|
365 |
* testNice method
|
|
|
366 |
*
|
|
|
367 |
* @return void
|
|
|
368 |
*/
|
|
|
369 |
public function testNice() {
|
|
|
370 |
$time = time() + 2 * DAY;
|
|
|
371 |
$this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
|
|
|
372 |
|
|
|
373 |
$time = time() - 2 * DAY;
|
|
|
374 |
$this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
|
|
|
375 |
|
|
|
376 |
$time = time();
|
|
|
377 |
$this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
|
|
|
378 |
|
|
|
379 |
$time = 0;
|
|
|
380 |
$this->assertEquals(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
|
|
|
381 |
|
|
|
382 |
$time = null;
|
|
|
383 |
$this->assertEquals(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
|
|
|
384 |
|
|
|
385 |
$time = time();
|
|
|
386 |
$this->assertEquals(date('D', $time), $this->Time->nice($time, null, '%a'));
|
|
|
387 |
$this->assertEquals(date('M d, Y', $time), $this->Time->nice($time, null, '%b %d, %Y'));
|
|
|
388 |
|
|
|
389 |
$this->Time->niceFormat = '%Y-%d-%m';
|
|
|
390 |
$this->assertEquals(date('Y-d-m', $time), $this->Time->nice($time));
|
|
|
391 |
$this->assertEquals('%Y-%d-%m', $this->Time->niceFormat);
|
|
|
392 |
|
|
|
393 |
CakeTime::$niceFormat = '%Y-%d-%m %H:%M';
|
|
|
394 |
$this->assertEquals(date('Y-d-m H:i', $time), $this->Time->nice($time));
|
|
|
395 |
$this->assertEquals('%Y-%d-%m %H:%M', $this->Time->niceFormat);
|
|
|
396 |
|
|
|
397 |
date_default_timezone_set('UTC');
|
|
|
398 |
$result = $this->Time->nice(null, 'America/New_York');
|
|
|
399 |
$expected = $this->Time->nice(time(), 'America/New_York');
|
|
|
400 |
$this->assertEquals(substr($expected, 0, -1), substr($result, 0, -1));
|
|
|
401 |
|
|
|
402 |
$this->_restoreSystemTimezone();
|
|
|
403 |
}
|
|
|
404 |
|
|
|
405 |
/**
|
|
|
406 |
* testNiceShort method
|
|
|
407 |
*
|
|
|
408 |
* @return void
|
|
|
409 |
*/
|
|
|
410 |
public function testNiceShort() {
|
|
|
411 |
$time = time();
|
|
|
412 |
$this->assertEquals('Today, ' . date('H:i', $time), $this->Time->niceShort($time));
|
|
|
413 |
|
|
|
414 |
$time = time() - DAY;
|
|
|
415 |
$this->assertEquals('Yesterday, ' . date('H:i', $time), $this->Time->niceShort($time));
|
|
|
416 |
|
|
|
417 |
$time = time() + DAY;
|
|
|
418 |
$this->assertEquals('Tomorrow, ' . date('H:i', $time), $this->Time->niceShort($time));
|
|
|
419 |
|
|
|
420 |
$time = strtotime('+6 days');
|
|
|
421 |
$this->assertEquals('On ' . date('l F d, H:i', $time), $this->Time->niceShort($time));
|
|
|
422 |
|
|
|
423 |
$time = strtotime('-6 days');
|
|
|
424 |
$this->assertEquals(date('l F d, H:i', $time), $this->Time->niceShort($time));
|
|
|
425 |
|
|
|
426 |
date_default_timezone_set('Europe/London');
|
|
|
427 |
$result = $this->Time->niceShort('2005-01-15 10:00:00', new DateTimeZone('Europe/Brussels'));
|
|
|
428 |
$this->assertEquals('Jan 15th 2005, 11:00', $result);
|
|
|
429 |
|
|
|
430 |
date_default_timezone_set('UTC');
|
|
|
431 |
$result = $this->Time->niceShort(null, 'America/New_York');
|
|
|
432 |
$expected = $this->Time->niceShort(time(), 'America/New_York');
|
|
|
433 |
$this->assertEquals($expected, $result);
|
|
|
434 |
|
|
|
435 |
$this->_restoreSystemTimezone();
|
|
|
436 |
}
|
|
|
437 |
|
|
|
438 |
/**
|
|
|
439 |
* testDaysAsSql method
|
|
|
440 |
*
|
|
|
441 |
* @return void
|
|
|
442 |
*/
|
|
|
443 |
public function testDaysAsSql() {
|
|
|
444 |
$begin = time();
|
|
|
445 |
$end = time() + DAY;
|
|
|
446 |
$field = 'my_field';
|
|
|
447 |
$expected = '(my_field >= \'' . date('Y-m-d', $begin) . ' 00:00:00\') AND (my_field <= \'' . date('Y-m-d', $end) . ' 23:59:59\')';
|
|
|
448 |
$this->assertEquals($expected, $this->Time->daysAsSql($begin, $end, $field));
|
|
|
449 |
}
|
|
|
450 |
|
|
|
451 |
/**
|
|
|
452 |
* testDayAsSql method
|
|
|
453 |
*
|
|
|
454 |
* @return void
|
|
|
455 |
*/
|
|
|
456 |
public function testDayAsSql() {
|
|
|
457 |
$time = time();
|
|
|
458 |
$field = 'my_field';
|
|
|
459 |
$expected = '(my_field >= \'' . date('Y-m-d', $time) . ' 00:00:00\') AND (my_field <= \'' . date('Y-m-d', $time) . ' 23:59:59\')';
|
|
|
460 |
$this->assertEquals($expected, $this->Time->dayAsSql($time, $field));
|
|
|
461 |
}
|
|
|
462 |
|
|
|
463 |
/**
|
|
|
464 |
* testToUnix method
|
|
|
465 |
*
|
|
|
466 |
* @return void
|
|
|
467 |
*/
|
|
|
468 |
public function testToUnix() {
|
|
|
469 |
$this->assertEquals(time(), $this->Time->toUnix(time()));
|
|
|
470 |
$this->assertEquals(strtotime('+1 day'), $this->Time->toUnix('+1 day'));
|
|
|
471 |
$this->assertEquals(strtotime('+0 days'), $this->Time->toUnix('+0 days'));
|
|
|
472 |
$this->assertEquals(strtotime('-1 days'), $this->Time->toUnix('-1 days'));
|
|
|
473 |
$this->assertEquals(false, $this->Time->toUnix(''));
|
|
|
474 |
$this->assertEquals(false, $this->Time->toUnix(null));
|
|
|
475 |
}
|
|
|
476 |
|
|
|
477 |
/**
|
|
|
478 |
* testToServer method
|
|
|
479 |
*
|
|
|
480 |
* @return void
|
|
|
481 |
*/
|
|
|
482 |
public function testToServer() {
|
|
|
483 |
date_default_timezone_set('Europe/Paris');
|
|
|
484 |
|
|
|
485 |
$time = time();
|
|
|
486 |
$this->assertEquals(date('Y-m-d H:i:s', $time), $this->Time->toServer($time));
|
|
|
487 |
|
|
|
488 |
date_default_timezone_set('America/New_York');
|
|
|
489 |
$time = time();
|
|
|
490 |
date_default_timezone_set('Europe/Paris');
|
|
|
491 |
$result = $this->Time->toServer($time, 'America/New_York');
|
|
|
492 |
$this->assertEquals(date('Y-m-d H:i:s', $time), $result);
|
|
|
493 |
|
|
|
494 |
date_default_timezone_set('Europe/Paris');
|
|
|
495 |
$time = '2005-10-25 10:00:00';
|
|
|
496 |
$result = $this->Time->toServer($time);
|
|
|
497 |
$date = new DateTime($time, new DateTimeZone('UTC'));
|
|
|
498 |
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
|
|
|
499 |
$expected = $date->format('Y-m-d H:i:s');
|
|
|
500 |
$this->assertEquals($expected, $result);
|
|
|
501 |
|
|
|
502 |
$time = '2002-01-01 05:15:30';
|
|
|
503 |
$result = $this->Time->toServer($time, 'America/New_York');
|
|
|
504 |
$date = new DateTime($time, new DateTimeZone('America/New_York'));
|
|
|
505 |
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
|
|
|
506 |
$expected = $date->format('Y-m-d H:i:s');
|
|
|
507 |
$this->assertEquals($expected, $result);
|
|
|
508 |
|
|
|
509 |
$time = '2010-01-28T15:00:00+10:00';
|
|
|
510 |
$result = $this->Time->toServer($time, 'America/New_York');
|
|
|
511 |
$date = new DateTime($time);
|
|
|
512 |
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
|
|
|
513 |
$expected = $date->format('Y-m-d H:i:s');
|
|
|
514 |
$this->assertEquals($expected, $result);
|
|
|
515 |
|
|
|
516 |
$date = new DateTime(null, new DateTimeZone('America/New_York'));
|
|
|
517 |
$result = $this->Time->toServer($date, 'Pacific/Tahiti');
|
|
|
518 |
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
|
|
|
519 |
$expected = $date->format('Y-m-d H:i:s');
|
|
|
520 |
$this->assertEquals($expected, $result);
|
|
|
521 |
|
|
|
522 |
$this->_restoreSystemTimezone();
|
|
|
523 |
|
|
|
524 |
$time = time();
|
|
|
525 |
$result = $this->Time->toServer($time, null, 'l jS \of F Y h:i:s A');
|
|
|
526 |
$expected = date('l jS \of F Y h:i:s A', $time);
|
|
|
527 |
$this->assertEquals($expected, $result);
|
|
|
528 |
|
|
|
529 |
$this->assertFalse($this->Time->toServer(time(), new Object()));
|
|
|
530 |
|
|
|
531 |
date_default_timezone_set('UTC');
|
|
|
532 |
|
|
|
533 |
$serverTime = new DateTime('2012-12-11 14:15:20');
|
|
|
534 |
|
|
|
535 |
$timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu');
|
|
|
536 |
foreach ($timezones as $timezone) {
|
|
|
537 |
$result = $this->Time->toServer($serverTime->format('Y-m-d H:i:s'), $timezone, 'U');
|
|
|
538 |
$tz = new DateTimeZone($timezone);
|
|
|
539 |
$this->assertEquals($serverTime->format('U'), $result + $tz->getOffset($serverTime));
|
|
|
540 |
}
|
|
|
541 |
|
|
|
542 |
date_default_timezone_set('UTC');
|
|
|
543 |
$date = new DateTime('now', new DateTimeZone('America/New_York'));
|
|
|
544 |
|
|
|
545 |
$result = $this->Time->toServer($date, null, 'Y-m-d H:i:s');
|
|
|
546 |
$date->setTimezone($this->Time->timezone());
|
|
|
547 |
$expected = $date->format('Y-m-d H:i:s');
|
|
|
548 |
$this->assertEquals($expected, $result);
|
|
|
549 |
|
|
|
550 |
$this->_restoreSystemTimezone();
|
|
|
551 |
}
|
|
|
552 |
|
|
|
553 |
/**
|
|
|
554 |
* testToAtom method
|
|
|
555 |
*
|
|
|
556 |
* @return void
|
|
|
557 |
*/
|
|
|
558 |
public function testToAtom() {
|
|
|
559 |
$this->assertEquals(date('Y-m-d\TH:i:s\Z'), $this->Time->toAtom(time()));
|
|
|
560 |
}
|
|
|
561 |
|
|
|
562 |
/**
|
|
|
563 |
* testToRss method
|
|
|
564 |
*
|
|
|
565 |
* @return void
|
|
|
566 |
*/
|
|
|
567 |
public function testToRss() {
|
|
|
568 |
$date = '2012-08-12 12:12:45';
|
|
|
569 |
$time = strtotime($date);
|
|
|
570 |
$this->assertEquals(date('r', $time), $this->Time->toRss($time));
|
|
|
571 |
|
|
|
572 |
$timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu');
|
|
|
573 |
foreach ($timezones as $timezone) {
|
|
|
574 |
$yourTimezone = new DateTimeZone($timezone);
|
|
|
575 |
$yourTime = new DateTime($date, $yourTimezone);
|
|
|
576 |
$userOffset = $yourTimezone->getOffset($yourTime) / HOUR;
|
|
|
577 |
$time = $yourTime->format('U');
|
|
|
578 |
$this->assertEquals($yourTime->format('r'), $this->Time->toRss($time, $userOffset), "Failed on $timezone");
|
|
|
579 |
$this->assertEquals($yourTime->format('r'), $this->Time->toRss($time, $timezone), "Failed on $timezone");
|
|
|
580 |
}
|
|
|
581 |
}
|
|
|
582 |
|
|
|
583 |
/**
|
|
|
584 |
* testFormat method
|
|
|
585 |
*
|
|
|
586 |
* @return void
|
|
|
587 |
*/
|
|
|
588 |
public function testFormat() {
|
|
|
589 |
$format = 'D-M-Y';
|
|
|
590 |
$tz = date_default_timezone_get();
|
|
|
591 |
$arr = array(time(), strtotime('+1 days'), strtotime('+1 days'), strtotime('+0 days'));
|
|
|
592 |
foreach ($arr as $val) {
|
|
|
593 |
$this->assertEquals(date($format, $val), $this->Time->format($format, $val));
|
|
|
594 |
$this->assertEquals(date($format, $val), $this->Time->format($format, $val, false, $tz));
|
|
|
595 |
}
|
|
|
596 |
|
|
|
597 |
$result = $this->Time->format('Y-m-d', null, 'never');
|
|
|
598 |
$this->assertEquals('never', $result);
|
|
|
599 |
|
|
|
600 |
$result = $this->Time->format('2012-01-13', '%d-%m-%Y', 'invalid');
|
|
|
601 |
$this->assertEquals('13-01-2012', $result);
|
|
|
602 |
|
|
|
603 |
$result = $this->Time->format('nonsense', '%d-%m-%Y', 'invalid', 'UTC');
|
|
|
604 |
$this->assertEquals('invalid', $result);
|
|
|
605 |
|
|
|
606 |
$result = $this->Time->format('0000-00-00', '%d-%m-%Y', 'invalid');
|
|
|
607 |
$this->assertEquals('invalid', $result);
|
|
|
608 |
}
|
|
|
609 |
|
|
|
610 |
/**
|
|
|
611 |
* testOfGmt method
|
|
|
612 |
*
|
|
|
613 |
* @return void
|
|
|
614 |
*/
|
|
|
615 |
public function testGmt() {
|
|
|
616 |
$hour = 3;
|
|
|
617 |
$min = 4;
|
|
|
618 |
$sec = 2;
|
|
|
619 |
$month = 5;
|
|
|
620 |
$day = 14;
|
|
|
621 |
$year = 2007;
|
|
|
622 |
$time = mktime($hour, $min, $sec, $month, $day, $year);
|
|
|
623 |
$expected = gmmktime($hour, $min, $sec, $month, $day, $year);
|
|
|
624 |
$this->assertEquals($expected, $this->Time->gmt(date('Y-n-j G:i:s', $time)));
|
|
|
625 |
|
|
|
626 |
$hour = date('H');
|
|
|
627 |
$min = date('i');
|
|
|
628 |
$sec = date('s');
|
|
|
629 |
$month = date('m');
|
|
|
630 |
$day = date('d');
|
|
|
631 |
$year = date('Y');
|
|
|
632 |
$expected = gmmktime($hour, $min, $sec, $month, $day, $year);
|
|
|
633 |
$this->assertEquals($expected, $this->Time->gmt(null));
|
|
|
634 |
}
|
|
|
635 |
|
|
|
636 |
/**
|
|
|
637 |
* testIsToday method
|
|
|
638 |
*
|
|
|
639 |
* @return void
|
|
|
640 |
*/
|
|
|
641 |
public function testIsToday() {
|
|
|
642 |
$result = $this->Time->isToday('+1 day');
|
|
|
643 |
$this->assertFalse($result);
|
|
|
644 |
$result = $this->Time->isToday('+1 days');
|
|
|
645 |
$this->assertFalse($result);
|
|
|
646 |
$result = $this->Time->isToday('+0 day');
|
|
|
647 |
$this->assertTrue($result);
|
|
|
648 |
$result = $this->Time->isToday('-1 day');
|
|
|
649 |
$this->assertFalse($result);
|
|
|
650 |
}
|
|
|
651 |
|
|
|
652 |
/**
|
|
|
653 |
* testIsFuture method
|
|
|
654 |
*
|
|
|
655 |
* @return void
|
|
|
656 |
*/
|
|
|
657 |
public function testIsFuture() {
|
|
|
658 |
$this->assertTrue($this->Time->isFuture('+1 month'));
|
|
|
659 |
$this->assertTrue($this->Time->isFuture('+1 days'));
|
|
|
660 |
$this->assertTrue($this->Time->isFuture('+1 minute'));
|
|
|
661 |
$this->assertTrue($this->Time->isFuture('+1 second'));
|
|
|
662 |
|
|
|
663 |
$this->assertFalse($this->Time->isFuture('-1 second'));
|
|
|
664 |
$this->assertFalse($this->Time->isFuture('-1 day'));
|
|
|
665 |
$this->assertFalse($this->Time->isFuture('-1 week'));
|
|
|
666 |
$this->assertFalse($this->Time->isFuture('-1 month'));
|
|
|
667 |
}
|
|
|
668 |
|
|
|
669 |
/**
|
|
|
670 |
* testIsPast method
|
|
|
671 |
*
|
|
|
672 |
* @return void
|
|
|
673 |
*/
|
|
|
674 |
public function testIsPast() {
|
|
|
675 |
$this->assertFalse($this->Time->isPast('+1 month'));
|
|
|
676 |
$this->assertFalse($this->Time->isPast('+1 days'));
|
|
|
677 |
$this->assertFalse($this->Time->isPast('+1 minute'));
|
|
|
678 |
$this->assertFalse($this->Time->isPast('+1 second'));
|
|
|
679 |
|
|
|
680 |
$this->assertTrue($this->Time->isPast('-1 second'));
|
|
|
681 |
$this->assertTrue($this->Time->isPast('-1 day'));
|
|
|
682 |
$this->assertTrue($this->Time->isPast('-1 week'));
|
|
|
683 |
$this->assertTrue($this->Time->isPast('-1 month'));
|
|
|
684 |
}
|
|
|
685 |
|
|
|
686 |
/**
|
|
|
687 |
* testIsThisWeek method
|
|
|
688 |
*
|
|
|
689 |
* @return void
|
|
|
690 |
*/
|
|
|
691 |
public function testIsThisWeek() {
|
|
|
692 |
// A map of days which goes from -1 day of week to +1 day of week
|
|
|
693 |
$map = array(
|
|
|
694 |
'Mon' => array(-1, 7), 'Tue' => array(-2, 6), 'Wed' => array(-3, 5),
|
|
|
695 |
'Thu' => array(-4, 4), 'Fri' => array(-5, 3), 'Sat' => array(-6, 2),
|
|
|
696 |
'Sun' => array(-7, 1)
|
|
|
697 |
);
|
|
|
698 |
$days = $map[date('D')];
|
|
|
699 |
|
|
|
700 |
for ($day = $days[0] + 1; $day < $days[1]; $day++) {
|
|
|
701 |
$this->assertTrue($this->Time->isThisWeek(($day > 0 ? '+' : '') . $day . ' days'));
|
|
|
702 |
}
|
|
|
703 |
$this->assertFalse($this->Time->isThisWeek($days[0] . ' days'));
|
|
|
704 |
$this->assertFalse($this->Time->isThisWeek('+' . $days[1] . ' days'));
|
|
|
705 |
}
|
|
|
706 |
|
|
|
707 |
/**
|
|
|
708 |
* testIsThisMonth method
|
|
|
709 |
*
|
|
|
710 |
* @return void
|
|
|
711 |
*/
|
|
|
712 |
public function testIsThisMonth() {
|
|
|
713 |
$result = $this->Time->isThisMonth('+0 day');
|
|
|
714 |
$this->assertTrue($result);
|
|
|
715 |
$result = $this->Time->isThisMonth($time = mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y')));
|
|
|
716 |
$this->assertTrue($result);
|
|
|
717 |
$result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') - mt_rand(1, 12)));
|
|
|
718 |
$this->assertFalse($result);
|
|
|
719 |
$result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') + mt_rand(1, 12)));
|
|
|
720 |
$this->assertFalse($result);
|
|
|
721 |
}
|
|
|
722 |
|
|
|
723 |
/**
|
|
|
724 |
* testIsThisYear method
|
|
|
725 |
*
|
|
|
726 |
* @return void
|
|
|
727 |
*/
|
|
|
728 |
public function testIsThisYear() {
|
|
|
729 |
$result = $this->Time->isThisYear('+0 day');
|
|
|
730 |
$this->assertTrue($result);
|
|
|
731 |
$result = $this->Time->isThisYear(mktime(0, 0, 0, mt_rand(1, 12), mt_rand(1, 28), date('Y')));
|
|
|
732 |
$this->assertTrue($result);
|
|
|
733 |
}
|
|
|
734 |
|
|
|
735 |
/**
|
|
|
736 |
* testWasYesterday method
|
|
|
737 |
*
|
|
|
738 |
* @return void
|
|
|
739 |
*/
|
|
|
740 |
public function testWasYesterday() {
|
|
|
741 |
$result = $this->Time->wasYesterday('+1 day');
|
|
|
742 |
$this->assertFalse($result);
|
|
|
743 |
$result = $this->Time->wasYesterday('+1 days');
|
|
|
744 |
$this->assertFalse($result);
|
|
|
745 |
$result = $this->Time->wasYesterday('+0 day');
|
|
|
746 |
$this->assertFalse($result);
|
|
|
747 |
$result = $this->Time->wasYesterday('-1 day');
|
|
|
748 |
$this->assertTrue($result);
|
|
|
749 |
$result = $this->Time->wasYesterday('-1 days');
|
|
|
750 |
$this->assertTrue($result);
|
|
|
751 |
$result = $this->Time->wasYesterday('-2 days');
|
|
|
752 |
$this->assertFalse($result);
|
|
|
753 |
}
|
|
|
754 |
|
|
|
755 |
/**
|
|
|
756 |
* testIsTomorrow method
|
|
|
757 |
*
|
|
|
758 |
* @return void
|
|
|
759 |
*/
|
|
|
760 |
public function testIsTomorrow() {
|
|
|
761 |
$result = $this->Time->isTomorrow('+1 day');
|
|
|
762 |
$this->assertTrue($result);
|
|
|
763 |
$result = $this->Time->isTomorrow('+1 days');
|
|
|
764 |
$this->assertTrue($result);
|
|
|
765 |
$result = $this->Time->isTomorrow('+0 day');
|
|
|
766 |
$this->assertFalse($result);
|
|
|
767 |
$result = $this->Time->isTomorrow('-1 day');
|
|
|
768 |
$this->assertFalse($result);
|
|
|
769 |
}
|
|
|
770 |
|
|
|
771 |
/**
|
|
|
772 |
* testWasWithinLast method
|
|
|
773 |
*
|
|
|
774 |
* @return void
|
|
|
775 |
*/
|
|
|
776 |
public function testWasWithinLast() {
|
|
|
777 |
$this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
|
|
|
778 |
$this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week'));
|
|
|
779 |
$this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
|
|
|
780 |
$this->assertTrue($this->Time->wasWithinLast('1 second', '-1 second'));
|
|
|
781 |
$this->assertTrue($this->Time->wasWithinLast('1 minute', '-1 minute'));
|
|
|
782 |
$this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
|
|
|
783 |
$this->assertTrue($this->Time->wasWithinLast('1 month', '-1 month'));
|
|
|
784 |
$this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
|
|
|
785 |
|
|
|
786 |
$this->assertTrue($this->Time->wasWithinLast('1 week', '-1 day'));
|
|
|
787 |
$this->assertTrue($this->Time->wasWithinLast('2 week', '-1 week'));
|
|
|
788 |
$this->assertFalse($this->Time->wasWithinLast('1 second', '-1 year'));
|
|
|
789 |
$this->assertTrue($this->Time->wasWithinLast('10 minutes', '-1 second'));
|
|
|
790 |
$this->assertTrue($this->Time->wasWithinLast('23 minutes', '-1 minute'));
|
|
|
791 |
$this->assertFalse($this->Time->wasWithinLast('0 year', '-1 year'));
|
|
|
792 |
$this->assertTrue($this->Time->wasWithinLast('13 month', '-1 month'));
|
|
|
793 |
$this->assertTrue($this->Time->wasWithinLast('2 days', '-1 day'));
|
|
|
794 |
|
|
|
795 |
$this->assertFalse($this->Time->wasWithinLast('1 week', '-2 weeks'));
|
|
|
796 |
$this->assertFalse($this->Time->wasWithinLast('1 second', '-2 seconds'));
|
|
|
797 |
$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
|
|
|
798 |
$this->assertFalse($this->Time->wasWithinLast('1 hour', '-2 hours'));
|
|
|
799 |
$this->assertFalse($this->Time->wasWithinLast('1 month', '-2 months'));
|
|
|
800 |
$this->assertFalse($this->Time->wasWithinLast('1 year', '-2 years'));
|
|
|
801 |
|
|
|
802 |
$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 weeks'));
|
|
|
803 |
$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
|
|
|
804 |
$this->assertFalse($this->Time->wasWithinLast('0 days', '-2 days'));
|
|
|
805 |
$this->assertTrue($this->Time->wasWithinLast('1 hour', '-20 seconds'));
|
|
|
806 |
$this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds'));
|
|
|
807 |
$this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months'));
|
|
|
808 |
$this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months'));
|
|
|
809 |
|
|
|
810 |
$this->assertTrue($this->Time->wasWithinLast('5 ', '-3 days'));
|
|
|
811 |
$this->assertTrue($this->Time->wasWithinLast('1 ', '-1 hour'));
|
|
|
812 |
$this->assertTrue($this->Time->wasWithinLast('1 ', '-1 minute'));
|
|
|
813 |
$this->assertTrue($this->Time->wasWithinLast('1 ', '-23 hours -59 minutes -59 seconds'));
|
|
|
814 |
}
|
|
|
815 |
|
|
|
816 |
/**
|
|
|
817 |
* testWasWithinLast method
|
|
|
818 |
*
|
|
|
819 |
* @return void
|
|
|
820 |
*/
|
|
|
821 |
public function testIsWithinNext() {
|
|
|
822 |
$this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
|
|
|
823 |
$this->assertFalse($this->Time->isWithinNext('1 week', '-1 week'));
|
|
|
824 |
$this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
|
|
|
825 |
$this->assertFalse($this->Time->isWithinNext('1 second', '-1 second'));
|
|
|
826 |
$this->assertFalse($this->Time->isWithinNext('1 minute', '-1 minute'));
|
|
|
827 |
$this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
|
|
|
828 |
$this->assertFalse($this->Time->isWithinNext('1 month', '-1 month'));
|
|
|
829 |
$this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
|
|
|
830 |
|
|
|
831 |
$this->assertFalse($this->Time->isWithinNext('1 week', '-1 day'));
|
|
|
832 |
$this->assertFalse($this->Time->isWithinNext('2 week', '-1 week'));
|
|
|
833 |
$this->assertFalse($this->Time->isWithinNext('1 second', '-1 year'));
|
|
|
834 |
$this->assertFalse($this->Time->isWithinNext('10 minutes', '-1 second'));
|
|
|
835 |
$this->assertFalse($this->Time->isWithinNext('23 minutes', '-1 minute'));
|
|
|
836 |
$this->assertFalse($this->Time->isWithinNext('0 year', '-1 year'));
|
|
|
837 |
$this->assertFalse($this->Time->isWithinNext('13 month', '-1 month'));
|
|
|
838 |
$this->assertFalse($this->Time->isWithinNext('2 days', '-1 day'));
|
|
|
839 |
|
|
|
840 |
$this->assertFalse($this->Time->isWithinNext('1 week', '-2 weeks'));
|
|
|
841 |
$this->assertFalse($this->Time->isWithinNext('1 second', '-2 seconds'));
|
|
|
842 |
$this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
|
|
|
843 |
$this->assertFalse($this->Time->isWithinNext('1 hour', '-2 hours'));
|
|
|
844 |
$this->assertFalse($this->Time->isWithinNext('1 month', '-2 months'));
|
|
|
845 |
$this->assertFalse($this->Time->isWithinNext('1 year', '-2 years'));
|
|
|
846 |
|
|
|
847 |
$this->assertFalse($this->Time->isWithinNext('1 day', '-2 weeks'));
|
|
|
848 |
$this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
|
|
|
849 |
$this->assertFalse($this->Time->isWithinNext('0 days', '-2 days'));
|
|
|
850 |
$this->assertFalse($this->Time->isWithinNext('1 hour', '-20 seconds'));
|
|
|
851 |
$this->assertFalse($this->Time->isWithinNext('1 year', '-60 minutes -30 seconds'));
|
|
|
852 |
$this->assertFalse($this->Time->isWithinNext('3 years', '-2 months'));
|
|
|
853 |
$this->assertFalse($this->Time->isWithinNext('5 months', '-4 months'));
|
|
|
854 |
|
|
|
855 |
$this->assertFalse($this->Time->isWithinNext('5 ', '-3 days'));
|
|
|
856 |
$this->assertFalse($this->Time->isWithinNext('1 ', '-1 hour'));
|
|
|
857 |
$this->assertFalse($this->Time->isWithinNext('1 ', '-1 minute'));
|
|
|
858 |
$this->assertFalse($this->Time->isWithinNext('1 ', '-23 hours -59 minutes -59 seconds'));
|
|
|
859 |
|
|
|
860 |
$this->assertTrue($this->Time->isWithinNext('7 days', '6 days, 23 hours, 59 minutes, 59 seconds'));
|
|
|
861 |
$this->assertFalse($this->Time->isWithinNext('7 days', '6 days, 23 hours, 59 minutes, 61 seconds'));
|
|
|
862 |
}
|
|
|
863 |
|
|
|
864 |
/**
|
|
|
865 |
* testUserOffset method
|
|
|
866 |
*
|
|
|
867 |
* @return void
|
|
|
868 |
*/
|
|
|
869 |
public function testUserOffset() {
|
|
|
870 |
$timezoneServer = new DateTimeZone(date_default_timezone_get());
|
|
|
871 |
$timeServer = new DateTime('now', $timezoneServer);
|
|
|
872 |
$yourTimezone = $timezoneServer->getOffset($timeServer) / HOUR;
|
|
|
873 |
|
|
|
874 |
$expected = time();
|
|
|
875 |
$result = $this->Time->fromString(time(), $yourTimezone);
|
|
|
876 |
$this->assertWithinMargin($expected, $result, 1);
|
|
|
877 |
|
|
|
878 |
$result = $this->Time->fromString(time(), $timezoneServer->getName());
|
|
|
879 |
$this->assertWithinMargin($expected, $result, 1);
|
|
|
880 |
|
|
|
881 |
$result = $this->Time->fromString(time(), $timezoneServer);
|
|
|
882 |
$this->assertWithinMargin($expected, $result, 1);
|
|
|
883 |
|
|
|
884 |
Configure::write('Config.timezone', $timezoneServer->getName());
|
|
|
885 |
$result = $this->Time->fromString(time());
|
|
|
886 |
$this->assertWithinMargin($expected, $result, 1);
|
|
|
887 |
Configure::delete('Config.timezone');
|
|
|
888 |
}
|
|
|
889 |
|
|
|
890 |
/**
|
|
|
891 |
* test fromString()
|
|
|
892 |
*
|
|
|
893 |
* @return void
|
|
|
894 |
*/
|
|
|
895 |
public function testFromString() {
|
|
|
896 |
$result = $this->Time->fromString('');
|
|
|
897 |
$this->assertFalse($result);
|
|
|
898 |
|
|
|
899 |
$result = $this->Time->fromString(0, 0);
|
|
|
900 |
$this->assertFalse($result);
|
|
|
901 |
|
|
|
902 |
$result = $this->Time->fromString('+1 hour');
|
|
|
903 |
$expected = strtotime('+1 hour');
|
|
|
904 |
$this->assertWithinMargin($expected, $result, 1);
|
|
|
905 |
|
|
|
906 |
$timezone = date('Z', time());
|
|
|
907 |
$result = $this->Time->fromString('+1 hour', $timezone);
|
|
|
908 |
$expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
|
|
|
909 |
$this->assertWithinMargin($expected, $result, 1);
|
|
|
910 |
|
|
|
911 |
$timezone = date_default_timezone_get();
|
|
|
912 |
$result = $this->Time->fromString('+1 hour', $timezone);
|
|
|
913 |
$expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
|
|
|
914 |
$this->assertWithinMargin($expected, $result, 1);
|
|
|
915 |
|
|
|
916 |
date_default_timezone_set('UTC');
|
|
|
917 |
$date = new DateTime('now', new DateTimeZone('Europe/London'));
|
|
|
918 |
$this->Time->fromString($date);
|
|
|
919 |
$this->assertEquals('Europe/London', $date->getTimeZone()->getName());
|
|
|
920 |
|
|
|
921 |
$this->_restoreSystemTimezone();
|
|
|
922 |
}
|
|
|
923 |
|
|
|
924 |
/**
|
|
|
925 |
* test fromString() with a DateTime object as the dateString
|
|
|
926 |
*
|
|
|
927 |
* @return void
|
|
|
928 |
*/
|
|
|
929 |
public function testFromStringWithDateTime() {
|
|
|
930 |
date_default_timezone_set('UTC');
|
|
|
931 |
|
|
|
932 |
$date = new DateTime('+1 hour', new DateTimeZone('America/New_York'));
|
|
|
933 |
$result = $this->Time->fromString($date, 'UTC');
|
|
|
934 |
$date->setTimezone(new DateTimeZone('UTC'));
|
|
|
935 |
$expected = $date->format('U') + $date->getOffset();
|
|
|
936 |
|
|
|
937 |
$this->assertWithinMargin($expected, $result, 1);
|
|
|
938 |
|
|
|
939 |
date_default_timezone_set('Australia/Melbourne');
|
|
|
940 |
|
|
|
941 |
$date = new DateTime('+1 hour', new DateTimeZone('America/New_York'));
|
|
|
942 |
$result = $this->Time->fromString($date, 'Asia/Kuwait');
|
|
|
943 |
|
|
|
944 |
$date->setTimezone(new DateTimeZone('Asia/Kuwait'));
|
|
|
945 |
$expected = $date->format('U') + $date->getOffset();
|
|
|
946 |
$this->assertWithinMargin($expected, $result, 1);
|
|
|
947 |
|
|
|
948 |
$this->_restoreSystemTimezone();
|
|
|
949 |
}
|
|
|
950 |
|
|
|
951 |
/**
|
|
|
952 |
* Test that datetimes in the default timezone are not modified.
|
|
|
953 |
*
|
|
|
954 |
* @return void
|
|
|
955 |
*/
|
|
|
956 |
public function testFromStringWithDateTimeNoConversion() {
|
|
|
957 |
Configure::write('Config.timezone', date_default_timezone_get());
|
|
|
958 |
$date = new DateTime('2013-04-09');
|
|
|
959 |
$result = $this->Time->fromString($date);
|
|
|
960 |
$this->assertEquals($result, $date->format('U'));
|
|
|
961 |
}
|
|
|
962 |
|
|
|
963 |
/**
|
|
|
964 |
* test converting time specifiers using a time definition localfe file
|
|
|
965 |
*
|
|
|
966 |
* @return void
|
|
|
967 |
*/
|
|
|
968 |
public function testConvertSpecifiers() {
|
|
|
969 |
App::build(array(
|
|
|
970 |
'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS)
|
|
|
971 |
), App::RESET);
|
|
|
972 |
Configure::write('Config.language', 'time_test');
|
|
|
973 |
$time = strtotime('Thu Jan 14 11:43:39 2010');
|
|
|
974 |
|
|
|
975 |
$result = $this->Time->convertSpecifiers('%a', $time);
|
|
|
976 |
$expected = 'jue';
|
|
|
977 |
$this->assertEquals($expected, $result);
|
|
|
978 |
|
|
|
979 |
$result = $this->Time->convertSpecifiers('%A', $time);
|
|
|
980 |
$expected = 'jueves';
|
|
|
981 |
$this->assertEquals($expected, $result);
|
|
|
982 |
|
|
|
983 |
$result = $this->Time->convertSpecifiers('%c', $time);
|
|
|
984 |
$expected = 'jue %d ene %Y %H:%M:%S %Z';
|
|
|
985 |
$this->assertEquals($expected, $result);
|
|
|
986 |
|
|
|
987 |
$result = $this->Time->convertSpecifiers('%C', $time);
|
|
|
988 |
$expected = '20';
|
|
|
989 |
$this->assertEquals($expected, $result);
|
|
|
990 |
|
|
|
991 |
$result = $this->Time->convertSpecifiers('%D', $time);
|
|
|
992 |
$expected = '%m/%d/%y';
|
|
|
993 |
$this->assertEquals($expected, $result);
|
|
|
994 |
|
|
|
995 |
$result = $this->Time->convertSpecifiers('%b', $time);
|
|
|
996 |
$expected = 'ene';
|
|
|
997 |
$this->assertEquals($expected, $result);
|
|
|
998 |
|
|
|
999 |
$result = $this->Time->convertSpecifiers('%h', $time);
|
|
|
1000 |
$expected = 'ene';
|
|
|
1001 |
$this->assertEquals($expected, $result);
|
|
|
1002 |
|
|
|
1003 |
$result = $this->Time->convertSpecifiers('%B', $time);
|
|
|
1004 |
$expected = 'enero';
|
|
|
1005 |
$this->assertEquals($expected, $result);
|
|
|
1006 |
|
|
|
1007 |
$result = $this->Time->convertSpecifiers('%n', $time);
|
|
|
1008 |
$expected = "\n";
|
|
|
1009 |
$this->assertEquals($expected, $result);
|
|
|
1010 |
|
|
|
1011 |
$result = $this->Time->convertSpecifiers('%n', $time);
|
|
|
1012 |
$expected = "\n";
|
|
|
1013 |
$this->assertEquals($expected, $result);
|
|
|
1014 |
|
|
|
1015 |
$result = $this->Time->convertSpecifiers('%p', $time);
|
|
|
1016 |
$expected = 'AM';
|
|
|
1017 |
$this->assertEquals($expected, $result);
|
|
|
1018 |
|
|
|
1019 |
$result = $this->Time->convertSpecifiers('%P', $time);
|
|
|
1020 |
$expected = 'am';
|
|
|
1021 |
$this->assertEquals($expected, $result);
|
|
|
1022 |
|
|
|
1023 |
$result = $this->Time->convertSpecifiers('%r', $time);
|
|
|
1024 |
$expected = '%I:%M:%S AM';
|
|
|
1025 |
$this->assertEquals($expected, $result);
|
|
|
1026 |
|
|
|
1027 |
$result = $this->Time->convertSpecifiers('%R', $time);
|
|
|
1028 |
$expected = '11:43';
|
|
|
1029 |
$this->assertEquals($expected, $result);
|
|
|
1030 |
|
|
|
1031 |
$result = $this->Time->convertSpecifiers('%t', $time);
|
|
|
1032 |
$expected = "\t";
|
|
|
1033 |
$this->assertEquals($expected, $result);
|
|
|
1034 |
|
|
|
1035 |
$result = $this->Time->convertSpecifiers('%T', $time);
|
|
|
1036 |
$expected = '%H:%M:%S';
|
|
|
1037 |
$this->assertEquals($expected, $result);
|
|
|
1038 |
|
|
|
1039 |
$result = $this->Time->convertSpecifiers('%u', $time);
|
|
|
1040 |
$expected = 4;
|
|
|
1041 |
$this->assertEquals($expected, $result);
|
|
|
1042 |
|
|
|
1043 |
$result = $this->Time->convertSpecifiers('%x', $time);
|
|
|
1044 |
$expected = '%d/%m/%y';
|
|
|
1045 |
$this->assertEquals($expected, $result);
|
|
|
1046 |
|
|
|
1047 |
$result = $this->Time->convertSpecifiers('%X', $time);
|
|
|
1048 |
$expected = '%H:%M:%S';
|
|
|
1049 |
$this->assertEquals($expected, $result);
|
|
|
1050 |
}
|
|
|
1051 |
|
|
|
1052 |
/**
|
|
|
1053 |
* test convert %e on windows.
|
|
|
1054 |
*
|
|
|
1055 |
* @return void
|
|
|
1056 |
*/
|
|
|
1057 |
public function testConvertPercentE() {
|
|
|
1058 |
$this->skipIf(DIRECTORY_SEPARATOR !== '\\', 'Cannot run windows tests on non-windows OS.');
|
|
|
1059 |
|
|
|
1060 |
$time = strtotime('Thu Jan 14 11:43:39 2010');
|
|
|
1061 |
$result = $this->Time->convertSpecifiers('%e', $time);
|
|
|
1062 |
$expected = '14';
|
|
|
1063 |
$this->assertEquals($expected, $result);
|
|
|
1064 |
|
|
|
1065 |
$result = $this->Time->convertSpecifiers('%e', strtotime('2011-01-01'));
|
|
|
1066 |
$expected = ' 1';
|
|
|
1067 |
$this->assertEquals($expected, $result);
|
|
|
1068 |
}
|
|
|
1069 |
|
|
|
1070 |
/**
|
|
|
1071 |
* test formatting dates taking in account preferred i18n locale file
|
|
|
1072 |
*
|
|
|
1073 |
* @return void
|
|
|
1074 |
*/
|
|
|
1075 |
public function testI18nFormat() {
|
|
|
1076 |
App::build(array(
|
|
|
1077 |
'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS)
|
|
|
1078 |
), App::RESET);
|
|
|
1079 |
Configure::write('Config.language', 'time_test');
|
|
|
1080 |
|
|
|
1081 |
$time = strtotime('Thu Jan 14 13:59:28 2010');
|
|
|
1082 |
|
|
|
1083 |
$result = $this->Time->i18nFormat($time);
|
|
|
1084 |
$expected = '14/01/10';
|
|
|
1085 |
$this->assertEquals($expected, $result);
|
|
|
1086 |
|
|
|
1087 |
$result = $this->Time->i18nFormat($time, '%c');
|
|
|
1088 |
$expected = 'jue 14 ene 2010 13:59:28 ' . utf8_encode(strftime('%Z', $time));
|
|
|
1089 |
$this->assertEquals($expected, $result);
|
|
|
1090 |
|
|
|
1091 |
$result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
|
|
|
1092 |
$expected = 'Time is 01:59:28 PM, and date is 14/01/10';
|
|
|
1093 |
$this->assertEquals($expected, $result);
|
|
|
1094 |
|
|
|
1095 |
$time = strtotime('Wed Jan 13 13:59:28 2010');
|
|
|
1096 |
|
|
|
1097 |
$result = $this->Time->i18nFormat($time);
|
|
|
1098 |
$expected = '13/01/10';
|
|
|
1099 |
$this->assertEquals($expected, $result);
|
|
|
1100 |
|
|
|
1101 |
$result = $this->Time->i18nFormat($time, '%c');
|
|
|
1102 |
$expected = 'miƩ 13 ene 2010 13:59:28 ' . utf8_encode(strftime('%Z', $time));
|
|
|
1103 |
$this->assertEquals($expected, $result);
|
|
|
1104 |
|
|
|
1105 |
$result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
|
|
|
1106 |
$expected = 'Time is 01:59:28 PM, and date is 13/01/10';
|
|
|
1107 |
$this->assertEquals($expected, $result);
|
|
|
1108 |
|
|
|
1109 |
$result = $this->Time->i18nFormat('invalid date', '%x', 'Date invalid');
|
|
|
1110 |
$expected = 'Date invalid';
|
|
|
1111 |
$this->assertEquals($expected, $result);
|
|
|
1112 |
}
|
|
|
1113 |
|
|
|
1114 |
/**
|
|
|
1115 |
* test new format() syntax which inverts first and second parameters
|
|
|
1116 |
*
|
|
|
1117 |
* @return void
|
|
|
1118 |
*/
|
|
|
1119 |
public function testFormatNewSyntax() {
|
|
|
1120 |
$time = time();
|
|
|
1121 |
$this->assertEquals($this->Time->format($time), $this->Time->i18nFormat($time));
|
|
|
1122 |
$this->assertEquals($this->Time->format($time, '%c'), $this->Time->i18nFormat($time, '%c'));
|
|
|
1123 |
}
|
|
|
1124 |
|
|
|
1125 |
/**
|
|
|
1126 |
* testListTimezones
|
|
|
1127 |
*
|
|
|
1128 |
* @return void
|
|
|
1129 |
*/
|
|
|
1130 |
public function testListTimezones() {
|
|
|
1131 |
$return = CakeTime::listTimezones();
|
|
|
1132 |
$this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
|
|
|
1133 |
$this->assertEquals('Bangkok', $return['Asia']['Asia/Bangkok']);
|
|
|
1134 |
$this->assertTrue(isset($return['America']['America/Argentina/Buenos_Aires']));
|
|
|
1135 |
$this->assertEquals('Argentina/Buenos_Aires', $return['America']['America/Argentina/Buenos_Aires']);
|
|
|
1136 |
$this->assertTrue(isset($return['UTC']['UTC']));
|
|
|
1137 |
$this->assertFalse(isset($return['Cuba']));
|
|
|
1138 |
$this->assertFalse(isset($return['US']));
|
|
|
1139 |
|
|
|
1140 |
$return = CakeTime::listTimezones('#^Asia/#');
|
|
|
1141 |
$this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
|
|
|
1142 |
$this->assertFalse(isset($return['Pacific']));
|
|
|
1143 |
|
|
|
1144 |
$return = CakeTime::listTimezones('#^(America|Pacific)/#', null, false);
|
|
|
1145 |
$this->assertTrue(isset($return['America/Argentina/Buenos_Aires']));
|
|
|
1146 |
$this->assertTrue(isset($return['Pacific/Tahiti']));
|
|
|
1147 |
|
|
|
1148 |
if (!$this->skipIf(version_compare(PHP_VERSION, '5.3.0', '<'))) {
|
|
|
1149 |
$return = CakeTime::listTimezones(DateTimeZone::ASIA);
|
|
|
1150 |
$this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
|
|
|
1151 |
$this->assertFalse(isset($return['Pacific']));
|
|
|
1152 |
|
|
|
1153 |
$return = CakeTime::listTimezones(DateTimeZone::PER_COUNTRY, 'US', false);
|
|
|
1154 |
$this->assertTrue(isset($return['Pacific/Honolulu']));
|
|
|
1155 |
$this->assertFalse(isset($return['Asia/Bangkok']));
|
|
|
1156 |
}
|
|
|
1157 |
}
|
|
|
1158 |
|
|
|
1159 |
/**
|
|
|
1160 |
* Tests that using CakeTime::format() with the correct sytax actually converts
|
|
|
1161 |
* from one timezone to the other correctly
|
|
|
1162 |
*
|
|
|
1163 |
* @return void
|
|
|
1164 |
*/
|
|
|
1165 |
public function testCorrectTimezoneConversion() {
|
|
|
1166 |
date_default_timezone_set('UTC');
|
|
|
1167 |
$date = '2012-01-01 10:00:00';
|
|
|
1168 |
$converted = CakeTime::format($date, '%Y-%m-%d %H:%M', '', 'Europe/Copenhagen');
|
|
|
1169 |
$expected = new DateTime($date);
|
|
|
1170 |
$expected->setTimezone(new DateTimeZone('Europe/Copenhagen'));
|
|
|
1171 |
$this->assertEquals($expected->format('Y-m-d H:i'), $converted);
|
|
|
1172 |
}
|
|
|
1173 |
|
|
|
1174 |
}
|