【发布时间】:2018-06-13 18:36:02
【问题描述】:
根据Python module to change system date and time - 我们可以使用此代码更改系统时间。我已经修改了这段代码并进行了相应的更改,但执行脚本会将时间更改为 UTC 格式,但会在大约 10 秒后更新为正确的时间。这种无法解释的行为促使我改用SetLocalTime 方法。但是,我无法让它工作。
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)
我返回错误TypeError: SetLocalTime() takes exactly 1 argument (8 given)。我知道 SetLocalTime 函数采用与 SetSystemTime 完全相同的参数,但事实并非如此。
我在 Google 上找不到任何示例,因此无法完成我的代码。
非常感谢任何建议。谢谢。
【问题讨论】:
-
@eryksun 谢谢,它有效!我并不特别知道它需要一个 datetime.datetime 实例。我试图使用时间模块本身的 pywin32.time 方法和方法,而不是日期时间。