misc-scripts/sunlight-percent.py
File Type: text/x-script.python
from datetime import datetime, timedelta
from astral import LocationInfo, sun
import pytz
# Define your location (example for New York)
city = LocationInfo("Pittsburgh", "USA", "US/Eastern", 40.7128, -74.0060)
s = sun.sun(city.observer, date=datetime.now(), tzinfo=city.timezone)
# Get sunrise and sunset times for today
sunrise = s['sunrise']
sunset = s['sunset']
# Current time
now = datetime.now(pytz.timezone(city.timezone))
# Calculate the length of daylight
daylight_length = (sunset - sunrise).total_seconds() / 3600 # in hours
# Calculate how much time has passed since sunrise
passed_since_sunrise = (now - sunrise).total_seconds() / 3600 # in hours
# Calculate percentage of daylight passed
if now < sunrise or now > sunset:
percentage_passed = 0 # Before sunrise or after sunset
else:
percentage_passed = (passed_since_sunrise / daylight_length) * 100
# Print result
print(f"Percentage of daylight passed today: {percentage_passed:.2f}%")