esu*_*gei 8 python datetime pendulum
在系统 pendulum v1.4 的当前部分中使用,但使用 croniter 它会导致https://github.com/sdispater/pendulum/issues/214 中描述的错误
这适用于 datetime.datetime 类型,我仍然必须使用 pendulum v1.4。
所以我正在寻找如何有效地将钟摆转换为 datetime.datetime 类型的解决方案?
已经尝试将钟摆格式化为字符串并使用 dateutil.parser 进行解析。
tos*_*osh 11
对此的另一个看法:
>>> from datetime import datetime
>>> import pendulum
>>> datetime.fromtimestamp(pendulum.now().timestamp(), pendulum.tz.UTC)
datetime.datetime(2021, 1, 12, 11, 41, 32, 387753, tzinfo=Timezone('UTC'))
Run Code Online (Sandbox Code Playgroud)
通常不需要这样做,因为pendulum'sDateTime继承自datetime.datetime. 任何与 stdlib 的datetime's 一起工作的代码也应该与 pendulum 一起工作。
小智 5
我也找不到 Pendulum 助手。那么,回到基础:
import datetime as dt
tz_info = dateutil.tz.gettz(zone_name)
pend_time = pendulum.datetime(...)
dt_time = dt.datetime(
pend_time.year,
pend_time.month,
pend_time.day,
pend_time.hour,
pend_time.minute,
pend_time.second,
pend_time.microsecond,
).astimezone(tz_info)
Run Code Online (Sandbox Code Playgroud)
请注意 dateutil 的使用。从 Python 3.6 开始,tzinfo 文档建议使用 dateutil.tz 而不是 pytz 作为 IANA 时区提供商。