(
latitude: number,
longitude: number,
date: Date,
)
| 122 | } |
| 123 | |
| 124 | function getSunsetSunriseUTCTime( |
| 125 | latitude: number, |
| 126 | longitude: number, |
| 127 | date: Date, |
| 128 | ) { |
| 129 | const dec31 = Date.UTC(date.getUTCFullYear(), 0, 0, 0, 0, 0, 0); |
| 130 | const oneDay = getDuration({days: 1}); |
| 131 | const dayOfYear = Math.floor((date.getTime() - dec31) / oneDay); |
| 132 | |
| 133 | const zenith = 90.83333333333333; |
| 134 | const D2R = Math.PI / 180; |
| 135 | const R2D = 180 / Math.PI; |
| 136 | |
| 137 | // convert the longitude to hour value and calculate an approximate time |
| 138 | const lnHour = longitude / 15; |
| 139 | |
| 140 | function getTime(isSunrise: boolean) { |
| 141 | const t = dayOfYear + (((isSunrise ? 6 : 18) - lnHour) / 24); |
| 142 | |
| 143 | // calculate the Sun's mean anomaly |
| 144 | const M = (0.9856 * t) - 3.289; |
| 145 | |
| 146 | // calculate the Sun's true longitude |
| 147 | let L = M + (1.916 * Math.sin(M * D2R)) + (0.020 * Math.sin(2 * M * D2R)) + 282.634; |
| 148 | if (L > 360) { |
| 149 | L -= 360; |
| 150 | } else if (L < 0) { |
| 151 | L += 360; |
| 152 | } |
| 153 | |
| 154 | // calculate the Sun's right ascension |
| 155 | let RA = R2D * Math.atan(0.91764 * Math.tan(L * D2R)); |
| 156 | if (RA > 360) { |
| 157 | RA -= 360; |
| 158 | } else if (RA < 0) { |
| 159 | RA += 360; |
| 160 | } |
| 161 | |
| 162 | // right ascension value needs to be in the same qua |
| 163 | const Lquadrant = (Math.floor(L / (90))) * 90; |
| 164 | const RAquadrant = (Math.floor(RA / 90)) * 90; |
| 165 | RA += (Lquadrant - RAquadrant); |
| 166 | |
| 167 | // right ascension value needs to be converted into hours |
| 168 | RA /= 15; |
| 169 | |
| 170 | // calculate the Sun's declination |
| 171 | const sinDec = 0.39782 * Math.sin(L * D2R); |
| 172 | const cosDec = Math.cos(Math.asin(sinDec)); |
| 173 | |
| 174 | // calculate the Sun's local hour angle |
| 175 | const cosH = (Math.cos(zenith * D2R) - (sinDec * Math.sin(latitude * D2R))) / (cosDec * Math.cos(latitude * D2R)); |
| 176 | if (cosH > 1) { |
| 177 | // always night |
| 178 | return { |
| 179 | alwaysDay: false, |
| 180 | alwaysNight: true, |
| 181 | time: 0, |
no test coverage detected
searching dependent graphs…