convert australian western time to gmt in python

To convert Australian Western Time to GMT in Python, you can use the datetime module to create a datetime object with the desired time in Australian Western Time, and then use the astimezone() method to convert it to GMT.

Here's an example code snippet that demonstrates this conversion:

main.py
from datetime import datetime
from pytz import timezone

# create a datetime object with the Australian Western Time
awt_time = datetime(2021, 6, 1, 14, 30, tzinfo=timezone('Australia/Perth'))

# convert the datetime object to GMT
gmt_time = awt_time.astimezone(timezone('GMT'))

# print the GMT time
print(gmt_time)
317 chars
12 lines

In this example, we create a datetime object with the Australian Western Time on June 1st, 2021 at 2:30 PM. We specify the timezone using the timezone() method from the pytz module. Then, we use the astimezone() method to convert the awt_time object to GMT, which is specified using the timezone() method again. Finally, we print the resulting gmt_time object.

gistlibby LogSnag