python修改操作系统时间的方法

本文实例讲述了python修改操作系统时间的方法。分享给大家供大家参考。具体实现方法如下:

#-*- coding:utf-8 -*-
import socket
import struct
import time
import win32api
timeserver = ‘210.72.145.44’ #国家授时中心ip
port = 123
def gettime():
time_1970 = 2208988800l
client = socket.socket(socket.af_inet, socket.sock_dgram)
data = ‘\x1b’ + 47 * ‘\0’
client.sendto(data, (timeserver, port))
data, address = client.recvfrom(1024)
data_result = struct.unpack(‘!12i’, data)[10]
data_result -= time_1970
return data_result
def setsystemtime():
tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst = time.gmtime(gettime())
win32api.setsystemtime(tm_year, tm_mon, tm_wday, tm_mday, tm_hour, tm_min, tm_sec, 0)
print “set system ok!”
if __name__ == ‘__main__’:
setsystemtime()
print “%d-%d-%d %d:%d:%d” % time.localtime(gettime())[:6]

希望本文所述对大家的python程序设计有所帮助。

Posted in 未分类

发表评论