convert australian central daylight time to gmt in python

To convert Australian Central Daylight Time (ACDT) to Greenwich Mean Time (GMT) in Python, you can use the datetime module in combination with pytz.

Here is an example code snippet:

main.py
import datetime
import pytz

# Create a timezone object for ACDT
acdt_tz = pytz.timezone('Australia/Adelaide')

# Create a datetime object representing a specific time in ACDT
acdt_time = datetime.datetime(2021, 3, 10, 10, 0, 0, tzinfo=acdt_tz)

# Convert the ACDT time to GMT
gmt_time = acdt_time.astimezone(pytz.utc)

# Format the GMT time string
gmt_str = gmt_time.strftime('%Y-%m-%d %H:%M:%S.%f %Z%z')

print(f"ACDT time: {acdt_time}")
print(f"GMT time: {gmt_str}")
470 chars
18 lines

In this example, the timezone object for ACDT is created using the pytz.timezone() function. The datetime object representing a specific time in ACDT is created using the datetime.datetime() function, with the tzinfo argument set to the ACDT timezone object.

To convert this ACDT time to GMT, the astimezone() method is called on the ACDT datetime object, with the argument set to the pytz.utc timezone object. This converts the ACDT time to the corresponding time in the GMT timezone.

Finally, the strftime() method is called on the GMT datetime object to format the time string using a specific format string.

gistlibby LogSnag