convert pacific time to gmt in python

To convert a Pacific Time datetime object to GMT in Python, you can use the pytz module to define the time zones and then use the astimezone method to convert the datetime. Here's an example code snippet:

main.py
import pytz
from datetime import datetime

# define Pacific Time zone
pacific = pytz.timezone('US/Pacific')

# define GMT timezone
gmt = pytz.timezone('GMT')

# create datetime object for Pacific Time
pacific_time = datetime(2022, 10, 31, 11, 30)

# convert Pacific Time to GMT
gmt_time = pacific_time.astimezone(gmt)

print("Pacific Time:", pacific_time)
print("GMT Time:", gmt_time)
385 chars
18 lines

Output:

main.py
Pacific Time: 2022-10-31 11:30:00
GMT Time: 2022-10-31 18:30:00+00:00
70 chars
3 lines

In this example, we first defined both the Pacific Time and GMT time zones using the pytz.timezone function. We then created a datetime object for the Pacific Time to be converted to GMT. Finally, we used the astimezone method to convert the Pacific Time to GMT and printed both the Pacific Time and GMT Time.

gistlibby LogSnag