Adjustments to DateTime tests

This commit is contained in:
MarkBaker 2016-08-13 20:21:45 +01:00
parent fd2df82faf
commit e149c1f191
7 changed files with 51 additions and 53 deletions

View File

@ -89,6 +89,7 @@ $aTests = array(
, '33chartcreate-line.php'
, '33chartcreate-pie.php'
, '33chartcreate-radar.php'
, '33chartcreate-scatter.php'
, '33chartcreate-stock.php'
, '33chartcreate-multiple-charts.php'
, '33chartcreate-composite.php'

View File

@ -167,19 +167,25 @@ class Date
public static function excelToDateTimeObject($excelTimestamp = 0, $timeZone = null)
{
$timeZone = ($timeZone === null) ? self::getDefaultTimezone() : self::validateTimeZone($timeZone);
if ($excelTimestamp < 1.0) {
// Unix timestamp base date
$baseDate = new \DateTime('1970-01-01', $timeZone);
} else {
// MS Excel calendar base dates
if (self::$excelCalendar == self::CALENDAR_WINDOWS_1900) {
// Allow adjustment for 1900 Leap Year in MS Excel
$baseDate = ($excelTimestamp < 60) ? new \DateTime('1899-12-31', $timeZone) : new \DateTime('1899-12-30', $timeZone);
} else {
$baseDate = new \DateTime('1904-01-01', $timeZone);
}
}
$days = floor($excelTimestamp);
$partDay = $excelTimestamp - $days;
$hours = floor($partDay * 24);
$partDay = $partDay * 24 - $hours;
$minutes = floor($partDay * 60);
$partDay = $partDay * 60 - $minutes;
$seconds = floor($partDay * 60);
// $fraction = $partDay - $seconds;
$seconds = round($partDay * 60);
$interval = '+' . $days . ' days';
return $baseDate->modify($interval)
@ -195,7 +201,7 @@ class Date
*/
public static function excelToTimestamp($excelTimestamp = 0, $timeZone = null)
{
return self::excelToDateTimeObject($excelTimestamp, $timeZone)
return (int) self::excelToDateTimeObject($excelTimestamp, $timeZone)
->format('U');
}

View File

@ -27,9 +27,9 @@ class DateTest extends \PHPUnit_Framework_TestCase
}
/**
* @dataProvider providerDateTimeExcelToPHP1900
* @dataProvider providerDateTimeExcelToTimestamp1900
*/
public function testDateTimeExcelToPHP1900()
public function testDateTimeExcelToTimestamp1900()
{
$result = call_user_func(
array('\PHPExcel\Shared\Date','setExcelCalendar'),
@ -38,16 +38,13 @@ class DateTest extends \PHPUnit_Framework_TestCase
$args = func_get_args();
$expectedResult = array_pop($args);
if ($args[0] < 1) {
$expectedResult += gmmktime(0, 0, 0);
}
$result = call_user_func_array(array('\PHPExcel\Shared\Date', 'ExcelToPHP'), $args);
$result = call_user_func_array(array('\PHPExcel\Shared\Date', 'excelToTimestamp'), $args);
$this->assertEquals($expectedResult, $result);
}
public function providerDateTimeExcelToPHP1900()
public function providerDateTimeExcelToTimestamp1900()
{
return new \PhpSpreadhsheet\unitTests\TestDataFileIterator('rawTestData/Shared/DateTimeExcelToPHP1900.data');
return new \PhpSpreadhsheet\unitTests\TestDataFileIterator('rawTestData/Shared/DateTimeExcelToTimestamp1900.data');
}
/**
@ -93,9 +90,9 @@ class DateTest extends \PHPUnit_Framework_TestCase
}
/**
* @dataProvider providerDateTimeExcelToPHP1904
* @dataProvider providerDateTimeExcelToTimestamp1904
*/
public function testDateTimeExcelToPHP1904()
public function testDateTimeExcelToTimestamp1904()
{
$result = call_user_func(
array('\PHPExcel\Shared\Date','setExcelCalendar'),
@ -104,16 +101,13 @@ class DateTest extends \PHPUnit_Framework_TestCase
$args = func_get_args();
$expectedResult = array_pop($args);
if ($args[0] < 1) {
$expectedResult += gmmktime(0, 0, 0);
}
$result = call_user_func_array(array('\PHPExcel\Shared\Date','ExcelToPHP'), $args);
$result = call_user_func_array(array('\PHPExcel\Shared\Date','excelToTimestamp'), $args);
$this->assertEquals($expectedResult, $result);
}
public function providerDateTimeExcelToPHP1904()
public function providerDateTimeExcelToTimestamp1904()
{
return new \PhpSpreadhsheet\unitTests\TestDataFileIterator('rawTestData/Shared/DateTimeExcelToPHP1904.data');
return new \PhpSpreadhsheet\unitTests\TestDataFileIterator('rawTestData/Shared/DateTimeExcelToTimestamp1904.data');
}
/**
@ -154,10 +148,10 @@ class DateTest extends \PHPUnit_Framework_TestCase
}
/**
* @dataProvider providerDateTimeExcelToPHP1900Timezone
* @dataProvider providerDateTimeExcelToTimestamp1900Timezone
* @group fail19
*/
public function testDateTimeExcelToPHP1900Timezone()
public function testDateTimeExcelToTimestamp1900Timezone()
{
$result = call_user_func(
array('\PHPExcel\Shared\Date','setExcelCalendar'),
@ -166,15 +160,12 @@ class DateTest extends \PHPUnit_Framework_TestCase
$args = func_get_args();
$expectedResult = array_pop($args);
if ($args[0] < 1) {
$expectedResult += gmmktime(0, 0, 0);
}
$result = call_user_func_array(array('\PHPExcel\Shared\Date','ExcelToPHP'), $args);
$result = call_user_func_array(array('\PHPExcel\Shared\Date','excelToTimestamp'), $args);
$this->assertEquals($expectedResult, $result);
}
public function providerDateTimeExcelToPHP1900Timezone()
public function providerDateTimeExcelToTimestamp1900Timezone()
{
return new \PhpSpreadhsheet\unitTests\TestDataFileIterator('rawTestData/Shared/DateTimeExcelToPHP1900Timezone.data');
return new \PhpSpreadhsheet\unitTests\TestDataFileIterator('rawTestData/Shared/DateTimeExcelToTimestamp1900Timezone.data');
}
}

View File

@ -1,23 +0,0 @@
#Excel DateTimeStamp Adjust Timezone Result Comments
22269, TRUE, 'America/New_York', -285138000 // 19-Dec-1960 00:00:00 UST
25569, TRUE, 'America/New_York', -18000 // PHP Base Date 01-Jan-1970 00:00:00 UST
30292, TRUE, 'America/New_York', 408049200 // 07-Dec-1982 00:00:00 UST
39611, TRUE, 'America/New_York', 1213214400 // 12-Jun-2008 00:00:00 UST
50424, TRUE, 'America/New_York', 2147454000 // PHP 32-bit Latest Date 19-Jan-2038 00:00:00 UST
22345.56789, TRUE, 'America/New_York', -278522534 // 18-May-1903 13:37:46 UST
22345.6789, TRUE, 'America/New_York', -278512943 // 18-Oct-1933 16:17:37 UST
0.5, TRUE, 'America/New_York', 25200 // 12:00:00 UST
0.75, TRUE, 'America/New_York', 46800 // 18:00.00 UST
0.12345, TRUE, 'America/New_York', -7334 // 02:57:46 UST
41215, TRUE, 'America/New_York', 1351800000 // 02-Nov-2012 00:00:00 UST
22269, TRUE, 'Pacific/Auckland', -285076800 // 19-Dec-1960 00:00:00 UST
25569, TRUE, 'Pacific/Auckland', 43200 // PHP Base Date 01-Jan-1970 00:00:00 UST
30292, TRUE, 'Pacific/Auckland', 408114000 // 07-Dec-1982 00:00:00 UST
39611, TRUE, 'Pacific/Auckland', 1213272000 // 12-Jun-2008 00:00:00 UST
50423.5, TRUE, 'Pacific/Auckland', 2147475600 // PHP 32-bit Latest Date 19-Jan-2038 00:00:00 UST
22345.56789, TRUE, 'Pacific/Auckland', -278461334 // 18-May-1903 13:37:46 UST
22345.6789, TRUE, 'Pacific/Auckland', -278451743 // 18-Oct-1933 16:17:37 UST
0.5, TRUE, 'Pacific/Auckland', 90000 // 12:00:00 UST
0.75, TRUE, 'Pacific/Auckland', 111600 // 18:00.00 UST
0.12345, TRUE, 'Pacific/Auckland', 57466 // 02:57:46 UST
41215, TRUE, 'Pacific/Auckland', 1351861200 // 02-Nov-2012 00:00:00 UST

View File

@ -0,0 +1,23 @@
#Excel DateTimeStamp Timezone Result Comments
22269, 'America/New_York', -285138000 // 19-Dec-1960 00:00:00 UST
25569, 'America/New_York', -18000 // PHP Base Date 01-Jan-1970 00:00:00 UST
30292, 'America/New_York', 408049200 // 07-Dec-1982 00:00:00 UST
39611, 'America/New_York', 1213214400 // 12-Jun-2008 00:00:00 UST
50424, 'America/New_York', 2147454000 // PHP 32-bit Latest Date 19-Jan-2038 00:00:00 UST
22345.56789, 'America/New_York', -278522534 // 18-May-1903 13:37:46 UST
22345.6789, 'America/New_York', -278512943 // 18-Oct-1933 16:17:37 UST
0.5, 'America/New_York', 25200 // 12:00:00 UST
0.75, 'America/New_York', 46800 // 18:00.00 UST
0.12345, 'America/New_York', -7334 // 02:57:46 UST
41215, 'America/New_York', 1351800000 // 02-Nov-2012 00:00:00 UST
22269, 'Pacific/Auckland', -285076800 // 19-Dec-1960 00:00:00 UST
25569, 'Pacific/Auckland', 43200 // PHP Base Date 01-Jan-1970 00:00:00 UST
30292, 'Pacific/Auckland', 408114000 // 07-Dec-1982 00:00:00 UST
39611, 'Pacific/Auckland', 1213272000 // 12-Jun-2008 00:00:00 UST
50423.5, 'Pacific/Auckland', 2147475600 // PHP 32-bit Latest Date 19-Jan-2038 00:00:00 UST
22345.56789, 'Pacific/Auckland', -278461334 // 18-May-1903 13:37:46 UST
22345.6789, 'Pacific/Auckland', -278451743 // 18-Oct-1933 16:17:37 UST
0.5, 'Pacific/Auckland', 90000 // 12:00:00 UST
0.75, 'Pacific/Auckland', 111600 // 18:00.00 UST
0.12345, 'Pacific/Auckland', 57466 // 02:57:46 UST
41215, 'Pacific/Auckland', 1351861200 // 02-Nov-2012 00:00:00 UST