-1

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
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135

1 Answers1

1

Although arrow is a fine library, we don't really need it here. Instead we will use datetime and pytz for this. The key elements are pytz.timezone().localize() and datetime.datetime.astimezone()

Code:

def adjust_timezone(datetime_string, input_tz, output_tz,
                    formatter="%Y-%m-%d %H:%M:%S"):
    # Now we make datetime naive datetime
    naive = dt.datetime.strptime(datetime_string, formatter)

    # this time stamp is from the input_tz
    with_input_tz = pytz.timezone(input_tz).localize(naive)

    # now express the timestamp as output_tz
    with_output_tz = with_input_tz.astimezone(pytz.timezone(output_tz))
    return with_output_tz

Test Code:

import datetime as dt
import pytz

print(adjust_timezone(
    '2017-06-04 00:00:00', "America/New_York", "Europe/London"))

Results:

2017-06-04 05:00:00+01:00
Community
  • 1
  • 1
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • Many thanks, it works perfectly for me, I think I read so much without absorbing it, it all just became completely confusing.. Just as a side question, where the new datetime has +0100 at the end, is that included within the 0500 and is just an indicator? I assume but maybe wrongly that it represents the daylight adjustment, so I could infact format this as a string, negating the +0100 – Ron Appleton Jun 07 '17 at 10:59
  • 1
    The +1 is simply information in the print routine, that indicates the offset from GMT. It will be seen on non-naive timestamps that are printed without using `strftime`. – Stephen Rauch Jun 07 '17 at 13:31
  • Thanks so much @Stephen – Ron Appleton Jun 13 '17 at 00:02