Navigatrix.net - A Voyager's Companion
http://navigatrix.net/

Set system time from GPS
http://navigatrix.net/viewtopic.php?f=4&t=896
Page 1 of 1

Author:  billlion [ 05 Aug 2016, 03:50 ]
Post subject:  Set system time from GPS

Surely there should be a simple way to set the system clock to GPS time soon after startup?

I found this but I thought other people might have already done it on Navigatrix. Maybe ntp is a bit over the top

http://catb.org/gpsd/gpsd-time-service- ... _with_gpsd

Right now the back up battery on my motherboard is not working so it is a pain to have to set time every time I start up the computer.

Author:  Moe [ 29 Aug 2016, 08:24 ]
Post subject:  Re: Set system time from GPS

Time is tough. Hardware clocks, system clocks, RTC (realtime clocks), UTC, and localtime.

Navigatrix uses the gpsd to set the time, or so I've been lead to believe all these years. I just don't know which time it is, or when it does it. It a problem with transparency, out of sight out of mind.

You might have read further down in the link, about using gpsd to feed ntpd timeserver magic, and if left alone it will. http://catb.org/gpsd/gpsd-time-service-howto.html#_ntp_with_gpsd
Quote:
When gpsd receives a sentence with a timestamp, it packages the received timestamp with current local time and sends it to a shared-memory segment with an ID known to ntpd, the network time synchronization daemon. If ntpd has been properly configured to receive this message, it will be used to correct the system clock.
As I understand it, the trouble comes with large time differences. The software become suspicious. Where have you been the last 8 years, 4 months, 23 days, 11 hours, 37 minutes 27 point 374321 seconds? This is not normal.

The software appraises the situation to see if it is, as the locals say, fair dinkum. Give reason, and some bona fides, the wild number isn't some 'wild number'. It takes time.

But who has time for that?

Software is also fun, and for those with no impulse control. Do it now. We want to install a Python3 gpsd client from the Pypi Python Cheeseshop https://pypi.python.org/pypi/gps3
Code:
sudo -H pip3 install gps3
This also gives you some examples of other fun Python-GPS things you can use or improve. Read the docs. For a whooping good time read the code itself.

Next we. and I mean any hardy soul, can open a text editor
Code:
gksudo medit /usr/local/bin/hardtimes.py
Enter something like
Code:
#!/usr/bin/python3
# coding=utf-8
"""hard system time changer.  Just brutal, but innocently brutal"""
import time
import subprocess
import sys
from gps3 import agps3 

gpsd_socket = agps3.GPSDSocket()
gpsd_socket.connect(host='localhost', port=2947)
gpsd_socket.watch()
data_stream = agps3.DataStream()

try:
    subprocess.call(['/usr/bin/timedatectl', 'set-ntp', 'false'])  # Turn off the automagic time sync
    print('Turn off automagic time')
    for new_data in gpsd_socket:
        if new_data:
            data_stream.unpack(new_data)
            if data_stream.time != 'n/a':
                #timetest = '2011-11-11T11:11:11.020Z'
                #hardtime, junk = timetest.replace('T', ' ').split('.')
                # uncomment the two lines prior and comment the one line following to change arbitrarily.
                hardtime, junk = data_stream.time.replace('T', ' ').split('.')
                subprocess.call(['/usr/bin/timedatectl', 'set-time', hardtime])
                subprocess.call(['/usr/bin/timedatectl', 'set-ntp', 'true'])  # Turn the automagic time sync back on
                print('Set time: ', hardtime)
                print('Turn automagic time back on')
                gpsd_socket.close()
                print('  Done, Now what?')
                sys.exit(0)
        else:
            time.sleep(.1)
            print("No time for that....")
 
except KeyboardInterrupt:
    gpsd_socket.close()
    print('\nTerminated by user\nGood Bye.\n')
# End
If you make it executable
Code:
sudo chmod +x /usr/local/bin/hardtimes.py
Then you can call it from the terminal
Code:
sudo /usr/local/bin/hardtimes.py
This snippet of code
  • turns off the automatic time adjustment
  • grabs the first valid timestamp from the gps
  • changes the system time to that time
  • turns the automatic time fixer back on for fine adjustment
  • exits

See if that gets you by with UTC and all. If it does, then it can be put somewhere hands-free and automagical too.

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/