从本地转换为utc时区

时间:2011-06-16 19:05:26

标签: python timezone pytz

我正在尝试制作一个带有时间对象并将其转换为UTC时间的函数。下面的代码似乎关闭了一个小时。当我通过转换器中午运行时,我回到18:00:00。但是当我通过在线转换器运行相同的数据时,我得到17:00:00。

我在这里做错了什么?任何帮助将不胜感激。

import pytz, datetime

def convert_to_utc(time, tz):
    now_dt = datetime.datetime.utcnow()
    #get a date object
    date_dt = now_dt.date()
    #combine the current date object with our given time object
    dt = datetime.datetime.combine(date_dt, time)
    #get an timezone object for the source timezone
    src_tz = pytz.timezone(str(tz))
    #stamp the source datetime object with the src timezone 
    src_dt = dt.replace(tzinfo=src_tz)
    #get the offset from utc to given timezone
    offset = str(int(src_dt.strftime("%z"))).rstrip('0')
    #convert the source datetime object to
    utc_dt = src_dt.astimezone(pytz.utc)
    #return the converted time and the offset in integer format
    return (utc_dt.time(), int(offset))

time = datetime.datetime.strptime('12:00:00', "%H:%M:%S").time()
(TIME, offset) = convert_to_utc(time, 'America/Chicago')
print TIME.strftime("%H:%M:%S")

**编辑* *

这是更新的(和功能性)代码,以防其他人需要帮助转换为/从UTC转换。

感谢大家的帮助!

import pytz, datetime

def convert_to_utc(time, tz): #this returns the offset in int form as well
    now_dt = datetime.datetime.utcnow()
    #get a date object
    date_dt = now_dt.date()
    #combine the current date object with our given time object
    dt = datetime.datetime.combine(date_dt, time)
    #get an timezone object for the source timezone
    src_tz = pytz.timezone(str(tz))
    #stamp the source datetime object with the src timezone 
    src_dt = src_tz.localize(dt)
    #get the offset from utc to given timezone
    offset = str(int(src_dt.strftime("%z"))).rstrip('0')
    #convert the source datetime object to
    utc_dt = src_dt.astimezone(pytz.utc)
    #return the converted time and the offset in integer format
    return (utc_dt.time(), int(offset))

def convert_from_utc(time, tz):
    now_dt = datetime.datetime.now()
    date = now_dt.date()
    dt = datetime.datetime.combine(date, time)
    dest = pytz.timezone(str(tz))
    dt = dt.replace(tzinfo=pytz.utc)
    dest_dt = dt.astimezone(dest)
    return dest_dt.time()

time = datetime.datetime.strptime('12:00:00', "%H:%M:%S").time()
(TIME, offset) = convert_to_utc(time, 'America/Chicago')
print TIME.strftime("%H:%M:%S")

utc_time = datetime.datetime.strptime('17:00:00', "%H:%M:%S").time()
TIME = convert_from_utc(utc_time, 'America/Chicago')
print TIME.strftime("%H:%M:%S")

3 个答案:

答案 0 :(得分:8)

更改

src_dt = dt.replace(tzinfo=src_tz)

src_dt = src_tz.localize(dt)

使用localize调整夏令时,而replace则不会。 请参阅标题为“本地化时间和日期算术”的部分in the docs

答案 1 :(得分:2)

在日期时间使用replace方法,您不允许为夏令时调整时区。尝试使用pytz documentation中记录的方法之一:

src_dt = src_tz.localize(dt)

答案 2 :(得分:2)

将给定时区的时间转换为UTC时间:

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()

如果is_dstNone,则会针对不明确的当地时间引发异常。

在给定时区内将UTC时间转换为本地时间:

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()

实施例

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

一般来说,最好使用完整的datetime个对象,以避免对正确的日期产生歧义,即传递和返回datetime个对象。