0

I'm trying to convert times with this function:

import pytz
from datetime import datetime

def ConvertTimezone(FromZone, timestring, ToZone):
    print("Start of ConvertTimezone from " + str(FromZone) + " " + timestring + " to " + str(ToZone))
    TFORMAT = "%Y %m %d %H:%M"
    ftz = pytz.timezone(FromZone)
    ttz = pytz.timezone(ToZone)
    dt_str = datetime.strptime(timestring, TFORMAT)
    dt_obj_ftz = ftz.localize(dt_str) #localising accounts for daylight savings          
    totime = dt_obj_ftz.astimezone(ttz)
    return totime.strftime(TFORMAT)

print(ConvertTimezone("Etc/GMT+10", "2020 08 30 12:00", "Etc/GMT-12"))

I expect the output to be

"2020 08 29 14:00" # the day before

but instead I get this, as if it has ADDED the timezone difference (22 hours) rather than subtracted it:

"2020 08 31 10:00" # the day after

Users are supposed to be able to use any of the timezones in pytz.all_timezones. What should I be doing for this to work?

1
  • Note, you should prefer locality-based time zones (such as Australia/Melbourne) whenever possible. The Etc/GMT±X zones are generally used for ships at sea. Aug 26, 2020 at 15:54

1 Answer 1

0

Found the result here: Printing datetime as pytz.timezone("Etc/GMT-5") yields incorrect result Apparently the pluses and minuses in pytz's Etc/GMT timezones are inverted for some reason.

1
  • 1
    Hi. I've closed your question as a duplicate to the link you provided, and another. (The question is a good one, but has been answered before.) You should delete this answer, as link-only answers are discouraged. Thanks. Aug 26, 2020 at 15:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.