Calculate "Solar Noon" using ephem, translating to local time

Solar noon is not the mean of sunrise and sunset (see equation of time for the explanation). The ephem package has methods for getting transit times which you should use instead:

>>> import ephem
>>> o = ephem.Observer()
>>> o.lat, o.long = '37.0625', '-95.677068'
>>> sun = ephem.Sun()
>>> sunrise = o.previous_rising(sun, start=ephem.now())
>>> noon = o.next_transit(sun, start=sunrise)
>>> sunset = o.next_setting(sun, start=noon)
>>> noon
2010/11/6 18:06:21
>>> ephem.date((sunrise + sunset) / 2)
2010/11/6 18:06:08

Note that noon today is 13 seconds later (at your location) than the mean of sunrise and sunset.

(The line of code ephem.date((sunrise + sunset) / 2) shows how you could easily manipulate dates in the ephem package, if it were the right thing to do.)


If using ephem is not a hard requirement, I recently wrote a library called daylight which has a native function for solar noon directly.

>>> import daylight, pytz
>>> from datetime import datetime
>>> sun = daylight.Sunclock(37.0625, -95.677068)
>>> t = sun.solar_noon(datetime.utcnow().timestamp())
>>> datetime.utcfromtimestamp(t)
datetime.datetime(2020, 6, 4, 18, 20, 54)

Not that above time is in UTC. For a more appropriate timezone, say EST, you can do:

>>> tz = pytz.timezone('EST')
>>> tz_offset = tz.utcoffset(datetime.utcnow()).total_seconds()/3600
>>> sun = daylight.Sunclock(37.0625, -95.677068, tz_offset)
>>> t = sun.solar_noon(datetime.utcnow().timestamp())
>>> datetime.utcfromtimestamp(t).astimezone(tz)
datetime.datetime(2020, 6, 3, 7, 50, 54, tzinfo=<StaticTzInfo 'EST'>)

or do this for a particular date, say May 21st 2020

>>> t = sun.solar_noon(datetime(2020, 5, 21).timestamp())
>>> datetime.utcfromtimestamp(t)
datetime.datetime(2020, 5, 20, 18, 19, 14)