1

I am new with python but I have passed sometime. I have played with datetime API. I have one requirement and definitely help will be some where but I am not able to find out. Actually I am converting UTC time to PST. It is working fine with me.

tz = timezone("US/Pacific")
utc_date_time = datetime.now(tz=pytz.utc)
opr_date = utc_date_time.astimezone(tz)
print(opr_date)

But I am not able to get GMT time and convert it into PST. Here are 2 questions

  1. How can I get GMT time
  2. How will it convert into PST

I am thankful in advance.

Awais Khan
  • 35
  • 1
  • 5
  • do you understand that your code already "gets GMT time and converts it into PST"? btw, you can [get the current time in a given timezone directly](http://stackoverflow.com/a/16660476/4279) (without the explicit conversion from utc time). – jfs Feb 24 '16 at 20:17
  • Yes, @J.F.Sebastian you are right. It was just misunderstanding of different time zone. Thanks for your comment. – Awais Khan Feb 25 '16 at 11:49

1 Answers1

0

Try this

To get time in GMT

import os
import time
os.environ["TZ"]="GMT"
print time.strftime("%T %Z", time.localtime(time.time()))

To get it in PST

from datetime import datetime
from pytz import timezone
import pytz
date_format='%m/%d/%Y %H:%M:%S %Z'
date = datetime.now(tz=pytz.utc)
date = date.astimezone(timezone('US/Pacific'))
print 'Local date & time is  :', date.strftime(date_format) 

Local date & time is  : 12/19/2017 01:09:08 PST
Anurag jain
  • 2,281
  • 1
  • 11
  • 10