1. 程式人生 > 其它 >獲取特定伺服器時間(格式:毫秒級 微秒級)注意顯示格式

獲取特定伺服器時間(格式:毫秒級 微秒級)注意顯示格式

def get_web_now_timea(time_format='YYYY-MM-DD HH:mm:ss.SSSSSS'):
        """
        獲取網路時間,返回時間格式:2019-12-13 11:39:48.398
        :param time_format:控制返回字串的格式,預設為:'YYYY-MM-DD HH:mm:ss.SSS'
        :return: 
        """
        import arrow as ar
        import requests as req
        #print('\n=========    獲取網路時間   =========')
try: res = req.get('http://shop.jsanycall.cn/').headers['Date'] # res = req.get('https://www.hao123.com/').headers['Date'] time_diff = ar.get(res[4:-4], 'DD MMM YYYY HH:mm:ss') - ar.now().floor('second') web_now_time = (ar.now() + time_diff).format(time_format)
#print('web_now_time={}'.format(web_now_time)) return web_now_time except BaseException as e: print('獲取網路時間出錯,出錯原因:{}'.format(e)) return -1

2022-03-20 09:29:46.379016
>>

def get_web_now_timea(time_format='YYYY-MM-DD HH:mm:ss.SSS'):
        """
        獲取網路時間,返回時間格式:2019-12-13 11:39:48.398
        :param time_format:控制返回字串的格式,預設為:'YYYY-MM-DD HH:mm:ss.SSS'
        :return: 
        
""" import arrow as ar import requests as req #print('\n========= 獲取網路時間 =========') try: res = req.get('http://shop.jsanycall.cn/').headers['Date'] # res = req.get('https://www.hao123.com/').headers['Date'] time_diff = ar.get(res[4:-4], 'DD MMM YYYY HH:mm:ss') - ar.now().floor('second') web_now_time = (ar.now() + time_diff).format(time_format) #print('web_now_time={}'.format(web_now_time)) return web_now_time except BaseException as e: print('獲取網路時間出錯,出錯原因:{}'.format(e)) return -1

2022-03-20 09:30:54.349

>>>

搜尋

複製