Skip to content

Commit

Permalink
Fix getWeekNumberToApply() with 1 week rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
webeweb committed May 28, 2018
1 parent 7e7114a commit 36b7c5a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
42 changes: 35 additions & 7 deletions Tests/Utility/Argument/DateUtilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
Expand Down
10 changes: 6 additions & 4 deletions Utility/Argument/DateUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 36b7c5a

Please sign in to comment.