Skip to content

Instantly share code, notes, and snippets.

@1CM69
Created October 29, 2023 13:10
Show Gist options
  • Save 1CM69/b51da3a365ebed37cea91b67cdf2c50b to your computer and use it in GitHub Desktop.
Save 1CM69/b51da3a365ebed37cea91b67cdf2c50b to your computer and use it in GitHub Desktop.
Just a small piece of Python using the ephem module to find the dates of the summer & winter solstices for the current year, formatting them to a readable format and also calculating the day lengths for each of these.
import time
import datetime
import ephem
LAT = '' # in form 'xx.xxxxxx' or "xx:xx:xx.xx"
LON = '' # in form 'xx.xxxxxx' or "xx:xx:xx.xx"
ELE = 0 # in whole meters
Today = datetime.date.today()
CurrentYear = Today.strftime("%Y")
home = ephem.Observer()
home.lat = LAT
home.lon = LON
home.elevation = ELE
LongestDay = ephem.next_solstice(CurrentYear)
print("Longest day: ", LongestDay.datetime().strftime("%a %-d %b"))
print()
ShortestDay = ephem.next_solstice(LongestDay)
print("Shortest day: ", ShortestDay.datetime().strftime("%a %-d %b"))
print()
LongestDaySunRise = home.next_rising(ephem.Sun(), start=LongestDay)
LongestDaySunSet = home.next_setting(ephem.Sun(), start=LongestDay)
LongestDayLength = int((LongestDaySunSet - LongestDaySunRise)*86400) # total number of seconds
LongestDayLengthFormatted = time.strftime("%-HHrs %-MMins %-SSecs", time.gmtime(LongestDayLength))
print("Length of Longest day: ",LongestDayLengthFormatted)
print()
ShortestDaySunRise = home.next_rising(ephem.Sun(), start=ShortestDay)
ShortestDaySunSet = home.next_setting(ephem.Sun(), start=ShortestDay)
ShortestDayLength = int((ShortestDaySunSet - ShortestDaySunRise)*86400) # total number of seconds
ShortestDayLengthFormatted = time.strftime("%-HHrs %-MMins %-SSecs", time.gmtime(ShortestDayLength))
print("Length of Shortest day: ",ShortestDayLengthFormatted)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment