convert central time to gmt in python

You can use the pytz library to convert a datetime from one timezone to another. Here's an example of how to convert Central Time (US & Canada) to GMT:

main.py
import datetime
import pytz

# Create a datetime with Central Timezone
ct = datetime.datetime.now(pytz.timezone('US/Central'))

# Convert to GMT timezone
gmt = pytz.timezone('GMT')
gmt_time = ct.astimezone(gmt)

print("Central Time: ", ct.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
print("GMT: ", gmt_time.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
335 chars
13 lines

In this example, datetime.datetime.now(pytz.timezone('US/Central')) creates a new datetime object with the current date and time in the US Central Timezone. Then, we convert it to GMT timezone using ct.astimezone(gmt) and store the result in gmt_time. Finally, we print both datetimes with their respective timezone codes.

Note that you need to install pytz library before using it. You can install it via pip by running $ pip install pytz in your terminal.

gistlibby LogSnag