quiet_hours_enabled) { return null; } $start = $this->parseTime((string) ($rule->quiet_hours_start ?? '')); $end = $this->parseTime((string) ($rule->quiet_hours_end ?? '')); if ($start === null || $end === null) { return null; } $timezone = $this->resolveTimezone($rule, $workspace); $localNow = ($now ?? CarbonImmutable::now($timezone))->setTimezone($timezone); if (! $this->isWithinQuietHours($localNow, $start, $end)) { return null; } $nextAllowedLocal = $this->nextAllowedAt($localNow, $start, $end); return $nextAllowedLocal->setTimezone('UTC'); } /** * @param array{hour:int,minute:int} $start * @param array{hour:int,minute:int} $end */ private function isWithinQuietHours(CarbonImmutable $localNow, array $start, array $end): bool { $nowMinutes = ((int) $localNow->format('H') * 60) + (int) $localNow->format('i'); $startMinutes = ($start['hour'] * 60) + $start['minute']; $endMinutes = ($end['hour'] * 60) + $end['minute']; if ($startMinutes === $endMinutes) { return true; } if ($startMinutes < $endMinutes) { return $nowMinutes >= $startMinutes && $nowMinutes < $endMinutes; } return $nowMinutes >= $startMinutes || $nowMinutes < $endMinutes; } /** * @param array{hour:int,minute:int} $start * @param array{hour:int,minute:int} $end */ private function nextAllowedAt(CarbonImmutable $localNow, array $start, array $end): CarbonImmutable { $nowMinutes = ((int) $localNow->format('H') * 60) + (int) $localNow->format('i'); $startMinutes = ($start['hour'] * 60) + $start['minute']; $endMinutes = ($end['hour'] * 60) + $end['minute']; if ($startMinutes === $endMinutes) { return $this->atLocalTime($localNow->addDay(), $end); } if ($startMinutes < $endMinutes) { if ($nowMinutes >= $startMinutes && $nowMinutes < $endMinutes) { return $this->atLocalTime($localNow, $end); } return $localNow; } if ($nowMinutes >= $startMinutes) { return $this->atLocalTime($localNow->addDay(), $end); } if ($nowMinutes < $endMinutes) { return $this->atLocalTime($localNow, $end); } return $localNow; } /** * @return array{hour:int,minute:int}|null */ private function parseTime(string $value): ?array { $value = trim($value); if (! preg_match('/^(?[01]\\d|2[0-3]):(?[0-5]\\d)$/', $value, $matches)) { return null; } return [ 'hour' => (int) $matches['hour'], 'minute' => (int) $matches['minute'], ]; } /** * @param array{hour:int,minute:int} $time */ private function atLocalTime(CarbonImmutable $baseDateTime, array $time): CarbonImmutable { return $baseDateTime ->setTime($time['hour'], $time['minute'], 0, 0); } private function resolveTimezone(AlertRule $rule, Workspace $workspace): string { $ruleTimezone = trim((string) ($rule->quiet_hours_timezone ?? '')); if ($ruleTimezone !== '' && in_array($ruleTimezone, \DateTimeZone::listIdentifiers(), true)) { return $ruleTimezone; } return $this->workspaceTimezoneResolver->resolve($workspace); } }