Skip to content

Instantly share code, notes, and snippets.

@webghostx
Last active October 2, 2021 18:02
Show Gist options
  • Save webghostx/bc8e982706eaa8ccd113db9340a749be to your computer and use it in GitHub Desktop.
Save webghostx/bc8e982706eaa8ccd113db9340a749be to your computer and use it in GitHub Desktop.
simple Day/Night detection in Python
#!/usr/bin/python
# -- coding: utf-8 --
'''
simple Day/Night detection in Python
require pyephem https://pypi.python.org/pypi/pyephem/
'''
import ephem
import time
run = True
def main():
home = ephem.Observer()
home.lat = '47.17' # str() Latitude
home.lon = '7.37' # str() Longitude
while run:
next_sunrise = home.next_rising(ephem.Sun()).datetime()
next_sunset = home.next_setting(ephem.Sun()).datetime()
if next_sunset < next_sunrise:
print("It's day")
else:
print("It's night")
time.sleep(2)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print '...stopped!'
@webghostx
Copy link
Author

webghostx commented May 5, 2017

Ganz wichtig ist die Koordinaten in Strings zu packen. Sonst erscheinen Meldungen wie diese:

'Sun' is still above the horizon at 2017/5/5 19:47:34
'Sun' transits below the horizon at 2017/5/6 07:47:32

usysto.net

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment