-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding the option to handle polar night and midnight sun cases
- Loading branch information
Showing
9 changed files
with
419 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules/ | ||
coverage/ | ||
coverage/ | ||
.idea |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,74 @@ | ||
import { dateByAddingSeconds, dayOfYear, isValidDate } from './DateUtils'; | ||
import adhan from './Adhan'; | ||
import SolarTime from './SolarTime'; | ||
import Astronomical from './Astronomical'; | ||
import TimeComponents from './TimeComponents'; | ||
|
||
export const PolarCircleResolution = { | ||
aqrabBalad: 'aqrab-balad', | ||
aqrabYaum: 'aqrab-yaum', | ||
unresolved: 'unresolved' | ||
}; | ||
|
||
export const POLAR_CIRCLE_RESOLUTION_LATITUDE_VARIATION_STEP = 0.5; // Degrees to add/remove at each search step | ||
export const POLAR_CIRCLE_RESOLUTION_VALID_LATITUDE = 65; // Based on https://en.wikipedia.org/wiki/Midnight_sun | ||
|
||
const isValidSolarTime = (solarTime) => { | ||
if (solarTime) { | ||
return solarTime && !isNaN(solarTime.sunrise) && !isNaN(solarTime.sunset); | ||
} | ||
return false; | ||
} | ||
|
||
export const polarCircleResolvedValues = (resolver, date, coordinates) => { | ||
let resolvedLatitude = coordinates.latitude; | ||
let resolvedDate = new Date(date.getTime()); | ||
let resolvedSolarTime = null; | ||
let daysAdded = 0; | ||
let pastDate = ((date) => { const newDate = new Date(date.getTime()); newDate.setDate(date.getDate() - 1); return newDate; })(date); | ||
let futureDate = ((date) => { const newDate = new Date(date.getTime()); newDate.setDate(date.getDate() + 1); return newDate; })(date); | ||
let adjustedLatitude = coordinates.latitude; | ||
|
||
switch (resolver) { | ||
case PolarCircleResolution.aqrabYaum: { | ||
while (!isValidSolarTime(resolvedSolarTime) && daysAdded <= Math.ceil(365 / 2)) { // Search within 6 months backward & forward | ||
resolvedSolarTime = new SolarTime(pastDate, coordinates); | ||
if (isValidSolarTime(resolvedSolarTime)) { | ||
resolvedDate.setTime(pastDate.getTime()); | ||
} else { | ||
resolvedSolarTime = new SolarTime(futureDate, coordinates); | ||
if (isValidSolarTime(resolvedSolarTime)) { | ||
resolvedDate.setTime(futureDate.getTime()); | ||
} | ||
} | ||
pastDate.setDate(pastDate.getDate() - 1); | ||
futureDate.setDate(pastDate.getDate() + 1); | ||
daysAdded++; | ||
} | ||
break; | ||
} | ||
default: | ||
case PolarCircleResolution.aqrabBalad: { | ||
while (!isValidSolarTime(resolvedSolarTime) && Math.abs(adjustedLatitude) >= POLAR_CIRCLE_RESOLUTION_VALID_LATITUDE) { | ||
adjustedLatitude -= (Math.sign(adjustedLatitude) * POLAR_CIRCLE_RESOLUTION_LATITUDE_VARIATION_STEP); | ||
resolvedSolarTime = new SolarTime(resolvedDate, { | ||
...coordinates, | ||
latitude: adjustedLatitude, | ||
}); | ||
if (isValidSolarTime(resolvedSolarTime)) { | ||
resolvedLatitude = adjustedLatitude; | ||
} | ||
} | ||
break; | ||
} | ||
} | ||
|
||
return { | ||
coordinates: { | ||
...coordinates, | ||
latitude: resolvedLatitude, | ||
}, | ||
date: resolvedDate, | ||
solarTime: resolvedSolarTime, | ||
}; | ||
} |
Oops, something went wrong.