Serious Problem with Timezone

Jason Scheirer jason.scheirer at gmail.com
Mon May 19 19:05:15 EDT 2008


On May 19, 3:02 pm, T-u-N-i-X <alperka... at gmail.com> wrote:
> Hey There,
>
> I'm a django developer and working on a project right now.. Last week
> I just discovered a new problem in Python.. Here's what I do..
>
> [01:00] (tunix at penguix ~)$ date
> Sal May 20 01:00:10 EEST 2008
> [01:00] (tunix at penguix ~)$ python
> Python 2.5.2 (r252:60911, Feb 23 2008, 21:20:32)
> [GCC 4.2.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.>>> from datetime import datetime
> >>> datetime.now()
>
> datetime.datetime(2008, 5, 20, 1, 0, 21, 131804)>>> import os
> >>> os.environ["TZ"] = "Europe/Istanbul"
> >>> datetime.now()
>
> datetime.datetime(2008, 5, 19, 22, 0, 38, 578438)
>
>
>
> It's 01:00 in Istanbul now and Python shows 22:00 on 19th of May if I
> set the TZ environment variable.. Django sets that variable
> automatically so I'm having problems with scheduled posts..
>
> I controlled my system's BIOS time.. It was wrong before, so I just
> corrected it.. I set the time to UTC on Linux.. What else can I do ?

You may want to investigate the datetime.astimezone() method, as well
as getting comfortable with using tzinfo objects. Check out
http://pypi.python.org/pypi/pytz/ for a module that can give you the
tz objects you want. Also useful is datetime.utcnow()* and
datetime.replace(tzinfo=other_tzinfo), which will give you that same
time but not 'smartly' try to adjust the components in the datetime
object.

I've found datetime.utcnow() is a little temperamental (bad tzinfo is
assigned by default, making it impossible to do conversions) and you
still need to do datetime.utcnow().replace(tzinfo=utctz) to get it to
behave well.



More information about the Python-list mailing list