]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/Date/tests/DateTest.php
[PEAR] Modernize Validate code
[quix0rs-gnu-social.git] / extlib / Date / tests / DateTest.php
1 <?php
2 // vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4:
3 // +----------------------------------------------------------------------+
4 // | PHP Version 4                                                        |
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2003 Marshall Roch                                |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to the New BSD license, That is bundled  |
9 // | with this package in the file LICENSE, and is available through      |
10 // | the world-wide-web at                                                |
11 // | http://www.opensource.org/licenses/bsd-license.php                   |
12 // | If you did not receive a copy of the new BSDlicense and are unable   |
13 // | to obtain it through the world-wide-web, please send a note to       |
14 // | pear-dev@lists.php.net so we can mail you a copy immediately.        |
15 // +----------------------------------------------------------------------+
16 // | Author: Marshall Roch <mroch@php.net>                                |
17 // +----------------------------------------------------------------------+
18 //
19 // $Id$
20 //
21
22 require_once 'Date.php';
23 require_once 'PHPUnit/Autoload.php';
24
25 class myDate extends Date
26 {
27     public function myDate($date)
28     {
29         $this->Date($date);
30     }
31 }
32
33 /**
34  * Test case for Date
35  *
36  * @package Date
37  * @author Marshall Roch <mroch@php.net>
38  */
39 class Date_Test extends PHPUnit_Framework_TestCase
40 {
41     public $time;
42
43     public function setUp()
44     {
45         $this->time = new Date("2003-10-04 14:03:24Z");
46     }
47
48     public function tearDown()
49     {
50         unset($this->time);
51     }
52
53     public function testDateNull()
54     {
55         $time = new Date();
56         $this->assertEquals(
57             date('Y-m-d H:i:s'),
58             sprintf(
59                 '%04d-%02d-%02d %02d:%02d:%02d',
60                 $time->year,
61                 $time->month,
62                 $time->day,
63                 $time->hour,
64                 $time->minute,
65                 $time->second
66             )
67         );
68     }
69
70     public function testAbstraction()
71     {
72         $d = new Date();
73         $my = new myDate($d);
74         $this->assertEquals($d->getDate(), $my->getDate());
75     }
76
77     public function testDateCopy()
78     {
79         $temp = new Date($this->time);
80         $this->assertEquals($temp, $this->time);
81     }
82
83     public function testDateISO()
84     {
85         $temp = new Date("2003-10-04 14:03:24");
86         $this->assertEquals(
87             '2003-10-04 14:03:24',
88             sprintf(
89                 '%04d-%02d-%02d %02d:%02d:%02d',
90                 $temp->year,
91                 $temp->month,
92                 $temp->day,
93                 $temp->hour,
94                 $temp->minute,
95                 $temp->second
96             )
97         );
98     }
99
100     public function testDateISOBasic()
101     {
102         $temp = new Date("20031004T140324");
103         $this->assertEquals(
104             '2003-10-04 14:03:24',
105             sprintf(
106                 '%04d-%02d-%02d %02d:%02d:%02d',
107                 $temp->year,
108                 $temp->month,
109                 $temp->day,
110                 $temp->hour,
111                 $temp->minute,
112                 $temp->second
113             )
114         );
115     }
116
117     public function testDateISOExtended()
118     {
119         $temp = new Date("2003-10-04T14:03:24");
120         $this->assertEquals(
121             '2003-10-04 14:03:24',
122             sprintf(
123                 '%04d-%02d-%02d %02d:%02d:%02d',
124                 $temp->year,
125                 $temp->month,
126                 $temp->day,
127                 $temp->hour,
128                 $temp->minute,
129                 $temp->second
130             )
131         );
132     }
133
134     public function testDateISOTimestamp()
135     {
136         $temp = new Date("20031004140324");
137         $this->assertEquals(
138             '2003-10-04 14:03:24',
139             sprintf(
140                 '%04d-%02d-%02d %02d:%02d:%02d',
141                 $temp->year,
142                 $temp->month,
143                 $temp->day,
144                 $temp->hour,
145                 $temp->minute,
146                 $temp->second
147             )
148         );
149     }
150
151     public function testDateUnixtime()
152     {
153         $temp = new Date();
154         $temp->setTZbyID("UTC");
155         $temp->setDate(strtotime("2003-10-04 14:03:24Z"));
156         $this->assertEquals(
157             '2003-10-04 14:03:24',
158             sprintf(
159                 '%04d-%02d-%02d %02d:%02d:%02d',
160                 $temp->year,
161                 $temp->month,
162                 $temp->day,
163                 $temp->hour,
164                 $temp->minute,
165                 $temp->second
166             )
167         );
168     }
169
170     public function testDateUnixtime2()
171     {
172         $temp = new Date();
173         $temp->setTZbyID("UTC-05:30");
174         $temp->setDate(strtotime("2003-10-04 14:03:24Z"));
175         $temp->convertTZbyID("UTC");
176         $this->assertEquals(
177             '2003-10-04 14:03:24',
178             sprintf(
179                 '%04d-%02d-%02d %02d:%02d:%02d',
180                 $temp->year,
181                 $temp->month,
182                 $temp->day,
183                 $temp->hour,
184                 $temp->minute,
185                 $temp->second
186             )
187         );
188     }
189
190     public function testDateUnixtime3()
191     {
192         $temp = new Date();
193         $temp->setTZbyID("America/Chicago");
194         $temp->setDate(strtotime("2003-10-04 14:03:24Z"));
195         $temp->convertTZbyID("UTC");
196         $this->assertEquals(
197             '2003-10-04 14:03:24',
198             sprintf(
199                 '%04d-%02d-%02d %02d:%02d:%02d',
200                 $temp->year,
201                 $temp->month,
202                 $temp->day,
203                 $temp->hour,
204                 $temp->minute,
205                 $temp->second
206             )
207         );
208     }
209
210     public function testDateUnixtime4()
211     {
212         $temp = new Date();
213         $temp->setTZbyID("Europe/London");
214         $temp->setDate(strtotime("2003-10-04 14:03:24Z")); // Summer time in London
215         $temp->setTZbyID("UTC");
216         $this->assertEquals(
217             '2003-10-04 15:03:24', // Preserves London local time (15.03)
218             sprintf(
219                 '%04d-%02d-%02d %02d:%02d:%02d',
220                 $temp->year,
221                 $temp->month,
222                 $temp->day,
223                 $temp->hour,
224                 $temp->minute,
225                 $temp->second
226             )
227         );
228     }
229
230     public function testSetDateISO()
231     {
232         $this->time->setDate("2003-10-04 14:03:24");
233         $this->assertEquals(
234             '2003-10-04 14:03:24',
235             sprintf(
236                 '%04d-%02d-%02d %02d:%02d:%02d',
237                 $this->time->year,
238                 $this->time->month,
239                 $this->time->day,
240                 $this->time->hour,
241                 $this->time->minute,
242                 $this->time->second
243             )
244         );
245     }
246
247     public function testSetDateISOBasic()
248     {
249         $this->time->setDate("20031004T140324");
250         $this->assertEquals(
251             '2003-10-04 14:03:24',
252             sprintf(
253                 '%04d-%02d-%02d %02d:%02d:%02d',
254                 $this->time->year,
255                 $this->time->month,
256                 $this->time->day,
257                 $this->time->hour,
258                 $this->time->minute,
259                 $this->time->second
260             )
261         );
262     }
263
264     public function testSetDateISOExtended()
265     {
266         $this->time->setDate("2003-10-04T14:03:24");
267         $this->assertEquals(
268             '2003-10-04 14:03:24',
269             sprintf(
270                 '%04d-%02d-%02d %02d:%02d:%02d',
271                 $this->time->year,
272                 $this->time->month,
273                 $this->time->day,
274                 $this->time->hour,
275                 $this->time->minute,
276                 $this->time->second
277             )
278         );
279     }
280
281     public function testSetDateTimestamp()
282     {
283         $this->time->setDate("20031004140324");
284         $this->assertEquals(
285             '2003-10-04 14:03:24',
286             sprintf(
287                 '%04d-%02d-%02d %02d:%02d:%02d',
288                 $this->time->year,
289                 $this->time->month,
290                 $this->time->day,
291                 $this->time->hour,
292                 $this->time->minute,
293                 $this->time->second
294             )
295         );
296     }
297
298     public function testSetDateUnixtime()
299     {
300         $this->time->setDate(strtotime("2003-10-04 14:03:24Z"));
301         $this->assertEquals(
302             '2003-10-04 14:03:24',
303             sprintf(
304                 '%04d-%02d-%02d %02d:%02d:%02d',
305                 $this->time->year,
306                 $this->time->month,
307                 $this->time->day,
308                 $this->time->hour,
309                 $this->time->minute,
310                 $this->time->second
311             )
312         );
313     }
314
315     public function testSetDateUnixtime2()
316     {
317         $hs_oldtz = $this->time->getTZID();
318         $this->time->setTZbyID("UTC-05:30");
319         $this->time->setDate(strtotime("2003-10-04 14:03:24Z"));
320         $this->time->convertTZbyID($hs_oldtz);
321         $this->assertEquals(
322             '2003-10-04 14:03:24',
323             sprintf(
324                 '%04d-%02d-%02d %02d:%02d:%02d',
325                 $this->time->year,
326                 $this->time->month,
327                 $this->time->day,
328                 $this->time->hour,
329                 $this->time->minute,
330                 $this->time->second
331             )
332         );
333     }
334
335     public function testSetDateUnixtime3()
336     {
337         $hs_oldtz = $this->time->getTZID();
338         $this->time->setTZbyID("America/Chicago");
339         $this->time->setDate(strtotime("2003-10-04 14:03:24Z"));
340         $this->time->convertTZbyID($hs_oldtz);
341         $this->assertEquals(
342             '2003-10-04 14:03:24',
343             sprintf(
344                 '%04d-%02d-%02d %02d:%02d:%02d',
345                 $this->time->year,
346                 $this->time->month,
347                 $this->time->day,
348                 $this->time->hour,
349                 $this->time->minute,
350                 $this->time->second
351             )
352         );
353     }
354
355     public function testGetDateISO()
356     {
357         $date = $this->time->getDate(DATE_FORMAT_ISO);
358         $this->assertEquals('2003-10-04 14:03:24', $date);
359     }
360
361     public function testGetDateISOBasic()
362     {
363         $date = $this->time->getDate(DATE_FORMAT_ISO_BASIC);
364         $this->assertEquals('20031004T140324Z', $date);
365     }
366
367     public function testGetDateISOExtended()
368     {
369         $date = $this->time->getDate(DATE_FORMAT_ISO_EXTENDED);
370         $this->assertEquals('2003-10-04T14:03:24Z', $date);
371     }
372
373     public function testGetDateTimestamp()
374     {
375         $date = $this->time->getDate(DATE_FORMAT_TIMESTAMP);
376         $this->assertEquals('20031004140324', $date);
377     }
378
379     public function testGetDateUnixtime()
380     {
381         $date = $this->time->getDate(DATE_FORMAT_UNIXTIME);
382         $this->assertEquals(strtotime('2003-10-04 14:03:24Z'), $date);
383     }
384
385     public function testGetDateUnixtime2()
386     {
387         $hs_oldtz = $this->time->getTZID();
388         $this->time->convertTZbyID("UTC-05:30");
389         $date = $this->time->getDate(DATE_FORMAT_UNIXTIME);
390         $this->assertEquals(strtotime('2003-10-04 14:03:24Z'), $date);
391         $this->time->convertTZbyID($hs_oldtz);
392     }
393
394     public function testGetDateUnixtime3()
395     {
396         $hs_oldtz = $this->time->getTZID();
397         $this->time->convertTZbyID("America/Chicago");
398         $date = $this->time->getDate(DATE_FORMAT_UNIXTIME);
399         $this->assertEquals(strtotime('2003-10-04 14:03:24Z'), $date);
400         $this->time->convertTZbyID($hs_oldtz);
401     }
402
403     public function testFormatLikeStrftime()
404     {
405         $codes = array(
406             'a' => 'Sat',
407             'A' => 'Saturday',
408             'b' => 'Oct',
409             'B' => 'October',
410             'C' => '20',
411             'd' => '04',
412             'D' => '10/04/2003',
413             'e' => '4',
414             'H' => '14',
415             'I' => '02',
416             'j' => '277',
417             'm' => '10',
418             'M' => '03',
419             'n' => "\n",
420             'O' => '+00:00',
421             'o' => '+00:00',
422             'p' => 'pm',
423             'P' => 'PM',
424             'r' => '02:03:24 PM',
425             'R' => '14:03',
426             'S' => '24',
427             't' => "\t",
428             'T' => '14:03:24',
429             'w' => '6',
430             'U' => '39',
431             'y' => '03',
432             'Y' => '2003',
433             '%' => '%'
434         );
435
436         foreach ($codes as $code => $expected) {
437             $this->assertEquals(
438                 "$code: $expected",
439                 $this->time->formatLikeStrftime("$code: %$code")
440             );
441         }
442     }
443
444     public function testToUTCbyOffset()
445     {
446         $this->time->setTZbyID('EST');
447         $this->time->toUTC();
448         $temp = new Date("2003-10-04 14:03:24");
449         $temp->toUTCbyOffset("-05:00");
450
451         $this->assertEquals($temp, $this->time);
452     }
453 }