Last active
August 29, 2015 14:13
-
-
Save martinsam/5517ab7e31e8df49d1a0 to your computer and use it in GitHub Desktop.
Get Timezone : Continent/Country
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| #http://stackoverflow.com/questions/7669938/get-the-olson-tz-name-for-the-local-timezone | |
| 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment