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

moon rise issues with timezone #94

Open
fonebone42 opened this issue Feb 1, 2024 · 1 comment
Open

moon rise issues with timezone #94

fonebone42 opened this issue Feb 1, 2024 · 1 comment

Comments

@fonebone42
Copy link

this morning (2024-02-01) I found my solution displaying an error as it could not determine the moon rise for today nor for tomorrow so after a bit of investigation I wrote the following test to show that there seems to be a problem with the moon rise time in situations where UTC has a rise but CET does not. Further this also shows inconsistent behavior (None vs. exception) when there is no moon rise

from astral import LocationInfo  
from astral.moon import moonrise, moonset  
  
from datetime import datetime, timedelta, timezone, tzinfo, date  

# https://www.timeanddate.com/astronomy/uk/london
city1 = LocationInfo("London", "England", "Europe/London", 51.5, -0.116)
# https://www.timeanddate.com/astronomy/switzerland/zurich
city2 = LocationInfo("Zurich", "Switzerland", "Europe/Zurich", 47.37, 8.54)

test = ({"city":city1, "tz":"Europe/London", "should": None,    "date":date(2024,2,1)}
       ,{"city":city1, "tz":"Europe/London", "should": "00:20", "date":date(2024,2,2)}
       ,{"city":city1, "tz":"Europe/London", "should": "01:36", "date":date(2024,2,3)}
       ,{"city":city2, "tz":"Europe/Zurich", "should": None,    "date":date(2024,2,1)}
       ,{"city":city2, "tz":"Europe/Zurich", "should": "00:34", "date":date(2024,2,2)}
       ,{"city":city2, "tz":"Europe/Zurich", "should": "01:44", "date":date(2024,2,3)}
       )

for x in test:
    try:
        mr = moonrise(x['city'].observer, date=x['date'], tzinfo=x['tz'])
        if mr:
            print(f'moon rise on {x["date"].strftime("%Y.%m.%d")} in {x["tz"]} (should be {x["should"]}): {mr.strftime("%H:%M")}')
        else:
            print(f'moon rise on {x["date"].strftime("%Y.%m.%d")} in {x["tz"]} (should be {x["should"]}): None')
    except Exception as e:
        print(f'moon rise on {x["date"].strftime("%Y.%m.%d")} in {x["tz"]} (should be {x["should"]}): exception: {e}')

the should-be I've taken from https://www.timeanddate.com/astronomy
and here's the output:

moon rise on 2024.02.01 in Europe/London (should be None): exception: Moon never rises on this date, at this location
moon rise on 2024.02.02 in Europe/London (should be 00:20): 00:21
moon rise on 2024.02.03 in Europe/London (should be 01:36): 01:36

moon rise on 2024.02.01 in Europe/Zurich (should be None): None
moon rise on 2024.02.02 in Europe/Zurich (should be 00:34): exception: Moon never rises on this date, at this location
moon rise on 2024.02.03 in Europe/Zurich (should be 01:44): 01:45
@csmoore
Copy link

csmoore commented Apr 5, 2024

Below is the quick workaround I tried that seems to work for me and for your test case above - but I am not confident enough to put in a PR:

File: astral/moon.py
Line: 424 @@ def moonrise(

    info = riseset(date, observer)

    #ADDED
    if not info[0]: 
        # TRICKY: date is based on UTC so it might not return a value for the current date/time if non-UTC so back up 1 day if None
        delta = datetime.timedelta(days=-1)
        new_date = date + delta
        info = riseset(new_date, observer)
    #ADDED

    if info[0]:

Line 476 @@ def moonset(

    info = riseset(date, observer)

    #ADDED
    if not info[1]:  
        # TRICKY: date is based on UTC so it might not return a value for the current UTC date/time if non-UTC so go forward 1 day if None
        delta = datetime.timedelta(days=1)
        new_date = date + delta
        info = riseset(new_date, observer)
    #ADDED

    if info[0]:

I think #88 is related. I do like and appreciate this lightweight library - it has been helpful and very easy to use and deploy.

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

2 participants