Tags: bug, code, conversion, convert, datetime, feature, following, hii, hint, local, programming, python, strange, surprised, tzinfo, whetherthat
datetime, tzinfo ... strange conversion?
On Programmer » Python
2,376 words with 1 Comments; publish: Wed, 07 May 2008 12:40:00 GMT; (20078.13, « »)
Hi!
I'm surprised about the following code, maybe you can give me a hint whether
that's bug or feature? I'm just trying to convert local time to GMT and one
method gives a strange result:
######################
> #! /usr/bin/env python > import datetime, pytz > _tz_utc = pytz.timezone("UTC") > _tz_germany = pytz.timezone("Europe/Berlin") > local = datetime.datetime.now(_tz_germany) > print "Local: ", local > gmt = local.astimezone(_tz_utc) > print "GMT (1)", gmt > gmt = datetime.datetime.combine(local.date(),
local.timetz()).astimezone(_tz_utc) > print "GMT (2)", gmt > gmt = datetime.datetime.combine(datetime.date.today(),
datetime.time(local.hour, local.minute, local.second, local.microsecond,
_tz_germany)).astimezone(_tz_utc) > print "GMT (3)", gmt > gmt = datetime.datetime.combine(datetime.date.today(),
datetime.time(local.hour, local.minute, local.second, local.microsecond,
local.tzinfo)).astimezone(_tz_utc) > print "GMT (4)", gmt > print "_tz_germany:", _tz_germany, "(id: %s)" % id(_tz_germany), ",
local.tzinfo:", local.tzinfo, "(id: %s)" % id(local.tzinfo)
######################
Result:
> tlange.python.itags.org.tinux:~/is.work/Scripts$ ./timeconv.py > Local: 2006-04-01 20:09:26.469445+02:00 > GMT (1) 2006-04-01 18:09:26.469445+00:00 > GMT (2) 2006-04-01 18:09:26.469445+00:00 > GMT (3) 2006-04-01 19:09:26.469445+00:00 > GMT (4) 2006-04-01 18:09:26.469445+00:00 > _tz_germany: Europe/Berlin (id: -1212869684) , local.tzinfo: Europe/Berlin
(id: -1212868756)
--> Why is GMT (3) wrong?
Cheers,
Tino
http://python.itags.org/q_python_19137.html
All Comments
Leave a comment...
- 1 Comments

- In article <e0mfrn$h1s$1.python.itags.org.newsreader3.netcologne.de>,
Tino Lange <tl_news.python.itags.org.nexgo.de> wrote:
>--> Why is GMT (3) wrong?
At a guess, it's because there's nothing in the parameters passed to
indicate that daylight saving is currently in effect.
Tip: always do your date/time calculations as far as possible in UTC.
Only convert to local time at the last possible step, before displaying
results to the user.
#1; Wed, 07 May 2008 12:42:00 GMT