As per Python module to change system date and time - We are able to change the system time using this code. I have adapted this code and changed it accordingly, but executing the script changes the time to the UTC format, but updates to the correct time about 10 seconds later. This unexplained behaviour has prompted me to instead, use the SetLocalTime
method instead. However, I cannot get this to work.
import socket
import struct
import sys
import time
import win32api
TIME1970 = 2208988800
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
data = '\x1b' + 47 * '\0'
client.sendto(data.encode(), ('10.0.0.1', 123))
data, address = client.recvfrom(1024)
if data:
print('Response received from:', address)
t = struct.unpack('!12I', data)[10]
t -= TIME1970
tf = time.strptime(time.ctime(t))
time_tuple = (tf[0], # Year
tf[1], # Month
tf[6], # Day of Week
tf[2], # Day
tf[3], # Hour
tf[4], # Minute
tf[5], # Second
0,#int(round(tf[5] * 1000)), # Millisecond
)
print(tf)
win32api.SetLocalTime(*time_tuple)
I am returned with the error TypeError: SetLocalTime() takes exactly 1 argument (8 given)
. I am aware the SetLocalTime function takes the exact same parameters as SetSystemTime
, but it's not.
I cannot find anywhere on Google with any examples so I'm unable to complete my code.
Any suggestions are greatly appreciated. Thank you.