-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feat] Adding an option to handle the polar night and midnight sun cases #30
[feat] Adding an option to handle the polar night and midnight sun cases #30
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JazakAllahu khairun for this PR. I think with a few adjustments this will be a great addition to the library.
I would recommend an approach where after these lines
we do something like
and this way the resolution code is only calculating |
This is definitely a great enhancement, I'm testing it but, it appears that even when the solar time is calculated correctly (which means |
@korbav that should actually be ok. If you look further down, we apply the high latitude rule to find isha and fajr times if we are in a situation that has no twilight.
|
It seems that the problem was coming from |
fe89fb0
to
f7a1a9c
Compare
src/PolarCircleResolution.js
Outdated
} | ||
break; | ||
} | ||
default: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the default case should return the original values
src/PrayerTimes.js
Outdated
(isNaN(tomorrowSolarTime.sunrise)) | ||
&& polarCircleResolver !== PolarCircleResolution.unresolved | ||
) { | ||
const resolved = polarCircleResolvedValues(polarCircleResolver, tomorrow, coordinates); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This concerns me slightly, as tomorrowSunrise needs to actually be one day later than the sunrise value. It's used for some high latitude adjustments like 1/7 of the night calculations. With the aqrab yaum calculation it might go back to the same day that the original resolver arrived at.
Perhaps we should move the first invalid check down here and then check if either sunrise, sunset or tomorrowSunrise are invalid. Then we can have the resolver find values that will allow the date and the following date to have valid sunrise and sunset times.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If my understanding is correct, we must ensure that the tomorrow's solar time will precisely correspond to the solar time of the day after the one we resolved for the today's solar time.
Thus, what we need in the resolution process, is to ensure that for the resolved today's solar time, the solar time of the day after is also valid. I made the changes correspondingly to that understanding.
801c43b
to
cb3efcf
Compare
dhuhrTime = new TimeComponents(solarTime.transit).utcDate(...dateComponents); | ||
sunriseTime = new TimeComponents(solarTime.sunrise).utcDate(...dateComponents); | ||
sunsetTime = new TimeComponents(solarTime.sunset).utcDate(...dateComponents); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we will also need to reset the value of tomorrow
as we use it below when setting tomorrowSunrise
@@ -16,7 +16,7 @@ export class PrayerTimes { | |||
} | |||
|
|||
export class CalculationParameters { | |||
constructor(fajrAngle: number, ishaAngle: number, ishaInterval: number, methodName?: string) | |||
constructor(methodName: string|undefined|null, fajrAngle: number, ishaAngle: number, ishaInterval: number, maghribAngle: number) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great catch, thank you! We will still need to add the typing for the new PolarCircleResolution type and the accompanying variable on the CalculationParameters type.
src/PolarCircleResolution.js
Outdated
} | ||
case PolarCircleResolution.aqrabBalad: { | ||
const resolved = aqrabBaladResolver(coordinates, date, resolvedLatitude - (Math.sign(resolvedLatitude) * LATITUDE_VARIATION_STEP)); | ||
if (resolved) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we have the individual resolver functions have the same return values as this function then we can simplify this to be
return resolved | defaultReturn
src/PolarCircleResolution.js
Outdated
const solarTimePast = new SolarTime(pastDate, coordinates); | ||
const solarTimePastTomorrow = new SolarTime(dateByAddingDays(pastDate, 1), coordinates); | ||
|
||
if (!isValidSolarTime(solarTimePast) || !isValidSolarTime(solarTimePastTomorrow)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could simplify having to do both past and future checks in one call by passing in a direction to indicate whether to add or subtract days to do the check. For example
const aqrabYaumResolver = (coordinates, date, daysAdded, direction)
then if the direction is positive we don't increment daysAdded but simply call back with the same daysAdded but with a negative direction. Then on the negative direction pass we do increment daysAdded and then swap back to positive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great enhancement!
98a3c09
to
3c1b187
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The final changes look good! We will need to update the README but I think that can be done in a separate PR.
3c1b187
to
2fce4b7
Compare
Great, I've just updated the documentation there for the commit log's sake. PS: I noticed the Pascal Case was used for the other parameters which are not proper names, I used Camel Case instead, for consistency I will change that if you're OK with that. |
@korbav yeah lets be consistent with our naming |
2fce4b7
to
9a0f249
Compare
In order to handle the particular cases of Midnight Sun & Polar Night days in the concerned areas, this PR adds a parameter
polarNightMidnightSunResolver
to the calculation parameters to let the user force the resolution of the prayers times normally not computable by choosing between one of thes 3 options described below.Issue reference : #29
PolarNightMidnightSunResolver.Unresolved
(default)Will let the invalid prayer times as they are.
PolarNightMidnightSunResolver.AqrabBalad
Will determine the closest location that can resolve the prayers times and use it for the prayer times normally not defined.
Note: The resolution consists on increasing the latitude (if negative) or decreasing it (if positive) towards the latitude of the Equator (0 degree), by a 0.5 degree step.
The algorithm will stop if the latitude has been brought down to absolute 65 degrees and the resolution is still impossible.
Indeed, based on Wiki Midnight Sun, the phenomenon should not occur for the latitudes between ]-65.44°; 65.44°[
PolarNightMidnightSunResolver.AqrabYaum
Will determine the closest day that can resolve the prayers times and use it for the prayer times normally not defined.
Note: The resolution consists on adding a day before and after until we find a valid day, the algorithm stops after having tried 6 months forward and backward.