## The Timing for Given Particular Sun Position

The following astronomical calculation of the timing for given particular sun position was taken from Almanac for Computers (1990) published by Nautical Almanac Office, United States Naval Observatory, Washington. Small modification has been applied, that is on the consistency of using degree (instead of radian) and on the inclusion of daylight saving time if necessary.

- Calculate the day of the year (N): Here, the inputs are YY, MM, and DD, which correspond to year, month, and day.
- Convert the longitude to hour value and calculate an approximate time (t):
- Calculate the Sun's mean anomaly (M):
- Calculate the Sun's true longitude (L)
- Calculate the Sun's right ascension (RA)
- Calculate the Sun's declination (sinDec, cosDec)
- Calculate the Sun's local hour angle (H)
- Calculate local mean time of rising/setting (T)
- Adjust back to UTC
- Convert UT value to local time zone of latitude/longitude (localT)
- Calculate the local time by considering the daylight saving time (timelocal)

N_1 = \rm{floor}\left(\frac{275 \times MM}{9}\right)

N_2 = \rm{floor}\left(\frac{MM + 9}{12}\right)

N_3 = 1 + \rm{floor}\left[\frac{1}{3}\left\{YY-4 \times \rm{floor}\left(\frac{YY}{4}\right)+2\right\}\right]

N = N_1 - (N_2 \times N_3) + DD - 30

lngHour = \frac{1}{15}longitude

If rising time is desired then:
t = N + \left\{\frac{1}{24}\left(6-lngHour\right)\right\}

If setting time is desired then:
t = N + \left\{\frac{1}{24}\left(18-lngHour\right)\right\}

M = (0.9856 \times t) - 3.289

L = M + \left[1.916\times \mathrm{sin}\left(\mathrm{deg2rad}(M)\right)\right] + \left[0.020\times \mathrm{sin}\left(2\times \mathrm{deg2rad}(M)\right)\right] + 282.634

NOTE: L potentially needs to be adjusted into the range between 0 and 360 by adding/subtracting with 360.
RA & = \mathrm{atan}\left\{0.91764\times \mathrm{tan}\left(\mathrm{deg2rad}(L)\right)\right\}

RA & = \mathrm{rad2deg}(RA)

NOTE: RA potentially needs to be adjusted into the range between 0 and 360 by adding/subtracting with 360.
Right ascension value needs to be in the same quadrant as $L$, such that:

{Lquadrant} = \left\{\mathrm{floor}\left(\frac{1}{90}\right)\right\} \times 90

{RAquadrant} = \left\{\mathrm{floor}\left(\frac{RA}{90}\right)\right\} \times 90

RA = RA + ({Lquadrant} - RAquadrant)

Right ascension value needs to be converted into hours.

RA = \frac{RA}{15}

sinDec = 0.39782 \times \mathrm{sin}\left(\mathrm{deg2rad}(L)\right)

cosDec = \mathrm{cos}\left(\mathrm{asin}(sinDec)\right)

cosH = \frac{\mathrm{cos}(zenith)-\left[sinDec \times \mathrm{sin}\{\mathrm{deg2rad}(latitude)\}\right]}{cosDec \times \mathrm{cos\{\mathrm{deg2rad}(latitude)\}}}

NOTE: If cosH>1 then the sun never rises on this location (on the specified date). If cosH<-1 then the sun never sets on this location (on the specified date).
If rising time is desired then:

H=360-\mathrm{rad2deg\{\mathrm{acos}(cosH)\}}

If setting time is desired then:
H=\mathrm{rad2deg\{\mathrm{acos}(cosH)\}}

Convert H into hours:

H = \frac{H}{15}

T = H + RA - (0.06571 \times t) - 6.622

NOTE: T potentially needs to be adjusted into the range between 0 and 24 by adding/subtracting with 24.
UT = T - lngHour

NOTE: UT potentially needs to be adjusted into the range between 0 and 24 by adding/subtracting with 24.
localT = UT + localOffset

NOTE: localT potentially needs to be adjusted into the range between 0 and 24 by adding/subtracting with 24.
timelocal = localT + Daylight