-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathgetdate.py
127 lines (106 loc) · 4.34 KB
/
getdate.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import sys
import time
from pydos_wifi import Pydos_wifi
if sys.implementation.name.upper() == 'MICROPYTHON':
import ntptime
import machine
elif sys.implementation.name.upper() == 'CIRCUITPYTHON':
try:
import adafruit_ntp
except:
pass
import rtc
def getdate(passedIn=""):
envVars['errorlevel'] = '1'
if Pydos_wifi.getenv('CIRCUITPY_WIFI_SSID') is None:
raise Exception("WiFi secrets are kept in settings.toml, please add them there by using setenv.py!")
if not Pydos_wifi.connect(Pydos_wifi.getenv('CIRCUITPY_WIFI_SSID'), Pydos_wifi.getenv('CIRCUITPY_WIFI_PASSWORD')):
raise Exception("Wifi Connection Error")
print("Connected to %s!" % Pydos_wifi.getenv('CIRCUITPY_WIFI_SSID'))
# We should have a valid IP now via DHCP
print("My IP address is", Pydos_wifi.ipaddress)
print("Attempting to set Date/Time",end="")
try:
print(" Using http worldtimeapi.org...",end="")
response = Pydos_wifi.get("http://worldtimeapi.org/api/ip",None,True)
time_data = Pydos_wifi.json()
if passedIn == "":
tz_hour_offset = int(time_data['utc_offset'][0:3])
tz_min_offset = int(time_data['utc_offset'][4:6])
if (tz_hour_offset < 0):
tz_min_offset *= -1
else:
tz_hour_offset = int(passedIn)
tz_min_offset = 0
unixtime = int(time_data['unixtime'] + (tz_hour_offset * 60 * 60)) + (tz_min_offset * 60)
ltime = time.localtime(unixtime)
if sys.implementation.name.upper() == "MICROPYTHON":
machine.RTC().datetime(tuple([ltime[0]-(time.localtime(0)[0]-1970)]+[ltime[i] for i in [1,2,6,3,4,5,7]]))
else:
rtc.RTC().datetime = ltime
print("\nTime and Date successfully set",end="")
envVars['errorlevel'] = '0'
except:
print( " FAILED. Trying NTP...",end="")
if passedIn == "":
tz_hour_offset = -4
else:
tz_hour_offset = int(passedIn)
if sys.implementation.name.upper() == "MICROPYTHON":
success = False
for i in range(10):
try:
ntptime.settime()
success = True
break
except:
time.sleep(2)
print(".",end="")
if not success:
print(".")
ntptime.settime()
unixtime = time.mktime(time.gmtime()) + tz_hour_offset*3600
ltime = time.localtime(unixtime)
machine.RTC().datetime(tuple([ltime[i] for i in [0,1,2,6,3,4,5,7]]))
print("\nTime and Date successfully set",end="")
envVars['errorlevel'] = '0'
elif sys.implementation.name.upper() == "CIRCUITPYTHON":
ntp = None
if not hasattr(Pydos_wifi.radio,'get_time'):
ntp = adafruit_ntp.NTP(Pydos_wifi._pool, server='pool.ntp.org', tz_offset=tz_hour_offset)
success = False
for i in range(5):
print(".",end="")
if ntp:
try:
rtc.RTC().datetime = ntp.datetime
success = True
break
except:
pass
else:
strtTime = time.time()
while time.time() < strtTime+5 and not success:
try:
rtc.RTC().datetime = time.localtime(Pydos_wifi.radio.get_time()[0] + tz_hour_offset*3600)
success = True
except:
time.sleep(.5)
if success:
break
# One more try, this time display the exception if we fail
if not success:
print(".")
if ntp:
rtc.RTC().datetime = ntp.datetime
else:
rtc.RTC().datetime = time.localtime(Pydos_wifi.radio.get_time()[0] + tz_hour_offset*3600)
print("\nTime and Date successfully set",end="")
envVars['errorlevel'] = '0'
print()
Pydos_wifi.close()
if __name__ != "PyDOS":
passedIn = input("Enter your current timezone offset (enter for default): ")
if 'envVars' not in dir():
envVars = {}
getdate(passedIn)