| @@ 11-46 (lines=36) @@ | ||
| 8 | ||
| 9 | namespace AnimeDb\SmartSleep\Rule; |
|
| 10 | ||
| 11 | class OnceDayRule implements Rule |
|
| 12 | { |
|
| 13 | /** |
|
| 14 | * @var \DateTime |
|
| 15 | */ |
|
| 16 | private $time; |
|
| 17 | ||
| 18 | public function __construct() |
|
| 19 | { |
|
| 20 | $this->time = new \DateTime(); // default time |
|
| 21 | } |
|
| 22 | ||
| 23 | /** |
|
| 24 | * @param \DateTime $time |
|
| 25 | * |
|
| 26 | * @return bool |
|
| 27 | */ |
|
| 28 | public function isMatched(\DateTime $time) |
|
| 29 | { |
|
| 30 | $this->time = clone $time; // save current time |
|
| 31 | ||
| 32 | return true; |
|
| 33 | } |
|
| 34 | ||
| 35 | /** |
|
| 36 | * @return int |
|
| 37 | */ |
|
| 38 | public function seconds() |
|
| 39 | { |
|
| 40 | $offset_time = clone $this->time; |
|
| 41 | $offset_time->modify('+1 day')->setTime(0, 0, 0); |
|
| 42 | $offset = $offset_time->getTimestamp() - $this->time->getTimestamp(); // offset to next day |
|
| 43 | ||
| 44 | return $offset + rand(0, 86400); // 86400 is a 1 day |
|
| 45 | } |
|
| 46 | } |
|
| 47 | ||
| @@ 11-46 (lines=36) @@ | ||
| 8 | ||
| 9 | namespace AnimeDb\SmartSleep\Rule; |
|
| 10 | ||
| 11 | class OnceWeekRule implements Rule |
|
| 12 | { |
|
| 13 | /** |
|
| 14 | * @var \DateTime |
|
| 15 | */ |
|
| 16 | private $time; |
|
| 17 | ||
| 18 | public function __construct() |
|
| 19 | { |
|
| 20 | $this->time = new \DateTime(); // default time |
|
| 21 | } |
|
| 22 | ||
| 23 | /** |
|
| 24 | * @param \DateTime $time |
|
| 25 | * |
|
| 26 | * @return bool |
|
| 27 | */ |
|
| 28 | public function isMatched(\DateTime $time) |
|
| 29 | { |
|
| 30 | $this->time = clone $time; // save current time |
|
| 31 | ||
| 32 | return true; |
|
| 33 | } |
|
| 34 | ||
| 35 | /** |
|
| 36 | * @return int |
|
| 37 | */ |
|
| 38 | public function seconds() |
|
| 39 | { |
|
| 40 | $offset_time = clone $this->time; |
|
| 41 | $offset_time->modify('+1 week')->setTime(0, 0, 0); |
|
| 42 | $offset = $offset_time->getTimestamp() - $this->time->getTimestamp(); // offset to next week |
|
| 43 | ||
| 44 | return $offset + rand(0, 604800); // 604800 is a 1 week |
|
| 45 | } |
|
| 46 | } |
|
| 47 | ||