-
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
10 changed files
with
397 additions
and
20 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 |
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
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,78 @@ | ||
import SolarTime from './SolarTime'; | ||
import { dateByAddingDays } from './DateUtils'; | ||
|
||
export const PolarCircleResolution = { | ||
aqrabBalad: 'aqrab-balad', | ||
aqrabYaum: 'aqrab-yaum', | ||
unresolved: 'unresolved' | ||
}; | ||
|
||
const LATITUDE_VARIATION_STEP = 0.5; // Degrees to add/remove at each resolution step | ||
const UNSAFE_LATITUDE = 65; // Based on https://en.wikipedia.org/wiki/Midnight_sun | ||
|
||
const isValidSolarTime = (solarTime) => solarTime && !isNaN(solarTime.sunrise) && !isNaN(solarTime.sunset); | ||
|
||
const aqrabYaumResolver = (coordinates, date, daysAdded = 1, direction = 1) => { | ||
if (daysAdded > Math.ceil(365 / 2)) { | ||
return null; | ||
} | ||
const testDate = new Date(date.getTime()); | ||
testDate.setDate(testDate.getDate() + (direction * daysAdded)); | ||
const tomorrow = dateByAddingDays(testDate, 1); | ||
const solarTime = new SolarTime(testDate, coordinates); | ||
const tomorrowSolarTime = new SolarTime(tomorrow, coordinates); | ||
|
||
if (!isValidSolarTime(solarTime) || !isValidSolarTime(tomorrowSolarTime)) { | ||
return aqrabYaumResolver(coordinates, date, daysAdded + (direction > 0 ? 0 : 1), -direction); | ||
} | ||
|
||
return { | ||
date, | ||
tomorrow, | ||
coordinates, | ||
solarTime, | ||
tomorrowSolarTime, | ||
}; | ||
} | ||
|
||
const aqrabBaladResolver = (coordinates, date, latitude) => { | ||
const solarTime = new SolarTime(date, { ...coordinates, latitude }); | ||
const tomorrow = dateByAddingDays(date, 1); | ||
const tomorrowSolarTime = new SolarTime(tomorrow, { ...coordinates, latitude }); | ||
if (!isValidSolarTime(solarTime) || !isValidSolarTime(tomorrowSolarTime)) { | ||
return (Math.abs(latitude) >= UNSAFE_LATITUDE) ? | ||
aqrabBaladResolver(coordinates, date, latitude - Math.sign(latitude) * LATITUDE_VARIATION_STEP) | ||
: null; | ||
} | ||
|
||
return { | ||
date, | ||
tomorrow, | ||
coordinates: { latitude, longitude: coordinates.longitude }, | ||
solarTime, | ||
tomorrowSolarTime, | ||
}; | ||
}; | ||
|
||
export const polarCircleResolvedValues = (resolver, date, coordinates) => { | ||
const defaultReturn = { | ||
date, | ||
tomorrow: dateByAddingDays(date, 1), | ||
coordinates, | ||
solarTime: new SolarTime(date, coordinates), | ||
tomorrowSolarTime: new SolarTime(dateByAddingDays(date, 1), coordinates), | ||
}; | ||
|
||
switch (resolver) { | ||
case PolarCircleResolution.aqrabYaum: { | ||
return aqrabYaumResolver(coordinates, date) || defaultReturn; | ||
} | ||
case PolarCircleResolution.aqrabBalad: { | ||
const { latitude } = coordinates; | ||
return aqrabBaladResolver(coordinates, date, latitude - (Math.sign(latitude) * LATITUDE_VARIATION_STEP)) || defaultReturn; | ||
} | ||
default: { | ||
return defaultReturn; | ||
} | ||
} | ||
}; |
Oops, something went wrong.