簡體   English   中英

從本地轉換為utc時區

[英]converting from local to utc timezone

我正在嘗試制作一個帶有時間對象並將其轉換為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")

更改

src_dt = dt.replace(tzinfo=src_tz)

src_dt = src_tz.localize(dt)

使用localize調整夏令時,而replace則不。 請參閱文檔中標題為“本地化時間和日期算術” 的部分

要將給定時區的時間轉換為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對象)的含糊不清。

通過在日期時間使用replace方法,您不允許為夏令時調整時區。 嘗試使用pytz文檔中記錄的方法之一:

src_dt = src_tz.localize(dt)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM