Skip to content

Instantly share code, notes, and snippets.

@RezhaBlue
Last active January 28, 2019 03:14
Show Gist options
  • Save RezhaBlue/6335a33d3ae4e73e500cdf3adf3c866f to your computer and use it in GitHub Desktop.
Save RezhaBlue/6335a33d3ae4e73e500cdf3adf3c866f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys, subprocess
def get_gps_datetime():
timestamp = ''
date = ''
datastream = open('/dev/serial/by-id/usb-Silicon_Labs_CP2102_USB_to_UART_Bridge_Controller_0001-if00-port0','r')
for line in datastream :
if line.startswith( '$GPZDA' ) :
# extract time
time_stamp = get_formatted_time(line)
# extract date
date_stamp = get_formatted_date(line)
break
return date_stamp+' '+time_stamp
def get_formatted_time(str):
time = str.strip().split(',')[1]
return time[:2]+':'+time[2:4]+':'+time[4:6]
def get_formatted_date(str):
day = str.strip().split(',')[2]
month = str.strip().split(',')[3]
year = str.strip().split(',')[4]
return year+'-'+month+'-'+day
def set_time(str):
print("date time is {}".format(str))
# os.system('sudo date -s %s' % str)
try:
subprocess.check_call(['timedatectl','set-ntp','false'])
except subprocess.CalledProcessError:
error_out("timedatectl execution failed: {0}".format(e))
try:
subprocess.check_call(['sudo','date','-s',str])
except subprocess.CalledProcessError:
error_out("date execution failed: {0}".format(e))
# subprocess.run(['sudo','hwclock','-w'])
print('done')
set_time(get_gps_datetime())
# put in a cron job, wait 5 min after boot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment