python으로 datetime을 주어진 시간대에서 EST로 변환하는 다음 스크립트를 작성했습니다. 제가 convert2EST이 메소드를 호출 할 때python을 사용하는 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
는 ('20110220', '11 : 00 : 00 ','아메리카/상파울루 ') 2011-02-20 8시
출력은' 00 EST-0500 '이지만 브라질의 DST는 2 월 20 일에 끝났으며 정답은'2011-02-20 09:00:00 EST-0500 '이어야합니다.
일부 실험에서 필자는 브라질의 DST가 pythz에 따르면 2 월 27 일에 끝난다는 사실을 알아 냈습니다.
pytz에 잘못된 데이터가 포함되어 있거나 누락되었습니다. 어떤 도움이나 의견을 주시면 감사하겠습니다.
문자열을 int로 변환하려면 내장 된 int 함수를 사용해야합니다. – sdolan
및 strptime을 사용하여 날짜로 변환하십시오. –