-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds active hours to restrict monitoring of a feed to a specific time window during the day. Addresses #189 ```YAML feeds: - name: GitHub Status url: https://www.githubstatus.com/history.rss interval: 600 active_hours: start_time: '09:30' end_time: '17:00' ```
- Loading branch information
1 parent
269547d
commit 57b76d4
Showing
19 changed files
with
756 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace Vigilant; | ||
|
||
use DateTime; | ||
use DateTimeZone; | ||
|
||
class ActiveHours | ||
{ | ||
private Logger $logger; | ||
|
||
private DateTime $now; | ||
private DateTime $start; | ||
private DateTime $end; | ||
|
||
private DateTimeZone $timezone; | ||
private bool $enabled = false; | ||
|
||
/** | ||
* @param DateTime $now Current time and date | ||
* @param ?string $startTime Start time | ||
* @param ?string $endTime End time | ||
* @param string $timezone Timezone | ||
* @param Logger $logger | ||
*/ | ||
public function __construct(DateTime $now, ?string $startTime, ?string $endTime, string $timezone, Logger $logger) | ||
{ | ||
$this->logger = $logger; | ||
|
||
$this->now = $now; | ||
$this->timezone = new DateTimeZone($timezone); | ||
|
||
$this->process($startTime, $endTime); | ||
} | ||
|
||
/** | ||
* Returns boolean indicating if time is in active hours window | ||
* @return bool | ||
*/ | ||
public function isEnabled(): bool | ||
{ | ||
return $this->enabled; | ||
} | ||
|
||
/** | ||
* Returns start time | ||
* @param string $format Format accepted by date() | ||
* @return string | ||
*/ | ||
public function getStartTime(string $format = 'Y-m-d H:i:s e'): string | ||
{ | ||
return $this->start->format($format); | ||
} | ||
|
||
/** | ||
* Returns end time | ||
* @param string $format Format accepted by date() | ||
* @return string | ||
*/ | ||
public function getEndTime(string $format = 'Y-m-d H:i:s e'): string | ||
{ | ||
return $this->end->format($format); | ||
} | ||
|
||
/** | ||
* @param ?string $startTime Start time | ||
* @param ?string $endTime End time | ||
*/ | ||
private function process(?string $startTime, ?string $endTime): void | ||
{ | ||
if ($startTime !== null && $endTime !== null) { | ||
$this->start = new DateTime($startTime, $this->timezone); | ||
$this->end = new DateTime($endTime, $this->timezone); | ||
|
||
if ($this->now >= $this->start && $this->now <= $this->end) { | ||
$this->enabled = true; | ||
} | ||
|
||
$this->logger->debug('Active hours details:'); | ||
$this->logger->debug('Current time: ' . $this->now->format('Y-m-d H:i:s e')); | ||
$this->logger->debug('Start time: ' . $this->start->format('Y-m-d H:i:s e')); | ||
$this->logger->debug('End time: ' . $this->end->format('Y-m-d H:i:s e')); | ||
$this->logger->debug('Enabled: ' . ($this->enabled ? 'true' : 'false')); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.