Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Python and timezones

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


shandy.b at gmail

Jul 1, 2008, 10:43 PM

Post #1 of 4 (243 views)
Permalink
Python and timezones

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???
--
http://mail.python.org/mailman/listinfo/python-list


mensanator at aol

Jul 2, 2008, 11:22 AM

Post #2 of 4 (230 views)
Permalink
Re: Python and timezones [In reply to]

On Jul 2, 12:43 am, "shand...@gmail.com" <shand...@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.
--
http://mail.python.org/mailman/listinfo/python-list


shandy.b at gmail

Jul 3, 2008, 4:18 PM

Post #3 of 4 (224 views)
Permalink
Re: Python and timezones [In reply to]

Nah, it's got to be something more than that. A bunch of them are
screwed up. It's showing America/Saskatchewan as UTC-06:59.

I think there's something wrong with my code. I've put it up at a
pastebin so that it's easier to read.

http://pastebin.ca/1061776

On Jul 2, 11:22 am, Mensanator <mensana...@aol.com> wrote:
> On Jul 2, 12:43 am, "shand...@gmail.com" <shand...@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.

--
http://mail.python.org/mailman/listinfo/python-list


shandy.b at gmail

Jul 3, 2008, 6:19 PM

Post #4 of 4 (222 views)
Permalink
Re: Python and timezones [In reply to]

I found a solution:

http://groups.google.com/group/comp.lang.python/browse_thread/thread/aea64448eaca4778/250e908652e2646d

If I use datetime.now(tz=tz) instead of datetime(Y,M,D,h,m,s,
tzinfo=tz), it works.


On Jul 3, 4:18 pm, "shand...@gmail.com" <shand...@gmail.com> wrote:
> Nah, it's got to be something more than that.  A bunch of them are
> screwed up.  It's showing America/Saskatchewan as UTC-06:59.
>
> I think there's something wrong with my code.  I've put it up at a
> pastebin so that it's easier to read.
>
> http://pastebin.ca/1061776
>
> On Jul 2, 11:22 am, Mensanator <mensana...@aol.com> wrote:
>
> > On Jul 2, 12:43 am, "shand...@gmail.com" <shand...@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.
>
>

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.