This is thoroughly confusing me. I have tried in multiple ways to convert a datetime string of 2017-06-04 00:00:00 (America/New_York)
to the Europe/London
timezone, which should be I believe with current daylight savings 6am?
The whole concept of the dates and conversion is baffling me, I simply seem to misunderstand, or not get the simple principles, this picture demonstrates the current code i have and the results im getting.
I simply want to be able to give a function, a datetime
string and what timezone it belongs too and give it a timezone I want it in and get a string back of the adjusted datetime
, but I am getting stuff which is 4 minutes out etc..
from datetime import datetime
import pytz
import arrow
#Calc Timezone Offsets
def adjust_timezone(datetime_string, input_tz, output_tz, formatter="%Y-%m-
%d %H:%M:%S"):
print datetime_string
# Now we make datetime naive datetime
date_to_convert = datetime.strptime(datetime_string, formatter)
a = arrow.get(datetime_string, 'YYYY-M-D HH:mm:ss').replace(tzinfo=pytz.utc)
print a.to(output_tz).format('YYYY-M-D HH:mm:ss')
# Make it tz aware
date_to_convert = date_to_convert.replace(tzinfo=pytz.timezone(input_tz))
print date_to_convert
date_to_convert = date_to_convert.astimezone(pytz.timezone(input_tz))
print date_to_convert
# Convert it
output_datetime = date_to_convert.astimezone(pytz.timezone(output_tz))
print output_datetime
# Make tz unaware again
output_datetime = output_datetime.replace(tzinfo=None)
return str(output_datetime)
print adjust_timezone('2017-06-04 00:00:00', "America/New_York", "Europe/London")
The above when run return the following results:
2017-06-04 00:00:00
2017-6-4 01:00:00
2017-06-04 00:00:00-04:56
2017-06-04 00:00:00-04:56
2017-06-04 05:56:00+01:00
2017-06-04 05:56:00