我有以下函数,对于提供的日期和一对纬度/经度变量,应该返回 UTC 中的 3 个时间戳的列表
不幸的是,可能因为计算从一天跳到另一天,结果完全不对。我哪里弄错了?有更好的方法吗?
import datetime as dt
from suntime import Sun
def key_timestamps(date, latitude, longitude):
date = dt.datetime.strptime(date, "%Y-%m-%d")
sun = Sun(latitude, longitude)
sunrise = sun.get_sunrise_time(date)
sunset = sun.get_sunset_time(date)
next_day = date + dt.timedelta(days=1)
sunrise_next_day = sun.get_sunrise_time(next_day)
solar_noon = sunrise + (sunset - sunrise) / 2
peak_heating = solar_noon + dt.timedelta(hours=2, minutes=30)
evening_release = sunset + dt.timedelta(hours=3)
morning_minimum = sunrise_next_day - dt.timedelta(hours=1)
return [
int(peak_heating.timestamp()),
int(evening_release.timestamp()),
int(morning_minimum.timestamp())
]
singapore_results = key_timestamps("2022-05-29", 1.3521, 103.8198)
print(singapore_results)
输出为
[1653809526, 1653746868, 1653947784]
,翻译为:
另一个例子:
la_results = key_timestamps("2022-05-29", 34.0549, 118.2426)
输出:
[1653806070, 1653747048, 1653940656]
翻译为:
将日期时间导入为 dt 从 suntime 导入 Sun 导入 pytz
def key_timestamps(日期、纬度、经度): # 解析日期并假设它是当地时间 日期 = dt.datetime.strptime(日期, "%Y-%m-%d")
# Sun calculation object
sun = Sun(latitude, longitude)
# Get sunrise and sunset in UTC
sunrise_utc = sun.get_sunrise_time(date)
sunset_utc = sun.get_sunset_time(date)
# Convert date to the next day for the next sunrise
next_day = date + dt.timedelta(days=1)
sunrise_next_day_utc = sun.get_sunrise_time(next_day)
# Calculate solar noon
solar_noon = sunrise_utc + (sunset_utc - sunrise_utc) / 2
# Calculate timestamps in UTC
peak_heating_utc = solar_noon + dt.timedelta(hours=2, minutes=30)
evening_release_utc = sunset_utc + dt.timedelta(hours=3)
morning_minimum_utc = sunrise_next_day_utc - dt.timedelta(hours=1)
# Return timestamps in UTC
return [
int(peak_heating_utc.timestamp()),
int(evening_release_utc.timestamp()),
int(morning_minimum_utc.timestamp())
]