Skip to content

Instantly share code, notes, and snippets.

Created May 17, 2013 09:57
Show Gist options
  • Save anonymous/5598132 to your computer and use it in GitHub Desktop.
Save anonymous/5598132 to your computer and use it in GitHub Desktop.
Connects to your GPS-Dongle and synchronize systemtime with GPS-UTC-time. Handles local-timezones
#! /usr/bin/python
#####################
# GPS-TIME-SYNC #
# 2013 flazer #
# info@flazer.net #
#####################
# Connects to your GPS-Dongle and synchronize systemtime with GPS-UTC-time.
# Handles local-timezones
import os
import sys
from gps import *
from datetime import datetime
from pytz import timezone
# Change to your timezone:
localzone = 'Europe/Berlin'
print 'Attempting to access GPS...'
try:
gpsd = gps(mode=WATCH_ENABLE)
except:
print 'No GPS connection. NO TIME-SYNC.'
sys.exit()
while True:
gpsd.next()
if gpsd.utc != None and gpsd.utc != '':
tz = timezone('UTC')
dtz = tz.normalize(tz.localize(datetime(int(gpsd.utc[0:4]), int(gpsd.utc[5:7]), int(gpsd.utc[8:10]), int(gpsd.utc[11:13]), int(gpsd.utc[14:16]), int(gpsd.utc[17:19]))))
dgmt = dtz.astimezone(timezone(localzone))
gpstime = dgmt.strftime('%m%d%H%M%Y.%S')
print 'Synchronizing system time... (' + gpstime + ')'
os.system('date ' + gpstime)
print 'System time set.'
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment