convert gmt to australian central standard time in python

To convert from GMT to Australian Central Standard Time in Python, you can use datetime, timezone, timedelta modules.

main.py
from datetime import datetime, timezone, timedelta

# set the input datetime in gmt timezone
input_datetime = datetime(2021, 3, 15, 10, 30, tzinfo=timezone.utc)

# convert the input datetime to Australian Central Standard Time
output_datetime = input_datetime.astimezone(timezone(timedelta(hours=9.5)))

# print the output datetime
print(output_datetime)
355 chars
11 lines

In the above code, we set the input datetime as 2021-03-15 10:30:00 in the GMT timezone by using the datetime and timezone modules. We then use the astimezone method to convert the input datetime to Australian Central Standard Time by passing the timezone offset of 9.5 hours to the timezone constructor. Finally, we print the output datetime using the print function.

Note: The above code assumes that the input datetime is in the GMT timezone. If the input datetime is in a different timezone, you need to convert it to the GMT timezone first before applying the above code.

gistlibby LogSnag