diff --git a/public/index.php b/public/index.php index 2ea51e7..20368f6 100644 --- a/public/index.php +++ b/public/index.php @@ -58,6 +58,16 @@ if (!function_exists('intervalOverlapSeconds')) { return max(0, $end - $start); } } +if (!function_exists('intervalDurationSeconds')) { + function intervalDurationSeconds(?DateTimeImmutable $start, ?DateTimeImmutable $end): int + { + if ($start === null || $end === null) { + return 0; + } + + return max(0, $end->getTimestamp() - $start->getTimestamp()); + } +} if (!function_exists('planetVisibleIntervalsForDay')) { function planetVisibleIntervalsForDay(array $planet, DateTimeImmutable $dayStart, DateTimeImmutable $dayEnd): array { @@ -101,19 +111,27 @@ if (!function_exists('planetVisibilityClass')) { $nightSeconds = 0; $twilightSeconds = 0; + $visibleSeconds = 0; foreach ($intervals as [$from, $to]) { + $visibleSeconds += intervalDurationSeconds($from, $to); $nightSeconds += intervalOverlapSeconds($from, $to, $astroDusk, $astroDawnNext); $twilightSeconds += intervalOverlapSeconds($from, $to, $civilDusk, $astroDusk); $twilightSeconds += intervalOverlapSeconds($from, $to, $astroDawnNext, $civilDawnNext); } - if ($nightSeconds > 0) { - return ['class' => 'planet-visibility-good', 'label' => 'nachts gut']; + if ($visibleSeconds <= 0) { + return ['class' => 'planet-visibility-bad', 'label' => 'am Tag']; } - if ($twilightSeconds > 0) { - return ['class' => 'planet-visibility-warn', 'label' => 'in der Daemmerung']; + $nightRatio = $nightSeconds / $visibleSeconds; + + if ($nightSeconds >= 3600 && $nightRatio >= 0.35) { + return ['class' => 'planet-visibility-good', 'label' => 'nachts gut sichtbar']; + } + + if (($nightSeconds + $twilightSeconds) > 0) { + return ['class' => 'planet-visibility-warn', 'label' => 'nur teilweise nachts / in der Dämmerung']; } return ['class' => 'planet-visibility-bad', 'label' => 'am Tag'];