islamic-network / prayer-times

An Islamic Prayer Times Library written in PHP. It performs the core calculations for the AlAdhan API @ https://aladhan.com.

Home Page:https://islamic.network

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Asr calculation uses server's system time, drifts over course of day

cpfair opened this issue · comments

As-salaamu alaykum,

I noticed that the Asr calculation uses the server's local time for the fractional component of the Julian day (date('i') + date('s')):

private function gregorianToJulianDate(): float
{
$julianDate = gregoriantojd($this->date->format('n'), $this->date->format('d'), $this->date->format('Y'));
//correct for half-day offset
$dayfrac = date('G') / 24 - .5;
if ($dayfrac < 0) $dayfrac += 1;
//now set the fraction of a day
$frac = $dayfrac + (date('i') + date('s') / 60) / 60 / 24;
return ($julianDate + $frac);
}
/**
* @param $factor
* @param $time
* @return mixed
*/
private function asrTime($factor, $time)
{
$julianDate = $this->gregorianToJulianDate();

This means that the server can return different Asr times for the same API call, depending on when in the day the call is made (relative to the system time of the server).

For example, this randomly generated API call for a specific date and lat/lng. When I made the API call a few days ago at 17:33, the Asr time was 13:57. When I make the same call now (at 08:15), the Asr time is 13:59. All other times are identical, as one would hope.

I don't think the Asr time should change based on when the API call is made relative to the server's clock. I think the original PrayTimes calculation is correct here.

3laykum salaam @cpfair. Thank you, this is a good catch and clearly a bug as the date that should be used here is $this->date instead of the date() function.

This is the result of fast coding 😄. I remember this change was made because the native php julian date function did not take fractions into account, and this produced inaccurate results. There's a long discussion on the Discord server somewhere about this, but I'm guessing at that point we made the change, tested what was then 'today' and moved on.

I'll try and patch this over the next couple of days.