python 时间库之pendulum
Pendulum: 掌握时间的艺术,让Python日期时间操作不再复杂
第一部分:背景介绍
在Python开发中,处理日期和时间是一个常见但复杂的任务。datetime模块虽然功能强大,但使用起来不够直观。Pendulum 库的出现,就是为了简化这一过程,它提供了更人性化的API来处理日期和时间。
第二部分:Pendulum是什么?
Pendulum是一个Python包,旨在简化日期时间的操作。它强制使用时区感知的日期时间,这是使用库的推荐方式。
第三部分:如何安装Pendulum?
通过命令行安装Pendulum非常简单,只需使用pip:
pip install pendulum
这条命令会从Python包索引中下载并安装Pendulum
库。
第四部分:简单的库函数使用方法
now()
获取当前的日期和时间。
import pendulum now = pendulum.now() print(now) # 输出类似于 '2016-06-28T16:51:45.978473-05:00'
today()
获取今天日期的开始时间(00:00:00)。
today = pendulum.today() print(today) # 输出类似于 '2016-06-28T00:00:00-05:00'
tomorrow()
获取明天日期的开始时间(00:00:00)。
tomorrow = pendulum.tomorrow('Europe/London') print(tomorrow) # 输出类似于 '2016-06-29T00:00:00+01:00'
yesterday()
获取昨天日期的开始时间(00:00:00)。
yesterday = pendulum.yesterday() print(yesterday) # 输出类似于 '2016-06-27T00:00:00-05:00'
parse()
解析日期时间字符串。
dt = pendulum.parse('1975-05-21T22:00:00') print(dt) # 输出 '1975-05-21T22:00:00+00:00'
这些函数展示了Pendulum
的基本用法,每个函数都对应一个具体的日期时间操作。
to_date_string()指定日期
to_time_string() 指定时间
import pendulum # 1.创建一个当前日期和时间对象 now = pendulum.now() print("当前日期和时间:", now.to_date_string(), now.to_time_string()) #结果: 当前日期和时间: 2025-01-02 17:06:08
# 2.创建一个指定日期和时间对象
dt = pendulum.datetime(2023, 10, 5, 14, 30, 0)
print("指定日期和时间:", dt.to_date_string(), dt.to_time_string())
#结果: 指定日期和时间: 2023-10-05 14:30:00
# 格式化日期和时间
formatted_dt = dt.format('YYYY-MM-DD HH:mm:ss')
print("格式化后的日期和时间:", formatted_dt)
#结果:格式化后的日期和时间: 2023-10-05 14:30:00
各种函数的使用:
# 创建当前时间对象 now = pendulum.now() print('now:',now) print("datetime:", now.to_datetime_string()) print("date:", now.to_date_string()) print("time:", now.to_time_string()) print("iso8601:", now.to_iso8601_string()) #结果: now: 2025-01-02T17:13:16.283939+08:00 datetime: 2025-01-02 17:13:16 date: 2025-01-02 time: 17:13:16 iso8601: 2025-01-02T17:13:16.283939+08:00
加减天数支持:
# 加减天数支持 ''' years=0, months=0, weeks=0, days=0, hours=0, minutes=0, seconds=0, microseconds=0 ''' #增加天数 print("now + 1 天",now.add(days=1)) #减少天数 print("now - 1 天",now.add(days=-1)) # 转时间戳 time_stamp = now.timestamp() print('时间戳:',time_stamp) # 创建指定时间对象 dt = pendulum.datetime(2023, 1, 1, 12, 0, 0) print(dt) # 日期格式化 print(dt.format('YYYY-MM-DD HH:mm:ss')) # 求差值 diff = now - dt print('2023年1-1 12:00.0 和现在差了多少秒:',diff.in_seconds())
创建指定时区的时间对象:
# 创建指定时区的时间对象 paris_now = pendulum.now('Europe/Paris') print('paris_now: ',paris_now) print('zone info:',paris_now.tzinfo) print('打印时区:',paris_now.format('Z'))
#结果:
paris_now: 2025-01-02T10:17:14.967905+01:00
zone info: Timezone('Europe/Paris')
打印时区: +01:00
# 日期格式:
dt = pendulum.parse('2023-01-01T12:00:00Z') print('转时间pendulum类型:', dt)
#结果:
转时间pendulum类型: 2023-01-01T12:00:00+00:00
第五部分:结合场景使用Pendulum
场景1:计算两个日期之间的差异
dt1 = pendulum.datetime(2000, 1, 1) dt2 = pendulum.datetime(2000, 1, 31) diff = dt2.diff(dt1).in_days() print(diff) # 输出 30
场景2:处理时区转换
dt = pendulum.datetime(2016, 8, 7, 22, 24, 30, tz='Europe/Paris') print(dt.in_timezone('America/New_York')) # 输出 '2016-08-07T16:24:30-04:00'
场景3:日期时间的格式化
dt = pendulum.datetime(1975, 5, 21) print(dt.format('dddd DD MMMM YYYY')) # 输出 'Wednesday 21 May 1975'
这些场景展示了Pendulum
在实际开发中的应用,包括日期差异计算、时区转换和格式化。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)