Skip to content

Commit

Permalink
Merge pull request mtdowling#41 from cmorbitzer/support-datetimeimmut…
Browse files Browse the repository at this point in the history
…able

Support DateTimeImmutable
  • Loading branch information
dragonmantank authored Mar 30, 2019
2 parents 714f6a8 + c38f254 commit 4d7bacd
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 87 deletions.
61 changes: 31 additions & 30 deletions src/Cron/CronExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,17 @@ public function setMaxIterationCount($maxIterationCount)
/**
* Get a next run date relative to the current date or a specific date
*
* @param string|\DateTime $currentTime Relative calculation date
* @param int $nth Number of matches to skip before returning a
* matching next run date. 0, the default, will return the current
* date and time if the next run date falls on the current date and
* time. Setting this value to 1 will skip the first match and go to
* the second match. Setting this value to 2 will skip the first 2
* matches and so on.
* @param bool $allowCurrentDate Set to TRUE to return the current date if
* it matches the cron expression.
* @param null|string $timeZone TimeZone to use instead of the system default
* @param string|\DateTimeInterface $currentTime Relative calculation date
* @param int $nth Number of matches to skip before returning a
* matching next run date. 0, the default, will return the
* current date and time if the next run date falls on the
* current date and time. Setting this value to 1 will
* skip the first match and go to the second match.
* Setting this value to 2 will skip the first 2
* matches and so on.
* @param bool $allowCurrentDate Set to TRUE to return the current date if
* it matches the cron expression.
* @param null|string $timeZone TimeZone to use instead of the system default
*
* @return \DateTime
* @throws \RuntimeException on too many iterations
Expand All @@ -201,11 +202,11 @@ public function getNextRunDate($currentTime = 'now', $nth = 0, $allowCurrentDate
/**
* Get a previous run date relative to the current date or a specific date
*
* @param string|\DateTime $currentTime Relative calculation date
* @param int $nth Number of matches to skip before returning
* @param bool $allowCurrentDate Set to TRUE to return the
* current date if it matches the cron expression
* @param null|string $timeZone TimeZone to use instead of the system default
* @param string|\DateTimeInterface $currentTime Relative calculation date
* @param int $nth Number of matches to skip before returning
* @param bool $allowCurrentDate Set to TRUE to return the
* current date if it matches the cron expression
* @param null|string $timeZone TimeZone to use instead of the system default
*
* @return \DateTime
* @throws \RuntimeException on too many iterations
Expand All @@ -219,14 +220,14 @@ public function getPreviousRunDate($currentTime = 'now', $nth = 0, $allowCurrent
/**
* Get multiple run dates starting at the current date or a specific date
*
* @param int $total Set the total number of dates to calculate
* @param string|\DateTime $currentTime Relative calculation date
* @param bool $invert Set to TRUE to retrieve previous dates
* @param bool $allowCurrentDate Set to TRUE to return the
* current date if it matches the cron expression
* @param null|string $timeZone TimeZone to use instead of the system default
* @param int $total Set the total number of dates to calculate
* @param string|\DateTimeInterface $currentTime Relative calculation date
* @param bool $invert Set to TRUE to retrieve previous dates
* @param bool $allowCurrentDate Set to TRUE to return the
* current date if it matches the cron expression
* @param null|string $timeZone TimeZone to use instead of the system default
*
* @return array Returns an array of run dates
* @return \DateTime[] Returns an array of run dates
*/
public function getMultipleRunDates($total, $currentTime = 'now', $invert = false, $allowCurrentDate = false, $timeZone = null)
{
Expand Down Expand Up @@ -277,8 +278,8 @@ public function __toString()
* specific date. This method assumes that the current number of
* seconds are irrelevant, and should be called once per minute.
*
* @param string|\DateTime $currentTime Relative calculation date
* @param null|string $timeZone TimeZone to use instead of the system default
* @param string|\DateTimeInterface $currentTime Relative calculation date
* @param null|string $timeZone TimeZone to use instead of the system default
*
* @return bool Returns TRUE if the cron is due to run or FALSE if not
*/
Expand Down Expand Up @@ -310,12 +311,12 @@ public function isDue($currentTime = 'now', $timeZone = null)
/**
* Get the next or previous run date of the expression relative to a date
*
* @param string|\DateTime $currentTime Relative calculation date
* @param int $nth Number of matches to skip before returning
* @param bool $invert Set to TRUE to go backwards in time
* @param bool $allowCurrentDate Set to TRUE to return the
* current date if it matches the cron expression
* @param string|null $timeZone TimeZone to use instead of the system default
* @param string|\DateTimeInterface $currentTime Relative calculation date
* @param int $nth Number of matches to skip before returning
* @param bool $invert Set to TRUE to go backwards in time
* @param bool $allowCurrentDate Set to TRUE to return the
* current date if it matches the cron expression
* @param string|null $timeZone TimeZone to use instead of the system default
*
* @return \DateTime
* @throws \RuntimeException on too many iterations
Expand Down
13 changes: 7 additions & 6 deletions src/Cron/DayOfMonthField.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cron;

use DateTime;
use DateTimeInterface;

/**
* Day of month field. Allows: * , / - ? L W
Expand Down Expand Up @@ -69,7 +70,7 @@ private static function getNearestWeekday($currentYear, $currentMonth, $targetDa
/**
* @inheritDoc
*/
public function isSatisfiedBy(DateTime $date, $value)
public function isSatisfiedBy(DateTimeInterface $date, $value)
{
// ? states that the field value is to be skipped
if ($value == '?') {
Expand Down Expand Up @@ -100,15 +101,15 @@ public function isSatisfiedBy(DateTime $date, $value)

/**
* @inheritDoc
*
* @param \DateTime|\DateTimeImmutable &$date
*/
public function increment(DateTime $date, $invert = false)
public function increment(DateTimeInterface &$date, $invert = false)
{
if ($invert) {
$date->modify('previous day');
$date->setTime(23, 59);
$date = $date->modify('previous day')->setTime(23, 59);
} else {
$date->modify('next day');
$date->setTime(0, 0);
$date = $date->modify('next day')->setTime(0, 0);
}

return $this;
Expand Down
21 changes: 12 additions & 9 deletions src/Cron/DayOfWeekField.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cron;

use DateTime;
use DateTimeInterface;
use InvalidArgumentException;

/**
Expand Down Expand Up @@ -51,8 +52,10 @@ public function __construct()

/**
* @inheritDoc
*
* @param \DateTime|\DateTimeImmutable $date
*/
public function isSatisfiedBy(DateTime $date, $value)
public function isSatisfiedBy(DateTimeInterface $date, $value)
{
if ($value == '?') {
return true;
Expand All @@ -71,7 +74,7 @@ public function isSatisfiedBy(DateTime $date, $value)
$weekday = str_replace('7', '0', $weekday);

$tdate = clone $date;
$tdate->setDate($currentYear, $currentMonth, $lastDayOfMonth);
$tdate = $tdate->setDate($currentYear, $currentMonth, $lastDayOfMonth);
while ($tdate->format('w') != $weekday) {
$tdateClone = new DateTime();
$tdate = $tdateClone
Expand Down Expand Up @@ -114,7 +117,7 @@ public function isSatisfiedBy(DateTime $date, $value)
}

$tdate = clone $date;
$tdate->setDate($currentYear, $currentMonth, 1);
$tdate = $tdate->setDate($currentYear, $currentMonth, 1);
$dayCount = 0;
$currentDay = 1;
while ($currentDay < $lastDayOfMonth + 1) {
Expand All @@ -123,7 +126,7 @@ public function isSatisfiedBy(DateTime $date, $value)
break;
}
}
$tdate->setDate($currentYear, $currentMonth, ++$currentDay);
$tdate = $tdate->setDate($currentYear, $currentMonth, ++$currentDay);
}

return $date->format('j') == $currentDay;
Expand All @@ -149,15 +152,15 @@ public function isSatisfiedBy(DateTime $date, $value)

/**
* @inheritDoc
*
* @param \DateTime|\DateTimeImmutable &$date
*/
public function increment(DateTime $date, $invert = false)
public function increment(DateTimeInterface &$date, $invert = false)
{
if ($invert) {
$date->modify('-1 day');
$date->setTime(23, 59, 0);
$date = $date->modify('-1 day')->setTime(23, 59, 0);
} else {
$date->modify('+1 day');
$date->setTime(0, 0, 0);
$date = $date->modify('+1 day')->setTime(0, 0, 0);
}

return $this;
Expand Down
14 changes: 7 additions & 7 deletions src/Cron/FieldInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Cron;

use DateTime;
use DateTimeInterface;

/**
* CRON field interface
Expand All @@ -12,23 +12,23 @@ interface FieldInterface
/**
* Check if the respective value of a DateTime field satisfies a CRON exp
*
* @param DateTime $date DateTime object to check
* @param string $value CRON expression to test against
* @param DateTimeInterface $date DateTime object to check
* @param string $value CRON expression to test against
*
* @return bool Returns TRUE if satisfied, FALSE otherwise
*/
public function isSatisfiedBy(DateTime $date, $value);
public function isSatisfiedBy(DateTimeInterface $date, $value);

/**
* When a CRON expression is not satisfied, this method is used to increment
* or decrement a DateTime object by the unit of the cron field
*
* @param DateTime $date DateTime object to change
* @param bool $invert (optional) Set to TRUE to decrement
* @param DateTimeInterface &$date DateTime object to change
* @param bool $invert (optional) Set to TRUE to decrement
*
* @return FieldInterface
*/
public function increment(DateTime $date, $invert = false);
public function increment(DateTimeInterface &$date, $invert = false);

/**
* Validates a CRON expression for a given field
Expand Down
31 changes: 16 additions & 15 deletions src/Cron/HoursField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Cron;

use DateTime;
use DateTimeInterface;
use DateTimeZone;

/**
Expand All @@ -23,32 +23,33 @@ class HoursField extends AbstractField
/**
* @inheritDoc
*/
public function isSatisfiedBy(DateTime $date, $value)
public function isSatisfiedBy(DateTimeInterface $date, $value)
{
if ($value == '?') {
return true;
}

return $this->isSatisfied($date->format('H'), $value);
}

/**
* {@inheritDoc}
*
* @param string|null $parts
* @param \DateTime|\DateTimeImmutable &$date
* @param string|null $parts
*/
public function increment(DateTime $date, $invert = false, $parts = null)
public function increment(DateTimeInterface &$date, $invert = false, $parts = null)
{
// Change timezone to UTC temporarily. This will
// allow us to go back or forwards and hour even
// if DST will be changed between the hours.
if (is_null($parts) || $parts == '*') {
$timezone = $date->getTimezone();
$date->setTimezone(new DateTimeZone('UTC'));
if ($invert) {
$date->modify('-1 hour');
} else {
$date->modify('+1 hour');
}
$date->setTimezone($timezone);
$date = $date->setTimezone(new DateTimeZone('UTC'));
$date = $date->modify(($invert ? '-' : '+') . '1 hour');
$date = $date->setTimezone($timezone);

$date->setTime($date->format('H'), $invert ? 59 : 0);
$date = $date->setTime($date->format('H'), $invert ? 59 : 0);
return $this;
}

Expand All @@ -72,11 +73,11 @@ public function increment(DateTime $date, $invert = false, $parts = null)

$hour = $hours[$position];
if ((!$invert && $date->format('H') >= $hour) || ($invert && $date->format('H') <= $hour)) {
$date->modify(($invert ? '-' : '+') . '1 day');
$date->setTime($invert ? 23 : 0, $invert ? 59 : 0);
$date = $date->modify(($invert ? '-' : '+') . '1 day');
$date = $date->setTime($invert ? 23 : 0, $invert ? 59 : 0);
}
else {
$date->setTime($hour, $invert ? 59 : 0);
$date = $date->setTime($hour, $invert ? 59 : 0);
}

return $this;
Expand Down
25 changes: 13 additions & 12 deletions src/Cron/MinutesField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Cron;

use DateTime;
use DateTimeInterface;

/**
* Minutes field. Allows: * , / -
Expand All @@ -22,24 +22,25 @@ class MinutesField extends AbstractField
/**
* @inheritDoc
*/
public function isSatisfiedBy(DateTime $date, $value)
public function isSatisfiedBy(DateTimeInterface $date, $value)
{
if ($value == '?') {
return true;
}

return $this->isSatisfied($date->format('i'), $value);
}

/**
* {@inheritDoc}
*
* @param string|null $parts
* @param \DateTime|\DateTimeImmutable &$date
* @param string|null $parts
*/
public function increment(DateTime $date, $invert = false, $parts = null)
public function increment(DateTimeInterface &$date, $invert = false, $parts = null)
{
if (is_null($parts)) {
if ($invert) {
$date->modify('-1 minute');
} else {
$date->modify('+1 minute');
}
$date = $date->modify(($invert ? '-' : '+') . '1 minute');
return $this;
}

Expand All @@ -62,11 +63,11 @@ public function increment(DateTime $date, $invert = false, $parts = null)
}

if ((!$invert && $current_minute >= $minutes[$position]) || ($invert && $current_minute <= $minutes[$position])) {
$date->modify(($invert ? '-' : '+') . '1 hour');
$date->setTime($date->format('H'), $invert ? 59 : 0);
$date = $date->modify(($invert ? '-' : '+') . '1 hour');
$date = $date->setTime($date->format('H'), $invert ? 59 : 0);
}
else {
$date->setTime($date->format('H'), $minutes[$position]);
$date = $date->setTime($date->format('H'), $minutes[$position]);
}

return $this;
Expand Down
Loading

0 comments on commit 4d7bacd

Please sign in to comment.