Skip to content

Commit 7291392

Browse files
committed
getting time via http can be a problem esp on Lilygo T-Deck, default to NTP first
1 parent dfd29b4 commit 7291392

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

PyDOS.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def PyDOS():
7373
global envVars
7474
if "envVars" not in globals().keys():
7575
envVars = {}
76-
_VER = "1.47"
76+
_VER = "1.48"
7777
prmpVals = ['>','(',')','&','|','\x1b','\b','<','=',' ',_VER,'\n','$','']
7878

7979
print("Starting Py-DOS...")

getdate.py

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,7 @@ def getdate(passedIn=""):
2828
print("Attempting to set Date/Time",end="")
2929

3030
try:
31-
print(" Using http worldtimeapi.org...",end="")
32-
Pydos_wifi.timeout = 1000
33-
response = Pydos_wifi.get("http://worldtimeapi.org/api/ip",None,True)
34-
time_data = Pydos_wifi.json()
35-
36-
if passedIn == "":
37-
tz_hour_offset = int(time_data['utc_offset'][0:3])
38-
tz_min_offset = int(time_data['utc_offset'][4:6])
39-
if (tz_hour_offset < 0):
40-
tz_min_offset *= -1
41-
else:
42-
tz_hour_offset = int(passedIn)
43-
tz_min_offset = 0
44-
45-
unixtime = int(time_data['unixtime'] + (tz_hour_offset * 60 * 60)) + (tz_min_offset * 60)
46-
ltime = time.localtime(unixtime)
47-
48-
if sys.implementation.name.upper() == "MICROPYTHON":
49-
machine.RTC().datetime(tuple([ltime[0]-(time.localtime(0)[0]-1970)]+[ltime[i] for i in [1,2,6,3,4,5,7]]))
50-
else:
51-
rtc.RTC().datetime = ltime
52-
53-
print("\nTime and Date successfully set",end="")
54-
envVars['errorlevel'] = '0'
55-
56-
except:
57-
print( " FAILED. Trying NTP...",end="")
31+
print( "Trying NTP...",end="")
5832

5933
if passedIn == "":
6034
tz_hour_offset = -4
@@ -117,6 +91,33 @@ def getdate(passedIn=""):
11791
print("\nTime and Date successfully set",end="")
11892
envVars['errorlevel'] = '0'
11993

94+
except:
95+
96+
print("FAILED. Trying http worldtimeapi.org...",end="")
97+
Pydos_wifi.timeout = 1000
98+
response = Pydos_wifi.get("http://worldtimeapi.org/api/ip",None,True)
99+
time_data = Pydos_wifi.json()
100+
101+
if passedIn == "":
102+
tz_hour_offset = int(time_data['utc_offset'][0:3])
103+
tz_min_offset = int(time_data['utc_offset'][4:6])
104+
if (tz_hour_offset < 0):
105+
tz_min_offset *= -1
106+
else:
107+
tz_hour_offset = int(passedIn)
108+
tz_min_offset = 0
109+
110+
unixtime = int(time_data['unixtime'] + (tz_hour_offset * 60 * 60)) + (tz_min_offset * 60)
111+
ltime = time.localtime(unixtime)
112+
113+
if sys.implementation.name.upper() == "MICROPYTHON":
114+
machine.RTC().datetime(tuple([ltime[0]-(time.localtime(0)[0]-1970)]+[ltime[i] for i in [1,2,6,3,4,5,7]]))
115+
else:
116+
rtc.RTC().datetime = ltime
117+
118+
print("\nTime and Date successfully set",end="")
119+
envVars['errorlevel'] = '0'
120+
120121
print()
121122
Pydos_wifi.close()
122123

0 commit comments

Comments
 (0)