安装
pip install astral
获取日出日落时间
from datetime import datetime, timedelta
from astral.sun import sun
from astral import LocationInfo
# 初始化位置信息
city = LocationInfo(name="AnJi", region="China", timezone="Asia/Shanghai", latitude=22.484786,longitude=114.549965)
new_date = datetime.now()
s = sun(city.observer, date=new_date,tzinfo=city.timezone)
print((
f'日期: {new_date.strftime("%Y-%m-%d")} -> '
f'黎明: {s["dawn"].strftime("%H:%M:%S")} '
f'日出: {s["sunrise"].strftime("%H:%M:%S")} '
f'正午: {s["noon"].strftime("%H:%M:%S")} '
f'日落: {s["sunset"].strftime("%H:%M:%S")} '
f'黄昏: {s["dusk"].strftime("%H:%M:%S")}'
))
网友评论