From 23fbc845dd56bb9921126527ee701cada8edaf39 Mon Sep 17 00:00:00 2001 From: Adrien Crivelli Date: Sat, 1 Oct 2016 22:27:41 +0900 Subject: [PATCH] Fix unit tests for WEEKNUM() --- src/PhpSpreadsheet/Calculation/DateTime.php | 12 ++++-- .../Calculation/DateTimeTest.php | 3 -- tests/data/Calculation/DateTime/WEEKNUM.php | 42 ++++++++++++++++++- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/PhpSpreadsheet/Calculation/DateTime.php b/src/PhpSpreadsheet/Calculation/DateTime.php index ceda521c..9f7d32f3 100644 --- a/src/PhpSpreadsheet/Calculation/DateTime.php +++ b/src/PhpSpreadsheet/Calculation/DateTime.php @@ -1242,10 +1242,14 @@ class DateTime $PHPDateObject = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($dateValue); $dayOfYear = $PHPDateObject->format('z'); $PHPDateObject->modify('-' . $dayOfYear . ' days'); - $dow = $PHPDateObject->format('w'); - $daysInFirstWeek = 7 - (($dow + (2 - $method)) % 7); - $dayOfYear -= $daysInFirstWeek; - $weekOfYear = ceil($dayOfYear / 7) + 1; + $firstDayOfFirstWeek = $PHPDateObject->format('w'); + $daysInFirstWeek = (6 - $firstDayOfFirstWeek + $method) % 7; + $interval = $dayOfYear - $daysInFirstWeek; + $weekOfYear = floor($interval / 7) + 1; + + if ($daysInFirstWeek) { + ++$weekOfYear; + } return (int) $weekOfYear; } diff --git a/tests/PhpSpreadsheetTests/Calculation/DateTimeTest.php b/tests/PhpSpreadsheetTests/Calculation/DateTimeTest.php index a7e8c3c8..d33a74ae 100644 --- a/tests/PhpSpreadsheetTests/Calculation/DateTimeTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/DateTimeTest.php @@ -140,12 +140,9 @@ class DateTimeTest extends \PHPUnit_Framework_TestCase /** * @dataProvider providerWEEKNUM - * @group fail19 */ public function testWEEKNUM() { - $this->markTestIncomplete('TODO: This test should be fixed'); - $args = func_get_args(); $expectedResult = array_pop($args); $result = call_user_func_array([DateTime::class, 'WEEKOFYEAR'], $args); diff --git a/tests/data/Calculation/DateTime/WEEKNUM.php b/tests/data/Calculation/DateTime/WEEKNUM.php index bdfa7d4a..494ef8be 100644 --- a/tests/data/Calculation/DateTime/WEEKNUM.php +++ b/tests/data/Calculation/DateTime/WEEKNUM.php @@ -6,14 +6,54 @@ return [ 1, 52, ], + [ + '2000-01-01', + 1, + 1, + ], + [ + '2000-01-02', + 1, + 2, + ], + [ + '2000-01-01', + 2, + 1, + ], + [ + '2000-01-03', + 2, + 2, + ], [ '1995-01-01', 1, 1, ], + [ + '1995-01-07', + 1, + 1, + ], + [ + '1995-01-08', + 1, + 2, + ], + [ + '1995-01-01', + 2, + 1, + ], + [ + '1995-01-02', + 2, + 2, + ], [ '3/7/1977', - 27, + 28, ], [ '3/7/1977',