Skip to content

Commit

Permalink
refs matomo-org#3074 fix range date is sometimes wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteur committed Oct 30, 2013
1 parent e0df029 commit 4e985d2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
33 changes: 24 additions & 9 deletions core/Period/Range.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@

/**
* Arbitrary date range representation.
*
*
* This class represents a period that contains a list of consecutive days as subperiods
* It is created when the **period** query parameter is set to **range** and is used
* to calculate the subperiods of multiple period requests (eg, when period=day and
* date=2007-07-24,2013-11-15).
*
*
* The range period differs from other periods mainly in that since it is arbitrary,
* range periods are not archived by the archive.php cron script.
*
Expand All @@ -37,7 +37,7 @@ class Range extends Period

/**
* Constructor.
*
*
* @param string $strPeriod The type of period each subperiod is. Either `'day'`, `'week'`,
* `'month'` or `'year'`.
* @param string $strDate The date range, eg, `'2007-07-24,2013-11-15'`.
Expand Down Expand Up @@ -273,19 +273,22 @@ protected function processOptimalSubperiods($startDate, $endDate)
} else {
// From start date,
// Process end of week
$week = new Week($startDate);
$week = new Week($startDate);
$startOfWeek = $week->getDateStart();
$endOfWeek = $week->getDateEnd();
$endOfWeek = $week->getDateEnd();

$firstDayNextMonth = $startDate->addPeriod(2, 'month')->setDay(1);
$useMonthsNextIteration = $firstDayNextMonth->isEarlier($endDate);

$useMonthsNextIteration = $startDate->addPeriod(2, 'month')->setDay(1)->isEarlier($endDate);
if ($useMonthsNextIteration
&& $endOfWeek->isLater($endOfMonth)
) {
$this->fillArraySubPeriods($startDate, $endOfMonth, 'day');
$endOfPeriod = $endOfMonth;
} // If end of this week is later than end date, we use days
elseif ($endOfWeek->isLater($endDate)
&& ($endOfWeek->isEarlier($this->today)
elseif ($this->isEndOfWeekLaterThanEndDate($endDate, $endOfWeek) &&
($endOfWeek->isEarlier($this->today)
|| $startOfWeek->toString() != $startDate->toString()
|| $endDate->isEarlier($this->today))
) {
$this->fillArraySubPeriods($startDate, $endDate, 'day');
Expand Down Expand Up @@ -374,7 +377,7 @@ public static function getLastDate($date = false, $period = false)
/**
* Returns a date ragne string given a period type, end date and number of periods
* the range spans over.
*
*
* @param string $period The sub period type, `'day'`, `'week'`, `'month'` and `'year'`.
* @param int $lastN The number of periods of type `$period` that the result range should
* span.
Expand All @@ -390,4 +393,16 @@ public static function getRelativeToEndDate($period, $lastN, $endDate, $site)
$date = $last30Relative->getDateStart()->toString() . "," . $last30Relative->getDateEnd()->toString();
return $date;
}

private function isEndOfWeekLaterThanEndDate($endDate, $endOfWeek)
{
$isEndOfWeekLaterThanEndDate = $endOfWeek->isLater($endDate);

$isEndDateAlsoEndOfWeek = ($endOfWeek->toString() == $endDate->toString());
$isEndOfWeekLaterThanEndDate = ($isEndOfWeekLaterThanEndDate
|| ($isEndDateAlsoEndOfWeek
&& $endDate->isLater($this->today)));

return $isEndOfWeekLaterThanEndDate;
}
}
17 changes: 17 additions & 0 deletions tests/PHPUnit/Core/Period/RangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,22 @@ public function testCustomRangeWeekInsideEndingToday()
$this->assertEquals($correct, $range->toString());
}

/**
* @group Core
*/
public function testRangeEndDateIsTodayAndStartDateNotStartOfTheWeek()
{
$range = new Range('range', '2013-10-29,2013-10-30', 'UTC', Date::factory('2013-10-30'));

$correct = array(
'2013-10-29',
'2013-10-30'
);

$this->assertEquals(count($correct), $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}

/**
* @group Core
*/
Expand Down Expand Up @@ -520,6 +536,7 @@ public function testRangePreviousmonthEndDateIsInFutureAndEndOfTheWeek()
$this->assertEquals(count($correct), $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}

/**
* @group Core
*/
Expand Down

0 comments on commit 4e985d2

Please sign in to comment.