日時を任意のタイムゾーンから EST に変換するために、Python で次のスクリプトを作成しました。
from datetime import datetime, timedelta
from pytz import timezone
import pytz
utc = pytz.utc
# Converts char representation of int to numeric representation '121'->121, '-1729'->-1729
def toInt(ch):
ret = 0
minus = False
if ch[0] == '-':
ch = ch[1:]
minus = True
for c in ch:
ret = ret*10 + ord(c) - 48
if minus:
ret *= -1
return ret
# Converts given datetime in tzone to EST. dt = 'yyyymmdd' and tm = 'hh:mm:ss'
def convert2EST(dt, tm, tzone):
y = toInt(dt[0:4])
m = toInt(dt[4:6])
d = toInt(dt[6:8])
hh = toInt(tm[0:2])
mm = toInt(tm[3:5])
ss = toInt(tm[6:8])
# EST timezone and given timezone
est_tz = timezone('US/Eastern')
given_tz = timezone(tzone)
fmt = '%Y-%m-%d %H:%M:%S %Z%z'
# Initialize given datetime and convert it to local/given timezone
local = datetime(y, m, d, hh, mm, ss)
local_dt = given_tz.localize(local)
est_dt = est_tz.normalize(local_dt.astimezone(est_tz))
dt = est_dt.strftime(fmt)
print dt
return dt
このメソッドを convert2EST('20110220', '11:00:00', 'America/Sao_Paulo') で呼び出すと、
出力は「2011-02-20 08:00:00 EST-0500」ですが、ブラジルの DST は 2 月 20 日に終了したため、正解は「2011-02-20 09:00:00 EST-0500」である必要があります。
いくつかの実験から、pytz によると、ブラジルの DST は 2 月 27 日に終了することがわかりましたが、これは正しくありません。
pytz に間違ったデータが含まれていますか、それとも何か不足していますか? ヘルプやコメントは大歓迎です。