Python Pendulum进行日期时间处理的示例详解
作者:小龙在山东
Pendulum 是对 Python datetime 的继承和发展,让日期时间处理更简单,这篇文章主要为大家详细介绍了Pendulum的具体应用,需要的可以参考下
Pendulum 是对 Python datetime 的继承和发展,让日期时间处理更简单,比 datatime 更加人性化,支持 Python 3.9 及以上版本。
安装
$ pip install pendulum
创建 datetime 对象
import pendulum dt = pendulum.datetime(2025, 2, 5) isinstance(dt, datetime) #True dt.timezone.name #'UTC'
设置时区
import pendulum
pendulum.datetime(2015, 2, 5, tz='Asia/Shanghai')
tz = pendulum.timezone('Asia/Shanghai')
pendulum.datetime(2025, 2, 5, tz=tz)
当前 datetime、今天、昨天、明天
now = pendulum.now()
print(now)
#'2016-06-28T16:51:45.978473-05:00'
today = pendulum.today()
print(today)
#'2016-06-28T00:00:00-05:00'
tomorrow = pendulum.tomorrow('Europe/London')
print(tomorrow)
#'2016-06-29T00:00:00+01:00'
yesterday = pendulum.yesterday()
print(yesterday)
#'2016-06-27T00:00:00-05:00'
# 从格式化字符串创建datetime
dt = pendulum.from_format('1975-05-21 22', 'YYYY-MM-DD HH')
print(dt)
#'1975-05-21T22:00:00+00:00'
dt = pendulum.from_format('1975-05-21 22', 'YYYY-MM-DD HH', tz='Asia/Shanghai')
#'1975-05-21T22:00:00+01:00'
从时间戳创建
dt = pendulum.from_timestamp(-1) print(dt) #'1969-12-31T23:59:59+00:00' dt = pendulum.from_timestamp(-1, tz='Europe/London') print(dt) #'1970-01-01T00:59:59+01:00'
从实例创建
dt = datetime(2008, 1, 1) p = pendulum.instance(dt) print(p) #'2008-01-01T00:00:00+00:00'
解析
import pendulum
dt = pendulum.parse('1975-05-21T22:00:00')
print(dt)
#'1975-05-21T22:00:00+00:00
# You can pass a tz keyword to specify the timezone
dt = pendulum.parse('1975-05-21T22:00:00', tz='Europe/Paris')
print(dt)
#'1975-05-21T22:00:00+01:00'
# Not ISO 8601 compliant but common
dt = pendulum.parse('1975-05-21 22:00:00')
宽松模式解析
import pendulum
dt = pendulum.parse('31-01-01')
#Traceback (most recent call last):
#...
#ParserError: Unable to parse string [31-01-01]
dt = pendulum.parse('31-01-01', strict=False)
print(dt)
#'2031-01-01T00:00:00+00:00'
格式化
import pendulum
dt = pendulum.datetime(1975, 12, 25, 14, 15, 16)
print(dt)
#'1975-12-25T14:15:16+00:00'
dt.to_date_string()
#'1975-12-25'
dt.to_formatted_date_string()
#'Dec 25, 1975'
dt.to_time_string()
#'14:15:16'
dt.to_datetime_string()
#'1975-12-25 14:15:16'
dt.to_day_datetime_string()
#'Thu, Dec 25, 1975 2:15 PM'
# You can also use the format() method
dt.format('dddd Do [of] MMMM YYYY HH:mm:ss A')
#'Thursday 25th of December 1975 02:15:16 PM'
# Of course, the strftime method is still available
dt.strftime('%A %-d%t of %B %Y %I:%M:%S %p')
#'Thursday 25th of December 1975 02:15:16 PM'
import pendulum
dt = pendulum.datetime(1975, 12, 25, 14, 15, 16)
dt.format('YYYY-MM-DD HH:mm:ss')
#'1975-12-25 14:15:16'
加减
import pendulum dt = pendulum.datetime(2012, 1, 31) dt.to_datetime_string() #'2012-01-31 00:00:00' 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
dt = pendulum.datetime(2012, 1, 31, 12, 0, 0)
dt.start_of('day')
#'2012-01-31 00:00:00'
dt.end_of('day')
#'2012-01-31 23:59:59'
dt.start_of('month')
#'2012-01-01 00:00:00'
dt.end_of('month')
#'2012-01-31 23:59:59'
dt.start_of('year')
#'2012-01-01 00:00:00'
dt.end_of('year')
#'2012-12-31 23:59:59'
dt.start_of('decade')
#'2010-01-01 00:00:00'
dt.end_of('decade')
#'2019-12-31 23:59:59'
dt.start_of('century')
#'2000-01-01 00:00:00'
dt.end_of('century')
#'2099-12-31 23:59:59'
dt.start_of('week')
#'2012-01-30 00:00:00'
dt.day_of_week == pendulum.MONDAY
#True # ISO8601 week starts on Monday
dt.end_of('week')
'2012-02-05 23:59:59'
dt.day_of_week == pendulum.SUNDAY
#True # ISO8601 week ends on SUNDAY
dt.next(pendulum.WEDNESDAY)
#'2012-02-01 00:00:00'
dt.day_of_week == pendulum.WEDNESDAY
#True
dt = pendulum.datetime(2012, 1, 1, 12, 0, 0)
dt.next()
#'2012-01-08 00:00:00'
dt.next(keep_time=True)
#'2012-01-08T12:00:00+00:00'
dt = pendulum.datetime(2012, 1, 31, 12, 0, 0)
dt.previous(pendulum.WEDNESDAY)
#'2012-01-25 00:00:00'
dt.day_of_week == pendulum.WEDNESDAY
#True
dt = pendulum.datetime(2012, 1, 1, 12, 0, 0)
dt.previous()
#'2011-12-25 00:00:00'
dt.previous(keep_time=True)
#'2011-12-25 12:00:00'
start = pendulum.datetime(2014, 1, 1)
end = pendulum.datetime(2014, 1, 30)
start.average(end)
#'2014-01-15 12:00:00'
# others that are defined that are similar
# and tha accept month, quarter and year units
# first_of(), last_of(), nth_of()
期间大小
import pendulum from datetime import datetime d1 = datetime(2012, 1, 1, 1, 2, 3, tzinfo=pytz.UTC) d2 = datetime(2011, 12, 31, 22, 2, 3, tzinfo=pytz.UTC) delta = d2 - d1 delta.days #-1 delta.seconds #75600 d1 = pendulum.datetime(2012, 1, 1, 1, 2, 3) d2 = pendulum.datetime(2011, 12, 31, 22, 2, 3) delta = d2 - d1 delta.days #0 delta.hours #-3 import pendulum it = pendulum.duration(days=1177, seconds=7284, microseconds=1234)
到此这篇关于Python Pendulum进行日期时间处理的示例详解 的文章就介绍到这了,更多相关Python Pendulum日期时间处理内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
