convert gmt to australian western time in python

To convert GMT to Australian Western Time (AWT) in Python, you can use the datetime module, timezone module from Python's standard library, and pytz library, which allows working with time zones in Python.

Here is an example code snippet:

main.py
import datetime
import pytz

# create a datetime object representing the GMT time
gmt_time = datetime.datetime.utcnow()

# set the timezone to GMT
gmt_timezone = pytz.timezone('GMT')
gmt_time = gmt_timezone.localize(gmt_time)

# convert the GMT time to Australian Western Time (AWT)
awt_timezone = pytz.timezone('Australia/Perth')
awt_time = gmt_time.astimezone(awt_timezone)

# print the results
print('GMT time: ', gmt_time.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
print('AWT time: ', awt_time.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
527 chars
18 lines

In this example, we first create a datetime object representing the current GMT time. We then set the timezone to GMT using the pytz.timezone function, and localize the datetime object to this timezone.

Next, we create another timezone object for Australian Western Time and use the astimezone method to convert the GMT time to AWT.

Finally, we print the results, using the strftime method to format the date and time strings.

gistlibby LogSnag