Python中的箭头模块
Arrow 是一个用于处理日期和时间的Python模块。它提供了一种合理且人性化的方法来创建、操作、格式化和转换日期、时间和时间戳。它允许轻松创建具有时区意识的日期和时间实例。
安装
使用以下命令安装箭头模块:
pip install arrow
特征
- 方便使用的。
- 默认情况下时区感知和 UTC。
- 时区转换。
- 时间范围从微秒到一年。
- 便于使用。
- 自动格式化和解析字符串。
- 支持不断增长的贡献语言环境列表。
获取 UTC(世界协调时间)时间。
为了获取当前的 UTC 时间,我们使用 utcnow() 方法。
Python3
# importing arrow module
import arrow
# getting UTC time
utc_time = arrow.utcnow()
# printing the current UTC time
print('Current UTC Time is =', utc_time)
Python3
# importing arrow module
import arrow
# getting current indian time
ind_time = arrow.now('Asia/Calcutta')
# printing the time
print('Current India Time =', ind_time)
Python3
# importing arrow module
import arrow
# date in string format
s ='2020-02-02 12:30:45'
# parsing string into date
date = arrow.get(s, 'YYYY-MM-DD HH:mm:ss')
# printing the date
print(date)
Python3
# importing arrow module
import arrow
# getting current utc time
utc = arrow.utcnow()
# printing the unix time
print(utc)
# getting unix time
unix_time = utc.timestamp
# printing unix time
print(unix_time)
# converting unix time into arrow date object
date = arrow.Arrow.fromtimestamp(unix_time)
# printing arrow dateobject
print(date)
Python3
# importing arrow module
import arrow
# importing datetime from datetime module
from datetime import datetime
# getting current time using datetime module
dt = datetime.now()
# creating arrow instance from datetime instance
arrow_dt = arrow.Arrow.fromdate(dt)
# printing datetime instance
print(dt)
# printing arrow instance
print(arrow_dt)
Python3
#import arrow module
import arrow
#Call datetime functions that return properties
a = arrow.utcnow()
print(a.time())
print(a.date())
#Get any datetime value
print(a.year)
Python3
#import arrow module
import arrow
# getting current utc time
a = arrow.utcnow()
# printing the unix time without alteration
print("without alteration: ",a)
# replacing only the hours to 5 and minutes to 30
b = a.replace(hour=5, minute=30)
print("with hours and minutes replaced: ",b)
# shifting forward in weeks
c = a.shift(weeks=+3)
print("with weeks shifted 3 forward: ",c)
# replacing only the timezone
d = a.replace(tzinfo='US/Pacific')
print("with timezone replaced: ",d)
Python3
#import arrow module
import arrow
#Humanize to past
apast = arrow.utcnow().shift(hours=-1)
print(apast.humanize())
#humanize to future
present = arrow.utcnow()
afuture = present.shift(hours=3)
print(afuture.humanize(present))
#Indicate a specific time granularity
afuture = present.shift(minutes=73)
print(afuture.humanize(present, granularity="minute"))
print(afuture.humanize(present, granularity=["hour", "minute"]))
输出 :
Current UTC Time is = 2020-02-28T18:06:39.228924+00:00
获取印度时间。
为了获得当前的区域(印度)时间,我们使用 now() 方法。
Python3
# importing arrow module
import arrow
# getting current indian time
ind_time = arrow.now('Asia/Calcutta')
# printing the time
print('Current India Time =', ind_time)
输出 :
Current India Time = 2020-02-28T23:40:07.112695+05:30
解析字符串到日期
为了将字符串解析为日期格式,我们使用 get() 方法。
Python3
# importing arrow module
import arrow
# date in string format
s ='2020-02-02 12:30:45'
# parsing string into date
date = arrow.get(s, 'YYYY-MM-DD HH:mm:ss')
# printing the date
print(date)
输出 :
2020-02-02T12:30:45+00:00
Unix 时间
Unix 时间是用于描述时间点的系统。它是自 Unix 纪元以来经过的秒数,即 1970 年 1 月 1 日 UTC 时间 00:00:00 减去闰秒。
- timestamp() 方法用于获取 unix 时间。
- fromtimestamp() 方法用于将 Unix 时间转换回箭头日期对象。
Python3
# importing arrow module
import arrow
# getting current utc time
utc = arrow.utcnow()
# printing the unix time
print(utc)
# getting unix time
unix_time = utc.timestamp
# printing unix time
print(unix_time)
# converting unix time into arrow date object
date = arrow.Arrow.fromtimestamp(unix_time)
# printing arrow dateobject
print(date)
输出 :
2020-03-04T13:33:15.041536+00:00
1583328795
2020-03-04T19:03:15+05:30
来自日期时间的箭头实例
箭头模块的实例也可以从 DateTime 模块创建。请考虑以下示例以更好地理解该主题。
Python3
# importing arrow module
import arrow
# importing datetime from datetime module
from datetime import datetime
# getting current time using datetime module
dt = datetime.now()
# creating arrow instance from datetime instance
arrow_dt = arrow.Arrow.fromdate(dt)
# printing datetime instance
print(dt)
# printing arrow instance
print(arrow_dt)
输出 :
2020-03-04 19:16:04.317690
2020-03-04T00:00:00+00:00
获取单个日期时间对象的属性
如果您想获取任何对象作为个体,这里有一些可以使用的属性。
Python3
#import arrow module
import arrow
#Call datetime functions that return properties
a = arrow.utcnow()
print(a.time())
print(a.date())
#Get any datetime value
print(a.year)
输出 :
datetime.time(19, 16, 04, 317690)
datetime.date(2020, 3, 4)
2020
替换和移位属性
如果您想单独替换或移动任何对象,这里有一些可以使用的属性。
Python3
#import arrow module
import arrow
# getting current utc time
a = arrow.utcnow()
# printing the unix time without alteration
print("without alteration: ",a)
# replacing only the hours to 5 and minutes to 30
b = a.replace(hour=5, minute=30)
print("with hours and minutes replaced: ",b)
# shifting forward in weeks
c = a.shift(weeks=+3)
print("with weeks shifted 3 forward: ",c)
# replacing only the timezone
d = a.replace(tzinfo='US/Pacific')
print("with timezone replaced: ",d)
输出 :
without alteration: 2020-03-04T13:33:15.041536+00:00
with hours and minutes replaced: 2020-03-04T05:30:15.041536+00:00
with weeks shifted 3 forward: 2020-03-25T13:33:15.041536+00:00
with timezone replaced: 2020-03-04T13:33:15.041536-07:00
人性化的格式
以上所有属性和函数输出都更像是一种计算机格式,但是如果您希望它更像是一种人类形式呢?例如:“一小时前”或“2小时前”,这里有一些属性可以用来实现人性化的格式。
Python3
#import arrow module
import arrow
#Humanize to past
apast = arrow.utcnow().shift(hours=-1)
print(apast.humanize())
#humanize to future
present = arrow.utcnow()
afuture = present.shift(hours=3)
print(afuture.humanize(present))
#Indicate a specific time granularity
afuture = present.shift(minutes=73)
print(afuture.humanize(present, granularity="minute"))
print(afuture.humanize(present, granularity=["hour", "minute"]))
输出 :
'an hour ago'
'in 3 hours'
'in 73 minutes'
'in an hour and 13 minutes'