1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| import os import platform import urllib.request import time import ssl import datetime import demjson3
ssl._create_default_https_context = ssl._create_unverified_context
time_url = "https://quan.suning.com/getSysTime.do" headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36 Edg/98.0.1108.62" }
def int_to_str_zfill2(int:int): return str(int).zfill(2)
try: req = urllib.request.Request(time_url, headers=headers) time_response = urllib.request.urlopen(req) net_time_json = demjson3.decode(time_response.read().decode("utf-8"))
time_offset = time.timezone if (time.localtime().tm_isdst == 0) else time.altzone offset_hour = int(time_offset / 60 / 60 * -1)
net_time = datetime.datetime.strptime(net_time_json['sysTime2'], '%Y-%m-%d %H:%M:%S')
utc_time = net_time - datetime.timedelta(hours=8)
obj_tz_time = utc_time + datetime.timedelta(hours=offset_hour)
if platform.system() == "Windows": os.system("date %s/%s/%s" % (obj_tz_time.year, int_to_str_zfill2(obj_tz_time.month), int_to_str_zfill2(obj_tz_time.day))) os.system("time %s:%s:%s" % (int_to_str_zfill2(obj_tz_time.hour), int_to_str_zfill2(obj_tz_time.minute), int_to_str_zfill2(obj_tz_time.second)))
elif platform.system() == "Darwin": os.system("sudo date %s%s%s%s%s.%s" % (int_to_str_zfill2(obj_tz_time.month), int_to_str_zfill2(obj_tz_time.day), int_to_str_zfill2(obj_tz_time.hour), int_to_str_zfill2(obj_tz_time.minute), obj_tz_time.year, int_to_str_zfill2(obj_tz_time.second)))
elif platform.system() == "Linux": os.system("date -s %s/%s/%s" % (int_to_str_zfill2(obj_tz_time.month), int_to_str_zfill2(obj_tz_time.day), obj_tz_time.year)) os.system("date -s %s:%s:%s" % (int_to_str_zfill2(obj_tz_time.hour), int_to_str_zfill2(obj_tz_time.minute), int_to_str_zfill2(obj_tz_time.second))) else: print(platform.system()) print("Other systems are not supported, only windows mac and linux are supported") except: print("There is an error in the synchronization time, the network may not be accessible or the system is not supported")
|