convert gmt to alaska time in python

To convert GMT (Greenwich Mean Time) to Alaska Time in Python, you need to use the datetime and pytz libraries. Here's an example code block that shows how to do the conversion:

main.py
import datetime
import pytz

# create a datetime object for the current time in GMT
gmt_time = datetime.datetime.now(pytz.timezone('GMT'))

# convert it to Alaska Time
alaska_time = gmt_time.astimezone(pytz.timezone('US/Alaska'))

# print the result
print("GMT Time: ", gmt_time.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
print("Alaska Time: ", alaska_time.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
386 chars
13 lines

In this example, we first create a datetime object for the current time in GMT using the pytz library. We then convert this datetime object to Alaska Time using the astimezone method and passing the timezone string 'US/Alaska'. Finally, we print the GMT time and Alaska time in a human-readable format using the strftime method.

gistlibby LogSnag