Python获取每天零点的时间戳
写这个的目的主要是主机本地的时间容易被修改,所以通过连接网页获取时间,再进行分解!
import http.client
import time
def update_time_show():
host = 'www.jd.com'
conn = http.client.HTTPConnection(host)
conn.request("GET", "/")
r = conn.getresponse()
ts = r.getheader('date') # 获取http头date部分
ltime = time.strptime(ts[5:25], "%d %b %Y %H:%M:%S")
ttime = time.localtime(time.mktime(ltime) + 8 * 60 * 60)
day_time_to_o = "%02u-%02u-%02u 00:00:00" % (ttime.tm_year, ttime.tm_mon, ttime.tm_mday)
stamp = time.mktime(time.strptime(day_time_to_o, '%Y-%m-%d %H:%M:%S'))
print(int(stamp),'||',day_time_to_o)
return stamp
update_time_show()
显示效果:
1617292800 || 2021-04-02 00:00:00