python中arrow庫(kù)用法大全
首先需要安裝arrow庫(kù):
pip install arrow

Arrow提供了一個(gè)合理的、人性化的方法來創(chuàng)建、操作、格式轉(zhuǎn)換的日期,時(shí)間,和時(shí)間戳,幫助我們使用較少的導(dǎo)入和更少的代碼來處理日期和時(shí)間。
獲取本地和世界標(biāo)準(zhǔn)時(shí)間:
示例代碼:
import arrow
# 獲取世界標(biāo)準(zhǔn)時(shí)間
utc_time = arrow.utcnow()
print(utc_time)
# 獲取本地時(shí)間
local_time = arrow.now()
print(local_time)
# 獲取指定時(shí)區(qū)的時(shí)間
US_time = arrow.now('US/Pacific')
print(US_time)運(yùn)行結(jié)果:

將時(shí)間戳轉(zhuǎn)化為arrow對(duì)象:arrow.get(timestamp)
示例代碼:
import time import arrow float_timestamp = time.time() print(float_timestamp) time_tmp = arrow.get(float_timestamp) print(time_tmp) int_timestamp = int(time.time()) print(int_timestamp) time_tmp2 = arrow.get(int_timestamp) print(time_tmp2)
運(yùn)行結(jié)果:

時(shí)間戳可以是int,float或者可以轉(zhuǎn)化為float的字符串
將字符串轉(zhuǎn)換為arrow對(duì)象:arrow.get(string[,format_string])
示例代碼:
import arrow
aa = arrow.get("2022-08-17 20:00:00", "YYYY-MM-DD HH:mm:ss")
print(aa)運(yùn)行結(jié)果:

可以從字符串中通過格式參數(shù)搜索時(shí)間
示例代碼:
import arrow
aa = arrow.get("My birthday is in May 1995", "MMMM YYYY")
print(aa)運(yùn)行結(jié)果:

也可以直接創(chuàng)建arrow對(duì)象
示例代碼:
import arrow aa = arrow.get(2022, 8, 17) print(aa)
運(yùn)行結(jié)果:

arrow對(duì)象屬性:datetime,timestamp,native,tzinfo
示例代碼:
import arrow aa = arrow.utcnow() print(aa) print(aa.datetime) print(aa.timestamp) print(aa.naive) print(aa.tzinfo) print(type(aa.datetime)) print(type(aa.timestamp)) print(type(aa.naive)) print(type(aa.tzinfo))
運(yùn)行結(jié)果:

按名稱或tzinfo轉(zhuǎn)換為時(shí)區(qū)
示例代碼:
import arrow
aa = arrow.utcnow()
print(aa)
bb = aa.to('US/Pacific')
print(bb)運(yùn)行結(jié)果:

獲取datetime對(duì)象的值
示例代碼:
import arrow aa = arrow.now() print(aa) print(aa.year) # 當(dāng)前年 print(aa.month) # 當(dāng)前月份 print(aa.day) # 當(dāng)前天 print(aa.hour) # 當(dāng)前第幾個(gè)小時(shí) print(aa.minute) # 當(dāng)前多少分鐘 print(aa.second) # 當(dāng)前多少秒 print(aa.timestamp) # 獲取時(shí)間戳 print(aa.float_timestamp) # 浮點(diǎn)數(shù)時(shí)間戳
運(yùn)行結(jié)果:

時(shí)間推移a.shift(**kwargs):shift方法獲取某個(gè)時(shí)間之前或之后的時(shí)間,關(guān)鍵字參數(shù)為years, months, weeks, days, hours,seconds,microseconds
示例代碼:
import arrow aa = arrow.now() print(aa) # 當(dāng)前時(shí)間 print(aa.shift(weeks=-3)) # 三周前 # print(aa.shift(weeks=3)) # 三周后 print(aa.shift(weeks=+3)) # 三周后 print(aa.shift(days=-1)) # 一天前 print(aa.shift(days=1)) # 一天后 print(aa.shift(weekday=6)) # 距離最近aa的星期日,weekday從0到6
運(yùn)行結(jié)果:

時(shí)間替換a.replace(**kwargs):返回一個(gè)被替換后的arrow對(duì)象,原對(duì)象不變
示例代碼:
import arrow
aa = arrow.now()
print(aa)
print(aa.replace(hour=6))
print(aa.replace(year=2023, month=3, day=6, hour=12, minute=12, second=12))
print(aa.replace(year=2023, month=3, day=6, hour=12, minute=12, second=12, microsecond=12))
print(aa.replace(year=2023, month=3, day=6, hour=12, minute=12, second=12, microsecond=12).to('UTC'))
print(aa.replace(year=2023, month=3, day=6, hour=12, minute=12, second=12, microsecond=12).to('local'))
print(aa.replace(year=2023, month=3, day=6, hour=12, minute=12, second=12, microsecond=12).to('local').naive)運(yùn)行結(jié)果:

