Get time zone information of the system in Python?

前端 未结 8 1130
情歌与酒
情歌与酒 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条回答
  •  旧时难觅i
    2020-12-02 12:34

    The code snippets for calculating offset are incorrect, see http://bugs.python.org/issue7229.

    The correct way to handle this is:

    def local_time_offset(t=None):
        """Return offset of local zone from GMT, either at present or at time t."""
        # python2.3 localtime() can't take None
        if t is None:
            t = time.time()
    
        if time.localtime(t).tm_isdst and time.daylight:
            return -time.altzone
        else:
            return -time.timezone
    

    This is in all likelihood, not the exact question that the OP asked, but there are two incorrect snippets on the page and time bugs suck to track down and fix.

提交回复
热议问题