La til en parser for å gjøre det enklere å scanne farger med Linshang LS171
[einar-bin] / sun.py
1 #!/usr/bin/python3
2 from suncalc import get_position, get_times
3 from datetime import datetime, timedelta, date
4
5 debug = False
6 #debug = True
7
8 now = datetime.now()
9 #now = datetime(2023, 12, 13, 0, 0)
10 date = now.replace(hour=12, minute=0, second=0, microsecond=0)
11
12 lon = 10.71
13 lat = 60.772
14 height = 100
15
16 sun = get_times(date, lon, lat, height)
17
18 if type(sun["nautical_dawn"]) is datetime:
19 sunup = sun["nautical_dawn"] + timedelta(hours=1)
20 else:
21 sunup = now
22 if type(sun["nautical_dusk"]) is datetime:
23 sundown = sun["nautical_dusk"] + timedelta(hours=1)
24 else:
25 sundown = now + timedelta(hours=23, minutes=59, seconds=50)
26
27 # If sunup is in the future
28 if now < sunup:
29 countdown = (sunup - now).total_seconds()
30 countdown_read = sunup - now
31 duration = (sundown - sunup).total_seconds() * 1000
32 duration_read = sundown - sunup
33 else:
34 countdown = 0
35 countdown_read = 0
36 duration = (sundown - now).total_seconds() * 1000
37 duration_read = sundown - now
38
39 if int(duration) < 0:
40 print("Sun already set, fool")
41 exit(1)
42
43 if debug:
44 print(" Now is ", now)
45 print(" Date is ", date)
46 print(" Sun up is ", sunup)
47 print(" Sun down is ", sundown)
48 print("Countdown is ", countdown_read)
49 print(" Duration is ", duration_read)
50 print("")
51
52 print(int(countdown), int(duration), sep='\t')