-
Notifications
You must be signed in to change notification settings - Fork 1
/
sun.py
55 lines (41 loc) · 1.6 KB
/
sun.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#
# Test script to calculate the sunrise and sunset times, and detect if we are between
# 1/2 hour before sunrise and 1/2 hour after sunset.
#
from time import sleep
from picamera import PiCamera
from time import localtime, strftime
import ephem
from datetime import datetime
from dateutil import tz
def isDay():
user = ephem.Observer()
user_sunset = ephem.Observer()
user_sunrise = ephem.Observer()
user.lat = '40.0931' # Worthington, OH
user_sunset.lat = user.lat
user_sunrise.lat = user.lat
user.lon = '-83.0180' # Worthington, OH
user_sunset.lon = '-90.0180'
user_sunrise.lon = '-76.0180'
user.elevation = 274 # See wikipedia.org/Oldenburg
user.temp = 20 # current air temperature gathered manually
user.pressure = 1019.5 # current air pressure gathered manually
user_sunset.elevation = user.elevation
user_sunrise.elevation = user.elevation
user_sunset.temp = user.temp
user_sunrise.temp = user.temp
user_sunset.pressure = user.pressure
user_sunrise.pressure = user.pressure
next_sunrise_datetime = user_sunrise.next_rising(ephem.Sun()).datetime()
next_sunset_datetime = user_sunset.next_setting(ephem.Sun()).datetime()
from_zone = tz.gettz('UTC')
to_zone = tz.gettz('America/New_York')
local_sunrise = next_sunrise_datetime.replace(tzinfo=from_zone).astimezone(to_zone)
local_sunset = next_sunset_datetime.replace(tzinfo=from_zone).astimezone(to_zone)
it_is_day = local_sunset < local_sunrise
it_is_night = local_sunrise < local_sunset
print(local_sunrise)
print(local_sunset)
return it_is_day
isDay()