tf2012
asked on
linux date/time setting via python
I'm using the following code in python to update the system date time. Basically user toggles the new date time with some spinners and presses enter and this is the code that is called ultimately.
Works great! That is, until I reboot. Then the date time simply goes back to what it was originally. Clearly I am doing something wrong. Can some linux command line guru or python black belt help me here?
Works great! That is, until I reboot. Then the date time simply goes back to what it was originally. Clearly I am doing something wrong. Can some linux command line guru or python black belt help me here?
def set_system_date_time(new_date_time):
try:
new_date_time_list = new_date_time.split(',', 1)
new_date = str(new_date_time_list[0])
new_time = str(new_date_time_list[1])
# set the new date first using subprocess and the default shell, otherwise weird things happen
command = 'sudo date --s="'+new_date+'"'
subprocess.check_call(command, shell=True)
# set the time last because the previous date function sets the time to midnight, so always do the time last
os.system('sudo date -s %s' % new_time)
# this is not sticking on restart???
except Exception, e:
gui_controller.logTestResult(e)
print "Exception in code:"
print "-" * 60
traceback.print_exc(file=sys.stdout)
print '-' * 60
ASKER
@arnold thanks but I barely understood what you are describing. I have no desire to use NTP or network time etc. I'm trying to set the time using python and have it stay that way even after the device is rebooted. Can someone help with that?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@arnold... great info testing now thanks!
ASKER
adding hwclock --syctohc worked perfectly.
You could onfigure ntpd to run without having it sync with a. External resource. It can then alculate the drift to maintain the time approximately within a reasonable range of accuracy. Depending on what your setup is, configuring one system as the local central ntp server...
To make sure your date/time are proximate to accurate time, make sure to enable ntpd while configuring it to synchronize with tine servers such as available at ntp.org
They gave a list that includes regional time server fir closer proximate to you.
The update for time on bootup often has a deviation beyond which the time will not be update, I think it is an hour or two.
Once you synchronize with external, you could periodically run the hwclock to see what the system HW clock has for the date/time versus your is synced date/time.........