2

I'm trying to make a function that can convert one timezone to another timezone.

def SetTimezone(TimezoneIn, TimezoneOut, year, month, day, hours, minutes, seconds):
  local = pytz.timezone(TimezoneOut)
  naive = datetime.strptime(f"{year}-{month}-{day} {hours}:{minutes}:{seconds}", "%Y-%m-%d %H:%M:%S")
  local_dt = local.localize(naive, is_dst=None)
  utc_dt = local_dt.astimezone(getattr(pytz, TimezoneIn))
  print("utc_dt:", utc_dt)

SetTimezone("utc", "America/Los_Angeles", 2022, 12, 16, 23, 21, 45)

The time is supposed to be 3:21 PM, 45 seconds in, on December 16th 2022. However, the output is instead returning as this:

utc_dt: 2022-12-17 07:21:45+00.00

Tried checking the UTC timezone in 24-hour time and it was 23:21:45 as I put into the arguments correctly. I don't know if this is important, but this code is for a Discord bot that will have a command that can convert timezones. Does anyone know why this problem is happening or, better yet, a more efficient way to convert timezones easily?

Yozy Opto
  • 23
  • 5

0 Answers0