Get time zone information of the system in Python?

前端 未结 8 1129
情歌与酒
情歌与酒 2020-12-02 11:40

I want to get the default timezone (PST) of my system from Python. What\'s the best way to do that? I\'d like to avoid forking another process.

8条回答
  •  孤城傲影
    2020-12-02 12:37

    Getting offset from UTC as timedelta:

    from datetime import datetime, timezone
    
    now = datetime.now()
    now.replace(tzinfo=timezone.utc) - now.astimezone(timezone.utc)
    

    Or like this (more obscure but also works):

    datetime.now(timezone.utc).astimezone().tzinfo.utcoffset(None)
    

    Both solutions give the same result. For example: datetime.timedelta(seconds=7200)

提交回复
热议问题