Closed tkhoa22 closed 9 months ago
I suspect this is the same issue I reported in #12, with a difference that you are on the other side of UTC so the sunrise is the wrong day instead of sunset.
I expanded my workaround, see it this works.
import datetime
from suntime import Sun
import pytz
def sunset(lat, lon, dt=None, tz=None):
if dt is None:
dt = datetime.datetime.now()
sun = Sun(lat, lon)
ss = sun.get_local_sunset_time(dt, local_time_zone=tz)
sr = sun.get_local_sunrise_time(dt, local_time_zone=tz)
if ss.utcoffset() < datetime.timedelta(0):
if ss < sr:
ss = ss + datetime.timedelta(1)
print('workaround: added 1 day to sunset time')
return ss
def sunrise(lat, lon, dt=None, tz=None):
if dt is None:
dt = datetime.datetime.now()
sun = Sun(lat, lon)
ss = sun.get_local_sunset_time(dt, local_time_zone=tz)
sr = sun.get_local_sunrise_time(dt, local_time_zone=tz)
if sr.utcoffset() > datetime.timedelta(0):
if ss < sr:
sr = sr - datetime.timedelta(1)
print('workaround: subtracted 1 day from sunrise time')
return sr
latitude = 56.49771
longitude = 82.0475315
tz = pytz.timezone('Asia/Novosibirsk')
day = datetime.datetime(2021, 4, 24)
print(f'For {latitude}, {longitude}, tz={tz}, day={day}')
print('sunrise:', sunrise(latitude, longitude, day, tz))
print('sunset: ', sunset(latitude, longitude, day, tz))
print()
latitude = 39.8895
longitude = -77.0353
tz = pytz.timezone('US/Eastern')
day = datetime.datetime(2020, 4, 29)
print(f'For {latitude}, {longitude}. tz={tz}, day={day}')
print('sunrise:', sunrise(latitude, longitude, day, tz))
print('sunset: ', sunset(latitude, longitude, day, tz))
Running that I get
For 56.49771, 82.0475315, tz=Asia/Novosibirsk, day=2021-04-24 00:00:00
workaround: subtracted 1 day from sunrise time
sunrise: 2021-04-24 06:04:00+07:00
sunset: 2021-04-24 20:57:00+07:00
For 39.8895, -77.0353. tz=US/Eastern, day=2020-04-29 00:00:00
sunrise: 2020-04-29 06:10:00-04:00
workaround: added 1 day to sunset time
sunset: 2020-04-29 20:01:00-04:00
Thank for reply I can fix it but I just want to report to module developer for module inside fix
Solved in https://github.com/SatAgro/suntime/pull/19, just add tz
as input and done.
Usage:
import datetime
import pytz
from suntime import Sun, SunTimeException
latitude = 7.7956
longitude = 110.3695
tz = pytz.timezone("Asia/Jakarta")
day = datetime.datetime(2022, 4, 24)
print(tz.utcoffset(day))
sun = Sun(latitude, longitude)
try:
print("")
print(datetime.datetime.now())
print()
print(sun.get_sunrise_time())
print(sun.get_sunset_time())
print("")
print(sun.get_sunrise_time(tz=tz))
print(sun.get_sunset_time(tz=tz))
Why is there something wrong on the day? I have tried other days and the number of wrong days is too much in a year (at least 1/2 year)