如何将 skyfield Time 转换为特定时区的 datetime
当你有 skyfield Time 对象时,如
skyfield_time_example.py
t = ts.now()
# Example: <Time tt=2459750.027604357>你可以使用 .astimezone() 和 pytz 库将其转换为特定时区(在此示例中为 Europe/Berlin)的 Python datetime:
skyfield_astimezone_example.py
t.astimezone(pytz.timezone("Europe/Berlin"))
# Example: datetime.datetime(2022, 6, 19, 14, 38, 35, 832445, tzinfo=<DstTzInfo 'Europe/Berlin' CEST+2:00:00 DST>)完整示例
skyfield_time_conversion_example.py
from skyfield import api
from datetime import datetime
import pytz
ts = api.load.timescale()
t = ts.now()
dt = t.astimezone(pytz.timezone("Europe/Berlin"))
print(dt) # 例如 2022-06-19 14:42:47.406786+02:00If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow