4

時間オブジェクトを取得してUTC時間に変換する関数を作成しようとしています。以下のコードは1時間ずれているようです。コンバーターを正午に実行すると、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")
4

3 に答える 3

8

変化する

src_dt = dt.replace(tzinfo=src_tz)

src_dt = src_tz.localize(dt)

を使用localizeすると、夏時間が調整されますが、調整されreplaceません。ドキュメントの「ローカライズされた時間と日付の計算」というタイトルのセクションを参照してください。

于 2011-06-16T19:27:24.253 に答える
3

指定されたタイムゾーンの時刻を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です。

于 2012-10-03T10:09:20.147 に答える
2

日時にこの方法を使用するreplaceと、夏時間に合わせてタイムゾーンを調整できなくなります。pytzのドキュメントに記載されている方法の1つを使用してみてください。

src_dt = src_tz.localize(dt)
于 2011-06-16T19:30:17.587 に答える