astral icon indicating copy to clipboard operation
astral copied to clipboard

Buggy times for northern hemisphere

Open AndKe opened this issue 2 years ago • 2 comments

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

AndKe avatar Aug 29 '21 16:08 AndKe

Can you explain more clearly including the code snippet?

yasirroni avatar Feb 25 '22 02:02 yasirroni

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))

AndKe avatar Feb 26 '22 10:02 AndKe