diff --git a/Tests/Utility/Argument/DateUtilityTest.php b/Tests/Utility/Argument/DateUtilityTest.php index 720c57785..6ca595a01 100644 --- a/Tests/Utility/Argument/DateUtilityTest.php +++ b/Tests/Utility/Argument/DateUtilityTest.php @@ -91,19 +91,41 @@ public function testGetWeekNumberToApply() { $this->assertEquals(-1, DateUtility::getWeekNumberToApply(new DateTime("2018-05-01"), new DateTime("2018-05-01"), 1, 0)); $this->assertEquals(-1, DateUtility::getWeekNumberToApply(new DateTime("2018-05-01"), new DateTime("2018-05-01"), 1, 2)); - // Test with closest dates. + // Test with one week. + $this->assertEquals(1, DateUtility::getWeekNumberToApply(new DateTime("2018-05-02"), new DateTime("2018-05-01"), 1, 1)); + $this->assertEquals(1, DateUtility::getWeekNumberToApply(new DateTime("2018-05-09"), new DateTime("2018-05-01"), 1, 1)); + $this->assertEquals(1, DateUtility::getWeekNumberToApply(new DateTime("2018-05-16"), new DateTime("2018-05-01"), 1, 1)); + $this->assertEquals(1, DateUtility::getWeekNumberToApply(new DateTime("2018-05-23"), new DateTime("2018-05-01"), 1, 1)); + $this->assertEquals(1, DateUtility::getWeekNumberToApply(new DateTime("2018-05-30"), new DateTime("2018-05-01"), 1, 1)); + + // Test with two weeks. + $this->assertEquals(1, DateUtility::getWeekNumberToApply(new DateTime("2018-05-02"), new DateTime("2018-05-01"), 2, 1)); + $this->assertEquals(2, DateUtility::getWeekNumberToApply(new DateTime("2018-05-09"), new DateTime("2018-05-01"), 2, 1)); + $this->assertEquals(1, DateUtility::getWeekNumberToApply(new DateTime("2018-05-16"), new DateTime("2018-05-01"), 2, 1)); + $this->assertEquals(2, DateUtility::getWeekNumberToApply(new DateTime("2018-05-23"), new DateTime("2018-05-01"), 2, 1)); + $this->assertEquals(1, DateUtility::getWeekNumberToApply(new DateTime("2018-05-30"), new DateTime("2018-05-01"), 2, 1)); + + // Test with three weeks. + $this->assertEquals(1, DateUtility::getWeekNumberToApply(new DateTime("2018-05-02"), new DateTime("2018-05-01"), 3, 1)); + $this->assertEquals(2, DateUtility::getWeekNumberToApply(new DateTime("2018-05-09"), new DateTime("2018-05-01"), 3, 1)); + $this->assertEquals(3, DateUtility::getWeekNumberToApply(new DateTime("2018-05-16"), new DateTime("2018-05-01"), 3, 1)); + $this->assertEquals(1, DateUtility::getWeekNumberToApply(new DateTime("2018-05-23"), new DateTime("2018-05-01"), 3, 1)); + $this->assertEquals(2, DateUtility::getWeekNumberToApply(new DateTime("2018-05-30"), new DateTime("2018-05-01"), 3, 1)); + + // Test with four weeks. + $this->assertEquals(1, DateUtility::getWeekNumberToApply(new DateTime("2018-05-02"), new DateTime("2018-05-01"), 4, 1)); + $this->assertEquals(2, DateUtility::getWeekNumberToApply(new DateTime("2018-05-09"), new DateTime("2018-05-01"), 4, 1)); + $this->assertEquals(3, DateUtility::getWeekNumberToApply(new DateTime("2018-05-16"), new DateTime("2018-05-01"), 4, 1)); + $this->assertEquals(4, DateUtility::getWeekNumberToApply(new DateTime("2018-05-23"), new DateTime("2018-05-01"), 4, 1)); + $this->assertEquals(1, DateUtility::getWeekNumberToApply(new DateTime("2018-05-30"), new DateTime("2018-05-01"), 4, 1)); + + // Test with five weeks. $this->assertEquals(1, DateUtility::getWeekNumberToApply(new DateTime("2018-05-02"), new DateTime("2018-05-01"), 5, 1)); $this->assertEquals(2, DateUtility::getWeekNumberToApply(new DateTime("2018-05-09"), new DateTime("2018-05-01"), 5, 1)); $this->assertEquals(3, DateUtility::getWeekNumberToApply(new DateTime("2018-05-16"), new DateTime("2018-05-01"), 5, 1)); $this->assertEquals(4, DateUtility::getWeekNumberToApply(new DateTime("2018-05-23"), new DateTime("2018-05-01"), 5, 1)); $this->assertEquals(5, DateUtility::getWeekNumberToApply(new DateTime("2018-05-30"), new DateTime("2018-05-01"), 5, 1)); - $this->assertEquals(1, DateUtility::getWeekNumberToApply(new DateTime("2018-05-01"), new DateTime("2018-05-01"), 5, 1)); - $this->assertEquals(2, DateUtility::getWeekNumberToApply(new DateTime("2018-05-08"), new DateTime("2018-05-01"), 5, 1)); - $this->assertEquals(3, DateUtility::getWeekNumberToApply(new DateTime("2018-05-15"), new DateTime("2018-05-01"), 5, 1)); - $this->assertEquals(4, DateUtility::getWeekNumberToApply(new DateTime("2018-05-22"), new DateTime("2018-05-01"), 5, 1)); - $this->assertEquals(5, DateUtility::getWeekNumberToApply(new DateTime("2018-05-29"), new DateTime("2018-05-01"), 5, 1)); - // Change the week number. $this->assertEquals(3, DateUtility::getWeekNumberToApply(new DateTime("2018-05-01"), new DateTime("2018-05-01"), 5, 3)); $this->assertEquals(4, DateUtility::getWeekNumberToApply(new DateTime("2018-05-08"), new DateTime("2018-05-01"), 5, 3)); @@ -119,6 +141,12 @@ public function testGetWeekNumberToApply() { $this->assertEquals(4, DateUtility::getWeekNumberToApply(new DateTime("2018-05-29"), new DateTime("2018-05-01"), 5, 5)); // Change the year. + $this->assertEquals(1, DateUtility::getWeekNumberToApply(new DateTime("2022-05-01"), new DateTime("2018-05-01"), 1, 1)); + $this->assertEquals(1, DateUtility::getWeekNumberToApply(new DateTime("2022-05-08"), new DateTime("2018-05-01"), 1, 1)); + $this->assertEquals(1, DateUtility::getWeekNumberToApply(new DateTime("2022-05-15"), new DateTime("2018-05-01"), 1, 1)); + $this->assertEquals(1, DateUtility::getWeekNumberToApply(new DateTime("2022-05-22"), new DateTime("2018-05-01"), 1, 1)); + $this->assertEquals(1, DateUtility::getWeekNumberToApply(new DateTime("2022-05-29"), new DateTime("2018-05-01"), 1, 1)); + $this->assertEquals(1, DateUtility::getWeekNumberToApply(new DateTime("2022-05-01"), new DateTime("2018-05-01"), 5, 1)); $this->assertEquals(2, DateUtility::getWeekNumberToApply(new DateTime("2022-05-08"), new DateTime("2018-05-01"), 5, 1)); $this->assertEquals(3, DateUtility::getWeekNumberToApply(new DateTime("2022-05-15"), new DateTime("2018-05-01"), 5, 1)); diff --git a/Utility/Argument/DateUtility.php b/Utility/Argument/DateUtility.php index 2c2ebfeaa..8397852b7 100644 --- a/Utility/Argument/DateUtility.php +++ b/Utility/Argument/DateUtility.php @@ -74,18 +74,20 @@ public static function getWeekNumber(DateTime $date) { * @param DateTime $date The date. * @param DateTime $startDate The start date. * @param integer $weekCount The week count. - * @param integer $weekNumber The week number. + * @param integer $weekOffset The week offset. * @return integer Returns the week number to apply between 1 and $weekCount. */ - public static function getWeekNumberToApply(DateTime $date, DateTime $startDate, $weekCount, $weekNumber) { + public static function getWeekNumberToApply(DateTime $date, DateTime $startDate, $weekCount, $weekOffset = 1) { // Check the week arguments. - if ($weekCount <= 0 || $weekNumber <= 0 || $weekCount < $weekNumber) { + if ($weekCount <= 0 || $weekOffset <= 0 || $weekCount < $weekOffset) { return -1; } // Calculate. - $result = intval($date->diff($startDate)->d / 7) + $weekNumber; + $result = intval($date->diff($startDate)->d / 7); + $result %= $weekCount; + $result += $weekOffset; if ($weekCount < $result) { $result -= $weekCount; }