Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Astral fails to correctly calculate sunrise and sunset times in unique test case of Norwegian Winter #67

Open
FMKaiba opened this issue Mar 28, 2021 · 10 comments

Comments

@FMKaiba
Copy link

FMKaiba commented Mar 28, 2021

import pytz
import datetime

import astral
import astral.location

tz='UTC'
utc_now = datetime.datetime(2010, 1, 1, tzinfo=pytz.timezone(tz))

obs = astral.Observer(latitude=66.5, longitude=162.4, elevation=0.0)

print(utc_now) # datetime.datetime(2010, 1, 1)

sunrise = astral.sun.sunrise(obs,utc_now)
sunset = astral.sun.sunset(obs,utc_now)
print(sunrise)
print(sunset)

utc_now = sunrise + datetime.timedelta(seconds=15)
print(utc_now) # Adjusted time to just after sunrise

print(astral.sun.sunrise(obs,utc_now)) #Completely different sunrise time, day before, why not same as above?
print(astral.sun.sunset(obs,utc_now)) #Same issue... getting time for previous day.

Produces:
2010-01-01 00:00:00+00:00
2009-12-31 23:53:51.078028+00:00 <==Sunrise Time
2010-01-01 02:33:44.399907+00:00 <==Sunset Time
2009-12-31 23:54:06.078028+00:00 <== Adjust time to Sunrise Time + 15 seconds
2009-12-30 23:55:49.136837+00:00 <== New Sunrise Time
2009-12-31 02:30:47.610544+00:00 <== New Sunset Time

@AndKe
Copy link

AndKe commented Apr 10, 2021

Same here.
This is exactly what I came to report.
While troubleshooting I've made a debug print log:

 # this is when the function was called
2021-04-10 01:00:00.015625, light schedule updated 
# this is the resulting data .. for the day before.
dawn:  2021-04-09 02:59:08.007731
dusk:  2021-04-09 20:37:25.089938

Using Astral 2.2

@yasirroni
Copy link

yasirroni commented Feb 25, 2022

It seems the solution can be achieved by edit this line of code

astral/src/astral/sun.py

Lines 363 to 370 in 96496f0

delta = -observer.longitude - degrees(hourangle)
timeDiff = 4.0 * delta
timeUTC = 720 + timeDiff - eq_of_time(t)
td = minutes_to_timedelta(timeUTC)
dt = datetime.datetime(date.year, date.month, date.day) + td
dt = pytz.utc.localize(dt) # pylint: disable=E1120
return dt

@AndKe
Copy link

AndKe commented Feb 25, 2022

@yasirroni thanks, but what is that ? - it's not committed, not a PR? - will it make into a release soon?

@yasirroni
Copy link

That is the code that astral use that we need to edit and make a PR to solve local timezone bug changing day.

@yasirroni
Copy link

See suntime package. Basically they reproduce the same bug and I already fix that on suntime. Haven't got time to fix here too. I just pin point the problem for those willing to solve it.

@AndKe
Copy link

AndKe commented Feb 26, 2022

@yasirroni unfortunatly, I can not use suntime due to SatAgro/suntime#18

@yasirroni
Copy link

Have you try my fork on test-pypi? Go to test-pypi and search suntime-yasirroni

@AndKe
Copy link

AndKe commented Mar 2, 2022

@yasirroni thanks, but I try to stick to libraries from pip or so, to prevent wild searching and retracing what I did when something needs to run on another distro/hardware x years later.

@yasirroni
Copy link

@yasirroni thanks, but I try to stick to libraries from pip or so, to prevent wild searching and retracing what I did when something needs to run on another distro/hardware x years later.

Then, you can fork it and publish it on pypi yourself. Easy, hahaa

@ShakeelKadri
Copy link

ShakeelKadri commented Mar 28, 2024

It seems the solution can be achieved by edit this line of code

astral/src/astral/sun.py

Lines 363 to 370 in 96496f0

delta = -observer.longitude - degrees(hourangle)
timeDiff = 4.0 * delta
timeUTC = 720 + timeDiff - eq_of_time(t)
td = minutes_to_timedelta(timeUTC)
dt = datetime.datetime(date.year, date.month, date.day) + td
dt = pytz.utc.localize(dt) # pylint: disable=E1120
return dt

Thanks I had that problem today, astral was showing error, "Unable to find a dawn time on the date specified". After applying your correction it works fine. Thank you very much.

I did one correction though : "eq_of_time(t)" I replaced t with jc for jc = julianday_to_juliancentury(jd + adjustment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants