sffjunkie / astral

Python calculations for the position of the sun and moon.
Apache License 2.0
237 stars 47 forks source link

Help with Timezone #53

Closed Badger101 closed 3 years ago

Badger101 commented 4 years ago

Hi, I'm upgrading my program from v1.6 to v2.1. I've notice since the clocks have changed in the U.K. that the time is off by one hour. I think it is the same as (closed) issue #29, but I cannot get this to work. Here is the code I've been using:

from datetime import date from datetime import timedelta # Automatically handle day that span over the month

from astral import LocationInfo from astral.sun import sun

city = LocationInfo('Headley', 'England', 'Europe/London', 51.1211, -0.8297)

today = date.today() print(today)

for x in range(0, 7): day = sun(city.observer, today) print('Dusk: %s' % str(day['dusk'])) print('Dawn: %s' % str(day['dawn'])) today += timedelta(days=1)

Also have the calculations changed between version as irrespective of the hour difference, the times are different by a few minutes. Any help would be appreciated.

sffjunkie commented 4 years ago

That is because the function returns times in UTC by default. Now that you're in BST the times are 1 hour off. If you want the times in BST make your call to sun as follows

day = sun(city.observer, today, tzinfo=pytz.timezone(city.timezone))

If you update to version 2.2 you can pass city.timezone to the sun function directly (no need to call pytz.timezone())

Badger101 commented 4 years ago

Hi sffjunkie,

Thanks for the info, I will definitely look into v2.2. I found a good workaround though. datetime.astimezone() will take the 'tz=None' argument which makes it use the system timezone so: day = sun(city.observer, today, tzinfo=None) Works well