Astral Library

3.2 · active · verified Sun Apr 12

Astral is a Python package for calculating the times of various aspects of the sun and moon, including dawn, sunrise, noon, sunset, dusk, and moon phases. It also provides functions for solar azimuth and elevation. The library includes a self-contained geocoder for looking up location information by name. The current version is 3.2, and it maintains an active release cadence, frequently adding new features and improvements.

Warnings

Install

Imports

Quickstart

This example demonstrates how to define a location using `LocationInfo`, retrieve an `Observer` object, and then calculate the sun's key events (dawn, sunrise, noon, sunset, dusk) for a specific date.

import datetime
from astral.location import LocationInfo
from astral.sun import sun

# Define a location
l = LocationInfo('London', 'England', 'Europe/London', 51.5, 0.1)

# Get an Observer object from the LocationInfo
observer = l.observer

# Get sun times for today
today = datetime.date.today()
s = sun(observer, date=today)

print(f"Sun Information for {l.name} on {today}:")
print(f"  Dawn: {s['dawn']}")
print(f"  Sunrise: {s['sunrise']}")
print(f"  Noon: {s['noon']}")
print(f"  Sunset: {s['sunset']}")
print(f"  Dusk: {s['dusk']}")

view raw JSON →