Use the pytz module, which comes with a full list of time zones + UTC. Figure out what the local timezone is, construct a timezone
object from it, and manipulate and attach it to the iso_format
datetime
.
Source code, using local timezone Asia/Kolkata
, for the string 2020-7-23 10:11:12
:
import pytz, datetime
time_zone = pytz.timezone("Asia/Kolkata")
iso_format = datetime.datetime.strptime("2020-7-23 10:11:12", "%Y-%m-%d %H:%M:%S")
local_date_time = time_zone.localize(iso_format, is_dst=None)
utc_date_time = local_date_time.astimezone(pytz.utc)
To convert it to UTC timing, you need to do this:
final_data = utc_date_time.strftime("%Y-%m-%d %H:%M:%S")
print(final_data)
# OUTPUT
# >>> 2020-07-23 04:41:12
ADD
To find your specified timezone, you can do this via pytz
only. Just use the below code
>>> pytz.all_timezones
>>> ['Africa/Abidjan',
'Africa/Accra',
'Africa/Addis_Ababa',
...]
Hope that answers your question