0

I am new to programming and I've been trying to create a python script that will take an input time/date from UTC timezone and provide the same time in Pacific.

The problem with my script is that for some reason, the output is not converting properly to the PST timezone, and I cannot figure out what I am doing wrong.

Was wondering if anyone could give me some tips here, thanks.

import os
import subprocess
import pytz
import keyboard
from datetime import datetime
import time
import parse
import sys

dt_utcnow = datetime.now(tz=pytz.timezone('UTC'))
print( "The current UTC time is:", dt_utcnow.strftime("%m/%d/%Y %I:%M:%S %p"))
dt_pacnow = datetime.now(tz=pytz.timezone('US/Pacific'))
print( "The current Pacific time is:", dt_pacnow.strftime("%m/%d/%Y %I:%M:%S %p"))
dt_utc_input = input('Please input the UTC time to convert to Pacific time (Input as MM/DD/YYYY HH:MM:SS AM/PM):')
unaware_utc = datetime.strptime(dt_utc_input, "%m/%d/%Y %I:%M:%S %p")
aware_utc = unaware_utc.astimezone(tz=pytz.timezone('Universal'))
aware_pac = unaware_utc.astimezone(tz=pytz.timezone('US/Pacific'))
print(aware_pac.strftime("%m/%d/%Y %I:%M:%S %p"))

Here is an example output from the above code

The current UTC time is: 09/30/2019 08:49:01 PM
The current Pacific time is: 09/30/2019 01:49:02 PM
Please input the UTC time to convert to Pacific time (Input as MM/DD/YYYY HH:MM:SS AM/PM):09/30/2019 08:49:01 PM
09/30/2019 06:49:01 PM
deltch
  • 1
  • See [this.](https://stackoverflow.com/questions/4563272/convert-a-python-utc-datetime-to-a-local-datetime-using-only-python-standard-lib) I think your timezone code isn't replacing correctly. – kokeen Sep 30 '19 at 21:13
  • I'm assuming on dt_utc_input right? How could I apply the timezone to an input? – deltch Sep 30 '19 at 21:22
  • I can't reproduce your issue. If I copy, paste and run your code with the input in your example, I get "09/30/2019 01:49:01 PM" from your last print statement. As an aside, you may want to remove all the unnecessary imports and the steps to get the current UTC and pacific datetimes from your example to trim it down. – benvc Sep 30 '19 at 21:53
  • @benvc I figured some of my imports were probably unnecessary but are there any that would potentially cause what I am experiencing? – deltch Sep 30 '19 at 22:26
  • Not unless there is additional code on your end that you have not included in the example you posted. – benvc Sep 30 '19 at 22:37
  • @benvc I've just managed to narrow it down to the input time corresponding to my native system time (Which is in CDT). By inputting my current system time of 6:42 PM, I found that the script is actually converting that to Pacific. Is there a way that I can explicitly define my input time as *being* UTC? `The current UTC time is: 09/30/2019 11:42:31 PM The current Pacific time is: 09/30/2019 04:42:31 PM Please input the UTC time to convert to Pacific time (Input as MM/DD/YYYY HH:MM:SS AM/PM):09/30/2019 6:42:31 PM 09/30/2019 04:42:31 PM` – deltch Sep 30 '19 at 23:45
  • Try simplifying things a bit. First: `dt = datetime.strptime('09/30/2019 6:42:31 PM', '%m/%d/%Y %I:%M:%S %p')`. Second: `utc = dt.astimezone(tz=pytz.timezone('UTC'))`. Third: `pacific = utc.astimezone(tz=pytz.timezone('America/Los_Angeles'))`. Print each of those variables to see the output of each step in the process. First, you are parsing an input string and converting to datetime, then you are setting the timezone of the unaware datetime to UTC, then you are converting UTC to Pacific Time. Is that what you are intending to do? – benvc Oct 01 '19 at 02:02
  • Just thought of something. There was a change in `datetime.astimezone` behavior in Python 3.6. What version of Python are you using? You may be better off using `pytz.localize()` depending on what version of Python you are on. For example: https://stackoverflow.com/questions/54374804/converting-datetime-from-one-time-zone-to-another-using-pytz – benvc Oct 01 '19 at 02:17

0 Answers0