Python 相关备忘:


  1. 将UTC时间转换为带有TZ格式的标准时间:

now_time = datetime.datetime.utcnow()+datetime.timedelta(hours=8)
now_time.strftime('%Y-%m-%dT%H:%M:%S.%f%z')

 2. 获取当前和七天前UTC格式时间戳

import datetime
last_7day=datetime.datetime.utcnow()-datetime.timedelta(days=6, hours=16)
now_time=datetime.datetime.utcnow()+datetime.timedelta(hours=8)
last_7day=last_7day.strftime('%Y-%m-%dT%H:%M:%S.%f%z')
now_time=now_time.strftime('%Y-%m-%dT%H:%M:%S.%f%z')

 3. datetime转换为UNIX时间戳

import time
import datetime
dtime = datetime.datetime.now()
ans_time = time.mktime(dtime.timetuple())

print (int(ans_time))                  #秒级时间戳
print (int(round(ans_time * 1000)))    #毫秒级时间戳
print (int(round(ans_time * 1000000))) #微秒级时间戳