Python and timezones

Mensanator mensanator at aol.com
Wed Jul 2 14:22:44 EDT 2008


On Jul 2, 12:43 am, "shand... at gmail.com" <shand... at gmail.com> wrote:
> 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 getZoneNamesOnThisComputer():
>     for i in pytz.all_timezones:
>         if os.path.exists( os.path.join('/tmp/usr/share/zoneinfo/',
> i)):
>             yield i
>
> def getZonesGroupedByOffset():
>     '''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]
>
>     zonesGroupedByOffset = {}
>
>     for name in getZoneNamesOnThisComputer():
>         tz = pytz.timezone(name)
>         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 zonesGroupedByOffset.has_key(offsetKey):
>             zonesGroupedByOffset[offsetKey] = []
>         zonesGroupedByOffset[offsetKey].append(name)
>     return zonesGroupedByOffset
>
> z = getZonesGroupedByOffset()
> print z['+0000']
>
> # Why is America/Iqaluit showing up here???

The database contains an error. Shit happens, deal with it.



More information about the Python-list mailing list