【问题标题】:Python - Check using UTC timestamp if it is DST in another timezonePython - 使用 UTC 时间戳检查它是否是另一个时区的 DST
【发布时间】:2020-03-05 13:47:36
【问题描述】:

我有一个 UTC 时间戳,我想检查当时是否是伦敦的夏令时。我将如何在 Python 中做到这一点?

【问题讨论】:

    标签: python python-3.x datetime time pytz


    【解决方案1】:

    您可以使用 os.environ 来设置您的时区。以下是在两个不同时区检查 DST 的示例:

    import time, os
    
    os.environ['TZ'] = 'Europe/London'                                                                                                                                                      
    
    timestamp = os.path.getmtime(filename) 
    isdst = time.localtime(timestamp).tm_isdst > 0                                                                                                                                          
    
    
    In [973]: timestamp                                                                                                                                                                               
    Out[973]: 1571900789.0347116
    
    In [965]: isdst                                                                                                                                                                                   
    Out[965]: True
    
    os.environ['TZ'] = 'USA/Colorado'                                                                                                                                                        
    
    timestamp = os.path.getmtime(filename) 
    isdst = time.localtime(timestamp).tm_isdst > 0                                                                                                                                          
    
    In [968]: isdst                                                                                                                                                                                   
    Out[968]: False
    

    【讨论】:

    • 什么是filename!?
    • 另外,改变环境时区也很重要。
    猜你喜欢
    • 1970-01-01
    • 2018-08-29
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    • 2021-05-14
    • 2013-05-07
    • 2015-04-20
    相关资源
    最近更新 更多