Use astral.sun.sun to calculate various solar events for a specific date and location. The function returns a dictionary containing keys for events like dawn, sunrise, noon, sunset, and dusk.
By default, results are returned in UTC. To get results in a specific timezone, pass a tzinfo object (such as a zoneinfo.ZoneInfo instance or a timezone string from a LocationInfo object) to the tzinfo parameter.
import datetime
from astral import LocationInfo
from astral.sun import sun
# Define location: name, region, timezone, latitude, longitude
city = LocationInfo("London", "England", "Europe/London", 51.5, -0.116)
# Calculate sun times for a specific date
s = sun(city.observer, date=datetime.date(2009, 4, 22))
print(s['sunrise'])
# To return times in a specific timezone:
# Option 1: Using LocationInfo's timezone
s_local = sun(city.observer, date=datetime.date(2009, 4, 22), tzinfo=city.tzinfo)
# Option 2: Using zoneinfo
import zoneinfo
tz = zoneinfo.ZoneInfo("Europe/London")
s_local = sun(city.observer, date=datetime.date(2009, 4, 22), tzinfo=tz)