Python时间操作之pytz模块如何使用
這篇文章主要講解了“Python時(shí)間操作之pytz模塊如何使用”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Python時(shí)間操作之pytz模塊如何使用”吧!
1. pytz 模塊概述
什么是 pytz 模塊
pytz 模塊是依賴Olson tz數(shù)據(jù)庫導(dǎo)入的,它支持直接使用時(shí)區(qū)名進(jìn)行時(shí)間計(jì)算
pytz 模塊涉及時(shí)區(qū),因此其也指定tzinfo信息(詳情可見datetime.tzinfo)
pytz 模塊通常與datetime模塊結(jié)合一起使用,返回具體的時(shí)間名
pytz 模塊可以解決夏令時(shí)結(jié)束時(shí)不明確的問題
重要說明:
pytz 模塊支持大多數(shù)的時(shí)區(qū)計(jì)算,使用IANA的數(shù)據(jù)接口,CLDR(Unicode 語言環(huán)境)項(xiàng)目提供翻譯
本地還需要按照依賴是時(shí)區(qū)映射表tzdata數(shù)據(jù)庫(pip install tzdata)
國家時(shí)區(qū)映射關(guān)系表
國家/城市代碼映射表,pytz庫中存儲(chǔ)在_CountryTimezoneDict()字典中
我們可以通過 pytz.country_timezones常量來獲取code,timezon
<pytz._CountryTimezoneDict object at 0x00000256FBE52E30>
pytz 模塊使用方法
由于pytz是第三方庫,因此我們?cè)谑褂们靶枰褂胮ip進(jìn)行下載其依賴庫
pipinstallpytz
代碼中使用時(shí),我們需要使用import來進(jìn)行導(dǎo)入
#方式一:導(dǎo)入整個(gè)模塊 importpytz #方式二:導(dǎo)入具體的庫 frompytzimporttimezone
2. pytz 相關(guān)方法
pytz 模塊包含國家碼查詢、時(shí)區(qū)名等方法
創(chuàng)建本地化時(shí)間:
方式一:pytz.timezone(tzname).localise()
tz=pytz.timezone('US/Eastern')
local_time=tz.localize(datetime.datetime(2022,6,13,23,0,0))
print(local_time)
方式二:local_time.astimezone(tzname)
ast=local_time.astimezone(tz)
方式三:tz.normzlize()處理夏令時(shí)
nor=tz.normzlize(datetime.datetime(2022,6,13,23,0,0))
時(shí)區(qū)名獲取:
-
時(shí)區(qū)名各式化:pytz.timezone(tzname)
-
獲取所有的時(shí)區(qū):pytz.country_timezones.values()
-
獲取地區(qū)的代碼:pytz.country_timezones.keys()
3. pytz 時(shí)區(qū)查詢
根據(jù)pytz模塊相關(guān)方法,我們可以寫一個(gè)函數(shù)來實(shí)現(xiàn)場(chǎng)景:
-
輸入一個(gè)城市:city,如"Simferopol"
-
輸出城市的時(shí)區(qū)偏離量:如+3
實(shí)現(xiàn)思路,大致如下:
-
首先調(diào)用pytz.country_timezones.values()獲取到所有的時(shí)區(qū)timezones
-
使用split()將時(shí)區(qū)的城市名進(jìn)行分割形成列表city_list
-
先在city_list.index[city]找到City_index
-
然后根據(jù)City_index在timezones找到時(shí)區(qū)tzname
-
pytz.timezone(tzname)格式化,算出標(biāo)準(zhǔn)時(shí)間
importpytz
fromdatetimeimportdatetime
deftimezon_city_gmt(city):
timezons=sum(list(pytz.country_timezones.values()),[])
cityList=[city.split("/")[1]forcityintimezons]
city_index=cityList.index(city)
tz=pytz.timezone(timezons[city_index])
gmt="GMT"+str(datetime.now().astimezone(tz))[-6:]
returngmt
print(timezon_city_gmt("Simferopol"))
---
GMT+03:00
---
4. pytz 日期計(jì)算
同理,我們?nèi)粘I钪懈鶕?jù)當(dāng)?shù)貢r(shí)間,算出對(duì)方所在時(shí)區(qū)的當(dāng)?shù)貢r(shí)間,思路與上述大致一樣。
datetime.strptime()將時(shí)間字符串轉(zhuǎn)化成datetime對(duì)象
importpytz
fromdatetimeimportdatetime
defupdate_datetime_tz(olddatetime,city,formate):
timezons=sum(list(pytz.country_timezones.values()),[])
cityList=[city.split("/")[1]forcityintimezons]
city_index=cityList.index(city)
tz=pytz.timezone(timezons[city_index])
datetime_type=datetime.strptime(olddatetime,formate)
newdatetime=datetime_type.astimezone(tz)
returnnewdatetime.strftime(str(formate))
print(update_datetime_tz("2022-06-1312:46:03","Moscow","%Y-%m-%d%H:%M:%S"))
---
2022-06-1307:46:03
---
總結(jié)
以上是生活随笔為你收集整理的Python时间操作之pytz模块如何使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Atitti dbutil获取多个返回结
- 下一篇: SheetJS中文文档-js导出Exce