/// YOUR PRAYER APP DOWNLOADS THE ANSWER. I DERIVED IT.

Your Prayer App Downloads the Answer. I Derived It.

How I turned a math paper about the Sun into a working prayer time calculator, using only the Sun and a little code.

BY NUH / / 4 MIN READ / go, math, cli

I found a paper called “Tracing the Shadow: Mathematical Calculation of Prayer Times Using Spherical Trigonometry.” The title alone had enough syllables to scare off a normal person, which is exactly how I knew I had to read it. I read it anyway.

I wanted to know how the app on my phone knew. Most apps download the answer from a server. Nobody shows the work. So I opened the paper and started translating it into Go.

The goal: every number in the output traces back to a page in the paper, and every result matches JAKIM. No API, no magic, no trust me. The Sun has no API. That is the point.

A meme about trusting the Sun with math
Roll Safe contemplating celestial mechanics.

The model

Picture the Sun as a lamp sliding across your ceiling. Zuhr is the moment it reaches the highest point. Every other prayer is measured from there. That is the whole model. The math is bookkeeping.

Sun position depends on three things: your latitude, the Sun’s tilt that day (declination), and the hour angle (how far the Sun is from its peak). Time is that angle converted into hours.

The Sun’s daily mood

The Sun does not sit in the same place every day. It drifts north and south through the year, which is why days get longer and shorter. That drift is called declination, and every prayer time depends on it.

I started with a simplified formula. The answers looked almost right. Almost right is the most dangerous kind of wrong, because you stop looking. So I switched to the full formula from the paper:

type PrayerTime struct {
	Latitude  float64
	Longitude float64
	Year      int
	Month     int
	Day       int
	Zone      int

	declination float64
	istiwa      float64
}

The calculate() method works out the day’s tilt:

n := daySinceJan1(w.Year, w.Month, w.Day)
t := 2 * math.Pi * float64(n-1) / 365

w.declination =
	0.006918 -
	0.399912*math.Cos(t) +
	0.070257*math.Sin(t) -
	0.006758*math.Cos(2*t) +
	0.000907*math.Sin(2*t) -
	0.002696*math.Cos(3*t) +
	0.00148*math.Sin(3*t)

Scary looking. It is a recipe: take the day of the year, mix sines and cosines, get the tilt.

Why noon is rarely noon

My Zuhr kept drifting. Right some days, minutes off on others. That is not a rounding error. The Earth does not orbit in a perfect circle and it sits tilted, so clock time and Sun time disagree by a few minutes depending on the date. The fix is a small correction called the equation of time:

E := L - alpha
if E > 50 {
	E -= 360
}
return -E * 4

One tiny function. It fixed Zuhr, and every prayer after it.

Zuhr is the anchor

The paper’s key sentence: every prayer time is an offset from solar noon. Zuhr is the moment the Sun crosses your local middle line, the meridian. Your watch is a suggestion. The math:

w.istiwa = (12 + eot/60) + ((120 - w.Longitude) / 15)

Your longitude shifts this. Two cities in the same timezone can have different Zuhrs. Longitude always matters.

Shocked audience meme about longitude
Computes everything. Forgets longitude.

Hour angle: angles in, hours out

This is the heart of the whole project. Give the formula a Sun angle, get back hours. Morning prayers subtract hours from Zuhr. Evening prayers add them:

func hourAngle(w *PrayerTime, angle float64) float64 {
	return rad2deg(math.Acos(
		(math.Sin(deg2rad(angle)) -
			math.Sin(w.declination)*math.Sin(deg2rad(w.Latitude))) /
			(math.Cos(w.declination)*math.Cos(deg2rad(w.Latitude))),
	)) / 15
}

One function, used by every prayer. Learn this one and you understand the project.

Asr follows the shadow

Asr uses shadows. When your shadow grows to a certain length compared to your height, it is Asr. The paper gives the exact ratio from your latitude and the day’s tilt. This part took me the longest, because getting shadow geometry slightly wrong breaks everything without any error message:

func (w *PrayerTime) Asr() string {
	r := math.Atan(1 / (1 + math.Tan(math.Abs(deg2rad(w.Latitude)-w.declination))))
	return formatTime(w.istiwa + hourAngle(w, rad2deg(r)))
}

Fajr and Isha: how dark is dark enough

Fajr and Isha happen when the Sun sits a certain depth below the horizon. How deep? Scholars differ. The paper lists several standards. I picked -18 degrees because it is widely used and keeps the math clean:

func (w *PrayerTime) Fajr() string {
	return formatTime(w.istiwa - hourAngle(w, -18))
}

Trust nothing, check everything

I did not trust my own code. I checked it against official prayer times, had an AI re-derive the formulas independently, and got a friend to poke at edge cases. The AI agreed with me, which was either reassuring or a sign that we were both wrong in the same way. Once all three checks passed, I stopped arguing with the output.

Final thought

If you cannot point at each number in your output and say which page of the paper it came from, you do not understand the calculation yet. Prayer times are the Sun doing laps. The math writes down the schedule.

This project is for learning. For actual worship times, refer to e-solat.gov.my.