2

I'm looking at making a quick python script that runs just after the system is booted. Basically, my CMOS battery is toast, and I can't get a new one at the moment. So I was thinking of making a script that checked the date and time online, and then set the system time via that. I've looked online for similar scripts, but I haven't seen any. Is this going to be possible in python? And how would I go about doing it? Thanks!

Nathan
  • 1,287
  • 6
  • 15
  • 32
  • What platform are you on? – wnnmaw Jan 23 '14 at 19:30
  • http://stackoverflow.com/questions/12664295/ntp-client-in-python – Guy Gavriely Jan 23 '14 at 19:32
  • Sorry, Im on Windows 7 Pro – Nathan Jan 23 '14 at 19:34
  • And @Guy, how would I go about setting the system time once I have it from a server? – Nathan Jan 23 '14 at 19:35
  • I believe Windows can do this automatically. See [How to Synchronize Windows Clock With Internet Time Server](http://www.guidingtech.com/3119/windows-clock-sync/) – Kevin Jan 23 '14 at 19:35
  • @Kevin, Is there a way to make it sync more then once a week? On startup would be ideal, otherwise I could do "Every 2 minutes" or something. – Nathan Jan 23 '14 at 19:38
  • 1
    Besides the GUI stuff, you can use `w32tm /resync` from a command prompt. (Which means you can put it in a batch file and put the batch file in your Startup directory.) See [this article](http://lifehacker.com/350074/sync-your-clock-to-internet-servers-from-the-command-prompt) and its accompanying comments for more information. Also, you can of course Google for `w32tm`. – John Y Jan 23 '14 at 20:51

2 Answers2

0

You can use a rest webservice (Python has json api but you can simply use 'requests' libs)

import requests
from requests_kerberos import HTTPKerberosAuth

r = requests.get('http://timeapi.org/utc/now.json?callback=myCallback', auth=('user', 'pass')

data = r.json()

and for the web service here you are (thanks to http://www.timeapi.org/ ) : http://timeapi.org/utc/now.json?callback=myCallback

leldo
  • 386
  • 2
  • 9
0

This python code will help you:

import os

os.environ['TZ'] = 'America/Montevideo'  # very important to set your time zone

os.system('ntpdate -s  south-america.pool.ntp.org')
print 'Time changed to: '
os.system('date')
Charles P.
  • 1,947
  • 16
  • 13