convert gmt to australian central time in python

To convert GMT to Australian Central Time (ACT) in python, you first need to create a datetime object for the given time in GMT timezone. Then, you can convert this datetime object to the ACT timezone using the astimezone() method.

Here's an example code snippet to convert GMT to ACT in python:

main.py
from datetime import datetime
import pytz  # This is the library for timezone information

# Define the UTC timezone
utc = pytz.UTC

# Define the ACT timezone
act = pytz.timezone('Australia/Adelaide')

# Define the datetime object for the given time in GMT timezone
gmt_time = datetime.strptime('2021-08-23 08:30:00', '%Y-%m-%d %H:%M:%S')
gmt_time = utc.localize(gmt_time)

# Convert the datetime to ACT timezone
act_time = gmt_time.astimezone(act)

# Print the result
print('GMT Time: ', gmt_time)
print('ACT Time: ', act_time)
529 chars
20 lines

In this example, we first import datetime and pytz libraries. We define the UTC timezone object using pytz.UTC, and the ACT timezone object using pytz.timezone('Australia/Adelaide').

Next, we define the datetime object for the given time in GMT timezone using datetime.strptime(). We then localize the datetime object to UTC timezone using utc.localize().

Finally, we convert the datetime object to ACT timezone using astimezone(). We print the GMT and ACT time using print() function.

Note that you may need to change the ACT timezone based on the location within Australia as it follows various time zones.

gistlibby LogSnag