python的时间管理库whenever的使用
whenever是一个时间管理库,可以处理日期时间、时区、自动转换夏令时。
安装whenever
pip install whenever
现在时间
from whenever import Instant
Instant.now()#Instant(2025-06-10 13:07:05.9070706Z)
Instant: 表示一个绝对的时间点(类似于 Unix 时间戳),与时区无关
Instant
构建Instant
p = Instant.from_utc(2022, 10, 24, hour=17)
#Instant(2025-06-11 01:55:29.6410401Z)
字符串构建Instant
from whenever import Instant
Instant.parse_common_iso("2025-04-19T19:02+04:00")
#Instant(2025-04-19 15:02:00Z)
加上一段时间
t = Instant.now()
t.add(hours=1) #可添加hours, minutes, seconds, milliseconds, microseconds
#Instant(2025-06-11 02:25:12.6431824Z)
减去一段时间
t.subtract(hours=2,minutes=9) #可减去hours, minutes, seconds, milliseconds, microseconds
#Instant(2025-06-10 23:16:12.6431824Z)
两个Instant的时间差
import time
t1 = Instant.now()
time.sleep(0.5)
t2 = Instant.now()
d = t2 - t1
#TimeDelta(PT0.5068491s)
TimeDelta的方法
d.in_hrs_mins_secs_nanos()
#(0, 0, 0, 506849100) #时,分,秒,纳秒
d.in_nanoseconds()
#506849100 #纳秒
d.in_microseconds()
#506849.10000000003 #毫秒
d.in_seconds()
#0.5068491 #秒
d.in_minutes()
#0.008447485000000001 #分钟
d.in_hours()
#0.0001407914166666667 #小时
d.in_days_of_24h()
#5.8663090277777786e-06 #天
构建一个TimeDelta
from whenever import TimeDelta
d = TimeDelta(hours=1, minutes=30) #可以使用hours, minutes,seconds,nanoseconds
#TimeDelta(PT1h30m)
用TimeDelta改变Instant时间
t = Instant.now()
t+d
#Instant(2025-06-11 03:02:13.766554Z)
为Instant添加时区
i = Instant.now()
i.to_tz("Europe/Berlin")
#ZonedDateTime(2025-06-10 15:08:15.8984677+02:00[Europe/Berlin])
ZonedDateTime: 表示一个带有时区信息的日期时间
添加系统时区
i = Instant.now()
i.to_system_tz()
#SystemDateTime(2025-06-10 21:08:15.8984677+08:00)
ZonedDateTime
创建ZonedDateTime对象
from whenever import ZonedDateTime
d = ZonedDateTime(2025,4,19,hour=15,tz="America/New_York")
#ZonedDateTime(2025-04-19 15:00:00-04:00[America/New_York])
字符串构建OffsetDateTime
from whenever import OffsetDateTime
OffsetDateTime.parse_common_iso("2025-04-19T19:02+04:00")
#OffsetDateTime(2025-04-19 19:02:00+04:00)
转换时区
d.to_tz("Europe/Berlin")#ZonedDateTime(2025-04-19 21:00:00+02:00[Europe/Berlin])
自动处理夏令时
from whenever import ZonedDateTime
start = ZonedDateTime(2024, 3, 10, 1, 30, tz="America/New_York") #创建一个跨越夏令时的日期时间对象end = start.add(hours=1) # 添加 1 小时(跨越夏令时切换)
#ZonedDateTime(2024-03-10 03:30:00-04:00[America/New_York])
添加时间
d.add(years=2) #可以使用years, months, weeks, days
#ZonedDateTime(2027-04-19 15:00:00-04:00[America/New_York])
减少时间
d.subtract(months=13)#可以使#用years, months, weeks, days
#ZonedDateTime(2024-03-19 15:00:00-04:00[America/New_York])
两个ZoneDateTime的时间差
d1 = ZonedDateTime(2025,4,19,hour=15,tz="America/New_York")
d2 = ZonedDateTime(2022,3,19,hour=12,tz="America/New_York")
d1 -d2
#TimeDelta(PT27051h) #结果是一个TimeDelta
ZonedDateTime可以加上或者减去一个TimeDelta
from whenever import TimeDelta
d = TimeDelta(hours=1, minutes=30) #可以使用hours, minutes,seconds,nanoseconds
#TimeDelta(PT1h30m)
d1 + d
#ZonedDateTime(2025-04-19 16:30:00-04:00[America/New_York])
d2 - d
#ZonedDateTime(2025-04-19 13:30:00-04:00[America/New_York])
获得unix时间截
from whenever import Instant
i = Instant.now()
i.timestamp()
#1749567127i.timestamp_millis()
#1749567127585
i.timestamp_nanos()
#1749567127585996000
从时间截获得日期
ZonedDateTime.from_timestamp(1749567127, tz="America/New_York")
#ZonedDateTime(2025-06-10 10:52:07-04:00[America/New_York])
whenever中的Date,Time
Date 日期,不包含时间、时区
Time 只包含时间
from whenever import Date, Time
d = Date(2023,10,1)
#Date(2023-10-01)
t = Time(12,30)
#Time(12:30:00)
由Date加上Time构成PlainDateTime
PlainDateTime 包含日期、时间,不包含时区
d.at(Time(12,30))
#PlainDateTime(2023-10-01 12:30:00)
或者这样构建PlainDateTime
d = PlainDateTime(2020, 8, 15, 23, 12)
#PlainDateTime(2020-08-15 23:12:00)
为PlainDateTime添加时区转换为ZonedDateTime
from whenever import PlainDateTime
datetime = PlainDateTime(2023, 10, 1, 12, 30)
datetime.assume_tz("America/New_York")
#ZonedDateTime(2023-10-01 12:30:00-04:00[America/New_York])
计算年龄
from whenever import Date
birth_date = Date(2023,11,2)
age = Date.today_in_system_tz() - birth_date
#DateDelta(P1y7m8d)months, days = age.in_months_days()
#(19, 8) age.in_years_months_days()
#(1, 7, 8) 年,月,日
构建DateDelta
from whenever import DateDelta
p = DateDelta(years=1, months=2, weeks=3, days=11)
#DateDelta(P1y2m32d)
和datetime库相互转换
datetime.datetime 转换为 whenever.Instant
import datetime
from whenever import Instant
py_dt = datetime.datetime.now(datetime.timezone.utc)
i = Instant.from_py_datetime(py_dt)
#Instant(2025-06-10 14:44:57.203521Z)
zdt = i.to_tz("America/New_York")
#ZonedDateTime(2025-06-10 10:44:57.203521-04:00[America/New_York])
whenever.ZonedDateTime 转换为 datetime.datetime
zdt.py_datetime()
#datetime.datetime(2025, 6, 10, 10, 44, 57, 203521, tzinfo=zoneinfo.ZoneInfo(key='America/New_York'))