2014-04-13 5 views
-2

시간이 변경 될 때 시간대와 국가가 혼동 스럽습니다. Carlifornia는 오전 10시에 NewYork을 오전 7시에 변경합니다.프로그래밍 방식으로 시간을 변경하십시오.

캘리포니아에서 시간이 다음 번에 변경 될 때 프로그래밍 방식으로 정보를 얻을 수 있습니까? DST 전환을 찾으려면

+1

에서 [시간대 태그 위키]을 읽어 보시기 바랍니다 (http://stackoverflow.com/tags/timezone/info)와 [DST 태그 위키] (http://stackoverflow.com/tags/dst/info). 특정 프로그래밍 질문이있는 경우 사용중인 프로그래밍 언어, 달성하려는 시도 및 지금까지 시도한 내용과 같은 세부 정보를 제공해주십시오. –

+0

프로그래밍 언어를 설정하는 것을 잊었습니다. 'dst'는 나에게 새로운 것이었다. –

답변

4

, 파이썬에서, 다음 DST 전환의 시간을 찾기 위해, 예를 들어 Olson timezone database에 액세스 할 수 있습니다 :

#!/usr/bin/env python 
from bisect import bisect 
from datetime import datetime 
import pytz # $ pip install pytz 

def next_dst(tz):  
    dst_transitions = getattr(tz, '_utc_transition_times', []) 
    index = bisect(dst_transitions, datetime.utcnow()) 
    if 0 <= index < len(dst_transitions): 
     return dst_transitions[index].replace(tzinfo=pytz.utc).astimezone(tz) 

예를 들어, 로스 앤젤레스에서 :

dt = next_dst(pytz.timezone('America/Los_Angeles')) 
print(dt.strftime('%Y-%m-%d %H:%M:%S %Z%z')) 

출력 :

2014-11-02 01:00:00 PST-0800 

또는 로컬 시간대 :

from tzlocal import get_localzone # $ pip install tzlocal 

dt = next_dst(get_localzone()) 
if dt is not None: 
    print(dt.strftime('%Y-%m-%d %H:%M:%S %Z%z')) 
else: 
    print("no future DST transitions") 
+0

좋아요, 잘 작동합니다. –

관련 문제