Skip to content

Commit

Permalink
fix(ScheduleRange) - fixed method of determining next date time to ac…
Browse files Browse the repository at this point in the history
…count for date range diff rounding
  • Loading branch information
nyeholt committed Jul 3, 2017
1 parent 8fcaabf commit f95509e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
27 changes: 22 additions & 5 deletions code/ScheduleRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,34 @@ public function getNextDateTime() {

protected function getScheduleDateTime() {
$now = new Datetime(SS_Datetime::now()->Format(DateTime::ATOM));
// Presume where checking at a point before the StartDate
// get a start time for 'today'
$nextDateTime = $this->getStartDateTime();

if($now >= $this->getStartDateTime()) {
if($now >= $nextDateTime) {
//Inside the ScheduleRange so set nextDateTime to now + interval
$nextDateTime = $now->add(new DateInterval('PT' . $this->Interval . 'S'));
}

if ($nextDateTime > $this->getEndDateTime()) {
// and an end-time for 'today'
$todayEndTime = $this->getEndDateTime();

$lastEndTime = $this->getLastScheduleTime();

if ($nextDateTime > $todayEndTime) {
//Now + interval falls outside the Schedule range
if($nextDateTime > $this->getLastScheduleTime()) {
if($nextDateTime > $lastEndTime) {
$nextDateTime = null;
} else {
$this->goToNextDay();
$nextDateTime = $this->getScheduleDateTime();
}
}

// lastly, compare the very last date time.
if($nextDateTime > $lastEndTime) {
$nextDateTime = null;
}

return $nextDateTime;
}

Expand All @@ -153,9 +163,16 @@ public function getLastScheduleTime() {
return new Datetime($this->EndDate .' '. $this->EndTime);
}

public function __accessForgetScheduleDay() {

}

protected function getScheduleDay() {
$scheduleDay = new Datetime($this->StartDate .' '. $this->StartTime);
$now = new Datetime(SS_Datetime::now()->Format(DateTime::ATOM));

// we use $this->StartTime, because if we leave it as 'now' time, we may actually be closer to
// the _next_ day, and the diff logic further on will instead return +1 day more than we expect.
$now = new Datetime(SS_Datetime::now()->Format('Y-m-d ' . $this->StartTime));

// make sure that the 'day' we start looking from is close to 'now' so our
// loops don't work through days that don't matter
Expand Down
3 changes: 2 additions & 1 deletion tests/ScheduleRangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public function testGetNextDateTimeAfter() {

//After
SS_Datetime::set_mock_now('2015-10-06 11:50:00');
$this->assertEquals(null, $sched->getNextDateTime());
$result = $sched->getNextDateTime();
$this->assertEquals(null, $result);
}

public function testGetNextDateTimeDuring() {
Expand Down

0 comments on commit f95509e

Please sign in to comment.