convert samoa time to gmt in python

To convert a Samoa time to GMT in Python, we can make use of the datetime and timezone modules. The following code demonstrates this conversion:

main.py
from datetime import datetime
import pytz

# Set the input datetime in Samoa
input_date = datetime(2022, 5, 6, 12, 0, 0)
samoa_tz = pytz.timezone('Pacific/Samoa')
input_date = samoa_tz.localize(input_date)

# Convert the datetime to GMT
gmt_tz = pytz.timezone('GMT')
output_date = input_date.astimezone(gmt_tz)

# Print the output
print(output_date)
350 chars
15 lines

In this code, we first create a datetime object for the input time in Samoa. We then use the pytz module to convert this object to a timezone-aware object in the 'Pacific/Samoa' timezone.

Next, we create a timezone object for GMT and use the astimezone() method to convert the input datetime to GMT. Finally, we print the output datetime object.

gistlibby LogSnag