代码
import pytzfrom datetime import datetimeimport time# 各时区实例utc = pytz.utcgeijing = pytz.timezone(“Asia/Shanghai”)pst = pytz.timezone(“US/Pacific”)tokyo = pytz.timezone(“Asia/Tokyo”)#时间戳loc_timestamp = time.time()print(“时间戳:%s” % loc_timestamp)# 转utc时间 datetime.datetime 类型utc_date = 便宜香港vps datetime.utcfromtimestamp(loc_timestamp)print(“UTC时间:%s” % utc_date)# 转utc当地 标识的时间utc_loc_time = utc.localize(utc_date)print(“UTC时间标记:%s” % utc_loc_time)fmt = ‘%Y-%m-%d %H:%M:%S %Z%z’# 转pst时间pst_time = utc_loc_time.astimezone(pst)print(“PST时间标记:%s” % pst_time)print(pst_time.strftime(fmt))# 转北京时间beijing_time = utc_loc_time.astimezone(geijing)print(“北京时间标记:%s” %beijing_time)print(beijing_time.strftime(fmt))
结果
时间戳:1594883228.5858855
UTC时间:2020-07-16 07:07:08.585886
UTC时间标记:2020-07-16 07:07:08.585886+00:00
PST时间标记:2020-07-16 00:07:08.585886-07:00
2020-07-16 00:07:08 PDT-0700
北京时间标记:2020-07-16 15:07:08.585886+08:00
2020-07-16 15:07:08 CST+0800
参考文章:
使用 pytz 处理 Python 中的时区问题
Python时间与日期操作(datetime、time、calendar)
python中时间、日期、时间戳的转换
98179072