convert gmt to pacific time in python

To convert GMT to Pacific Time in Python, you can use the datetime module and pytz library. Here's a sample code for converting:

main.py
import pytz
from datetime import datetime

# create datetime object for GMT time
gmt_time = datetime.now(pytz.timezone('GMT'))

# convert to Pacific Time
pst_time = gmt_time.astimezone(pytz.timezone('US/Pacific'))

# print the converted time
print('GMT time:', gmt_time)
print('Pacific Time:', pst_time)
304 chars
13 lines

In the above code, we are first creating a datetime object for the current time in GMT timezone. We then convert it to the US/Pacific timezone using the .astimezone() method. Finally, we print both the GMT and Pacific times using the print() function.

gistlibby LogSnag