Fix unit tests for DATEDIF()

Leap years where not properly taken into consideration
This commit is contained in:
Adrien Crivelli 2016-10-02 14:46:11 +09:00
parent 8061f7516e
commit 8c4c11346b
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
3 changed files with 45 additions and 7 deletions

View File

@ -729,13 +729,24 @@ class DateTime
case 'YD':
$retVal = intval($difference);
if ($endYears > $startYears) {
while ($endYears > $startYears) {
$isLeapStartYear = $PHPStartDateObject->format('L');
$wasLeapEndYear = $PHPEndDateObject->format('L');
// Adjust end year to be as close as possible as start year
while ($PHPEndDateObject >= $PHPStartDateObject) {
$PHPEndDateObject->modify('-1 year');
$endYears = $PHPEndDateObject->format('Y');
}
$retVal = $PHPEndDateObject->format('z') - $PHPStartDateObject->format('z');
if ($retVal < 0) {
$retVal += 365;
$PHPEndDateObject->modify('+1 year');
// Get the result
$retVal = $PHPEndDateObject->diff($PHPStartDateObject)->days;
// Adjust for leap years cases
$isLeapEndYear = $PHPEndDateObject->format('L');
$limit = new \DateTime($PHPEndDateObject->format('Y-02-29'));
if (!$isLeapStartYear && !$wasLeapEndYear && $isLeapEndYear && $PHPEndDateObject >= $limit ) {
$retVal--;
}
}
break;

View File

@ -416,12 +416,9 @@ class DateTimeTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider providerDATEDIF
* @group fail19
*/
public function testDATEDIF()
{
$this->markTestIncomplete('TODO: This test should be fixed');
$args = func_get_args();
$expectedResult = array_pop($args);
$result = call_user_func_array([DateTime::class, 'DATEDIF'], $args);

View File

@ -1,6 +1,36 @@
<?php
return [
[
'2016-01-01',
'2016-12-31',
'YD',
365
],
[
'2015-01-01',
'2015-12-31',
'YD',
364
],
[
'2015-01-01',
'2016-12-31',
'YD',
364
],
[
'2016-01-01',
'2017-12-31',
'YD',
365
],
[
'2017-01-01',
'2018-12-31',
'YD',
364
],
[
'ABC',
'2007-1-10',