python模块arrow_Python Arrow模块

python模块arrow

Python Arrow module is a replacement library for datetime. It allows easy creation of date and time instances with timezone awareness. It’s a simple module with a human-friendly approach to creating, manipulating, formatting and converting dates, times, and timestamps.

Python Arrow模块是datetime的替代库。 它可以轻松创建具有时区意识的日期和时间实例。 这是一个简单的模块,采用人性化的方法来创建,操作,格式化和转换日期,时间和时间戳。

Python Arrow模块 (Python Arrow Module)

We can install python arrow module using PIP command.

我们可以使用PIP命令安装python arrow模块。

pip install arrow

Python arrow module is Timezone-aware & UTC by default. It provides support for creating dates from arguments, timestamp etc. We can easily perform date time manipulations, converting one timezone to another, shift time to get future or past date, format date to string and parse a string to create date instance.

Python箭头模块默认为时区感知和UTC。 它提供了从参数,时间戳等创建日期的支持。我们可以轻松地执行日期时间操作,将一个时区转换为另一个时区,转换时间以获取未来或过去的日期,将日期格式化为字符串并将其解析为字符串以创建日期实例。

Python箭头示例 (Python Arrow Example)

Let’s see how to use arrow module to get current UTC time, IST time and local time.

让我们看看如何使用箭头模块获取当前UTC时间,IST时间和本地时间。

utc_time = arrow.utcnow()
print('Current UTC Time =', utc_time)

ist_time = arrow.now('Asia/Calcutta')
print('Current IST Time =', ist_time)
print('tzinfo =', ist_time.tzinfo)

local_time = arrow.now()
print('Current Local Time =', local_time)

Output:

输出:

Current UTC Time = 2018-09-26T06:16:54.724068+00:00
Current IST Time = 2018-09-26T11:46:54.724375+05:30
tzinfo = tzfile('/usr/share/zoneinfo/Asia/Calcutta')
Current Local Time = 2018-09-26T11:46:54.724472+05:30

转换时区 (Converting Timezone)

We can use to() function to convert one timezone to another.

我们可以使用to()函数将一个时区转换为另一个时区。

pst_time = ist_time.to('US/Pacific')
print('Current PST Time =', pst_time)

Output:

输出:

Current PST Time = 2018-09-25T23:16:54.724375-07:00

迄今时间戳记 (Date to Timestamp to Date)

print('Current Local Timestamp =', local_time.timestamp)

dt = arrow.get(1537941232)
print('Date from Timestamp =', dt)

Output:

输出:

Current Local Timestamp = 1537942614
Date from Timestamp = 2018-09-26T05:53:52+00:00

格式化字符串的日期 (Date to Formatted String)

print('Formatted Date =', local_time.format())
print('Specific Formatted Date =', local_time.format('YYYY-MM-DD HH:mm:ss ZZ'))

Output:

输出:

Formatted Date = 2018-09-26 11:46:54+05:30
Specific Formatted Date = 2018-09-26 11:46:54 +05:30

解析字符串到日期 (Parse String to Date)

dt = arrow.get('2013-05-05 12:30:45', 'YYYY-MM-DD HH:mm:ss')
print(type(dt))
print(dt)

Output:

输出:

<class 'arrow.arrow.Arrow'>
2013-05-05T12:30:45+00:00

从参数实例化日期 (Instantiating Date from Arguments)

dt = arrow.get(2018, 9, 26)
print(dt)

Output: 2018-09-26T00:00:00+00:00

输出: 2018-09-26T00:00:00+00:00

日期时间操作 (Date Time Manipulations)

We can use replace() and shift() function to get future and past dates.

我们可以使用replace()和shift()函数来获取将来和过去的日期。

utc_time = arrow.utcnow()
print('Current UTC Time =', utc_time)
utc_time_updated = utc_time.replace(year=2019, month=6)
print('Updated UTC Time =', utc_time_updated)
utc_time_updated = utc_time.shift(years=-2, weeks=4)
print('Updated UTC Time =', utc_time_updated)

Output:

输出:

Current UTC Time = 2018-09-26T06:16:54.727167+00:00
Updated UTC Time = 2019-06-26T06:16:54.727167+00:00
Updated UTC Time = 2016-10-24T06:16:54.727167+00:00

相对日期的可读格式 (Relative Date to Human Readable Format)

past = arrow.utcnow().shift(hours=-1)
print(past.humanize())
future = arrow.utcnow().shift(hours=+1)
print(future.humanize())
print(future.humanize(locale='de_DE'))
print(future.humanize(past))

Output:

输出:

an hour ago
in an hour
in einer Stunde
in 2 hours

从日期时间创建Arrow实例 (Creating Arrow instance from datetime)

from datetime import datetime
dt = datetime.now()
arrow_dt = arrow.Arrow.fromdate(dt)
print(dt)
print(arrow_dt)

Output:

输出:

2018-09-26 12:34:57.532227
2018-09-26T00:00:00+00:00

That’s all for a brief introduction of python arrow module. There are many other functions available in this module, you can get complete details from their documentation.

这就是python arrow模块的简要介绍。 该模块中还有许多其他功能,您可以从其文档中获取完整的详细信息。

摘要 (Summary)

Python arrow module is a drop-in replacement for built-in datetime module. It’s timezone aware and there are many utility functions to help us in date time operations. It’s very similar to python pendulum module.

Python箭头模块是内置datetime模块的直接替代。 它了解时区,并且有许多实用程序功能可以帮助我们进行日期时间操作。 它与python pendulum模块非常相似。

GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。

翻译自: https://www.journaldev.com/23412/python-arrow-module

python模块arrow

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值