KenK73
Posts: 23
Joined: Sun Mar 18, 2018 6:32 pm

pytz timezone conversion help

Sun Apr 22, 2018 10:17 am

Hello,

I tried to search the forum for this, but it keeps giving me a "high traffic at this time" failure.

I want to convert a UTC time into 6 other timezones. I installed PIP3.2 and used that to install pytz, but the system wouldnt recognize that pytz was installed. So I used easy_install and that seems to have worked, or at least my python script doesnt fail now.

My shell is set to America/Phoenix and my clock script will show my local time if I run it without any pytz nonsense. I can create a UTC time and get that working fine, but when I try to take that object and translate it to any other timezone, the script just freezes.

Code: Select all

#!/usr/bin/python

import time
import datetime
from pytz import timezone
import pytz

while(True):
  utc_now = pytz.utc.localize(datetime.datetime.utcnow())
  hour = utc_now.hour
  minute = utc_now.minute
  second = utc_now.second

  print('UTC:', utc_now)

  mst_now = pytz.utc.localize(datetime.datetime.utcnow())
  hour = mst_now.hour
  minute = mst_now.minute
  second = mst_now.second

  print('MST:', mst_now)

  pst_now = pytz.utc.localize(datetime.datetime.utcnow())
  hour = pst_now.hour
  minute = pst_now.minute
  second = pst_now.second

  print('PST:', pst_now)
The above works, or at least it gives me 3 UTC's. What do I need to change to get MST/PST on the last two? I have tried all kinds of things from Google (astimezone, pytz.timezone, etc) and it's all crap. Everything I have tried just causes the script to hang after it prints the first UTC.

KenK73
Posts: 23
Joined: Sun Mar 18, 2018 6:32 pm

Re: pytz timezone conversion help

Sun Apr 22, 2018 11:07 am

So this is what happens when you're a moron and you have no idea what's included with your distro...

My Raspbian has python 2.7 as default, so installing PIP3.2 was not a good move. I removed it and the pytz it installed then installed PIP for 2.6 (yeah, whatever) and then removed and re-installed pytz using that PIP. And I rebooted for good measure. This fixed the lockup issues. Now I have my timezones working:

Code: Select all

#!/usr/bin/python

import time
import datetime
import pytz

while(True):
  utc_now = pytz.utc.localize(datetime.datetime.utcnow())
  hour = utc_now.hour
  minute = utc_now.minute
  second = utc_now.second

  print('UTC:', utc_now)
  
  mst_now = utc_now.astimezone(pytz.timezone("America/Phoenix"))
  hour = mst_now.hour
  minute = mst_now.minute
  second = mst_now.second

  print('MST:', mst_now)
  
  pst_now = utc_now.astimezone(pytz.timezone("Pacific/Honolulu"))
  hour = pst_now.hour
  minute = pst_now.minute
  second = pst_now.second

  print('PST:', pst_now)
  

User avatar
scruss
Posts: 1800
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: pytz timezone conversion help

Sun Apr 22, 2018 12:21 pm

I know you're likely removed the packages now, but changing this

Code: Select all

#!/usr/bin/python
to this

Code: Select all

#!/usr/bin/python3
probably would have fixed the problem.

pip3 installs packages for Python 3, pip for Python 2. /usr/bin/python is Python 2
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

KenK73
Posts: 23
Joined: Sun Mar 18, 2018 6:32 pm

Re: pytz timezone conversion help

Mon Apr 23, 2018 3:51 am

Thanks for the tip. I wasn't sure if I had partially installed Python3 in my attempt to get pytz or if it was already there. As I get more familiar I will hopefully do less dumb stuff. :)

Return to “Python”

Who is online

Users browsing this forum: No registered users and 19 guests