5

I am trying to convert a UTC date/time to 'Europe/London' timezone. I have tried using tz.localize using the date but I am told the datetime is not naive. If I try to remove the the timezone I still get the same error.

Fundamentally, I am trying to get this date: 04/05/2016 15:00 to become this date: 04/05/2016 14:00

My code is as follows where I am parsing through the date using iso8601.

date = 2016-05-04 15:00:00+00:00
fix_pars = iso8601.parse_date(str(date))

I have tried used pytz.localize but with no success as it doesn't like the fact my date already has a timezone set.

I find the documentation on this really confusing. All I want is the magic function that takes a date and converts based on a requested timezone. What function am I best to use?

LemusThelroy
  • 293
  • 1
  • 6
  • 15
  • `date = 2016-05-04 15:00:00+00:00` is not valid Python. Why do you call `str(date)`? What is `type(date)`? – jfs May 04 '16 at 17:44

2 Answers2

14
import pytz
from datetime import datetime
tz = pytz.timezone('UTC')
naive_time = datetime.strptime('2016-04-05 15:00', '%Y-%m-%d %H:%M')
tz_time = tz.localize(naive_time)
london_tz = pytz.timezone('Europe/London')
london_time = tz_time.astimezone(london_tz)

And here are my outputs:

>>> print tz_time
2016-04-05 15:00:00+00:00
>>> print london_time
2016-04-05 16:00:00+01:00
Scratch'N'Purr
  • 9,959
  • 2
  • 35
  • 51
  • Amended my code to: date = '2016-06-11 16:00:00+00:00' naive_time = datetime.strptime(date, '%Y-%m-%d %H:%M') tz_time = tz.localize(naive_time) london_time = tz_time.astimezone(london_tz) But I get... data_string[found.end():]) ValueError: unconverted data remains: :00+00:00 – LemusThelroy May 04 '16 at 14:37
  • @LemusThelroy You would have to convert that to a string then remove the timezone from it: `str(theTime)[:-6]` and then if you want to update the `naive_time` variable to include the seconds as well `naive_time = datetime.strptime(str(theTime)[:-6], '%Y-%m-%d %H:%M:%S')` – Scratch'N'Purr May 04 '16 at 14:40
  • If I wanted to convert the london_time to dd/mm/yyyy hh:mm format, what function should I use? I will then go and read the documentation on this. – LemusThelroy May 04 '16 at 14:56
  • @LemusThelroy You can still use the [strptime](http://www.tutorialspoint.com/python/time_strptime.htm) method. You'll just have to edit the formating string to do it: `naive_time = datetime.strptime('05/04/2016 15:00', '%d/%m/%Y %H:%M')` – Scratch'N'Purr May 04 '16 at 15:07
  • Hi I want to convert Asia honk Kong time to new York time zone but I am getting wrong hours.normally there is difference between both is 12 hrs but I am getting difference as 20hrs – ThinkTank Apr 25 '22 at 08:26
3

You don't need tz.localize with utc timezone because it has a fixed utc offset (zero). If you have a timezone-aware datetime object (as its text representation suggests) then to convert it to Europe/London timezone:

london_dt = aware_dt.astimezone(pytz.timezone('Europe/London'))

that is all.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670