import ntplib
import os,datetime
hosts = ['0.cn.pool.ntp.org','1.cn.pool.ntp.org','2.cn.pool.ntp.org','3.cn.pool.ntp.org']
hosts=['ntp.aliyun.com','ntp1.aliyun.com','ntp2.aliyun.com','ntp3.aliyun.com','ntp4.aliyun.com','ntp5.aliyun.com']
def ntp_client():
#hosts = ['0.cn.pool.ntp.org','1.cn.pool.ntp.org','2.cn.pool.ntp.org','3.cn.pool.ntp.org']
hosts=['ntp.aliyun.com','ntp1.aliyun.com','ntp2.aliyun.com','ntp3.aliyun.com','ntp4.aliyun.com','ntp5.aliyun.com']
#创建实例,NTPClient()是一个类
t = ntplib.NTPClient()
for host in hosts:
try:
#ntp server可以填写主机和域名,建议用域名
#缺省端口为ntp, 版本为2, 超时为5s
#作用:查询 NTP 服务器,并返回对象
r = t.request(host , port='ntp', version=4, timeout=5)
#r = t.request(host ,version=4, timeout=5)
if r:
break
except Exception as e:
pass
#显示的是时间戳
t = r.tx_time
#print(t)
#使用datetime模块,格式化:x年x月x日 时:分:秒.毫秒
_date,_time = str(datetime.datetime.fromtimestamp(t))[:22].split(' ')
print("调整前时间是:", datetime.datetime.now())
os.system('date {} && time {}'.format(_date, _time))
print("调整后时间是:", datetime.datetime.now())
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
def get_web_now_timeaa(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
def zf13stamp(ax):
ata=datetime.datetime.strptime(ax[:-4], "%Y-%m-%d %H:%M:%S")
att=get_time13(ata)
atx=att+int(ax[-3:])
return atx
def zf16stamp(ax):
ata=datetime.datetime.strptime(ax[:-7], "%Y-%m-%d %H:%M:%S")
att=get_time16(ata)
atx=att+int(ax[-6:])
return atx
def get_time13(datetime_obj):
import datetime, time
# 生成13时间戳 eg:1557842280000
datetime_str = datetime.datetime.strftime(datetime_obj, '%Y-%m-%d %H:%M:00')
datetime_obj = datetime.datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:00')
#print(datetime_obj)
# 10位,时间点相当于从1.1开始的当年时间编号
date_stamp = str(int(time.mktime(datetime_obj.timetuple())))
# 3位,微秒
data_microsecond = str("%06d" % datetime_obj.microsecond)[0:3]
date_stamp = date_stamp + data_microsecond
return int(date_stamp)
def get_time16(datetime_obj):
import datetime, time
# 生成13时间戳 eg:1557842280000
datetime_str = datetime.datetime.strftime(datetime_obj, '%Y-%m-%d %H:%M:00')
datetime_obj = datetime.datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:00')
#print(datetime_obj)
# 10位,时间点相当于从1.1开始的当年时间编号
date_stamp = str(int(time.mktime(datetime_obj.timetuple())))
# 3位,微秒
data_microsecond = str("%06d" % datetime_obj.microsecond)[0:6]
date_stamp = date_stamp + data_microsecond
return int(date_stamp)
if __name__ == '__main__':
#ntp_client()
aa=zf16stamp(get_web_now_timea())
print(aa)
aa=zf13stamp(get_web_now_timeaa())
print(aa)
1647741300520409
1647741300868
>>>
搜索
复制