Python: Weird behavior with signs of pytz timezone

2019-08-15 02:17发布

I'm in Argentina, my time is GMT-3 (http://wwp.greenwichmeantime.com/time-zone/south-america/argentina/)

I was playing with Pytz and noticed something weird, take a look:

from pytz import timezone
bsas = timezone('America/Argentina/Buenos_Aires')

gmt_plus_3 = timezone('Etc/GMT+3')
gmt_min_3 = timezone('Etc/GMT-3')

# Date and time at this momement: 2012-07-26 10:23:01

fmt = '%Y-%m-%dT%H:%M:%S'
datetime.now(bs).strftime(fmt)  #'2012-07-26T10:23:01' <-- GOOD!

datetime.now(gmt_min_3).strftime(fmt)  #'2012-07-26T16:25:21' <-- WRONG!

datetime.now(gmt_plus_3).strftime(fmt)  #'2012-07-26T10:23:01' <-- GOOD! WTF!

I'd expect:

GMT-3 should be 10:23:01 and gives me 16:23:01
GMT+3 should be 16:23:01 and gives me 10:23:01

Is this a bug? Am i doing it wrong??

1条回答
SAY GOODBYE
2楼-- · 2019-08-15 03:12

It is not a bug, it is a feature (Wikipedia: Tz_database):

The special area of Etc is used for some administrative zones, particularly for "Etc/UTC" which represents Coordinated Universal Time. In order to conform with the POSIX style, those zone names 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 in their name (e.g "Etc/GMT-14" is 14 hours ahead/east of GMT.)

查看更多
登录 后发表回答