Python库之Pendulum使用指南
Python 中有许多库可用于日期时间,比如 Pendulum 库在日期的任何操作上都非常简单易操作。
Pendulum 库扩展了Python内置的日期时间模块,添加了更直观易用的API接口用于处理时区并对日期和时间执行操作。
1、安装
pip install pendulum
2,使用示例
实例化时区:
import pendulum
dt = pendulum.datetime(2023, 9, 9)
使用本地时区:
local = pendulum.local(2023, 9, 9)
print("本地时间:", local)
print("本地时区:", local.timezone.name)
输出:
本地时间:2023-09-09T00:00:00+08:00
本地时区:Asia/Shanghai
创建日期时间实例
utc = pendulum.now('UTC')
print("Current UTC time:", utc)
输出:
Current UTC time: 2023-09-09T10:44:51.856673+00:00
将 UTC 时区转换为巴黎时间
europe_timezone = utc.in_timezone('Europe/Paris')
print("巴黎当前时间:", europe_timezone)
输出:
Current UTC time: 2023-09-09T10:47:27.836789+00:00
Current time in Paris: 2023-09-09T12:47:27.836789+02:00
我的笔记