2012-06-24 21:33:33 +00:00
|
|
|
<?php
|
|
|
|
|
2016-08-31 17:18:12 +00:00
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Shared;
|
2012-06-24 21:33:33 +00:00
|
|
|
|
2016-08-31 17:18:12 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
Fix Issue 1441 (isDateTime and Formulas) (#1480)
* Fix Issue 1441 (isDateTime and Formulas)
When you have a date-field which is a formula, isDateTime returns false.
https://github.com/PHPOffice/PhpSpreadsheet/issues/1441
Report makes sense; fixed as suggested. Also fixed a few minor
related issues, and added tests so that Shared/Date and Shared/TimeZone
are now completely covered.
Date/setDefaultTimeZone and TimeZone/setTimeZone were not consistent
about what to do in event of failure - return false or throw.
They will now both return false, which is what Date's function
said it would do in its doc block anyhow. Date/validateTimeZone will
continue to throw; it was protected, but was never called outside
Date, so I changed it to private.
TimeZone/getTimeZoneAdjustment checked for 'UST' when it probably
meant 'UTC', and, as it turns out, the check is not even needed.
The most serious problem was that TimeZone/validateTimeZone does not
check the backwards-compatible time zones. The timezone project
aggressively, and very controversially, "demotes" timezones;
such timezones eventually wind up in the PHP backwards-compatible list.
We want to make sure to check that list so that our applications do not
break when this happens.
2020-05-24 18:02:39 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
2017-11-08 15:48:01 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2012-06-24 21:33:33 +00:00
|
|
|
|
2017-11-08 15:48:01 +00:00
|
|
|
class DateTest extends TestCase
|
2012-06-24 21:33:33 +00:00
|
|
|
{
|
Fix Issue 1441 (isDateTime and Formulas) (#1480)
* Fix Issue 1441 (isDateTime and Formulas)
When you have a date-field which is a formula, isDateTime returns false.
https://github.com/PHPOffice/PhpSpreadsheet/issues/1441
Report makes sense; fixed as suggested. Also fixed a few minor
related issues, and added tests so that Shared/Date and Shared/TimeZone
are now completely covered.
Date/setDefaultTimeZone and TimeZone/setTimeZone were not consistent
about what to do in event of failure - return false or throw.
They will now both return false, which is what Date's function
said it would do in its doc block anyhow. Date/validateTimeZone will
continue to throw; it was protected, but was never called outside
Date, so I changed it to private.
TimeZone/getTimeZoneAdjustment checked for 'UST' when it probably
meant 'UTC', and, as it turns out, the check is not even needed.
The most serious problem was that TimeZone/validateTimeZone does not
check the backwards-compatible time zones. The timezone project
aggressively, and very controversially, "demotes" timezones;
such timezones eventually wind up in the PHP backwards-compatible list.
We want to make sure to check that list so that our applications do not
break when this happens.
2020-05-24 18:02:39 +00:00
|
|
|
private $dttimezone;
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
$this->dttimezone = Date::getDefaultTimeZone();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown(): void
|
|
|
|
{
|
|
|
|
Date::setDefaultTimeZone($this->dttimezone);
|
|
|
|
}
|
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testSetExcelCalendar(): void
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
2016-08-16 15:33:57 +00:00
|
|
|
$calendarValues = [
|
2016-08-14 04:08:43 +00:00
|
|
|
Date::CALENDAR_MAC_1904,
|
|
|
|
Date::CALENDAR_WINDOWS_1900,
|
2016-08-16 15:33:57 +00:00
|
|
|
];
|
2015-05-17 13:00:02 +00:00
|
|
|
|
|
|
|
foreach ($calendarValues as $calendarValue) {
|
2016-08-26 06:39:29 +00:00
|
|
|
$result = Date::setExcelCalendar($calendarValue);
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertTrue($result);
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-24 21:33:33 +00:00
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testSetExcelCalendarWithInvalidValue(): void
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
|
|
|
$unsupportedCalendar = '2012';
|
2016-08-26 06:39:29 +00:00
|
|
|
$result = Date::setExcelCalendar($unsupportedCalendar);
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertFalse($result);
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-06-24 21:33:33 +00:00
|
|
|
|
|
|
|
/**
|
2016-08-13 19:21:45 +00:00
|
|
|
* @dataProvider providerDateTimeExcelToTimestamp1900
|
2017-01-23 05:49:10 +00:00
|
|
|
*
|
|
|
|
* @param mixed $expectedResult
|
2012-06-24 21:33:33 +00:00
|
|
|
*/
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testDateTimeExcelToTimestamp1900($expectedResult, ...$args): void
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
2016-08-26 06:39:29 +00:00
|
|
|
Date::setExcelCalendar(Date::CALENDAR_WINDOWS_1900);
|
2015-05-17 13:00:02 +00:00
|
|
|
|
2017-01-23 05:49:10 +00:00
|
|
|
$result = Date::excelToTimestamp(...$args);
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($expectedResult, $result);
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-06-24 21:33:33 +00:00
|
|
|
|
2016-08-13 19:21:45 +00:00
|
|
|
public function providerDateTimeExcelToTimestamp1900()
|
2012-06-24 21:33:33 +00:00
|
|
|
{
|
2020-05-17 09:35:55 +00:00
|
|
|
return require 'tests/data/Shared/Date/ExcelToTimestamp1900.php';
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-06-24 21:33:33 +00:00
|
|
|
|
2012-07-24 12:21:31 +00:00
|
|
|
/**
|
2016-08-14 21:36:30 +00:00
|
|
|
* @dataProvider providerDateTimeTimestampToExcel1900
|
2017-01-23 05:49:10 +00:00
|
|
|
*
|
|
|
|
* @param mixed $expectedResult
|
2012-07-24 12:21:31 +00:00
|
|
|
*/
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testDateTimeTimestampToExcel1900($expectedResult, ...$args): void
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
2016-08-26 06:39:29 +00:00
|
|
|
Date::setExcelCalendar(Date::CALENDAR_WINDOWS_1900);
|
2015-05-17 13:00:02 +00:00
|
|
|
|
2017-01-23 05:49:10 +00:00
|
|
|
$result = Date::timestampToExcel(...$args);
|
2020-04-27 10:28:36 +00:00
|
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1E-5);
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-07-24 12:21:31 +00:00
|
|
|
|
2016-08-14 21:36:30 +00:00
|
|
|
public function providerDateTimeTimestampToExcel1900()
|
2012-07-24 12:21:31 +00:00
|
|
|
{
|
2020-05-17 09:35:55 +00:00
|
|
|
return require 'tests/data/Shared/Date/TimestampToExcel1900.php';
|
2016-08-14 21:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerDateTimeDateTimeToExcel
|
2017-01-23 05:49:10 +00:00
|
|
|
*
|
|
|
|
* @param mixed $expectedResult
|
2016-08-14 21:36:30 +00:00
|
|
|
*/
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testDateTimeDateTimeToExcel($expectedResult, ...$args): void
|
2016-08-14 21:36:30 +00:00
|
|
|
{
|
2016-08-26 06:39:29 +00:00
|
|
|
Date::setExcelCalendar(Date::CALENDAR_WINDOWS_1900);
|
2016-08-14 21:36:30 +00:00
|
|
|
|
2017-01-23 05:49:10 +00:00
|
|
|
$result = Date::dateTimeToExcel(...$args);
|
2020-04-27 10:28:36 +00:00
|
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1E-5);
|
2016-08-14 21:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function providerDateTimeDateTimeToExcel()
|
|
|
|
{
|
2020-05-17 09:35:55 +00:00
|
|
|
return require 'tests/data/Shared/Date/DateTimeToExcel.php';
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-07-24 12:21:31 +00:00
|
|
|
|
|
|
|
/**
|
2016-03-22 21:26:17 +00:00
|
|
|
* @dataProvider providerDateTimeFormattedPHPToExcel1900
|
2017-01-23 05:49:10 +00:00
|
|
|
*
|
|
|
|
* @param mixed $expectedResult
|
2012-07-24 12:21:31 +00:00
|
|
|
*/
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testDateTimeFormattedPHPToExcel1900($expectedResult, ...$args): void
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
2016-08-26 06:39:29 +00:00
|
|
|
Date::setExcelCalendar(Date::CALENDAR_WINDOWS_1900);
|
2015-05-17 13:00:02 +00:00
|
|
|
|
2017-01-23 05:49:10 +00:00
|
|
|
$result = Date::formattedPHPToExcel(...$args);
|
2020-04-27 10:28:36 +00:00
|
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1E-5);
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-07-24 12:21:31 +00:00
|
|
|
|
2016-03-22 21:26:17 +00:00
|
|
|
public function providerDateTimeFormattedPHPToExcel1900()
|
2012-07-24 12:21:31 +00:00
|
|
|
{
|
2020-05-17 09:35:55 +00:00
|
|
|
return require 'tests/data/Shared/Date/FormattedPHPToExcel1900.php';
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-07-24 12:21:31 +00:00
|
|
|
|
2012-06-24 21:33:33 +00:00
|
|
|
/**
|
2016-08-13 19:21:45 +00:00
|
|
|
* @dataProvider providerDateTimeExcelToTimestamp1904
|
2017-01-23 05:49:10 +00:00
|
|
|
*
|
|
|
|
* @param mixed $expectedResult
|
2012-06-24 21:33:33 +00:00
|
|
|
*/
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testDateTimeExcelToTimestamp1904($expectedResult, ...$args): void
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
2016-08-26 06:39:29 +00:00
|
|
|
Date::setExcelCalendar(Date::CALENDAR_MAC_1904);
|
2015-05-17 13:00:02 +00:00
|
|
|
|
2017-01-23 05:49:10 +00:00
|
|
|
$result = Date::excelToTimestamp(...$args);
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($expectedResult, $result);
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-06-24 21:33:33 +00:00
|
|
|
|
2016-08-13 19:21:45 +00:00
|
|
|
public function providerDateTimeExcelToTimestamp1904()
|
2012-06-24 21:33:33 +00:00
|
|
|
{
|
2020-05-17 09:35:55 +00:00
|
|
|
return require 'tests/data/Shared/Date/ExcelToTimestamp1904.php';
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-06-24 21:33:33 +00:00
|
|
|
|
2012-07-24 12:21:31 +00:00
|
|
|
/**
|
2016-08-14 21:36:30 +00:00
|
|
|
* @dataProvider providerDateTimeTimestampToExcel1904
|
2017-01-23 05:49:10 +00:00
|
|
|
*
|
|
|
|
* @param mixed $expectedResult
|
2012-07-24 12:21:31 +00:00
|
|
|
*/
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testDateTimeTimestampToExcel1904($expectedResult, ...$args): void
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
2016-08-26 06:39:29 +00:00
|
|
|
Date::setExcelCalendar(Date::CALENDAR_MAC_1904);
|
2015-05-17 13:00:02 +00:00
|
|
|
|
2017-01-23 05:49:10 +00:00
|
|
|
$result = Date::timestampToExcel(...$args);
|
2020-04-27 10:28:36 +00:00
|
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1E-5);
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-07-24 12:21:31 +00:00
|
|
|
|
2016-08-14 21:36:30 +00:00
|
|
|
public function providerDateTimeTimestampToExcel1904()
|
2012-07-24 12:21:31 +00:00
|
|
|
{
|
2020-05-17 09:35:55 +00:00
|
|
|
return require 'tests/data/Shared/Date/TimestampToExcel1904.php';
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-07-24 12:21:31 +00:00
|
|
|
|
2012-06-24 21:33:33 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider providerIsDateTimeFormatCode
|
2017-01-23 05:49:10 +00:00
|
|
|
*
|
|
|
|
* @param mixed $expectedResult
|
2012-06-24 21:33:33 +00:00
|
|
|
*/
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testIsDateTimeFormatCode($expectedResult, ...$args): void
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
2017-01-23 05:49:10 +00:00
|
|
|
$result = Date::isDateTimeFormatCode(...$args);
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($expectedResult, $result);
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-06-24 21:33:33 +00:00
|
|
|
|
|
|
|
public function providerIsDateTimeFormatCode()
|
|
|
|
{
|
2020-05-17 09:35:55 +00:00
|
|
|
return require 'tests/data/Shared/Date/FormatCodes.php';
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-06-24 21:33:33 +00:00
|
|
|
|
2012-11-02 23:07:01 +00:00
|
|
|
/**
|
2016-08-13 19:21:45 +00:00
|
|
|
* @dataProvider providerDateTimeExcelToTimestamp1900Timezone
|
2017-01-23 05:49:10 +00:00
|
|
|
*
|
|
|
|
* @param mixed $expectedResult
|
2012-11-02 23:07:01 +00:00
|
|
|
*/
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testDateTimeExcelToTimestamp1900Timezone($expectedResult, ...$args): void
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
2016-08-26 06:39:29 +00:00
|
|
|
Date::setExcelCalendar(Date::CALENDAR_WINDOWS_1900);
|
2015-05-17 13:00:02 +00:00
|
|
|
|
2017-01-23 05:49:10 +00:00
|
|
|
$result = Date::excelToTimestamp(...$args);
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($expectedResult, $result);
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-11-02 23:07:01 +00:00
|
|
|
|
2016-08-13 19:21:45 +00:00
|
|
|
public function providerDateTimeExcelToTimestamp1900Timezone()
|
2012-11-02 23:07:01 +00:00
|
|
|
{
|
2020-05-17 09:35:55 +00:00
|
|
|
return require 'tests/data/Shared/Date/ExcelToTimestamp1900Timezone.php';
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
Fix Issue 1441 (isDateTime and Formulas) (#1480)
* Fix Issue 1441 (isDateTime and Formulas)
When you have a date-field which is a formula, isDateTime returns false.
https://github.com/PHPOffice/PhpSpreadsheet/issues/1441
Report makes sense; fixed as suggested. Also fixed a few minor
related issues, and added tests so that Shared/Date and Shared/TimeZone
are now completely covered.
Date/setDefaultTimeZone and TimeZone/setTimeZone were not consistent
about what to do in event of failure - return false or throw.
They will now both return false, which is what Date's function
said it would do in its doc block anyhow. Date/validateTimeZone will
continue to throw; it was protected, but was never called outside
Date, so I changed it to private.
TimeZone/getTimeZoneAdjustment checked for 'UST' when it probably
meant 'UTC', and, as it turns out, the check is not even needed.
The most serious problem was that TimeZone/validateTimeZone does not
check the backwards-compatible time zones. The timezone project
aggressively, and very controversially, "demotes" timezones;
such timezones eventually wind up in the PHP backwards-compatible list.
We want to make sure to check that list so that our applications do not
break when this happens.
2020-05-24 18:02:39 +00:00
|
|
|
|
|
|
|
public function testVarious(): void
|
|
|
|
{
|
|
|
|
Date::setDefaultTimeZone('UTC');
|
|
|
|
self::assertFalse(Date::stringToExcel('2019-02-29'));
|
|
|
|
self::assertTrue((bool) Date::stringToExcel('2019-02-28'));
|
|
|
|
self::assertTrue((bool) Date::stringToExcel('2019-02-28 11:18'));
|
|
|
|
self::assertFalse(Date::stringToExcel('2019-02-28 11:71'));
|
|
|
|
$date = Date::PHPToExcel('2020-01-01');
|
|
|
|
self::assertEquals(43831.0, $date);
|
|
|
|
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
|
|
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
|
|
$sheet->setCellValue('B1', 'x');
|
|
|
|
$val = $sheet->getCell('B1')->getValue();
|
|
|
|
self::assertFalse(Date::timestampToExcel($val));
|
|
|
|
$cell = $sheet->getCell('A1');
|
|
|
|
self::assertNotNull($cell);
|
|
|
|
$cell->setValue($date);
|
|
|
|
$sheet->getStyle('A1')
|
|
|
|
->getNumberFormat()
|
|
|
|
->setFormatCode(NumberFormat::FORMAT_DATE_DATETIME);
|
|
|
|
self::assertTrue(null !== $cell && Date::isDateTime($cell));
|
|
|
|
$cella2 = $sheet->getCell('A2');
|
|
|
|
self::assertNotNull($cella2);
|
|
|
|
$cella2->setValue('=A1+2');
|
|
|
|
$sheet->getStyle('A2')
|
|
|
|
->getNumberFormat()
|
|
|
|
->setFormatCode(NumberFormat::FORMAT_DATE_DATETIME);
|
|
|
|
self::assertTrue(null !== $cella2 && Date::isDateTime($cella2));
|
|
|
|
$cella3 = $sheet->getCell('A3');
|
|
|
|
self::assertNotNull($cella3);
|
|
|
|
$cella3->setValue('=A1+4');
|
|
|
|
$sheet->getStyle('A3')
|
|
|
|
->getNumberFormat()
|
|
|
|
->setFormatCode('0.00E+00');
|
|
|
|
self::assertFalse(null !== $cella3 && Date::isDateTime($cella3));
|
|
|
|
}
|
2012-06-24 21:33:33 +00:00
|
|
|
}
|