Python: Figure out local timezone

前端 未结 18 1685
野性不改
野性不改 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 15:58

    Here's a slightly more concise version of @vbem's solution:

    from datetime import datetime as dt
    
    dt.utcnow().astimezone().tzinfo
    

    The only substantive difference is that I replaced datetime.datetime.now(datetime.timezone.utc) with datetime.datetime.utcnow(). For brevity, I also aliased datetime.datetime as dt.

    For my purposes, I want the UTC offset in seconds. Here's what that looks like:

    dt.utcnow().astimezone().utcoffset().total_seconds()
    

提交回复
热议问题