Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytz and Etc/GMT-5

I'm having trouble understanding the conversion between the "Etc/GMT-5" timezone and UTC in pytz.

>>> dt = datetime(2009, 9, 9, 10, 0) # September 9 2009, 10:00
>>> gmt_5 = pytz.timezone("Etc/GMT-5")
>>> gmt_5.localize(dt)
datetime.datetime(2009, 9, 9, 10, 0, tzinfo=<StaticTzInfo 'Etc/GMT-5'>)

Everything is fine so far, but then I try to convert that to UTC:

>>> gmt_5.localize(dt).astimezone(pytz.utc)
datetime.datetime(2009, 9, 9, 5, 0, tzinfo=<UTC>)

So to me it seems that when converting from 10:00 in GMT-5 to UTC I get 05:00? I would expect pytz to give me 15:00 instead.

What am I missing?

EDIT: I have confirmed that timezone conversion for the US/Eastern timezone works just as I'd expect:

>>> eastern = pytz.timezone("US/Eastern")
>>> eastern.localize(dt)
datetime.datetime(2009, 9, 9, 10, 0, tzinfo=...) # Too long
>>> pytz.utc.normalize(eastern.localize(dt).astimezone(pytz.utc))
datetime.datetime(2009, 9, 9, 14, 0, tzinfo=<UTC>)

EDIT 2: I have confirmed that when I use Etc/GMT+5 I get 15:00, which is what I'd expect to get from Etc/GMT-5. Is this a pytz bug?

like image 738
Deniz Dogan Avatar asked Oct 24 '10 15:10

Deniz Dogan


People also ask

What is pytz timezone?

pytz brings the Olson tz database into Python. This library allows accurate and cross platform timezone calculations using Python 2.4 or higher. It also solves the issue of ambiguous times at the end of daylight saving time, which you can read more about in the Python Library Reference ( datetime.

What is pytz?

Pytz brings the Olson tz database into Python and thus supports almost all time zones. This module serves the date-time conversion functionalities and helps user serving international client's base.

Does pytz account for daylight savings?

pytz will help you to tell if an date is under DST influence by checking dst() method. What you can really do is to convert back to naive-offset datetime, then apply the delta…

Which Python library is used for time zone?

The zoneinfo module provides a concrete time zone implementation to support the IANA time zone database as originally specified in PEP 615.


1 Answers

This is apparently a POSIX thing. From Wikipedia:

In order to conform with the POSIX style, those zones beginning with "Etc/GMT" have their sign reversed from what most people expect. In this style, zones west of GMT have a positive sign and those east have a negative sign.

like image 95
adw Avatar answered Sep 28 '22 06:09

adw