Skip to content

Timezone offset from UTC is wrong when not in DST #362

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

Closed
pquentin opened this issue Nov 4, 2015 · 2 comments
Closed

Timezone offset from UTC is wrong when not in DST #362

pquentin opened this issue Nov 4, 2015 · 2 comments

Comments

@pquentin
Copy link

pquentin commented Nov 4, 2015

It turns out that the Europe/Paris timezone just got out of DST and is now in UTC+1 back from UTC+2. It means that dates should currently be recorded with +0100. date does it correctly:

$ TZ='Europe/Paris' date +"%Y-%m-%dT%H:%M:%S%z"
2015-11-04T08:45:34+0100

However, GitPython uses time.altzone to guess dates in commits, which yields +0200 instead of +0100:

$ TZ='Europe/Paris' python -c 'import time; print(-time.altzone/3600)'
2.0

While the date is still correct, the timezone is now wrong. Indeed, using time.altzone is only correct when:

  1. time.daylight is non zero, eg. the current timezone has a DST defined at some point in the year (always true for Europe/Paris, but always false for Indian/Reunion)
  2. and DST is currently active, which you can check with time.localtime().tm_isdst > 0.

To fix this issue, you should use this code to get the UTC offset in seconds.

is_dst = time.daylight and time.localtime().tm_isdst > 0
utc_offset = time.altzone if is_dst else time.timezone

You still need to multiply by -1 and divide by 3600, but you will get the correct offset this way.

Thank you!

@Byron
Copy link
Member

Byron commented Nov 8, 2015

Thanks for taking the time to create this awesome and informative issue ! It's much appreciated.

As I am unable to post new releases to pypi, I hope you will be able to use the latest version on HEAD and let me know if this fixes your problem.

@pquentin
Copy link
Author

pquentin commented Nov 8, 2015

Thank you for the fix! It works perfectly.

@pquentin pquentin closed this as completed Nov 8, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants