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

Buggy times for northern hemisphere #73

Open
AndKe opened this issue Aug 29, 2021 · 2 comments
Open

Buggy times for northern hemisphere #73

AndKe opened this issue Aug 29, 2021 · 2 comments

Comments

@AndKe
Copy link

AndKe commented Aug 29, 2021

city = LocationInfo("Tromsø", "Norway", "Europe/Tromsø", 69.65, 18.78)

produces:
dawn: 2021-08-29 01:31:26.185793
dusk: 2021-08-29 19:55:29.714044

Real data: https://www.timeanddate.no/astronomi/sol/norge/tromso

@yasirroni
Copy link

Can you explain more clearly including the code snippet?

@AndKe
Copy link
Author

AndKe commented Feb 26, 2022

It was simply about that dawn and dusk times being off with several hours, compared to other sources and experience.
If anyone should try to figure it out, this code can be used:

from astral import LocationInfo
from astral.sun import sun
import datetime
import time

def check_date(now):
    city = LocationInfo("Tromsø", "Norway", "Europe/Tromsø", 69.65, 18.78)
    #s = sun(city.observer)  # , date=datetime.date(2021, 2, 18))

    try:
        s = sun(city.observer, date=now)
    except ValueError as ve:
        print (ve)
        if ve == "Sun never reaches 6.0 degrees below the horizon, at this location.":
            print("alwayson")

        if ve == "Sun is always below the horizon on this day, at this location.":
            print("alwaysoff")


    dawn = (s['dawn']) # + datetime.timedelta(hours=0))  # negative hours gives earlier "off"
    dusk = (s['dusk']) # + datetime.timedelta(hours=0))  # positive hours postpone "on"
    dawn = dawn.replace(tzinfo=None)  # remove timezone info
    dusk = dusk.replace(tzinfo=None)

    print(time.strftime('%H:%M:%S'), ', light schedule updated')
    print('dawn: ', dawn)
    print('dusk: ', dusk)
    print('now: ', now)

    if dawn < now:
        print("it is past dawn time")
    if dusk < now:
        print("it is past dusk time")

# for testing

#check_date(datetime.datetime.now())
check_date(datetime.datetime(2021, 8, 29, 23, 59, 59, 342380))

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