Python: Figure out local timezone

前端 未结 18 1631
野性不改
野性不改 2020-12-07 15:50

I want to compare UTC timestamps from a log file with local timestamps. When creating the local datetime object, I use something like:

>>&         


        
18条回答
  •  春和景丽
    2020-12-07 16:17

    The following appears to work for 3.7+, using standard libs:

    from datetime import timedelta
    from datetime import timezone
    import time
    
    def currenttz():
        if time.daylight:
            return timezone(timedelta(seconds=-time.altzone),time.tzname[1])
        else:
            return timezone(timedelta(seconds=-time.timezone),time.tzname[0])
    

提交回复
热议问题