格式化輸出: a.format([format_string])
示例代碼:
import arrow
aa = arrow.now()
print(aa)
print(aa.format())
print(aa.format('YYYY-MM-DD HH:mm:ss ZZ'))
print(aa.ctime()) # 返回日期和時(shí)間的ctime格式化表示
print(aa.weekday()) # 以整數(shù)形式返回星期幾(0-6)
print(aa.isoweekday()) # 以整數(shù)形式返回一周中的ISO日(1-7)
print(aa.isocalendar()) # 返回3元組(ISO年,ISO周數(shù),ISO工作日)
print(aa.toordinal()) # 返回日期的格雷戈里序數(shù)運(yùn)行結(jié)果:

人性化輸出:a.humanize()
示例代碼:
import arrow aa = arrow.now() print(aa.shift(hours=-1)) print(aa.shift(hours=-1).humanize()) # 相對(duì)于當(dāng)前時(shí)間 print(aa.shift(hours=2)) print(aa.shift(hours=2).humanize()) # 相對(duì)于參數(shù)時(shí)間 print(aa.shift(hours=2).humanize(locale='zh')) # locale參數(shù)可以指定地區(qū)語言
運(yùn)行結(jié)果:

獲取任意時(shí)間單位的時(shí)間跨度
示例代碼:
import arrow
aa = arrow.utcnow().span('hour')
print(aa)
bb = arrow.utcnow().span('year')
print(bb)
cc = arrow.utcnow().span('month')
print(cc)
dd = arrow.utcnow().span('day')
print(dd)運(yùn)行結(jié)果:

只得到任意單位時(shí)間中的最大值或最小值
示例代碼:
import arrow
aa = arrow.utcnow().floor('hour')
print(aa)
bb = arrow.utcnow().ceil('hour')
print(bb)
cc = arrow.utcnow().floor('day')
print(cc)
dd = arrow.utcnow().ceil('day')
print(dd)運(yùn)行結(jié)果:

到此這篇關(guān)于python中arrow庫(kù)用法詳解的文章就介紹到這了,更多相關(guān)python中arrow庫(kù)用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python arrow模塊使用方法
- 使用python?dateutil庫(kù)輕松處理日期和時(shí)間
- Python?dateutil庫(kù)簡(jiǎn)化日期時(shí)間處理利器使用場(chǎng)景實(shí)踐
- python使用datetime模塊處理日期時(shí)間及常用功能詳解
- Python時(shí)間處理模塊Time和DateTime
- 封裝?Python?時(shí)間處理庫(kù)創(chuàng)建自己的TimeUtil類示例
- python簡(jiǎn)單幾步實(shí)現(xiàn)時(shí)間日期處理到數(shù)據(jù)文件的讀寫
- Python Arrow處理時(shí)間數(shù)據(jù)使用詳解(標(biāo)準(zhǔn)庫(kù)之外另一種選擇)
相關(guān)文章
Python遠(yuǎn)程控制Windows服務(wù)器的方法詳解
在很多企業(yè)會(huì)使用閑置的 Windows 機(jī)器作為臨時(shí)服務(wù)器,有時(shí)候我們想遠(yuǎn)程調(diào)用里面的程序或查看日志文件。本文分享了利用Python遠(yuǎn)程控制Windows服務(wù)器的方法,感興趣的可以學(xué)習(xí)一下2022-05-05
Pygame鼠標(biāo)進(jìn)行圖片的移動(dòng)與縮放案例詳解
pygame是Python的第三方庫(kù),里面提供了使用Python開發(fā)游戲的基礎(chǔ)包。本文將介紹如何通過Pygame實(shí)現(xiàn)鼠標(biāo)進(jìn)行圖片的移動(dòng)與縮放,感興趣的可以關(guān)注一下2021-12-12
Python+threading模塊對(duì)單個(gè)接口進(jìn)行并發(fā)測(cè)試
這篇文章主要為大家詳細(xì)介紹了Python+threading模塊對(duì)單個(gè)接口進(jìn)行并發(fā)測(cè)試,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
Python如何讀取16進(jìn)制byte數(shù)據(jù)
這篇文章主要介紹了Python如何讀取16進(jìn)制byte數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
Python命令行定時(shí)任務(wù)自動(dòng)化工作流程
本文介紹如何使用Python編寫定時(shí)任務(wù),以自動(dòng)執(zhí)行命令行任務(wù)。您將學(xué)習(xí)如何安排定期的任務(wù),處理任務(wù)結(jié)果,以及如何使用Python自動(dòng)化工作流程,從而提高工作效率。無需手動(dòng)執(zhí)行重復(fù)任務(wù),Python幫您搞定2023-04-04
Mac PyCharm中的.gitignore 安裝設(shè)置教程
這篇文章主要介紹了Mac PyCharm中的.gitignore 安裝設(shè)置教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Python3數(shù)據(jù)庫(kù)操作包pymysql的操作方法
這篇文章主要介紹了Python3數(shù)據(jù)庫(kù)操作包pymysql的操作方法,文章通過實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-07-07
python opencv 找出圖像中的最大輪廓并填充(生成mask)
這篇文章主要介紹了python opencv 找出圖像中的最大輪廓并填充(生成mask),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

