convert australian central time to gmt in python

You can convert Australian Central Time (ACT) to GMT using the datetime and timezone modules in Python. Here's an example code snippet to demonstrate the conversion:

main.py
from datetime import datetime
import pytz

# create a datetime object with the ACT timezone
act_tz = pytz.timezone('Australia/Adelaide')
act_time = datetime.now(act_tz)

# convert to GMT timezone
gmt_tz = pytz.timezone('GMT')
gmt_time = act_time.astimezone(gmt_tz)

# print the results
print("ACT time:", act_time.strftime("%Y-%m-%d %H:%M:%S %Z%z"))
print("GMT time:", gmt_time.strftime("%Y-%m-%d %H:%M:%S %Z%z"))
414 chars
15 lines

In this example, we first create a datetime object with the ACT timezone using the pytz.timezone() function. Then, we convert this to the GMT timezone using the astimezone() function and another pytz.timezone() call. Finally, we print the results using the strftime() method to format the output.

Note that you'll need to have the pytz module installed in your environment to run this code. If you're using Python 3.9+, you can use the built-in zoneinfo module instead of pytz.

gistlibby LogSnag