Get the Olson TZ name for the local timezone?

前端 未结 11 1365
孤街浪徒
孤街浪徒 2020-11-30 03:19

How do I get the Olson timezone name (such as Australia/Sydney) corresponding to the value given by C\'s localtime call?

This is the value overridden vi

11条回答
  •  囚心锁ツ
    2020-11-30 03:23

    I changed tcurvelo's script to find the right form of time zone (Continent/..../City), in most of cases, but return all of them if fails

    #!/usr/bin/env python
    
    from hashlib import sha224
    import os
    from os import listdir
    from os.path import join, isfile, isdir
    
    infoDir = '/usr/share/zoneinfo/'
    
    def get_current_olsonname():
        result = []
        tzfile_digest = sha224(open('/etc/localtime').read()).hexdigest()
    
        test_match = lambda filepath: sha224(open(filepath).read()).hexdigest() == tzfile_digest
    
        def walk_over(dirpath):
            for root, dirs, filenames in os.walk(dirpath):
                for fname in filenames:
                    fpath = join(root, fname)
                    if test_match(fpath):
                        result.append(tuple(root.split('/')[4:]+[fname]))
    
        for dname in listdir(infoDir):
            if dname in ('posix', 'right', 'SystemV', 'Etc'):
                continue
            dpath = join(infoDir, dname)
            if not isdir(dpath):
                continue
            walk_over(dpath)
    
        if not result:
            walk_over(join(infoDir))
    
        return result
    
    
    if __name__ == '__main__':
        print get_current_olsonname()
    

提交回复
热议问题