銆銆浠g爜浼間箮涔熶笉鏄緢澶嶆潅錛屽熀浜巔ython 3.0---
import time
import urllib.request
import urllib.parse
import ctypes
SetSystemTime = ctypes.windll.kernel32.SetSystemTime
GetSystemTime = ctypes.windll.kernel32.GetSystemTime
class SYSTEMTIME(ctypes.Structure):
c_ushort= ctypes.c_ushort
_fields_ = (
('wYear', c_ushort),
('wMonth', c_ushort),
('wDayOfWeek', c_ushort),
('wDay', c_ushort),
('wHour', c_ushort),
('wMinute', c_ushort),
('wSecond', c_ushort),
('wMilliseconds', c_ushort),
)
def __str__(self):
return '%4d%02d%02d%02d%02d%02d.%03d' % (self.wYear,self.wMonth,self.wDay,self.wHour,self.wMinute,self.wSecond,self.wMilliseconds)
def updateSystemTime():
url= 'http://www.baidu.com'
try:
response= urllib.request.urlopen(url)
header= response.info()
date=header['Date']
gmt=time.strptime(date[5:25], "%d %b %Y %H:%M:%S")
st=SYSTEMTIME(gmt.tm_year,gmt.tm_mon,gmt.tm_wday,gmt.tm_mday,gmt.tm_hour,gmt.tm_min,gmt.tm_sec,0)
SetSystemTime(ctypes.byref(st))
print('緗戠粶鏍℃椂鎴愬姛錛?/span>')
except Exception as ex:
print(ex)
print('緗戠粶鏍℃椂澶辮觸')
return False
return True
def printCurTime():
now= time.localtime(time.time())
print('褰撳墠緋葷粺鏃墮棿:', time.strftime("%Y-%m-%d %H:%M:%S", now))
if __name__=='__main__':
printCurTime()
if updateSystemTime():
printCurTime()
淇濆瓨鏂囦歡鍚嶄負updateTime.py錛屾斁鍏ユ煇涓郴緇熺洰褰曪紝鍦ㄥ懡浠よ涓祴璇曞涓嬶細
C:\>updatetime
褰撳墠緋葷粺鏃墮棿: 2009-12-16 16:51:11
緗戠粶鏍℃椂鎴愬姛錛?br>褰撳墠緋葷粺鏃墮棿: 2008-12-16 16:51:12
C:\>
銆涔嬪墠錛屾垜鏁呮剰鎶婃椂闂磋皟鏁村埌浜?009騫淬?br>
銆銆

]]>