0

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)
sruthi
  • 163
  • 9
  • Does this answer your question? [How can I convert windows timezones to timezones pytz understands?](https://stackoverflow.com/questions/16156597/how-can-i-convert-windows-timezones-to-timezones-pytz-understands) – Andrew Ryan Feb 07 '22 at 01:16
  • @AndrewRyan thanks for your reply i've tried but it didn't work for me. I'm trying to use timezone from windows system something like this "(UTC-08:00) Pacific Time (US & Canada) Pacific Standard Time ". – sruthi Feb 07 '22 at 01:21
  • @AndrewRyan although Windows has some "specialties", that is not a time zone name Windows uses (it uses just "India Standard Time" for IANA name "Asia/Calcutta"). – FObersteiner Feb 10 '22 at 13:39

1 Answers1

1

If we remove the try/except, the error is :

  File "/home/stack_overflow/so71012475.py", line 6, in get_uk_time
    country_tz = timezone(time_zone)
pytz.exceptions.UnknownTimeZoneError: '(UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi India Standard Time'

Because (UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi India Standard Time is not a correct identifier. I could not find which identifiers it corresponds to, but I can suggest another approach.

Because you know the UTC offset, you can create a timezone object with it, and use it to compute the UK time :

import datetime
import re

import pytz


# the data you provided
my_json = {'timezone': '(UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi India Standard Time'}

# a regex to extract relevant info from your data
pattern = re.compile(r"\(UTC(?P<sign>[+-])(?P<hours>\d\d):(?P<minutes>\d\d)\)(?P<name>.*)")

# extract the info
match = pattern.match(my_json["timezone"])
assert match is not None
tz_name = match.group("name").strip()
hours, minutes = int(match.group("hours")), int(match.group("minutes"))
is_negative = match.group("sign") == "-"

# build the timezone object
my_tz = datetime.timezone(offset=datetime.timedelta(hours=hours, minutes=minutes), name=tz_name)
if is_negative:
    my_tz = -my_tz

# use the timezone object to localize the current time
my_time = datetime.datetime.now().replace(tzinfo=my_tz)
print(my_time)

# and convert it to UK time
uk_tz = pytz.timezone('Europe/London')
uk_time = my_time.astimezone(uk_tz)
print(uk_time)
2022-02-07 14:33:12.463372+05:30
2022-02-07 09:03:12.463372+00:00

(it is actually 14:30 at my local time when I am writing this answer, and if I was in India, it would mean that it is 09:00 in London.

Lenormju
  • 4,078
  • 2
  • 8
  • 22