python:pendulum日期时间处理

介绍

datetime 模块是 Python 中最重要的内置模块之一,用于时间处理,详见 链接

datetime 模块也有一些缺点,如:不支持时区处理

本文介绍 Pendulum 第三方库,目前是处理时间最好的库,完全可以替代 datetime 模块

安装

pip install pendulum

导入

import pendulum

基本使用

获取当前日期时间

import pendulum

now = pendulum.now() # 现在的时间,默认使用本机时区,即:Asia/Shangha
print( type(now) )
print(now)
print( now.timezone.name )

执行结果:

<class 'pendulum.datetime.DateTime'>
2023-04-15T16:48:59.868247+08:00
Asia/Shanghai

获取今天日期

d1 = pendulum.today() # 今天
print(d1)

执行结果:

2023-04-15T00:00:00+08:00

获取昨天日期

d2 = pendulum.yesterday()  # 昨天
print(d2)

执行结果:

2023-04-14T00:00:00+08:0

获取明天日期

d3 = pendulum.tomorrow() # 明天
print(d3)

执行结果:

2023-04-16T00:00:00+08:00

创建对象

声明

def datetime(
    year,  # type: int
    month,  # type: int
    day,  # type: int
    hour=0,  # type: int
    minute=0,  # type: int
    second=0,  # type: int
    microsecond=0,  # type: int
    tz=UTC,  # type: Optional[Union[str, float, _Timezone]]
    dst_rule=POST_TRANSITION,  # type: str
):

参数:

  • year:年
  • month:月
  • day:日
  • hour=0:时,默认值是0
  • minute=0:分,默认值是0
  • second=0:秒,默认值是0
  • microsecond=0:微妙,默认值是0。1秒(s)=1000毫秒(ms);1毫秒(ms)=1000微秒(μs)
  • tz=UTC:时区

例子

import pendulum

d1 = pendulum.datetime(2012, 1, 31, 9,20)
print(d1)

执行结果:

2012-01-31T09:20:00+00:00

转字符串

  • .to_date_string():转成日期字符串
  • .to_time_string():转成时间字符串
  • .to_datetime_string():转成日期时间字符串
import pendulum

now = pendulum.now()

print(now.to_date_string())
print(now.to_time_string())
print(now.to_datetime_string())

执行结果:

2023-04-15
17:41:58
2023-04-15 17:41:58

格式化成字符串

可自定义转换日期格式

.strftime(fmt)

参数:

  • fmt:同 datetime 模块的格式化方法,详见 链接

例子

import pendulum

now = pendulum.now()

t1 = now.strftime('%Y-%m-%d %H:%M:%S')

print(t1)

时间差

声明

d2.diff(d1)

参数:

  • dt:pendulum 对象

返回值: <class 'pendulum.period.Period'> 对象

Period 对象

  • in_years():相差多少年
  • in_months():相差多少月
  • in_weeks():相差多少周
  • in_days():相差多少天
  • in_hours():相差多少小时
  • in_minutes():相差多少分
  • in_seconds():相差多少秒

例子

import pendulum

d1 = pendulum.today() # 今天
d2 = pendulum.yesterday()  # 昨天

diff = d2.diff(d1)
print( type(diff) )
print(diff)

diff_day = diff.in_days()
print(diff_day)

diff_hour = diff.in_hours()
print(diff_hour)

执行结果:

<class 'pendulum.period.Period'>
<Period [2023-04-14T00:00:00+08:00 -> 2023-04-15T00:00:00+08:00]>
1
24

日期增减

时间增加或者减少某些时间,可以使用 addsubtract 方法

增加

def add(
    years=0,
    months=0,
    weeks=0,
    days=0,
    hours=0,
    minutes=0,
    seconds=0,
    microseconds=0,
)
def subtract(
    years=0,
    months=0,
    weeks=0,
    days=0,
    hours=0,
    minutes=0,
    seconds=0,
    microseconds=0,
)

例子

import pendulum

dt = pendulum.now()

dt = dt.add(years=5)
# '2017-01-31 00:00:00'
dt = dt.add(years=1)
# '2018-01-31 00:00:00'
dt = dt.subtract(years=1)
# '2017-01-31 00:00:00'
dt = dt.subtract(years=5)
# '2012-01-31 00:00:00'

dt = dt.add(months=60)
# '2017-01-31 00:00:00'
dt = dt.add(months=1)
# '2017-02-28 00:00:00'
dt = dt.subtract(months=1)
# '2017-01-28 00:00:00'
dt = dt.subtract(months=60)
# '2012-01-28 00:00:00'

dt = dt.add(days=29)
# '2012-02-26 00:00:00'
dt = dt.add(days=1)
# '2012-02-27 00:00:00'
dt = dt.subtract(days=1)
# '2012-02-26 00:00:00'
dt = dt.subtract(days=29)
# '2012-01-28 00:00:00'

dt = dt.add(weeks=3)
# '2012-02-18 00:00:00'
dt = dt.add(weeks=1)
# '2012-02-25 00:00:00'
dt = dt.subtract(weeks=1)
# '2012-02-18 00:00:00'
dt = dt.subtract(weeks=3)
# '2012-01-28 00:00:00'

dt = dt.add(hours=24)
# '2012-01-29 00:00:00'
dt = dt.add(hours=1)
# '2012-02-25 01:00:00'
dt = dt.subtract(hours=1)
# '2012-02-29 00:00:00'
dt = dt.subtract(hours=24)
# '2012-01-28 00:00:00'

dt = dt.add(minutes=61)
# '2012-01-28 01:01:00'
dt = dt.add(minutes=1)
# '2012-01-28 01:02:00'
dt = dt.subtract(minutes=1)
# '2012-01-28 01:01:00'
dt = dt.subtract(minutes=24)
# '2012-01-28 00:00:00'

dt = dt.add(seconds=61)
# '2012-01-28 00:01:01'
dt = dt.add(seconds=1)
# '2012-01-28 00:01:02'
dt = dt.subtract(seconds=1)
# '2012-01-28 00:01:01'
dt = dt.subtract(seconds=61)
# '2012-01-28 00:00:00'

dt = dt.add(years=3, months=2, days=6, hours=12, minutes=31, seconds=43)
# '2015-04-03 12:31:43'
dt = dt.subtract(years=3, months=2, days=6, hours=12, minutes=31, seconds=43)
# '2012-01-28 00:00:00'

设置时区

import pendulum

dt1 = pendulum.datetime(2021, 10, 3) # 默认 UTC 时区
print(dt2.timezone.name) # UTC

dt2 = pendulum.datetime(2021, 10, 3, tz="Asia/Shanghai") # 指定上海时区

print(dt2.timezone.name) # Asia/Shanghai

执行结果:

UTC
Asia/Shanghai

参考:
https://zhuanlan.zhihu.com/p/420561184
https://zhuanlan.zhihu.com/p/432807711


原文出处:https://malaoshi.top/show_1IX5K6SxIPmj.html