欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python?Pendulum進(jìn)行日期時(shí)間處理的示例詳解

 更新時(shí)間:2025年02月17日 09:42:41   作者:小龍?jiān)谏綎|  
Pendulum?是對?Python?datetime?的繼承和發(fā)展,讓日期時(shí)間處理更簡單,這篇文章主要為大家詳細(xì)介紹了Pendulum的具體應(yīng)用,需要的可以參考下

Pendulum 是對 Python datetime 的繼承和發(fā)展,讓日期時(shí)間處理更簡單,比 datatime 更加人性化,支持 Python 3.9 及以上版本。

安裝

$ pip install pendulum

創(chuàng)建 datetime 對象

import pendulum

 dt = pendulum.datetime(2025, 2, 5)
isinstance(dt, datetime)
#True
 dt.timezone.name
#'UTC'

設(shè)置時(shí)區(qū)

import pendulum

pendulum.datetime(2015, 2, 5, tz='Asia/Shanghai')
tz = pendulum.timezone('Asia/Shanghai')
pendulum.datetime(2025, 2, 5, tz=tz)

當(dāng)前 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'

# 從格式化字符串創(chuàng)建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'

從時(shí)間戳創(chuàng)建

 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'

從實(shí)例創(chuàng)建

 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)

到此這篇關(guān)于Python Pendulum進(jìn)行日期時(shí)間處理的示例詳解 的文章就介紹到這了,更多相關(guān)Python Pendulum日期時(shí)間處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論