-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSunCalculator.php
More file actions
176 lines (140 loc) · 6.58 KB
/
Copy pathSunCalculator.php
File metadata and controls
176 lines (140 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* SunCalculator class
*
* Handles all sun time calculations including sunrise, sunset, dawn, dusk,
* midnight sun, and polar night periods.
*/
class SunCalculator {
private float $latitude;
private float $longitude;
public function __construct(float $latitude, float $longitude) {
$this->latitude = $latitude;
$this->longitude = $longitude;
}
/**
* Check if the given timestamp falls within the midnight sun period
*/
public function isMidnightSun(int $timestamp): bool {
$month = (int)date('m', $timestamp);
$day = (int)date('d', $timestamp);
$start = WebcamConfig::MIDNIGHT_SUN_PERIOD['start'];
$end = WebcamConfig::MIDNIGHT_SUN_PERIOD['end'];
return ($month == $start['month'] && $day >= $start['day']) ||
($month == 6) ||
($month == $end['month'] && $day <= $end['day']);
}
/**
* Check if the given timestamp falls within the polar night period
*/
public function isPolarNight(int $timestamp): bool {
$month = (int)date('m', $timestamp);
$day = (int)date('d', $timestamp);
$start = WebcamConfig::POLAR_NIGHT_PERIOD['start'];
$end = WebcamConfig::POLAR_NIGHT_PERIOD['end'];
return ($month == $start['month'] && $day >= $start['day']) ||
($month == $end['month'] && $day <= $end['day']);
}
/**
* Find sunrise, sunset, dawn, and dusk times for a given timestamp
*
* Returns array with:
* - sunrise: Unix timestamp
* - sunset: Unix timestamp
* - dawn: Unix timestamp
* - dusk: Unix timestamp
* - midnight_sun: boolean
* - polar_night: boolean
*
* Handles midnight sun and polar night by faking appropriate times.
* Adjusts dawn and dusk to be on the same day as sunrise and sunset.
*/
public function findSunTimes(int $timestamp): array {
$year = (int)date('Y', $timestamp);
$month = (int)date('m', $timestamp);
$day = (int)date('d', $timestamp);
$midnight_sun = $this->isMidnightSun($timestamp);
$polar_night = $this->isPolarNight($timestamp);
if ($midnight_sun) {
return $this->getMidnightSunTimes($year, $month, $day, $midnight_sun, $polar_night);
} elseif ($polar_night) {
return $this->getPolarNightTimes($year, $month, $day, $midnight_sun, $polar_night);
} else {
return $this->getNormalSunTimes($timestamp, $year, $month, $day, $midnight_sun, $polar_night);
}
}
/**
* Get fake sun times for midnight sun period
*/
private function getMidnightSunTimes(int $year, int $month, int $day, bool $midnight_sun, bool $polar_night): array {
// Fake sunrise and sunset to show some images
$sunrise = mktime(0, 0, 1, $month, $day, $year); // 00:00:01
$sunset = mktime(23, 59, 59, $month, $day, $year); // 23:59:59
$dawn = $sunrise;
$dusk = $sunset;
return [$sunrise, $sunset, $dawn, $dusk, $midnight_sun, $polar_night];
}
/**
* Get fake sun times for polar night period
*/
private function getPolarNightTimes(int $year, int $month, int $day, bool $midnight_sun, bool $polar_night): array {
$adjust_hours = WebcamConfig::POLAR_NIGHT_DAWN_DUSK_ADJUST_HOURS;
// Fake sunrise and sunset to show some images
$sunrise = mktime(WebcamConfig::POLAR_NIGHT_FAKE_SUNRISE_HOUR, 0, 0, $month, $day, $year);
$sunset = mktime(WebcamConfig::POLAR_NIGHT_FAKE_SUNSET_HOUR, 0, 0, $month, $day, $year);
$dawn = mktime(WebcamConfig::POLAR_NIGHT_FAKE_SUNRISE_HOUR - $adjust_hours, 0, 0, $month, $day, $year);
$dusk = mktime(WebcamConfig::POLAR_NIGHT_FAKE_SUNSET_HOUR + $adjust_hours, 0, 0, $month, $day, $year);
return [$sunrise, $sunset, $dawn, $dusk, $midnight_sun, $polar_night];
}
/**
* Get actual calculated sun times for normal days
*/
private function getNormalSunTimes(int $timestamp, int $year, int $month, int $day, bool $midnight_sun, bool $polar_night): array {
// Get sun info using PHP's built-in functionality
$sun_info = date_sun_info($timestamp, $this->latitude, $this->longitude);
$sunrise = $sun_info['sunrise'];
$sunset = $sun_info['sunset'];
// Fix sunrise if invalid. PHP's date_sun_info() returns 1 or a specific bogus timestamp
// (1715464868 = 2024-05-12 00:01:08 UTC) when it cannot calculate a value (e.g. polar night).
if ($sunrise == 1 || $sunrise == 1715464868 || !$sunrise) {
$sunrise = mktime(0, 0, 0, $month, $day, $year);
}
// Fix sunset if invalid (same bogus values as sunrise above).
if ($sunset == 1 || $sunset == 1715464868 || !$sunset) {
$sunset = mktime(23, 59, 59, $month, $day, $year);
}
$dawn = $this->calculateDawn($sun_info, $sunrise, $month, $day, $year);
$dusk = $this->calculateDusk($sun_info, $sunset, $month, $day, $year);
return [$sunrise, $sunset, $dawn, $dusk, $midnight_sun, $polar_night];
}
/**
* Calculate dawn, ensuring it's on the same day as sunrise
*/
private function calculateDawn(array $sun_info, int $sunrise, int $month, int $day, int $year): int {
$adjust_seconds = WebcamConfig::POLAR_NIGHT_DAWN_DUSK_ADJUST_HOURS * 60 * 60;
$dawn = $sun_info['nautical_twilight_begin'];
if ($dawn == 1715464868 || !$dawn) {
$dawn = $sunrise - $adjust_seconds;
// If dawn is on the previous day, set it to midnight
if (date('d', $dawn) != $day) {
$dawn = mktime(0, 0, 0, $month, $day, $year);
}
}
return $dawn;
}
/**
* Calculate dusk, ensuring it's on the same day as sunset
*/
private function calculateDusk(array $sun_info, int $sunset, int $month, int $day, int $year): int {
$adjust_seconds = WebcamConfig::POLAR_NIGHT_DAWN_DUSK_ADJUST_HOURS * 60 * 60;
$dusk = $sun_info['nautical_twilight_end'];
if ($dusk == 1715464868 || !$dusk) {
$dusk = $sunset + $adjust_seconds;
// If dusk is on the next day, set it to end of day
if (date('d', $dusk) != $day) {
$dusk = mktime(23, 59, 59, $month, $day, $year);
}
}
return $dusk;
}
}