怎么查看localtime的时区?
解答
print( time.strftime("%Z", time.localtime()))
import time
import datetime
import pytz
from datetime import tzinfo, timezone
time1 = '2018-01-17 11:12:14Z'
time2 = '2018-01-17 11:12:14+08:00'
#将时间戳转换成 年 月 日
timeStamp = 1516187490
form1 = time.localtime(timeStamp)
form2 = time.strftime("%Y年%m月%d日", form1)
print(1, form2)
#求出不同时区的时间戳时间
print(9, datetime.datetime.fromtimestamp(timeStamp, pytz.utc))
print(10, datetime.datetime.fromtimestamp(timeStamp, pytz.timezone('US/Pacific')))
print(10, datetime.datetime.fromtimestamp(timeStamp, pytz.timezone('Asia/Shanghai')))
#算出utc时间并在utc时间上加上2
form3 = datetime.datetime.utcnow()
print(2, form3)
form4 = form3 + datetime.timedelta(hours=2)
print(3, form4)
#将2017-07-18转换成2017年07月18日
form5 = time.strptime('2017-07-18', "%Y-%m-%d")
form6 = time.strftime("%Y年%m月%d日", form5)
print(5, form6)
#求出上海时区的现在的时间
form8 = datetime.datetime.now()
print(7, form8)
print(11, datetime.datetime.now(pytz.timezone('Asia/Shanghai')))
#转换各个时区的格式
# 'Asia/Shanghai'
# UTC
# PST
date = form8.astimezone(tz=pytz.utc)
date_1 = date.tzname()
print(8, date, date_1)
date1 = form8.astimezone(pytz.timezone('Asia/Shanghai'))
date1_1 = date1.tzname()
print(9, date1, date1_1)
date2 = form8.astimezone(tz=pytz.timezone('US/Pacific'))
date2_2 = date2.tzname()
print(10, date2, date2_2)