SatAgro / suntime

Simple sunset and sunrise time calculation python library.
GNU Lesser General Public License v3.0
108 stars 38 forks source link

Sunset returned as yesterday #30

Open buelowp opened 6 months ago

buelowp commented 6 months ago

I think this may be the same as #29 but testing it, I couldn't exactly get the same reproduction. At issue is when I use this library on my Pi, I'm getting a sunset value from the day before, but sunrise is correct.

This little snippet reproduces the issue for me.

import time
from datetime import datetime
from suntime import Sun, SunTimeException
from dateutil import tz

latitude = 42.01
longitude = -87.98
sun = Sun(latitude, longitude)

def main():
    while True:
        now = datetime.now(tz.gettz('US/Central'))
        ss = sun.get_sunset_time(at_date=now, time_zone=tz.gettz('US/Central'))
        sr = sun.get_sunrise_time(at_date=now, time_zone=tz.gettz('US/Central'))
        print("Now: {}, sr: {}, ss: {}".format(now, sr, ss))

        time.sleep(1)

if __name__ == '__main__':
    main()

which gives me the following results

Now: 2024-03-19 07:55:42.667241-05:00, sr: 2024-03-19 06:55:48-05:00, ss: 2024-03-18 19:03:36-05:00
Now: 2024-03-19 07:55:43.670629-05:00, sr: 2024-03-19 06:55:48-05:00, ss: 2024-03-18 19:03:36-05:00
Now: 2024-03-19 07:55:44.673976-05:00, sr: 2024-03-19 06:55:48-05:00, ss: 2024-03-18 19:03:36-05:00
Now: 2024-03-19 07:55:45.677398-05:00, sr: 2024-03-19 06:55:48-05:00, ss: 2024-03-18 19:03:36-05:00
Now: 2024-03-19 07:55:46.680704-05:00, sr: 2024-03-19 06:55:48-05:00, ss: 2024-03-18 19:03:36-05:00
Now: 2024-03-19 07:55:47.684021-05:00, sr: 2024-03-19 06:55:48-05:00, ss: 2024-03-18 19:03:36-05:00
Now: 2024-03-19 07:55:48.687425-05:00, sr: 2024-03-19 06:55:48-05:00, ss: 2024-03-18 19:03:36-05:00
Now: 2024-03-19 07:55:49.690767-05:00, sr: 2024-03-19 06:55:48-05:00, ss: 2024-03-18 19:03:36-05:00
Now: 2024-03-19 07:55:50.694099-05:00, sr: 2024-03-19 06:55:48-05:00, ss: 2024-03-18 19:03:36-05:00
Now: 2024-03-19 07:55:51.697491-05:00, sr: 2024-03-19 06:55:48-05:00, ss: 2024-03-18 19:03:36-05:00

The values returned are correct, but notice that sunset is yesterday, which doesn't make a lot of sense. This behavior is the same whether I use at_date or not.

Name: suntime
Version: 1.3.2
Summary: Simple sunset and sunrise time calculation python library
Home-page: https://github.com/SatAgro/suntime
Author: Krzysztof Stopa
Author-email: None
License: LGPLv3
Location: /home/pi/.local/lib/python3.9/site-packages
Requires: python-dateutil
Required-by: 
thennen commented 6 months ago

I also ran into this problem...

CozyBlanket commented 6 months ago

I've run into the same issue too. I think the problem might be in suntime.py with get_sun_timedelta function as the returning timedelta(hours=UT) variable is negative.

varaki commented 6 months ago

I have the same problem too since a couple of days ago, but it's consistent ever since 😐

gablank commented 6 months ago

This issue seems to have been introduced by e9b1fa471dbed4da039a1905596de68e9adfaa70.

The bug seems to only happen for days 1 through 80 of the year (i.e. 2024-01-01 through 2024-03-19 are fine this year).

Update: Removing this conditional (run the code for both sunrise and sunset) seems to fix the issue. Not sure if it impacts anything else, but the conditional wasn't there before.

fluxion-t commented 3 months ago

Same issue here.

thomarse-ef commented 2 months ago

I have the same issue

Erotemic commented 2 months ago

Also hitting this. It would be nice to have a fix. As it currently stands my estimation of the amount of sunlight at given points looks very wrong:

images_timeofday_distribution

joe-mccarthy commented 1 month ago

I've just run into this issue.

TomGudman commented 2 weeks ago

In the mean time you can use version 1.2.5 (1.3.0 and 1.3.1 are borked and 1.3.2 is one day in the past):

 pip3 install suntime==1.2.5
TomGudman commented 2 weeks ago

I have just noted that, despite using 1.2.5, now my sunrise date is the following day but the time is right. So, for my basic chicken coop needs I have done this ugly hack: combining the current date with the sunrise/sunset times. Don't use that if you have NASA needs ;-)

    # UTC+10
    tz_name = 'Australia/Brisbane'
    tz = zoneinfo.ZoneInfo(tz_name)
    # Fix for buggy(?) suntime library (sunrise is right time but date is the next day):
    # was:                   2024-09-20 05:41:00+10:00 --> 2024-09-19 17:43:00+10:00
    # with this fix becomes: 2024-09-19 05:41:00       --> 2024-09-19 17:43:00
    sunrise = datetime.combine(datetime.now(), today_sr.astimezone(tz).time())
    sunset  = datetime.combine(datetime.now(), today_ss.astimezone(tz).time())