Skip to content

Instantly share code, notes, and snippets.

@priyadi
Last active May 20, 2019 14:41
Show Gist options
  • Save priyadi/1c3a696bf9e541d6fa1c385f6064e89f to your computer and use it in GitHub Desktop.
Save priyadi/1c3a696bf9e541d6fa1c385f6064e89f to your computer and use it in GitHub Desktop.
Calculation For the Beginning of a Month in the Hijri Calendar
#!/usr/bin/env python
'''
A simple script to calculate the beginning of month
in the Hijri calendar. For educational purposes only.
'''
import ephem
from datetime import datetime, timedelta
# start and end time
now = datetime(2018, 1, 1, 5, 0, 0)
end = datetime(2022, 12, 1, 5, 0, 0)
# initialize moon and sun
moon = ephem.Moon()
sun = ephem.Sun()
# location of the observer
observer = ephem.Observer()
observer.lon = '106.816667'
observer.lat = '-6.2'
observer.elevation = 8
# loop every day from now until end
yesterdayaltitude = 0
while now <= end:
observer.date = now
# sunset time
sunsettime = observer.next_setting(sun)
observer.date = sunsettime
moon.compute(observer)
# the altitude of the moon during the sunset, in degrees
altitude = float(moon.alt) / 0.01745329252
# if the moon is below the sun yesterday,
# but it is above the sun now, then it is a new month
if altitude > 0 and yesterdayaltitude <= 0:
print("%s-%s-%s: %s°" % (now.year, now.month, now.day, altitude))
# advance a day
yesterdayaltitude = altitude
now += timedelta(days = 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment