Is a specific timezone using DST right now?

前端 未结 1 1341
闹比i
闹比i 2020-12-06 17:06

How would I get my python script to check whether or not a specific timezone that is stored in a variable using DST right now? My server is set to UTC. So I have say for in

1条回答
  •  囚心锁ツ
    2020-12-06 17:32

    from pytz import timezone
    from datetime import datetime
    
    zonename = "Pacific/Wallis"
    now = datetime.now(tz=timezone(zonename))
    dst_timedelta = now.dst()
    ### dst_timedelta is offset to the winter time, 
    ### thus timedelta(0) for winter time and timedelta(0, 3600) for DST; 
    ### it returns None if timezone is not set
    
    print "DST" if dst_timedelta else "no DST"
    

    alternative is to use:

    now.timetuple().tm_isdst 
    

    Which can have one of 3 values: 0 for no DST, 1 for DST and -1 for timezone not set.

    0 讨论(0)
提交回复
热议问题