Search code examples
pythontimezonepytz

convert given time from a given timezone to UTC


I have two inputs time 00:00 and timezone 'Asia/Kolkata'

I want to convert this to UTC time like '18.30'

I don't want to add or subtract offsets because it may affect the day light saving

what i did is

 local = pytz.timezone ("UTC")
 nativetime = datetime.strptime (setTime,frmt)
 local_dt = local.localize(nativetime, is_dst=None)
 utc_dt = local_dt.astimezone(pytz.utc)

but this doesn't change anything, the time is not converted to UTC

Please help


Solution

  • Something like this, assuming you're on py3:

    >>> import datetime
    >>> import pytz
    >>> tz = pytz.timezone('Asia/Kolkata')
    >>> dt = datetime.datetime(2020, 8, 4, 0, 0, tzinfo=tz)
    >>> dt.astimezone(pytz.utc)
    datetime.datetime(2020, 8, 3, 18, 7, tzinfo=<UTC>)
    >>>