2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5 * Tests for the Date_Calc class
7 * Any individual tests that fail will have their name, expected result
8 * and actual result printed out. So seeing no output when executing
9 * this file is a good thing.
11 * Can be run via CLI or a web server.
13 * This test senses whether it is from an installation of PEAR::Date or if
14 * it's from CVS or a .tar file. If it's an installed version, use the
15 * installed version of Date_Calc. Otherwise, use the local development
18 * PHP versions 4 and 5
22 * Copyright (c) 1997-2005 Daniel Convissor <danielc@php.net>
23 * All rights reserved.
25 * This source file is subject to the New BSD license that is bundled
26 * with this package in the file LICENSE.txt.
27 * It is also available through the world-wide-web at this URL:
28 * http://www.opensource.org/licenses/bsd-license.php
29 * If you did not receive a copy of the license and are unable to
30 * obtain it through the world-wide-web, please send an email
31 * to pear-dev@lists.php.net so we can send you a copy immediately.
33 * @category Date and Time
35 * @author Daniel Convissor <danielc@php.net>
36 * @copyright Copyright (c) 1997-2005 Daniel Convissor <danielc@php.net>
37 * @license http://www.opensource.org/licenses/bsd-license.php
40 * @link http://pear.php.net/package/Date
41 * @since File available since Release 1.5
44 if ('@include_path@' != '@' . 'include_path' . '@') {
47 ini_get('include_path')
48 . PATH_SEPARATOR . '.'
53 realpath(dirname(__FILE__) . '/../')
54 . PATH_SEPARATOR . '.' . PATH_SEPARATOR
55 . ini_get('include_path')
60 * Get the needed class
62 require_once 'Date/Calc.php';
65 * Compare the test result to the expected result
67 * If the test fails, echo out the results.
69 * @param mixed $expect the scalar or array you expect from the test
70 * @param mixed $actual the scalar or array results from the test
71 * @param string $test_name the name of the test
75 function compare($expect, $actual, $test_name)
77 if (is_array($expect)) {
78 if (count(array_diff($actual, $expect))) {
79 echo "$test_name failed. Expect:\n";
85 if ($expect != $actual) {
86 echo "$test_name failed. Expect: $expect. Actual: $actual\n";
91 if (php_sapi_name() != 'cli') {
96 compare('20001122', Date_Calc::dateFormat(22, 11, 2000, '%Y%m%d'), 'dateFormat');
97 compare('20001122', Date_Calc::dateFormat('22', '11', '2000', '%Y%m%d'), 'dateFormat str');
99 compare('2001', Date_Calc::defaultCentury('1'), 'defaultCentury 1 str');
100 compare('2001', Date_Calc::defaultCentury(1), 'defaultCentury 1');
101 compare('1960', Date_Calc::defaultCentury(60), 'defaultCentury 2');
102 compare('2010', Date_Calc::defaultCentury(10), 'defaultCentury 3');
104 compare(2451871, Date_Calc::dateToDays('22', '11', '2000'), 'dateToDays str');
105 compare(2451871, Date_Calc::dateToDays(22, 11, 2000), 'dateToDays');
106 compare('20001122', Date_Calc::daysToDate(2451871), 'daysToDate');
108 compare('2000-47-3', Date_Calc::gregorianToISO('22', '11', '2000'), 'gregorianToISO str');
109 compare('2000-47-3', Date_Calc::gregorianToISO(22, 11, 2000), 'gregorianToISO');
110 compare(2451716.56767, Date_Calc::dateSeason('SUMMERSOLSTICE', 2000), 'dateSeason');
112 compare(date('Ymd'), Date_Calc::dateNow(), 'dateNow');
113 compare(date('Y'), Date_Calc::getYear(), 'getYear');
114 compare(date('m'), Date_Calc::getMonth(), 'getMonth');
115 compare(date('d'), Date_Calc::getDay(), 'getDay');
117 compare(327, Date_Calc::dayOfYear(22, 11, 2000), 'dayOfYear');
118 compare('November', Date_Calc::getMonthFullname(11), 'getMonthFullname');
119 compare('Nov', Date_Calc::getMonthAbbrname(11), 'getMonthAbbrname');
120 compare('Saturday', Date_Calc::getWeekdayFullname(1, 1, 2005), 'getWeekdayFullname');
121 compare('Sat', Date_Calc::getWeekdayAbbrname(1, 1, 2005), 'getWeekdayAbbrname');
122 compare(11, Date_Calc::getMonthFromFullName('November'), 'getMonthFromFullName');
124 compare(327, Date_Calc::dayOfYear('22', '11', '2000'), 'dayOfYear str');
125 compare('November', Date_Calc::getMonthFullname('11'), 'getMonthFullname str');
126 compare('Nov', Date_Calc::getMonthAbbrname('11'), 'getMonthAbbrname str');
127 compare('Saturday', Date_Calc::getWeekdayFullname('01', '01', '2005'), 'getWeekdayFullname str');
128 compare('Sat', Date_Calc::getWeekdayAbbrname('01', '01', '2005'), 'getWeekdayAbbrname str');
144 compare($exp, Date_Calc::getMonthNames(), 'getMonthNames');
155 compare($exp, Date_Calc::getWeekDays(), 'getWeekDays');
157 compare(3, Date_Calc::dayOfWeek(22, 11, 2000), 'dayOfWeek');
158 compare(47, Date_Calc::weekOfYear(22, 11, 2000), 'weekOfYear');
159 compare(4, Date_Calc::quarterOfYear(22, 11, 2000), 'quarterOfYear');
161 compare(3, Date_Calc::dayOfWeek('22', '11', '2000'), 'dayOfWeek str');
162 compare(47, Date_Calc::weekOfYear('22', '11', '2000'), 'weekOfYear str');
163 compare(4, Date_Calc::quarterOfYear('22', '11', '2000'), 'quarterOfYear str');
165 compare(28, Date_Calc::daysInMonth(2, 1900), 'daysInMonth 1');
166 compare(29, Date_Calc::daysInMonth(2, 1996), 'daysInMonth 2');
167 compare(29, Date_Calc::daysInMonth(2, 2000), 'daysInMonth 3');
168 compare(28, Date_Calc::daysInMonth(2, 2001), 'daysInMonth 4');
169 compare(30, Date_Calc::daysInMonth(11, 2000), 'daysInMonth 5');
171 compare(28, Date_Calc::daysInMonth('02', 1900), 'daysInMonth 1 str');
172 compare(29, Date_Calc::daysInMonth('02', 1996), 'daysInMonth 2 str');
173 compare(29, Date_Calc::daysInMonth('02', 2000), 'daysInMonth 3 str');
174 compare(28, Date_Calc::daysInMonth('02', 2001), 'daysInMonth 4 str');
175 compare(30, Date_Calc::daysInMonth('11', '2000'), 'daysInMonth 5 str');
177 compare(5, Date_Calc::weeksInMonth(11, 2000), 'weeksInMonth');
178 compare(5, Date_Calc::weeksInMonth('11', '2000'), 'weeksInMonth str');
190 compare($exp, Date_Calc::getCalendarWeek(27, 2, 1900), 'getCalendarWeek 1');
201 compare($exp, Date_Calc::getCalendarWeek(28, 2, 2000), 'getCalendarWeek 2');
212 compare($exp, Date_Calc::getCalendarWeek(27, 11, 2000), 'getCalendarWeek 3');
213 compare($exp, Date_Calc::getCalendarWeek('27', '11', '2000'), 'getCalendarWeek 3 str');
261 compare($exp, Date_Calc::getCalendarMonth(11, 2000), 'getCalendarMonth');
262 compare($exp, Date_Calc::getCalendarMonth('11', '2000'), 'getCalendarMonth str');
264 // I don't feel like dealing with this right now...
265 //compare('', Date_Calc::getCalendarYear(2000), 'getCalendarYear');
267 compare('20001121', Date_Calc::prevDay(22, 11, 2000), 'prevDay');
268 compare('20001123', Date_Calc::nextDay(22, 11, 2000), 'nextDay');
269 compare('20001121', Date_Calc::prevDay(22, 11, 2000), 'prevDay str');
270 compare('20001123', Date_Calc::nextDay('22', '11', '2000'), 'nextDay str');
272 compare('20001117', Date_Calc::prevWeekday('19', '11', '2000'), 'prevWeekday 1 str');
273 compare('20001117', Date_Calc::prevWeekday(19, 11, 2000), 'prevWeekday 1');
274 compare('20001121', Date_Calc::prevWeekday(22, 11, 2000), 'prevWeekday 2');
275 compare('20001123', Date_Calc::nextWeekday(22, 11, 2000), 'nextWeekday 1');
276 compare('20001127', Date_Calc::nextWeekday(24, 11, 2000), 'nextWeekday 2');
277 compare('20001127', Date_Calc::nextWeekday('24', '11', '2000'), 'nextWeekday 2 str');
279 compare('20001121', Date_Calc::prevDayOfWeek('2', '22', '11', '2000'), 'prevDayOfWeek 1 str');
280 compare('20001121', Date_Calc::prevDayOfWeek(2, 22, 11, 2000), 'prevDayOfWeek 1');
281 compare('20001115', Date_Calc::prevDayOfWeek(3, 22, 11, 2000), 'prevDayOfWeek 2');
282 compare('20001122', Date_Calc::prevDayOfWeek(3, 22, 11, 2000, '%Y%m%d', true), 'prevDayOfWeek 3');
283 compare('20001122', Date_Calc::nextDayOfWeek(3, 22, 11, 2000, '%Y%m%d', true), 'nextDayOfWeek 1');
284 compare('20001129', Date_Calc::nextDayOfWeek(3, 22, 11, 2000), 'nextDayOfWeek 2');
285 compare('20001123', Date_Calc::nextDayOfWeek(4, 22, 11, 2000), 'nextDayOfWeek 3');
286 compare('20001123', Date_Calc::nextDayOfWeek('4', '22', '11', '2000'), 'nextDayOfWeek 3 str');
288 compare('20001121', Date_Calc::prevDayOfWeekOnOrBefore('2', '22', '11', '2000'), 'prevDayOfWeekOnOrBefore 1 str');
289 compare('20001121', Date_Calc::prevDayOfWeekOnOrBefore(2, 22, 11, 2000), 'prevDayOfWeekOnOrBefore 1');
290 compare('20001122', Date_Calc::prevDayOfWeekOnOrBefore(3, 22, 11, 2000), 'prevDayOfWeekOnOrBefore 2');
291 compare('20001122', Date_Calc::nextDayOfWeekOnOrAfter(3, 22, 11, 2000), 'nextDayOfWeekOnOrAfter 1');
292 compare('20001123', Date_Calc::nextDayOfWeekOnOrAfter(4, 22, 11, 2000), 'nextDayOfWeekOnOrAfter 2');
293 compare('20001123', Date_Calc::nextDayOfWeekOnOrAfter('4', '22', '11', '2000'), 'nextDayOfWeekOnOrAfter 2 str');
295 compare('20001120', Date_Calc::beginOfWeek('22', '11', '2000'), 'beginOfWeek str');
296 compare('20001120', Date_Calc::beginOfWeek(22, 11, 2000), 'beginOfWeek');
297 compare('20001126', Date_Calc::endOfWeek(22, 11, 2000), 'endOfWeek');
298 compare('20001126', Date_Calc::endOfWeek('22', '11', '2000'), 'endOfWeek str');
300 compare('20001113', Date_Calc::beginOfPrevWeek(22, 11, 2000), 'beginOfPrevWeek');
301 compare('20001127', Date_Calc::beginOfNextWeek(22, 11, 2000), 'beginOfNextWeek');
302 compare('20001113', Date_Calc::beginOfPrevWeek('22', '11', '2000'), 'beginOfPrevWeek str');
303 compare('20001127', Date_Calc::beginOfNextWeek('22', '11', '2000'), 'beginOfNextWeek str');
305 compare('20001101', Date_Calc::beginOfMonth(11, 2000), 'beginOfMonth');
306 compare('20001101', Date_Calc::beginOfMonth('11', '2000'), 'beginOfMonth str');
308 compare('20001001', Date_Calc::beginOfPrevMonth(22, 11, 2000), 'beginOfPrevMonth');
309 compare('20001031', Date_Calc::endOfPrevMonth(22, 11, 2000), 'endOfPrevMonth');
310 compare('20001001', Date_Calc::beginOfPrevMonth('22', '11', '2000'), 'beginOfPrevMonth str');
311 compare('20001031', Date_Calc::endOfPrevMonth('22', '11', '2000'), 'endOfPrevMonth str');
313 compare('20001201', Date_Calc::beginOfNextMonth(22, 11, 2000), 'beginOfNextMonth');
314 compare('20001231', Date_Calc::endOfNextMonth(22, 11, 2000), 'endOfNextMonth');
315 compare('20001201', Date_Calc::beginOfNextMonth('22', '11', '2000'), 'beginOfNextMonth str');
316 compare('20001231', Date_Calc::endOfNextMonth('22', '11', '2000'), 'endOfNextMonth str');
318 compare('19991001', Date_Calc::beginOfMonthBySpan(-13, 11, 2000), 'beginOfMonthBySpan 1');
319 compare('20001001', Date_Calc::beginOfMonthBySpan(-1, 11, 2000), 'beginOfMonthBySpan 2');
320 compare('20001101', Date_Calc::beginOfMonthBySpan(0, 11, 2000), 'beginOfMonthBySpan 3');
321 compare('20001201', Date_Calc::beginOfMonthBySpan(1, 11, 2000), 'beginOfMonthBySpan 4');
322 compare('20011201', Date_Calc::beginOfMonthBySpan(13, 11, 2000), 'beginOfMonthBySpan 5');
324 compare('19990101', Date_Calc::beginOfMonthBySpan('-13', '02', '2000'), 'beginOfMonthBySpan 6 str');
325 compare('19990101', Date_Calc::beginOfMonthBySpan(-13, 2, 2000), 'beginOfMonthBySpan 6');
326 compare('20000101', Date_Calc::beginOfMonthBySpan(-1, 2, 2000), 'beginOfMonthBySpan 7');
327 compare('20000201', Date_Calc::beginOfMonthBySpan(0, 2, 2000), 'beginOfMonthBySpan 8');
328 compare('20000301', Date_Calc::beginOfMonthBySpan(1, 2, 2000), 'beginOfMonthBySpan 9');
329 compare('20010301', Date_Calc::beginOfMonthBySpan(13, 2, 2000), 'beginOfMonthBySpan 10');
330 compare('20010301', Date_Calc::beginOfMonthBySpan('13', '02', '2000'), 'beginOfMonthBySpan 10 str');
332 compare('19991031', Date_Calc::endOfMonthBySpan(-13, 11, 2000), 'endOfMonthBySpan 1');
333 compare('20001031', Date_Calc::endOfMonthBySpan(-1, 11, 2000), 'endOfMonthBySpan 2');
334 compare('20001130', Date_Calc::endOfMonthBySpan(0, 11, 2000), 'endOfMonthBySpan 3');
335 compare('20001231', Date_Calc::endOfMonthBySpan(1, 11, 2000), 'endOfMonthBySpan 4');
336 compare('20011231', Date_Calc::endOfMonthBySpan(13, 11, 2000), 'endOfMonthBySpan 5');
338 compare('19990131', Date_Calc::endOfMonthBySpan('-13', '02', '2000'), 'endOfMonthBySpan 6 str');
339 compare('19990131', Date_Calc::endOfMonthBySpan(-13, 2, 2000), 'endOfMonthBySpan 6');
340 compare('20000131', Date_Calc::endOfMonthBySpan(-1, 2, 2000), 'endOfMonthBySpan 7');
341 compare('20000229', Date_Calc::endOfMonthBySpan(0, 2, 2000), 'endOfMonthBySpan 8');
342 compare('20000331', Date_Calc::endOfMonthBySpan(1, 2, 2000), 'endOfMonthBySpan 9');
343 compare('20010331', Date_Calc::endOfMonthBySpan(13, 2, 2000), 'endOfMonthBySpan 10');
344 compare('20010331', Date_Calc::endOfMonthBySpan('13', '02', '2000'), 'endOfMonthBySpan 10 str');
346 compare(3, Date_Calc::firstOfMonthWeekday(11, 2000), 'firstOfMonthWeekday');
347 compare(3, Date_Calc::firstOfMonthWeekday('11', '2000'), 'firstOfMonthWeekday str');
349 compare('20050101', Date_Calc::NWeekdayOfMonth(1, 6, 1, 2005), 'NWeekdayOfMonth 161');
350 compare('20050102', Date_Calc::NWeekdayOfMonth(1, 0, 1, 2005), 'NWeekdayOfMonth 101');
351 compare('20050103', Date_Calc::NWeekdayOfMonth(1, 1, 1, 2005), 'NWeekdayOfMonth 111');
352 compare('20050104', Date_Calc::NWeekdayOfMonth(1, 2, 1, 2005), 'NWeekdayOfMonth 121');
353 compare('20050105', Date_Calc::NWeekdayOfMonth(1, 3, 1, 2005), 'NWeekdayOfMonth 131');
354 compare('20050106', Date_Calc::NWeekdayOfMonth(1, 4, 1, 2005), 'NWeekdayOfMonth 141');
355 compare('20050107', Date_Calc::NWeekdayOfMonth(1, 5, 1, 2005), 'NWeekdayOfMonth 151');
357 compare('20050108', Date_Calc::NWeekdayOfMonth('2', '6', '01', '2005'), 'NWeekdayOfMonth 261');
358 compare('20050109', Date_Calc::NWeekdayOfMonth('2', '0', '01', '2005'), 'NWeekdayOfMonth 201');
359 compare('20050110', Date_Calc::NWeekdayOfMonth('2', '1', '01', '2005'), 'NWeekdayOfMonth 211');
360 compare('20050111', Date_Calc::NWeekdayOfMonth('2', '2', '01', '2005'), 'NWeekdayOfMonth 221');
361 compare('20050112', Date_Calc::NWeekdayOfMonth('2', '3', '01', '2005'), 'NWeekdayOfMonth 231');
362 compare('20050113', Date_Calc::NWeekdayOfMonth('2', '4', '01', '2005'), 'NWeekdayOfMonth 241');
363 compare('20050114', Date_Calc::NWeekdayOfMonth('2', '5', '01', '2005'), 'NWeekdayOfMonth 251');
365 compare('20050131', Date_Calc::NWeekdayOfMonth('last', 1, 1, 2005), 'NWeekdayOfMonth l11');
366 compare('20050130', Date_Calc::NWeekdayOfMonth('last', 0, 1, 2005), 'NWeekdayOfMonth l01');
367 compare('20050129', Date_Calc::NWeekdayOfMonth('last', 6, 1, 2005), 'NWeekdayOfMonth l61');
368 compare('20050128', Date_Calc::NWeekdayOfMonth('last', 5, 1, 2005), 'NWeekdayOfMonth l51');
369 compare('20050127', Date_Calc::NWeekdayOfMonth('last', 4, 1, 2005), 'NWeekdayOfMonth l41');
370 compare('20050126', Date_Calc::NWeekdayOfMonth('last', 3, 1, 2005), 'NWeekdayOfMonth l31');
371 compare('20050125', Date_Calc::NWeekdayOfMonth('last', 2, 1, 2005), 'NWeekdayOfMonth l21');
373 compare('20050331', Date_Calc::NWeekdayOfMonth('last', 4, 3, 2005), 'NWeekdayOfMonth l43');
374 compare('20050330', Date_Calc::NWeekdayOfMonth('last', 3, 3, 2005), 'NWeekdayOfMonth l33');
375 compare('20050329', Date_Calc::NWeekdayOfMonth('last', 2, 3, 2005), 'NWeekdayOfMonth l23');
376 compare('20050328', Date_Calc::NWeekdayOfMonth('last', 1, 3, 2005), 'NWeekdayOfMonth l13');
377 compare('20050327', Date_Calc::NWeekdayOfMonth('last', 0, 3, 2005), 'NWeekdayOfMonth l03');
378 compare('20050326', Date_Calc::NWeekdayOfMonth('last', 6, 3, 2005), 'NWeekdayOfMonth l63');
379 compare('20050325', Date_Calc::NWeekdayOfMonth('last', 5, 3, 2005), 'NWeekdayOfMonth l53');
382 compare(false, Date_Calc::isValidDate(29, 2, 1900), 'isValidDate 1');
383 compare(true, Date_Calc::isValidDate(29, 2, 2000), 'isValidDate 2');
384 compare(true, Date_Calc::isValidDate('29', '02', '2000'), 'isValidDate 2 str');
386 compare(false, Date_Calc::isLeapYear(1900), 'isLeapYear 1');
387 compare(true, Date_Calc::isLeapYear(1996), 'isLeapYear 2');
388 compare(true, Date_Calc::isLeapYear(2000), 'isLeapYear 3');
389 compare(false, Date_Calc::isLeapYear(2001), 'isLeapYear 4');
390 compare(false, Date_Calc::isLeapYear('2001'), 'isLeapYear 4 str');
392 compare(false, Date_Calc::isFutureDate('22', '11', '2000'), 'isFutureDate 1 str');
393 compare(false, Date_Calc::isFutureDate(22, 11, 2000), 'isFutureDate 1');
394 compare(true, Date_Calc::isFutureDate(22, 11, date('Y') + 1), 'isFutureDate 2');
396 compare(false, Date_Calc::isPastDate(22, 11, date('Y') + 1), 'isPastDate 1');
397 compare(true, Date_Calc::isPastDate(22, 11, 2000), 'isPastDate 2');
398 compare(true, Date_Calc::isPastDate('22', '11', '2000'), 'isPastDate 2 str');
400 compare(10, Date_Calc::dateDiff(22, 11, 2000, 12, 11, 2000), 'dateDiff 1');
401 compare(10, Date_Calc::dateDiff(12, 11, 2000, 22, 11, 2000), 'dateDiff 2');
402 compare(61, Date_Calc::dateDiff(22, 11, 2000, 22, 1, 2001), 'dateDiff 3');
403 compare(61, Date_Calc::dateDiff('22', '11', '2000', '22', '01', '2001'), 'dateDiff 3 str');
405 compare(-1, Date_Calc::compareDates(12, 11, 2000, 22, 11, 2000), 'compareDates 1');
406 compare(0, Date_Calc::compareDates(22, 11, 2000, 22, 11, 2000), 'compareDates 2');
407 compare(1, Date_Calc::compareDates(22, 11, 2000, 12, 11, 2000), 'compareDates 3');
408 compare(1, Date_Calc::compareDates('22', '11', '2000', '12', '11', '2000'), 'compareDates 3 str');