I'm trying to write some code to deal with world timezones, and I'm
getting a strange result. America/Iqaluit should not be showing up in
UTC+00:00, but it is. Can anyone see what's wrong with this code?
import time
from datetime import datetime
import os
import pytz
def getZoneNamesOnT hisComputer():
for i in pytz.all_timezo nes:
if os.path.exists( os.path.join('/tmp/usr/share/zoneinfo/',
i)):
yield i
def getZonesGrouped ByOffset():
'''return a dict of all the zone names , grouped by their offset
eg:
{'-0400': ['America/Blanc-Sablon', 'Etc/GMT+4'],
'+0000': ['America/Iqaluit', 'Etc/GMT', 'Europe/Belfast', 'GB',
'GMT'],
...
}
'''
Y,M,D,h,m,s = time.localtime( )[:6]
zonesGroupedByO ffset = {}
for name in getZoneNamesOnT hisComputer():
tz = pytz.timezone(n ame)
now = datetime(Y,M,D, h,m,s, tzinfo=tz)
# strfime returns a string like "-0800" or "+0430" or "0000"
offsetKey = now.strftime('% z')
if not zonesGroupedByO ffset.has_key(o ffsetKey):
zonesGroupedByO ffset[offsetKey] = []
zonesGroupedByO ffset[offsetKey].append(name)
return zonesGroupedByO ffset
z = getZonesGrouped ByOffset()
print z['+0000']
# Why is America/Iqaluit showing up here???
getting a strange result. America/Iqaluit should not be showing up in
UTC+00:00, but it is. Can anyone see what's wrong with this code?
import time
from datetime import datetime
import os
import pytz
def getZoneNamesOnT hisComputer():
for i in pytz.all_timezo nes:
if os.path.exists( os.path.join('/tmp/usr/share/zoneinfo/',
i)):
yield i
def getZonesGrouped ByOffset():
'''return a dict of all the zone names , grouped by their offset
eg:
{'-0400': ['America/Blanc-Sablon', 'Etc/GMT+4'],
'+0000': ['America/Iqaluit', 'Etc/GMT', 'Europe/Belfast', 'GB',
'GMT'],
...
}
'''
Y,M,D,h,m,s = time.localtime( )[:6]
zonesGroupedByO ffset = {}
for name in getZoneNamesOnT hisComputer():
tz = pytz.timezone(n ame)
now = datetime(Y,M,D, h,m,s, tzinfo=tz)
# strfime returns a string like "-0800" or "+0430" or "0000"
offsetKey = now.strftime('% z')
if not zonesGroupedByO ffset.has_key(o ffsetKey):
zonesGroupedByO ffset[offsetKey] = []
zonesGroupedByO ffset[offsetKey].append(name)
return zonesGroupedByO ffset
z = getZonesGrouped ByOffset()
print z['+0000']
# Why is America/Iqaluit showing up here???