-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgps_setclock.py
More file actions
executable file
·48 lines (39 loc) · 1.25 KB
/
gps_setclock.py
File metadata and controls
executable file
·48 lines (39 loc) · 1.25 KB
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
#!/usr/bin/python3
# To get this to work:
# sudo apt-get install gpsd gpsd-clients python3-gps
# then run the get_time script in ~/bin
import os
import sys
import time
from gps import *
import tkinter as tk
from datetime import datetime,time
from widgets_tk import *
def SetSysClock():
val = lcd.val
print('Setting system clock to',val,'...')
cmd = "sudo date --set="+val+" &"
os.system("echo "+cmd)
os.system(cmd)
print("Done.")
print("Set Sys Clock to GPS UTC time")
try:
gpsd = gps(mode=WATCH_ENABLE)
except:
print("ERROR: No GPS found, the time has not been set")
sys.exit()
while True:
#wait until the next GPSD time tick
gpsd.next()
print('tic',gpsd.utc)
if gpsd.utc != None and gpsd.utc != "":
#gpsd.utc is formatted like 2015-04-01T17:32:04.000Z
#convert it to a form the date -u command will accept: 20140401 17:32:04
#use python slice notation [start:end] (where end desired end char + 1)
# gpsd.utc[0:4] is 2015
# gpsd.utc[5:7] is 04
# gpsd.utc[8:10] is 01
gpsutc = gpsd.utc[0:4] + gpsd.utc[5:7] + gpsd.utc[8:10] + ' ' + gpsd.utc[11:19]
print('GPS Time=',gpsutc,' UTC')
#os.system("sudo date -u -set=%s" % gpsutc)
#sys.exit()