converting from local to utc timezone

后端 未结 3 569
天命终不由人
天命终不由人 2021-02-10 19:38

I\'m attempting to craft a function that takes a time object and converts it to UTC time. The code below appears to be off by one hour. When i run noon through the converter,

3条回答
  •  温柔的废话
    2021-02-10 20:21

    To convert time in given timezone to UTC time:

    from datetime import datetime
    import pytz
    
    def convert_to_utc(time, tzname, date=None, is_dst=None):
        tz = pytz.timezone(tzname)
        if date is None: # use date from current local time in tz
            date = datetime.now(tz).date()
    
        dt = tz.localize(datetime.combine(date, time), is_dst=is_dst)
        return dt.astimezone(pytz.utc).time(), dt.utcoffset().total_seconds()
    

    if is_dst is None it raises an exception for ambiguous local times.

    To convert UTC time to local time in given timezone:

    def convert_from_utc(time, tzname, date=None):
        tz = pytz.timezone(tzname)
        if date is None: # use date from current time in utc
            date = datetime.utcnow().date()
        dt = datetime.combine(date, time).replace(tzinfo=pytz.utc)
        return tz.normalize(dt.astimezone(tz)).time()
    

    Example:

    time = datetime.strptime('12:00:00', "%H:%M:%S").time()
    utc_time, offset = convert_to_utc(time, 'America/Chicago')
    print utc_time.strftime("%H:%M:%S"), offset # -> 17:00:00 -18000.0
    
    utc_time = datetime.strptime('17:00:00', "%H:%M:%S").time()
    time = convert_from_utc(utc_time, 'America/Chicago')
    print time.strftime("%H:%M:%S") # -> 12:00:00
    

    In general it is preferable to work with full datetime objects to avoid ambiguity with what is the correct date i.e., pass and return datetime objects.

提交回复
热议问题