[ad_1]

i’m trying to convert “(UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi India Standard Time” to uk timzone. I found the solution using this “Asia/Kolkata” but I should either pass “(UTC+05:30)” or “Chennai, Kolkata, Mumbai, New Delhi” or “India Standard Time” to my function to convert it into uk time.

Here is my code:

from datetime import datetime
from pytz import timezone

def get_uk_time(time_zone):
    try:
        country_tz = timezone(time_zone)
        format = "%Y, %m, %d, %H, %M, %S"
        time_now = datetime.now(timezone(time_zone))
        format_time = time_now.strftime(format)
        london_tz = timezone('Europe/London')

        local_time = country_tz.localize(datetime.strptime(format_time, format))
        convert_to_london_time = local_time.astimezone(london_tz)
        uk_time = convert_to_london_time.strftime('%H:%M')
        return uk_time
    except:
        print('unable to find the timezone')

#my_json = {'timezone': 'Asia/Kolkata'}
my_json = {'timezone': '(UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi India Standard Time'}
time_zone = my_json['timezone']
time_in_london = get_uk_time(time_zone)
print(time_in_london)

[ad_2]