<p>I was learning to calculate local solar time with <code>ephem</code>. I must admit, unit conversion between radians, hours and days, which are all represented by a float in the program can be somewhat confusing for first-timers.</p>
<pre><code>import ephem
from math import pi

sun = ephem.Sun()
loc = ephem.city(&quot;Taipei&quot;)

# Calculate the value of base_date, 
# which is the start of a new (solar) day at 0:00 hrs
# to which solar time hours can be added

ref = round(loc.date)
if ref &gt; loc.date:
    base_date = ref - 1.5
else:
    base_date = ref - 0.5

sun.compute(loc)

# Calculate solar time:

# (A) Using the sun's hour angle
# solar time = hour angle + 12 hrs
# because ephem dates are stored as a float number 
# indicating the number of (sidereal?) days counting from 1900/1/1 12:00:00,
# I divide the radian value by 2pi (rad/day) to convert its unit to day 
# and add that to the base date.

loc_time_a = ephem.date(base_date + (sun.ha + ephem.hours('12:00')) / (2 * pi))

# (B) Using UTC
# solar time = UTC + longitude
# Longitude is in radians, 
# so we need to divide by pi and multiply by 12 hrs to convert its unit to hours.
# Finally, we multiply by `ephem.hour` to convert the unit to days 
# to add to the date.

loc_time_b = ephem.date(loc.date + (loc.long / pi * 12) * ephem.hour)

print(str(loc_time_a), str(loc_time_b), sep=&quot;\n&quot;)
</code></pre>
<h1>Output:</h1>
<pre><code>2022/6/15 10:53:00 
2022/6/15 10:53:26
</code></pre>
<p>As can be seen from the output above, there is a discrepancy of about half a minute between the two calculations.</p>
<p>I was wondering how best to make sense of this difference: Is it safe to say that <code>loc_time_a</code> is the <em>apparent</em> solar time while <code>loc_time_b</code> is the <em>mean</em>?</p>
<p>I don't know how <code>sun.ha</code> is calculated in the <code>ephem</code> library behind the scene. I presume the above claim would stand, to the extent which <code>sun.ha</code> can represent the actual position of the sun at a given time, in a specific location. Am I missing something?</p>

CC BY-SA 4.0
2022-06-15T13:12:33.893Z
One is solar time, the other is mean solar time.  UTC is roughly based on mean solar time, so simply adding your longitude also produces mean solar time.  To convert mean time to solar time, use the "Equation of time", which does work out to be about 26 seconds on the date in question. https://en.wikipedia.org/wiki/Equation_of_time
Greg Miller
CC BY-SA 4.0
2022-06-15T15:57:06.450Z
@GregMiller — Your comment includes so good a guess about the cause, that you might want to post it as a candidate answer!
Brandon Rhodes
<the-sun><time><python><ephemeris>
Calculating local solar time with python ephem library
Sati
# Calculating local solar time with python ephem library

(Asked by: username_0 on 2022-06-15)

I was learning to calculate local solar time with `ephem`. I must admit, unit conversion between radians, hours and days, which are all represented by a float in the program can be somewhat confusing for first-timers.

```
import ephem
from math import pi

sun = ephem.Sun()
loc = ephem.city("Taipei")

# Calculate the value of base_date, 
# which is the start of a new (solar) day at 0:00 hrs
# to which solar time hours can be added

ref = round(loc.date)
if ref > loc.date:
    base_date = ref - 1.5
else:
    base_date = ref - 0.5

sun.compute(loc)

# Calculate solar time:

# (A) Using the sun's hour angle
# solar time = hour angle + 12 hrs
# because ephem dates are stored as a float number 
# indicating the number of (sidereal?) days counting from 1900/1/1 12:00:00,
# I divide the radian value by 2pi (rad/day) to convert its unit to day 
# and add that to the base date.

loc_time_a = ephem.date(base_date + (sun.ha + ephem.hours('12:00')) / (2 * pi))

# (B) Using UTC
# solar time = UTC + longitude
# Longitude is in radians, 
# so we need to divide by pi and multiply by 12 hrs to convert its unit to hours.
# Finally, we multiply by `ephem.hour` to convert the unit to days 
# to add to the date.

loc_time_b = ephem.date(loc.date + (loc.long / pi * 12) * ephem.hour)

print(str(loc_time_a), str(loc_time_b), sep="\n")

```

# Output:

```
2022/6/15 10:53:00 
2022/6/15 10:53:26

```

As can be seen from the output above, there is a discrepancy of about half a minute between the two calculations.

I was wondering how best to make sense of this difference: Is it safe to say that `loc_time_a` is the *apparent* solar time while `loc_time_b` is the *mean*?

I don't know how `sun.ha` is calculated in the `ephem` library behind the scene. I presume the above claim would stand, to the extent which `sun.ha` can represent the actual position of the sun at a given time, in a specific location. Am I missing something?

**Tags:** the-sun, time, python, ephemeris

> **Comments:**
> **username_1**: One is solar time, the other is mean solar time.  UTC is roughly based on mean solar time, so simply adding your longitude also produces mean solar time.  To convert mean time to solar time, use the "Equation of time", which does work out to be about 26 seconds on the date in question. https://en.wikipedia.org/wiki/Equation_of_time  
> **username_2**: @username_1 — Your comment includes so good a guess about the cause, that you might want to post it as a candidate answer!  

---

## 0 answers

