Suntime
raw JSON → 1.3.2 verified Mon Apr 27 auth: no python
Simple sunset and sunrise time calculation Python library. Current version 1.3.2. Released irregularly.
pip install suntime Common errors
error AttributeError: 'Sun' object has no attribute 'get_sunrise_time' ↓
cause Using an older version of suntime (<1.3.0) where only get_local_sunrise_time existed.
fix
Upgrade to latest version: pip install --upgrade suntime. Or use get_local_sunrise_time if staying on older version.
error ValueError: Invalid latitude/longitude ↓
cause Latitude or longitude out of range. Latitude must be -90 to 90, longitude -180 to 180.
fix
Check coordinate values. For example, lat=51.5, lon=-0.13.
error TypeError: unsupported type for timedelta seconds component: datetime.date ↓
cause Passing a datetime object instead of date object to get_sunrise_time/get_sunset_time.
fix
Use .date() on datetime: e.g., sun.get_sunrise_time(datetime.now().date()).
Warnings
deprecated get_local_sunrise_time and get_local_sunset_time are deprecated since v1.3.0. ↓
fix Use get_sunrise_time and get_sunset_time which return UTC datetime objects. Convert to local timezone with pytz or zoneinfo.
gotcha Coordinates are in decimal degrees. Negative longitude for west, negative latitude for south. ↓
fix Ensure correct sign: e.g., London is +51.5, -0.13.
gotcha Results are approximate (seconds calculation depends on orography). ↓
fix Understand that reported seconds are approximate; do not rely on sub-minute accuracy.
gotcha get_sunrise_time and get_sunset_time require a date object (not datetime). ↓
fix Pass a datetime.date object: e.g., datetime.now().date().
Imports
- Sun
from suntime import Sun
Quickstart
from suntime import Sun
from datetime import datetime, timedelta
# Example coordinates: London
lat = 51.5
lon = -0.13
sun = Sun(lat, lon)
# Get sunrise and sunset for today
today = datetime.now().date()
sunrise = sun.get_sunrise_time(today)
sunset = sun.get_sunset_time(today)
print(f"Sunrise: {sunrise}, Sunset: {sunset}")
# Deprecated (remove in future): get_local_sunrise_time, get_local_sunset_time
# sunrise_local = sun.get_local_sunrise_time(today)
# sunset_local = sun.get_local_sunset_time(today)