3

Possible Duplicate:
Python datetime object show wrong timezone offset

I have a problem with conversion between timezones in Python, using the pytz library (last version 2012h). Here in Brussels we have normally UTC+1 hour in winter and UTC+2 hours in summer.

I have the following code :

from datetime import datetime

import pytz

brussels_tz = pytz.timezone('Europe/Brussels')
utc_tz = pytz.utc

def main():
    intermdate = datetime(2012, 07, 15, 8, 0, 0, 0, brussels_tz)
    utcdate = intermdate.astimezone(utc_tz)
    print "Brussels time is %s" % intermdate
    print "UTC time is %s" % utcdate

if __name__ == '__main__':
    main()

The problem is that I get the following result :

Brussels time is 2012-07-15 08:00:00+00:00
UTC time is 2012-07-15 08:00:00+00:00

So no difference. In my opinion the result should be (in summer):

Brussels time is 2012-07-15 08:00:00+02:00
UTC time is 2012-07-15 06:00:00+00:00

If I use the timezone Europe/Paris (normally the same time as in Brussels) I get even more strange results:

Paris time is 2012-07-15 08:00:00+00:09
UTC time is 2012-07-15 07:51:00+00:00

A 9 minutes difference !?!

Could anybody help me?

Community
  • 1
  • 1
Mercator
  • 33
  • 3

1 Answers1

9

You need to use the .localize() method to move a datetime into a timezone:

intermdate = brussels_tz.localize(datetime(2012, 07, 15, 8, 0, 0, 0))
utcdate = intermdate.astimezone(utc_tz)

The output is then:

Brussels time is 2012-07-15 08:00:00+02:00
UTC time is 2012-07-15 06:00:00+00:00

See the pytz documentation:

Unfortunately using the tzinfo argument of the standard datetime constructors ‘’does not work’’ with pytz for many timezones.

>>> datetime(2002, 10, 27, 12, 0, 0, tzinfo=amsterdam).strftime(fmt)
'2002-10-27 12:00:00 AMT+0020'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343