Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > pytz giving incorrect offset and timezone

Reply
Thread Tools

pytz giving incorrect offset and timezone

 
 
Sanjay
Guest
Posts: n/a
 
      07-12-2007
Hi All,

Using pytz, I am facing a problem with Asia/Calcutta, described below.

Asia/Calcutta is actually IST, which is GMT + 5:30. But while using
pytz, it was recognized as HMT (GMT + 5:53). While I digged into the
oslan database, I see the following:

# Zone NAME GMTOFF RULES FORMAT [UNTIL]
Zone Asia/Calcutta 5:53:28 - LMT 1880 # Kolkata
5:53:20 - HMT 1941 Oct # Howrah Mean Time?
6:30 - BURT 1942 May 15 # Burma Time
5:30 - IST 1942 Sep
5:30 1:00 IST 1945 Oct 15
5:30 - IST

Searching in this group, I saw a similar problem posted at
http://groups.google.co.in/group/com...496e85797ac890
without any solutions.

I mailed to Stuart and also posted it at the launchpad of pytz, but
did not get any response.

Unable to know how to proceed further. Any suggestion will be of vital
help.

thanks
Sanjay

 
Reply With Quote
 
 
 
 
Simon Percivall
Guest
Posts: n/a
 
      07-12-2007
On Jul 12, 11:47 am, Sanjay <skpate...@gmail.com> wrote:
> Hi All,
>
> Using pytz, I am facing a problem with Asia/Calcutta, described below.
>
> Asia/Calcutta is actually IST, which is GMT + 5:30. But while using
> pytz, it was recognized as HMT (GMT + 5:53). While I digged into the
> oslan database, I see the following:
>
> # Zone NAME GMTOFF RULES FORMAT [UNTIL]
> Zone Asia/Calcutta 5:53:28 - LMT 1880 # Kolkata
> 5:53:20 - HMT 1941 Oct # Howrah Mean Time?
> 6:30 - BURT 1942 May 15 # Burma Time
> 5:30 - IST 1942 Sep
> 5:30 1:00 IST 1945 Oct 15
> 5:30 - IST
>
> Searching in this group, I saw a similar problem posted athttp://groups.google.co.in/group/comp.lang.python/browse_thread/threa...
> without any solutions.
>
> I mailed to Stuart and also posted it at the launchpad of pytz, but
> did not get any response.
>
> Unable to know how to proceed further. Any suggestion will be of vital
> help.
>
> thanks
> Sanjay


I don't use pytz myself that often so I can't be sure, but I don't
think it's a bug in pytz.

The problem seems to be that the timezone has changed for the
location. Now, without a date as reference, pytz can't know what
timezone to use when constructing the tzinfo; you might want a date
from the 1800's.

When you're constructing the datetime with the tzinfo argument, you're
saying: use this timezone as the local timezone. datetime_new (the
constructor in C) never calls the tzinfo to verify that the timezone
is still valid, it just uses it.

On the other hand: When you construct a datetime with datetime.now()
and pass a timezone, datetime_now (again, in C) calls the method
fromutz() on the tzinfo object. Now the pytz tzinfo object has a
reference by which to choose the current timezone for the location,
and that's why it's correct when you use datetime.now() but not for a
manual construction.

A "workaround" (or maybe the proper way to do it) is to construct the
datetime without a tzinfo set, and then use the localize() method on
the tzinfo object, this will give you the correct result.

>>> tz = pytz.timezone("Asia/Calcutta")
>>> mydate = datetime.datetime(2007, 2, 18, 15, 35)
>>> print tz.localize(mydate)

2007-02-18 15:35:00+05:30

 
Reply With Quote
 
 
 
 
Sanjay
Guest
Posts: n/a
 
      07-18-2007
Hi Simon,

localize worked perfectly. Thanks a lot for the vital help! Your
elaboration on the concepts was also very nice and informative!

thanks
Sanjay

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing offset-aware and offset-naive datetimes? Roy Smith Python 4 01-27-2013 03:17 PM
fromutc: dt.tzinfo is not self: pytz.timezone('UTC').fromutc(datetime.utcnow()) aspineux Python 3 10-21-2011 04:06 PM
Update pytz timezone definitions _robby Python 1 03-14-2008 12:50 PM
pytz giving incorrect offset and timezone Sanjay Python 0 07-13-2007 11:55 AM
Datetime, pytz and strange offset David Pratt Python 0 12-13-2005 10:48 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57